首页 > 解决方案 > 如何将时间添加到 netcdf 文件?

问题描述

我想在具有 3 个维度(经度、纬度、时间 [无限])的 netcdf 文件中创建时间序列。应该从其他 netcdf 文件创建时间序列。他们每个人只有一个时间点 [例如 17856]。我知道如何创建新的 netcdf 文件,如何从 netcdf 文件中提取数据作为二维数组以及数据的时间。我的问题是:如何将 2D 数组以正确的时间放入 netcdf 文件中?“ncvar_put”函数中的 start 和 count 参数是如何工作的?

我使用 ncdf4 包并阅读以下教程:

http://geog.uoregon.edu/bartlein/courses/geog490/week04-netCDF.html#create-and-write-a-netcdf-file并搜索了答案,但我仍然不明白。我仍然对 netcdf 文件不熟悉。

例子

e of my problem:

# data from other netcdf file
values = array(data = c(1:9)/10, dim = c(3,3))
values_2 = array(data = c(9:25)/10, dim = c(3,3))
time = 25
time_2 = 23

# set parameters
lon = 1:3
lat = 1:3


# define dimensions
# Longitude
londim = ncdim_def(name = "longitude", units = "degrees", vals = as.double(lon),
                   longname = "longitude")
# Latitude
latdim = ncdim_def(name = "latitude", units = "degrees", vals = as.double(lat),
                   longname = "latitude")
# Time
timedim = ncdim_def(name = "time", units ="days since 1582-10-15 00:00", vals = as.double(1),
                    unlim = TRUE, calendar = "gregorian")

# define variables
B01 = ncvar_def(name = "B01",
                units ="percent",
                list(londim,latdim,timedim), 
                missval = NA,
                prec="double")

# create netcdf
nc_test = nc_create("test.nc", list(B01), force_v4 = TRUE)

# Add values 
### Here is somethin missing --> How do I add the timestamp?
ncvar_put(nc_test, "B01", values, start=c(1,1,1), count=c(-1,-1,1))
ncvar_put(nc_test, "B01", values2, start=c(1,1,2), count=c(-1,-1,1))

当我想提取数据时,我得到了 3-3-2 数组,但时间步长不正确,因为我没有添加它们。我该怎么做呢?我想要 3-3-2 数组,当我花时间时,我想要正确的时间以正确的顺序。

标签: rtime-seriesnetcdfnetcdf4

解决方案


我使用另一种方法将时间添加到 netCDF 文件。这是供您参考的示例代码。

from datetime import datetime
from datetime import timedelta
from netCDF4 import date2num
from netCDF4 import Dataset
import os

# generate time for netCDF with 1 hour interval
utc_now = datetime.utcnow()
time_list = [utc_now + timedelta(hours=1*step) for step in range(6)]
trans_time = date2num(time_list, units="hours since 0001-01-01 00:00:00.0", calendar="gregorian") 

with Dataset(os.getcwd() + "/sample.nc", "w") as sample:
    # Create dimension for sample.nc
    time = sample.createDimension("time", None)
    lat = sample.createDimension("lat", 3)  # 3 is the latitude size in this sample
    lon = sample.createDimension("lon", 3)

    # Create variable for sample.nc
    time = sample.createVariable("time","f8",("time",))
    lat = sample.createVariable("lat","f4",("lat",))
    lon = sample.createVariable("lon","f4",("lon",))

    time[:] = trans_time

    variable_with_time = sample.createVariable("variable_with_time", "f4", ("time", "lat", "lon"))

    for key, value in sample.variables.items():
        print(key)
        print(value)
        print("*"*70)

输出:

time
<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
unlimited dimensions: time
current shape = (6,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
lat
<class 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
unlimited dimensions: 
current shape = (3,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
lon
<class 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
unlimited dimensions: 
current shape = (3,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
variable_with_time
<class 'netCDF4._netCDF4.Variable'>
float32 variable_with_time(time, lat, lon)
unlimited dimensions: time
current shape = (6, 3, 3)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************

您可能会注意到,时间被放置为第一维度。有关详细信息,这是我引用的文档的链接。


推荐阅读