setting values below a threshold to the threshold in a netcdf file

戏子无情 提交于 2019-12-10 23:05:43

问题


I want to set all values below a constant c to c itself in a netcdf file: file.nc

A solution using climate data operators (CDO) would be

cdo mul -gec,$c file.nc file.nc t1.nc
cdo add -mulc,$c -ltc,$c file.nc t1.nc output.nc
rm -f t1.nc

But is there a neater/shorter way to do this?


回答1:


You can use NCO's ncap2 to do this easily.

For example, set all values of x below 100 to 100 in file.nc and output in file2.nc:

>>> ncap2 -s 'where(x<100.) x=100;' file.nc -O file2.nc 



回答2:


ncap2's clipping operator is most concise:

ncap2 -s 'x=x>>100' in.nc out.nc



回答3:


Using Python with NumPy and netCDF4 you could do the following:

import numpy as np
from netCDF4 import Dataset

dataset = Dataset('/path/to/dataset','r+')

data = dataset.variables['data_variable_name'][:]

threshold = 100 # or whatever your constant c is

# np.where(condition, value if true, value if false)
new_data = np.where(data < threshold, threshold, data)

# Write your new data back to the NetCDF file
dataset.variables['data_variable_name'][:] = new_data[:]

dataset.close()

Good luck!



来源:https://stackoverflow.com/questions/37164297/setting-values-below-a-threshold-to-the-threshold-in-a-netcdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!