-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
181 lines (156 loc) · 5.56 KB
/
util.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json
from datetime import datetime, timedelta
import glob
import numpy as np
import xarray as xr
from zeep import Client
from requestEDSdata import EDSrequestData
import netCDF4 as nc
from parcels import FieldSet, ParticleSet, JITParticle, AdvectionRK4
class EDS:
def __init__(self, start_date, end_date, x1, y1, x2, y2):
self.start = start_date
self.end = end_date
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def get_data(self):
client = Client('http://coastmap.com/eds20/eds.asmx?WSDL')
result = client.service.GetData(
Key='rpstest',
SourceID=696,
SourceName='Hycom_Global',
OutputType=1,
StartDate=self.start.strftime("%Y-%m-%dT%H:%M:%S"),
EndDate=self.end.strftime("%Y-%m-%dT%H:%M:%S"),
X1=self.x1,
Y1=self.y1,
X2=self.x2,
Y2=self.y2,
Zipped=False
)
while client.service.GetStatus(result) != 'COMPLETE ':
if client.service.GetStatus(result) == 'COMPLETE ':
print("Status: 'COMPLETE '")
url = f'https://coastmap.com/edsoutput/{result}'
print(f'URL: {url}')
return url
def parcels_to_geojson(lat, lon, t0=(datetime.today() - timedelta(days=6)).strftime('%Y-%m-%d'), dataset='HYCOM GLOBAL NAVY'): #'2010_0420'='HYCOM GLOBAL NAVY'
# Download new files
EDSrequestData(lat, lon, t0=t0, dataset=dataset)
data = '/data/*'
files = os.listdir('/data') # todo: nix this
fname = 'parcels_out.nc'
ds = nc.Dataset('/data/' + files[0])
names = list(ds.variables.keys())
# determine longitude and latitude variable names
if any(x == 'lon' for x in names):
varlon = 'lon'
varlat = 'lat'
elif any(x == 'longitude' for x in names):
varlon = 'longitude'
varlat = 'latitude'
else:
varlon = 'Longitude'
varlat = 'Latitude'
# determine u and v variable names
if any(x == 'water_u' for x in names):
varu = 'water_u'
varv = 'water_v'
elif any(x == 'uo' for x in names):
varu = 'uo'
varv = 'vo'
else:
varu = 'u'
varv = 'v'
# determine time variable name
if any(x == 'time' for x in names):
vartime = 'time'
else:
vartime = 'MT'
# covert site location from -180 to 180 TO 0 to 360 (if dataset in 0-360)
if any(ds[varlon][:] > 180):
if lon < 0:
lon=360+lon
if any(ds[varlat][:] > 180):
if lat < 0:
lat=360+lat
# Hydrodynamics
# TODO: make input t0 an actual option
# filenames = {'U': data, 'V': data}
# variables = {'U': 'u', 'V': 'v'}
# dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
filenames = {'U': data, 'V': data}
variables = {'U': varu, 'V': varv}
dimensions = {'lat': varlat, 'lon': varlon, 'time': vartime}
fset = FieldSet.from_netcdf(filenames, variables, dimensions, allow_time_extrapolation=True)
# Particles
pset = ParticleSet.from_list(
fieldset=fset,
pclass=JITParticle,
lon=lon, #-88.386944
lat=lat #28.736667
)
# Execute run and save results to output file
pset.execute(
AdvectionRK4,
runtime=timedelta(days=len(files)), # todo: replace this with some other way
dt=timedelta(minutes=60),
# https://github.com/OceanParcels/parcels/issues/704#issuecomment-567840058
output_file=pset.ParticleFile(name=fname, outputdt=timedelta(hours=24))
)
# Load into xarray, then dataframe
ds = xr.load_dataset(fname)
df = ds.to_dataframe().reset_index(drop=True)
# covert 0 to 360 TO -180 to 180
df['lon'] = df.apply(lambda x: x.lon-360 if x.lon > 180 else x.lon, axis=1)
df['lat'] = df.apply(lambda x: x.lat-360 if x.lat > 180 else x.lat, axis=1)
# Geojson features
features = []
for val in df['trajectory'].unique():
d = df[df['trajectory']==val]
feature = {
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': list(zip(d.lon, d.lat))
},
'properties': {
'metadata': {
'id': val
},
'data': {
'lon': {
'type': 'trajectory',
'units': 'degrees',
'times': list(d.time),
'values': list(d.lon)
},
'lat': {
'type': 'trajectory',
'units': 'degrees',
'times': list(d.time),
'values': list(d.lat)
},
'z': {
'type': 'trajectory',
'units': 'meters',
'times': list(d.time),
'values': list(d.z)
}
}
}
}
features.append(feature)
geojson = {'type': 'FeatureCollection', 'features': features}
# Convert time
def time_converter(o):
'''Convert time data type to json serializable string'''
if isinstance(o, type(df.loc[0, 'time'])):
return o.__str__()
# Write geojson
return json.loads(json.dumps(geojson, default=time_converter))