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

Adding NCL_lcnative_1_lg.py example python file #81

Closed
wants to merge 24 commits into from

Conversation

michaelavs
Copy link
Contributor

Closes issue #63

image

michaelavs and others added 13 commits May 20, 2020 14:38
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
Co-authored-by: Kevin Paul <kpaul@ucar.edu>
@michaelavs
Copy link
Contributor Author

Currently having an issue with using the Lambert conformal projection for this visualization. When projection=ccrs.LambertConformal() in line 25, the code on line 46 (gvutil.add_lat_lon_ticklabels(ax)) throws an error that "This formatter cannot be used with non-rectangular projection". But when switching to Lambert Conformal and removing this line of code, the projection does not look the same as the example, there is a triangular chunk missing from the upper right corner of the visualization. You will also probably notice that the contours are not the same for this projection as in the example. I am not sure if this comes from the data that is being read in, or if there is a discrepancy between the original example and the one I created/used.

@michaelavs michaelavs linked an issue May 27, 2020 that may be closed by this pull request
Copy link
Collaborator

@erogluorhan erogluorhan left a comment

Choose a reason for hiding this comment

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

Please see some inline comments

Plots/MapProjections/NCL_proj_2_lg.py Outdated Show resolved Hide resolved
Plots/Contours/NCL_polar_1_lg.py Outdated Show resolved Hide resolved
Plots/MapProjections/NCL_lcnative_1_lg.py Outdated Show resolved Hide resolved
Plots/MapProjections/NCL_lcnative_1_lg.py Show resolved Hide resolved
@clyne
Copy link
Collaborator

clyne commented May 27, 2020

Currently having an issue with using the Lambert conformal projection for this visualization. When projection=ccrs.LambertConformal() in line 25, the code on line 46 (gvutil.add_lat_lon_ticklabels(ax)) throws an error that "This formatter cannot be used with non-rectangular projection". But when switching to Lambert Conformal and removing this line of code, the projection does not look the same as the example, there is a triangular chunk missing from the upper right corner of the visualization. You will also probably notice that the contours are not the same for this projection as in the example. I am not sure if this comes from the data that is being read in, or if there is a discrepancy between the original example and the one I created/used.

Have you configured the Cartopy parameters for the Lambert Conformal projection to match those used by NCL's LC projection (i.e. the Meridian and parallels)? See

https://www.ncl.ucar.edu/Document/Graphics/Resources/mp.shtml

https://scitools.org.uk/cartopy/docs/latest/crs/projections.html#lambertconformal

@michaelavs
Copy link
Contributor Author

@clyne just looked into it and added the central_longitude and standard_parallels that are listed in the ncl file but with no success. I then tried to again remove line 48 which allowed this visualization to be produced:
image
and I am not sure why it is one color and not reporting the data correctly

@clyne
Copy link
Collaborator

clyne commented May 28, 2020

Looks like it's going to take some more digging :-). I'd suggest ignoring the data for now and just trying to get the map projection correct (or vise versa)

@michaelavs
Copy link
Contributor Author

here is the conformal projection with gridlines turned on (so we can see where the lat/lon lines are located)
image
I will also be pushing the code for this shortly

# Generate axes using Cartopy and draw coastlines
projection = ccrs.LambertConformal(central_longitude=45, standard_parallels=(36,55))
ax = plt.axes(projection=projection, frameon=True)
print(ax)
Copy link
Contributor

Choose a reason for hiding this comment

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

unnecessary print statement

Suggested change
print(ax)

