From b120f4f3e8cf63c0207bf0cb9235a2b6837ecb3b Mon Sep 17 00:00:00 2001 From: "Fernandez Vilanova, Lucas" Date: Fri, 5 Apr 2024 11:34:00 +0000 Subject: [PATCH] minor improvements --- aiida_flexpart/calculations/collect_sens.py | 3 +- aiida_flexpart/data/nc_data.py | 98 ++++--------------- .../workflows/multi_dates_workflow.py | 10 +- 3 files changed, 25 insertions(+), 86 deletions(-) diff --git a/aiida_flexpart/calculations/collect_sens.py b/aiida_flexpart/calculations/collect_sens.py index 466eaa5..96a310e 100644 --- a/aiida_flexpart/calculations/collect_sens.py +++ b/aiida_flexpart/calculations/collect_sens.py @@ -29,6 +29,7 @@ def define(cls, spec): spec.input('metadata.options.custom_scheduler_commands', valid_type=str, default='') spec.input('metadata.options.withmpi', valid_type=bool, default=False) spec.input('metadata.options.output_filename', valid_type=str, default='aiida.out', required=True) + spec.input('metadata.options.parser_name', valid_type=str, default='collect.sens') #Inputs spec.input_namespace('remote', valid_type=orm.RemoteStashFolderData, required=True) @@ -68,7 +69,7 @@ def prepare_for_submission(self, folder): 'domain.str':self.inputs.outgrid, 'nest':self.inputs.outgrid_n, 'met_model':self.inputs.model, - 'model_version':'FLEXPART '+str_ + 'model_version':'FLEXPART '+ str_ }) _ = yaml.dump(params_dict, f) diff --git a/aiida_flexpart/data/nc_data.py b/aiida_flexpart/data/nc_data.py index 5062989..89208b9 100644 --- a/aiida_flexpart/data/nc_data.py +++ b/aiida_flexpart/data/nc_data.py @@ -4,76 +4,11 @@ import tempfile from pathlib import Path -def ncdump(nc_fid, verb=True): - ''' - ncdump outputs dimensions, variables and their attribute information. - The information is similar to that of NCAR's ncdump utility. - ncdump requires a valid instance of Dataset. - - Parameters - ---------- - nc_fid : netCDF4.Dataset - A netCDF4 dateset object - verb : Boolean - whether or not nc_attrs, nc_dims, and nc_vars are printed - - Returns - ------- - nc_attrs : list - A Python list of the NetCDF file global attributes - nc_dims : list - A Python list of the NetCDF file dimensions - nc_vars : list - A Python list of the NetCDF file variables - ''' - def print_ncattr(key): - """ - Prints the NetCDF file attributes for a given key - - Parameters - ---------- - key : unicode - a valid netCDF4.Dataset.variables key - """ - try: - print("\t\ttype:", repr(nc_fid.variables[key].dtype)) - for ncattr in nc_fid.variables[key].ncattrs(): - print('\t\t%s:' % ncattr,\ - repr(nc_fid.variables[key].getncattr(ncattr))) - except KeyError: - print("\t\tWARNING: %s does not contain variable attributes" % key) - - # NetCDF global attributes - nc_attrs = nc_fid.ncattrs() - if verb: - print ("Global Attributes:") - for nc_attr in nc_attrs: - print('\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr))) - nc_dims = [dim for dim in nc_fid.dimensions] # list of nc dimensions - # Dimension shape information. - if verb: - print("dimension information:") - for dim in nc_dims: - print(dim) - print("\t\tsize:", len(nc_fid.dimensions[dim])) - print_ncattr(dim) - # Variable information. - nc_vars = [var for var in nc_fid.variables] # list of nc variables - if verb: - print("variable information:") - for var in nc_vars: - if var not in nc_dims: - print(var) - print("\t\tdimensions:", nc_fid.variables[var].dimensions) - print("\t\tsize:", nc_fid.variables[var].size) - print_ncattr(var) - - class NetCDFData(Data): def __init__(self, filepath, **kwargs): """ - Data plugin for Netcdf files. + Data plugin for Netcdf objects. """ super().__init__(**kwargs) @@ -81,24 +16,25 @@ def __init__(self, filepath, **kwargs): nc_file = Dataset(filepath, mode='r') filename = os.path.basename(filepath) - self.put_object_from_file(filepath, filename) - self.base.attributes.set('filename', filename) + self.put_object_from_file(filepath, filename) self._nc_file = nc_file + self._set_attributes(nc_file, filename) + + def _set_attributes(self, nc_file, filename): - att_dict = {} + g_att = {} for a in nc_file.ncattrs(): - att_dict[a] = repr(nc_file.getncattr(a)) + g_att[a] = repr(nc_file.getncattr(a)) - self.base.attributes.set('global_attributes', att_dict) + self.base.attributes.set('global_attributes', g_att) + self.base.attributes.set('filename', filename) - def _get_netcdf(self): - if self.is_stored: - return self._nc_file + def _get_nc(self): + filename = self.base.attributes.get('filename') + with tempfile.TemporaryDirectory() as td: + self.copy_tree(Path(td)) + nc_file = Dataset(Path(td)/filename, mode='r') + return nc_file - def get_info(self): - """ - Print netcdf info in a similar format as ncdump -h - """ - ncdump(self._nc_file) - - \ No newline at end of file + def _get_netcdf(self): + return self._get_nc_from_repo() \ No newline at end of file diff --git a/aiida_flexpart/workflows/multi_dates_workflow.py b/aiida_flexpart/workflows/multi_dates_workflow.py index 20a889c..e2cc706 100644 --- a/aiida_flexpart/workflows/multi_dates_workflow.py +++ b/aiida_flexpart/workflows/multi_dates_workflow.py @@ -111,10 +111,12 @@ def define(cls, spec): spec.outline( cls.setup, engine.while_(cls.condition)( - engine.if_(cls.run_cosmo)(engine.if_( - cls.prepare_meteo_folder_cosmo)(cls.run_cosmo_simulation)), - engine.if_(cls.run_ifs)(engine.if_( - cls.prepare_meteo_folder_ifs)(cls.run_ifs_simulation)), + engine.if_(cls.run_cosmo)( + engine.if_(cls.prepare_meteo_folder_cosmo)( + cls.run_cosmo_simulation)), + engine.if_(cls.run_ifs)( + engine.if_(cls.prepare_meteo_folder_ifs)( + cls.run_ifs_simulation)), cls.post_processing, ), cls.results,