-
Notifications
You must be signed in to change notification settings - Fork 42
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
Conversation
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>
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. |
There was a problem hiding this 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
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 |
@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: |
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) |
…GeoCAT-examples into NCL_lcnative_1_lg.py Updating the code for lcnative
# 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary print statement
print(ax) |
@@ -0,0 +1,80 @@ | |||
""" | |||
NCL_polar_1_lg.py |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
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.
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}) |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#print(t) |
Unnecessary print statement
# Extract a slice of the data | ||
t = ds.pre[0,:] | ||
#print(t) | ||
#gvutil.xr_add_cyclic_longitudes(t,'lon') |
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
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.
|
I have only used those functions on rectangular maps. I suggest looking at the |
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? |
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.
Please try
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 |
Try this: |
I got this error: PackagesNotFoundError: The following packages are not available from current channels:
Current channels: |
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:
|
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 |
@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. |
Closing this PR to switch to newer version to fix issues with pushing changes |
Closes issue #63