Skip to content
Snippets Groups Projects
Commit 4fdfa76b authored by Frisinghelli Daniel's avatar Frisinghelli Daniel
Browse files

Improved logging for aggregation.

parent 2ce1f770
No related branches found
No related tags found
No related merge requests found
...@@ -148,6 +148,7 @@ if __name__ == '__main__': ...@@ -148,6 +148,7 @@ if __name__ == '__main__':
# check whether to aggregate the netcdf files of a simulation covering # check whether to aggregate the netcdf files of a simulation covering
# differnt time periods into a single file # differnt time periods into a single file
if args.aggregate: if args.aggregate:
LogConfig.init_log('Aggregating time periods of simulations.')
# list of unique simulations # list of unique simulations
simulations = np.unique([file.stem.rpartition('_')[0] for file in simulations = np.unique([file.stem.rpartition('_')[0] for file in
target]) target])
...@@ -159,23 +160,32 @@ if __name__ == '__main__': ...@@ -159,23 +160,32 @@ if __name__ == '__main__':
group = [file for file in target if file.name.startswith(sim)] group = [file for file in target if file.name.startswith(sim)]
group = sorted(group) group = sorted(group)
# read multiple netcdf files using xarray and dask
ds = xr.open_mfdataset(group)
# create filename for netcdf covering the entire time period of # create filename for netcdf covering the entire time period of
# the current simulation # the current simulation
y_min, _ = _parse_cordex_time_span(group[0]) # first year y_min, _ = _parse_cordex_time_span(group[0]) # first year
_, y_max = _parse_cordex_time_span(group[-1]) # last year _, y_max = _parse_cordex_time_span(group[-1]) # last year
filename = '_'.join([sim, '-'.join([y_min, y_max])]) filename = '_'.join([sim, '-'.join([y_min, y_max])])
filename = file.parent.joinpath(filename) filename = group[0].parent.joinpath(filename)
# log simulation name, time span and files
LOGGER.info('Aggregating simulation: {}, Time span: {}'
.format(sim, '-'.join([y_min, y_max])))
LOGGER.info(('\n ' + (len(__name__) + 1) * ' ').join(
['{}'.format(file) for file in group]))
# read multiple netcdf files using xarray and dask
ds = xr.open_mfdataset(group)
# save aggregated netcdf file # save aggregated netcdf file
LOGGER.info('Saving aggregated NetCDF: {}'.format(filename))
ds.to_netcdf(filename) ds.to_netcdf(filename)
# remove single netcdf files from disk # remove single netcdf files from disk
if args.remove: if args.remove:
LOGGER.info('Removing individual NetCDF files ...')
for file in group: for file in group:
file.unlink() file.unlink()
LOGGER.info('rm {}'.format(file))
else: else:
LOGGER.info('{} does not exist.'.format(str(args.source))) LOGGER.info('{} does not exist.'.format(str(args.source)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment