Navigation Menu

Skip to content

Commit

Permalink
refactor igs_ftp to use the common ftp_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
aewallin committed Dec 13, 2015
1 parent 547938e commit 86b8d4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bipm_ftp.py
Expand Up @@ -10,7 +10,7 @@
This file is part of ppp-tools, https://github.com/aewallin/ppp-tools
GPL license.
"""
import ftplib
# import ftplib
import datetime
import pytz
import os
Expand Down
6 changes: 3 additions & 3 deletions glab_ppp.py
Expand Up @@ -49,8 +49,8 @@ def glab_run(station, dt, rapid=True, prefixdir=""):
doy = dt.timetuple().tm_yday
rinex = station.get_rinex( dt )

(server, igs_directory, igs_files, localdir) = igs_ftp.CODE_rapid_files(dt, prefixdir=prefixdir)
files = igs_ftp.CODE_download(server, igs_directory, igs_files, localdir)
(server, username, password, igs_directory, igs_files, localdir) = igs_ftp.CODE_rapid_files(dt, prefixdir=prefixdir)
files = igs_ftp.CODE_download(server, username, password, igs_directory, igs_files, localdir)
(clk, eph, erp) = (files[0], files[1], files[2])

print "ppp_run start: ", dt_start
Expand Down Expand Up @@ -139,7 +139,7 @@ def glab_run(station, dt, rapid=True, prefixdir=""):

# example processing:
station = UTCStation.usno
dt = datetime.datetime.now()-datetime.timedelta(days=4)
dt = datetime.datetime.now()-datetime.timedelta(days=5)
current_dir = os.getcwd()

# run gLAB PPP for given station, day
Expand Down
85 changes: 35 additions & 50 deletions igs_ftp.py
Expand Up @@ -2,114 +2,93 @@
Library for downloading GPS products (orbits & clocks) from
an IGS datacenter.
This file is part of ppp-tools, https://github.com/aewallin/ppp-tools
GPL license.
Anders Wallin, 2013-2015
"""
import ftplib
import datetime
import sys
import os

import ftp_tools
import gpstime

# Examples of Rapid files:
# ftp://ftp.unibe.ch/aiub/CODE/COD17840.EPH_R ephemeris aka orbits
# ftp://ftp.unibe.ch/aiub/CODE/COD17840.ERP_R erp, earth rotation parameters
# ftp://ftp.unibe.ch/aiub/CODE/COD17840.CLK_R clk, clocks

def check_dir(target_dir):
# check that directory exsits, create if not
if not os.path.isdir(target_dir):
print "creating target directory ", target_dir
os.mkdir(target_dir)

# retrieve rapid CODE products
def CODE_rapid_files(dt, prefixdir=""):
"""
retrieve rapid CODE products for the datetime dt
Examples of Rapid files:
ftp://ftp.unibe.ch/aiub/CODE/COD17840.EPH_R ephemeris aka orbits
ftp://ftp.unibe.ch/aiub/CODE/COD17840.ERP_R erp, earth rotation parameters
ftp://ftp.unibe.ch/aiub/CODE/COD17840.CLK_R clk, clocks
"""
server = "ftp.unibe.ch"
directory = "aiub/CODE/"
remotedir = "aiub/CODE/"
week = gpstime.gpsWeek( dt.year, dt.month, dt.day )
dow = gpstime.dayOfWeek( dt.year, dt.month, dt.day )
clk = "COD%s%s.CLK_R" % ( week, dow )
sp3 = "COD%s%s.EPH_R" % ( week, dow )
erp = "COD%s%s.ERP_R" % ( week, dow )
print "CODE rapid products for ", dt.year ,"-", dt.month, "-",dt.day
print "CODE rapid products for %d-%02d-%0d" %( dt.year , dt.month, dt.day )
print "CLK = ", clk
print "SP3 = ", sp3
print "ERP = ", erp

check_dir(prefixdir + "/products/")
ftp_tools.check_dir(prefixdir + "/products/")
localdir = prefixdir + "/products/CODE_rapid/"
print "local dir = ", localdir

#return CODE_download(server, directory, [clk, sp3, erp], localdir)
return (server, directory, [clk, sp3, erp], localdir)
return (server, "", "", remotedir, [clk, sp3, erp], localdir)

# retrieve final CODE products
def CODE_final_files(dt, prefixdir=""):
server = "ftp.unibe.ch"
directory = "aiub/CODE/%s/" % (dt.year)
remotedir = "aiub/CODE/%s/" % (dt.year)
week = gpstime.gpsWeek( dt.year, dt.month, dt.day )
dow = gpstime.dayOfWeek( dt.year, dt.month, dt.day )
clk = "COD%s%s.CLK.Z" % ( week, dow ) # clock
sp3 = "COD%s%s.EPH.Z" % ( week, dow ) # orbit
erp = "COD%s%s.ERP.Z" % ( week, dow ) # earth
print "CODE final products for ", dt.year ,"-", dt.month, "-",dt.day
print "CODE final products for %d-%02d-%0d" %( dt.year , dt.month, dt.day )
print "CLK = ", clk
print "SP3 = ", sp3
print "ERP = ", erp

check_dir(prefixdir + "/products/")
ftp_tools.check_dir(prefixdir + "/products/")
localdir = prefixdir + "/products/CODE_final/"
print "local dir = ", localdir
return (server, directory, [clk, sp3, erp], localdir)
return (server, "", "", remotedir, [clk, sp3, erp], localdir)

def CODE_download( server, directory, files, localdir):
def CODE_download( server, username, password, remotedir, files, localdir):
print "CODE_download start ", datetime.datetime.now()
check_dir(localdir)
ftp_tools.check_dir(localdir)

for f in files:
local_file = localdir+f
if not os.path.exists( local_file ):
# Connection information
server = server
username = ''
password = ''
print 'Remote ', f
print 'Local ', local_file
sys.stdout.flush()

# Establish the connection
ftp = ftplib.FTP(server)
ftp.login(username, password)

# Change to the proper directory
ftp.cwd(directory)

# Loop through matching files and download each one individually
fhandle = open(local_file, 'wb')
ftp.retrbinary('RETR ' + f, fhandle.write)
fhandle.close()
ftp.close()
else: # don't download if file already exists on disk
print f," already exists. not downloading."
ftp_tools.ftp_download( server, username, password, remotedir, f, localdir)
print "CODE_download Done ", datetime.datetime.now()
sys.stdout.flush()
output=[]
for f in files:
output.append( localdir+f)
return output # returns list of files: [CLK, EPH, ERP]

if __name__ == "__main__":
def example_igs_ftp():
current_dir = os.getcwd()
# example of how to use the functions:
dt_rapid = datetime.datetime.now() - datetime.timedelta(days=3) # rapid products avaible with 2(?) days latency
dt_final = datetime.datetime.now() - datetime.timedelta(days=14) # final product worst case latency 14 days ?
(server, directory, files, localdir) = CODE_final_files(dt_final, prefixdir=current_dir)
files = CODE_download(server, directory, files, localdir)
(server, username, password, directory, files, localdir) = CODE_final_files(dt_final, prefixdir=current_dir)
files = CODE_download(server, username, password, directory, files, localdir)
print files # [CLK, EPH, ERP] note that final products are zipped
(server, directory, files, localdir) = CODE_rapid_files(dt_rapid, prefixdir=current_dir)
files = CODE_download(server, directory, files, localdir)
(server, username, password, directory, files, localdir) = CODE_rapid_files(dt_rapid, prefixdir=current_dir)
files = CODE_download(server, username, password, directory, files, localdir)
print files # [CLK, EPH, ERP] rapid products are unzipped

"""
sample output:
Expand All @@ -125,3 +104,9 @@ def CODE_download( server, directory, files, localdir):
local dir = /home/anders/ppp-tools/CODE_rapid/
"""

if __name__ == "__main__":
example_igs_ftp()
pass


0 comments on commit 86b8d4a

Please sign in to comment.