1 |
#!/opt/env/minihycom/bin/python |
---|
2 |
|
---|
3 |
""" |
---|
4 |
Drill North Atlantic HYCOM THREDDS catalog for for input to Ichthyop. |
---|
5 |
|
---|
6 |
Catalog is sliced by time, bounding box, and area of interest. |
---|
7 |
Depth dimension is eliminated by using only top layer. |
---|
8 |
Dimensions are transformed by specialized functions to simulate ROMS input |
---|
9 |
(TBD with Trond Kristiansen). |
---|
10 |
|
---|
11 |
Delete this module from the importing namespace after use |
---|
12 |
to free up memory consumed by the input catalog. |
---|
13 |
""" |
---|
14 |
|
---|
15 |
import datetime |
---|
16 |
|
---|
17 |
__authors__ = ('Chris Calloway', |
---|
18 |
'Trond Kristiansen', |
---|
19 |
'Nathan F. Putman', |
---|
20 |
'Thomas Shay', |
---|
21 |
) |
---|
22 |
__copyright__ = 'Copyright 2008 UNC-Chapel Hill Department of Marine Sciences' |
---|
23 |
__license__ = 'GPL2' |
---|
24 |
__version__ = '1.0.0' |
---|
25 |
__created__ = datetime.date(2008, 6, 11) |
---|
26 |
__modified__ = datetime.date(2008, 6, 11) |
---|
27 |
|
---|
28 |
import os |
---|
29 |
import dap.client |
---|
30 |
import Nio |
---|
31 |
|
---|
32 |
# Move this out to a config file |
---|
33 |
dataSource = 'http://hycom.coaps.fsu.edu/thredds/dodsC/atl_ops' |
---|
34 |
dataDestination = '/seacoos/data/minihycom/minihycom.nc' |
---|
35 |
|
---|
36 |
# Nio cannot create new file when old exists |
---|
37 |
try: |
---|
38 |
os.remove(dataDestination) |
---|
39 |
except: |
---|
40 |
# FIX THIS: Narrow down source of exception |
---|
41 |
print dataDestination, 'does not already exist.' |
---|
42 |
|
---|
43 |
# Note: is it not necessary (or possible) to close the input catalog, |
---|
44 |
# as backing files are only opened during data access functions. |
---|
45 |
dataIn = dap.client.open(dataSource, verbose=1) |
---|
46 |
|
---|
47 |
# Map numpy types to Nio types. |
---|
48 |
# Expand as necessary to cover more types. |
---|
49 |
# Expand as necessary to cover Numeric and Numarray. |
---|
50 |
# Among other things, check this if you see a glibc error. |
---|
51 |
datatypes = {'Float32' : 'f', |
---|
52 |
'Float64' : 'd', |
---|
53 |
} |
---|
54 |
|
---|
55 |
# Dimensional constraints. |
---|
56 |
timeName = dataIn.MT.name |
---|
57 |
timeType = datatypes[dataIn.MT.type] |
---|
58 |
minTime = 0 |
---|
59 |
maxTime = 3 |
---|
60 |
timeLen = maxTime - minTime |
---|
61 |
|
---|
62 |
# Note: depth is not referenced as a dimension |
---|
63 |
# by any other variable in the output except depth. |
---|
64 |
depthName = dataIn.Depth.name |
---|
65 |
depthType = datatypes[dataIn.Depth.type] |
---|
66 |
minDepth = 0 |
---|
67 |
maxDepth = dataIn.Depth.shape[0] |
---|
68 |
depthLen = maxDepth - minDepth |
---|
69 |
|
---|
70 |
latitudeName = dataIn.Latitude.name |
---|
71 |
latitudeType = datatypes[dataIn.Latitude.type] |
---|
72 |
minLatitude = 365 |
---|
73 |
maxLatitude = 1214 |
---|
74 |
latitudeLen = maxLatitude - minLatitude |
---|
75 |
|
---|
76 |
longitudeName = dataIn.Longitude.name |
---|
77 |
longitudeType = datatypes[dataIn.Longitude.type] |
---|
78 |
minLongitude = 0 |
---|
79 |
maxLongitude = dataIn.Longitude.shape[0] |
---|
80 |
longitudeLen = maxLongitude - minLongitude |
---|
81 |
|
---|
82 |
# Variables of interest. |
---|
83 |
dateName = dataIn.Date.name |
---|
84 |
dateType = datatypes[dataIn.Date.type] |
---|
85 |
uName = dataIn.u.name |
---|
86 |
uType = datatypes[dataIn.u.type] |
---|
87 |
vName = dataIn.v.name |
---|
88 |
vType = datatypes[dataIn.v.type] |
---|
89 |
temperatureName = dataIn.temperature.name |
---|
90 |
temperatureType = datatypes[dataIn.temperature.type] |
---|
91 |
salinityName = dataIn.salinity.name |
---|
92 |
salinityType = datatypes[dataIn.salinity.type] |
---|
93 |
densityName = dataIn.density.name |
---|
94 |
densityType = datatypes[dataIn.density.type] |
---|
95 |
sshName = dataIn.ssh.name |
---|
96 |
sshType = datatypes[dataIn.ssh.type] |
---|
97 |
thicknessName = dataIn.surface_boundary_layer_thickness.name |
---|
98 |
thicknessType = datatypes[dataIn.surface_boundary_layer_thickness.type] |
---|
99 |
|
---|
100 |
def createVariableAndAttributes(dataOut, varName, varType, dims, dimSlices): |
---|
101 |
""" |
---|
102 |
Create variable and all attributes at once. |
---|
103 |
|
---|
104 |
createVariableAndAttributes(dataOut, varName, varType, dims, dimSlices) -> 0 |
---|
105 |
|
---|
106 |
varName - string name of variable |
---|
107 |
varType - string for Nio type code |
---|
108 |
dims - tuple of dimension names |
---|
109 |
dimSlices - tuple of slice objects constraining input dataset |
---|
110 |
""" |
---|
111 |
dummy = dataOut.create_variable(varName, varType, dims) |
---|
112 |
dummy[:] = dataIn[varName][dimSlices] |
---|
113 |
for attributeName, attributeValue in dataIn[varName].attributes.items(): |
---|
114 |
setattr(dataOut.variables[varName], attributeName, attributeValue) |
---|
115 |
return 0 |
---|
116 |
|
---|
117 |
def _main(): |
---|
118 |
""" |
---|
119 |
Make the output NetCDF dataset. |
---|
120 |
|
---|
121 |
Run module as script to invoke. |
---|
122 |
""" |
---|
123 |
dataOut = Nio.open_file(dataDestination, mode="c") |
---|
124 |
|
---|
125 |
# Copy global attributes. |
---|
126 |
for attributeName, attributeValue in dataIn.attributes['NC_GLOBAL'].items(): |
---|
127 |
setattr(dataOut, attributeName, attributeValue) |
---|
128 |
|
---|
129 |
# Create dimensions |
---|
130 |
dataOut.create_dimension(timeName, timeLen) |
---|
131 |
# Note: depth is not referenced as a dimension |
---|
132 |
# by any other variable in the output except depth. |
---|
133 |
dataOut.create_dimension(depthName, depthLen) |
---|
134 |
dataOut.create_dimension(latitudeName, latitudeLen) |
---|
135 |
dataOut.create_dimension(longitudeName, longitudeLen) |
---|
136 |
|
---|
137 |
# Create variables and their attributes. |
---|
138 |
createVariableAndAttributes(dataOut, |
---|
139 |
timeName, |
---|
140 |
timeType, |
---|
141 |
(timeName,), |
---|
142 |
(slice(minTime, maxTime),)) |
---|
143 |
|
---|
144 |
# Note: depth is not referenced as a dimension |
---|
145 |
# by any other variable in the output except depth. |
---|
146 |
createVariableAndAttributes(dataOut, |
---|
147 |
depthName, |
---|
148 |
depthType, |
---|
149 |
(depthName,), |
---|
150 |
(slice(minDepth, maxDepth),)) |
---|
151 |
|
---|
152 |
createVariableAndAttributes(dataOut, |
---|
153 |
latitudeName, |
---|
154 |
latitudeType, |
---|
155 |
(latitudeName,), |
---|
156 |
(slice(minLatitude, maxLatitude),)) |
---|
157 |
|
---|
158 |
createVariableAndAttributes(dataOut, |
---|
159 |
longitudeName, |
---|
160 |
longitudeType, |
---|
161 |
(longitudeName,), |
---|
162 |
(slice(minLongitude, maxLongitude),)) |
---|
163 |
|
---|
164 |
# The Date variable is another expression of the time dimension. |
---|
165 |
createVariableAndAttributes(dataOut, |
---|
166 |
dateName, |
---|
167 |
dateType, |
---|
168 |
(timeName,), |
---|
169 |
(slice(minTime, maxTime),)) |
---|
170 |
|
---|
171 |
# Eliminate depth as a dimension by slicing a scalar depth. |
---|
172 |
createVariableAndAttributes(dataOut, |
---|
173 |
uName, |
---|
174 |
uType, |
---|
175 |
(timeName, latitudeName, longitudeName,), |
---|
176 |
(slice(minTime, maxTime), |
---|
177 |
0, |
---|
178 |
slice(minLatitude, maxLatitude), |
---|
179 |
slice(minLongitude, maxLongitude),)) |
---|
180 |
|
---|
181 |
createVariableAndAttributes(dataOut, |
---|
182 |
vName, |
---|
183 |
vType, |
---|
184 |
(timeName, latitudeName, longitudeName,), |
---|
185 |
(slice(minTime, maxTime), |
---|
186 |
0, |
---|
187 |
slice(minLatitude, maxLatitude), |
---|
188 |
slice(minLongitude, maxLongitude),)) |
---|
189 |
|
---|
190 |
createVariableAndAttributes(dataOut, |
---|
191 |
temperatureName, |
---|
192 |
temperatureType, |
---|
193 |
(timeName, latitudeName, longitudeName,), |
---|
194 |
(slice(minTime, maxTime), |
---|
195 |
0, |
---|
196 |
slice(minLatitude, maxLatitude), |
---|
197 |
slice(minLongitude, maxLongitude),)) |
---|
198 |
|
---|
199 |
createVariableAndAttributes(dataOut, |
---|
200 |
salinityName, |
---|
201 |
salinityType, |
---|
202 |
(timeName, latitudeName, longitudeName,), |
---|
203 |
(slice(minTime, maxTime), |
---|
204 |
0, |
---|
205 |
slice(minLatitude, maxLatitude), |
---|
206 |
slice(minLongitude, maxLongitude),)) |
---|
207 |
|
---|
208 |
createVariableAndAttributes(dataOut, |
---|
209 |
densityName, |
---|
210 |
densityType, |
---|
211 |
(timeName, latitudeName, longitudeName,), |
---|
212 |
(slice(minTime, maxTime), |
---|
213 |
0, |
---|
214 |
slice(minLatitude, maxLatitude), |
---|
215 |
slice(minLongitude, maxLongitude),)) |
---|
216 |
|
---|
217 |
# Depth is not a dimension in the input for the remaining variables. |
---|
218 |
createVariableAndAttributes(dataOut, |
---|
219 |
sshName, |
---|
220 |
sshType, |
---|
221 |
(timeName, latitudeName, longitudeName,), |
---|
222 |
(slice(minTime, maxTime), |
---|
223 |
slice(minLatitude, maxLatitude), |
---|
224 |
slice(minLongitude, maxLongitude),)) |
---|
225 |
|
---|
226 |
createVariableAndAttributes(dataOut, |
---|
227 |
thicknessName, |
---|
228 |
thicknessType, |
---|
229 |
(timeName, latitudeName, longitudeName,), |
---|
230 |
(slice(minTime, maxTime), |
---|
231 |
slice(minLatitude, maxLatitude), |
---|
232 |
slice(minLongitude, maxLongitude),)) |
---|
233 |
|
---|
234 |
# Write the output dataset |
---|
235 |
dataOut.close() |
---|
236 |
return 0 |
---|
237 |
|
---|
238 |
# Run as script. |
---|
239 |
if __name__ == "__main__": |
---|
240 |
_main() |
---|