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

Update cice.t-test.py to use cartopy instead of basemap. #727

Closed
wants to merge 5 commits into from

Conversation

daveh150
Copy link
Contributor

@daveh150 daveh150 commented Jun 2, 2022

For detailed information about submitting Pull Requests (PRs) to the CICE-Consortium,
please refer to: https://github.com/CICE-Consortium/About-Us/wiki/Resource-Index#information-for-developers

PR checklist

  • Short (1 sentence) summary of your PR:
    Update cice.t-test.py to use cartopy instead of basemap.

  • Developer(s):
    David Hebert

  • Suggest PR reviewers from list in the column to the right.

  • Please copy the PR test results link or provide a summary of testing completed below.

  • How much do the PR code changes differ from the unmodified code?

    • [ x] bit for bit
    • different at roundoff level
    • more substantial
  • Does this PR create or have dependencies on Icepack or any other models?

    • Yes
    • [x ] No
  • Does this PR add any new test cases?

    • Yes
    • [ x] No
  • Is the documentation being updated? ("Documentation" includes information on the wiki or in the .rst files from doc/source/, which are used to create the online technical docs at https://readthedocs.org/projects/cice-consortium-cice/. A test build of the technical docs will be performed as part of the PR testing.)

    • [ x] Yes
    • No, does the documentation need to be updated at a later time?
      • Yes
      • [ x] No
  • Please provide any additional information or relevant details below:

@eclare108213
Copy link
Contributor

@daveh150
These are fairly extensive changes -- it took some work to do this. Thanks! Do you have sample output, before and/or at least after?

For future reference (and you can edit your text above) - deleting the space from the checkbox code when you add an x will make them work... [x] instead of [ x] etc.

@daveh150
Copy link
Contributor Author

daveh150 commented Jun 2, 2022

Thank you. The contour plot section was the challenging part, needing both +/- 180 and 0-360 lat/lons.

I admittedly did not test the plot in this exact script, I do not have a perturbation run to test with (I likely missed how to set that up on the testing documentation). I was testing plots outside of this script using output from one the 'smoke' tests. If anyone has suggestion on how to make a perturbation test I can test this.

@daveh150
Copy link
Contributor Author

daveh150 commented Jun 2, 2022

Oh, I think I see why I couldn't run a QC test before. Previously I got a message that my test and baseline were bit-for-bit, so no qc test was required. But I did not make any changes to the ice_in, so the runs were identical. I'll try to change the shortwave and ktherm in the 'test' run and see if I can run the QC test to get example plots from this script. Sorry about that.

@daveh150
Copy link
Contributor Author

daveh150 commented Jun 3, 2022

Does anyone use the contour option in these plots? That is by far the most challenging, the scatter and pcolor seem to do what we want. I'd like to just remove the contour plots if they aren't used.

@eclare108213
Copy link
Contributor

I didn't know that contour plotting was even available. As far as I'm concerned, color maps should be sufficient and you can comment the contouring part out. (Am I correctly understanding the issue here?) Check the docs to see if it's mentioned, and if so then comment that out too. Thx

@apcraig
Copy link
Contributor

apcraig commented Jun 3, 2022

This looks fine to me, I'll try to run a t-test on cheyenne and make sure everything is working to independently confirm. I've struggled at times getting the python environment setup, has anything changed in that realm?

@daveh150
Copy link
Contributor Author

daveh150 commented Jun 3, 2022

@apcraig The only update to your python environment I can think of is the need for the cartopy package.

Here are results I obtained with gx3 case using the scatter and pcolor options.
ice_thickness_nrlssc_gnu_smoke_gx3_20x1_qc qc_base
ice_thickness_nrlssc_gnu_smoke_gx3_20x1_qc qc_base_minus_nrlssc_gnu_smoke_gx3_20x1_qc qc_te

ice_thickness_nrlssc_gnu_smoke_gx3_20x1_qc qc_base
ice_thickness_nrlssc_gnu_smoke_gx3_20x1_qc qc_base_minus_nrlssc_gnu_smoke_gx3_20x1_qc qc_te

The contour plot is below, I am having a very difficult time getting rid of the space in the Northern Hemisphere plot. I think it is related to contour lines crossing +/- 180 but I can't seem to fix it.

ice_thickness_nrlssc_gnu_smoke_gx3_20x1_qc qc_base

@apcraig
Copy link
Contributor

apcraig commented Jun 3, 2022

Thanks @daveh150, those plots look pretty good overall. No idea about the missing data in the contour plots.

… is selected it will instead make a pcolor plot
@daveh150
Copy link
Contributor Author

daveh150 commented Jun 3, 2022

I made an update where if contour is selected, the plots will revert to pcolor. Eventually that whole section of the code could be removed once we are sure no one wants to use it. It cleans up the plotting code significantly. I did not see a need to add the cyclic arrays with the pcolormesh.

Copy link
Member

@phil-blain phil-blain left a comment

Choose a reason for hiding this comment

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

I took a quick look to the changes and this looks good, thanks @daveh150 !

One thing I would suggest is to add cartopy to the conda environment specification file (environment.yml under configuration/scripts/machines) and remove basemap from there.

@dabail10
Copy link
Contributor

dabail10 commented Jun 10, 2022

Just got a chance to look at this. This is great! Thanks @daveh150 for doing this. Here are the plots from my gx3 B vs C grid comparison. A couple of notes for enhancements later.

  1. This does seem to be in the right sense of the differences I would expect. That is the B grid solution is thinner.
  2. However, It would be nice if the colorbar could be reset a bit to bring out the differences. The "red" area is basically zero everywhere and so a red-white-blue colormap might be nice for the difference plot. Then instead of having the maximum distance as the colorbar, maybe a one-sigma difference is better. That is computing the spatial variance of the distances and using that as the max on the colorbar instead of the absolute max difference. Perhaps even two-sigma. This would help bring out the smaller differences better.
  3. Also, it would be nice if the colorbar for the two cases was the same. This way one could see the differences pretty clearly in these plots. Or we could always use a max of 5m in the NH and 2m in the SH. Similarly in the difference plot, we could always use a min/max of -1/1m.

ice_thickness_gx3testb
ice_thickness_gx3testc
ice_thickness_gx3testb_minus_gx3testc

Copy link
Contributor

@dabail10 dabail10 left a comment

Choose a reason for hiding this comment

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

This looks great. I added some additional comments for enhancements.

@dabail10
Copy link
Contributor

I made an update where if contour is selected, the plots will revert to pcolor. Eventually that whole section of the code could be removed once we are sure no one wants to use it. It cleans up the plotting code significantly. I did not see a need to add the cyclic arrays with the pcolormesh.

I agree that it is probably not worth chasing down the issues with the contour plots. Typically we don't see issues like this except on tripolar grids. It also looks like the line of missing values goes along the 90E/W line. It would make more sense if it was related to 180/0. I recall that Matt had designed it so that one could choose in case the pcolor doesn't work for some reason. What happens if you don't use filled contours?

@apcraig
Copy link
Contributor

apcraig commented Jun 13, 2022

@dabail10, thanks for reviewing and testing the scripts. @daveh150, Are we ready to merge? It seems like the outstanding requests may be

  • update environment.yml, add cartopy, remove basemap
  • update the colorbar

I think those are two good suggestions. If we prefer to defer, I'm also OK merging.

@apcraig
Copy link
Contributor

apcraig commented Jun 21, 2022

@daveh150, just checking in again to see how we should proceed.

@daveh150
Copy link
Contributor Author

daveh150 commented Jun 21, 2022 via email

@daveh150
Copy link
Contributor Author

Thanks for your patience while I catch up from being out the past two weeks.

@dabail10 I took you suggestion and added individual colormaps to each plot. For the NH/SH data I hardwired the limits to 0-5 in NH, 0-2 in SH. happy to change that if needed. Also, for the difference plot I added blue/white/red colormap and determine the range from the absolute value of the data. Could you please see how this works for you?

@phil-blain I modifies environment.yml to remove basemap and add cartopy.

@apcraig
Copy link
Contributor

apcraig commented Jun 23, 2022

Thanks @daveh150. @dabail10, do you have some QC data you can run the updated scripts on to verify they work and look good? If not, I will try to generate some data and do the same. thanks!

@dabail10
Copy link
Contributor

I do have some runs I can test this on.

@dabail10
Copy link
Contributor

These are great! It is great to see such a large area of white where the differences are close to zero. We might even be able to set the min/max on the difference plots to even smaller, but this is great for now.

ice_thickness_gx1testb_minus_gx1testc
ice_thickness_gx1testc
ice_thickness_gx1testb

@phil-blain
Copy link
Member

phil-blain commented Jun 28, 2022

That might be something on my side, but with the script in this PR I get plots like this;

image

I haven't yet looked into why it's all blue...

sc = m.contourf(x, y, d1, cmap='jet')
else: # pcolor
sc = m.pcolor(x, y, d1, cmap='jet')
print("contour plot depreciated. using pcolor.")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
print("contour plot depreciated. using pcolor.")
print("contour plot deprecated. using pcolor.")

@dabail10
Copy link
Contributor

This all looks good to me. Not sure why Phillipe is getting blue everywhere. Is the colormap slightly different on your machine?

@phil-blain
Copy link
Member

It should not be... I'm looking into it ....

@phil-blain
Copy link
Member

phil-blain commented Jun 28, 2022

This is with Cartopy 0.20.2, Matpltlib 3.5.2. What versions are you using ?

@dabail10
Copy link
Contributor

Cartopy 0.20.2 matplotlib 3.5.1

@phil-blain
Copy link
Member

I can investigate on my own as to not delay merging this.

@phil-blain
Copy link
Member

my plots are on the gx1 grid, maybe that's why I'm seeing something different?

@dabail10
Copy link
Contributor

Just ran it on one of my gx1 tests and I don't see the blue.

ice_thickness_gx1testc
ice_thickness_gx1testb_minus_gx1testc
ice_thickness_gx1testb

@phil-blain
Copy link
Member

In my plot I've just noticed there is also a dark longitudinal line near 30°E...

@phil-blain
Copy link
Member

phil-blain commented Jun 28, 2022

I've spent more time debugging this. I think it's due to the fact that the t_lat and t_lon arrays are masked arrays due to land proc elimination:

t_lat:
image
t_lon:
image

If I do something like t_lat=t_lat.filled(0), and the same for t_lon, i.e. filling the masked values with zeros:
image

Then I get the expected plot:

image

EDIT the doc for pcolormesh does state that masked arrays is only supported for the data itself, not the coordinates arrays...

EDIT 2 and if we try to do a plain matplotlib pcolormesh (i.e. without cartopy), we get an error saying the coordinate arrays can't be masked arrays. So it's really cartopy here that seems to fail to check its inputs...


So @dabail10 I guess my next question would be what was your MPI x OpenMP configuration for the gx1 runs you used to test the new script ? Would it be possible that there was no land procs?

@phil-blain
Copy link
Member

So I zoomed out a bit in my plot above and I don't think it's as easy as filling the lat/lon arrays with zeros:

image

we get spurious polygons, with different shapes depending on the filling value. I think we should use "complete" i.e. non masked tlat, tlon. This leads to the correct plot:

image

So one way to do it would be for the script to read the location of the input data directory (CICE_data) from the env.machine_compiler file and then read the grid from there to get the full lat/lon arrays, The only "problem" is that we currently do not have the gx1 grid as a NetCDF file.

@daveh150
Copy link
Contributor Author

@phil-blain @dabail10 if the issue is related to masked locations in the tlon/tlat but NOT the data, could you try this to get the mask the same. Then I think the pcolor will ignore the masked locations:

data.mask = np.logical_or(data.mask,lon.mask)

@phil-blain
Copy link
Member

I just tried that and it does not work (gives me the blue plot again). I think that we should use the full lat/lon arrays.

@eclare108213
Copy link
Contributor

In my opinion, tlat and tlon ought to be made 'complete' (no eliminated land blocks) in the output files. One way to do this would be to write the original, global arrays, rather than writing them after the decomposition when the blocks are eliminated. Not sure how easy this would be, or if it would work for PIO etc.

@daveh150
Copy link
Contributor Author

Thanks @phil-blain. I think you had an idea to read the gx1 grid file instead of from the history file, if it was netcdf? That should be straight forward, we just need to write the TGRID in addition the UGRID.

@daveh150
Copy link
Contributor Author

I made a grid_gx1.nc file. Hopefully it is attached, can y'all verify it is okay then perhaps add it to the CICE_data grid files for future use?
grid_gx1.nc.gz

Updating the cice.t-test.py to find this file will take a bit of thought, since it is not in the run directory.

@daveh150
Copy link
Contributor Author

Sorry for quick modification of the gx1 netcdf, I changed the variables to be all lowercase in accordance with popgrid_nc. Also changed the kmt data type to integer and unit to 'none'.

grid_gx1.nc.gz

phil-blain added a commit to phil-blain/CICE that referenced this pull request Jul 28, 2022
Cartopy (and the underlying Matplotlib) do not support using masked
arrays as coordinates arrays for the 'pcolormesh' plotting function.
Matplotlib verifies that, but Cartopy does not and seems to (wrongly)
interpolate the coordinates and data arrays.

When running the 'cice.t-test.py' script to produce plots, the latitude
and longitude arrays in the CICE output files (TLON,TLAT) are read as
masked arrays by the NetCDF4 module since they contain missing values
due to land blocks being eliminated. This leads to spurious polygons
being drawn when using 'pcolormesh', as can be seen e.g. at [1] and [2].

Avoid that by adjusting the 'qc' option to disable land block
elimination, resulting in "complete" TLON,TLAT arrays in the output
files, and thus non-masked arrays in the Python script.

[1] CICE-Consortium#727 (comment)
[2] CICE-Consortium#727 (comment)
phil-blain added a commit to phil-blain/CICE that referenced this pull request Jul 28, 2022
Cartopy (and the underlying Matplotlib) do not support using masked
arrays as coordinates arrays for the 'pcolormesh' plotting function.
Matplotlib verifies that, but Cartopy does not and seems to (wrongly)
interpolate the coordinates and data arrays.

When running the 'cice.t-test.py' script to produce plots, the latitude
and longitude arrays in the CICE output files (TLON,TLAT) are read as
masked arrays by the NetCDF4 module since they contain missing values
due to land blocks being eliminated. This leads to spurious polygons
being drawn when using 'pcolormesh', as can be seen e.g. at [1] and [2].

Avoid that by adjusting the 'qc' option to disable land block
elimination, resulting in "complete" TLON,TLAT arrays in the output
files, and thus non-masked arrays in the Python script.

[1] CICE-Consortium#727 (comment)
[2] CICE-Consortium#727 (comment)

Suggested-by: David A. Bailey <dbailey@ucar.edu>
@phil-blain
Copy link
Member

I implemented what we talked about in the previous meeting i.e. disabling land blocks elimination.

@daveh150, you could update your branch to include my commit thusly:

git checkout update_plots
git pull git@github.com:phil-blain/CICE.git update_plots
git push

@apcraig
Copy link
Contributor

apcraig commented Jul 28, 2022

Thanks for doing that @phil-blain, I think I volunteered but then dropped the ball. @daveh150, could you pull @phil-blain's change to your sandbox and retest? @phil-blain, have you confirmed that this fixes the issues you were seeing? If so, and everyone agrees, we can problem merge soon. Thanks!

@phil-blain
Copy link
Member

I had already verified that using the full TLON/TLAT arrays made the script work correctly. I had done that by creating a gx1 test with -p 1x1, which then changed the block size and resulted in a single block and thus no land blocks elimination. I then manually loaded these TLON/TLAT arrays in Python and used those to create the "correct" maps in #727 (comment).

I just verified now that with this updated qc option, i.e. including distribution_wght = 'blockall', creating a test with -p 44x1 -s qc , i.e. what the doc suggests using, the land blocks are indeed not eliminated and the TLON/TLAT arrays in the output files do not have any missing values. So I think this shows the fix is sufficient :)

@apcraig
Copy link
Contributor

apcraig commented Jul 28, 2022

Thanks @phil-blain, it makes sense the blockall would fix the problem. Thanks for confirming.

@daveh150, if there are any mods related to pulling in a separate lon/lat file for plotting, those can be removed again.

@daveh150, @phil-blain's mod is a one line change. If you prefer to implement it manually rather than pull/push, that would be fine too. You can see the mod here, daveh150/CICE@update_plots...phil-blain:CICE:update_plots, as noted by @phil-blain.

@apcraig
Copy link
Contributor

apcraig commented Jul 31, 2022

See #742

@apcraig apcraig closed this Jul 31, 2022
@apcraig apcraig mentioned this pull request Aug 1, 2022
@daveh150
Copy link
Contributor Author

daveh150 commented Aug 2, 2022

I've been on leave since last week. I did not have any other changes for the lat/lon issue, glad that the blockall solution by @phil-blain worked! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants