Skip to content

Commit b5555e9

Browse files
committed
only reduce number_of_files if it exists
1 parent fb0b3ab commit b5555e9

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

pyxlma/lmalib/io/read.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,30 +210,31 @@ def reorder_stations(dataset, index_mapping):
210210
# Update the global attributes
211211
all_data.attrs.update(final_attrs)
212212
# To reduce complexity and resource usage, if the 'number_of_files' dimension is the same for all variables, then the dimension is unnecessary
213-
all_the_same = True
214-
# These variables are expected to change between files, so they are not checked for equality
215-
expected_to_change = ['station_event_fraction', 'station_power_ratio', 'file_start_time']
216-
for var in all_data.data_vars:
217-
if var in expected_to_change:
218-
continue
219-
if 'number_of_files' in all_data[var].dims:
220-
if (all_data[var] == all_data[var].isel(number_of_files=0)).all():
221-
pass
222-
else:
223-
all_the_same = False
224-
# If all of the variables are the same for all files, remove the 'number_of_files' dimension.
225-
if all_the_same:
226-
# Some variables need to be averaged
227-
mean_data = all_data.mean(dim='number_of_files')
228-
# ...but most can just be copied over (this is necessary because some are strings which can't be averaged)
229-
all_data = all_data.isel(number_of_files=0)
230-
# the file_start_time variable is not needed in the reduced dataset
231-
all_data = all_data.drop_vars(['file_start_time'])
232-
# replace the copied variables with the averaged variables where necessary
233-
for var in expected_to_change:
234-
if var == 'file_start_time':
213+
if 'number_of_files' in all_data.dims:
214+
all_the_same = True
215+
# These variables are expected to change between files, so they are not checked for equality
216+
expected_to_change = ['station_event_fraction', 'station_power_ratio', 'file_start_time']
217+
for var in all_data.data_vars:
218+
if var in expected_to_change:
235219
continue
236-
all_data[var] = mean_data[var]
220+
if 'number_of_files' in all_data[var].dims:
221+
if (all_data[var] == all_data[var].isel(number_of_files=0)).all():
222+
pass
223+
else:
224+
all_the_same = False
225+
# If all of the variables are the same for all files, remove the 'number_of_files' dimension.
226+
if all_the_same:
227+
# Some variables need to be averaged
228+
mean_data = all_data.mean(dim='number_of_files')
229+
# ...but most can just be copied over (this is necessary because some are strings which can't be averaged)
230+
all_data = all_data.isel(number_of_files=0)
231+
# the file_start_time variable is not needed in the reduced dataset
232+
all_data = all_data.drop_vars(['file_start_time'])
233+
# replace the copied variables with the averaged variables where necessary
234+
for var in expected_to_change:
235+
if var == 'file_start_time':
236+
continue
237+
all_data[var] = mean_data[var]
237238
return all_data
238239

239240
def dataset(filenames, sort_time=True):

0 commit comments

Comments
 (0)