使用 python 编辑 netcdf 变量并写出新的 netcdf 文件
Editing netcdf variables and writting out a new netcdf file using python
我正在与 MITgcm 合作进行一些模拟,特别是使用内波模型;我得到了包含结果的 .nc 文件,但一些变量的坐标并不完全相同。我会解释一下:我想计算出速度的分量,但是由于一些我不完全理解的数字原因,水平速度坐标在单元格的左侧,垂直坐标在单元格的底部.要使用速度数据进行操作,我需要统一所有坐标的参考。
为此,我有以下代码:
import netCDF4
import numpy as np
ncfile = netCDF4.Dataset('state.global.nc', 'r')
u = ncfile.variables['U'][:,:,:] # nx+1 x ny x nz
v = ncfile.variables['V'][:,:,:] # nx x ny+1 x nz
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,:])
# Write out u_center and v_center into a new netCDF file
ncfile_out = netCDF4.Dataset('./output.nc', 'w')
ncfile_out.createDimension('longitude', nx)
ncfile_out.createDimension('latitude', ny)
ncfile_out.createDimension('level', nz)
ncfile_out.createDimension('time', None)
u_out = ncfile_out.createVariable('u_center', 'f4', ('time', 'longitude', 'latitude', 'level'))
v_out = ncfile_out.createVariable('v_center', 'f4', ('time', 'longitude', 'latitude', 'level'))
time = ncfile_out.createVariable('Time', 'i4', 'time')
u_out[:,:,:] = u_center[:,:,:]
v_out[:,:,:] = v_center[:,:,:]
ncfile_out.close()
但它没有编译,它在第 24 行和第 25 行显示错误 (u_out[:,:,:]...);确切地说 'IndexError: size of data array does not conform to slice'。我尝试将 u_out[:,:,:] 更改为 u_out[:,:,:,:] 等等。我不知道我的错误是什么。
有关更多信息,我在此处粘贴有关我的原始 netcdf 文件的信息:
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 ;
}
我只对速度变量感兴趣。
非常感谢。
您将 u_out
创建为具有四个维度('time'、'longitude'、'latitude'、'level')的 netCDF 变量,但您使用了3D 切片 ([:,:,:]) 为其分配数据。
我正在与 MITgcm 合作进行一些模拟,特别是使用内波模型;我得到了包含结果的 .nc 文件,但一些变量的坐标并不完全相同。我会解释一下:我想计算出速度的分量,但是由于一些我不完全理解的数字原因,水平速度坐标在单元格的左侧,垂直坐标在单元格的底部.要使用速度数据进行操作,我需要统一所有坐标的参考。
为此,我有以下代码:
import netCDF4
import numpy as np
ncfile = netCDF4.Dataset('state.global.nc', 'r')
u = ncfile.variables['U'][:,:,:] # nx+1 x ny x nz
v = ncfile.variables['V'][:,:,:] # nx x ny+1 x nz
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,:])
# Write out u_center and v_center into a new netCDF file
ncfile_out = netCDF4.Dataset('./output.nc', 'w')
ncfile_out.createDimension('longitude', nx)
ncfile_out.createDimension('latitude', ny)
ncfile_out.createDimension('level', nz)
ncfile_out.createDimension('time', None)
u_out = ncfile_out.createVariable('u_center', 'f4', ('time', 'longitude', 'latitude', 'level'))
v_out = ncfile_out.createVariable('v_center', 'f4', ('time', 'longitude', 'latitude', 'level'))
time = ncfile_out.createVariable('Time', 'i4', 'time')
u_out[:,:,:] = u_center[:,:,:]
v_out[:,:,:] = v_center[:,:,:]
ncfile_out.close()
但它没有编译,它在第 24 行和第 25 行显示错误 (u_out[:,:,:]...);确切地说 'IndexError: size of data array does not conform to slice'。我尝试将 u_out[:,:,:] 更改为 u_out[:,:,:,:] 等等。我不知道我的错误是什么。
有关更多信息,我在此处粘贴有关我的原始 netcdf 文件的信息:
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 ;
}
我只对速度变量感兴趣。
非常感谢。
您将 u_out
创建为具有四个维度('time'、'longitude'、'latitude'、'level')的 netCDF 变量,但您使用了3D 切片 ([:,:,:]) 为其分配数据。