/
2_merge_two_hydrological_year_result.py
267 lines (228 loc) · 12.1 KB
/
2_merge_two_hydrological_year_result.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import glob
import datetime
import netCDF4 as nc
import numpy as np
import pcraster as pcr
# netcdf reporting module:
import outputNetCDF
# utility module:
import virtualOS as vos
# variable dictionaries:
import aqueduct_flood_analyzer_variable_list as varDict
import logging
logger = logging.getLogger(__name__)
########################################################################
# The script to merge the results from two hydrological years
########################################################################
# input files
input_files = {}
#
# PCR-GLOBWB 5 arcmin results
#~ # - WATCH historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/watch_1960-1999/"
#~ # - gfdl-esm2m historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/gfdl-esm2m_1960-1999/"
#~ # - hadgem2-es historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/hadgem2-es_1960-1999/"
#~ # - ipsl-cm5a-lr historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/ipsl-cm5a-lr_1960-1999/"
#~ # - miroc-esm-chem historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/miroc-esm-chem_1960-1999/"
#~ # - noresm1-m historical
#~ input_files['folder'] = "/scratch-shared/edwinsut/flood_analyzer_analysis/maximum_events/noresm1-m_1960-1999"
#
# - input folder based on the system argument
input_files['folder'] = os.path.abspath(sys.argv[1]) + "/"
#
#
# general input files
# - clone map
input_files['clone_map_05min'] = "/projects/0/dfguu/data/hydroworld/PCRGLOBWB20/input5min/routing/lddsound_05min.map"
# - cell area, ldd maps
input_files['cell_area_05min'] = "/projects/0/dfguu/data/hydroworld/PCRGLOBWB20/input5min/routing/cellsize05min.correct.map"
input_files['ldd_map_05min'] = "/projects/0/dfguu/data/hydroworld/PCRGLOBWB20/input5min/routing/lddsound_05min.map"
#
#
# - hydrological year type (based on the WATCH data)
input_files['hydro_year_05min'] = "/projects/0/aqueduct/users/edwinsut/aqueduct_flood_analyzer_results/version_2016_12_11/flood_analyzer_analysis/hydrological_year/watch_1960-1999/hydrological_year_type.map"
input_files['hydro_year_05min'] = sys.argv[2]
# option to save/present results at the landmask region only:
landmask_only = True
# output files
output_files = {}
#
# - output folder
#~ # - WATCH historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/watch_1960-1999/"
#~ # - gfdl-esm2m historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/gfdl-esm2m_1960-1999/"
#~ # - hadgem2-es historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/hadgem2-es_1960-1999/"
#~ # - ipsl-cm5a-lr historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/ipsl-cm5a-lr_1960-1999/"
#~ # - miroc-esm-chem historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/miroc-esm-chem_1960-1999/"
#~ # - noresm1-m historical
#~ output_files['folder'] = "/scratch-shared/edwinhs-last/flood_analyzer_output/maximum_events_merged/noresm1-m_1960-1999/"
#
# output folder based on the system argument
output_folder_for_this_analysis = sys.argv[3]
output_files['folder'] = output_folder_for_this_analysis + "/"
#
#
try:
os.makedirs(output_files['folder'])
except:
os.system('rm -r ' + output_files['folder'] + "/*")
pass
# - temporary output folder (e.g. needed for resampling/gdalwarp)
output_files['tmp_folder'] = output_files['folder'] + "/tmp/"
try:
os.makedirs(output_files['tmp_folder'])
except:
os.system('rm -r ' + output_files['tmp_folder'] + "/*")
pass
# - prepare logger and its directory
log_file_location = output_files['folder'] + "/log/"
try:
os.makedirs(log_file_location)
except:
pass
vos.initialize_logging(log_file_location)
# start and end years for this analysis:
#~ # - for historical runs
#~ str_year = 1960
#~ end_year = 1999
#~ # - for the year 2030
#~ str_year = 2010
#~ end_year = 2049
#~ # - for the year 2050
#~ str_year = 2030
#~ end_year = 2069
#~ # - for the year 2080
#~ str_year = 2060
#~ end_year = 2099
# - based on the system arguments:
str_year = int(sys.argv[4])
end_year = int(sys.argv[5])
# netcdf general setup:
netcdf_setup = {}
netcdf_setup['format'] = "NETCDF4"
netcdf_setup['zlib'] = True
netcdf_setup['institution'] = "Department of Physical Geography, Utrecht University"
netcdf_setup['title' ] = "PCR-GLOBWB 2 output (post-processed for the Aqueduct Flood Analyzer): Annual Flood Maxima"
netcdf_setup['created by' ] = "Edwin H. Sutanudjaja (E.H.Sutanudjaja@uu.nl)"
netcdf_setup['description'] = 'The annual flood maxima for each year/period starting from October of the year until September of its following year (for normal hydrological years) or ' +\
'for each year/period starting from July of the year until June of its following year (for alternative hydrological years)."'
netcdf_setup['source' ] = "Utrecht University, Department of Physical Geography - contact: Edwin H. Sutanudjaja (E.H.Sutanudjaja@uu.nl)"
netcdf_setup['references' ] = "Sutanudjaja et al., in prep."
# change to the output folder (use it as the working folder)
os.chdir(output_files['folder'])
# object for reporting/making netcdf files
netcdf_report = outputNetCDF.OutputNetCDF()
# - variables that will be reported:
#~ variable_names = ['channelStorage', 'floodVolume', 'dynamicFracWat']
variable_names = ['channelStorage', 'dynamicFracWat']
for var_name in variable_names:
output_files[var_name] = {}
# - attribute information for netcdf files
output_files[var_name]['short_name'] = varDict.netcdf_short_name[var_name]
output_files[var_name]['unit'] = varDict.netcdf_unit[var_name]
output_files[var_name]['long_name'] = varDict.netcdf_long_name[var_name]
output_files[var_name]['comment'] = varDict.comment[var_name]
output_files[var_name]['description'] = varDict.description[var_name]
# - add more information
if output_files[var_name]['long_name'] == None: output_files[var_name]['long_name'] = output_files[var_name]['short_name']
if output_files[var_name]['comment'] == None: output_files[var_name]['comment'] = ""
output_files[var_name]['institution'] = netcdf_setup['institution']
output_files[var_name]['title' ] = netcdf_setup['title' ]
output_files[var_name]['created by' ] = netcdf_setup['created by' ]
if output_files[var_name]['description'] == None: output_files[var_name]['description'] = ""
output_files[var_name]['description'] = netcdf_setup['description'] + " " + output_files[var_name]['description']
output_files[var_name]['source' ] = netcdf_setup['source' ]
output_files[var_name]['references' ] = netcdf_setup['references' ]
# - resolution
output_files[var_name]['resolution_arcmin'] = 5. # unit: arc-minutes
# - preparing netcdf files:
output_files[var_name]['file_name'] = output_files['folder'] + "/" + \
varDict.netcdf_short_name[var_name] + \
"_annual_flood_maxima.nc"
msg = "Preparing the netcdf file: " + output_files[var_name]['file_name']
logger.info(msg)
netcdf_report.createNetCDF(output_files[var_name])
# the input (netcdf) file that will be used
msg = "Using the following PCR-GLOBWB simulation output (netcdf) files:"
logger.info(msg)
input_files['file_name'] = {}
for hydro_year in ["hydrological_year_1", "hydrological_year_2"]:
input_files['file_name'][hydro_year] = {}
for var in variable_names:
input_files['file_name'][hydro_year][var] = glob.glob(input_files['folder'] + "/" + str(hydro_year) + "/" + str(var) + "*" + str(str_year) + "_to_" + str(end_year) + "*.nc")[0]
msg = input_files['file_name'][hydro_year][var]
logger.info(msg)
# set the pcraster clone, ldd, landmask, and cell area map
msg = "Setting the clone, ldd, landmask, and cell area maps" + ":"
logger.info(msg)
# - clone
clone_map_file = input_files['clone_map_05min']
pcr.setclone(clone_map_file)
# - ldd
ldd = vos.readPCRmapClone(input_files['ldd_map_05min'],
clone_map_file,
output_files['tmp_folder'],
None,
True)
ldd = pcr.lddrepair(pcr.ldd(ldd))
ldd = pcr.lddrepair(ldd)
# - landmask
landmask = pcr.ifthen(pcr.defined(ldd), pcr.boolean(1.0))
# - cell area
cell_area = vos.readPCRmapClone(input_files['cell_area_05min'],
clone_map_file,
output_files['tmp_folder'])
# read the hydrological year
msg = "Reading the hydrological year types" + ":"
logger.info(msg)
hydro_year_type = pcr.nominal(\
vos.readPCRmapClone(input_files['hydro_year_05min'],
input_files['clone_map_05min'],
output_files['tmp_folder'],
None, False, None, True))
hydro_year_type = pcr.cover(hydro_year_type, pcr.nominal(1.0))
# loop through every year
msg = "Merging two hydrologyical years for the following variables:"
logger.info(msg)
#
for i_year in range(str_year, end_year + 1):
for var in variable_names:
msg = "Merging for the variable " + str(var) + " for the year " + str(i_year)
logger.info(msg)
# time index for this year
time_index_in_netcdf_file = i_year - str_year + 1
value_from_the_hydrological_year_1 = vos.netcdf2PCRobjClone(input_files['file_name']["hydrological_year_1"][var], \
varDict.netcdf_short_name[var], time_index_in_netcdf_file,\
useDoy = "Yes",
cloneMapFileName = clone_map_file,\
LatitudeLongitude = True,\
specificFillValue = None)
value_from_the_hydrological_year_2 = vos.netcdf2PCRobjClone(input_files['file_name']["hydrological_year_2"][var], \
varDict.netcdf_short_name[var], time_index_in_netcdf_file,\
useDoy = "Yes",
cloneMapFileName = clone_map_file,\
LatitudeLongitude = True,\
specificFillValue = None)
# merging two hydrological years
value_for_this_year = pcr.ifthenelse(pcr.scalar(hydro_year_type) == 1, value_from_the_hydrological_year_1, \
value_from_the_hydrological_year_2)
value_for_this_year = pcr.cover(value_for_this_year, 0.0)
if landmask_only: value_for_this_year = pcr.ifthen(landmask, value_for_this_year)
# report to a netcdf file
ncFileName = output_files[var]['file_name']
msg = "Saving to the netcdf file: " + str(ncFileName)
logger.info(msg)
time_stamp_used = datetime.datetime(i_year, 12, 31, 0)
netcdf_report.data2NetCDF(ncFileName, varDict.netcdf_short_name[var], pcr.pcr2numpy(value_for_this_year, vos.MV), time_stamp_used)