Warning, /tools/darwin/mkdarwintracers is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit 8fbfd1f3 on 2018-02-26 18:39:21 UTC
8fbfd1f382 Oliv*0001 #!/usr/bin/env python
0002 '''
0003 Call this in the build directory (or in any directory with current DARWIN_SIZE.h
0004 and DARWIN_INDICES.h), and it will print a part of data.ptracers with tracer
0005 names appropriate for this setup.
0006
0007 Options:
0008
0009 -r include "PTRACERS_ref" lines
0010 -f include "PTRACERS_initialFile" lines
0011 -u include "PTRACERS_units" lines
0012 -s print names, units, etc separately
0013
0014 Example:
0015
0016 mkdarwintracers -r >> data.ptracers
0017 '''
0018 import sys
0019 import os
0020 import re
0021 from math import log10
0022 from fortran import OrderedDict
0023 from subprocess import Popen, PIPE
0024 from fortran import readparameters
0025
0026 units = {
0027 'DIC': 'mmol C/m^3',
0028 'NH4': 'mmol N/m^3',
0029 'NO2': 'mmol N/m^3',
0030 'NO3': 'mmol N/m^3',
0031 'PO4': 'mmol P/m^3',
0032 'SiO2': 'mmol Si/m^3',
0033 'FeT': 'mmol Fe/m^3',
0034 'DOC': 'mmol C/m^3',
0035 'DON': 'mmol N/m^3',
0036 'DOP': 'mmol P/m^3',
0037 'DOFe': 'mmol Fe/m^3',
0038 'POC': 'mmol C/m^3',
0039 'PON': 'mmol N/m^3',
0040 'POP': 'mmol P/m^3',
0041 'POSi': 'mmol Si/m^3',
0042 'POFe': 'mmol Fe/m^3',
0043 'PIC': 'mmol C/m^3',
0044 'ALK': 'mmol eq/m^3',
0045 'O2': 'mmol O/m^3',
0046 'CDOM': 'mmol C/m^3',
0047 }
0048 unitsre = {
0049 '^c[0-9]': 'mmol C/m^3',
0050 '^n[0-9]': 'mmol N/m^3',
0051 '^p[0-9]': 'mmol P/m^3',
0052 '^si[0-9]': 'mmol Si/m^3',
0053 '^fe[0-9]': 'mmol Fe/m^3',
0054 '^Chl[0-9]': 'mg Chl a/m^3',
0055 }
0056 def getunit(name):
0057 try:
0058 return units[name]
0059 except KeyError:
0060 for k,v in unitsre.items():
0061 if re.match(k, name):
0062 return v
0063 else:
0064 return None
0065
0066 def findfile(fname):
0067 origfname = fname
0068 if os.path.exists(fname): return fname
0069 fname = os.path.join('..', 'pkg', 'darwin', fname)
0070 if os.path.exists(fname): return fname
0071 fname = os.path.join('..', fname)
0072 if os.path.exists(fname): return fname
0073 fname = os.path.join('..', fname)
0074 if os.path.exists(fname): return fname
0075 raise RuntimeError('File not found: {}\n'.format(origfname))
0076
0077
0078 args = sys.argv[1:]
0079 if '-h' in args or '--help' in args:
0080 sys.exit(__doc__)
0081 printref = '-r' in args
0082 printfile = '-f' in args
0083 printunits = '-u' in args
0084 separate = '-s' in args
0085 template = None
0086 for arg in args:
0087 if arg not in ['-r', '-f', '-u', '-s']:
0088 if template is None:
0089 template = arg
0090 else:
0091 sys.exit(__doc__)
0092
0093 files = []
0094 for fname in ['DARWIN_OPTIONS.h', 'DARWIN_SIZE.h', 'DARWIN_INDICES.h']:
0095 files.append(findfile(fname))
0096 sys.stderr.write(' '.join(['Using']+files) + '\n')
0097 pipe1 = Popen(['cat'] + files, stdout=PIPE)
0098 pipe2 = Popen(['grep', '-v', 'PACKAGES_CONFIG'], stdin=pipe1.stdout, stdout=PIPE)
0099 pipe1.stdout.close()
0100 pipe3 = Popen(['grep', '-v', 'CPP_OPTIONS'], stdin=pipe2.stdout, stdout=PIPE)
0101 pipe2.stdout.close()
0102 pipe4 = Popen(['cpp', '-DALLOW_DARWIN', '-traditional', '-P'], stdin=pipe3.stdout, stdout=PIPE)
0103 pipe3.stdout.close()
0104 p = readparameters(pipe4.stdout)
0105 pipe4.stdout.close()
0106
0107 digits = int(log10(int(p['nplank']))) + 1
0108
0109 ends = OrderedDict((k[1:], v) for k,v in p.items() if k.startswith('e'))
0110 starts = OrderedDict((k[1:], v) for k,v in p.items() if k.startswith('i') and k[1:4] not in ['Min', 'Max'])
0111 cellnames = ends.keys()
0112 for k in starts:
0113 if k not in ends:
0114 ends[k] = starts[k]
0115
0116 nptr = max(ends.values())
0117
0118 print " &PTRACERS_PARM01"
0119 print " PTRACERS_numInUse= {0},".format(nptr)
0120
0121 valdict = {}
0122 if template is not None:
0123 with open(template) as f:
0124 for line in f:
0125 try:
0126 name,val = line.rstrip().split('=')
0127 except ValueError:
0128 raise ValueError('#{0}#'.format(line.rstrip()))
0129 name = name.strip()
0130 val = val.strip()
0131 if name.startswith('PTRACERS'):
0132 print ' ' + line.rstrip().replace('N', str(nptr)) + ','
0133 else:
0134 valdict[name] = val
0135
0136 files = []
0137 unitl = []
0138 refs = []
0139 for k,s in sorted(starts.items(), key=lambda x:x[1]):
0140 e = ends[k]
0141 for i in range(s,e+1):
0142 if k in cellnames:
0143 name = '{0}{1:0{d}d}'.format(k, i-s+1, d=digits)
0144 else:
0145 name = k
0146 out = " PTRACERS_names({0})= '{1}',".format(i, name)
0147 if separate:
0148 print out
0149 else:
0150 print " " + out
0151 if name in valdict:
0152 val = valdict[name]
0153 if val[0] == "'":
0154 out = " PTRACERS_initialFile({0})= {1},".format(i, val)
0155 if separate:
0156 files.append(out)
0157 else:
0158 print out
0159 else:
0160 out = " PTRACERS_ref(:,{0})= {1},".format(i, val)
0161 if separate:
0162 refs.append(out)
0163 else:
0164 print " " + out
0165 else:
0166 if printref:
0167 out = " PTRACERS_ref(:,{0})= ".format(i)
0168 if separate:
0169 refs.append(out)
0170 else:
0171 print " " + out
0172 if printunits:
0173 unit = getunit(name)
0174 if unit is not None:
0175 out = " PTRACERS_units({0})= '{1}'".format(i, unit)
0176 if separate:
0177 unitl.append(out)
0178 else:
0179 print " " + out
0180 if printfile:
0181 out = " PTRACERS_initialFile({0})= '',".format(i)
0182 if separate:
0183 files.append(out)
0184 else:
0185 print out
0186
0187 if separate:
0188 print '\n'.join(['#'] + unitl)
0189 print '\n'.join(['#'] + files)
0190 print '\n'.join(['#'] + refs)
0191
0192 print " &"