@@ -63,55 +63,53 @@ def coordinates_range(start=0, nelems=1, step=1):
63
63
return elem_array
64
64
65
65
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 ):
67
67
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
+ first_date_last_month = day_inside_last_month .replace (day = 1 ) + datetime .timedelta (seconds = (2 * timestep_stride ))
82
+ num_steps_done_in_last_month = int ((last_date_last_month - first_date_last_month ).total_seconds () / timestep_stride ) + 1
83
+ output_index = num_steps_done_in_last_month
84
+ elif current_output_index >= num_steps_done_in_current_month :
85
+ output_index = num_steps_done_in_current_month - 1
87
86
return filename_suffix , output_index
88
87
89
88
90
- def get_output_parameters_yearly (start_date , timestep , current_output_index ):
89
+ def get_output_parameters_yearly (start_date , timestep , time_frequency , timestep_stride , current_output_index ):
91
90
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
91
+ current_date = start_date + datetime .timedelta (seconds = ((timestep - 1 ) * timestep_stride ))
92
+ filename_suffix = current_date .strftime ('%Y' )
93
+
94
+ first_date_current_year = start_date .replace (year = current_date .year )
95
+ seconds_between = (current_date - first_date_current_year ).total_seconds ()
96
+ num_steps_done_in_current_year = int (seconds_between / timestep_stride ) + 1
97
+ last_date_last_year = first_date_current_year - datetime .timedelta (seconds = timestep_stride )
98
+ day_inside_last_year = last_date_last_year - datetime .timedelta (seconds = timestep_stride )
99
+
100
+ if current_date == first_date_current_year :
101
+ output_index = 0
102
+ elif current_date == last_date_last_year :
103
+ filename_suffix = day_inside_last_year .strftime ('%Y' )
104
+ first_date_last_year = first_date_current_year .replace (year = current_date .year - 1 )
105
+ num_steps_done_in_last_year = int ((last_date_last_year - first_date_last_year ).total_seconds () / timestep_stride ) + 1
106
+ output_index = num_steps_done_in_last_year - 1
107
+ elif current_output_index >= num_steps_done_in_current_year :
108
+ output_index = num_steps_done_in_current_year - 1
111
109
return filename_suffix , output_index
112
110
113
111
114
- def get_output_parameters (settings , netcdf_output_file , start_date , timestep , current_output_index ):
112
+ def get_output_parameters (settings , netcdf_output_file , start_date , timestep , time_frequency , timestep_stride , current_output_index ):
115
113
output_index = current_output_index
116
114
p = Path (netcdf_output_file )
117
115
netfile = Path (p .parent ) / Path ('{}.nc' .format (p .name ) if not p .name .endswith ('.nc' ) else p .name )
@@ -120,9 +118,9 @@ def get_output_parameters(settings, netcdf_output_file, start_date, timestep, cu
120
118
if splitIO :
121
119
monthlyIO = settings .get_option ('monthlyOutput' )
122
120
if monthlyIO :
123
- filename_suffix , output_index = get_output_parameters_monthly (start_date , timestep , current_output_index )
121
+ filename_suffix , output_index = get_output_parameters_monthly (start_date , timestep , time_frequency , timestep_stride , current_output_index )
124
122
else :
125
- filename_suffix , output_index = get_output_parameters_yearly (start_date , timestep , current_output_index )
123
+ filename_suffix , output_index = get_output_parameters_yearly (start_date , timestep , time_frequency , timestep_stride , current_output_index )
126
124
netfile = Path (p .parent ) / Path ('{}_{}.nc' .format (p .name , filename_suffix ) if not p .name .endswith ('.nc' ) else p .name )
127
125
return prefix , netfile , output_index
128
126
@@ -140,6 +138,16 @@ def set_time_dimension(settings, netcdf_obj, time_variable_name, start_date, var
140
138
start_date_6hourly = start_date - datetime .timedelta (hours = 18 )
141
139
time .units = 'hours since %s' % start_date_6hourly .strftime ('%Y-%m-%d %H:%M:%S.0' )
142
140
time .frequency = 6
141
+ elif 'internal.time.unit' in settings .binding :
142
+ internal_time_units = settings .binding ['internal.time.unit' ]
143
+ # Separate the different parts of time units, ex:
144
+ # "seconds since 1970-01-01 00:00:00"
145
+ # "days since 1970-01-01 00:00:00"
146
+ # "hours since 1970-01-01 00:00:00"
147
+ # "minutes since 1970-01-01 00:00:00"
148
+ time_units_parts = internal_time_units .split (' ' )
149
+ time .units = '%s since %s' % (time_units_parts [0 ], start_date .strftime ('%Y-%m-%d %H:%M:%S.0' ))
150
+ time .frequency = int (settings .binding ['internal.time.frequency' ])
143
151
else :
144
152
time .units = 'days since %s' % start_date .strftime ('%Y-%m-%d %H:%M:%S.0' )
145
153
time .frequency = 1
@@ -276,19 +284,21 @@ def writenet(current_output_index, inputmap, netcdf_output_file, current_timeste
276
284
netfile: output netcdf filename
277
285
timestep:
278
286
"""
279
- start_date = calendar_day_start + datetime .timedelta (days = 1 )
287
+ settings = LisSettings .instance ()
288
+
289
+ timestep_stride = int (settings .binding ['DtSec' ])
290
+ time_frequency = int (settings .binding ['internal.time.frequency' ])
291
+ start_date = calendar_day_start + datetime .timedelta (seconds = timestep_stride )
280
292
timestep = current_timestep
281
293
282
294
cutmap = CutMap .instance ()
283
295
nrows = np .abs (cutmap .cuts [3 ] - cutmap .cuts [2 ])
284
296
ncols = np .abs (cutmap .cuts [1 ] - cutmap .cuts [0 ])
285
297
286
- settings = LisSettings .instance ()
287
-
288
298
time_variable = 'time'
289
299
output6hourly = settings .get_option ('output6hourly' )
290
- prefix , netfile , output_index = get_output_parameters (settings , netcdf_output_file , start_date ,
291
- timestep , current_output_index )
300
+ prefix , netfile , output_index = get_output_parameters (settings , netcdf_output_file , start_date , timestep ,
301
+ time_frequency , timestep_stride , current_output_index )
292
302
293
303
# Create and setup the netcdf file when the first map needs to be stored
294
304
if output_index == 0 or not netfile .exists ():
@@ -304,15 +314,15 @@ def writenet(current_output_index, inputmap, netcdf_output_file, current_timeste
304
314
mapnp [np .isnan (mapnp )] = (nan_value - add_offset ) * scale_factor
305
315
if flag_time :
306
316
# In case output6hourly==True, replicate four daily maps to get the 6 hourly output (EFCC-2316)
307
- # The timestep need to increase by 4
317
+ # The timestep needs to increase by 4
308
318
if output6hourly :
309
319
time_frequency = 6
310
320
for i in range (4 ):
311
321
map_idx = output_index * 4 + i
312
322
nf1 .variables [time_variable ][map_idx ] = (timestep * 4 - 4 + i ) * time_frequency
313
323
nf1 .variables [prefix ][map_idx , :, :] = mapnp
314
324
else : # Generate daily output
315
- nf1 .variables [time_variable ][output_index ] = timestep - 1
325
+ nf1 .variables [time_variable ][output_index ] = ( timestep - 1 ) * time_frequency # timestep - time_frequency
316
326
nf1 .variables [prefix ][output_index , :, :] = mapnp
317
327
else :
318
328
nf1 .variables [prefix ][:, :] = mapnp
0 commit comments