繁体   English   中英

Python 3,找到最近的时间日期值

[英]Python 3, find nearest timedate value

我希望在这里找到帮助,我是 python 新手,我想从 2 个具有不同时间结构的不同 nc 文件中提取时间。 第一个 nc 文件将时间和日期信息存储为:

year: 2009
month: 10
day: 12
hour: 20
minute: 10
second: 4.0

并且第二个nc文件时间和日期是n事件的“unix epoch time”格式(即这个nc文件包含几个float64时间(n_event)中的时间日期信息)

我想要做的是找到从第二个 nc 文件到第一个 nc 文件时间的最近时间。

我已经为第一个 nc 文件尝试了此代码:

    #time
hour=(nc.__dict__['hour'])
minute=(nc.__dict__['minute'])
second=(nc.__dict__['second'])
    #time= hour,minute,second
time= str(hour)+':'+str(minute)+':'+str(second)

    #date
year_nc=(nc.__dict__['year'])
month_nc=(nc.__dict__['month'])
day_nc=(nc.__dict__['day'])
date_nc= year_nc,month_nc,day_nc
#solved-not used

date_nc_arr = np.array(date_nc)
time_nc_arr = np.array(time)

time_nc_arr
date_nc_arr


full_date_nc_arr=[date_nc_arr,time_nc_arr]
full_date_nc_arr

我得到

[array([2009,   10,   12], dtype=int32), array('20:10:4.0', dtype='<U9')]

对于第二个 nc 文件,我做了:

from datetime import datetime
import datetime


import datetime 
from netCDF4 import Dataset,num2date 

t_unit = nc2.variables['time'].units # get unit  
t_cal = nc2.variables['time'].calendar
tvalue = num2date(timess,units = t_unit,calendar = t_cal)
str_time = [i.strftime("%Y-%m-%d %H:%M:%S") for i in tvalue]


str_time = list(map(lambda x: datetime.datetime.strptime(x,  "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %I:%M:%S"), str_time))
print(str_time)

输出类似于:(我只是在这里粘贴了一些时间)

['2009-10-12 09:19:25', '2009-10-12 09:24:15', '2009-10-12 09:25:03', '2009-10-12 09:26:37', '2009-10-12 09:31:48', '2009-10-12 09:36:13', '2009-10-12 09:40:46', '2009-10-12 09:41:28', '2009-10-12 09:42:13', '2009-10-12 09:46:14', '2009-10-12 09:47:18', '2009-10-12 09:54:58', '2009-10-12 10:03:07', '2009-10-12 10:09:10', '2009-10-12 10:14:54', '2009-10-12 10:15:34', '2009-10-12 10:16:49', '2009-10-12 10:18:29', '2009-10-12 10:18:59', '2009-10-12 10:20:13', '2009-10-12 10:26:12', '2009-10-12 10:26:14', '2009-10-12 10:32:02', '2009-10-12 10:34:28', '2009-10-12 10:34:42', '2009-10-12 10:35:34', '2009-10-12 10:36:30', '2009-10-12 10:40:35', '2009-10-12 10:47:22', '2009-10-12 10:56:07', '2009-10-12 10:57:02', '2009-10-12 10:59:33', '2009-10-12 11:00:26', '2009-10-12 11:14:17', '2009-10-12 11:14:55', '2009-10-12 11:21:07', '2009-10-12 11:27:10', '2009-10-12 11:27:39', '2009-10-12 11:36:08', '2009-10-12 11:36:58', '2009-10-12 11:40:28', '2009-10-12 11:42:02', '2009-10-12 11:46:35', '2009-10-12 11:48:31', '2009-10-12 11:50:13', '2009-10-12 11:52:06']

但是当我尝试找到第一个 nc 文件最接近第二个 nc 文件的时间日期值时:

def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return array[idx]

array = str_time
value = full_date_nc_arr

print(find_nearest(array, value))

我收到几个不同的错误,例如:

ValueError:操作数无法与形状一起广播 (463,) (6,)

如果有人能给我一个解决方案或更好的方法来解决这个问题,我将不胜感激。

如果您有日期\/时间数据,则使用日期时间对象。

from datetime import datetime
t1 = datetime(
    nc.year, nc.month, nc.day,
    nc.hour, nc.minute, nc.second
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM