Skip to content

Commit

Permalink
fix dt is mo or yr
Browse files Browse the repository at this point in the history
  • Loading branch information
miniufo committed Apr 11, 2020
1 parent 3d5aca3 commit b47e8c4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
6 changes: 3 additions & 3 deletions tests/test_CtlDescriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@author: MiniUFO
Copyright 2018. All rights reserved. Use is subject to license terms.
"""
from xgrads.core import CtlDescriptor
from xgrads.xgrads import CtlDescriptor


content=\
Expand All @@ -23,7 +23,7 @@

print(CtlDescriptor(content=content))

for i in range(9):
print(CtlDescriptor(file='../ctls/test'+str(i+1)+'.ctl'))
for i in range(10):
print(CtlDescriptor(file='./xgrads/ctls/test'+str(i+1)+'.ctl'))
print('\n')

4 changes: 2 additions & 2 deletions tests/test_openDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@author: MiniUFO
Copyright 2018. All rights reserved. Use is subject to license terms.
"""
from xgrads.core import open_CtlDataset
from xgrads.xgrads import open_CtlDataset
import xarray as xr

#dset = open_CtlDataset('D:/Data/ULFI/output/2017101712_1721.ctl')
Expand All @@ -16,7 +16,7 @@
# dset = open_CtlDataset('D:/Data/MITgcm/flt/float/Stat.ctl')
# dset = open_CtlDataset('../ctls/test8.ctl')

dset = open_CtlDataset('../ctls/test9.ctl')
dset = open_CtlDataset('./xgrads/ctls/test9.ctl')

dset2 = xr.tutorial.open_dataset('air_temperature')

Expand Down
80 changes: 71 additions & 9 deletions xgrads/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,10 @@ def _processTDef(self, oneline):
if tokens[2]!='linear':
raise Exception('nonlinear tdef is not supported')

start = GrADStime_to_datetime64(tokens[3])
intv = GrADS_increment_to_timedelta64(tokens[4])

self.incre = intv
self.tdef = Coordinate('tdef', np.arange(start,
start + intv * tnum, intv))
times = self._times_to_array(tokens[3], tokens[4], tnum)

self.incre = GrADS_increment_to_timedelta64(tokens[4])
self.tdef = Coordinate('tdef', times)

def _processVars(self, oneline, fileContent):
if (self.dtype != 'station' and
Expand Down Expand Up @@ -510,6 +508,52 @@ def _split_by_len(self, s, size):

return [s[i:i + size] for i in range(0, chunks, size)]


def _times_to_array(self, strTime, incre, tnum):
"""
Convert GrADS time string of strart time and increment
to an array of numpy.datetime64.
Parameters
----------
strTime : str
Grads start time e.g., 00:00z01Jan2000.
incre : str
Grads time increment in str format e.g., 1dy.
tnum : int
Grads time increment in str format e.g., 1dy.
Returns
----------
re : numpy array of datetime64
"""
if 'mo' in incre:
start = GrADStime_to_datetime(strTime)

lst = []
for l in range(tnum):
y, m = start.year, start.month
y, m = y+int((m+l-1)/12), int((m+l-1)%12)+1
lst.append(start.replace(year=y, month=m))

return np.asarray(lst, dtype='datetime64[s]')

elif 'yr' in incre:
start = GrADStime_to_datetime(strTime)

lst = []
for l in range(tnum):
y = start.year + l
lst.append(start.replace(year=y))

return np.asarray(lst, dtype='datetime64[s]')

else:
start = GrADStime_to_datetime64(strTime)
intv = GrADS_increment_to_timedelta64(incre)

return np.arange(start, start + intv * tnum, intv)

def __str__(self):
"""
Print this class as a string.
Expand All @@ -530,7 +574,7 @@ def __str__(self):
' periodicX: ' + str(self.periodicX) + '\n'\
' cal365Days: ' + str(self.cal365Days)+ '\n'\
' sequential: ' + str(self.hasData) + '\n'\
' byteOrder: ' + str(self.hasData) + '\n'\
' byteOrder: ' + str(self.byteOrder) + '\n'\
' xdef: ' + str(self.xdef) + '\n'\
' ydef: ' + str(self.ydef) + '\n'\
' zdef: ' + str(self.zdef) + '\n'\
Expand Down Expand Up @@ -761,7 +805,7 @@ def open_CtlDataset(desfile):
return dset


def GrADStime_to_datetime64(gradsTime):
def GrADStime_to_datetime(gradsTime):
"""
Convert GrADS time string e.g., 00:00z01Jan2000 to numpy.datetime64
Expand All @@ -772,7 +816,7 @@ def GrADStime_to_datetime64(gradsTime):
Returns
----------
re : datetime64
re : datetime
"""
lens = len(gradsTime)

Expand All @@ -787,6 +831,24 @@ def GrADStime_to_datetime64(gradsTime):
else:
raise Exception('invalid length of GrADS date/time string')

return time


def GrADStime_to_datetime64(gradsTime):
"""
Convert GrADS time string e.g., 00:00z01Jan2000 to numpy.datetime64
Parameters
----------
gradsTime : str
Grads time in str format e.g., 00:00z01Jan2000.
Returns
----------
re : datetime64
"""
time = GrADStime_to_datetime(gradsTime)

return datetime64(time.strftime('%Y-%m-%dT%H:%M:%S'))


Expand Down

0 comments on commit b47e8c4

Please sign in to comment.