-
Notifications
You must be signed in to change notification settings - Fork 0
/
hysplit.py
273 lines (239 loc) · 12.5 KB
/
hysplit.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
General Python class for creating HYSPLIT back trajectories.
Created on Sat Nov 1 21:11:10 2014
@author: Von P. Walden
Washington State University
Laboratory for Atmospheric Research
"""
class HYSPLIT:
"""This class defines attributes and methods for calculating back trajectories
using NOAA's HYSPLIT model.
Example usage #1:
# Retrieve reanalysis data from NOAA for the ICECAPS time period.
import hysplit
import pandas as pd
dates = pd.date_range('2019-12-01','2020-03-01', freq='1M') # Dec 2019, Jan 2020 and Feb 2020
hy = hysplit.HYSPLIT(dates)
hy.retrieveReanalysisDataFromNOAA()
Example usage #2:
# Calculate a back trajectory for a given date.
import hysplit
import pandas as pd
dates = pd.date_range('2020-01-09', '2020-01-17 18:00', freq='3H') # Every 3 hours for 9 days in Jan 2020
length = 120 # in hours
lat = +46.7
lon = -117.2
alts = [500, 1000, 1500]
hy = hysplit.HYSPLIT(dates,length,lat,lon,alts,'Pullman')
hy.runBackTrajectory() # Pullman, WA at 500 m, 1000 m and 1500 m.
"""
def __init__(self, dates, length = 48, latitude=0., longitude=0., altitudes=0., descriptor='backTrajectory'):
"""INITIAL: Initializes the HYSPLIT processing object with the
desired dates to process.
Inputs:
dates - list of datetime objects to create back trajectories for; MUST BE A LIST!!
length - length of back trajectory in hours.
latitude - latitude of the start position for back trajectory.
longitude - longitude of the start position for back trajectory.
altitude - series of altitudes to start back trajectories from.
descriptor - text that describes back trajectories; used for filenames.
Written by Von P. Walden
1 Nov 2014
Updated on 8 Nov 2015
20 Nov 2015 - Changed the way dates are inputed; list of
datetime objects instead of beginning and ending.
29 Nov 2015 - Can now input latitude, longitude and altitudes.
"""
import sys
from socket import gethostname
from dateutil.relativedelta import relativedelta
# Specify the dates to process.
self.dates = dates
self.length = length
self.latitude = latitude
self.longitude = longitude
self.altitudes = altitudes
self.descriptor = descriptor
self.deltaMonths = relativedelta(months=1)
# Set up the necessary directories.
self.hostname = gethostname()
if self.hostname.rfind('aeolus')>=0 or self.hostname.rfind('compute')>=0:
# For aeolus.wsu.edu (Linux cluster)
self.directory = {'data': '/data/lar/users/vonw/hysplit4/reanalysis/',
'traj': '/data/lar/users/vonw/hysplit4/backTrajectories/',
'code': '/home/vonw/hysplit4/exec/',
'plot': '/data/lar/users/vonw/hysplit4/backTrajectories/plots/'}
elif self.hostname.rfind('sila')>=0 or self.hostname.rfind('nuia')>=0:
# For sila.paccar.wsu.edu (iMac)
self.directory = {'data': '/Users/vonw/data/hysplit/reanalysis/',
'traj': '/Users/vonw/data/hysplit/backTrajectories/',
'code': '/Users/vonw/hysplit/exec/',
'plot': '/Users/vonw/data/hysplit/backTrajectories/plots/'}
#elif self.hostname.rfind('gaia')>=0:
# For 134.121.21.45 (gaia - Linux workstation)
# self.directory = {'data': '/mnt/data/vonw/hysplit/reanalysis/',
# 'traj': '/mnt/data/vonw/hysplit/backTrajectories/',
# 'code': '/home/vonw/hysplit4/exec/',
# 'plot': '/mnt/data/vonw/hysplit/backTrajectories/plots/'}
elif self.hostname.rfind('gaia')>=0:
# For 134.121.21.204 (gaia - Linux workstation)
self.directory = {'data': '/home/anacarla/hysplit/data/reanalysis/',
'traj': '/home/anacarla/hysplit/traj/',
'container': 'apptainer shell /apptainer/hysplit/hysplit-5.1.0.sif cd /home/anacarla/hysplit/traj/',
'code': 'exec /opt/share/hysplit.v5.1.0_UbuntuOS20.04.2LTS/exec/',
'plot': '/home/anacarla/hysplit/traj/plots/'}
else:
print('Whoa, Partner... Your local computer is unrecognized by hysplit.HYSPLIT. Talk to Von!')
sys.exit()
return
def retrieveReanalysisDataFromNOAA(self):
"""Automates the retrieval of reanalysis data from NOAAs
data archive. Currently one can only retrieve data from the
NCEP/NCAR reanalyses.
Written by Von P. Walden
1 Nov 2014
"""
import os
from subprocess import call
os.chdir(self.directory['data'])
CDC = 'ftp://arlftp.arlhq.noaa.gov/archives/reanalysis/'
for date in self.dates:
f = 'RP'+date.strftime('%Y%m')+'.gbl'
print('Retrieving NCEP/NCAR reanalyses data: '+f)
call(['wget', CDC+f])
return
def retrieveMostRecent7daysReanalysisDataFromNOAA(self):
"""Automates the retrieval of reanalysis data from NOAAs
data archive. Currently one can only retrieve data from the
NCEP/NCAR reanalyses.
Usage:
import hysplit
dates = [datetime.datetime.now()]
hy = hysplit.HYSPLIT(dates)
hy.retrieveMostRecent7daysReanalysisDataFromNOAA()
Written by Von P. Walden
1 Nov 2014
"""
import os
from subprocess import call
os.chdir(self.directory['data'])
CDC = 'ftp://arlftp.arlhq.noaa.gov/pub/archives/gdas1/'
for date in self.dates:
f = 'current7days.t00z'
print('Retrieving GDAS reanalyses data: '+f)
call(['wget', CDC+f])
return
def runBackTrajectory(self):
"""Creates HYSPLIT input file based on the __init__ function, then
runs the specified backtrajectory.
Written by Von P. Walden
1 Nov 2014
Updated 29 Sep 2014 - Added ability to specify
lat, lon and alt.
20 Nov 2015 - Added capability to specify the
number of hours to calculate the
back trajectory for.
29 Nov 2015 - Cleaned things up by putting all
initializations into __init__.
"""
import os
from subprocess import call
# Navigate to the "trajectory" directory.
os.chdir(self.directory['traj'])
for date in self.dates:
# Define a unique date string for the trajectory.
dstr = date.strftime('%Y%m%d%H')
# Based on the desired date, determine the three nearest reanalysis data files.
month1 = date.strftime('%Y%m')
month2 = (date-self.deltaMonths).strftime('%Y%m')
month3 = (date+self.deltaMonths).strftime('%Y%m')
# Create the SETUP input file for HYSPLIT
f = open(self.directory['traj']+'SETUP.'+dstr,'w')
f.write('&SETUP\n')
f.write('KMSL=0,\n')
f.write('tm_tpot=1,\n')
f.write('tm_tamb=1,\n')
f.write('tm_rain=1,\n')
f.write('tm_mixd=1,\n')
f.write('tm_relh=1,\n')
f.write('tm_terr=1,\n')
f.write('tm_dswf=1,\n')
f.write('/ \n')
f.close()
# Create the CONTROL input file for HYSPLIT
f = open(self.directory['traj']+'CONTROL.'+dstr,'w')
f.write(date.strftime('%y %m %d %H %M')+'\n')
f.write('%d\n' % len(self.altitudes))
for altitude in self.altitudes:
f.write('%9f %10f %d\n' % (self.latitude, self.longitude, altitude))
f.write(str(int(-self.length))+'\n')
f.write('0 \n')
f.write('13000\n')
f.write('3\n')
f.write(self.directory['data']+'\n')
f.write('RP'+month1+'.gbl\n')
f.write(self.directory['data']+'\n')
f.write('RP'+month2+'.gbl\n')
f.write(self.directory['data']+'\n')
f.write('RP'+month3+'.gbl\n')
f.write(self.directory['traj']+'\n')
f.write(self.descriptor+date.strftime('%Y%m%d%H')+'.trj\n')
f.close()
if self.hostname.rfind('aeolus')>=0 or self.hostname.rfind('compute')>=0:
# For aeolus.wsu.edu (Linux cluster)
call([self.directory['code']+'hyts_std', dstr])
elif self.hostname.rfind('sila')>=0 or self.hostname.rfind('nuia')>=0:
# For sila.paccar.wsu.edu (iMac) or nuia (Macbook Pro)
call([self.directory['code']+'hyts_std', dstr])
elif self.hostname.rfind('gaia')>=0:
# For 134.121.21.204 (gaia - Linux workstation)
call([self.directory['container']],shell=True)
call([self.directory['code']+'hyts_std', dstr],shell=True)
return
def plotBackTrajectory(self, colorScaleVariable='altitude'):
"""Generate a plot of HYSPLIT back trajectories.
Written by Von P. Walden
1 Nov 2014
Updated: 11 Aug 2020 - Now uses cartopy and geopandas instead of basemap.
"""
import pandas as pd
import hvplot
import hvplot.pandas # noqa
for date in self.dates:
# Define a unique date string for the trajectory.
dstr = date.strftime('%Y%m%d%H')
print('Creating HTML plot for: '+self.descriptor+dstr+'.trj')
# Read in the trajectory data file.
numalts = len(self.altitudes)
df = pd.read_csv(self.directory['traj']+self.descriptor+dstr+'.trj',
skiprows=6+numalts,
delimiter=r"\s+",
names=['trajectory',
'run',
'year',
'month',
'day',
'hour',
'minute',
'seconds',
'time',
'latitude',
'longitude',
'altitude',
'pressure',
'potential temperature',
'air temperature',
'rainfall',
'mix depth',
'relative humidity',
'terrain above msl',
'solar flux'])
plot = df.hvplot.points('longitude', 'latitude',
geo=True,
c=df[colorScaleVariable],
clabel=colorScaleVariable,
cmap='viridis',
tiles='StamenTerrain',
title='Back Trajectories for ' + self.descriptor + ' (' + colorScaleVariable + ')' + ': ' + dstr)
hvplot.save(plot, self.directory['plot'] + self.descriptor + '_' + colorScaleVariable + '_' + dstr + '.html')
return