My download routine for downloading weather forcasts for the states NY and PA.
It is quick and dirty. It need to be transformed to a more function/procedure like structure
to get rid of the duplicated code.
Russ how are the thwo letter codes (PA, NY) officially called ?
Is there a special name for that ?
In the moment the download routine is ready. The next step is the conversation to telex format,
which is not finished in the moment.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#*********************************************************************
# Connect USA FTP Server and download Forecast und Warnlagen
# Autor : Frank Ulbrich
# Date : 13.05.2018
#*********************************************************************
# Links
#=======
# http://www.pythonforbeginners.com/code- ... in-python/
#
# Server : ftp://tgftp.nws.noaa.gov/data/forecasts/zone/
# User : anonymous
# PW: anonymus
# Interpreter : python 2.7.13
#
# Datumpruefung
# Download auf RAM DISK /dev/shm
#*********************************************************************
# Versionsnummer ausgeben
version = "0.00.2"
print("****************************************")
print("2tlx_ftp_down_noaa_forecast_v.py Version : " + version )
print("****************************************")
#*********************************************************************
# imported libraries
import fnmatch # match names
import ftplib # ftp connect
import datetime
#*********************************************************************
# FTP Server Parameter
#*********************************************************************
#drive = "./"
drive = "/dev/shm/" # RAMDISK
filepath = drive + "filelist/" # Linux
ftpserver = 'tgftp.nws.noaa.gov' # ftp server unites states weather 2018
ftpuser = 'anonymous' # Persoenlicher User
ftppw = 'frank.ulbrich' # Persoenliches Password
ftppath0 = '/data/forecasts/zone'
ftppath1 = '/data/forecasts/zone/pa' # Warnmeldungen PA - paz***.txt FPUS51
ftppath2 = '/data/forecasts/zone/ny' # Warnmeldungen NY - nyz***.txt
filelist1 = filepath + 'noaa_warnlagen_pa.txt'
filelist2 = filepath + 'noaa_warnlagen_ny.txt'
now = datetime.datetime.now() # Der heutige Tag
tag = now.strftime("%d")
print "Das heutige Datum (Tag) lautet : " + tag + "\n"
print ("* FTP - Server Connect ")
ftp = ftplib.FTP(ftpserver, ftpuser, ftppw)
#ftp = ftplib.FTP('ftp.sunet.se', 'anonymous', 'anonymous@sunet.se')
welcome = ftp.getwelcome()
print (welcome)
ftp.dir()
ftp.cwd(ftppath0) # break for downloading only one filechanging to forecasts
ftp.dir()
print ("* FTP - Server nun connected ")
#*********************************************************************
# Read filelist Forecast pa paz***.txt
#*********************************************************************
print ("* Read forecast pa filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath1) # changing to directory
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True) # Absteigende Sortierung der Namen
print (filelist)
#*********************************************************************
# Create textfile with filenames paz***.txt
#*********************************************************************
print ("* Create FORECAST paz textfile with filnames ")
# https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist1,'w')
for item in filelist:
#if fnmatch.fnmatch(item, "paz" + tag + "*"): # filter for files with specific characters
if fnmatch.fnmatch(item, "paz*"): # filter for files with specific characters
print item
myfile.write(item+"\n")
#myfile.writelines("%s\n" % item)
myfile.close()
#*********************************************************************
# Read filelist Forecast ny nyz***.txt
#*********************************************************************
print ("* Read forecast ny filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath2) # changing to directory
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True) # Absteigende Sortierung der Namen
print (filelist)
#*********************************************************************
# Create textfile with filenames nyz***.txt
#*********************************************************************
print ("* Create FORECAST nyz textfile with filnames ")
# https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist2,'w')
for item in filelist:
#if fnmatch.fnmatch(item, "*" + "VHDL17_DWOG_" + tag + "*"): # filter for files with specific characters
if fnmatch.fnmatch(item, "nyz*"):
print item
myfile.write(item+"\n")
#myfile.writelines("%s\n" % item)
myfile.close()
######################################################################################
# DOWNLOADS
######################################################################################
#http://stackoverflow.com/questions/2362 ... nges-using
#http://ftputil.sschwarzer.net/trac/wiki/Documentation
#https://docs.python.org/3/library/fnmatch.html
#*********************************************************************
# Download textfile with filenames from saved filelist.file PA FPUS51
#*********************************************************************
print ('* Oeffne Listen Datei PA : %s' %(filelist1))
myfile = open(filelist1,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entfernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath1)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#******************************************************************************
# Speziell, da EOL geloest wird und ein CR fehlt
#foofile = open( drive + 'filedownload/forecast/' + takefile, 'w')
foofile = open( drive + 'filedownload/forecast/' + 'FPUS51PA.txt', 'w')
def customWriter(line):
foofile.write(line + '\r') # CR an das Zeilenden anhaengen.
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\forecast\\' + takefile + ".txt", 'wb').write)
#ftp.retrbinary('RETR '+takefile, open('filedownload/forecast/' + takefile + ".txt", 'wb').write)
#ftp.retrlines('RETR '+takefile,customWriter)
ftp.retrlines('RETR '+takefile,customWriter) # FPUS51PA
foofile.close()
break # break for downloading only one file
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file NY FPUS51
#*********************************************************************
print ('* Oeffne Listen Datei NY : %s' %(filelist2))
myfile = open(filelist2,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entfernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath2)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#******************************************************************************
# Speziell, da EOL geloest wird und ein CR fehlt
#foofile = open( drive + 'filedownload/forecast/' + takefile, 'w')
foofile = open( drive + 'filedownload/forecast/' + 'FPUS51NY.txt', 'w')
def customWriter(line):
foofile.write(line + '\r') # CR an das Zeilenden anhaengen.
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\forecast\\' + takefile + ".txt", 'wb').write)
#ftp.retrbinary('RETR '+takefile, open('filedownload/forecast/' + takefile + ".txt", 'wb').write)
#ftp.retrlines('RETR '+takefile,customWriter)
ftp.retrlines('RETR '+takefile,customWriter) # FPUS51PA
foofile.close()
break # break for downloading only one file
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
print ('* Closing FTP connection')
ftp.quit() # implicits ftp.close(), schliessen, falls noch nicht geschlossen.
quit()
#Ende des Program
Munter bleiben
Frank
This is a result :
Expires:201805132000;;887886
FPUS51 KCTP 131117
ZFPCTP
Zone Forecast Product for Central Pennsylvania
National Weather Service State College PA
715 AM EDT Sun May 13 2018
PAZ004-132000-
Warren-
Including the city of Warren
715 AM EDT Sun May 13 2018
.TODAY...Cloudy. Patchy fog this morning. Showers, mainly this
morning. Highs in the lower 60s. South winds around 5 mph. Chance
of rain 90 percent.
.TONIGHT...Cloudy with a 20 percent chance of showers. Lows in
the lower 50s. Southwest winds around 5 mph.
.MONDAY...Mostly cloudy with a slight chance of showers. A chance
of thunderstorms in the afternoon. Warmer with highs in the lower
70s. Southwest winds 5 to 10 mph. Chance of rain 30 percent.
.MONDAY NIGHT...Mostly cloudy with a chance of showers and
thunderstorms. Lows in the mid 50s. South winds around 5 mph.
Chance of rain 50 percent.
.TUESDAY...Showers likely. Thunderstorms in the afternoon. Highs
in the mid 70s. Southwest winds around 5 mph, becoming west in
the afternoon. Chance of rain 80 percent.
.TUESDAY NIGHT...Mostly cloudy with a chance of showers and
thunderstorms. Lows in the upper 50s. Chance of rain 40 percent.
.WEDNESDAY...Mostly cloudy with a chance of showers and
thunderstorms. Highs in the mid 70s. Chance of rain 50 percent.
.WEDNESDAY NIGHT...Mostly cloudy. A chance of showers and
thunderstorms in the evening. Lows in the upper 50s. Chance of
rain 50 percent.
.THURSDAY...Mostly cloudy in the morning, then becoming partly
sunny. A 40 percent chance of showers. Highs in the lower 70s.
.THURSDAY NIGHT...Mostly cloudy with a 50 percent chance of
showers. Lows in the mid 50s.
.FRIDAY...Showers likely. Highs in the upper 60s. Chance of rain
60 percent.
.FRIDAY NIGHT...Showers likely. Lows in the mid 50s. Chance of
rain 60 percent.
.SATURDAY...Mostly cloudy with a chance of thunderstorms. Showers
likely, mainly in the morning. Highs in the lower 70s. Chance of
rain 60 percent.
$$