[英]How to combine data arrays by multi longitude and latitude in one dataset in python
I made for loop of my data array of each long/lat point and I appended all result in one list as below:我对每个 long/lat 点的数据数组进行了 for 循环,并将所有结果附加到一个列表中,如下所示:
out_list=[]
for i in ds.longitude.values:
for j in ds.latitude.values:
point = arr.sel(longitude=i,latitude=j)
p_detrend = sm.tsa.tsatools.detrend(point, order=1,axis=0)
out_list.append(p_detrend)
and my list as below:我的清单如下:
and you can see there are many arrays and each one has long/lat.
你可以看到有很多 arrays 并且每个都有长/纬度。 How to combine all arrays in one dataset by longitude and latitude?
如何按经度和纬度将所有 arrays 组合在一个数据集中?
maybe you could use dictionary instead, something like:也许您可以改用字典,例如:
out_list = []
for i in ds.longitude.values:
for j in ds.latitude.values:
point = arr.sel(longitude=i,latitude=j)
p_detrend = sm.tsa.tsatools.detrend(point, order=1,axis=0)
result = {
'longitude': i,
'latitude': j,
'detrend_arr': p_detrend
}
out_list.append(result)
then you may use pandas to convert into pd.DataFrame
for other manipulation;然后您可以使用 pandas 转换为
pd.DataFrame
进行其他操作;
What if you use xarray.merge()
?如果你使用
xarray.merge()
怎么办?
I used the example from documentation and your description to create a fake data for different longitudes and latitudes, all stacked in a list.我使用 文档中的示例和您的描述为不同的经度和纬度创建了一个假数据,所有这些数据都堆叠在一个列表中。
Then, if I use xarray.merge()
, it will automatically structure the data into ('time','longitude','latitude')
xarray object.然后,如果我使用
xarray.merge()
,它会自动将数据构造成('time','longitude','latitude')
xarray object。
import numpy as np
import pandas as pd
import xarray as xr
np.random.seed(123)
xr.set_options(display_style="html")
times = pd.date_range("2000-01-01", "2001-12-31", name="time")
annual_cycle = np.sin(2 * np.pi * (times.dayofyear.values / 365.25 - 0.28))
ds = []
for lon in range(-40,-30):
for lat in range(0,10):
base = 10 + 15 * annual_cycle.reshape(-1,1,1)
tmin = base + 3 * np.random.randn(annual_cycle.size,1,1)
tmax = base + 10 + 3 * np.random.randn(annual_cycle.size,1,1)
ds.append(xr.Dataset(
{
"tmin": (("time", "latitude","longitude"), tmin),
"tmax": (("time", "latitude","longitude"), tmax),
},
{"time": times,"latitude":[lat],"longitude":[lon]},
))
ds = xr.merge(ds)
First, ds
will be like a list of different xarray
objects for each latitude and longitude:首先,
ds
就像每个纬度和经度的不同xarray
对象的列表:
[<xarray.Dataset>
Dimensions: (latitude: 1, longitude: 1, time: 731)
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31
* latitude (latitude) int64 0
* longitude (longitude) int64 -40
Data variables:
tmin (time, latitude, longitude) float64 -4.646 -3.527 ... -3.939
tmax (time, latitude, longitude) float64 8.436 1.992 ... 3.366 1.605,
<xarray.Dataset>
Dimensions: (latitude: 1, longitude: 1, time: 731)
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31
* latitude (latitude) int64 1
* longitude (longitude) int64 -40
Data variables:
tmin (time, latitude, longitude) float64 -2.966 -3.322 ... -7.731
tmax (time, latitude, longitude) float64 3.156 5.467 ... 11.85 7.26,
<xarray.Dataset>
.
.
.
.
]
Then, after the merge, it will be a xarray.dataset
just like this:然后,在合并之后,它将是一个
xarray.dataset
,就像这样:
Please, let me know if that helps, or if I need to adapt in some way to work for your specific type of data.请让我知道这是否有帮助,或者我是否需要以某种方式适应您的特定类型的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.