如何将时间添加到netcdf文件?

时间:2019-07-12 11:51:33

标签: r time-series netcdf netcdf4

我想在3个维度(lon,lat,time [unlimited])的netcdf文件中创建一个时间序列。时间序列应从其他netcdf文件创建。他们每个人只有一个时间点[例如17856]。 我知道如何创建新的netcdf文件,如何从netcdf文件中将数据提取为2D数组以及数据获取的时间。 我的问题是: 如何将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阵列,并在抽出时间时希望以正确的顺序进行正确的时间。

1 个答案:

答案 0 :(得分:0)

我使用另一种方法将时间添加到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
**********************************************************************

您可能会注意到,时间是第一个维度。有关详细信息,这是我引用的文档的link