0

In python 3.6 I have imported a netCDF4 file containing global precipitation values. I have also imported a shapefile which contains the shape for the Colorado River basin. My goal is to be able to read/extract precipitation data only within my shapefile. I have looked up multiple examples but none have really helped.

Here is my code so far:

from netCDF4 import Dataset
import numpy as np
import geopandas as gpd

nc = Dataset('filename.nc')
long = nc.variables['lon'][:]
lati = nc.variables['lat'][:]
rainfall = nc.variables['precip'][:]

shapefile=gpd.read_file('filename.shp')

There are no error messages on the code above.

CPG
  • 97
  • 2
  • 15
  • From what i gather, you're trying to analyze data from your nc dataset that is within the colorado river basin (as defined by your shape file). Is that correct? – Bob Haffner Jun 10 '17 at 20:09

1 Answers1

1

Oh, look, hydrologist in the house! ;)

Well, so far you haven't done much with your code, all you did was read files into memory.

When I was trying to perform the same analysis (only with grib files), I found a great Python library for exactly such purpose, called RasterStats.

It supports working with ndarray raster objects as well as most of the GDAL supported raster filetypes (must be netCDF also!), and it generates exactly the thing you want.

For more, see a very neat manual and let me know if you get stuck somewhere!

Marjan Moderc
  • 2,747
  • 23
  • 44