【问题标题】:Define time dependent variable in a netcdf file using python使用python在netcdf文件中定义时间相关变量
【发布时间】:2016-06-06 11:19:04
【问题描述】:

我将解释一下我的情况:我正在尝试编辑 netcdf 文件的一些变量并写出一个新的 netcdf 文件,该文件显示新编辑的变量。我的问题是变量是时间相关的,我不知道如何将其包含在我的代码中:

import netCDF4
import numpy as np

 ncfile = netCDF4.Dataset('toread.nc', 'r')
 u = ncfile.variables['u'][:,:,:]
 v = ncfile.variables['v'][:,:,:]

 nx = np.shape(u)[0] - 1 
 ny = np.shape(v)[1] - 1 
 nz = np.shape(u)[2] 

 u_center = 0.5 * (u[0:nx,:,:] + u[1:nx+1,:,:]) 
 v_center = 0.5 * (v[:,0:ny,:] + v[:,1:ny+1,:])


 ncfile_out = netCDF4.Dataset('./output.nc', 'w')
 ncfile_out.createDimension('longitude', nx)
 ncfile_out.createDimension('latitude', ny)
 ncfile_out.createDimension('level', nz)
 u_out = ncfile_out.createVariable('u_center', 'f4', ('longitude', 'latitude', 'level'))
 v_out = ncfile_out.createVariable('v_center', 'f4', ('longitude', 'latitude', 'level'))
 u_out[:,:,:] = u_center[:,:,:]
 v_out[:,:,:] = v_center[:,:,:]
 ncfile_out.close()

我试图编译它,但它显示了一个 Value Error 谈论维度问题。我认为我的变量是 4D 的,包括时间依赖性,但我不知道如何定义它并完成我的代码。

ncdump 信息:

   netcdf state.global {
dimensions:
    T = UNLIMITED ; // (10001 currently)
    Xp1 = 61 ;
    Y = 1 ;
    Z = 20 ;
    X = 60 ;
    Yp1 = 2 ;
    Zl = 20 ;
variables:
    double Xp1(Xp1) ;
        Xp1:long_name = "X-Coordinate of cell corner" ;
        Xp1:units = "meters" ;
    double Y(Y) ;
        Y:long_name = "Y-Coordinate of cell center" ;
        Y:units = "meters" ;
    double Z(Z) ;
        Z:long_name = "vertical coordinate of cell center" ;
        Z:units = "meters" ;
        Z:positive = "up" ;
    double X(X) ;
        X:long_name = "X-coordinate of cell center" ;
        X:units = "meters" ;
    double Yp1(Yp1) ;
        Yp1:long_name = "Y-Coordinate of cell corner" ;
        Yp1:units = "meters" ;
    double Zl(Zl) ;
        Zl:long_name = "vertical coordinate of upper cell interface" ;
        Zl:units = "meters" ;
        Zl:positive = "up" ;
    double T(T) ;
        T:long_name = "model_time" ;
        T:units = "s" ;
    int iter(T) ;
        iter:long_name = "iteration_count" ;
    double U(T, Z, Y, Xp1) ;
        U:units = "m/s" ;
        U:coordinates = "XU YU RC iter" ;
    double V(T, Z, Yp1, X) ;
        V:units = "m/s" ;
        V:coordinates = "XV YV RC iter" ;
    double Temp(T, Z, Y, X) ;
        Temp:units = "degC" ;
        Temp:long_name = "potential_temperature" ;
        Temp:coordinates = "XC YC RC iter" ;
    double S(T, Z, Y, X) ;
        S:long_name = "salinity" ;
        S:coordinates = "XC YC RC iter" ;
    double Eta(T, Y, X) ;
        Eta:long_name = "free-surface_r-anomaly" ;
        Eta:units = "m" ;
        Eta:coordinates = "XC YC iter" ;
    double W(T, Zl, Y, X) ;
        W:units = "m/s" ;
        W:coordinates = "XC YC RC iter" ;

// global attributes:
        :MITgcm_version = "****************" ;
        :build_user = "************" ;
        :build_host = "**************" ;
        :build_date = "*******************" ;
        :MITgcm_URL = "***************" ;
        :MITgcm_tag_id = "*******************" ;
        :MITgcm_mnc_ver = 0.9 ;
        :sNx = 30 ;
        :sNy = 1 ;
        :OLx = 2 ;
        :OLy = 2 ;
        :nSx = 2 ;
        :nSy = 1 ;
        :nPx = 1 ;
        :nPy = 1 ;
        :Nx = 60 ;
        :Ny = 1 ;
        :Nr = 20 ;
}

【问题讨论】:

    标签: python netcdf


    【解决方案1】:

    你必须为时间创建另一个维度:

    ncfile_out.createDimension('time', nt) # to make time dimension unlimited put None instead of nt
    v_out = ncfile_out.createVariable('v_center', 'f4', ('time', 'longitude', 'latitude', 'level'))
    

    然后添加额外的数组来存储时间值并填充它:

    time = ncfile_out.createVariable('Time', 'i4', 'time')
    

    您可以在此处找到其他信息:http://pyhogs.github.io/intro_netcdf4.html

    【讨论】:

    • 我会试试的。 ('Time') 和 ('time') 有什么区别?
    • 我猜,没关系,因为它只是一个名字。问题是您的数组有 3 个维度,但您需要编写 4d 数组。
    • 怎么样?您能发布必要的更改吗?
    • 喜欢这里:newu = np.zeros((nx,ny,nz,2), dtype=np.float32); newu[:,:,:,0] = u_center[:,:,:]
    • 所以我应该把 v_out[:,:,:] = v_center[:,:,:] 改成 v_out[:,:,:,:]=v_center[:,:,:,: ] ?
    猜你喜欢
    • 1970-01-01
    • 2016-02-15
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多