barronh / pseudonetcdf

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

eval() error when SDATE is -635 in IOAPI file #127

Closed eastjames closed 3 years ago

eastjames commented 3 years ago

When using eval() on a file opened with format='ioapi' which has the attribute SDATE=-635, I receive an error:

Example commands:

import PseudoNetCDF as pnc
file=pnc.pncopen('./IOAPI_FILE.nc', format='ioapi')
file.eval('FAKE=VAR[:]')

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-128-8b06510cd7d2> in <module>
----> 1 natearth.eval('FAKE=USA[:]')

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/cmaqfiles/_ioapi.py in eval(self, *args, **kwds)
    559         """
    560         # oldkeys = set(self.variables)
--> 561         out = PseudoNetCDFFile.eval(self, *args, **kwds)
    562         outkeys = set(out.variables)
    563         # newkeys = outkeys.difference(oldkeys)

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/core/_files.py in eval(self, expr, inplace, copyall)
    981             else:
    982                 newkeys = [key]
--> 983                 outf = self.subsetVariables(newkeys)
    984                 try:
    985                     del outf.variables[key]

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/cmaqfiles/_ioapi.py in subsetVariables(self, varkeys, inplace, exclude, keepcoords)
    202         setattr(outf, 'VAR-LIST', '')
    203         outf._add2Varlist(newvarlist)
--> 204         outf.updatemeta()
    205         return outf
    206 

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/cmaqfiles/_ioapi.py in updatemeta(self, attdict, sortmeta)
    519 
    520         self._updatetime()
--> 521         self.updatetflag()
    522         if sortmeta:
    523             ioapi_sort_meta(self)

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/cmaqfiles/_ioapi.py in updatetflag(self, overwrite, startdate, tstep)
    435                 self.TSTEP = tstep
    436 
--> 437             times = self.getTimes()
    438             tvar = self.createVariable(
    439                 'TFLAG', 'i', ('TSTEP', 'VAR', 'DATE-TIME'))

~/.conda/envs/pynco/lib/python3.6/site-packages/PseudoNetCDF/core/_files.py in getTimes(self, datetype, bounds)
   1742                 hasattr(self, 'TSTEP') and 'TSTEP' in self.dimensions:
   1743             refdate = datetime.strptime(
-> 1744                 '%07d %06d+0000' % (self.SDATE, self.STIME), '%Y%j %H%M%S%z')
   1745             tstepstr = '%06d' % self.TSTEP
   1746             timeincr = timedelta(seconds=int(tstepstr[-2:])) + \

~/.conda/envs/pynco/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    563     """Return a class cls instance based on the input string and the
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]
    567     args = tt[:6] + (fraction,)

~/.conda/envs/pynco/lib/python3.6/_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %

ValueError: time data '-000635 000000+0000' does not match format '%Y%j %H%M%S%z'

This error does not occur if I open the file with format='netcdf'

I can provide the file used to create this error if needed.

barronh commented 3 years ago

This is a broader problem solved in #123. There are several work arounds until the next version is released.

newfile = pnc.cmaqfiles.ioapi.from_ncf(file)
newfile.SDATE = 1970001
newfile.eval(…)

Does that work for you?

eastjames commented 3 years ago

Yes, that solves it. Thank you.