-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage_he
executable file
·132 lines (111 loc) · 4.46 KB
/
average_he
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
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
import glob
import subprocess as sp
import numpy as np
import re
import sys
import os
import json
import flo_utils as fu
from netCDF4 import Dataset as nc
# import math
ncea = "ncea"
js_config = json.load(open(os.path.expanduser('~')+"/MPI/offsets.json"))
def average_files(infiles, outfile):
# infiles = [ "../"+x for x in infiles ] # for use with ln -s
print ([ncea] + infiles + [outfile])
fu.qo ([ncea] + infiles + [outfile])
def generate_atm_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/100)*10
return ("%s/ECHAM/OUTPUT/10yr/%sATM_%d-9_lm.nc"%(run_id, run_id, file_years))
def generate_bot_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/100)*10
return ("%s/ECHAM/OUTPUT/10yr/%sBOT_%d-9_lm.nc"%(run_id, run_id, file_years))
def generate_mpiom_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/100)*10
pattern = "%s/MPIOM/OUTPUT/10_year_means/3d_year_%d?-90_lm.nc"%(run_id, file_years)
files = (glob.glob(pattern))
if (len(files) == 1):
return files[0]
else:
fu.cerr("ERROR GENERATING MPIOM NAMES! RECEIVED WRONG COUNT OF MATCHES FOR THE PATTERN")
fu.cerr(pattern)
fu.cerr (files)
sys.exit(666)
def generate_mpiom_surf_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/100)*10
pattern = "%s/MPIOM/OUTPUT/10_year_means/2d_year_%d?-90_lm.nc"%(run_id, file_years)
files = (glob.glob(pattern))
if (len(files) == 1):
return files[0]
else:
fu.cerr("ERROR GENERATING MPIOM NAMES! RECEIVED WRONG COUNT OF MATCHES FOR THE PATTERN")
fu.cerr(pattern)
fu.cerr (files)
sys.exit(666)
def generate_pism_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/10)
return ("%s/mPISM/OUTPUT/main_fields/main_fields%d.nc"%(run_id, file_years))
def generate_mpiom_flux_name(run_id, offset, timestep):
file_years = round ((offset + timestep)/10)
return ("%s/mPISM/OUTPUT/fluxes/mpiom_fluxes_%d.nc"%(run_id, file_years))
def average_he(options):
events = options.events
experiment_ids = [ x[0:3] for x in events ]
print (experiment_ids)
offsets = { x : js_config["offsets"][x] for x in events }
timesteps = np.arange(-350, 3600, options.interval)
fu.mkdir(options.output)
if options.PISM:
types = ["main_fields"]
name_generators = {"main_fields":generate_pism_name}
elif options.mpiom_fluxes:
types = ["mpiom_fluxes"]
name_generators = {"mpiom_fluxes":generate_mpiom_flux_name}
else:
types = ["atm", "bot", "mpiom", "mpiom_surf"]
name_generators = {"atm": generate_atm_name, "bot": generate_bot_name, "mpiom": generate_mpiom_name, "mpiom_surf": generate_mpiom_surf_name}
for timestep in timesteps:
for ctype in types:
ng = name_generators[ctype]
infiles = [ ng(run, offsets[event], timestep) for (run, event) in zip (experiment_ids, events) ]
infiles = [ x for x in infiles if x ]
outfile = "%s/%s_%+05d.nc"%(options.output, ctype, timestep)
average_files(infiles, outfile)
fdb = fu.debug
if options.PISM:
of = nc(outfile, "r+")
of.variables["t"][0]=timestep
if options.mpiom_fluxes:
of = nc(outfile, "r+")
of.variables["time"][0]=timestep
else:
fu.debug=True
fu.qo(["ncatted", "-a", "average_period,global,o,c,%d-%d"%(timestep-options.interval/2, timestep+options.interval/2), outfile])
fu.debug=fdb
def parse_args():
parser = ArgumentParser()
parser.description = "Average HE data"
parser.add_argument ("events", help='First year to use in average', nargs='*')
parser.add_argument ("-o", "--output", help='where to dump the output', required=True)
parser.add_argument ( "--interval", help='interval at which to expect data', type=int, default=100)
parser.add_argument ( "--PISM", help='Process PISM files', action="store_true")
parser.add_argument ( "--mpiom_fluxes", help='Process PISM mpiom_flux files', action="store_true")
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
parser.add_argument("--ncecat",
help='''Use ncecat instead of ncea''', action="store_true")
options = parser.parse_args()
return options
def main():
options = parse_args()
if options.verbose:
print (dir(options))
if options.ncecat:
global ncea
ncea = "ncecat"
average_he(options)
if __name__ == "__main__":
main()