From f28f38b596d014cf7191928d760848fa307d8e78 Mon Sep 17 00:00:00 2001 From: Christina Kalb Date: Tue, 16 May 2023 12:16:36 -0600 Subject: [PATCH 1/5] Added new Spacetime files --- .../contributed/spacetime/cross_spectra.py | 98 ++++++++++++------- .../contributed/spacetime/spectra_comp.yaml | 12 +++ 2 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 metcalcpy/contributed/spacetime/spectra_comp.yaml diff --git a/metcalcpy/contributed/spacetime/cross_spectra.py b/metcalcpy/contributed/spacetime/cross_spectra.py index a7adb497..d4e9c5ab 100644 --- a/metcalcpy/contributed/spacetime/cross_spectra.py +++ b/metcalcpy/contributed/spacetime/cross_spectra.py @@ -13,6 +13,7 @@ data sets. """ import argparse +import os import xarray as xr import numpy as np @@ -24,6 +25,24 @@ from spacetime import get_symmasymm from spacetime_utils import save_Spectra +import metcalcpy.util.read_env_vars_in_config as readconfig + + +# Read in the YAML config file +# user can use their own, if none specified at the command line, +# use the "default" example YAML config file, spectra_plot_coh2.py +# Using a custom YAML reader so we can use environment variables +cross_config_file = os.getenv("YAML_CONFIG_NAME","spectra_comp.yaml") + +config_dict = readconfig.parse_config(cross_config_file) + +# Retrieve settings from config file +#pathdata is now set in the METplus conf file +pathout = config_dict['pathout'][0] +print("Output path ",pathout) +model = config_dict['model'] +print("Model ",model) + """ Set parameters for the spectra calculation. spd: Number of observations per day. @@ -35,62 +54,68 @@ latMin: Minimum latitude to include. latMax: Maximum latitude to include. """ -spd = 2 -nperseg = 46 * spd -segOverLap = -20 * spd -Symmetry = "symm" -latMin = -15. -latMax = 15. -datestrt = '2015-12-01' # plot start date, format: yyyy-mm-dd -datelast = '2016-03-31' # plot end date, format: yyyy-mm-dd - -parser = argparse.ArgumentParser() -parser.add_argument('--datapath', type=str, - help='data path') -parser.add_argument('--pathout', type=str, - help='output path') -args = parser.parse_args() - -# datapath = '../data/' -# path to the location where to save the output spectra -# pathout = '../data/' -datapath = args.datapath -pathout = args.pathout +#spd = 4 +#nperseg = 15 * spd +#segOverLap = -5 * spd +#Symmetry = "symm" +#latMin = -15. +#latMax = 15. +#datestrt = '2014-04-01T06:00:00' # plot start date, format: yyyy-mm-dd +#datelast = '2014-05-04T18:00:00' # plot end date, format: yyyy-mm-dd +spd = config_dict['spd'] +nperseg = config_dict['nperseg']*spd +segOverLap = config_dict['segOverLap']*spd +Symmetry = config_dict['Symmetry'] +latMin = config_dict['latMin'] +latMax = config_dict['latMax'] +datestrt = config_dict['datestrt'] +datelast = config_dict['datelast'] +print('spd: ', spd,', nperseg: ',nperseg,', segOverLap: ',segOverLap) +print('symmetry: ',Symmetry,', latMin: ',latMin,', latMax: ',latMax) +print('datestrt: ',datestrt,', datelast: ',datelast) print("reading data from file:") """ Read in data here. Example: """ -ds = xr.open_dataset(datapath+'precip.trmm.'+str(spd)+'x.1p0.v7a.fillmiss.comp.2014-2016.nc') +filenames = os.environ.get("INPUT_FILE_NAMES","P_verif,P_model,Vlev1_model,Vlev2_model").split(",") +print("Filename ",filenames[1]) +ds = xr.open_dataset(filenames[0]) z = ds.precip z = z.sel(lat=slice(latMin, latMax)) z = z.sel(time=slice(datestrt, datelast)) z = z.squeeze() latC = ds.lat.sel(lat=slice(latMin, latMax)) -ds = xr.open_dataset(datapath+'precip.erai.sfc.1p0.'+str(spd)+'x.2014-2016.nc') -x = ds.precip +ds = xr.open_dataset(filenames[1]) +x = ds.prate x = x.sel(lat=slice(latMin, latMax)) x = x.sel(time=slice(datestrt, datelast)) x = x.squeeze() latA = ds.lat.sel(lat=slice(latMin, latMax)) ds.close() -ds = xr.open_dataset(datapath+'div.erai.850.1p0.'+str(spd)+'x.2014-2016.nc') -y = ds.div +ds = xr.open_dataset(filenames[2]) +ds = ds.rename({'latitude':'lat'}) +ds = ds.sortby('lat',ascending=True) +y = ds.u y = y.sel(lat=slice(latMin, latMax)) y = y.sel(time=slice(datestrt, datelast)) y = y.squeeze() latB = ds.lat.sel(lat=slice(latMin, latMax)) -ds = xr.open_dataset(datapath+'div.erai.200.1p0.'+str(spd)+'x.2014-2016.nc') -w = ds.div -w = y.sel(lat=slice(latMin, latMax)) -w = y.sel(time=slice(datestrt, datelast)) -w = y.squeeze() +ds = xr.open_dataset(filenames[3]) +ds = ds.rename({'latitude':'lat'}) +ds = ds.sortby('lat',ascending=True) +w = ds.u +w = w.sel(lat=slice(latMin, latMax)) +w = w.sel(time=slice(datestrt, datelast)) +w = w.squeeze() latB = ds.lat.sel(lat=slice(latMin, latMax)) - +print(x.shape) +print(y.shape) +print(z.shape) if any(latA - latB) != 0: print("Latitudes must be the same for both variables! Check latitude ordering.") @@ -132,7 +157,7 @@ freq = freq * spd wnum = result['wave'] # save spectra in netcdf file -fileout = 'SpaceTimeSpectra_ERAI_P_D850_' + Symmetry + '_' + str(spd) + 'spd' +fileout = 'SpaceTimeSpectra_'+model+'_P_D850_' + Symmetry + '_' + str(spd) + 'spd' print('saving spectra to file: ' + pathout + fileout + '.nc') save_Spectra(STC, freq, wnum, fileout, pathout) @@ -142,16 +167,17 @@ freq = freq * spd wnum = result['wave'] # save spectra in netcdf file -fileout = 'SpaceTimeSpectra_ERAI_P_D200_' + Symmetry + '_' + str(spd) + 'spd' +fileout = 'SpaceTimeSpectra_'+model+'_P_D200_' + Symmetry + '_' + str(spd) + 'spd' print('saving spectra to file: ' + pathout + fileout + '.nc') save_Spectra(STC, freq, wnum, fileout, pathout) -result = mjo_cross(X, Z, nperseg, segOverLap) +spd = 2 +result = mjo_cross(X.sel(time=Z.time), Z, nperseg, segOverLap) STC = result['STC'] # , freq, wave, number_of_segments, dof, prob, prob_coh2 freq = result['freq'] freq = freq * spd wnum = result['wave'] # save spectra in netcdf file -fileout = 'SpaceTimeSpectra_ERAI_TRMM_P_' + Symmetry + '_' + str(spd) + 'spd' +fileout = 'SpaceTimeSpectra_ERAI_'+model+'_P_' + Symmetry + '_' + str(spd) + 'spd' print('saving spectra to file: ' + pathout + fileout + '.nc') save_Spectra(STC, freq, wnum, fileout, pathout) diff --git a/metcalcpy/contributed/spacetime/spectra_comp.yaml b/metcalcpy/contributed/spacetime/spectra_comp.yaml new file mode 100644 index 00000000..a643e55b --- /dev/null +++ b/metcalcpy/contributed/spacetime/spectra_comp.yaml @@ -0,0 +1,12 @@ +pathout: +- !ENV '${OUTPUT_DIR}' + +model: 'ufs_p7' +spd : 4 +nperseg : 15 # in days +segOverLap : -5 # in days +Symmetry : 'symm' +latMin : -15. +latMax : 15. +datestrt : '2014-04-01T06:00:00' # start date, format: yyyy-mm-ddTHH:MM:ss +datelast : '2014-05-04T18:00:00' # end date, format: yyyy-mm-ddTHH:MM:ss From 14cd159e6900cdac226836b208383385bf2695eb Mon Sep 17 00:00:00 2001 From: Christina Kalb Date: Tue, 6 Jun 2023 17:13:27 -0600 Subject: [PATCH 2/5] Updated spacetime files --- metcalcpy/contributed/spacetime/spectra_comp.yaml | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 metcalcpy/contributed/spacetime/spectra_comp.yaml diff --git a/metcalcpy/contributed/spacetime/spectra_comp.yaml b/metcalcpy/contributed/spacetime/spectra_comp.yaml deleted file mode 100644 index a643e55b..00000000 --- a/metcalcpy/contributed/spacetime/spectra_comp.yaml +++ /dev/null @@ -1,12 +0,0 @@ -pathout: -- !ENV '${OUTPUT_DIR}' - -model: 'ufs_p7' -spd : 4 -nperseg : 15 # in days -segOverLap : -5 # in days -Symmetry : 'symm' -latMin : -15. -latMax : 15. -datestrt : '2014-04-01T06:00:00' # start date, format: yyyy-mm-ddTHH:MM:ss -datelast : '2014-05-04T18:00:00' # end date, format: yyyy-mm-ddTHH:MM:ss From 3aa996afa4dfd571359d1c401393baad5f89d3bf Mon Sep 17 00:00:00 2001 From: Christina Kalb Date: Tue, 6 Jun 2023 18:44:11 -0600 Subject: [PATCH 3/5] Updated spacetime yaml file name --- metcalcpy/contributed/spacetime/cross_spectra.py | 2 +- metcalcpy/contributed/spacetime/spectra_comp.yaml | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 metcalcpy/contributed/spacetime/spectra_comp.yaml diff --git a/metcalcpy/contributed/spacetime/cross_spectra.py b/metcalcpy/contributed/spacetime/cross_spectra.py index d4e9c5ab..aa378e47 100644 --- a/metcalcpy/contributed/spacetime/cross_spectra.py +++ b/metcalcpy/contributed/spacetime/cross_spectra.py @@ -32,7 +32,7 @@ # user can use their own, if none specified at the command line, # use the "default" example YAML config file, spectra_plot_coh2.py # Using a custom YAML reader so we can use environment variables -cross_config_file = os.getenv("YAML_CONFIG_NAME","spectra_comp.yaml") +cross_config_file = os.getenv("COMP_YAML_CONFIG_NAME","spectra_comp.yaml") config_dict = readconfig.parse_config(cross_config_file) diff --git a/metcalcpy/contributed/spacetime/spectra_comp.yaml b/metcalcpy/contributed/spacetime/spectra_comp.yaml new file mode 100644 index 00000000..a643e55b --- /dev/null +++ b/metcalcpy/contributed/spacetime/spectra_comp.yaml @@ -0,0 +1,12 @@ +pathout: +- !ENV '${OUTPUT_DIR}' + +model: 'ufs_p7' +spd : 4 +nperseg : 15 # in days +segOverLap : -5 # in days +Symmetry : 'symm' +latMin : -15. +latMax : 15. +datestrt : '2014-04-01T06:00:00' # start date, format: yyyy-mm-ddTHH:MM:ss +datelast : '2014-05-04T18:00:00' # end date, format: yyyy-mm-ddTHH:MM:ss From a53bfc048161b02f875920c5c01d26b7cfc23c80 Mon Sep 17 00:00:00 2001 From: Christina Kalb Date: Tue, 6 Jun 2023 18:56:58 -0600 Subject: [PATCH 4/5] Fixed path --- metcalcpy/contributed/spacetime/cross_spectra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metcalcpy/contributed/spacetime/cross_spectra.py b/metcalcpy/contributed/spacetime/cross_spectra.py index aa378e47..7c40f6cc 100644 --- a/metcalcpy/contributed/spacetime/cross_spectra.py +++ b/metcalcpy/contributed/spacetime/cross_spectra.py @@ -78,7 +78,7 @@ """ Read in data here. Example: """ -filenames = os.environ.get("INPUT_FILE_NAMES","P_verif,P_model,Vlev1_model,Vlev2_model").split(",") +filenames = os.environ.get("COMP_INPUT_FILE_NAMES","P_verif,P_model,Vlev1_model,Vlev2_model").split(",") print("Filename ",filenames[1]) ds = xr.open_dataset(filenames[0]) z = ds.precip From 9979f871a8a56ec3f57f911bab1f6b1bdf527550 Mon Sep 17 00:00:00 2001 From: Christina Kalb Date: Tue, 6 Jun 2023 19:25:56 -0600 Subject: [PATCH 5/5] Added line to make output dir if it doesn't exist --- metcalcpy/contributed/spacetime/cross_spectra.py | 8 ++++++-- metcalcpy/contributed/spacetime/spectra_comp.yaml | 12 ------------ 2 files changed, 6 insertions(+), 14 deletions(-) delete mode 100644 metcalcpy/contributed/spacetime/spectra_comp.yaml diff --git a/metcalcpy/contributed/spacetime/cross_spectra.py b/metcalcpy/contributed/spacetime/cross_spectra.py index 7c40f6cc..689207e0 100644 --- a/metcalcpy/contributed/spacetime/cross_spectra.py +++ b/metcalcpy/contributed/spacetime/cross_spectra.py @@ -32,7 +32,7 @@ # user can use their own, if none specified at the command line, # use the "default" example YAML config file, spectra_plot_coh2.py # Using a custom YAML reader so we can use environment variables -cross_config_file = os.getenv("COMP_YAML_CONFIG_NAME","spectra_comp.yaml") +cross_config_file = os.getenv("COMP_SPECTRA_YAML_CONFIG_NAME","spectra_comp.yaml") config_dict = readconfig.parse_config(cross_config_file) @@ -43,6 +43,10 @@ model = config_dict['model'] print("Model ",model) +# Make output directory if it does not exist +if not os.path.exists(pathout): + os.makedirs(pathout) + """ Set parameters for the spectra calculation. spd: Number of observations per day. @@ -78,7 +82,7 @@ """ Read in data here. Example: """ -filenames = os.environ.get("COMP_INPUT_FILE_NAMES","P_verif,P_model,Vlev1_model,Vlev2_model").split(",") +filenames = os.environ.get("COMP_SPECTRA_INPUT_FILE_NAMES","P_verif,P_model,Vlev1_model,Vlev2_model").split(",") print("Filename ",filenames[1]) ds = xr.open_dataset(filenames[0]) z = ds.precip diff --git a/metcalcpy/contributed/spacetime/spectra_comp.yaml b/metcalcpy/contributed/spacetime/spectra_comp.yaml deleted file mode 100644 index a643e55b..00000000 --- a/metcalcpy/contributed/spacetime/spectra_comp.yaml +++ /dev/null @@ -1,12 +0,0 @@ -pathout: -- !ENV '${OUTPUT_DIR}' - -model: 'ufs_p7' -spd : 4 -nperseg : 15 # in days -segOverLap : -5 # in days -Symmetry : 'symm' -latMin : -15. -latMax : 15. -datestrt : '2014-04-01T06:00:00' # start date, format: yyyy-mm-ddTHH:MM:ss -datelast : '2014-05-04T18:00:00' # end date, format: yyyy-mm-ddTHH:MM:ss