forked from edwinkost/extreme_value_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_downscaling_parallel.py
142 lines (121 loc) · 6.04 KB
/
6_downscaling_parallel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import os
import sys
import shutil
import glob
import datetime
import numpy as np
import virtualOS as vos
import logging
logger = logging.getLogger(__name__)
# input folder that contain extreme values (in pcraster format):
#~ # - WATCH historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/watch_1960-1999/"
#~ # - gfdl-esm2m historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/gfdl-esm2m_1960-1999/"
#~ # - hadgem2-es historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/hadgem2-es_1960-1999/"
#~ # - ipsl-cm5a-lr historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/ipsl-cm5a-lr_1960-1999/"
#~ # - miroc-esm-chem historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/miroc-esm-chem_1960-1999/"
#~ # - noresm1-m historical
#~ input_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/extreme_values/noresm1-m_1960-1999/"
#
# - input folder based on the system argument
input_folder = os.path.abspath(sys.argv[1]) + "/"
# output folder
#~ # - WATCH historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/watch_1960-1999/"
#~ # - gfdl-esm2m historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/gfdl-esm2m_1960-1999/"
#~ # - hadgem2-es historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/hadgem2-es_1960-1999/"
#~ # - ipsl-cm5a-lr historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/ipsl-cm5a-lr_1960-1999/"
#~ # - miroc-esm-chem historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/miroc-esm-chem_1960-1999/"
#~ # - noresm1-m historical
#~ general_output_folder = "/scratch-shared/edwinhs-last/flood_analyzer_output/inundation_downscaled/noresm1-m_1960-1999/"
#
# output folder based on the system argument
output_folder_for_this_analysis = sys.argv[2]
general_output_folder = output_folder_for_this_analysis + "/"
# - type of files (options are: "normal"; "bias_corrected"; and "including_bias")
type_of_files = str(sys.argv[3])
# - option for map types: *flood_inundation_volume.map or *channel_storage.map
map_type_name = "channel_storage.map"
map_type_name = str(sys.argv[4])
# - bankfull_capacity map
surface_water_bankfull_capacity_file_name = None
surface_water_bankfull_capacity_file_name = "/projects/0/aqueduct/users/edwinsut/aqueduct_flood_analyzer_results/version_2016_12_11/flood_analyzer_analysis/historical/extreme_values/watch_1960-1999/2-year_of_channel_storage.map"
surface_water_bankfull_capacity_file_name = "/projects/0/aqueduct/users/edwinsut/aqueduct_flood_analyzer_results/version_2016_12_11/flood_analyzer_analysis/historical/extreme_values/watch_1960-1999/2-year_of_channel_storage_used_as_bankfull_capacity.map"
surface_water_bankfull_capacity_file_name = str(sys.argv[5])
# - option for strahler order number
strahler_order_used = 6 # default
try:
strahler_order_used = int(sys.argv[6])
except:
pass
# - option with first upscaling model results to 30 arc-min model
with_upscaling = False # default
try:
with_upscaling = str(sys.argv[7]) == "with_upscaling"
except:
with_upscaling = False
# clean any files exists on the ouput directory (this can be done for global runs)
clean_previous_output = True
if clean_previous_output and os.path.exists(general_output_folder): shutil.rmtree(general_output_folder)
# make log folder and initialize logging
log_file_folder = general_output_folder + "/global/log/"
if clean_previous_output and os.path.exists(log_file_folder): shutil.rmtree(log_file_folder)
if os.path.exists(log_file_folder) == False: os.makedirs(log_file_folder)
vos.initialize_logging(log_file_folder)
# run the downscaling scripts parallelly
msg = "Run the downscaling scripts."
logger.info(msg)
#
number_of_clone_maps = 53
all_clone_codes = ['M%02d'%i for i in range(1,number_of_clone_maps+1,1)]
#~ all_clone_codes = ['M09']
#
# - due to limited memory, we have to split the runs into several groups (assumption: a process takes maximum about 4.5 GB RAM and we will use normal nodes)
num_of_clones_in_a_grp = np.int(np.floor(64.0 / 4.5))
number_of_clone_groups = np.int(np.ceil(float(number_of_clone_maps)/ num_of_clones_in_a_grp))
start_clone = 0
for i_group in range(number_of_clone_groups):
if i_group > 0: start_clone = last_clone
last_clone = np.minimum(number_of_clone_maps, start_clone + num_of_clones_in_a_grp)
clone_codes = all_clone_codes[start_clone:last_clone]
print clone_codes
msg = "Run the downscaling scripts for " + str(clone_codes)
logger.info(msg)
i_clone = 0
# - command lines for running the downscling script parallely
cmd = ''
for clone_code in clone_codes:
cmd += "python downscaling_with_30min_option.py " + input_folder + " " + \
general_output_folder + " " + "downscaling.ini" + " " + \
clone_code + " " + type_of_files + " " + map_type_name + " " + \
surface_water_bankfull_capacity_file_name + " " + \
str(strahler_order_used)
if with_upscaling:
cmd = cmd + " " + "with_upscaling"
cmd = cmd + " & "
i_clone += 1
cmd = cmd + " wait "
# - execute the command
print cmd
msg = "Call: "+str(cmd)
logger.debug(msg)
vos.cmd_line(cmd, using_subprocess = False)
#
# wait until all downscaling processes are done:
status = False
while status == False:
status = vos.check_downscaling_status(general_output_folder, clone_codes)
# Finish
msg = "All downscaling calculations are done. "
logger.info(msg)