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

Rewrite horizontal regridding to use netCDF wrapper functions #48

Merged
merged 12 commits into from
Jan 8, 2022

Conversation

marshallward
Copy link
Member

This patch replaces the explicit netCDF functions inside the horizontal remapping with generic wrapper functions.

It also resolves some issues with axes_info creation, and introduces a new read_variable netCDF wrapper function.

The read_variable function does not support data decomposition, and

This PR was authored by @MJHarrison-GFDL with modifications by myself, and I am submitting on his behalf.

MJHarrison-GFDL and others added 6 commits October 25, 2021 10:22
This patch sets the following netCDF attributes in the function
`horiz_interp_and_extrap_tracer_record` via `read_attribute`.

* `missing_value` (as `_FillValue`)
* `scale_factor`
* `add_offset`

This resolves some issues related to uninitialized values.
Set netCDF attrs in MOM_horizontal_regridding
This patch extends the `read_variable` interface to include 2d array
support, in order to facilitate domainless I/O via netCDF calls.

This is far from the best implementation (e.g. read_variable_2d
introduces another `broadcast` alongside the original one in the
horizontal regridding) but it addresses the immediate issues with
`MOM_read_data()`.
@codecov
Copy link

codecov bot commented Dec 21, 2021

Codecov Report

Merging #48 (ecfa52c) into dev/gfdl (df46be4) will increase coverage by 0.12%.
The diff coverage is 62.39%.

Impacted file tree graph

@@             Coverage Diff              @@
##           dev/gfdl      #48      +/-   ##
============================================
+ Coverage     28.82%   28.95%   +0.12%     
============================================
  Files           242      242              
  Lines         71495    71557      +62     
============================================
+ Hits          20612    20717     +105     
+ Misses        50883    50840      -43     
Impacted Files Coverage Δ
src/framework/MOM_horizontal_regridding.F90 35.11% <59.25%> (-2.51%) ⬇️
src/framework/MOM_io.F90 33.26% <63.33%> (+10.49%) ⬆️
config_src/infra/FMS1/MOM_coms_infra.F90 40.56% <0.00%> (+4.02%) ⬆️

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 df46be4...ecfa52c. Read the comment docs.

Copy link
Member

@Hallberg-NOAA Hallberg-NOAA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TC4 test case is giving qualitatively different answers from what was there before. This change in the solution needs to be corrected or explained before these proposed code changes could be accepted.

if (rc /= NF90_NOERR) call MOM_error(FATAL, hdr // trim(nf90_strerror(rc)) //&
" Difficulties reading "//trim(varname)//" from "//trim(filename))

if (size(start) /= ndims .or. size(nread) /= ndims) then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that there a several problems with this test.

  1. Because the start and nread arguments are optional, there needs to be test for whether they are present to wrap the test for whether they have the right size. Otherwise, this code as written could lead to a segmentation fault.
  2. This error handling block should probably be moved up 6 lines, so that it occurs before start and nread are used in the call to nf90_get_var.
  3. The fact that this code appears to work, suggests that perhaps start and nread are not optional after all?
  4. ndims is a property of an input file, whereas the sizes of start and nread are effectively hard coded in the call to read_variable. Could there be a case where it would be appropriate to set start and nread so that this routine could, for example, read either from a 2-d file or read a single level from a 3-d file into a 2-d array, depending on what is in the file that is being read? If so, issuing an fatal error might not be the right answer.

Could this work properly if the tests were changed to if (present(start) .and. present(nread)) then ; if ((size(start) < ndims) .or. size(nread) < ndims) then, and then the call to nf90_get_var were changed to
if (present(start) .and. present(nread) then call nf90_get_var(ncid, varid, var, start(1:ndims), nread(1:ndims)) else call nf90_get_var(ncid, varid, var) endif

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional arguments are pass-through, so the call to nf90_get_var does not need to be changed. But I agree with the general conclusions here.

" Difficulties reading "//trim(varname)//" from "//trim(filename))

! NOTE: We could check additional information here (type, size, ...)
rc = nf90_get_var(ncid, varid, var, start, nread)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing this to something like:

Suggested change
rc = nf90_get_var(ncid, varid, var, start, nread)
if (present(start) .and. present(nread)) then
if ((size(start) < ndims) .or. (size(nread) < ndims)) call MOM_error(FATAL, &
"read_variable_2d: start or nread are not large enough to read "//trim(varname)//" from "//trim(filename))
rc = nf90_get_var(ncid, varid, var, start(1:ndims), nread(1:ndims))
else
rc = nf90_get_var(ncid, varid, var)
endif

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to support reading 2d slices in a 3d+ field, I would prefer stronger dimensional testing.

In truth, I don't think we should be encouraging use of this function at all. It does not support parallel I/O and problems at high resolution are inevitable.

This patch modifies read_variable_2d so that the size() tests of the
optional arguments are applied before the call to nf90_get_var.

The tests are also wrapped inside present() flags to avoid checking
unassigned variables.

Thanks to Robert Hallberg for the suggestions.
@marshallward
Copy link
Member Author

marshallward commented Jan 4, 2022

I added the present() checks for the ndims testing, but did not implement the slicing support.

I would suggest we defer this feature until it is needed somewhere. As alluded to in the comment, this is already an inefficient and potentially unsafe function, and I don't think we want to encourage its use with additional features. Its only real purpose is backwards compatibility with explicit netCDF calls in legacy code.

@MJHarrison-GFDL
Copy link

I believe we should be able to deprecate this function after transitioning to FMS2_io so it should be considered only for legacy support.

@Hallberg-NOAA
Copy link
Member

I am perhaps less confident than others on the advisability of relying on FMS to read or write small, non-domain decomposed files if we do not have to, given our history with the FMS development team not always being as careful as we would like about respecting backward compatibility. The purpose of this commit was to consolidate all existing direct NetCDF calls within the MOM6 code into a single module, and it succeeds in doing so. With the changes that have been made, I am now more confident that we will not be generating unexplained segmentation faults or other errors. This PR has passed the TC testing, and it should be accepted (via a squash merge) once the pipeline testing has passed.

When this PR is accepted, we should finally be able to close mom-ocean#1312.

@Hallberg-NOAA
Copy link
Member

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

@Hallberg-NOAA Hallberg-NOAA merged commit f7a2254 into NOAA-GFDL:dev/gfdl Jan 8, 2022
@Hallberg-NOAA Hallberg-NOAA added enhancement New feature or request refactor Code cleanup with no changes in functionality or results labels Feb 1, 2022
@marshallward marshallward deleted the hor_regrid branch April 21, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactor Code cleanup with no changes in functionality or results
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants