-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_for_INITMIP
executable file
·96 lines (84 loc) · 2.89 KB
/
process_for_INITMIP
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
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
import flo_utils as fu
import json
import netCDF4 as nc
import numpy as np
import os
import re
# import subprocess as sp
import sys
import threading
cdo='cdo'
def process_files(options):
threads = []
variables = options["variables"]
files = options["FILES"]
for ff in files:
if not fu.check_files(["%s3"%ff], False):
command = "/sw/rhel6-x64/nco/nco-4.5.0-gcc48/bin/ncpdq -3 -a time,y,x %s %s3"%(ff, ff)
# print (command)
t = threading.Thread(target=fu.qo, args=(command,))
threads.append(t)
t.start()
[ x.join() for x in threads]
threads = []
filetimes = np.array([ nc.Dataset(x,"r").variables["time"][0] for x in files ])
files = ["%s3"%ff for ff in files]
durations = filetimes[1:]-filetimes[:-1]
for n, v in variables.iteritems():
outname = "%s_%s_%s_%s_%s.nc"%(n, "GIS", "MPIM", options["model"], options["name"])
# fu.rm_if_exist([outname])
for i,f in enumerate(files[1:]):
extras=""
if v.get("unit", False):
extras+= " -setunit,'%s' "%v.get("unit", "NO UNIT! PANIC!")
if v.get("divtime", False):
extras+= " -divc,%s "%durations[i]
if v.get("expression", False):
extras+= " -expr,'%s=%s' "%(n, v.get("expression", "NO EXPRESSION! PANIC!"))
if v.get("rundiff", False):
extras = "-sub %s %s %s %s"%(extras, f, extras, files[i])
else:
extras += " %s "%f
command = "%s -s -cat %s %s"%(cdo, extras, outname)
print (command)
# t = threading.Thread(target=fu.qo, args=(command,))
# threads.append(t)
# t.start()
# [ x.join() for x in threads]
# threads = []
# fu.qo(command)
if v.get("standard_name", False):
command = "ncatted -a 'standard_name,%s,o,c,%s' %s"%(n, v.get("standard_name"), outname)
print (command)
# fu.qo(command)
def parse_args():
parser = ArgumentParser()
parser.description = "Convert PISM output for INITMIP"
parser.add_argument ("FILES", nargs="*", help='Files to process')
parser.add_argument("-c", "--config",
help='''Config file (required)''', required = True)
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
parser.add_argument("-n", "--name",
help="experiment name", required = True)
parser.add_argument("-m", "--model",
help="model name", required = True)
options = parser.parse_args()
options_dict = vars(options)
config = json.load(open(options.config))
for x in options_dict.keys():
if not options_dict[x] is None :
config[x] = options_dict[x]
if options.verbose:
fu.cerr(dir(options))
fu.cerr(dir(config))
fu.set_debug(True)
return config
def main():
options = parse_args()
process_files(options)
if __name__ == "__main__":
main()