forked from katiedagon/CLM5_ParameterUncertainty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLHC_read_nl.py
79 lines (60 loc) · 2.11 KB
/
LHC_read_nl.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
# For now run this ncar python env in the command line (or bash script)
# Not sure how to execute within python script:
#source /glade/work/kdagon/ncar_pylib_clone/bin/activate
# this doesn't work:
#import subprocess
#subprocess.call(["source","/glade/work/kdagon/ncar_pylib_clone/bin/activate"])
import sys
import os
import math
import numpy
def read_params(param_file=None):
"""
Reads params from disk.
"""
data = None
if param_file is None:
param_file = os.path.dirname(os.path.abspath(__file__))
param_file = os.path.join(param_file, "parameters.npy")
data = numpy.load(file=param_file, allow_pickle=True)
return data
def print_params_nl(case_number=None):
"""
Generates string output for use by bash script.
Namelist parameters only.
:return: string
"""
# Check to see if we have a case number.
if case_number is None:
print ("Missing case number")
return None
# Instantiate the output string
output = ""
# Specify namelist parameter names
nl_param_names = ['fff','dint','baseflow_scalar']
# get all the parameters values from read_params()
sets = read_params()
# case_number must be an integer
case = sets[int(case_number)]
# grab the namelist parameter values (last 3)
nl_params = case[3:6]
# add case number to output string
#output += "\n----case: %s\n" % c_num
# param counter
p_count = 0
# loop over the paramter names
for name in nl_param_names:
# for each param name, put the corresponding value, and format correctly.
output += "%s=%s\n" % (name, nl_params[p_count])
p_count += 1
return output
# Run read and print functions to output
# Must give a case number (specified in bash script)
# sys.argv knows to take the number entered after python command
print (print_params_nl(sys.argv[1]))
# write out parameter values as csv file
# temporary fix until this can be done in one script
#import csv
#with open('LHC.csv', 'w') as output:
# writer = csv.writer(output, lineterminator='\n')
# writer.writerows(param_sets)