11
11
from numpy import datetime64 , timedelta64
12
12
13
13
14
+ template_patterns = [
15
+ r'%x1' , r'%x3' , r'%y2' , r'%y4' , r'%m1' , r'%m2' ,
16
+ r'%mc' , r'%d1' , r'%d2' , r'%h1' , r'%h2' , r'%h3' ,
17
+ r'%n2' , r'%f2' , r'%f3' , r'%fn2' , r'%fhn' , r'%fdhn' ,
18
+ r'%j3' , r'%t1' , r'%t2' , r'%t3' , r'%t4' , r'%t5' ,
19
+ r'%t6' , r'%tm1' , r'%tm2' , r'%tm3' , r'%tm4' , r'%tm5' , r'%tm6'
20
+ ]
21
+
14
22
"""
15
23
Core classes are defined below
16
24
"""
@@ -243,26 +251,20 @@ def _processDSets(self, dpath_str):
243
251
if strPos == - 1 :
244
252
raise Exception ('template is used in ctl but no % in dset' )
245
253
246
- endPos = len (dpath_str ) - dpath_str [::- 1 ].find ('.' ) - 1
247
- # endPos = len(dpath_str) if endPos == -1 else endPos
248
-
249
- template = dpath_str [strPos :endPos ]
254
+ matches = find_patterns (dpath_str )
255
+ fmtO = '' .join (matches )
250
256
251
- tokens = [self ._get_template_format ('%' + token )
252
- for token in filter (lambda x : x != '' , template .split ('%' ))]
253
- # tokens = []
254
- # for token in filter(lambda x: x != '', template.split('%')):
255
- # tokens.append(self._get_template_format('%' + token))
256
- fmt = '' .join (tokens )
257
+ tokens = [self ._get_template_format (token ) for token in matches ]
258
+ fmtN = '' .join (tokens )
257
259
258
260
fileList = []
259
261
260
262
times = self .tdef .samples
261
263
base = self ._get_field (times [0 ])
262
264
for l in range (len (times )):
263
- part = times [l ].item ().strftime (fmt )
265
+ part = times [l ].astype ( 'datetime64[s]' ). item ().strftime (fmtN )
264
266
265
- fname = dpath_str [: strPos ] + part + dpath_str [ endPos :]
267
+ fname = dpath_str . replace ( fmtO , part )
266
268
fname = self ._replace_forecast_template (fname , l , base )
267
269
268
270
# remove duplicated file
@@ -622,16 +624,10 @@ def _get_template_format(self, part):
622
624
str
623
625
The format in python datetime
624
626
"""
625
- template_cases = ['%x1' , '%x3' , '%y2' , '%y4' , '%m1' , '%m2' ,
626
- '%mc' , '%d1' , '%d2' , '%h1' , '%h2' , '%h3' ,
627
- '%n2' , '%f2' , '%f3' , '%fn2' , '%fhn' , '%fdhn' ,
628
- '%j3' , '%t1' , '%t2' , '%t3' , '%t4' , '%t5' ,
629
- '%t6' , '%tm1' , '%tm2' , '%tm3' , '%tm4' , '%tm5' , '%tm6' ]
630
-
631
- for c in template_cases :
627
+ for c in template_patterns :
632
628
if c in part :
633
629
length = len (c )
634
- fmt = part [:length ] # format in template_cases
630
+ fmt = part [:length ] # format in template_patterns
635
631
rem = part [length :] # remaining str in part
636
632
break
637
633
else :
@@ -779,7 +775,7 @@ def _times_to_array(self, strTime, incre, tnum):
779
775
y , m = y + int ((m + l - 1 )/ 12 ), int ((m + l - 1 )% 12 )+ 1
780
776
lst .append (start .replace (year = y , month = m ))
781
777
782
- return np .asarray (lst , dtype = 'datetime64[s]' )
778
+ return np .asarray (lst , dtype = 'datetime64[s]' ). astype ( 'datetime64[ns]' )
783
779
784
780
elif 'yr' in incre :
785
781
start = GrADStime_to_datetime (strTime )
@@ -789,7 +785,7 @@ def _times_to_array(self, strTime, incre, tnum):
789
785
y = start .year + l
790
786
lst .append (start .replace (year = y ))
791
787
792
- return np .asarray (lst , dtype = 'datetime64[s]' )
788
+ return np .asarray (lst , dtype = 'datetime64[s]' ). astype ( 'datetime64[ns]' )
793
789
794
790
else :
795
791
start = GrADStime_to_datetime64 (strTime )
@@ -804,11 +800,11 @@ def _times_to_array(self, strTime, incre, tnum):
804
800
lst .append (start )
805
801
start += intv
806
802
807
- return np .asarray (lst )
803
+ return np .asarray (lst ). astype ( 'datetime64[ns]' )
808
804
809
805
else :
810
806
811
- return np .arange (start , start + intv * tnum , intv )
807
+ return np .arange (start , start + intv * tnum , intv ). astype ( 'datetime64[ns]' )
812
808
813
809
def __repr__ (self ):
814
810
"""Print this class as a string"""
@@ -1146,5 +1142,20 @@ def GrADS_increment_to_timedelta64(incre):
1146
1142
return timedelta64 (int (amount ), unitDict [unit ])
1147
1143
1148
1144
1149
-
1145
+ def find_patterns (template_path ):
1146
+ """Find template patterns in a given path
1147
+
1148
+ Parameters
1149
+ ----------
1150
+ template_path: str
1151
+ A path containing template strings.
1152
+
1153
+ Returns
1154
+ -------
1155
+ re: list
1156
+ matched strings
1157
+ """
1158
+ pattern = re .compile ('|' .join (template_patterns ))
1159
+
1160
+ return pattern .findall (template_path )
1150
1161
0 commit comments