-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_eam_ic_nonudge.py
149 lines (106 loc) · 3.88 KB
/
create_eam_ic_nonudge.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
#! /usr/bin/env python
#==============================================================================
# create_eam_ic_nonudge.py
"""
Python script.
This script is to replace state variables and surface pressure (U, V, T, Q, PS)
in a EAM initial condition file with reanalysis data.
The Renalaysis has to be processed to the model grid (e.g., the nudging files)
You need to have a initial condition file from any model runs (e.g., .i file).
One way to obtain this without doing a nudging run is to perform a AMIP
run and save the ".i" file at the desired time.
This script should be run in the directory where the renalysis data is.
Interactive usage:
create_eam_ic_nonudge.py <case_id nudge_xml_file targ_dir
year month day hour>
year month day_start day_end hours>
Arguments:
case_id # input file name
nudge_xml_file # reanalysis file (nc or xml)
targ_dir # move new file to the target directory (have to create first)
year # reanalysis year
month # reanalysis month
day # reanalysis day
hour # reanalysis hour
"""
#==============================================================================
import subprocess
import sys
import numpy as n
import cdms2
import cdtime
VAR_LIST = ['U', 'V', 'T', 'Q']
def _make_dtg_tag(time):
#-----------------------
"""
time set up
"""
seconds = time.hour * 60 * 60
ymdh = '%d-%02d-%02d-%05d' % (time.year, time.month, time.day, seconds)
return(ymdh)
def _swap(src_filename, targ_filename, time):
#--------------------------------------------
"""
src_filename # source file name
targ_filename # target file name
time # time
"""
print ('File names: ', src_filename, targ_filename)
fsrc = cdms2.open(src_filename)
ftarg = cdms2.open(targ_filename, 'r+')
for myvar in VAR_LIST:
vsrc = fsrc(myvar, time=time)
vtarg = ftarg.variables[myvar]
print ('time: ', time)
print ('src_filename: ', src_filename)
print ('targ_filename: ', targ_filename)
try:
vtarg[0] = vsrc[0]
except TypeError:
vtarg[0] = vsrc[0].astype(n.float)
vsrc = fsrc('PS', time=time)
vtarg = ftarg.variables['PS']
try:
vtarg[0] = vsrc[0]
except TypeError:
vtarg[0] = vsrc[0].astype(n.float)
fsrc.close()
ftarg.close()
return
def _create_eam_ic_nonudge(case_id, nudge_xml_file, targ_dir,
year, month, day, hour):
#---------------------------------------------------------------
"""
See file header.
"""
time_suff = cdtime.comptime(year, month, day, hour)
tag_suff = _make_dtg_tag(time_suff)
# Copy new atm file to correct name for running.
#-------------------------------------------------------
src_file = case_id + '.nc'
targ_file = 'fa_' + case_id + '_' + tag_suff + '.nc'
cmd = 'cp ' + src_file + ' ' + targ_file
print (cmd)
ret = subprocess.Popen(cmd, shell=True)
ret.wait()
# Swap in the new fields in the atm file.
#----------------------------------------
_swap(nudge_xml_file, targ_file, time_suff)
# Move the renamed, swapped files to the proper directory.
#---------------------------------------------------------
#cmd = 'mv ' + targ_file + ' ' + targ_dir
#print (cmd)
#ret = subprocess.Popen(cmd, shell=True)
#ret.wait()
return
if __name__ == '__main__':
#-------------------------
case_id = sys.argv[1]
nudge_xml_file = sys.argv[2]
targ_dir = sys.argv[3]
year = int(sys.argv[4])
month = int(sys.argv[5])
day = int(sys.argv[6])
hour = int(sys.argv[7])
_create_eam_ic_nonudge(case_id, nudge_xml_file, targ_dir,
year, month, day, hour)