-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__main__.py
88 lines (77 loc) · 3.87 KB
/
__main__.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
from __future__ import print_function
import optparse
import os
from glob import glob
import shutil
from sys import exit
usage = "usage: %prog [options] outpath"
parser = optparse.OptionParser(usage=usage)
archived_options = list(map(os.path.basename, glob(os.path.join(os.path.dirname(__file__), 'mechmap_archive', '*'))))
parser.add_option("-a", dest = "archive", help = "Use archived mapping (one of: %s)" % ', '.join(archived_options), metavar="ARCHIVE", default = None)
parser.add_option("-t", dest = "tracerinfo", help = "path to tracerinfo.dat (defaults to SOA from v9-01-01)", metavar="TRACERINFO", default = None)
parser.add_option("-s", dest = "smvlog", help = "path to smv2.log (defaults to SOA from v9-01-01)", metavar="SMV", default = None)
parser.add_option("-m", dest = "mechpath", help = "path to mechanism include files (e.g., mechpath*.EXT; defaults to cb05cl_ae6_aq)", metavar="MECHPATH", default = None)
parser.add_option("-c", dest = "conversion", help = "path to converstion (i.e., mapping file)", metavar="CONV", default = None)
parser.add_option("-e", dest = "extfiles", help = "use ext files instead of namelist", metavar="EXT", default = False, action = 'store_true')
parser.add_option("-p", dest = "profiledat", help = "path to profile (e.g., CMAQ BCON input) (defaults profile.dat", metavar="PROFILEDAT", default = None)
options, args = parser.parse_args()
mapopt = dict([(o.dest, getattr(options, o.dest)) for o in parser.option_list[1:]])
convpath = options.conversion or os.path.join(os.path.dirname(__file__), 'mapping', 'saprc07t.csv')
if not os.path.exists(convpath):
temppath = os.path.join(os.path.dirname(__file__), 'mapping', convpath + '.csv')
if os.path.exists(temppath):
convpath = temppath
del temppath
tracerpath = options.tracerinfo or os.path.join(os.path.dirname(__file__), 'testdata', 'tracerinfo.dat')
smvpath = options.smvlog or os.path.join(os.path.dirname(__file__), 'testdata', 'smv2.log')
mechpath = options.mechpath or os.path.join(os.path.dirname(__file__), 'testdata')
profilepath = options.profiledat or os.path.join(os.path.dirname(__file__), 'testdata', 'profile.dat')
if len(args) < 1:
parser.print_help()
exit()
else:
out = args[0]
if not os.path.exists(out):
os.mkdir(out)
outdir = out
if options.archive in archived_options:
for f in glob(os.path.join(os.path.dirname(__file__), 'fortran_template', '*')):
shutil.copy(f, outdir)
for f in glob(os.path.join(os.path.dirname(__file__), 'mechmap_archive', options.archive, '*')):
shutil.copy(f, outdir)
from .both import geos
from .profile import profile
from .mech import mechnml, mechinc, mechext
from .map import map, trymap
if options.extfiles:
mech = mechext
else:
mech = mechnml
go = geos(tracerpath,
smvpath)
po = profile(profilepath)
if not os.path.exists(convpath):
if os.path.exists(os.path.join(os.path.dirname(__file__), convpath + '.csv')):
convpath = os.path.join(os.path.dirname(__file__), convpath + '.csv')
elif 'Y' == input("Conversion path does not exist; type Y to create it or any other key to abort\n"):
trymap(mech(mechpath), convpath, go)
else:
exit()
mech_info = mechinc(mech(mechpath), convpath)
cspec_info = go.cspec_info()
tracer_info = go.tracer_info()
profile_info = po.profile_info()
try:
mappings, nprof = map(mech(mechpath), convpath, go, po)
except TypeError as err:
print('No CMAQ namelist found. Check namelist directory.')
exit()
mech_info = (" INTEGER :: NSPC_DFLT = %d\n" % nprof) + mech_info
out = os.path.join(out, 'MAPPING')
open('%s.MECH' % out, 'w').write(mech_info)
open('%s.CSPEC' % out, 'w').write(cspec_info)
open('%s.TRACER' % out, 'w').write(tracer_info)
open('%s.MAP' % out, 'w').write(mappings)
open('%s.PROFILE' % out, 'w').write(profile_info)
for f in glob(os.path.join(os.path.dirname(__file__), 'fortran_template', '*')):
shutil.copy(f, outdir)