async def st_download(config, filename, norad, epoch_from, epoch_to): # Login to space-track and download data try: st = AsyncSpaceTrackClient( config.user, config.password ) except: ttprint("Error connecting to Space-Track. Exiting.") exit() else: # Read chunks of 100kB, see: # https://spacetrack.readthedocs.io/en/latest/usage.html#streaming-downloads ttprint("Downloading OMM from space-track.org") async with st: data = await st.gp_history( iter_content=True, norad_cat_id=norad, epoch=op.inclusive_range(epoch_from, epoch_to), #epoch='2017-07-14--2020-12-31', format='xml', ) with gzip.open(filename, 'wt') as fp: nlines=1 async for line in data: fp.write(line) if VERBOSE: ttprint(" Write chunk " + str(nlines) + " ...") nlines += 1 ttprint("Downloaded file: " + filename)
def get_spacetrack_tle(sat_id, start_date, end_date, username, password, latest=False): # Реализуем экземпляр класса SpaceTrackClient, инициализируя его именем пользователя и паролем st = SpaceTrackClient(identity=username, password=password) # Выполнение запроса для диапазона дат: if not latest: # Определяем диапазон дат через оператор библиотеки daterange = op.inclusive_range(start_date, end_date) # Собственно выполняем запрос через st.tle data = st.tle(norad_cat_id=sat_id, orderby='epoch desc', limit=1, format='tle', epoch=daterange) # Выполнение запроса для актуального состояния else: # Выполняем запрос через st.tle_latest data = st.tle_latest(norad_cat_id=sat_id, orderby='epoch desc', limit=1, format='tle') # Если данные недоступны if not data: return 0, 0 # Иначе возвращаем две строки tle_1 = data[0:69] tle_2 = data[70:139] return tle_1, tle_2
def get_spacetrack_tle(sat_id, start_date, end_date, username, password, latest=False): st = SpaceTrackClient(identity=username, password=password) if not latest: daterange = op.inclusive_range(start_date, end_date) data = st.tle(norad_cat_id=sat_id, orderby='epoch desc', limit=1, format='tle', epoch=daterange) else: data = st.tle_latest(norad_cat_id=sat_id, orderby='epoch desc', limit=1, format='tle') if not data: return 0, 0 tle_1 = data[0:69] tle_2 = data[70:139] return tle_1, tle_2
def test_inclusive_range(): assert op.inclusive_range('a', 'b') == 'a--b'
def test_inclusive_range(): assert op.inclusive_range('a', 'b') == 'a--b'
def grab_gp_history_data(socrates_files_path, tle_file_path, spacetrack_key_file='./spacetrack_pwd.key'): ''' Determines which TLE data is missing and grabs it from Space Track Parameters: ----------- socrates_files_path : str Relative file path of socrates files tle_file_path : str Relative file path of TLE data spacetrack_key_file : str Relative file path of login credentials for spacetrack. File format: email,password Returns ------- None ''' print('Building socrates dataframe...') soc_df, tle_df = socrates.get_all_socrates_and_tle_data( socrates_files_path, tle_file_path) print('Complete') # Get space track login data spacetrack_usr, spacetrack_pwd = open( spacetrack_key_file).read()[:-1].split(',') st = SpaceTrackClient(identity=spacetrack_usr, password=spacetrack_pwd) tmp_tle_file = './tle2.csv' # Create a new df of the socrates entries with missing TLE data mtle_df1 = tle_df[tle_df['sat1_tle'].isnull()][[ 'sat1_norad', 'sat1_last_epoch' ]].rename(columns={ 'sat1_norad': 'norad', 'sat1_last_epoch': 'last_epoch' }) mtle_df2 = tle_df[tle_df['sat2_tle'].isnull()][[ 'sat2_norad', 'sat2_last_epoch' ]].rename(columns={ 'sat2_norad': 'norad', 'sat2_last_epoch': 'last_epoch' }) miss_tle_df = pd.concat([mtle_df1, mtle_df2]) miss_tle_df = miss_tle_df.sort_values('last_epoch') # Split the missing TLE dataset into bins bin_size = 100 num_bins = round(len(miss_tle_df) / bin_size + 0.49999) miss_tle_df['bin'] = pd.qcut(miss_tle_df['last_epoch'], num_bins, labels=list(range(num_bins))) print( f'There are {len(miss_tle_df)} missing TLE entries. We will make {num_bins} requests for this data.' ) print('Getting missing TLE data from Space Track...') # For each bin - make a request to SpaceTrack for all norads within that bin with a min/max daterange # This will save the data to a CSV file we will parse next (we save to ensure an interrupted progress # does not result in a massive amount of lost data) count = 0 for b in tqdm(range(num_bins)): tmp_df = miss_tle_df[miss_tle_df['bin'] == b] min_epoch = tmp_df['last_epoch'].min().to_pydatetime() max_epoch = tmp_df['last_epoch'].max().to_pydatetime() epoch_range = op.inclusive_range(min_epoch - timedelta(minutes=5), max_epoch + timedelta(minutes=5)) all_norads = list(tmp_df['norad'].unique()) all_data = st.gp_history(norad_cat_id=all_norads, epoch=epoch_range) # Write the data to a file for idx, row in tmp_df.iterrows(): d = list( filter( lambda rec: (rec['NORAD_CAT_ID'] == str(row['norad'])) & (abs( pd.to_datetime(rec['EPOCH'], format='%Y-%m-%dT%H:%M:%S.%f') - row['last_epoch']) < pd.Timedelta('5 min')), all_data))[0] with open(tmp_tle_file, 'a') as f: s = ','.join([ str(row['norad']), row['last_epoch'].strftime('%Y%m%d%H%M%S%f'), d['TLE_LINE1'], d['TLE_LINE2'], d['EPOCH'] ]) f.write(s + '\n') # Open the file created above which contains our new TLE data from SpaceTrack new_tle_df = pd.read_csv( tmp_tle_file, names=['norad', 'last_epoch', 'tle_line1', 'tle_line2', 'tle_epoch']) new_tle_df['last_epoch'] = pd.to_datetime(new_tle_df['last_epoch'], format='%Y%m%d%H%M%S%f') print(f'Space Track grabs are complete.') # For each TLE record, find the corresponding socrates data and update their TLE print(f'Merging {len(new_tle_df)} records with our socrates data...') count = 0 all_success = True for index, row in tqdm(new_tle_df.iterrows()): found = False target = tle_df[(tle_df['sat1_norad'] == row['norad']) & (abs(tle_df['sat1_last_epoch'] - row['last_epoch']) < pd.Timedelta('5 min'))].index if len(target) > 0: found = True tle_df.at[target, 'sat1_tle'] = row['tle_line1'] + ',' + row['tle_line2'] tle_df.at[target, 'sat1_tle_epoch'] = row['tle_epoch'] count += 1 target = tle_df[(tle_df['sat2_norad'] == row['norad']) & (abs(tle_df['sat2_last_epoch'] - row['last_epoch']) < pd.Timedelta('5 min'))].index if len(target) > 0: found = True tle_df.at[target, 'sat2_tle'] = row['tle_line1'] + ',' + row['tle_line2'] tle_df.at[target, 'sat2_tle_epoch'] = row['tle_epoch'] count += 1 if not found: print( f'Cant find norad={row["norad"]} for date {row["last_epoch"]} - perhaps we have an updated socrates file?' ) all_success = False print(f'Finished merging {count} records') # Save the TLE data to a pickle file print(f'Saving results...') tle_df[[ 'sat_pair', 'tca_time', 'sat1_norad', 'sat2_norad', 'sat1_tle', 'sat1_tle_epoch', 'sat2_tle', 'sat2_tle_epoch' ]].to_pickle(tle_file_path, 'gzip') print(f'Save to {tle_file_path} complete') if all_success: remove(tmp_tle_file) print(f'Removed temporary file') else: print( '******************************* WARNING *******************************' ) print( f'Please check messages and remove {tmp_tle_file} if everything was okay.' ) return None
julian_dates = load(os.path.join(passfile, 'julian_dates0.npy')) norad_id = load(os.path.join(passfile, 'norad_id0.npy')) timesteps = diff(julian_dates) * 24 * 3600 plt.plot(timesteps) plt.xlabel('Index') plt.ylabel('Timestep') plt.savefig(os.path.join(passfile, 'timesteps.png')) plt.close() times = (julian_dates - julian_dates[0]) * 24 * 3600 dt1 = julian.from_jd(julian_dates[0]) dt2 = dt1 + dt.timedelta(days=1) drange = op.inclusive_range(dt1, dt2) try: obj_data = st.tle(epoch=drange, norad_cat_id=norad_id)[0] except Exception as e: print('Failed to find object ID: ' + str(norad_id)) print(e) continue line1 = obj_data['TLE_LINE1'] line2 = obj_data['TLE_LINE2'] name = obj_data['TLE_LINE0'][2:] file = open(os.path.join(passfile, 'data0.txt'), 'a+') for key in obj_data.keys(): file.write(key + ': ' + str(obj_data[key])) file.write('\n')
def retrieveTLE(arglist=None): parser = ArgumentRecorder( description='Retrieve TLD data from space-track.org', fromfile_prefix_chars='@') parser.add_argument('-v', '--verbosity', type=int, default=1, private=True) parser.add_argument('--no-comments', action='store_true', help='Do not output descriptive comments') parser.add_argument('--no-header', action='store_true', help='Do not output CSV header with column names') parser.add_argument('-u', '--user', type=str, required=True, help='SpaceTrack.org username') parser.add_argument('-p', '--password', type=str, required=True, help='SpaceTrack.org password') parser.add_argument('-s', '--satellite', type=str, required=True, help='NORAD name') parser.add_argument('--startdate', type=str, required=True, help='Start date/time in any sensible format') parser.add_argument('--enddate', type=str, help='End date/time in any sensible format') parser.add_argument('-l', '--limit', type=int, help='Limit number of TLEs to retrieve') parser.add_argument('-o', '--outfile', type=str, help='Output CSV file, otherwise use stdout.', output=True) parser.add_argument( '--logfile', type=str, help= "Logfile, default is 'outfile'.log or stdout if outfile not specified") parser.add_argument('--no-logfile', action='store_true', help='Do not output descriptive comments') args = parser.parse_args(arglist) args.startdate = dateparser.parse( args.startdate) if args.startdate else None args.enddate = dateparser.parse(args.enddate) if args.enddate else None if args.outfile is None: outfile = sys.stdout else: if os.path.exists(args.outfile): shutil.move(args.outfile, args.outfile + '.bak') outfile = open(args.outfile, 'w') if not args.no_logfile: if not args.logfile and not args.outfile: logfile = sys.stdout else: if args.logfile: logfilename = args.logfile elif args.outfile: logfilename = args.outfile.split('/')[-1].rsplit('.', 1)[0] + '.log' logfile = open(logfilename, 'w') parser.write_comments(args, logfile, incomments=ArgumentHelper.separator()) if args.logfile or args.outfile: logfile.close() st = SpaceTrackClient(identity=args.user, password=args.password) tledict = tlefile.read_platform_numbers() norad_cat_id = tledict[args.satellite] drange = op.inclusive_range(args.startdate, args.enddate or date.today()) lines = st.tle(norad_cat_id=norad_cat_id, epoch=drange, format='tle', limit=args.limit).split("\n") for line in lines: if len(line): outfile.write(line + "\n") if args.outfile is not None: outfile.close() exit(0)
import spacetrack.operators as op from spacetrack import SpaceTrackClient print("execute getTLE") referenceTime = (dt.datetime.utcnow() + dt.timedelta(days=-2)).timetuple() endTime = (dt.datetime.utcnow() + dt.timedelta(days=-1)).timetuple() currentYear = referenceTime[0] currentMonth = referenceTime[1] currentDay = referenceTime[2] currentHour = referenceTime[3] currentMinute = referenceTime[4] currentSecond = referenceTime[5] endYear = endTime[0] endMonth = endTime[1] endDay = endTime[2] dateRange = op.inclusive_range(dt.date(currentYear, currentMonth, currentDay), dt.date(endYear, endMonth, endDay)) satellite = 'TERRA' # hardcode at moment, need to improve satelliteID = 25994 st = SpaceTrackClient('*****@*****.**', 'P62-AER-sNJ-ubK') lines = st.tle(iter_lines=True, epoch=dateRange, norad_cat_id=satelliteID, orderby='TLE_LINE1 ASC', format='tle') filePath = os.getcwd() + '/app/tle.txt' print(filePath) with open(filePath, 'w') as fp: fp.write(satellite + '\n') for line in lines: print(line)