简体   繁体   English

Python 从 .netcdf 列更改 xarray 维度坐标值

[英]Python change xarray dimension coordinate values from netcdf column

I have two datasets:我有两个数据集:

  • An dataframe df with converted time data带有转换时间数据的 dataframe df
  • A.netCDF file on my disk, loaded as an xarray.Dataset ds.我磁盘上的 A.netCDF 文件,作为 xarray.Dataset ds 加载。

The.netcdf/xarray has 3 dimensions: time, latitude, longitude. .netcdf/xarray 有 3 个维度:时间、纬度、经度。

I want to update the time dimension in the xarray.Dataset with the converted values of the dataframe. Currently, I am doing it like this:我想用 dataframe 的转换值更新 xarray.Dataset 中的时间维度。目前,我是这样做的:

import xarray as xr

# load netcdf
ds = xr.open_dataset('msr.nc', decode_times= False) # as the time data is not correct

# Copy converted time
ds['time'] = df['conv_time']

The coordinates get updated as another column, but the original stays.坐标会更新为另一列,但原始坐标会保留。

The xarray.Dataset looks as the following: xarray.Dataset 如下所示:


> Dimensions:  dim_0: 504 longitude: 720 latitude: 361 time: 504
> Coordinates:
time       (dim_0)      datetime64[ns]  1979-01-15 ... 2020-12-15
longitude  (longitude)  float32        -179.5 -179.0 ... 179.5 180.0
latitude   (latitude)   float32        -90.0 -89.5 -89.0 ... 89.5 90.0
dim_0      (dim_0)      int64           0 1 2 3 4 5 ... 499 500 501 502 503

It seems it creates a new time dimension and makes a new one(dim_0) of the old one, but doesn't correctly make the link with the updated datetime coordinates.它似乎创建了一个新的时间维度并创建了一个新的时间维度(dim_0),但没有正确地与更新的日期时间坐标建立链接。

The file now has 4 dimensions, but only 3 coordinates and it still links to the wrong one.该文件现在有 4 个维度,但只有 3 个坐标,它仍然链接到错误的坐标。

I have tried creating a new coordinate, deleting the other one, swapping the names.我试过创建一个新坐标,删除另一个坐标,交换名称。 But this all doesn't work.但这一切都行不通。 I just want to simply change the values of the time dimension to the ones I already have.我只想简单地将时间维度的值更改为我已有的值。

How do I fix this to make sure the updated time is the only time dimension?我该如何解决这个问题以确保更新时间是唯一的时间维度?

It was very simple, and use:这非常简单,使用:

ds["time"] = ("time", updated_times)

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

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