-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_asos.py
289 lines (236 loc) · 8.56 KB
/
process_asos.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
This module processes ASOS data. It reads
ASOS 6406 and 6405 files, combines their data,
and produces one netCDF4_CLASSIC file.
Dean Meyer 2021
"""
import io
import os
import datetime as dt
import netCDF4 as nc4
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def asos_6406_to_df(file):
"""
Read an ASOS 64060 file into a Pandas DataFrame.
"""
# read the file and process it
with open(file, "r") as f:
lines = list(f) # read each line of the file into a list
# remove brackets if they exist
lines = [l.replace("[", " ") for l in lines]
lines = [l.replace("]", " ") for l in lines]
# determine number of columns
ncols = len(lines[0].split())
# define column names
if ncols == 9:
cols = [
"StationID",
"DateTime",
"Precip",
"Unknown",
"PrecipAmt",
"FrzPrcpSnsrFreq",
"Press1",
"T",
"Td",
]
if ncols == 10:
cols = [
"StationID",
"DateTime",
"Precip",
"Unknown",
"PrecipAmt",
"FrzPrcpSnsrFreq",
"Press1",
"Press2",
"T",
"Td",
]
if ncols == 11:
cols = [
"StationID",
"DateTime",
"Precip",
"Unknown",
"PrecipAmt",
"FrzPrcpSnsrFreq",
"Press1",
"Press2",
"Press3",
"T",
"Td",
]
# initialize the DataFrame
df = pd.read_csv(
io.StringIO("\n".join(lines)),
names=cols,
na_values=["M"],
delim_whitespace=True,
error_bad_lines=False,
)
# format datetimes and set them as the index
df["DateTime"] = df["DateTime"].apply(format_asos_datetime)
df["DateTime"] = pd.to_datetime(df["DateTime"])
df = df.set_index("DateTime")
# convert numeric columns
df["PrecipAmt"] = pd.to_numeric(df["PrecipAmt"])
df["FrzPrcpSnsrFreq"] = pd.to_numeric(df["FrzPrcpSnsrFreq"])
df["Press1"] = pd.to_numeric(df["Press1"])
df["Press2"] = pd.to_numeric(df["Press2"])
df["T"] = pd.to_numeric(df["T"])
df["Td"] = pd.to_numeric(df["Td"])
return df
def asos_6405_to_df(file):
"""
Read an ASOS 64050 file into a Pandas DataFrame.
"""
# read the file and process it
with open(file, "r") as f:
lines = list(f) # read each line of the file into a list
# remove brackets if they exist
lines = [l.replace("[", " ") for l in lines]
lines = [l.replace("]", " ") for l in lines]
# define column names
cols = [
"StationID",
"DateTime",
"ExtinctCoef",
"DayNight",
"Wdir2Min",
"Wspd2Min",
"Wdir5Sec",
"Wspd5Sec",
]
# initialize the DataFrame
df = pd.read_csv(
io.StringIO("\n".join(lines)),
names=cols,
index_col=False,
na_values=["M"],
delim_whitespace=True,
error_bad_lines=False,
)
# format datetimes and set them as the index
df["DateTime"] = df["DateTime"].apply(format_asos_datetime)
df["DateTime"] = pd.to_datetime(df["DateTime"])
df = df.set_index("DateTime")
# convert numeric columns
df["ExtinctCoef"] = pd.to_numeric(df["ExtinctCoef"])
df["Wdir2Min"] = pd.to_numeric(df["Wdir2Min"])
df["Wspd2Min"] = pd.to_numeric(df["Wspd2Min"])
df["Wdir5Sec"] = pd.to_numeric(df["Wdir5Sec"])
df["Wspd5Sec"] = pd.to_numeric(df["Wspd5Sec"])
return df
def format_asos_datetime(x):
"""
Format datetime columns in ASOS data.
Retrieves the date and local(!!) time from the datetime column string.
"""
return x[3:15]
def asos_to_nc4_classic(file_6405, file_6406, save_dir=None):
"""
Read and combine ASOS 6405 and 6406 files into one DataFrame
and then write a netCDF4_CLASSIC file.
Method assumes data files share the same exact timeline.
save_dir is the path to the directory the file will save in.
Returns nothing and saves a .nc file.
"""
# read files into DataFrames
df1 = asos_6405_to_df(file_6405)
df2 = asos_6406_to_df(file_6406)
# ensure same station ID
if df1["StationID"][0] != df2["StationID"][0]:
raise ValueError("Station ID of files do not match!")
# combine DFs into one
# df = pd.merge(df1, df2, how='outer', on=df1.index)
df = df1.join(df2, how="outer", lsuffix="_")
# find filename from given file
ncFilename = os.path.basename(file_6405).split(".")[0][5:]
ncFilename = "".join([ncFilename, ".nc"])
if save_dir is not None:
fullpath = "/".join([save_dir, ncFilename])
else:
fullpath = ncFilename
# open a netCDF4 file
with nc4.Dataset(fullpath, "w", format="NETCDF4_CLASSIC") as f:
# create dimensions
time_dim = f.createDimension(
"time", None
) # None means unlimited length
# create variables
time = f.createVariable("epochTime", np.float64, ("time"))
time.units = "seconds"
time.long_name = "Seconds Since 00 UTC 1970 01 01"
ext = f.createVariable("extinct", np.float64, ("time"))
ext.units = "unitless"
ext.long_name = "Visibility extinction coefficient"
dayNight = f.createVariable("DayNight", "S1", ("time"))
dayNight.units = "unitless"
dayNight.long_name = "Day/Night sensor: Day=D Night=N"
wdir2min = f.createVariable("Wdir2Min", np.float64, ("time"))
wdir2min.units = "degrees"
wdir2min.long_name = "Direction of 2-minute average wind"
wspd2min = f.createVariable("Wspd2Min", np.float64, ("time"))
wspd2min.units = "knots"
wspd2min.long_name = "Speed of 2-minute average wind"
wdir5sec = f.createVariable("Wdir5Sec", np.float64, ("time"))
wdir5sec.units = "degrees"
wdir5sec.long_name = "Direction of max 5-second average wind"
wspd5sec = f.createVariable("Wspd5Sec", np.float64, ("time"))
wspd5sec.units = "knots"
wspd5sec.long_name = "Speed of max 5-second average wind"
precip = f.createVariable("Precip", "S1", ("time"))
precip.units = "unitless"
precip.long_name = "Precipitation ID: R=Rain S=Snow"
precipAmt = f.createVariable("PrecipAmt", np.float64, ("time"))
precipAmt.units = "hundredths of inches"
precipAmt.long_name = "Precipitation amount"
frzFreq = f.createVariable("FrzPrcpSnsrFreq", np.float32, ("time"))
frzFreq.units = "frequency"
frzFreq.long_name = "Frozen precipitation sensor frequency"
press1 = f.createVariable("Pressure1", np.float32, ("time"))
press1.units = "inches Hg"
press1.long_name = "Station pressure from sensor 1"
if "Press2" in df.columns:
press2 = f.createVariable("Pressure2", np.float32, ("time"))
press2.units = "inches Hg"
press2.long_name = "Station pressure from sensor 2"
if "Press3" in df.columns:
press3 = f.createVariable("Pressure3", np.float32, ("time"))
press3.units = "inches Hg"
press3.long_name = "Station pressure from sensor 3"
temp = f.createVariable("Temperature", np.int32, ("time"))
temp.units = "degrees Fahrenheit"
temp.long_name = "Average 1-minute dry bulb temperature"
td = f.createVariable("Dewpoint Temperature", np.int32, ("time"))
td.units = "degrees Fahrenheit"
td.long_name = "Average 1-minute dewpoint temperature"
# convert instrument time to seconds since the epoch 1970-01-01 0000Z
epoch = dt.datetime(1970, 1, 1, 0, 0, 0)
epoch = np.datetime64(epoch).astype("datetime64[s]")
index_time = df.index.values.astype("datetime64[s]")
sec_since_epoch = index_time - epoch
# load data into variables
time[:] = sec_since_epoch
ext[:] = df["ExtinctCoef"]
dayNight[:] = df["DayNight"]
wdir2min[:] = df["Wdir2Min"]
wspd2min[:] = df["Wspd2Min"]
wdir5sec[:] = df["Wdir5Sec"]
wspd5sec[:] = df["Wspd5Sec"]
precip[:] = df["Precip"]
precipAmt[:] = df["PrecipAmt"]
frzFreq[:] = df["FrzPrcpSnsrFreq"]
press1[:] = df["Press1"]
if "Press2" in df.columns:
press2[:] = df["Press2"]
if "Press3" in df.columns:
press3[:] = df["Press3"]
temp[:] = df["T"]
td[:] = df["Td"]
# write metadata
f.description = f"ASOS data for {df['StationID'][0]}"
f.history = f"Created {dt.datetime.today()}"