Python中日期范围的联合



我正在编写一个python代码,在这里我模拟卫星接触持续时间和给定地面段的日期范围,现在我有一个星座中多个(>=2)卫星的这些数据帧。到目前为止,我通过熊猫分析了每颗卫星的数据。我试图实现的是合并多个卫星重叠的日期范围到一个单一的结果文件。以两个卫星为例:

文件1:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
450.61717646411466  2022-01-01 13:11:18.686564  2022-01-01 13:18:49.303741
272.9796195817538   2022-01-01 14:45:04.846243  2022-01-01 14:49:37.825862

文件2:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
576.600683837155    2022-01-01 13:06:51.364924  2022-01-01 13:16:27.965608
568.5843137051123   2022-01-01 14:40:38.840363  2022-01-01 14:50:07.424677

我的目标是将这些日期范围和持续时间合并并固定在一个文件中,就像这样:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
718.600683837155    2022-01-01 13:06:51.364924  2022-01-01 13:18:49.303741
568.5843137051123   2022-01-01 14:40:38.840363  2022-01-01 14:50:07.424677

pandas(或任何其他库)有现成的函数来处理这种问题吗?否则,谁能帮我解决这个问题?

提前感谢。

感谢@MrFuppes和@NuLo的反馈。

我最终能够自己整理:首先,我将所有输入收集到一个文件中,并根据初始数据对它们进行排序。第二步是合并它们。下面是我的一段代码,作为一个函数,现在可以工作了,我认为它可以帮助别人在未来:

import os
import pandas as pd
def mergeContactsIntoConstellation(originDIR, destinyDIR, station, noSatellites):
'''
Reads each of the individual satellites accesses for a given ground station,
and merge them into a single constellation contact.
:originDIR: directory containing the individual files
:destintyR: directory where the merged file is saved
:station: ground station of interest
:noSatellites: number of satellites in the constellation
'''
iDates = []
fDates = []
for i in range(0, noSatellites):
file = originDIR + station.name+'_sat{}_Contacts.txt'.format(i+1)
if os.stat(file).st_size == 0:
continue
file = pd.read_csv(originDIR + station.name+'_sat{}_Contacts.txt'.format(i+1),delimiter='t',header=0,engine='python')
file.columns = ['duration', 'itime', 'ftime']
itime = file['itime']
ftime = file['ftime']
for iDate, fDate in zip(itime, ftime):
iDates.append(datetime.strptime(iDate, '%Y-%m-%d %H:%M:%S.%f'))
fDates.append(datetime.strptime(fDate, '%Y-%m-%d %H:%M:%S.%f'))
newiDates = sorted(iDates)
newfDates = []
for newiDate in newiDates:
for idate, fdate in zip(iDates, fDates):
if newiDate == idate:
newfDates.append(fdate)
Dates = [newiDates, newfDates]
resultingiDates = []
resultingfDates = []
for i in range(0, len(Dates[0])-1):
if Dates[1][0] >= Dates[0][i+1]:
Dates[0][0] = min(Dates[0][0], Dates[0][i+1])
Dates[1][0] = max(Dates[1][0], Dates[1][i+1])
else:
resultingiDates.append(Dates[0][0])
resultingfDates.append(Dates[1][0])
Dates[0][0] = Dates[0][i+1]
Dates[1][0] = Dates[1][i+1]
else:
resultingiDates.append(Dates[0][0])
resultingfDates.append(Dates[1][0])
jointContacts = pd.DataFrame()
for idate, fdate in zip(resultingiDates, resultingfDates):
jointContacts=jointContacts.append({
"Duration (s)": datetime_to_absolutedate(fdate).durationFrom(datetime_to_absolutedate(idate)),
"Start Time (UTC)": idate,
"Stop Time (UTC)":fdate}, ignore_index=True)
jointContacts.to_csv(destinyDIR+station.name+'_JointContacts.txt', sep='t', index=False)

一些函数,如"durationFrom"来自OREKIT的python包装器

最新更新