-1

I have downloaded the CMIP5 climate model HadGEM2-CC in a nc file. I know how to extract data for a given longitude and latitude, now I would like to do it over a range of longitude and latitude rather than individually extract data.

How do I do it for a longitude range and latitude range of: 0 to 90 and 50 to 80, respectively? Below is my code:

tcmodel = Dataset(path + '/treeFrac_Lmon_HadGEM2-CC_rcp85_r2i1p1_203012-205511.nc')
print(tcmodel.variables.keys())

lon = tcmodel.variables['lon']
lat = tcmodel.variables['lat']
time = tcmodel.variables['time']
tc = tcmodel.variables['treeFrac']

print(tcmodel.dimensions.items())

lon_array = lon[:]
lat_array = lat[:]
time_array = time[:]
date = datetime.datetime(1959, 12, 1)
tc_array = tc[:,:,:]
time = []

for i in time_array:
    time.append(date + datetime.timedelta(i))

time = np.array(time, dtype = 'datetime64')

k = [245:257]
i = np.abs(lon_array - 90).argmin() #This I would like to change to a range
j = np.abs(lat_array - 80).argmin() #This I would like to change to a range

tc_time = tc_array[k,j,i]

How can I do it for the range of longitude and latitude?

Thomas
  • 441
  • 3
  • 16
  • Does this answer your question? [netcdf4 extract for subset of lat lon](https://stackoverflow.com/questions/29135885/netcdf4-extract-for-subset-of-lat-lon) – ClimateUnboxed Oct 01 '20 at 12:39
  • hi - xarray and standard python solutions at the link above, hope that helps, have voted to close as duplicate. – ClimateUnboxed Oct 01 '20 at 12:41

1 Answers1

0

If you are on Linux or macOS, you could do this using nctoolkit (https://nctoolkit.readthedocs.io/en/latest/).

import nctoolkit as nc
data = nc.open_data(path + '/treeFrac_Lmon_HadGEM2-CC_rcp85_r2i1p1_203012-205511.nc')
data.crop(lon = [0,90], lat = [50,90])
# if you want to save the file, just do this
data.to_nc("outfile.nc")
Robert Wilson
  • 3,192
  • 11
  • 19
  • In that case, it's probably easier to do it in xarray, which could wrap your above code and the clipping into a couple of lines. Too busy to remember the code – Robert Wilson Oct 01 '20 at 11:49