-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_full_remap.py
executable file
·209 lines (149 loc) · 6.32 KB
/
run_full_remap.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
import os
import shutil
import argparse
import subprocess as sp
SHAREDIR = '/discover/nobackup/projects/gmao/share/gmao_ops'
def rename_dirs(directory, in_pattern, out_pattern):
"""
This function takes a directory and will rename any
subdirectories called 'in_pattern' to be named
'out_pattern'
"""
for root, dirs, files in os.walk(directory, topdown=False):
for dir in dirs:
if dir == in_pattern:
os.rename(os.path.join(root, in_pattern), os.path.join(root, out_pattern))
def link_surface_dirs(directory, in_pattern):
"""
This function takes a directory and will rename any
subdirectories called 'in_pattern' to be named
'out_pattern'
"""
for root, dirs, files in os.walk(directory, topdown=False):
for dir in dirs:
if dir == in_pattern:
#print(root, dir)
# First, grab the end of the root where in_pattern exists
# /a/b/c/AeroCom/L132 ==> root = /a/b/c/AeroCom
# Then:
# os.path.split(root)[1] = AeroCom
emissionspath, emissionsdir = os.path.split(root)
inputrootdir = os.path.split(emissionspath)[1]
sfcdir = os.path.join(SHAREDIR,inputrootdir,emissionsdir,'sfc')
xdir = os.path.join(SHAREDIR,inputrootdir,emissionsdir,'x')
os.symlink(sfcdir,os.path.join(root,'sfc'))
os.symlink( xdir,os.path.join(root,'x'))
def find_script(directory, scriptname):
"""
This function takes a directory and finds executable
files that are in that directory and returns a list
of files that match
"""
scripts = []
for root, dirs, files in os.walk(directory, topdown=False):
for file in files:
if file == scriptname:
if os.access(os.path.join(root, file), os.X_OK):
scripts.append(os.path.join(root,file))
return scripts
def execute_scripts(outdir, scripts, levels, scriptname, dryrun):
"""
"""
for script in scripts:
scriptdir = os.path.dirname(script)
logfile = os.path.dirname(script) + os.sep + scriptname + '.log'
spcmd = [script,'-levs',str(levels),'-outdir',scriptdir]
print(f"Executing: {script}")
print(f"Logfile: {logfile}")
if not dryrun:
with open(logfile, 'w') as f:
sp.call(spcmd, stdout=f)
print(f"Execution of {scriptname} complete")
print()
def check_env():
"""
Check of environment to make sure all is okay
"""
assert("BINDIR" in os.environ), 'BINDIR not found in environment, set so that $BINDIR/g5_modules exists'
bindir = os.environ['BINDIR']
g5modfile = os.path.join(bindir, 'g5_modules')
if not os.path.exists(g5modfile):
raise Exception("g5_modules file not found in %s" % bindir)
gfioremapfile = os.path.join(bindir, 'GFIO_remap.x')
if not os.path.exists(gfioremapfile):
raise Exception("GFIO_remap.x file not found in %s" % bindir)
convertaerofile = os.path.join(bindir, 'convert_aerosols.x')
if not os.path.exists(convertaerofile):
raise Exception("convert_aerosols.x file not found in %s" % bindir)
def print_advice(outdir):
"""Print a message on what to set in gcm_run.j"""
chmdirstr = f"setenv CHMDIR {os.path.join(outdir, 'fvInput')}"
border = "*" * 72
padding = " " * 68
messages = [
"In order to test these, in gcm_run.j, for CHMDIR use:",
chmdirstr,
"Also go into SC-CFC and make a new SC file there"
]
print(border)
print(border)
for message in messages:
print(f"**{padding}**")
print(f"**{message.center(68)}**")
print(f"**{padding}**")
print(border)
print(border)
def main():
check_env()
comm_args = parse_args()
numlevs = comm_args['levs']
outdir = comm_args['output']
overwrite = comm_args['overwrite']
dryrun = comm_args['dryrun']
currdir = os.path.dirname(os.path.realpath(__file__))
print(f"Num levs: {numlevs}")
print(f"Current Directory: {currdir}")
print(f"Output Directory: {outdir}")
print()
print(f"Share directory: {SHAREDIR}")
print()
levdir = 'L'+str(numlevs)
if overwrite:
if os.path.isdir(outdir):
shutil.rmtree(outdir)
# Copy the current directory tree to outdir,
# ignoring any bash, py, or git files
shutil.copytree(currdir, outdir, symlinks=True,
ignore=shutil.ignore_patterns('*.bash', '*.py', '.git*'))
# Copytree keeps the access time of whatever was copied. This
# command will essentially "touch" the directory
os.utime(outdir, None)
# Rename the scripts directorys to L<numlevs>
rename_dirs(outdir, 'scripts', levdir)
# Run the doremap script
doremap_scripts = find_script(outdir, 'doremap')
execute_scripts(outdir, doremap_scripts, numlevs, 'doremap', dryrun)
# Run the remap script
remap_scripts = find_script(outdir, 'remap')
execute_scripts(outdir, remap_scripts, numlevs, 'remap', dryrun)
# Run the remap_gfed script
remap_gfed_scripts = find_script(outdir, 'remap_gfed')
execute_scripts(outdir, remap_gfed_scripts, numlevs, 'remap_gfed', dryrun)
# Create links to the sfc and x directories in SHAREDIR
link_surface_dirs(outdir, levdir)
# Create a link to the g5chem dir in the fvinput dir
os.symlink( os.path.join(outdir,'fvInput_nc3','g5chem'), os.path.join(outdir,'fvInput','g5chem'))
os.symlink( os.path.join(outdir,'fvInput_nc3','g5gcm'), os.path.join(outdir,'fvInput','g5gcm'))
# Finally, print out a nice advice to users
print_advice(outdir)
def parse_args():
p = argparse.ArgumentParser(description="Utility to create new files for new number of levels")
group1 = p.add_argument_group('required arguments')
group1.add_argument('-l','--levs', type=int, help="Number of levels", required=True)
group1.add_argument('-o','--output', type=str, help="Output directory", required=True)
p.add_argument( '--overwrite', help="Remove output directory", action='store_true')
p.add_argument( '--dryrun', help="Create the tree, but don't execute the scripts", action='store_true')
return vars(p.parse_args())
if __name__ == "__main__":
main()