Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+Partial consolidation of netcdf calls in MOM_io #1311

Merged
merged 11 commits into from
Feb 4, 2021

Conversation

Hallberg-NOAA
Copy link
Collaborator

Took preliminary steps to consolidate the direct calls to netcdf in
MOM_io.F90. Renamed check_grid_def to verify_variable_units and moved it to
MOM_io, where it can be used more widely than just in MOM_regridding.F90. Also
split get_var_sizes and get_varid out of num_timelevels and made them publicly
visible. All answers are bitwise identical, but there are new public interfaces
and the renaming of one routine.

  Took preliminary steps to consolidate the direct calls to netcdf in
MOM_io.F90. Renamed check_grid_def to verify_variable_units and moved it to
MOM_io, where it can be used more widely than just in MOM_regridding.F90.  Also
split get_var_sizes and get_varid out of num_timelevels and made them publicly
visible.  All answers are bitwise identical, but there are new public interfaces
and the renaming of one routine.
  Modified get_var_sizes to have the root_PE do the reading and then broadcast
this information to the other processors, unless directed not to via the new
optional argument all_read.  Part of this involved splitting read_var_sizes out
from get_var_sizes.  Also added the new optional argument alt_units to
verify_variable_units, to give some flexibility when checking units without hard
coding the "meters" to "m" comparison within verify_variable_units.
  When the field_chksum interfaces was added, it returned integers, but the set
of mpp_chksum routines from FMS it wraps return 64-bit integers, not 32-bit
integers. For small domain sizes (like in the TC tests) this is not a problem,
but for much larger problems (like tides_025), this results in a truncation of
the results and a change in the checksums, which in turn causes the model to
fail if it tries to read a depth-list file.  This commit restores the model
behavior prior to MOM6 PR#1299 (commit# 797b195..).  All answers are bitwise
identical in cases that worked with the previous version of the code, but some
depth-list files that were created with MOM6 code after that commit may have
incorrect verification checksums that lead to spurious fatal errors and should
be deleted.
  Added the new set of routines read_variable to do a simple read of an entire
array from a netCDF file with the root PE, and then broadcast this information
to the other PEs.  Also added read_attribute to read a global or variable
attribute.  The option to read dimension names was added to get_var_sizes, and
the option for get_varid to indicate whether a variable has been found. As a
part of this, separate variants of broadcast were added for 32-bit and 64-bit
integers, as were new variants of MOM_read_data for 1-d and 0-d integer fields.
All answers are bitwise identical, but there are new options and optional
arguments for overloaded interface, and there are two new overloaded interfaces
for reading.
  Replaced the calls to netCDF in read_depth_list with calls to routines in
MOM_io.F90 that accomplish the same thing.  Also added trim calls to a number
of error messages in write_depth_list_file.  All answers and output are bitwise
identical.
  Eliminated the direct netCDF calls in MOM_shared_initialization, replacing
them with calls to routines in MOM_io.  All answers are bitwise identical.
  Eliminated the direct netCDF calls in read_Z_edges in MOM_tracer_Z_init,
replacing them with calls to routines in MOM_io.  All answers are bitwise
identical.
  Added the missing comments describing the new interface read_attribute.  All
answers are bitwise identical.
@klindsay28
Copy link
Contributor

@Hallberg-NOAA, note that the netCDF library doesn't check for adequate space when reading in attributes. The netCDF library will happily write beyond the end of your array if it is not long enough. This is a pain to debug when it happens. I've yet to see a compiler that catches this, I suspect because the underlying read is happening in C. I think you are more likely to get bitten with char/string attributes than other types, because non-char attributes tend to be scalars in typical netCDF files, not arrays.

I suggest comparing the attribute length to the size of att_val in read_attribute_* before calling NF90_get_att, particularly for read_attribute_str.

Code in the CESM fork of POP that does this for char attributes is at https://github.com/ESCOMP/POP2-CESM/blob/9f2a48328dda6bd98be74f4ccfc13248b1b13868/source/io_netcdf.F90#L232-L258. Note that this code is using the pio API, not the netCDF API. But the issues are the same.

  Modified read_attribute_str to work with allocatable character string
arguments to properly handle string attributes of unknown (and possibly
enormous) size, following advice from Keith Lindsay.  Because this is a change
in an argument to a (recently added) publicly visible interface, it required
changes to the two places where it was called.  All answers are bitwise
identical, but an interface has changed.
@codecov
Copy link

codecov bot commented Feb 1, 2021

Codecov Report

Merging #1311 (f87e4ea) into dev/gfdl (1b4f41c) will increase coverage by 1.73%.
The diff coverage is 2.43%.

Impacted file tree graph

@@             Coverage Diff              @@
##           dev/gfdl    #1311      +/-   ##
============================================
+ Coverage     44.05%   45.78%   +1.73%     
============================================
  Files           234      234              
  Lines         72269    72460     +191     
============================================
+ Hits          31837    33177    +1340     
+ Misses        40432    39283    -1149     
Impacted Files Coverage Δ
src/ALE/MOM_regridding.F90 31.68% <0.00%> (+1.01%) ⬆️
src/diagnostics/MOM_sum_output.F90 67.29% <0.00%> (+3.40%) ⬆️
src/framework/MOM_io.F90 30.00% <0.00%> (-23.04%) ⬇️
src/framework/MOM_io_infra.F90 35.67% <0.00%> (+7.69%) ⬆️
src/tracer/MOM_tracer_Z_init.F90 0.00% <0.00%> (ø)
src/initialization/MOM_shared_initialization.F90 41.45% <12.50%> (+7.39%) ⬆️
src/framework/MOM_coms_infra.F90 59.42% <80.00%> (+2.49%) ⬆️
...c/parameterizations/vertical/MOM_vert_friction.F90 74.35% <0.00%> (+0.11%) ⬆️
src/framework/MOM_file_parser.F90 72.00% <0.00%> (+0.12%) ⬆️
... and 26 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1b4f41c...1b59d70. Read the comment docs.

  Added the option to provide a handle to an open netCDF file via the new
ncid_in optional arguments to various routines that read from netCDF files.
Along with the new interfaces open_file_to_read and close_file_to read in MOM_io
these allow for files to be opened and closed once while reading a number of
fields or attributes from the same file.  If these arguments are not provided,
the files are opened and closed with each call as before.  All answers are
bitwise identical, although there are new optional arguments and new public
interfaces.
@Hallberg-NOAA
Copy link
Collaborator Author

Added more commits to PR#1311 to further concentrate I/O calls in MOM_io.F90, and to respond
to comments on these capabilities. These additional commits are:

With these added commits, I consider PR#1311 to be complete, and have no plans for any additions.

@Hallberg-NOAA
Copy link
Collaborator Author

This PR has passed pipeline testing at https://gitlab.gfdl.noaa.gov/ogrp/MOM6/-/pipelines/12001.

@marshallward marshallward merged commit dbf6f56 into mom-ocean:dev/gfdl Feb 4, 2021
@Hallberg-NOAA Hallberg-NOAA deleted the consolidate_netcdf branch July 30, 2021 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants