def calculate_otl_coeff(self,x=None,y=None,z=None): if not self.x and (x is None or y is None or z is None): raise pyOTLException('Cartesian coordinates not initialized and not provided in calculate_otl_coef') if not self.x: self.x = x if not self.y: self.y = y if not self.z: self.z = z out, err = pyRunWithRetry.RunCommand(self.grdtab + ' ' + str(self.x) + ' ' + str(self.y) + ' ' + str(self.z) + ' ' + self.StationCode, 5, self.rootdir).run_shell() if err: raise pyOTLException('grdtab returned an error: ' + err) fatal_path = os.path.join(self.rootdir, 'GAMIT.fatal') harpos_path = os.path.join(self.rootdir, 'harpos.' + self.StationCode) if os.path.isfile(fatal_path) and not os.path.isfile(harpos_path): raise pyOTLException('grdtab returned an error:\n' + file_read_all(fatal_path)) # open otl file return file_read_all(harpos_path)
def __exec_ppp__(self, raise_error=True): try: # DDG: handle the error found in PPP (happens every now and then) # Fortran runtime error: End of file for i in range(2): out, err = pyRunWithRetry.RunCommand(self.ppp, 60, self.rootdir, 'input.inp').run_shell() if '*END - NORMAL COMPLETION' not in out: if 'Fortran runtime error: End of file' in err and i == 0: # error detected, try again! continue msg = 'PPP ended abnormally for ' + self.rinex.rinex_path + ':\n' + err + '\n' + out if raise_error: raise pyRunPPPException(msg) else: return False, msg else: path = os.path.join(self.rootdir, self.rinex.rinex[:-3]) self.out = file_readlines(path + 'sum') self.pos = file_readlines(path + 'pos') break except pyRunWithRetry.RunCommandWithRetryExeception as e: msg = str(e) if raise_error: raise pyRunPPPException(e) else: return False, msg except IOError as e: raise pyRunPPPException(e) return True, ''
def __init__(self, archive, date, filename, copyto): if date.gpsWeek < 0 or date > pyDate.Date(datetime=datetime.now()): # do not allow negative weeks or future orbit downloads! raise pyProductsExceptionUnreasonableDate( 'Orbit requested for an unreasonable date: week ' + str(date.gpsWeek) + ' day ' + str(date.gpsWeekDay) + ' (' + date.yyyyddd() + ')') archive = archive.replace('$year', str(date.year)) archive = archive.replace('$doy', str(date.doy).zfill(3)) archive = archive.replace('$gpsweek', str(date.gpsWeek).zfill(4)) archive = archive.replace('$gpswkday', str(date.gpsWeekDay)) self.archive = archive self.path = None self.filename = filename # try both zipped and unzipped n files archive_file_path = os.path.join(archive, self.filename) if os.path.isfile(archive_file_path): try: copyfile(archive_file_path, os.path.join(copyto, self.filename)) self.file_path = os.path.join(copyto, self.filename) except Exception: raise else: ext = None if os.path.isfile(archive_file_path + '.Z'): ext = '.Z' elif os.path.isfile(archive_file_path + '.gz'): ext = '.gz' elif os.path.isfile(archive_file_path + '.zip'): ext = '.zip' if ext is not None: copyfile(archive_file_path + ext, os.path.join(copyto, self.filename + ext)) self.file_path = os.path.join(copyto, self.filename) cmd = pyRunWithRetry.RunCommand( 'gunzip -f ' + self.file_path + ext, 15) try: cmd.run_shell() except Exception: raise else: raise pyProductsException( 'Could not find the archive file for ' + self.filename)
def __init__(self, archive, date, filename, copyto): if date.gpsWeek < 0 or date > pyDate.Date(datetime=datetime.now()): # do not allow negative weeks or future orbit downloads! raise pyProductsExceptionUnreasonableDate('Orbit requested for an unreasonable date: ' 'week ' + str(date.gpsWeek) + \ ' day ' + str(date.gpsWeekDay) + \ ' (' + date.yyyyddd() + ')') archive = archive.replace('$year', str(date.year)) \ .replace('$doy', str(date.doy).zfill(3)) \ .replace('$gpsweek', str(date.gpsWeek).zfill(4)) \ .replace('$gpswkday', str(date.gpsWeekDay)) self.archive = archive self.path = None self.filename = filename archive_file_path = os.path.join(archive, self.filename) copy_path = os.path.join(copyto, self.filename) self.file_path = copy_path # try both zipped and unzipped n files if os.path.isfile(archive_file_path): copyfile(archive_file_path, copy_path) else: for ext in ('.Z', '.gz', '.zip'): if os.path.isfile(archive_file_path + ext): copyfile(archive_file_path + ext, copy_path + ext) pyRunWithRetry.RunCommand('gunzip -f ' + copy_path + ext, 15).run_shell() break else: raise pyProductsException( 'Could not find the archive file for ' + self.filename)
def calculate_otl_coeff(self, x=None, y=None, z=None): if not self.x and (x is None or y is None or z is None): raise pyOTLException( 'Cartesian coordinates not initialized and not provided in calculate_otl_coef' ) else: if not self.x: self.x = x if not self.y: self.y = y if not self.z: self.z = z cmd = pyRunWithRetry.RunCommand( self.grdtab + ' ' + str(self.x) + ' ' + str(self.y) + ' ' + str(self.z) + ' ' + self.StationCode, 5, self.rootdir) out, err = cmd.run_shell() if err or os.path.isfile(os.path.join( self.rootdir, 'GAMIT.fatal')) and not os.path.isfile( os.path.join(self.rootdir, 'harpos.' + self.StationCode)): if err: raise pyOTLException('grdtab returned an error: ' + err) else: with open(os.path.join(self.rootdir, 'GAMIT.fatal'), 'r') as fileio: raise pyOTLException('grdtab returned an error:\n' + fileio.read()) else: # open otl file with open( os.path.join(self.rootdir, 'harpos.' + self.StationCode), 'r') as fileio: return fileio.read()
def main(): parser = argparse.ArgumentParser( description='Archive operations Main Program') parser.add_argument( '-date', '--date_range', nargs='+', action=required_length(1, 2), metavar='date_start|date_end', help= "Date range to check given as [date_start] or [date_start] and [date_end]. " "Allowed formats are yyyy.doy or yyyy/mm/dd..") parser.add_argument( '-win', '--window', nargs=1, metavar='days', type=int, help= "Download data from a given time window determined by today - {days}.") try: args = parser.parse_args() Config = pyOptions.ReadOptions('gnss_data.cfg') dates = () now = datetime.now() try: if args.window: # today - ndays d = pyDate.Date(year=now.year, month=now.month, day=now.day) dates = (d - int(args.window[0]), d) else: dates = process_date(args.date_range) except ValueError as e: parser.error(str(e)) if dates[0] < pyDate.Date(gpsWeek=650, gpsWeekDay=0): dates = (pyDate.Date(gpsWeek=650, gpsWeekDay=0), pyDate.Date(year=now.year, month=now.month, day=now.day)) # go through the dates drange = np.arange(dates[0].mjd, dates[1].mjd, 1) pbar = tqdm(desc='%-30s' % ' >> Synchronizing orbit files', total=len(drange), ncols=160) # connect to ftp ftp = ftplib.FTP_TLS(FTP_HOST, FTP_USER, FTP_PASS) ftp.set_pasv(True) ftp.prot_p() def downloadIfMissing(ftp_list, ftp_filename, local_filename, local_dir, desc): mark_path = os.path.join(local_dir, local_filename) if not os.path.isfile(mark_path) and ftp_filename in ftp_list: tqdm.write('%-31s: %s' % (' -- trying to download ' + desc, filename)) down_path = os.path.join(local_dir, ftp_filename) with open(down_path, 'wb') as f: ftp.retrbinary("RETR " + ftp_filename, f.write) return True def get_archive_path(archive, date): return archive.replace('$year', str(date.year)) \ .replace('$doy', str(date.doy).zfill(3)) \ .replace('$gpsweek', str(date.gpsWeek).zfill(4)) \ .replace('$gpswkday', str(date.gpsWeekDay)) for date in (pyDate.Date(mjd=mdj) for mdj in drange): sp3_archive = get_archive_path(Config.sp3_path, date) if not os.path.exists(sp3_archive): os.makedirs(sp3_archive) for repro in ('', '/repro2'): # try both in the repro and / folders folder = "/pub/gps/products/" + date.wwww() + repro try: tqdm.write(' -- Changing folder to ' + folder) ftp.cwd(folder) ftp_list = set(ftp.nlst()) except: # folder not present, skip continue for orbit in Config.sp3types + Config.sp3altrn: for ext in ('.sp3', '.clk', '.erp', '7.erp'): try: filename = orbit + date.wwwwd() + ext + '.Z' downloadIfMissing( ftp_list, filename, filename, sp3_archive, 'EOP' if ext == '7.erp' else ext.upper()) except: pass ###### now the brdc files ######### try: folder = "/pub/gps/data/daily/%s/%s/%sn" % ( date.yyyy(), date.ddd(), date.yyyy()[2:]) tqdm.write(' -- Changing folder to ' + folder) ftp.cwd(folder) ftp_list = set(ftp.nlst()) except: continue brdc_archive = get_archive_path(Config.brdc_path, date) if not os.path.exists(brdc_archive): os.makedirs(brdc_archive) try: filename = 'brdc%s0.%sn' % (str( date.doy).zfill(3), str(date.year)[2:4]) ftp_filename = filename + '.Z' if downloadIfMissing(ftp_list, ftp_filename, filename, brdc_archive, 'BRDC'): # decompress file pyRunWithRetry.RunCommand('gunzip -f ' + ftp_filename, 15).run_shell() except: continue pbar.set_postfix(gpsWeek='%i %i' % (date.gpsWeek, date.gpsWeekDay)) pbar.update() pbar.close() ftp.quit() except argparse.ArgumentTypeError as e: parser.error(str(e))
def main(): parser = argparse.ArgumentParser(description='Archive operations Main Program') parser.add_argument('-date', '--date_range', nargs='+', action=required_length(1, 2), metavar='date_start|date_end', help="Date range to check given as [date_start] or [date_start] and [date_end]. " "Allowed formats are yyyy.doy or yyyy/mm/dd..") parser.add_argument('-win', '--window', nargs=1, metavar='days', type=int, help="Download data from a given time window determined by today - {days}.") try: args = parser.parse_args() Config = pyOptions.ReadOptions('gnss_data.cfg') dates = [] try: if args.window: # today - ndays d = pyDate.Date(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day) dates = [d-int(args.window[0]), d] else: dates = process_date(args.date_range) except ValueError as e: parser.error(str(e)) if dates[0] < pyDate.Date(gpsWeek=650, gpsWeekDay=0): dates = [pyDate.Date(gpsWeek=650, gpsWeekDay=0), pyDate.Date(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)] # go through the dates drange = np.arange(dates[0].mjd, dates[1].mjd, 1) pbar = tqdm(desc='%-30s' % ' >> Synchronizing orbit files', total=len(drange), ncols=160) # connect to ftp ftp = ftplib.FTP('198.118.242.40', 'Anonymous', '*****@*****.**') for date in [pyDate.Date(mjd=mdj) for mdj in drange]: sp3_archive = get_archive_path(Config.sp3_path, date) if not os.path.exists(sp3_archive): os.makedirs(sp3_archive) for repro in ['', '/repro2']: # try both in the repro and / folders folder = "/pub/gps/products/" + date.wwww() + repro try: ftp.cwd(folder) except Exception: # folder not present, skip continue tqdm.write(' -- Changing folder to ' + folder) ftp_list = ftp.nlst() for orbit in Config.sp3types + Config.sp3altrn: for ext in ['.sp3.Z', '.clk.Z', '.erp.Z']: filename = orbit + date.wwwwd() + ext if not os.path.isfile(os.path.join(sp3_archive, filename)) and filename in ftp_list: tqdm.write('%-31s: %s' % (' -- trying to download ' + ext.replace('.Z', '').upper(), filename)) try: ftp.retrbinary("RETR " + filename, open(os.path.join(sp3_archive, filename), 'wb').write) except Exception: continue # now the eop file filename = orbit + date.wwww() + '7.erp.Z' if not os.path.isfile(os.path.join(sp3_archive, filename)) and filename in ftp_list: tqdm.write('%-31s: %s' % (' -- trying to download EOP', filename)) try: ftp.retrbinary("RETR " + filename, open(os.path.join(sp3_archive, filename), 'wb').write) except Exception: continue ###### now the brdc files ######### try: folder = "/pub/gps/data/daily/%s/%s/%sn" % (date.yyyy(), date.ddd(), date.yyyy()[2:]) tqdm.write(' -- Changing folder to ' + folder) ftp.cwd(folder) ftp_list = ftp.nlst() except Exception: continue brdc_archive = get_archive_path(Config.brdc_path, date) if not os.path.exists(brdc_archive): os.makedirs(brdc_archive) filename = 'brdc' + str(date.doy).zfill(3) + '0.' + str(date.year)[2:4] + 'n' if not os.path.isfile(os.path.join(brdc_archive, filename)) and filename + '.Z' in ftp_list: tqdm.write('%-31s: %s' % (' -- trying to download BRDC', filename)) try: ftp.retrbinary("RETR " + filename + '.Z', open(os.path.join(brdc_archive, filename + '.Z'), 'wb').write) # decompress file cmd = pyRunWithRetry.RunCommand('gunzip -f ' + os.path.join(brdc_archive, filename + '.Z'), 15) cmd.run_shell() except Exception: continue pbar.set_postfix(gpsWeek='%i %i' % (date.gpsWeek, date.gpsWeekDay)) pbar.update() pbar.close() ftp.quit() except argparse.ArgumentTypeError as e: parser.error(str(e))