@@ -98,6 +98,27 @@ def read4(f):
9898 filename , count_healthy )
9999 return ephem
100100
101+ def rinex_creation_date (filename ):
102+ """ Read RINEX file creation time
103+
104+ Returns:
105+ datetime object
106+ """
107+ dt = None
108+ with open (filename ,'r' ) as f :
109+ while True :
110+ line = f .readline ()[:- 1 ]
111+ if not line :
112+ break
113+ if "PGM / RUN BY / DATE" not in line :
114+ continue
115+
116+ # Extract creation time
117+ timestring = '-' .join (line .split ()[2 :4 ])
118+ dt = datetime .strptime (timestring ,"%Y%m%d-%H%M%S" )
119+
120+ return dt
121+
101122def obtain_ephemeris (t , settings ):
102123 """
103124 Finds an appropriate GNSS ephemeris file for a certain time,
@@ -121,17 +142,31 @@ def obtain_ephemeris(t, settings):
121142 """
122143 print "Obtaining ephemeris file for " , t
123144
124- #TODO: If it's today and more than 1 hr old, check for an update
125-
126145 filename = t .strftime ("brdc%j0.%yn" )
127146 filedir = os .path .join (settings .cacheDir , "ephem" )
128147 filepath = os .path .join (filedir , filename )
129148 url = t .strftime (
130149 'http://qz-vision.jaxa.jp/USE/archives/ephemeris/%Y/' ) + filename
150+
151+ # If not in cache, then fetch it
131152 if not os .path .isfile (filepath ):
132153 if not os .path .exists (filedir ):
133154 os .makedirs (filedir )
134155 _ , hdrs = urllib .urlretrieve (url , filepath )
156+
157+ # Otherwise, use cache file if isn't stale
158+ else :
159+ rinex_gen_date = rinex_creation_date (filepath )
160+ if rinex_gen_date is None :
161+ # Something wrong with RINEX format, update cache
162+ _ , hdrs = urllib .urlretrieve (url , filepath )
163+ else :
164+ # If rinex generated more than an hour then fetch it again
165+ if (t - rinex_gen_date ) > timedelta (hours = 1 ):
166+ _ , hdrs = urllib .urlretrieve (url , filepath )
167+
168+ rinex_gen_date = rinex_creation_date (filepath )
169+
135170 ephem = load_rinex_nav_msg (filepath , t , settings )
136171 if len (ephem ) < 22 :
137172 raise ValueError (
0 commit comments