Skip to content

Instantly share code, notes, and snippets.

@nmathewa
Last active May 24, 2021 14:12
Show Gist options
  • Save nmathewa/0bb4aa49b08190aaade74c1289a99f9c to your computer and use it in GitHub Desktop.
Save nmathewa/0bb4aa49b08190aaade74c1289a99f9c to your computer and use it in GitHub Desktop.
Netcdf pytfon subset using numpy
import numpy as np
from netCDF4 import Dataset
nc = "your_file.nc"
dset = Dataset(nc)
lats = dset.variables["lat"][:]
lons = dset.variables["lon"][:]
sub_lat,sub_lon = [10,20],[72,84]
lat_fil = np.where((lats > sub_lat[0]) & (lats < sub_lat[1]))[0]
lon_fil = np.where((lons > sub_lon[0]) & (lons < sub_lon[1]))[0]
nw_subset = dset.variables["ws10"][:,:,lat_fil,lon_fil]
""" The nw_subset is a float array """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment