-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathprocess_monthly_mass.py
More file actions
358 lines (306 loc) · 15.3 KB
/
process_monthly_mass.py
File metadata and controls
358 lines (306 loc) · 15.3 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
""" derive monthly glacier mass for PyGEM simulation using annual glacier mass and monthly total mass balance """
# Built-in libraries
import argparse
import collections
import copy
import inspect
import multiprocessing
import os
import sys
import time
# External libraries
import pandas as pd
import pickle
import numpy as np
import xarray as xr
import pygem
# Local libraries
import pygem_input as pygem_prms
import pygem.pygem_modelsetup as modelsetup
def get_monthly_mass(glac_mass_annual, glac_massbaltotal_monthly):
"""
funciton to calculate the monthly glacier mass
from annual glacier mass and monthly total mass balance
Parameters
----------
glac_mass_annual : float
ndarray containing the annual glacier mass for each year computed by PyGEM
shape: [#glac, #years]
unit: kg
glac_massbaltotal_monthly : float
ndarray containing the monthly total mass balance computed by PyGEM
shape: [#glac, #months]
unit: kg
Returns
-------
glac_mass_monthly: float
ndarray containing the monthly glacier mass
shape : [#glac, #months]
unit: kg
"""
# get running total monthly mass balance - reshape into subarrays of all values for a given year, then take cumulative sum
oshape = glac_massbaltotal_monthly.shape
running_glac_massbaltotal_monthly = np.reshape(glac_massbaltotal_monthly, (-1,12), order='C').cumsum(axis=-1).reshape(oshape)
# tile annual mass to then superimpose atop running glacier mass balance (trim off final year from annual mass)
glac_mass_monthly = np.repeat(glac_mass_annual[:,:-1], 12, axis=-1)
# add annual mass values to running glacier mass balance
glac_mass_monthly += running_glac_massbaltotal_monthly
return glac_mass_monthly
def update_xrdataset(input_ds, glac_mass_monthly):
"""
update xarray dataset to add new fields
Parameters
----------
xrdataset : xarray Dataset
existing xarray dataset
newdata : ndarray
new data array
description: str
describing new data field
output_ds : xarray Dataset
empty xarray dataset that contains variables and attributes to be filled in by simulation runs
encoding : dictionary
encoding used with exporting xarray dataset to netcdf
"""
# coordinates
glac_values = input_ds.glac.values
time_values = input_ds.time.values
output_coords_dict = collections.OrderedDict()
output_coords_dict['glac_mass_monthly'] = (
collections.OrderedDict([('glac', glac_values), ('time', time_values)]))
# Attributes dictionary
output_attrs_dict = {}
output_attrs_dict['glac_mass_monthly'] = {
'long_name': 'glacier mass',
'units': 'kg',
'temporal_resolution': 'monthly',
'comment': 'monthly glacier mass'}
# Add variables to empty dataset and merge together
count_vn = 0
encoding = {}
for vn in output_coords_dict.keys():
empty_holder = np.zeros([len(output_coords_dict[vn][i]) for i in list(output_coords_dict[vn].keys())])
output_ds = xr.Dataset({vn: (list(output_coords_dict[vn].keys()), empty_holder)},
coords=output_coords_dict[vn])
count_vn += 1
# Merge datasets of stats into one output
if count_vn == 1:
output_ds_all = output_ds
else:
output_ds_all = xr.merge((output_ds_all, output_ds))
# Add attributes
for vn in output_ds_all.variables:
try:
output_ds_all[vn].attrs = output_attrs_dict[vn]
except:
pass
# Encoding (specify _FillValue, offsets, etc.)
encoding[vn] = {'_FillValue': None,
'zlib':True,
'complevel':9
}
output_ds_all['glac_mass_monthly'].values = (
glac_mass_monthly
)
return output_ds_all, encoding
def main(list_packed_vars):
"""
create monthly mass data product
Parameters
----------
list_packed_vars : list
list of packed variables that enable the use of parallels
Returns
-------
statsds : netcdf Dataset
updated stats netcdf containing monthly glacier mass
"""
# Unpack variables
count = list_packed_vars[0]
glac_no = list_packed_vars[1]
gcm_name = list_packed_vars[2]
scenario = list_packed_vars[3]
realization = list_packed_vars[4]
gcm_bc_startyear = list_packed_vars[5]
gcm_startyear = list_packed_vars[6]
gcm_endyear = list_packed_vars[7]
# ===== LOAD GLACIERS =====
main_glac_rgi = modelsetup.selectglaciersrgitable(glac_no=glac_no)
for glac in range(main_glac_rgi.shape[0]):
if glac == 0:
print(gcm_name,':', main_glac_rgi.loc[main_glac_rgi.index.values[glac],'RGIId'])
# Select subsets of data
glacier_rgi_table = main_glac_rgi.loc[main_glac_rgi.index.values[glac], :]
glacier_str = '{0:0.5f}'.format(glacier_rgi_table['RGIId_float'])
reg_str = str(glacier_rgi_table.O1Region).zfill(2)
rgiid = main_glac_rgi.loc[main_glac_rgi.index.values[glac],'RGIId']
# get datapath to stats datasets produced from run_simulation.py
output_sim_stats_fp = pygem_prms.output_sim_fp + reg_str + '/' + gcm_name + '/'
if gcm_name not in ['ERA-Interim', 'ERA5', 'COAWST']:
output_sim_stats_fp += scenario + '/'
output_sim_stats_fp += 'stats/'
# Create filepath if it does not exist
if os.path.exists(output_sim_stats_fp) == False:
os.makedirs(output_sim_stats_fp, exist_ok=True)
# Number of simulations
if pygem_prms.option_calibration == 'MCMC':
sim_iters = pygem_prms.sim_iters
else:
sim_iters = 1
# Netcdf filename
if gcm_name in ['ERA-Interim', 'ERA5', 'COAWST']:
# Filename
netcdf_fn = (glacier_str + '_' + gcm_name + '_' + str(pygem_prms.option_calibration) + '_ba0' +
'_' + str(sim_iters) + 'sets' + '_' + str(gcm_startyear) + '_' + str(gcm_endyear) + '_all.nc')
elif realization is not None:
netcdf_fn = (glacier_str + '_' + gcm_name + '_' + scenario + '_' + realization + '_' +
str(pygem_prms.option_calibration) + '_ba' + str(pygem_prms.option_bias_adjustment) +
'_' + str(sim_iters) + 'sets' + '_' + str(gcm_bc_startyear) + '_' +
str(gcm_endyear) + '_all.nc')
else:
netcdf_fn = (glacier_str + '_' + gcm_name + '_' + scenario + '_' +
str(pygem_prms.option_calibration) + '_ba' + str(pygem_prms.option_bias_adjustment) +
'_' + str(sim_iters) + 'sets' + '_' + str(gcm_bc_startyear) + '_' +
str(gcm_endyear) + '_all.nc')
if os.path.exists(output_sim_stats_fp + netcdf_fn):
try:
# open dataset
statsds = xr.open_dataset(output_sim_stats_fp + netcdf_fn)
# calculate monthly mass - pygem glac_massbaltotal_monthly is in units of m3, so convert to mass using density of ice
glac_mass_monthly = get_monthly_mass(
statsds.glac_mass_annual.values,
statsds.glac_massbaltotal_monthly.values * pygem_prms.density_ice,
)
statsds.close()
# update dataset to add monthly mass change
output_ds_stats, encoding = update_xrdataset(statsds, glac_mass_monthly)
# close input ds before write
statsds.close()
# append to existing stats netcdf
output_ds_stats.to_netcdf(output_sim_stats_fp + netcdf_fn, mode='a', encoding=encoding, engine='netcdf4')
# close datasets
output_ds_stats.close()
except:
pass
else:
print('Simulation not found: ',output_sim_stats_fp + netcdf_fn)
return
#%% PARALLEL PROCESSING
if __name__ == '__main__':
time_start = time.time()
# set up CLI
parser = argparse.ArgumentParser(
description='''Script to process montly mass for PyGEM simulations file\n\nExample calls:\n$python process_monthly_mass.py -rgi_glac_number=13.40312 -gcm_name=CESM2 -scenario=ssp585\n$python process_monthly_mass.py -rgi_region01=13 -gcm_name=CESM2 -scenario=ssp585''',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-rgi_region01', type=int, default=None,
help='Randoph Glacier Inventory region')
parser.add_argument('-rgi_glac_number', type=str, default=None,
help='Randoph Glacier Inventory region')
parser.add_argument('-rgi_glac_number_fn', action='store', type=str, default=None,
help='Filename containing list of rgi_glac_number, helpful for running batches on spc')
parser.add_argument('-gcm_list_fn', action='store', type=str, default=pygem_prms.ref_gcm_name,
help='text file full of commands to run')
parser.add_argument('-gcm_name', action='store', type=str, default=None,
help='GCM name used for model run')
parser.add_argument('-scenario', action='store', type=str, default=None,
help='rcp or ssp scenario used for model run (ex. rcp26 or ssp585)')
parser.add_argument('-realization', action='store', type=str, default=None,
help='realization from large ensemble used for model run (ex. 1011.001 or 1301.020)')
parser.add_argument('-realization_list', action='store', type=str, default=None,
help='text file full of realizations to run')
parser.add_argument('-gcm_bc_startyear', action='store', type=int, default=pygem_prms.gcm_bc_startyear,
help='start year for bias correction')
parser.add_argument('-gcm_startyear', action='store', type=int, default=pygem_prms.gcm_startyear,
help='start year for the model run')
parser.add_argument('-gcm_endyear', action='store', type=int, default=pygem_prms.gcm_endyear,
help='end year for the model run')
parser.add_argument('-num_simultaneous_processes', action='store', type=int, default=4,
help='number of simultaneous processes (cores) to use')
parser.add_argument('-batch_number', action='store', type=int, default=None,
help='Batch number used to differentiate output on supercomputer')
# flags
parser.add_argument('-option_ordered', action='store_true',
help='Flag to keep glacier lists ordered (default is off)')
parser.add_argument('-option_parallels', action='store_true',
help='Flag to use or not use parallels (default is off)')
args = parser.parse_args()
# RGI glacier number
if args.rgi_glac_number:
glac_no = [args.rgi_glac_number]
elif args.rgi_glac_number_fn is not None:
with open(args.rgi_glac_number_fn, 'rb') as f:
glac_no = pickle.load(f)
elif args.rgi_region01:
main_glac_rgi_all = modelsetup.selectglaciersrgitable(
rgi_regionsO1=[args.rgi_region01], rgi_regionsO2=pygem_prms.rgi_regionsO2,
rgi_glac_number=pygem_prms.rgi_glac_number, glac_no=pygem_prms.glac_no,
include_landterm=pygem_prms.include_landterm, include_laketerm=pygem_prms.include_laketerm,
include_tidewater=pygem_prms.include_tidewater,
min_glac_area_km2=pygem_prms.min_glac_area_km2)
glac_no = list(main_glac_rgi_all['rgino_str'].values)
elif pygem_prms.glac_no is not None:
glac_no = pygem_prms.glac_no
else:
main_glac_rgi_all = modelsetup.selectglaciersrgitable(
rgi_regionsO1=pygem_prms.rgi_regionsO1, rgi_regionsO2=pygem_prms.rgi_regionsO2,
rgi_glac_number=pygem_prms.rgi_glac_number, glac_no=pygem_prms.glac_no,
include_landterm=pygem_prms.include_landterm, include_laketerm=pygem_prms.include_laketerm,
include_tidewater=pygem_prms.include_tidewater,
min_glac_area_km2=pygem_prms.min_glac_area_km2)
glac_no = list(main_glac_rgi_all['rgino_str'].values)
# Number of cores for parallel processing
if args.option_parallels != 0:
num_cores = int(np.min([len(glac_no), args.num_simultaneous_processes]))
else:
num_cores = 1
# Glacier number lists to pass for parallel processing
glac_no_lsts = modelsetup.split_list(glac_no, n=num_cores, option_ordered=args.option_ordered)
# Read GCM names from argument parser
gcm_name = args.gcm_list_fn
if args.gcm_name is not None:
gcm_list = [args.gcm_name]
scenario = args.scenario
elif args.gcm_list_fn == pygem_prms.ref_gcm_name:
gcm_list = [pygem_prms.ref_gcm_name]
scenario = args.scenario
else:
with open(args.gcm_list_fn, 'r') as gcm_fn:
gcm_list = gcm_fn.read().splitlines()
scenario = os.path.basename(args.gcm_list_fn).split('_')[1]
print('Found %d gcms to process'%(len(gcm_list)))
# Read realizations from argument parser
if args.realization is not None:
realizations = [args.realization]
elif args.realization_list is not None:
with open(args.realization_list, 'r') as real_fn:
realizations = list(real_fn.read().splitlines())
print('Found %d realizations to process'%(len(realizations)))
else:
realizations = None
# Producing realization or realization list. Best to convert them into the same format!
# Then pass this as a list or None.
# If passing this through the list_packed_vars, then don't go back and get from arg parser again!
# Loop through all GCMs
for gcm_name in gcm_list:
print('Processing:', gcm_name, scenario)
# Pack variables for multiprocessing
list_packed_vars = []
if realizations is not None:
for realization in realizations:
for count, glac_no_lst in enumerate(glac_no_lsts):
list_packed_vars.append([count, glac_no_lst, gcm_name, scenario, realization, args.gcm_bc_startyear, args.gcm_startyear, args.gcm_endyear])
else:
for count, glac_no_lst in enumerate(glac_no_lsts):
list_packed_vars.append([count, glac_no_lst, gcm_name, scenario, realizations, args.gcm_bc_startyear, args.gcm_startyear, args.gcm_endyear])
print('len list packed vars:', len(list_packed_vars))
# Parallel processing
if args.option_parallels != 0:
print('Processing in parallel with ' + str(args.num_simultaneous_processes) + ' cores...')
with multiprocessing.Pool(args.num_simultaneous_processes) as p:
p.map(main,list_packed_vars)
# If not in parallel, then only should be one loop
else:
# Loop through the chunks and export bias adjustments
for n in range(len(list_packed_vars)):
main(list_packed_vars[n])
print('Total processing time:', time.time()-time_start, 's')