简体   繁体   English

Python插值时间

[英]Python Interpolating Time

Novice in python so I hope I am asking correctly. python新手,希望我能正确询问。

I have a huge set of data that I would like to interpolate to every one second and fill in the gaps with the appropriate latitude and longitude provided: 我想将大量数据插值到每秒,并使用提供的适当纬度和经度填补空白:

  Lat     Long    Time
-87.10   30.42    16:38:49
                      .
                      .
                      .
-87.09   30.40    16:39:22
                      .
                      .
                      .
-87.08   30.40    16:39:30

So I would like to generate a new latitude and longitude every second. 因此,我想每秒生成一个新的纬度和经度。

I have already plotted the corresponding latitude and longitude and would like to fill in the gaps with the interpolated data with points possibly. 我已经绘制了相应的纬度和经度,并希望用点插入的数据来填补空白。

If linear interpolation is good enough for you, you can use the numpy.interp function with the time array that you want to use for interpolation and the time and longitude/latitude data points that are read from the input file (the time must be increasing so you may need to sort the data). 如果线性插值对您足够好,则可以将numpy.interp函数与要用于插值的时间数组以及从输入文件中读取的时间和经度/纬度数据点一起使用(时间必须增加因此您可能需要对数据进行排序)。

To read the data for the file you can use the numpy.loadtxt function adding a converter to transform the time to an increasing number: 要读取文件的数据,可以使用numpy.loadtxt函数添加一个转换器以将时间转换为递增的数字:

import numpy as np
from matplotlib.dates import strpdate2num

lon, lat, time = np.loadtxt('data.txt', skiprows=1,
        converters={2:strpdate2num('%H:%M:%S')}, unpack=True)

Then you can interpolate the longitude and latitude values using the interp function. 然后,您可以使用interp函数对经度和纬度值进行插值。 The last argument to the linspace function gives the number of points in the time interpolated data. linspace函数的最后一个参数给出了时间插值数据中的点数。

interp_time = np.linspace(time[0], time[-1], 100)
interp_lon = np.interp(interp_time, time, lon)
interp_lat = np.interp(interp_time, time, lat)

For something more complicated that linear interpolation there are several facilities in scipy.interpolation . 对于比线性插值更复杂的内容, scipy.interpolation有多种功能。

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

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