-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathcompute_woce_transects.py
More file actions
executable file
·200 lines (160 loc) · 7.88 KB
/
Copy pathcompute_woce_transects.py
File metadata and controls
executable file
·200 lines (160 loc) · 7.88 KB
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
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, \
unicode_literals
import gsw
import xarray
import os
import numpy
import argparse
import zipfile
import glob
from datetime import datetime
from mpas_analysis.shared.io.utility import make_directories
from mpas_analysis.shared.io.download import download_files
from mpas_analysis.shared.io import write_netcdf
def process_transect(url, stations, outFileName, transectName, inDir, outDir):
urlBase, file = url.rsplit('/', 1)
download_files([file], urlBase=urlBase, outDir=inDir)
with zipfile.ZipFile('{}/{}'.format(inDir, file), 'r') as f:
f.extractall('{}/stations/'.format(inDir))
inFileList = sorted(glob.glob('{}/stations/{}_*.nc'.format(
inDir, transectName)))
pressureValues = set()
fileNames = {}
validStations = set()
for fileName in inFileList:
path, file = os.path.split(fileName)
_, station, cast, _ = file.split('_')
station = int(station)
cast = int(cast)
if station not in stations:
continue
validStations.add(station)
with xarray.open_dataset(fileName) as ds:
for pressure in ds.pressure.values:
pressureValues.add(pressure)
if station not in fileNames:
fileNames[station] = {}
fileNames[station][cast] = fileName
stations = validStations
nStations = len(stations)
nDepths = len(pressureValues)
latitude = numpy.zeros(nStations)
longitude = numpy.zeros(nStations)
pressure = numpy.zeros((nStations, nDepths))
temperature = numpy.zeros((nStations, nDepths))
salinity = numpy.zeros((nStations, nDepths))
nValues = numpy.zeros((nStations, nDepths), int)
for stationIndex, station in enumerate(stations):
castCount = len(fileNames[station])
for cast in fileNames[station]:
fileName = fileNames[station][cast]
with xarray.open_dataset(fileName) as ds:
nPoints = len(ds.pressure)
nValues[stationIndex, 0:nPoints] += 1
pressure[stationIndex, 0:nPoints] += ds.pressure
temperature[stationIndex, 0:nPoints] += ds.temperature
salinity[stationIndex, 0:nPoints] += ds.salinity
longitude[stationIndex] += ds.longitude.values[0]
latitude[stationIndex] += ds.latitude.values[0]
longitude[stationIndex] /= castCount
latitude[stationIndex] /= castCount
# average over casts
validMask = nValues > 0
pressure = numpy.ma.masked_array(
numpy.divide(pressure, nValues, where=validMask),
mask=(nValues == 0))
temperature = numpy.ma.masked_array(
numpy.divide(temperature, nValues, where=validMask),
mask=(nValues == 0))
salinity = numpy.ma.masked_array(
numpy.divide(salinity, nValues, where=validMask),
mask=(nValues == 0))
Lat = numpy.tile(latitude.reshape(nStations, 1), (1, nDepths))
Lon = numpy.tile(longitude.reshape(nStations, 1), (1, nDepths))
z = numpy.ma.masked_all(pressure.shape)
z[validMask] = gsw.z_from_p(pressure[validMask], Lat[validMask])
SA = gsw.SA_from_SP(salinity[validMask], pressure[validMask],
Lon[validMask], Lat[validMask])
CT = gsw.CT_from_t(SA, temperature[validMask], pressure[validMask])
potDensity = numpy.ma.masked_all(pressure.shape)
potDensity[validMask] = gsw.rho(SA, CT, 0.)
potTemp = numpy.ma.masked_all(pressure.shape)
potTemp[validMask] = gsw.pt0_from_t(SA, temperature[validMask],
pressure[validMask])
longitude = xarray.DataArray.from_dict({'dims': ('nPoints',),
'data': longitude,
'attrs': {'long_name': 'longitude',
'units': 'degrees'}})
latitude = xarray.DataArray.from_dict({'dims': ('nPoints',),
'data': latitude,
'attrs': {'long_name': 'latitude',
'units': 'degrees'}})
pressure = xarray.DataArray.from_dict({'dims': ('nPoints',
'nz'),
'data': pressure,
'attrs':
{'long_name': 'pressure',
'units': 'dbar'}})
z = xarray.DataArray.from_dict({'dims': ('nPoints', 'nz'),
'data': z,
'attrs':
{'long_name': 'height',
'units': 'm'}})
potTemp = xarray.DataArray.from_dict({'dims': ('nPoints',
'nz'),
'data': potTemp,
'attrs':
{'long_name':
'potential temperature',
'units': 'deg C'}})
salinity = xarray.DataArray.from_dict({'dims': ('nPoints',
'nz'),
'data': salinity,
'attrs':
{'long_name': 'salinity',
'units': 'PSU'}})
potDensity = xarray.DataArray.from_dict(
{'dims': ('nPoints', 'nz'),
'data': potDensity,
'attrs': {'long_name': 'potential denisty',
'units': 'kg m^{-3}'}})
dsTransect = xarray.Dataset({'lon': longitude,
'lat': latitude,
'pressure': pressure,
'z': z,
'potentialTemperature': potTemp,
'salinity': salinity,
'potentialDensity': potDensity})
write_netcdf(dsTransect, '{}/{}'.format(outDir, outFileName))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-i", "--inDir", dest="inDir", required=True,
help="Directory where intermediate files used in "
"processing should be downloaded")
parser.add_argument("-o", "--outDir", dest="outDir", required=True,
help="Directory where final preprocessed observation "
"are stored")
args = parser.parse_args()
make_directories(args.inDir)
make_directories(args.outDir)
date = datetime.now().strftime('%Y%m%d')
process_transect(url='https://cchdo.ucsd.edu/data/4801/a21_nc_ctd.zip',
stations=numpy.arange(102, 118),
outFileName='WOCE_A21_Drake_Passage_{}.nc'.format(date),
transectName='a21',
inDir=args.inDir,
outDir=args.outDir)
process_transect(url='https://cchdo.ucsd.edu/data/3654/a23_nc_ctd.zip',
stations=numpy.arange(3, 128),
outFileName='WOCE_A23_South_Atlantic_{}.nc'.format(date),
transectName='a23',
inDir=args.inDir,
outDir=args.outDir)
process_transect(url='https://cchdo.ucsd.edu/data/4941/a12_nc_ctd.zip',
stations=numpy.arange(536, 607),
outFileName='WOCE_A12_Prime_Meridian_{}.nc'.format(date),
transectName='a12',
inDir=args.inDir,
outDir=args.outDir)