-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifs_perturb.py
executable file
·248 lines (228 loc) · 8.92 KB
/
ifs_perturb.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
#!/usr/bin/env python
import numpy as np
import argparse
import pygrib
import os
import shutil
import hashlib
import string
import random
'''
description: Create an ensemble of IFS initial conditions from an existing
initial state.
license: APACHE 2.0
author: Ronald van Haren, NLeSC ([email protected])
'''
class FullPaths(argparse.Action):
"""Expand user- and relative-paths"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest,
os.path.abspath(os.path.expanduser(values)))
def is_dir(dirname):
"""
Checks if a path is an actual directory
:param dirname: path of a directory
:type dirname: string
:returns: path of a directory
:rtype: string
"""
if not os.path.isdir(dirname):
msg = "{0} is not a directory".format(dirname)
raise argparse.ArgumentTypeError(msg)
else:
return dirname
def sha256_checksum(filename, block_size=65536):
'''
Return sha256 checksum of file
:param filename: path of a file
:type filename: string
:returns: sha256 checksum
:rtype: string
'''
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def expName_generator(size=4, chars=string.ascii_uppercase + string.digits):
'''
Return experiment name,
a combination of uppercase letters and numbers of length 4
'''
return ''.join(random.choice(chars) for _ in range(size))
def main(istate, iexp, outdir, members, seed, var='t',
level=30, pert=0.1, force=False):
"""
Create an ensemble of perturbed IFS initial conditions.
Perturbs one random mode at level=level by amount=pert.
:param istate: directory containing initial state to perturb [path]
:type istate: string
:param iexp: experiment name of initial state to perturb
:type iexp: string
:param outdir: base output directory [path]
:type outdir: string
:param members: number of ensemble members to create
:type members: int
:param seed: random seed
:type seed: int
:param var: variable name to perturb
:type var: string
:param level: model level
:type level: int
:param pert: amount to perturb with
:type pert: float
:param force: Force overwriting existing perturbed conditions
:type force: bool
"""
inFile = "ICMSH{}INIT".format(iexp)
file_in = os.path.join(istate, inFile)
file_in_sha256 = sha256_checksum(file_in)
# Open and read messages in original initial state file
istate_in = pygrib.open(file_in)
messages_in = istate_in.read() # read all messages in grib file
istate_in.close()
# total number of modes
nmodes = len(messages_in[0].values)
# seed the random generator
np.random.seed(seed)
random.seed(seed)
# pick random even modes
modes = np.random.choice(range(0, nmodes, 2), members, replace=False)
# define input file
for idx, mode in enumerate(modes):
# define new experiment name
expName = expName_generator()
# output directory experiment
outDirExp = os.path.join(outdir, expName)
# remove directory if exists and force==True
if force:
shutil.rmtree(outDirExp, ignore_errors=True)
# create output directory
os.makedirs(outDirExp)
# create logger
logger = open(os.path.join(outDirExp, expName + ".log"), 'w')
log = {}
log['input'] = ["[Input] sha256 {}: {}\n".format
(file_in, file_in_sha256)]
log['output'] = []
# randomize sign of perturbation
pert = np.random.choice([-1, 1]) * pert
# list of outputfiles to copy that don't need to be modified
oFiles = ["ICMGG{}INIT", "ICMGG{}INIUA"]
for oFile in oFiles:
outputFile = oFile.format(expName)
outputPath = os.path.join(outDirExp, outputFile)
inputFile = oFile.format(iexp)
inputPath = os.path.join(istate, inputFile)
# copy original initial state directory to new experiment
try:
shutil.copyfile(inputPath, outputPath)
# add to logging dict
sha256_in = sha256_checksum(inputPath)
sha256_out = sha256_checksum(outputPath)
log['input'].append("[Input] sha256 {}: {}\n".format
(inputPath, sha256_in))
log['output'].append("[Output] sha256 {}: {}\n".format
(outputPath, sha256_out))
except IOError:
pass # silently fail
# create a new grib file in which perturbed initial conditions
# will be saved
outFile = "ICMSH{}INIT".format(expName)
fileOut = os.path.join(outDirExp, outFile)
grbOut = open(fileOut, 'wb')
# perturb
for msg in messages_in:
if (msg['shortName'] == var and msg['level'] == level):
t = msg['values']
t[mode] = t[mode] + pert
msg['values'] = t
else:
pass
# write perturbed gribfile
grbOut.write(msg.tostring())
# Closing the perturbed gribfile
grbOut.close()
sha256_out = sha256_checksum(fileOut)
# add to logging dict
log['output'].insert(0, "[Output] sha256 {}: {}\n".format
(fileOut, sha256_out))
# write log
[logger.write(msg) for msg in log['input']]
logger.write("\n")
[logger.write(msg) for msg in log['output']]
# test if perturbation was a success
if testPerturbation(file_in, fileOut, var, level, mode):
msg = (("\nPerturbed ensemble {}, variable {}, mode {}, " +
"level {}, by amount {}. ").format
(expName, var, mode, level, pert))
print(msg)
logger.write(msg)
msg = (("Random seed {} was used to create the perturbed " +
"initial state.\n").format(seed))
logger.write(msg)
logger.close()
else:
msg = (("Perturbation failed: input file {} and output file {}" +
" are the same.\n").format(file_in, fileOut))
logger.write("\n {}".format(msg))
logger.close()
raise IOError(msg)
def testPerturbation(iFile, oFile, var, level, mode):
'''
Test if perturbation was succesful
:param iFile: original input file [path]
:type iFile: string
:param oFile: perturbed output file [path]
:type oFile: string
:param var: variable name
:type var: string
:param level: perturbed model level
:type level: int
:param mode: perturbed mode
:type mode: int
:returns: success
:rtype: bool
'''
# unperturbed gribfile
grbindxIn = pygrib.index(iFile, 'shortName', 'level')
unPert = grbindxIn.select(shortName=var, level=level)[0]['values'][mode]
grbindxIn.close()
# perturbed gribfile
grbindxOut = pygrib.index(oFile, 'shortName', 'level')
pert = grbindxOut.select(shortName=var, level=level)[0]['values'][mode]
grbindxOut.close()
if not (unPert == pert):
return True
else:
return False
if __name__ == '__main__':
# define command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--initialstate',
help='Directory containing initial state to perturb',
action=FullPaths, type=is_dir, required=True)
parser.add_argument('-e', '--experiment',
help='Experiment name of initial state to perturb',
type=str, required=True)
parser.add_argument('-o', '--outputdir', help='Output directory',
action=FullPaths, type=is_dir, required=True)
parser.add_argument('-m', '--members',
help='Number of initial states to create',
type=int, default=1, required=False)
parser.add_argument('-s', '--seed', help='Random seed', type=int,
required=False)
parser.add_argument('-f', '--force', action='store_true',
help="force overwriting existing files")
parser.add_argument('-v', '--variable', type=str, required=False,
help="variable name", default='t')
parser.add_argument('-l', '--level', type=int, required=False,
help="model level", default=30)
parser.add_argument('-p', '--perturbation', type=float, required=False,
help="perturbation amount", default=0.1)
# get arguments
args = parser.parse_args()
# call main()
main(args.initialstate, args.experiment, args.outputdir,
args.members, args.seed,
args.variable, args.level, args.perturbation, args.force)