-
Notifications
You must be signed in to change notification settings - Fork 37
/
variability_across_timescales_PS_driver.py
161 lines (147 loc) · 4.42 KB
/
variability_across_timescales_PS_driver.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
import copy
import glob
import os
import sys
import cdms2 as cdms
import MV2 as MV
from genutil import StringConstructor
import pcmdi_metrics
from pcmdi_metrics.driver.pmp_parser import PMPParser
from pcmdi_metrics.precip_variability.lib import (
AddParserArgument,
Avg_PS_DomFrq,
ClimAnom,
Powerspectrum,
Regrid2deg,
)
# Read parameters
P = PMPParser()
P = AddParserArgument(P)
param = P.get_parameter()
mip = param.mip
mod = param.mod
var = param.var
dfrq = param.frq
modpath = param.modpath
prd = param.prd
fac = param.fac
nperseg = param.nperseg
noverlap = param.noverlap
print(modpath)
print(mod)
print(prd)
print(nperseg, noverlap)
# Get flag for CMEC output
cmec = param.cmec
# Create output directory
case_id = param.case_id
outdir_template = param.process_templated_argument("results_dir")
outdir = StringConstructor(
str(outdir_template(output_type="%(output_type)", mip=mip, case_id=case_id))
)
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
try:
os.makedirs(outdir(output_type=output_type))
except FileExistsError:
pass
print(outdir(output_type=output_type))
# Read data
file_list = sorted(glob.glob(os.path.join(modpath, "*" + mod + "*")))
f = []
data = []
for ifl in range(len(file_list)):
f.append(cdms.open(file_list[ifl]))
file = file_list[ifl]
if mip == "obs":
model = file.split("/")[-1].split(".")[2]
data.append(model)
else:
model = file.split("/")[-1].split(".")[2]
ens = file.split("/")[-1].split(".")[3]
data.append(model + "." + ens)
print("# of data:", len(data))
print(data)
# Regridding -> Anomaly -> Power spectra -> Domain&Frequency average -> Write
psdmfm = {"RESULTS": {}}
syr = prd[0]
eyr = prd[1]
for id, dat in enumerate(data):
cal = f[id][var].getTime().calendar
if "360" in cal:
ldy = 30
else:
ldy = 31
print(dat, cal)
for iyr in range(syr, eyr + 1):
do = (
f[id](
var,
time=(
str(iyr) + "-1-1 0:0:0",
str(iyr) + "-12-" + str(ldy) + " 23:59:59",
),
)
* float(fac)
)
# Regridding
rgtmp = Regrid2deg(do)
if iyr == syr:
drg = copy.deepcopy(rgtmp)
else:
drg = MV.concatenate((drg, rgtmp))
print(iyr, drg.shape)
# Anomaly
if dfrq == "day":
ntd = 1
elif dfrq == "3hr":
ntd = 8
else:
sys.exit("ERROR: dfrq " + dfrq + " is not defined!")
clim, anom = ClimAnom(drg, ntd, syr, eyr)
# Power spectum of total
freqs, ps, rn, sig95 = Powerspectrum(drg, nperseg, noverlap)
# Domain & Frequency average
psdmfm_forced = Avg_PS_DomFrq(ps, freqs, ntd, dat, mip, "forced")
# Write data (nc file)
outfilename = "PS_pr." + str(dfrq) + "_regrid.180x90_" + dat + ".nc"
with cdms.open(
os.path.join(outdir(output_type="diagnostic_results"), outfilename), "w"
) as out:
out.write(freqs, id="freqs")
out.write(ps, id="power")
out.write(rn, id="rednoise")
out.write(sig95, id="sig95")
# Power spectum of anomaly
freqs, ps, rn, sig95 = Powerspectrum(anom, nperseg, noverlap)
# Domain & Frequency average
psdmfm_unforced = Avg_PS_DomFrq(ps, freqs, ntd, dat, mip, "unforced")
# Write data (nc file)
outfilename = "PS_pr." + str(dfrq) + "_regrid.180x90_" + dat + "_unforced.nc"
with cdms.open(
os.path.join(outdir(output_type="diagnostic_results"), outfilename), "w"
) as out:
out.write(freqs, id="freqs")
out.write(ps, id="power")
out.write(rn, id="rednoise")
out.write(sig95, id="sig95")
# Write data (json file)
psdmfm["RESULTS"][dat] = {}
psdmfm["RESULTS"][dat]["forced"] = psdmfm_forced
psdmfm["RESULTS"][dat]["unforced"] = psdmfm_unforced
outfilename = (
"PS_pr." + str(dfrq) + "_regrid.180x90_area.freq.mean_" + dat + ".json"
)
JSON = pcmdi_metrics.io.base.Base(
outdir(output_type="metrics_results"), outfilename
)
JSON.write(
psdmfm,
json_structure=["model+realization", "variability type", "domain", "frequency"],
sort_keys=True,
indent=4,
separators=(",", ": "),
)
if cmec:
JSON.write_cmec(indent=4, separators=(",", ": "))