@@ -0,0 +1,80 @@
"""
NCL_polar_1_lg.py
Copy link
Contributor

Choose a reason for hiding this comment

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

should be "NCL_lcnative_1_lg.py"


# Use geocat.viz.util convenience function to add minor and major tick lines
#Use geocat.viz.util convenience function to set axes tick values
#gvutil.set_axes_limits_and_ticks(ax, xticks=np.linspace(30, 55, 6), yticks=np.linspace(20, 45, 6))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason you took the lat/lon ticks and the title out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we use the Lambert conformal projection, the convenience tools don't like that the ticks are non-rectangular so if they are active in the code, we get an error in the code. This is most likely going to have to be addressed in the convenience functions to allow for the projection to have ticks added to non-rectangular projections. The best way to visualize the problem the function is running into is in the last projection I uploaded vs the original NCL projection. In the NCL projection, the image is "flat" so the ticks going around the outside border make sense to be read in as such, however, in the python version the gridlines show where the lat/lon lines should be and they are not "flat" which I think is why the convenience function is getting angry.


# Plot data and create colorbar
newcmp = gvcmaps.BlueYellowRed
t.plot.contourf(ax=ax, cmap=newcmp, transform=ccrs.PlateCarree(), levels = 14, cbar_kwargs={"orientation":"horizontal", "ticks":np.arange(0, 240, 20), "label":'', "shrink":0.9})
Copy link
Contributor

Choose a reason for hiding this comment

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

I would change the "shrink" argument to make the colorbar line up with the map.

Suggested change
t.plot.contourf(ax=ax, cmap=newcmp, transform=ccrs.PlateCarree(), levels = 14, cbar_kwargs={"orientation":"horizontal", "ticks":np.arange(0, 240, 20), "label":'', "shrink":0.9})
t.plot.contourf(ax=ax, cmap=newcmp, transform=ccrs.PlateCarree(), levels = 14, cbar_kwargs={"orientation":"horizontal", "ticks":np.arange(0, 240, 20), "label":'', "shrink":0.7})

Copy link
Contributor

@hCraker hCraker left a comment

Choose a reason for hiding this comment

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

I think your issue with the projection is that you are trying to transform the data from the Plate Carree projection to the Lambert Conformal projection but the data is native to Lambert Conformal. I left a more detailed comment about this and some other unrelated suggestions.

import geocat.datafiles as gdf
from geocat.viz import cmaps as gvcmaps
from geocat.viz import util as gvutil
import matplotlib.ticker as ticker
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move the ticker import next to the plt import. We should group import statements from the same package together and put the geocat imports at the end for clarity. That way, the imports are organized by common packages (matplotlib.pyploy, cartopy) at the top and less common (geocat.util) at the bottom.


# Extract a slice of the data
t = ds.pre[0,:]
#print(t)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#print(t)

Unnecessary print statement

# Extract a slice of the data
t = ds.pre[0,:]
#print(t)
#gvutil.xr_add_cyclic_longitudes(t,'lon')
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still needed or can it be deleted?


# Plot data and create colorbar
newcmp = gvcmaps.BlueYellowRed
t.plot.contourf(ax=ax, cmap=newcmp, transform=ccrs.PlateCarree(), levels = 14, cbar_kwargs={"orientation":"horizontal", "ticks":np.arange(0, 240, 20), "label":'', "shrink":0.9})
Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a comment from the NCL file:

; Usually, when data is placed onto a map, it is TRANSFORMED to the specified
; projection. Since this model is already on a native lambert conformal grid,
; we want to turn OFF the transformation.

I think the reason why your image isn't "flat" is because you are transforming the data from one projection to another. This data is native to a Lambert Conformal grid so transform=ccrs.PlateCarree() isn't needed. Give it a try with out the transformation and you may be able to add the lat/lon ticks.

@michaelavs
Copy link
Contributor Author

image
I was able to find this method to add the lat/lon lines. I haven't quite figured out how to get the the longitude lines to go from being in the middle of the projection to being on the x axis instead. Also not too sure why it presents two sets of numbers on the projection. @clairefio @hCraker do either of you have any experience with the xlines, xlocator, or xlabel functions?

@hCraker
Copy link
Contributor

hCraker commented Jun 10, 2020

image
I was able to find this method to add the lat/lon lines. I haven't quite figured out how to get the the longitude lines to go from being in the middle of the projection to being on the x axis instead. Also not too sure why it presents two sets of numbers on the projection. @clairefio @hCraker do either of you have any experience with the xlines, xlocator, or xlabel functions?

I have only used those functions on rectangular maps. I suggest looking at the Gridliner function in cartopy. It has options to draw labels along with the latitude and longitude lines.
Here's the link https://scitools.org.uk/cartopy/docs/v0.13/matplotlib/gridliner.html

@michaelavs
Copy link
Contributor Author

I've been doing some more trial and error attempts with this projection and just got this error: "TypeError: Cannot label gridlines on a LambertConformal plot. Only PlateCarree and Mercator plots are currently supported." With a quick google search, it appears this was questioned in a SciTools Github and may have been resolved in the most recent update of cartopy which was release on May 3, 2020. It may not be an immediate thing to address, but I would be curious if updating our Cartopy in the geocat examples environment may be able to address the problem with adding lat/lon lines to Lambert conformal projections. That being said, will I need to do anything in particular to proceed with this version update on my side @erogluorhan? I just tried conda update cartopy and it shows as up to date, but conda list shows as version 0.17. Additionally, if this does end up solving the issue, how would we go about updating the environment for all users?

@erogluorhan
Copy link
Collaborator

I've been doing some more trial and error attempts with this projection and just got this error: "TypeError: Cannot label gridlines on a LambertConformal plot. Only PlateCarree and Mercator plots are currently supported." With a quick google search, it appears this was questioned in a SciTools Github and may have been resolved in the most recent update of cartopy which was release on May 3, 2020.

I agree that version 0.18.0 would likely have resolved this issue, reading major changes under release notes of this version. It'd be good to give this version a try then.

I just tried conda update cartopy and it shows as up to date, but conda list shows as version 0.17.

Please try conda update --all as well. I checked my cartopy version and it is 0.18.0, and I think it was handled by recently calling conda update --all. FYI: The possible reasons of why conda cannot update to the actual latest version of a package by simply calling conda update <package_name> but conda update --all can simply work is discussed here: conda/conda#786

Additionally, if this does end up solving the issue, how would we go about updating the environment for all users?

There is nothing to do on your side to update others' environments. Other users of the geocat-examples will get errors with the cartopy v0.18.0 function you would use, then they will handle their own environment update.

@michaelavs
Copy link
Contributor Author

I've been doing some more trial and error attempts with this projection and just got this error: "TypeError: Cannot label gridlines on a LambertConformal plot. Only PlateCarree and Mercator plots are currently supported." With a quick google search, it appears this was questioned in a SciTools Github and may have been resolved in the most recent update of cartopy which was release on May 3, 2020.

I agree that version 0.18.0 would likely have resolved this issue, reading major changes under release notes of this version. It'd be good to give this version a try then.

I just tried conda update cartopy and it shows as up to date, but conda list shows as version 0.17.

Please try conda update --all as well. I checked my cartopy version and it is 0.18.0, and I think it was handled by recently calling conda update --all. FYI: The possible reasons of why conda cannot update to the actual latest version of a package by simply calling conda update <package_name> but conda update --all can simply work is discussed here: conda/conda#786

Additionally, if this does end up solving the issue, how would we go about updating the environment for all users?

There is nothing to do on your side to update others' environments. Other users of the geocat-examples will get errors with the cartopy v0.18.0 function you would use, then they will handle their own environment update.

I just tried doing conda update --all in both my base environment and geocat-examples and cartopy is still at version 0.17. I have also tried using conda update --all -c ncar just from the update we did for GeoCAT packages today and it also kept cartopy at 0.17.

@erogluorhan
Copy link
Collaborator

I just tried doing conda update --all in both my base environment and geocat-examples and cartopy is still at version 0.17. I have also tried using conda update --all -c ncar just from the update we did for GeoCAT packages today and it also kept cartopy at 0.17.

Try this: conda install cartopy=0.18.0

@michaelavs
Copy link
Contributor Author

michaelavs commented Jun 15, 2020

I just tried doing conda update --all in both my base environment and geocat-examples and cartopy is still at version 0.17. I have also tried using conda update --all -c ncar just from the update we did for GeoCAT packages today and it also kept cartopy at 0.17.

Try this: conda install cartopy=0.18.0

I got this error:

PackagesNotFoundError: The following packages are not available from current channels:

  • cartopy=0.18.0

Current channels:

@clyne
Copy link
Collaborator

clyne commented Jun 16, 2020

FWIW I just checked and I have 0.18.0 on my system.

@erogluorhan
Copy link
Collaborator

FWIW I just checked and I have 0.18.0 on my system.

Thanks! We later figured out that conda-forge was not added into conda channels, so we managed to upgrade to 0.18.0 by enforcing conda-forge.

@michaelavs you may want to add conda-forge to your conda channels for future convenince as follows:

conda config --add channels conda-forge

@michaelavs
Copy link
Contributor Author

Was able to turn on gridlines using cartopy 0.18, here is the new version:
image
I am now looking into if there is a way to be more specific about which labels are present because there is overlap between come of the lat/lon lines and their location on the visualization

@michaelavs
Copy link
Contributor Author

I've been looking into this issue some more and talked with John about this idea yesterday, but wanted some more input from everyone else before making a decisions on direction for this projection. When using NCL, the Lambert conformal projection has no curvature to the latitude lines, which it should have because a conformal projection is conic in shape. From looking more into the documentation of the code, it appears there is a function that essentially pins the projection to 4 corners on lat/lon lines and will project as a rectangle instead of cone. The line that does this is res@mpLimitMode = "Corners" which is not turned on in the lcmask projection which Rose has worked on, which is why that projection is rounded while this one is rectangular (in NCL). All of that being said, Cartopy has three separate projection options for Lambert: Conformal, Cylindrical (rectangular output), and AzimuthalEqualArea (rounded with equal area). The cylindrical projection allows to almost perfectly recreate the NCL projection and has no issues with adding gridlines or using convenience functions, but conformal is technically the version we should be using to remain true to NCL. However, John and I discussed the potential of including all three of these Lambert projections to showcase the true functionality of Cartopy and even discuss how there are differences between NCL and Python when it comes to certain projections and therefore the importance of choosing the correct projection for the data. What is everyones thoughts on slightly straying from the norm and including extra info/plots for this example? I am also including the three separate outputs so you can all see them as well @clairefio @hCraker @jukent @kmpaul
image

image

image

@kmpaul
Copy link
Contributor

kmpaul commented Jun 25, 2020

@michaelavs: It seems to me that you might have answered your own question. You said that a "Conformal" plot should be conical, but that is not what NCL is displaying. So, by that definition, the NCL plot is either wrong or, at least, misleading. If you decide that the NCL plot is actually not displaying what it suggests it is displaying, then I think the best coarse of action is to display the correct thing... and perhaps showcase what all Cartopy can do. So, I think the "all Lambert options" solution is the way to go.

@michaelavs michaelavs mentioned this pull request Jul 6, 2020
@michaelavs
Copy link
Contributor Author

Closing this PR to switch to newer version to fix issues with pushing changes

@michaelavs michaelavs closed this Jul 6, 2020
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.

lcnative_1 (Lambert Conformal projection)
6 participants