barronh / pseudonetcdf

PseudoNetCDF like NetCDF except for many scientific format backends
GNU Lesser General Public License v3.0
77 stars 35 forks source link

Issues in IOAPI projections #61

Closed pgamez closed 5 years ago

pgamez commented 6 years ago

Hi, I got an error when parsing the projection from an ioapi file:

**PNC:/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/conventions/ioapi/_ioapi.py:134:UserWarning:
  IOAPI_ISPH is assumed to be 6370000.; consistent with WRF
Traceback (most recent call last):
  File "./plume_rise_stack.py", line 57, in <module>
    proj = fp.getproj()
  File "/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/core/_files.py", line 114, in getproj
    return getproj(self, withgrid = withgrid)
  File "/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/coordutil.py", line 499, in getproj
    proj4str = getproj4(ifile, withgrid = withgrid)
  File "/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/coordutil.py", line 518, in getproj4
    mapstr = getproj4_from_cf_var(gridmapping, withgrid = withgrid)
  File "/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/coordutil.py", line 452, in getproj4_from_cf_var
    pv4name = pv4s[gname]
KeyError: 'equatorial_mercator'

I found that there is an inconsistency in file coordutil.py, function getproj4_from_cf_var. I suggest to make this change:

def getproj4_from_cf_var(gridmapping, withgrid = False):
    mapstr_bits = OrderedDict()
    gname = getattr(gridmapping, 'grid_mapping_name')
    pv4s = dict(lambert_conformal_conic = 'lcc',
               rotated_latitude_longitude = 'ob_tran',
               latitude_longitude = 'lonlat',
               transverse_mercator = 'merc',
               #mercator = 'merc',
               equatorial_mercator = 'merc',
               polar_stereographic = 'stere'
               )

By the way, I think that the name of transverse_mercator in proj4 syntax is tmerc (not merc as seen in the code).

Thanks!

Pedro

pgamez commented 6 years ago

Another issue... pncwarn is not imported in core/_files.py:

In [8]: fp.ll2ij(50,50,bounds='warn')
**PNC:/home/pgamez/.local/lib/python3.6/site-packages/PseudoNetCDF/conventions/ioapi/_ioapi.py:134:UserWarning:
  IOAPI_ISPH is assumed to be 6370000.; consistent with WRF
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-290a68f38acf> in <module>()
----> 1 fp.ll2ij(50,50,bounds='warn')

~/.local/lib/python3.6/site-packages/PseudoNetCDF/core/_files.py in ll2ij(self, lon, lat, bounds)
    182                     raise ValueError(message)
    183                 else:
--> 184                     pncwarn(message)
    185         return i, j
    186 

NameError: name 'pncwarn' is not defined
barronh commented 6 years ago

Thanks for the feedback.

1) If pncwarn is not imported, you are using an older version. That won't fix your main concerns.

2) You're right about the inconsistency. Thanks for the heads-up.

3) transverse_mercator. Thanks for the heads up.

I'll push a fix to the misc-dev branch, which will get incorporated into master and v3.1.0 before CMAS.

In the future, you can also fork the repository, make the fix yourself, and issue a pull request. I'm always grateful for fixes from other folks.

pgamez commented 6 years ago

Thank you Barron. I am using the last version from pip repository (3.0.0). I am noticing that there is a v3.0.1 in github OK, next time I could push the fix! thanks Pedro

pgamez commented 6 years ago

Ah! sorry

I just realize that the grid mapping name equatorial_mercator doesn't exist in CF conventions. The official name is mercator, so the fix should be made in conventions/ioapi/_ioapi.py (line 145) instead of coordutils.py:

#_gdnames = {1: "latitude_longitude", 2: "lambert_conformal_conic", 7: "equatorial_mercator", 6: "polar_stereographic"}
_gdnames = {1: "latitude_longitude", 2: "lambert_conformal_conic", 7: "mercator", 6: "polar_stereographic"}
barronh commented 5 years ago

Sorry, I missed that this was still active. I've made the change in branch misc-dev.