I currently have to do some calculations on a netcdf dataset. For this, I have to apply a function to each non-NaN element.
Here is my current approach:
import xarray as xr
def calc_things(wind_speed):
if np.isnan(wind_speed):
return np.nan
if wind_speed<3:
return 0
if wind_speed>11.3:
return 100
# do math stuff here
if __name__ == "__main__":
with xr.open_dataset("input.nc") as df:
wind_data = df['sfcWind']
wind_applied = xr.apply_ufunc(calc_things, wind_data, vectorize=True)
ds.to_netcdf("modified.nc")
Can somebody tell me if this is the right way to do it? I am also wondering if there is a better way to handle the NaN values instead of having an if-statement in the function.
All help is appreciated.
asked Jun 13, 2025 at 13:31
ATYslh
981 gold badge2 silver badges9 bronze badges
-
If this snippet gives expected results, can you elaborate what you mean by the 'right' way to do things?Divyansh Gupta– Divyansh Gupta2025年06月16日 15:34:08 +00:00Commented Jun 16, 2025 at 15:34
-
Just because something works doesn't mean that it is the best way to do it. For example you can append to a numpy array, even though it is not encouraged due to performance.ATYslh– ATYslh2025年06月25日 08:15:29 +00:00Commented Jun 25, 2025 at 8:15
-
Maybe you should try code review. On Stack Overflow we ask specific question on specific problem. Do you have a performance problem for example, sine you mentioned it?chrslg– chrslg2025年07月14日 04:56:24 +00:00Commented Jul 14, 2025 at 4:56
lang-py