Python 使用CDO将累积变量转换为netcdf文件中的时间步长值
我有一个netcdf文件,在一个带有一个变量的网格上有大约100个timesteps,它是在timesteps上累积的。我现在感兴趣的是计算每个时间步对变量值的贡献(即连续时间步的差值) 目前,我使用以下顺序:Python 使用CDO将累积变量转换为netcdf文件中的时间步长值,python,netcdf,nco,cdo-climate,Python,Netcdf,Nco,Cdo Climate,我有一个netcdf文件,在一个带有一个变量的网格上有大约100个timesteps,它是在timesteps上累积的。我现在感兴趣的是计算每个时间步对变量值的贡献(即连续时间步的差值) 目前,我使用以下顺序: 要将每个时间步提取到新文件中,我使用cdo seltimestep,$I… 使用cdo sub$i${i-1}… 最后用cdo mergetime…将这些新文件合并到一个结果文件中 在我看来,这似乎是非常繁琐的和不理想的表现。由于时间步长太多,我无法使用cdo管道,因此需要同时创建许多文
cdo seltimestep,$I…
cdo sub$i${i-1}…
cdo mergetime…
将这些新文件合并到一个结果文件中import netCDF4
import numpy as np
ncfile = netCDF4.Dataset('./myfile.nc', 'r')
var = ncfile.variables['variable'][:,:,:] # [time x lat x lon]
# Differences with a step of 1 along the 'time' axis (0)
var_diff = np.diff(var, n=1, axis=0)
ncfile.close()
# Write out the new variable to a new file
ntim, nlat, nlon = np.shape(var_diff)
ncfile_out = netCDF4.Dataset('./outfile.nc', 'w')
ncfile_out.createDimension('time', ntim)
ncfile_out.createDimension('lat', nlat)
ncfile_out.createDimension('lon', nlon)
var_out = ncfile_out.createVariable('variable', 'f4', ('time', 'lat', 'lon',))
var_out[:,:,:] = var_diff[:,:,:]
ncfile_out.close()
计算连续条目的差异
我怀疑您的文件中有一个多维变量,因此下面是一个通用示例:
import netCDF4
import numpy as np
ncfile = netCDF4.Dataset('./myfile.nc', 'r')
var = ncfile.variables['variable'][:,:,:] # [time x lat x lon]
# Differences with a step of 1 along the 'time' axis (0)
var_diff = np.diff(var, n=1, axis=0)
ncfile.close()
# Write out the new variable to a new file
ntim, nlat, nlon = np.shape(var_diff)
ncfile_out = netCDF4.Dataset('./outfile.nc', 'w')
ncfile_out.createDimension('time', ntim)
ncfile_out.createDimension('lat', nlat)
ncfile_out.createDimension('lon', nlon)
var_out = ncfile_out.createVariable('variable', 'f4', ('time', 'lat', 'lon',))
var_out[:,:,:] = var_diff[:,:,:]
ncfile_out.close()
是我做这类事情的首选工具:
import xarray as xr
# Open the netCDF file
ds = xr.open_dataset('./myfile.nc')
# Take the diff along the time dimension
ds['new_variable'] = ds['variable'].diff(dim='time')
# Write a new file
ds.to_netcdf('outfile.nc')
是我做这类事情的首选工具:
import xarray as xr
# Open the netCDF file
ds = xr.open_dataset('./myfile.nc')
# Take the diff along the time dimension
ds['new_variable'] = ds['variable'].diff(dim='time')
# Write a new file
ds.to_netcdf('outfile.nc')
如果您想要基于CDO的解决方案,有一种较短的方法可以避免循环和写入大量文件:
file=your_file_name.nc # just to keep the code shorter in the following :-)
# calculate number of steps in the file:
nstep=`cdo -s ntime $file`
# do difference between steps 2:n and steps 1:(n-1)
cdo sub -seltimestep,2/$nstep $file -seltimestep,1/`expr $nstep - 1` $file diff.nc
如果您不担心第一个timestep,可以停在这里,否则需要将其解压缩并粘贴到文件的前面:
cdo mergetime -seltimestep,1 $file diff.nc output.nc
您可以尝试将整个事情作为一个单一的管道,尽管它有点混乱(我确实发现过于雄心勃勃的管道可能会导致总线错误)
如果您想要基于CDO的解决方案,有一种较短的方法可以避免循环和写入大量文件:
file=your_file_name.nc # just to keep the code shorter in the following :-)
# calculate number of steps in the file:
nstep=`cdo -s ntime $file`
# do difference between steps 2:n and steps 1:(n-1)
cdo sub -seltimestep,2/$nstep $file -seltimestep,1/`expr $nstep - 1` $file diff.nc
如果您不担心第一个timestep,可以停在这里,否则需要将其解压缩并粘贴到文件的前面:
cdo mergetime -seltimestep,1 $file diff.nc output.nc
您可以尝试将整个事情作为一个单一的管道,尽管它有点混乱(我确实发现过于雄心勃勃的管道可能会导致总线错误)
您是否只对
cdo
解决方案感兴趣?我有一个Python的1-liner,可以帮你做这件事。@jhamman我很有兴趣看到这个1-liner,我不限于cdo
你只对cdo
解决方案感兴趣吗?我有一个Python的1-liner可以帮你完成这项工作。@jhamman我很有兴趣看到这个1-liner,我不限于cdo
有没有一种简单的方法可以覆盖现有文件中的旧值或将新值写入文件的副本?我以前从未使用过Python NetCDF4…感谢您指出变量
拼写错误修复!我已经更新了我的答案,以展示如何将新变量写入新的netcdf文件。有关更多示例,请查看此处的文档:与我的cdo解决方案相比,此版本的速度快得多。是否有一种简单的方法可以覆盖现有文件中的旧值或将新值写入文件副本?我以前从未使用过Python NetCDF4…感谢您指出变量
拼写错误修复!我已经更新了我的答案,以展示如何将新变量写入新的netcdf文件。有关更多示例,请查看此处的文档:与我的cdo解决方案相比,此版本的速度快得多,我对此印象深刻。我喜欢此解决方案,但至少在我的用例中,xarray比numpy慢。我喜欢此解决方案,但至少在我的用例中,xarray比numpy慢