# pyscissor
![Supported Version](https://img.shields.io/badge/python-3.6|3.7|3.8-blue.svg)
![Action: Build](https://github.com/nzahasan/pyscissor/workflows/build/badge.svg)
![publish](https://github.com/nzahasan/pyscissor/workflows/publish/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
A Python3 module for extracting data from netcdf file under a shapefile region.
<img src="data/sample.png" height="500" align="center">
### Installation
pyscissor can be installed using the following commands
```bash
$ git clone https://github.com/nzahasan/pyscissor.git
$ cd pyscissor
$ python3 setup.py install
```
or using pip
```bash
$ pip install pyscissor
```
### Using pyscissor
```python
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
- <a href="notebooks/example_01.ipynb">example_01.ipynb</a>
- <a href="notebooks/example_02.ipynb">example_02.ipynb</a>
- <a href="notebooks/example_03.ipynb">example_03.ipynb</a>
- <a href="notebooks/example_04.ipynb">example_04.ipynb</a>
### 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
```bash
# 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
Raw data
{
"_id": null,
"home_page": "https://github.com/nzahasan/pyscissor",
"name": "pyscissor",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "netcdf,crop,shapefile",
"author": "nzahasan",
"author_email": "nzahasan@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/54/13/3f339b50256e1265971655ed2113a5b9da4876425ed88c09f802bd4d55af/pyscissor-1.1.7.tar.gz",
"platform": null,
"description": "# pyscissor\n![Supported Version](https://img.shields.io/badge/python-3.6|3.7|3.8-blue.svg) \n![Action: Build](https://github.com/nzahasan/pyscissor/workflows/build/badge.svg)\n![publish](https://github.com/nzahasan/pyscissor/workflows/publish/badge.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) \nA Python3 module for extracting data from netcdf file under a shapefile region. \n\n<img src=\"data/sample.png\" height=\"500\" align=\"center\">\n\n\n### Installation\n\npyscissor can be installed using the following commands\n\n```bash\n$ git clone https://github.com/nzahasan/pyscissor.git\n$ cd pyscissor\n$ python3 setup.py install\n```\nor using pip\n\n```bash\n$ pip install pyscissor\n```\n\n### Using pyscissor\n\n```python\nimport fiona\nimport numpy as np\nfrom netCDF4 import Dataset\nfrom shapely.geometry import shape\nfrom pyscissor import scissor \n\n\n# read shapefile\nsf = fiona.open('data/shape.geojson')\nshapely_shp =shape(sf.get(0)['geometry'])\n\n\n# read netcdf\nnf = Dataset('data/sample_2.nc','r')\nlats = nf.variables['lat'][:]\nlons = nf.variables['lon'][:]\n\n# create scissor object \npys = scissor(shapely_shp,lats,lons)\n\nweight_grid = pys.get_masked_weight() #=> returns a masked array containing weights\n\n# get weighted average\navg = np.average(var,weights=weight_grid)\n\n# if only intersection mask with shape is needed use `weight_grid.mask`\n```\nA detailed use case can be found in the following jupyter notebooks \n- <a href=\"notebooks/example_01.ipynb\">example_01.ipynb</a>\n- <a href=\"notebooks/example_02.ipynb\">example_02.ipynb</a>\n- <a href=\"notebooks/example_03.ipynb\">example_03.ipynb</a>\n- <a href=\"notebooks/example_04.ipynb\">example_04.ipynb</a>\n\n\n\n### Using nc2ts_by_shp.py\nthis package contains a `nc2ts_by_shp.py` script. A command line tool that can be used to quickly extract \nreduced(min/max/average/weighted average) time-series form netcdf file with shapefile\n\n```bash\n# with 3d array [data/sample_2.nc] generel case\n$ nc2ts_by_shp.py -nc=sample_2.nc -nci='Y=lat;X=lon;T=time;V=tmin;' -s=shape_esri.zip \\\n\t\t-sp='ADM2_EN;ADM3_EN' -r=avg -o=test2.csv\n\n# with 4d array [data/sample_1.nc]\n$ nc2ts_by_shp.py -nc=sample_1.nc -nci='Y=lat;X=lon;T=time;V=temperature;slicer=[:,0,:,:]' -sf=shape_esri.zip \\\n\t\t-sfp='ADM2_EN;ADM3_EN' -r=wavg -o=test1.csv\n\n```\nOptions:\n\n\t-nc = netcdf file\n\n\t-nci = netcdf variable and dimension information\n\t\t\tavailable options:\n\t\t\tX = x dimension variable name,\n\t\t\tY = y dimension variable name,\n\t\t\tT = time dimension variable name,\n\t\t\tV = variable name,\n\t\t\tslicer = slicing index for obtaining 3d array [optional]\n\t\t\t\t\t\n\t\t\tnote: `slicer` is required if variable has more than three dimension\n\n\t-sf = shape file ( can be zipped shapefile, shapefile or geojson )\n\n\t-sfp = shapefile properties\n\t\t\tonly required when shapefile contains multiple records\n\n\t-r = reducer, default is average\n\t\t\tAvailable options: min,max,avg,wavg\n\n\t-o = output file name\n\n\n### Causes of Erroneous output\n\n\t- when shapefile and netcdf file have different projection\n\t- shapefile dosen't fully reside within netcdf bounds \n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A python module for obtaining reduced(min,max,avg) value from netCDF file under a polygon region",
"version": "1.1.7",
"project_urls": {
"Homepage": "https://github.com/nzahasan/pyscissor"
},
"split_keywords": [
"netcdf",
"crop",
"shapefile"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5e3eb750f13e2722f7c44281713f96d2d52732ad81b07b2b7a962caff590a31d",
"md5": "9412140cc0228de18b2f800330955a76",
"sha256": "6211f803859cefd01a6261e854299349dcac43d7310e44c81d69dbf67b4b6439"
},
"downloads": -1,
"filename": "pyscissor-1.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9412140cc0228de18b2f800330955a76",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 12444,
"upload_time": "2023-07-15T13:52:58",
"upload_time_iso_8601": "2023-07-15T13:52:58.538469Z",
"url": "https://files.pythonhosted.org/packages/5e/3e/b750f13e2722f7c44281713f96d2d52732ad81b07b2b7a962caff590a31d/pyscissor-1.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "54133f339b50256e1265971655ed2113a5b9da4876425ed88c09f802bd4d55af",
"md5": "951c7b26cfc934b08fceef697a83a7dc",
"sha256": "5c197ff61dfc97b237219f28114d57244ade5d2d54614df64662520b4caa2676"
},
"downloads": -1,
"filename": "pyscissor-1.1.7.tar.gz",
"has_sig": false,
"md5_digest": "951c7b26cfc934b08fceef697a83a7dc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 9999,
"upload_time": "2023-07-15T13:52:59",
"upload_time_iso_8601": "2023-07-15T13:52:59.992366Z",
"url": "https://files.pythonhosted.org/packages/54/13/3f339b50256e1265971655ed2113a5b9da4876425ed88c09f802bd4d55af/pyscissor-1.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-15 13:52:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nzahasan",
"github_project": "pyscissor",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "pyscissor"
}