-
Notifications
You must be signed in to change notification settings - Fork 1
/
shiny_extract_one.py
298 lines (248 loc) · 10.4 KB
/
shiny_extract_one.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#Right now, this needs to be run in the directory with the data
# and it will write output to the current working directory
import f90nml
import os
import sys
import subprocess
#sets the paths for outputs, debug, and year
from setvars import *
iargs = len(sys.argv)
if iargs < 1:
sys.exit("Usage: python shiny_extract.py which_node")
else:
which_node = sys.argv[1]
suffix = "_1.nc"
#Directory with original SCHISM nc output
schismdir = OUTPUTS
#Directory to output timeseries files
#will go to a .git-ignored directory in the current working directory
thisdir = os.getcwd()
outdir = os.path.join(thisdir,"outputs")
#Check that output directory exists
if not os.path.exists(schismdir):
sys.exit("SCHISM output directory " + schismdir + " does not exist. Exiting.")
#Create a directory for output if one doesn't exist.
#Note, existing files will be overwritten
if not os.path.exists(outdir):
os.makedirs(outdir)
#Which year
which_year = iYr0
#to make the files
basename = "GEN_"
#read nml used by schism
cgem = f90nml.read('cgem.nml')
#number of phytoplankton groups
nospA = cgem.get('nosp').get('nospa')
#number of zooplankton groups
nospZ = cgem.get('nosp').get('nospz')
if debug:
print(nospA,nospZ,"\n")
#Number of state vars, A/Qn/Qp(nospa), Z(nospz), and the rest(25)
nf = 3*nospA + nospZ + 25
if debug:
print('nf',nf,"\n")
#Initialize list of variable names
names = []
descriptions = []
units = []
#This script defines all variables
#['A','Qn','Qp','Z','NO3','NH4','PO4','DIC','O2','OM1CA','OM1NA','OM1PA','OM2CA','OM2NA','OM2PA','OM1CZ','OM1NZ','OM1PZ','OM2CZ','OM2NZ','OM2PZ','OM1R','OM2R','CDOM','Si','OM1BC','OM2BC','Alk']
#but skips extraction for variables not listed in cgem_vars.
#skip_var is True or False
skip_var = []
#!-A; Phytoplankton number density (cells/m3);
for i in range(nospA):
names.append("A" + str(i+1))
descriptions.append("Phytoplankton group " + str(i+1) + " number density")
units.append("cells/m3")
skip_var.append(False) if 'A' in cgem_vars else skip_var.append(True)
#!-Qn: Phytoplankton Nitrogen Quota (mmol-N/cell)
for i in range(nospA):
names.append("Qn" + str(i+1))
descriptions.append("Phytoplankton group " + str(i+1) + " nitrogen quota.")
units.append("mmol-N/cell")
skip_var.append(False) if 'Qn' in cgem_vars else skip_var.append(True)
#!-Qp: Phytoplankton Phosphorus Quota (mmol-P/cell)
for i in range(nospA):
names.append("Qp" + str(i+1))
descriptions.append("Phytoplankton group " + str(i+1) + " phosphorus quota.")
units.append("mmol-P/cell")
skip_var.append(False) if 'Qp' in cgem_vars else skip_var.append(True)
#!-Z: Zooplankton number density (individuals/m3);
for i in range(nospZ):
names.append("Z" + str(i+1))
descriptions.append("Zooplankton group " + str(i+1) + " number density.")
units.append("organisms/m3")
skip_var.append(False) if 'Z' in cgem_vars else skip_var.append(True)
#!-NO3; Nitrate (mmol-N/m3)
names.append("NO3")
descriptions.append("Nitrate")
units.append("mmol-N/m3")
skip_var.append(False) if 'NO3' in cgem_vars else skip_var.append(True)
#!-NH4; Ammonium (mmol-N/m3)
names.append("NH4")
descriptions.append("Ammonium")
units.append("mmol-N/m3")
skip_var.append(False) if 'NH4' in cgem_vars else skip_var.append(True)
#!-PO4: Phosphate (mmol-P/m3)
names.append("PO4")
descriptions.append("Phosphate")
units.append("mmol-P/m3")
skip_var.append(False) if 'PO4' in cgem_vars else skip_var.append(True)
#!-DIC: Dissolved Inorganic Carbon (mmol-C/m3)
names.append("DIC")
descriptions.append("Dissolved Inorganic Carbon")
units.append("mmol-C/m3")
skip_var.append(False) if 'DIC' in cgem_vars else skip_var.append(True)
#!-O2: Molecular Oxygen (mmol-O2/m3)
names.append("O2")
descriptions.append("Molecular Oxygen")
units.append("mmol-O2/m3")
skip_var.append(False) if 'O2' in cgem_vars else skip_var.append(True)
#!-OM1CA: (mmol-C/m3--particulate)
#! -- Particulate Organic Matter from dead Phytoplankton
names.append("OM1CA")
descriptions.append("Carbon Particulate Organic Matter from dead Phytoplankton")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM1CA' in cgem_vars else skip_var.append(True)
#!-OM1NA: (mmol-N/m3--particulate)
#! -- Particulate Organic Matter from dead Phytoplankton
names.append("OM1NA")
descriptions.append("Nitrogen Particulate Organic Matter from dead Phytoplankton")
units.append("mmol-N/m3")
skip_var.append(False) if 'OM1NA' in cgem_vars else skip_var.append(True)
#!-OM1PA: (mmol-PC/m3--particulate)
#! -- Particulate Organic Matter from dead Phytoplankton
names.append("OM1PA")
descriptions.append("Phosporus Particulate Organic Matter from dead Phytoplankton")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM1PA' in cgem_vars else skip_var.append(True)
#!-OM2CA: (mmol-C/m3--dissolved)
#! -- Dissolved Organic Matter from dead Phytoplankton
names.append("OM2CA")
descriptions.append("Carbon Dissolved Organic Matter from dead Phytoplankton")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM2CA' in cgem_vars else skip_var.append(True)
#!-OM2NA: (mmol-NC/m3--dissolved)
#! -- Dissolved Organic Matter from dead Phytoplankton
names.append("OM2NA")
descriptions.append("Nitrogen Dissolved Organic Matter from dead Phytoplankton")
units.append("mmol-N/m3")
skip_var.append(False) if 'OM2NA' in cgem_vars else skip_var.append(True)
#!-OM2PA: (mmol-P/m3--dissolved)
#! -- Dissolved Organic Matter from dead Phytoplankton
names.append("OM2PA")
descriptions.append("Phosphorus Dissolved Organic Matter from dead Phytoplankton")
units.append("mmol-P/m3")
skip_var.append(False) if 'OM2PA' in cgem_vars else skip_var.append(True)
#!-OM1Z:(mmol-C/m3--particulate)
#! -- Particulate Organic Matter from Zooplankton fecal pellets.
names.append("OM1CZ")
descriptions.append("Carbon Particulate Organic Matter from Zooplankton fecal pellets")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM1CZ' in cgem_vars else skip_var.append(True)
#!-OM1NZ:(mmol-N/m3--particulate)
#! -- Particulate Organic Matter from Zooplankton fecal pellets.
names.append("OM1NZ")
descriptions.append("Nitrogen Particulate Organic Matter from Zooplankton fecal pellets")
units.append("mmol-N/m3")
skip_var.append(False) if 'OM1NZ' in cgem_vars else skip_var.append(True)
#!-OM1PZ:(mmol-P/m3--particulate)
#! -- Particulate Organic Matter from Zooplankton fecal pellets.
names.append("OM1PZ")
descriptions.append("Phosphorus Particulate Organic Matter from Zooplankton fecal pellets")
units.append("mmol-P/m3")
skip_var.append(False) if 'OM1PZ' in cgem_vars else skip_var.append(True)
#!-OM2CZ:(mmol-C/m3--dissolved)
#! -- Dissolved Organic Matter from Zooplankton fecal pellets.
names.append("OM2CZ")
descriptions.append("Carbon Dissolved Organic Matter from Zooplankton fecal pellets")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM2CZ' in cgem_vars else skip_var.append(True)
#!-OM2NZ:(mmol-C/m3--dissolved)
#! -- Dissolved Organic Matter from Zooplankton fecal pellets.
names.append("OM2NZ")
descriptions.append("Nitrogen Dissolved Organic Matter from Zooplankton fecal pellets")
units.append("mmol-N/m3")
skip_var.append(False) if 'OM2NZ' in cgem_vars else skip_var.append(True)
#!-OM2PZ:(mmol-P/m3--dissolved)
#! -- Dissolved Organic Matter from Zooplankton fecal pellets.
names.append("OM2PZ")
descriptions.append("Phosphorus Dissolved Organic Matter from Zooplankton fecal pellets")
units.append("mmol-P/m3")
skip_var.append(False) if 'OM2PZ' in cgem_vars else skip_var.append(True)
#!-OM1R: (mmol-C/m3--particulate)
#!-- Particulate Organic Matter from river outflow
names.append("OM1R")
descriptions.append("Particulate Organic Matter from river outflow")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM1R' in cgem_vars else skip_var.append(True)
#!-OM2R: (mmol-C/m3--dissolved)
#!-- Dissolved Organic Matter from river outflow
names.append("OM2R")
descriptions.append("Dissolved Organic Matter from river outflow")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM2R' in cgem_vars else skip_var.append(True)
#!-CDOM: (ppb)
#!-- Colored Dissolved Organic Matter
names.append("CDOM")
descriptions.append("Colored Dissolved Organic Matter")
units.append("ppb")
skip_var.append(False) if 'CDOM' in cgem_vars else skip_var.append(True)
#!-Si: (mmol-Si/m3) -- Silica
names.append("Si")
descriptions.append("Silica")
units.append("mmol-Si/m3")
skip_var.append(False) if 'Si' in cgem_vars else skip_var.append(True)
#!-OM1BC: (mmol-C/m3--particulate)
#!-- Particulate Organic Matter in initial and boundary conditions
names.append("OM1BC")
descriptions.append("Particulate Organic Matter in initial and boundary conditions")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM1BC' in cgem_vars else skip_var.append(True)
#!-OM2BC: (mmol-C/m3--dissolved)
#!-- Dissolved Organic Matter in initial and boundary conditions
names.append("OM2BC")
descriptions.append("Dissolved Organic Matter in initial and boundary conditions")
units.append("mmol-C/m3")
skip_var.append(False) if 'OM2BC' in cgem_vars else skip_var.append(True)
#!-Alk: (mmol-HCO3/m3) -- Alkalinity
names.append("Alk")
descriptions.append("Alkalinity")
units.append("mmol-N/m3")
skip_var.append(False) if 'Alk' in cgem_vars else skip_var.append(True)
#!-Tr: Tracer
names.append("Tr")
descriptions.append("Tracer")
units.append("1/m3")
skip_var.append(False) if 'Tr' in cgem_vars else skip_var.append(True)
if debug:
print(len(names),'names:',names)
print(len(descriptions),'descriptions:',descriptions)
print(len(units),'units:',units,"\n")
print(len(skip_var),'skip_var:',skip_var,"\n")
#Start a list for output file names
outputfiles = []
#For all the state variables
for i in range(nf):
if skip_var[i]:
if debug: print("skipping",names[i])
continue
base = basename + str(i+1)
inputfile = os.path.join(schismdir,base + suffix)
if debug : print('inputfile',inputfile,"\n")
outputfile = names[i] + "_" + str(which_node) + ".nc"
outputfile = os.path.join(outdir,outputfile)
#Extract a time series to a file, which will be overwritten if it exists
command = 'ncks -O -d nSCHISM_hgrid_node,' + str(which_node) + ' ' + inputfile + ' ' + outputfile
print(command)
#Add description attribute
command = 'ncatted -O -a description,' + base + ',c,c,\"' + descriptions[i] + '\" ' + outputfile
print(command)
#Add units attribute
command = 'ncatted -O -a units,' + base + ',c,c,' + units[i] + ' ' + outputfile
print(command)
#Rename the variable
command = 'ncrename -v ' + base + ',' + names[i] + ' ' + outputfile
print(command)
sys.exit()