首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将时间添加到netcdf文件?

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

Stack Overflow用户
提问于 2019-07-12 19:51:34
回答 1查看 993关注 0票数 0

我想在netcdf文件中创建一个3维的时间序列(经度、纬度、时间无限)。timeseries应该从其他netcdf文件创建。它们中的每一个只有一个时间点,例如17856。我知道如何创建新的netcdf-file,如何从netcdf-file中提取作为2D数组的数据,以及数据的时间。我的问题是:如何将2D数组放入netcdf-file中并显示其正确的时间?"ncvar_put“函数中的开始和计数参数是如何工作的?

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

我在http://geog.uoregon.edu/bartlein/courses/geog490/week04-netCDF.html#create-and-write-a-netcdf-file上找到了答案,但我还是不明白。我仍然没有使用netcdf文件的经验。

示例

代码语言:javascript
复制
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数组,当我花时间,我想以正确的顺序获得正确的时间。

EN

回答 1

Stack Overflow用户

发布于 2019-10-11 00:02:29

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

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57006443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档