I have wind speed NetCDF files for the UK (land and sea areas) I wanted to multiply a constant (say 1.5) by the wind variable for all land areas and a constant (say 2.4) for all sea areas. How can I do that, preferably in Python? If not in Python, what else could I use, as I have a lot of these .nc files and I will loop over them to apply the constants? An example of a .nc file is in the link.



1 Answer 1


You could do it in python by opening the netcdf variables and making them numpy arrays. To multiply a constant by a numpy array from a netcdf is simple e.g. 1.5*array.

It depends on your data and how you can tell which cells are land and which water. Say cells above a threshold are land and below are water, you probably want to use something like an 'if' in loop to say multiple x by cells in the array that are below the threshold, and y by cells that are above the threshold.

To start off in python you want something like

from netCDF4 import Dataset #"onda install -c anaconda netcdf4"
import numpy as np

path = '/Users//Documents/AutoSeaIceProduc2/yourfule.nc'#Path to your  file
NETCDFdata = Dataset(path) #Open your netcdf
NETCDFdata.variables #look at your file
NETCDFdata.variables.keys #look at your file
lats = NETCDFdata.variables['latitude'][:] #these names comes from keys in your file
lons = NETCDFdata.variables['longitude'][:]
time = NETCDFdata.variables['time'][:]
wind = NETCDFdata.variables['winddata'][:,:,:] 


DoubleWind = 2*wind #This would multiple whole array for all time steps by 2. 

Now you have the data in python as arrays to work with

myarray[myarray > 0.7] = 0 #this would make all the values above 0.7 = 0 if that approach helps you distinguish land/sea, for example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.