@@ -63,55 +63,47 @@ def coordinates_range(start=0, nelems=1, step=1):
6363 return elem_array
6464
6565
66- def get_output_parameters_monthly (start_date , timestep , current_output_index ):
66+ def get_output_parameters_monthly (start_date , timestep , time_frequency , timestep_stride , current_output_index ):
6767 output_index = current_output_index
68- start_yearmonth = start_date .strftime ('%Y%m' )
69- current_date = start_date + datetime .timedelta (days = timestep - 1 )
70- current_yearmonth = current_date .strftime ('%Y%m' )
71- filename_suffix = current_yearmonth
72- if start_yearmonth != current_yearmonth :
73- first_day_current_month = current_date .replace (day = 1 , hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
74- current_day = current_date .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
75- days_between = (current_day - first_day_current_month ).days
76- if days_between == 0 :
77- # Get last month because the 1st of the month belongs in the last month file
78- last_day_last_month = first_day_current_month - datetime .timedelta (days = 1 )
79- filename_suffix = (last_day_last_month ).strftime ('%Y%m' )
80- num_days_last_month = last_day_last_month .day
81- # If last month was complete the idx needs to be set to the last idx of the previous file
82- if output_index > num_days_last_month :
83- output_index = num_days_last_month - 1
84- else :
85- # Since the 1st day belongs to the last year file, the remaining days need to start on index 0...
86- output_index = days_between - 1
68+ current_date = start_date + datetime .timedelta (seconds = ((timestep - 1 ) * timestep_stride ))
69+ filename_suffix = current_date .strftime ('%Y%m' )
70+
71+ first_date_current_month = start_date .replace (year = current_date .year , month = current_date .month )
72+ seconds_between = (current_date - first_date_current_month ).total_seconds ()
73+ num_steps_done_in_current_month = int (seconds_between / timestep_stride ) + 1
74+ last_date_last_month = first_date_current_month - datetime .timedelta (seconds = timestep_stride )
75+ day_inside_last_month = last_date_last_month - datetime .timedelta (seconds = timestep_stride )
76+
77+ if current_date == first_date_current_month :
78+ output_index = 0
79+ elif current_date == last_date_last_month :
80+ filename_suffix = day_inside_last_month .strftime ('%Y%m' )
81+ elif current_output_index >= num_steps_done_in_current_month :
82+ output_index = num_steps_done_in_current_month - 1
8783 return filename_suffix , output_index
8884
8985
90- def get_output_parameters_yearly (start_date , timestep , current_output_index ):
86+ def get_output_parameters_yearly (start_date , timestep , time_frequency , timestep_stride , current_output_index ):
9187 output_index = current_output_index
92- start_year = start_date .strftime ('%Y' )
93- current_date = start_date + datetime .timedelta (days = timestep - 1 )
94- current_year = current_date .strftime ('%Y' )
95- filename_suffix = current_year
96- if start_year != current_year :
97- first_day_current_year = current_date .replace (month = 1 , day = 1 , hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
98- current_day = current_date .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
99- days_between = (current_day - first_day_current_year ).days
100- if days_between == 0 :
101- # Get last year because the 1st of January belongs in the last year file
102- last_day_last_year = first_day_current_year - datetime .timedelta (days = 1 )
103- filename_suffix = (last_day_last_year ).strftime ('%Y' )
104- num_days_last_year = last_day_last_year .timetuple ().tm_yday
105- # If last year was complete the idx needs to be set to the last idx of the previous file
106- if output_index > num_days_last_year :
107- output_index = num_days_last_year - 1
108- else :
109- # Since the 1st day belongs to the last year file, the remaining days need to start on index 0...
110- output_index = days_between - 1
88+ current_date = start_date + datetime .timedelta (seconds = ((timestep - 1 ) * timestep_stride ))
89+ filename_suffix = current_date .strftime ('%Y' )
90+
91+ first_date_current_year = start_date .replace (year = current_date .year )
92+ seconds_between = (current_date - first_date_current_year ).total_seconds ()
93+ num_steps_done_in_current_year = int (seconds_between / timestep_stride ) + 1
94+ last_date_last_year = first_date_current_year - datetime .timedelta (seconds = timestep_stride )
95+ day_inside_last_year = last_date_last_year - datetime .timedelta (seconds = timestep_stride )
96+
97+ if current_date == first_date_current_year :
98+ output_index = 0
99+ elif current_date == last_date_last_year :
100+ filename_suffix = day_inside_last_year .strftime ('%Y' )
101+ elif current_output_index >= num_steps_done_in_current_year :
102+ output_index = num_steps_done_in_current_year - 1
111103 return filename_suffix , output_index
112104
113105
114- def get_output_parameters (settings , netcdf_output_file , start_date , timestep , current_output_index ):
106+ def get_output_parameters (settings , netcdf_output_file , start_date , timestep , time_frequency , timestep_stride , current_output_index ):
115107 output_index = current_output_index
116108 p = Path (netcdf_output_file )
117109 netfile = Path (p .parent ) / Path ('{}.nc' .format (p .name ) if not p .name .endswith ('.nc' ) else p .name )
@@ -120,9 +112,9 @@ def get_output_parameters(settings, netcdf_output_file, start_date, timestep, cu
120112 if splitIO :
121113 monthlyIO = settings .get_option ('monthlyOutput' )
122114 if monthlyIO :
123- filename_suffix , output_index = get_output_parameters_monthly (start_date , timestep , current_output_index )
115+ filename_suffix , output_index = get_output_parameters_monthly (start_date , timestep , time_frequency , timestep_stride , current_output_index )
124116 else :
125- filename_suffix , output_index = get_output_parameters_yearly (start_date , timestep , current_output_index )
117+ filename_suffix , output_index = get_output_parameters_yearly (start_date , timestep , time_frequency , timestep_stride , current_output_index )
126118 netfile = Path (p .parent ) / Path ('{}_{}.nc' .format (p .name , filename_suffix ) if not p .name .endswith ('.nc' ) else p .name )
127119 return prefix , netfile , output_index
128120
@@ -140,6 +132,16 @@ def set_time_dimension(settings, netcdf_obj, time_variable_name, start_date, var
140132 start_date_6hourly = start_date - datetime .timedelta (hours = 18 )
141133 time .units = 'hours since %s' % start_date_6hourly .strftime ('%Y-%m-%d %H:%M:%S.0' )
142134 time .frequency = 6
135+ elif 'internal.time.unit' in settings .binding :
136+ internal_time_units = settings .binding ['internal.time.unit' ]
137+ # Separate the different parts of time units, ex:
138+ # "seconds since 1970-01-01 00:00:00"
139+ # "days since 1970-01-01 00:00:00"
140+ # "hours since 1970-01-01 00:00:00"
141+ # "minutes since 1970-01-01 00:00:00"
142+ time_units_parts = internal_time_units .split (' ' )
143+ time .units = '%s since %s' % (time_units_parts [0 ], start_date .strftime ('%Y-%m-%d %H:%M:%S.0' ))
144+ time .frequency = int (settings .binding ['internal.time.frequency' ])
143145 else :
144146 time .units = 'days since %s' % start_date .strftime ('%Y-%m-%d %H:%M:%S.0' )
145147 time .frequency = 1
@@ -276,19 +278,21 @@ def writenet(current_output_index, inputmap, netcdf_output_file, current_timeste
276278 netfile: output netcdf filename
277279 timestep:
278280 """
279- start_date = calendar_day_start + datetime .timedelta (days = 1 )
281+ settings = LisSettings .instance ()
282+
283+ timestep_stride = int (settings .binding ['DtSec' ])
284+ time_frequency = int (settings .binding ['internal.time.frequency' ])
285+ start_date = calendar_day_start + datetime .timedelta (seconds = timestep_stride )
280286 timestep = current_timestep
281287
282288 cutmap = CutMap .instance ()
283289 nrows = np .abs (cutmap .cuts [3 ] - cutmap .cuts [2 ])
284290 ncols = np .abs (cutmap .cuts [1 ] - cutmap .cuts [0 ])
285291
286- settings = LisSettings .instance ()
287-
288292 time_variable = 'time'
289293 output6hourly = settings .get_option ('output6hourly' )
290- prefix , netfile , output_index = get_output_parameters (settings , netcdf_output_file , start_date ,
291- timestep , current_output_index )
294+ prefix , netfile , output_index = get_output_parameters (settings , netcdf_output_file , start_date , timestep ,
295+ time_frequency , timestep_stride , current_output_index )
292296
293297 # Create and setup the netcdf file when the first map needs to be stored
294298 if output_index == 0 or not netfile .exists ():
@@ -304,15 +308,15 @@ def writenet(current_output_index, inputmap, netcdf_output_file, current_timeste
304308 mapnp [np .isnan (mapnp )] = (nan_value - add_offset ) * scale_factor
305309 if flag_time :
306310 # In case output6hourly==True, replicate four daily maps to get the 6 hourly output (EFCC-2316)
307- # The timestep need to increase by 4
311+ # The timestep needs to increase by 4
308312 if output6hourly :
309313 time_frequency = 6
310314 for i in range (4 ):
311315 map_idx = output_index * 4 + i
312316 nf1 .variables [time_variable ][map_idx ] = (timestep * 4 - 4 + i ) * time_frequency
313317 nf1 .variables [prefix ][map_idx , :, :] = mapnp
314318 else : # Generate daily output
315- nf1 .variables [time_variable ][output_index ] = timestep - 1
319+ nf1 .variables [time_variable ][output_index ] = ( timestep - 1 ) * time_frequency # timestep - time_frequency
316320 nf1 .variables [prefix ][output_index , :, :] = mapnp
317321 else :
318322 nf1 .variables [prefix ][:, :] = mapnp
0 commit comments