-
Notifications
You must be signed in to change notification settings - Fork 0
/
average_esm
executable file
·162 lines (140 loc) · 6.78 KB
/
average_esm
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
162
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
import subprocess as sp
import numpy as np
import re
import sys
import os
cdo="/sw/rhel6-x64/cdo/cdo-1.8.0-gcc48/bin/cdo"
# import flo_utils as fu
debug = False
def co (args):
try:
sp.check_output(args)
except sp.CalledProcessError as ERR:
print ("TRIED")
print (" ".join(args))
print ("GOT")
print (ERR.output.decode('ascii'))
sys.exit(ERR.returncode)
def rm_if_exist(files):
if type (files) is str:
files = [files]
for filename in files:
if (debug):
print ("removing %s if exists"%(filename), file=sys.stderr)
if ( os.access(filename,os.R_OK) ) :
if (debug):
print ("%s exists. Removing it"%(filename), file=sys.stderr)
os.remove(filename)
def mkdir(target):
if not os.path.isdir(target):
if (debug):
print ("creating directory " + target, file=sys.stderr)
os.makedirs(target)
return target
def average_esm(options):
run=options.run
years=np.arange(options.startyear,options.endyear+1)
oceyears = np.arange(options.startyear,options.endyear+1, options.ocestep)
echam_mm = [ "RUNXXXX/outdata/echam6/RUNXXXX_echam6_accw_YYYY.grb",\
"RUNXXXX/outdata/echam6/RUNXXXX_echam6_ATM_mm_YYYY.grb",\
"RUNXXXX/outdata/echam6/RUNXXXX_echam6_BOT_mm_YYYY.grb",\
"RUNXXXX/outdata/echam6/RUNXXXX_echam6_co2_mm_YYYY.grb"]
jsbach_mm = [ "RUNXXXX/outdata/jsbach/RUNXXXX_jsbach_jsbach_mm_YYYY.grb",\
"RUNXXXX/outdata/jsbach/RUNXXXX_jsbach_land_mm_YYYY.grb",\
"RUNXXXX/outdata/jsbach/RUNXXXX_jsbach_surf_mm_YYYY.grb", \
"RUNXXXX/outdata/jsbach/RUNXXXX_jsbach_veg_mm_YYYY.grb",\
"RUNXXXX/outdata/jsbach/RUNXXXX_jsbach_yasso_mm_YYYY.grb" ]
mpiom_mm = [ "RUNXXXX/outdata/mpiom/RUNXXXX_mpiom_data_2d_mm_YYYY0101_YYYY1231.nc",\
"RUNXXXX/outdata/mpiom/RUNXXXX_mpiom_data_3d_mm_YYYY0101_YYYY1231.nc",\
"RUNXXXX/outdata/mpiom/RUNXXXX_mpiom_data_moc_mm_YYYY0101_YYYY1231.nc",\
"RUNXXXX/outdata/mpiom/RUNXXXX_mpiom_timeser_mm_YYYY0101_YYYY1231.nc" ]
mpiom_ym = [ "RUNXXXX/outdata/mpiom/RUNXXXX_mpiom_monitoring_ym_YYYY0101_YYYY1231.nc" ]
if options.hd:
hd_mm = [ "RUNXXXX/outdata/jsbach/RUNXXXX_hd_higres_mon_YYYY0101_YYYY1231.nc" ]
else:
hd_mm = []
if options.hamocc:
hamocc_mm = [ "RUNXXXX/outdata/hamocc/RUNXXXX_hamocc_data_2d_mm_YYYY0101_YYYY1231.nc",
"RUNXXXX/outdata/hamocc/RUNXXXX_hamocc_data_eu_mm_YYYY0101_YYYY1231.nc" ]
hamocc_ym = [ "RUNXXXX/outdata/hamocc/RUNXXXX_hamocc_data_3d_ym_YYYY0101_YYYY1231.nc" ,\
"RUNXXXX/outdata/hamocc/RUNXXXX_hamocc_data_sedi_ym_YYYY0101_YYYY1231.nc",\
"RUNXXXX/outdata/hamocc/RUNXXXX_hamocc_monitoring_ym_YYYY0101_YYYY1231.nc"]
else:
hamocc_mm = []
hamocc_ym = []
oce_patterns_mm = mpiom_mm + hamocc_mm
oce_patterns_ym = mpiom_ym + hamocc_ym
patterns_mm = hd_mm + echam_mm + jsbach_mm + oce_patterns_mm
# "hamocc/RUNXXXX_hamocc_co2_YYYY0101_YYYY1231.nc"
for pat in patterns_mm + oce_patterns_ym:
template = re.sub("RUNXXXX", run, pat)
infiles = [ re.sub("YYYY", str(year), template) for year in years ]
if pat in oce_patterns_mm + oce_patterns_ym + hd_mm:
infiles = [ re.sub("YYYY0101_YYYY", "%s0101_%s"%(str(year), str( year+options.ocestep-1 )), template) for year in oceyears ]
catfile = re.sub("YYYY0101_YYYY123[01]", "%d-%d"%(options.startyear,options.endyear), template)
catfile = re.sub("YYYY", "%d-%d"%(options.startyear,options.endyear), catfile)
catfile = re.sub("outdata/.*/", "means/", catfile)
if pat in hd_mm:
catfile = re.sub("_mon_", "_mm_", catfile) # Uwe does not stick to the rules
catfile = options.prepend +"/" + catfile
lmean = re.sub("_mm_", "_lm_", catfile)
lmean = re.sub(".grb", ".nc", lmean)
tmean = re.sub("_[ym]m_", "_tm_", catfile)
tmean = re.sub(".grb", ".nc", tmean)
smean = re.sub("_lm_", "_sm_", lmean)
seasons_rough = [["-timmean", "-selmon,%s"%months, lmean ] for months in ("12,1,2", "3,4,5", "6,7,8", "9,10,11") ]
seasons = seasons_rough[0]+seasons_rough[1]+seasons_rough[2]+seasons_rough[3]
code_files={"echam6_accw_YYYY.grb" : "echam6_accw.codes",
"ham6_ATM_mm_YYYY.grb" : "echam6_echam.codes",
"ham6_BOT_mm_YYYY.grb" : "echam6_echam.codes",
"ham6_co2_mm_YYYY.grb" : "echam6_co2.codes",
"h_jsbach_mm_YYYY.grb" : "jsbach_jsbach.codes",
"ach_land_mm_YYYY.grb" : "jsbach_land.codes",
"ach_surf_mm_YYYY.grb" : "jsbach_surf.codes",
"bach_veg_mm_YYYY.grb" : "jsbach_veg.codes",
"ch_yasso_mm_YYYY.grb" : "jsbach_yasso.codes"}
rm_if_exist([catfile,smean,lmean,tmean, smean])
mkdir(os.path.dirname(catfile))
co([ cdo, "--silent", "-cat" ] + infiles + [catfile])
print ("Going to create ", tmean, file=sys.stderr)
if (pat in patterns_mm ) :
if pat in echam_mm + jsbach_mm:
codefile = "%s/log/%s_%s"%(run, run,code_files[pat[-20:]])
co([ cdo, "--silent", "-f", "nc", "-setpartab,"+codefile, "-lmean", catfile, lmean])
else:
co([ cdo, "--silent", "-lmean", catfile, lmean])
if ("_lm_" in lmean):
co([ cdo, "--silent", "-mergetime"] + seasons + [smean])
co([ cdo, "--silent", "-timmean", lmean, tmean])
else:
co([ cdo, "--silent", "-timmean", catfile, tmean])
rm_if_exist(catfile)
def parse_args():
parser = ArgumentParser()
parser.description = "Average output files"
parser.add_argument ("startyear", help='First year to use in average', type=int)
parser.add_argument ("endyear", help='Last year to use in average', type=int)
parser.add_argument ("run", help='run to average')
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
parser.add_argument("-p", "--prepend",
help='''Prepend path for output files''', default = ".")
parser.add_argument("--ocestep",
help='''Step taken by ocean model in years''', type= int, default = 1)
parser.add_argument("--no_hamocc",
help='''Do not expect hamocc files''', dest = "hamocc", action="store_false")
parser.add_argument("--hd",
help='''Include HD files''', dest = "hd", action="store_true")
parser.set_defaults(hamocc=True)
options = parser.parse_args()
return options
def main():
options = parse_args()
if options.verbose:
print (dir(options))
average_esm(options)
if __name__ == "__main__":
main()