Skip to main content

A python module for obtaining reduced(min,max,avg) value from netCDF file under a polygon region

Project description

pyscissor

Supported Version Action: Build publish License: MIT
A Python3 module for extracting data from netcdf file under a shapefile region.

Installation

pyscissor can be installed using the following commands

$ git clone https://github.com/nzahasan/pyscissor.git
$ cd pyscissor
$ python3 setup.py install

or using pip

$ pip install pyscissor

Using pyscissor

import fiona
import numpy as np
from netCDF4 import Dataset
from shapely.geometry import shape
from pyscissor import scissor 


# read shapefile
sf = fiona.open('data/shape.geojson')
shapely_shp =shape(sf.get(0)['geometry'])


# read netcdf
nf = Dataset('data/sample_2.nc','r')
lats = nf.variables['lat'][:]
lons = nf.variables['lon'][:]

# create scissor object 
pys = scissor(shapely_shp,lats,lons)

weight_grid = pys.get_masked_weight() #=> returns a masked array containing weights

# get weighted average
avg = np.average(var,weights=weight_grid)

# if only intersection mask with shape is needed use `weight_grid.mask`

A detailed use case can be found in the following jupyter notebooks

Using nc2ts_by_shp.py

this package contains a nc2ts_by_shp.py script. A command line tool that can be used to quickly extract reduced(min/max/average/weighted average) time-series form netcdf file with shapefile

# with 3d array [data/sample_2.nc] generel case
$ nc2ts_by_shp.py -nc=sample_2.nc -nci='Y=lat;X=lon;T=time;V=tmin;' -s=shape_esri.zip \
		-sp='ADM2_EN;ADM3_EN' -r=avg -o=test2.csv

# with 4d array [data/sample_1.nc]
$ nc2ts_by_shp.py -nc=sample_1.nc -nci='Y=lat;X=lon;T=time;V=temperature;slicer=[:,0,:,:]' -sf=shape_esri.zip \
		-sfp='ADM2_EN;ADM3_EN' -r=wavg -o=test1.csv

Options:

-nc  = netcdf file

-nci = netcdf variable and dimension information
		available options:
		X = x dimension variable name,
		Y = y dimension variable name,
		T = time dimension variable name,
		V = variable name,
		slicer = slicing index for obtaining 3d array [optional]
				
		note: `slicer` is required if variable has more than three dimension

-sf  = shape file ( can be zipped shapefile, shapefile or geojson )

-sfp = shapefile properties
		only required when shapefile contains multiple records

-r   = reducer, default is average
		Available options: min,max,avg,wavg

-o   = output file name

Causes of Erroneous output

- when shapefile and netcdf file have different projection
- shapefile dosen't fully reside within netcdf bounds 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyscissor-1.1.7.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

pyscissor-1.1.7-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file pyscissor-1.1.7.tar.gz.

File metadata

  • Download URL: pyscissor-1.1.7.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyscissor-1.1.7.tar.gz
Algorithm Hash digest
SHA256 5c197ff61dfc97b237219f28114d57244ade5d2d54614df64662520b4caa2676
MD5 951c7b26cfc934b08fceef697a83a7dc
BLAKE2b-256 54133f339b50256e1265971655ed2113a5b9da4876425ed88c09f802bd4d55af

See more details on using hashes here.

File details

Details for the file pyscissor-1.1.7-py3-none-any.whl.

File metadata

  • Download URL: pyscissor-1.1.7-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyscissor-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6211f803859cefd01a6261e854299349dcac43d7310e44c81d69dbf67b4b6439
MD5 9412140cc0228de18b2f800330955a76
BLAKE2b-256 5e3eb750f13e2722f7c44281713f96d2d52732ad81b07b2b7a962caff590a31d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page