-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_noaa_winwx_plots.py
executable file
·177 lines (155 loc) · 6.35 KB
/
get_noaa_winwx_plots.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
#!/usr/bin/python3
import os
import sys
import time
from datetime import timedelta
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import shutil
from ftplib import FTP
def listFD(url, ext=''):
page = requests.get(url).text
#print page
soup = BeautifulSoup(page, 'html.parser')
return [url + '/' + node.get('href') for node in soup.find_all('a') if node.get('href').endswith(ext)]
# User inputs
debug = 1
test = False
secsPerDay = 86400
pastSecs = secsPerDay # check last days
winwxUrl = 'https://www.wpc.ncep.noaa.gov/archives/winwx'
targetDirBase = '/home/disk/bob/impacts/sfc/winwx'
products = {'lowtrack':'low_tracks_and_clusters',
'day1_psnow_gt_04':'day1_psnow_gt_4in',
'day2_psnow_gt_04':'day2_psnow_gt_4in',
'day3_psnow_gt_04':'day3_psnow_gt_4in'}
catalogPrefix = 'surface.NOAA_WPC'
tempDir = '/tmp'
# Field Catalog inputs
if test:
ftpCatalogServer = 'ftp.atmos.washington.edu'
ftpCatalogUser = 'anonymous'
ftpCatalogPassword = '[email protected]'
catalogDestDir = 'brodzik/incoming/impacts'
else:
ftpCatalogServer = 'catalog.eol.ucar.edu'
ftpCatalogUser = 'anonymous'
catalogDestDir = '/pub/incoming/catalog/impacts'
# Open ftp connection to NCAR sever
if test:
catalogFTP = FTP(ftpCatalogServer,ftpCatalogUser,ftpCatalogPassword)
catalogFTP.cwd(catalogDestDir)
else:
catalogFTP = FTP(ftpCatalogServer,ftpCatalogUser)
catalogFTP.cwd(catalogDestDir)
# get current time
nowTime = time.gmtime()
now = datetime(nowTime.tm_year, nowTime.tm_mon, nowTime.tm_mday,
nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec)
nowDateStr = now.strftime("%Y%m%d")
nowTimeStr = now.strftime("%H%M%S")
if debug:
print("nowDateTimeStr = ", nowDateStr+nowTimeStr)
# compute start time
pastDelta = timedelta(0, pastSecs)
startTime = now - pastDelta
startDateStr = startTime.strftime("%Y%m%d")
startTimeStr = startTime.strftime("%H%M%S")
if debug:
print("startDateTimeStr = ", startDateStr+startTimeStr)
# set up list of dates to be checked
dateStrList = []
dateStrList.append(nowDateStr)
if nowDateStr != startDateStr:
dateStrList.append(startDateStr)
if debug:
print("dateStrList = ", dateStrList)
nDates = len(dateStrList)
for t in range(0,nDates):
currentDate = dateStrList[t]
for prod in products:
if debug:
print("Processing", currentDate, "run for", prod, "data")
# get list of files on server for this run and this product
# only interested in forecasts up to and including 'lastForecastHour'
urlFileList = []
url = winwxUrl+'/'+currentDate+'/'
ext = 'gif'
for file in listFD(url, ext):
tmp = os.path.basename(file)
if prod in tmp:
if prod == 'lowtrack':
(base,ext) = os.path.splitext(tmp)
parts = base.split('_')
if len(parts) == 2:
urlFileList.append(tmp)
else:
urlFileList.append(tmp)
if debug:
print("urlFileList = ", urlFileList)
if len(urlFileList) == 0:
if debug:
print("WARNING: ignoring run and product - no data on server")
print(" for model run time: ", currentModelRun)
print(" for product : ", products[i])
else:
# make target directory, if necessary, and cd to it
#targetDir = targetDirBase+'/'+dateHourStrList[i]+'/'+products[i]
targetDir = targetDirBase
if not os.path.exists(targetDir):
os.makedirs(targetDir)
os.chdir(targetDir)
# get local file list - i.e. those which have already been downloaded
localFileList = os.listdir('.')
#if debug:
# print(" localFileList: ", localFileList)
# loop through the url file list, downloading those that have
# not yet been downloaded
if debug:
print("Starting to loop through url file list")
for idx,urlFileName in enumerate(urlFileList,0):
if debug:
print(" idx =", idx,"and urlFileName =", urlFileName)
if urlFileName not in localFileList:
if debug:
print(" ",urlFileName,"not in localFileList -- get file")
try:
command = 'wget '+winwxUrl+'/'+currentDate+'/'+urlFileName
os.system(command)
except Exception as e:
print(" wget failed, exception: ", e)
continue
# rename file and move to web server
if prod == 'lowtrack':
(dateHr,rest) = urlFileName.split('_')
fileDate = dateHr[0:8]
fileHour = dateHr[8:10]
if fileHour == '15':
newFileDateTime = fileDate+'1200'
elif fileHour == '03':
newFileDateTime = fileDate+'0000'
else:
(base,ext) = os.path.splitext(urlFileName)
(junk1,junk2,junk3,junk4,dateHr) = base.split('_')
fileDate = dateHr[0:8]
fileHour = dateHr[8:10]
newFileDateTime = fileDate+fileHour+'00'
# create full file name
catalogName = catalogPrefix+'.'+newFileDateTime+'.'+products[prod]+'.gif'
if debug:
print(" catalogName = ", catalogName)
# copy file to tempDir and rename
shutil.copy(targetDir+'/'+urlFileName,tempDir+'/'+catalogName)
# ftp file to catalog location
os.chdir(tempDir)
ftpFile = open(os.path.join(tempDir,catalogName),'rb')
catalogFTP.storbinary('STOR '+catalogName,ftpFile)
ftpFile.close()
os.chdir(targetDir)
if debug:
print('ftpd',catalogName,'to NCAR FC')
# remove file from tempDir
os.remove(os.path.join(tempDir,catalogName))
# Close ftp connection
catalogFTP.quit()