@@ -13,7 +13,6 @@ def num2date(time_value, unit, calendar):
1313 time_value = int (round (time_value ))
1414 if ('common_year' in unit ):
1515 my_unit = unit .replace ('common_year' , 'day' )
16- ## my_time_value = time_value * 365
1716 my_time_value = int (round (time_value )) * 365
1817 else :
1918 my_unit = unit
@@ -130,7 +129,7 @@ def get_input_dates(glob_str, comm, rank, size):
130129 comm .sync ()
131130 return g_stream_dates ,g_file_slices ,calendar .lower (),units ,time_period_freq
132131
133- def get_cesm_date (fn ,t = None ):
132+ def get_cesm_date (fn ,tseries_tper , t = None ):
134133
135134 '''
136135 Open a netcdf file and return its datestamp
@@ -156,28 +155,42 @@ def get_cesm_date(fn,t=None):
156155 # for the first lnd and rof file
157156 if ( - 1.0 < d < 0.0 ):
158157 d = 0
159- if t == 'bb' :
158+ elif t == 'bb' :
160159 d = f .variables [att ['bounds' ]][0 ][0 ]
161160 # for the first lnd and rof file
162161 if ( - 1.0 < d < 0.0 ):
163162 d = 0
164163 elif (d > 1 ):
165- d = d = f .variables [att ['bounds' ]][0 ][1 ]
164+ d = f .variables [att ['bounds' ]][0 ][1 ]
166165 elif t == 'e' :
167166 l = len (f .variables [att ['bounds' ]])
168167 d = (f .variables [att ['bounds' ]][l - 1 ][1 ])- 1
169168 elif t == 'ee' :
170169 l = len (f .variables [att ['bounds' ]])
171170 d = (f .variables [att ['bounds' ]][l - 1 ][1 ])
171+
172+ # problem if global attr time_period_freq does not exist in the nc file
173+ if 'time_period_freq' in f .ncattrs ():
174+ if 'month' in f .time_period_freq :
175+ if t == 'bb' or t == 'b' :
176+ d = (f .variables [att ['bounds' ]][0 ][0 ] + f .variables [att ['bounds' ]][0 ][1 ]) / 2
177+ if t == 'ee' or t == 'e' :
178+ l = len (f .variables [att ['bounds' ]])
179+ d = (f .variables [att ['bounds' ]][l - 1 ][0 ]+ f .variables [att ['bounds' ]][l - 1 ][1 ])/ 2
180+ elif 'month' in tseries_tper :
181+ if t == 'bb' or t == 'b' :
182+ d = (f .variables [att ['bounds' ]][0 ][0 ] + f .variables [att ['bounds' ]][0 ][1 ]) / 2
183+ if t == 'ee' or t == 'e' :
184+ l = len (f .variables [att ['bounds' ]])
185+ d = (f .variables [att ['bounds' ]][l - 1 ][0 ]+ f .variables [att ['bounds' ]][l - 1 ][1 ])/ 2
186+
172187 else :
173188 # problem if time has only one value when units are common_year
174189 try :
175190 d = f .variables ['time' ][1 ]
176191 except :
177192 d = f .variables ['time' ][0 ]
178193
179-
180- ## d1 = cf_units.num2date(d,att['units'],att['calendar'].lower())
181194 d1 = num2date (d ,att ['units' ],att ['calendar' ].lower ())
182195 f .close ()
183196
@@ -202,7 +215,6 @@ def get_chunk_range(tper, size, start, cal, units):
202215 '''
203216
204217 # Get the first date
205- ## d1 = cf_units.num2date(start, units, cal)
206218 d1 = num2date (start , units , cal )
207219
208220 # Figure out how many days each chunk should be
@@ -222,17 +234,15 @@ def get_chunk_range(tper, size, start, cal, units):
222234 y2 = y2 + 1
223235 m2 = m2 - 12
224236 d2 = datetime .datetime (y2 , m2 , d1 .day , d1 .hour , d1 .minute )
225- ## end = cf_units.date2num(d2, units, cal)
226237 end = date2num (d2 , units , cal )
227238
228239 elif 'year' in tper : #year
229240 d2 = datetime .datetime (int (size )+ d1 .year , d1 .month , d1 .day , d1 .hour , d1 .minute )
230- ## end = cf_units.date2num(d2, units, cal)
231241 end = date2num (d2 , units , cal )
232242
233243 return start , end
234244
235- def get_chunks (tper , index , size , stream_dates , ts_log_dates , cal , units , s ):
245+ def get_chunks (tper , index , size , stream_dates , ts_log_dates , cal , units , s , tseries_tper ):
236246
237247 '''
238248 Figure out what chunks there are to do for a particular CESM output stream
@@ -249,6 +259,7 @@ def get_chunks(tper, index, size, stream_dates, ts_log_dates, cal, units, s):
249259 units(string) - the units to use to figure out chunk size
250260 s(string) - flag to determine if we need to wait until we have all data before we create a chunk or
251261 if it's okay to do an incomplete chunk
262+ tseries_tper - time_period_freq read from XML rather than nc file
252263
253264 Output:
254265 files(dictionary) - keys->chunk, values->a list of all files needed for this chunk and the start and end dates
@@ -307,10 +318,10 @@ def get_chunks(tper, index, size, stream_dates, ts_log_dates, cal, units, s):
307318 files [chunk_n ] = {}
308319 files [chunk_n ]['fn' ] = sorted (cfiles )
309320 if chunk_n > 0 :
310- files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],t = 'bb' )
321+ files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],tseries_tper , t = 'bb' )
311322 else :
312- files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],t = 'b' )
313- files [chunk_n ]['end' ] = get_cesm_date (cfiles [- 1 ],t = 'e' )
323+ files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],tseries_tper , t = 'b' )
324+ files [chunk_n ]['end' ] = get_cesm_date (cfiles [- 1 ],tseries_tper , t = 'e' )
314325 for cd in sorted (cdates ):
315326 dates .append (cd )
316327 e = True
@@ -319,8 +330,11 @@ def get_chunks(tper, index, size, stream_dates, ts_log_dates, cal, units, s):
319330 files [chunk_n ] = {}
320331 s_cdates = sorted (cdates )
321332 files [chunk_n ]['fn' ] = sorted (cfiles )
322- files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],t = 'bb' )
323- files [chunk_n ]['end' ] = get_cesm_date (cfiles [- 1 ],t = 'ee' )
333+ if chunk_n > 0 :
334+ files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],tseries_tper ,t = 'bb' )
335+ else :
336+ files [chunk_n ]['start' ] = get_cesm_date (cfiles [0 ],tseries_tper ,t = 'b' )
337+ files [chunk_n ]['end' ] = get_cesm_date (cfiles [- 1 ],tseries_tper ,t = 'ee' )
324338 for cd in sorted (cdates ):
325339 dates .append (cd )
326340 chunk_n = chunk_n + 1
0 commit comments