-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg_ens_data.py
69 lines (52 loc) · 2.36 KB
/
agg_ens_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Python modules for aggregating an ensemble of across year mean files,
and writing output netcdf file
"""
import os
import numpy as np
import pandas as pd
import xarray as xr
from optparse import OptionParser
from plotdailymean import plot_timeseries
from writeoutfile import write_netcdf_output
__author__ = 'Eva Sinha'
__email__ = '[email protected]'
parser = OptionParser();
parser.add_option("--crop", dest="crop", default="", \
help="Modeled crop name")
parser.add_option("--outdir", dest="outdir", default="", \
help="Directory where ELM ensemble files containing across year mean are stored")
parser.add_option("--nsamples", dest="nsamples", default="", \
help="Number of parameters samples considered in the ensemble")
parser.add_option("--caseid", dest="caseid", default="", \
help="Case name")
parser.add_option("--fnamepre", dest="fnamepre", default="", \
help="file name prefix for saving plots")
(options, args) = parser.parse_args()
#----------------------------------------------------------
# Read names of all NetCDF files
fnames = []
for ind in range(1, int(options.nsamples)+1):
fnames.append(options.outdir + options.fnamepre + '/indiv_ens_member/' + options.caseid + '_mean_across_years_' + str(ind) + '.nc')
# Open a multiple netCDF data file and load the data into xarrays
with xr.open_mfdataset(fnames, concat_dim='ensemble', combine='nested') as ds:
# Reorder dimensions
ds = ds.transpose('dayofyear', 'ensemble')
# Assign coordinates to ensemble
ds = ds.assign_coords(ensemble=range(1, int(options.nsamples)+1))
# Save output xarray as a netcdf file
outfname = options.caseid + '_mean_across_years.nc'
write_netcdf_output(ds, options.fnamepre, filename=outfname)
# Conversion constant
CONV_SEC_DAY = 1 / (24 * 60 * 60)
# List of variable names that we want to keep
varnames=['GPP', 'NPP', 'NEE', 'ER']
# Create array of ylabels for each plot
ylabel = ['GPP [$gC~m^{-2}~day^{-1}$]', 'NPP [$gC~m^{-2}~day^{-1}$]',
'NEE [$gC~m^{-2}~day^{-1}$]','ER [$gC~m^{-2}~day^{-1}$]']
# Title for plot
title = options.crop
conv_factor = [CONV_SEC_DAY, CONV_SEC_DAY, CONV_SEC_DAY, CONV_SEC_DAY]
# Plot timeseries
plot_timeseries(outfname, varnames, title, ylabel, conv_factor, options.fnamepre)
print('Finished plotting')