diff --git a/video_prediction_tools/main_scripts/main_visualize_postprocess.py b/video_prediction_tools/main_scripts/main_visualize_postprocess.py index 0c6504115baa11c6764e1d3933b493c7f4acfe8c..9d008cf0e756b6842efe29aa8a4c343818abfd8d 100644 --- a/video_prediction_tools/main_scripts/main_visualize_postprocess.py +++ b/video_prediction_tools/main_scripts/main_visualize_postprocess.py @@ -19,7 +19,8 @@ import json from typing import Union, List # own modules from normalization import Norm_data -from general_utils import get_era5_varatts, check_dir +from netcdf_datahandling import get_era5_varatts +from general_utils import check_dir from metadata import MetaData as MetaData from main_scripts.main_train_models import * from data_preprocess.preprocess_data_step2 import * diff --git a/video_prediction_tools/utils/general_utils.py b/video_prediction_tools/utils/general_utils.py index 9ab152bac87c76cc2e37341df597133e5af9089b..73d0366ea89f85f81a23ed17e61c5873f3cad812 100644 --- a/video_prediction_tools/utils/general_utils.py +++ b/video_prediction_tools/utils/general_utils.py @@ -14,7 +14,7 @@ Provides: * get_unique_vars # import modules import os import numpy as np -import xarray as xr +#import xarray as xr # routines def get_unique_vars(varnames): @@ -199,38 +199,3 @@ def provide_default(dict_in, keyname, default=None, required=False): return dict_in[keyname] -def get_era5_varatts(data_arr: xr.DataArray, name: str): - """ - Writes longname and unit to data arrays given their name is known - :param data_arr: the data array - :param name: the name of the variable - :return: data array with added attributes 'longname' and 'unit' if they are known - """ - - era5_varname_map = {"2t": "2m temperature", "t_850": "850 hPa temperature", "tcc": "total cloud cover", - "msl": "mean sealevel pressure", "10u": "10m u-wind", "10v": "10m v-wind"} - era5_varunit_map = {"2t": "K", "t_850": "K", "tcc": "%", - "msl": "Pa", "10u": "m/s", "10v": "m/s"} - - name_splitted = name.split("_") - if "fcst" in name: - addstr = "from {0} model".format(name_splitted[1]) - elif "ref" in name: - addstr = "from ERA5 reanalysis" - else: - addstr = "" - - longname = provide_default(era5_varname_map, name_splitted[0], -1) - if longname == -1: - pass - else: - data_arr.attrs["longname"] = "{0} {1}".format(longname, addstr) - - unit = provide_default(era5_varunit_map, name_splitted[0], -1) - if unit == -1: - pass - else: - data_arr.attrs["unit"] = unit - - return data_arr - diff --git a/video_prediction_tools/utils/netcdf_datahandling.py b/video_prediction_tools/utils/netcdf_datahandling.py index a66611c63b5ce42b5432ccd7fbb70fa764c9271c..210b3e2e931f899f467e2592f02f0122541d5bd3 100644 --- a/video_prediction_tools/utils/netcdf_datahandling.py +++ b/video_prediction_tools/utils/netcdf_datahandling.py @@ -1,6 +1,10 @@ """ Classes to handle netCDF-data files and to extract gridded data on a subdomain -(e.g. used for handling ERA5-reanalysis data) +(e.g. used for handling ERA5-reanalysis data) + +Content: * get_era5_varatts (auxiliary function!) + * NetcdfUtils + * GeoSubdomain """ __email__ = "b.gong@fz-juelich.de" @@ -13,6 +17,44 @@ import numpy as np import xarray as xr from general_utils import is_integer, add_str_to_path, check_str_in_list, isw +# auxiliary function that is not generic enough to be placed in NetcdfUtils + + +def get_era5_varatts(data_arr: xr.DataArray, name: str): + """ + Writes longname and unit to data arrays given their name is known + :param data_arr: the data array + :param name: the name of the variable + :return: data array with added attributes 'longname' and 'unit' if they are known + """ + + era5_varname_map = {"2t": "2m temperature", "t_850": "850 hPa temperature", "tcc": "total cloud cover", + "msl": "mean sealevel pressure", "10u": "10m u-wind", "10v": "10m v-wind"} + era5_varunit_map = {"2t": "K", "t_850": "K", "tcc": "%", + "msl": "Pa", "10u": "m/s", "10v": "m/s"} + + name_splitted = name.split("_") + if "fcst" in name: + addstr = "from {0} model".format(name_splitted[1]) + elif "ref" in name: + addstr = "from ERA5 reanalysis" + else: + addstr = "" + + longname = provide_default(era5_varname_map, name_splitted[0], -1) + if longname == -1: + pass + else: + data_arr.attrs["longname"] = "{0} {1}".format(longname, addstr) + + unit = provide_default(era5_varunit_map, name_splitted[0], -1) + if unit == -1: + pass + else: + data_arr.attrs["unit"] = unit + + return data_arr + class NetcdfUtils: """