3

I have a netCDF file with a grid (each step 0.25°). What I want is the value of the variable, lets say tempMax, at a certain gridpoint, over the last 50 years.

I am aware that you read the data into python like this

lon = numpy.array(file.variables['longitude'][:])
lat = numpy.array(file.variables['latitude'][:])
temp = numpy.array(file.variables['tempMax'][:])
time = numpy.array(file.variables['time'][:])

That leaves me with an array and I do not know how to "untangle" it. How to get the value at a certain coordinate (stored in temp) over the whole time (stored in time)? S display is the value over the time at the certain coordinate.

Any ideas how I could achieve that?

Thanks!

1 Answer 1

6

I'm guessing that tempMax is 3D (time x lat x lon) and should then be read in as

temp = ncfile.variables['tempMAx'][:,:,:]

(Note two things: (1) if you're using Python v2, it's best to avoid the word file and instead use something like ncfile as shown above, (2) temp will be automatically stored as a numpy.ndarray simply with the call above, you don't need to use the numpy.array() command during the read in of variables.)

Now you can extract temperatures for all times at a certain location with

temp_crd = temp[:,lat_idx,lon_idx] 

where lat_idx and lon_idx are integers corresponding to the index of the latitude and longitude coordinates. If you know these indices beforehand, great, just plug them in, e.g. temp_crd = temp[:,25,30]. (You can use the tool ncdump to view the contents of a netCDF file, https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/ncdump.html)

The more likely case is that you know the coordinates, but not their indices beforehand. Let's say you want temperatures at 50N and 270E. You can use the numpy.where function to extract the indices of the coordinates given the lat and lon arrays that you've already read in.

lat_idx = numpy.where(lat==50)[0][0]
lon_idx = numpy.where(lon==270)[0][0]

tmp_crd = temp[:,lat_idx,lon_idx]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.