簡體   English   中英

在python中更改netcdf變量中的單位

[英]Change unit in a netcdf variable in python

我在 Python 中有一個 netCDF 文件,其中時間變量的單位是days_since_Jan11900xarray包無法讀取該xarray 它拋出錯誤

unable to decode time units 'days_since_Jan11900' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.

我不想使用decode_times而只是將其更改為days_since_0 這是從 1/1/0000 開始的幾天。

如何將 netCDF 中的單位更改為可讀的單位?

<class 'netCDF4._netCDF4.Variable'>
float64 days(time)
    units: days_since_Jan11900
    long_name: calendar_days
unlimited dimensions: 
current shape = (87600,)
filling on, default _FillValue of 9.969209968386869e+36 used

如果您使用的是 Unix,則可以嘗試使用 CDO 更改日歷。 以下應該可以解決您的問題:

cdo -L  -setreftime,0001-01-01  -settaxis,0001-01-01,0:00:00,1day  -setcalendar,standard infile.nc outfile.nc 

我已將時間設置為第 1 年,因為我認為任何有效日歷中都沒有第 0 年。

正如我所要求的那樣,我自己在 Python 中解決了這個問題,而不是通過 cdo。 這是我所做的,這很容易:

ds['days'] = ds.days - 730966
ds.days.attrs['units'] = 'days since 1860-01-01'
ds.to_netcdf(path + 'name_of_file.nc')

730966是我在days數組中的起始值,因此我減去了它並將我的單位更改為一個足夠的單位,在我的情況下, days since 1860-01-01

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM