Skip to content

Commit

Permalink
fixed installation script
Browse files Browse the repository at this point in the history
  • Loading branch information
domeniconappo committed Jun 12, 2019
1 parent 3b97c42 commit 288d66f
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 53 deletions.
1 change: 1 addition & 0 deletions pcr2nc/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include VERSION
include pcr2nc_script.py
40 changes: 22 additions & 18 deletions pcr2nc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Ensure you have properly installed the following software:
- GDAL C library and software
- netCDF4 C library

Create a python3.5 virtualenv for using the software and activate it.
Create a python3 virtualenv for using the software and activate it.

If you have virtualenvwrapper:
```bash
Expand All @@ -32,6 +32,27 @@ Otherwise just execute the activate script
$ source /path/to/virtualenvs/pcr2nc/bin/activate
```

### Install via pip tool

Activate the virtualenv and then:

```bash
$ pip install pcr2nc
```

After the install was complete, you still have to install the proper GDAL package,
according to the version of gdal library that is installed on your machine.
You will also need C GDAL headers to properly install python GDAL wrapper.

E.g.

```bash
sudo apt-get install libgdal-dev libgdal
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
gdal-config --version # 2.2.3
pip install GDAL==2.2.3
```

### Install by cloning the repository

Expand Down Expand Up @@ -65,23 +86,6 @@ $ pip install GDAL==2.1
$ pip install -r requirements.txt
```

### Install via pip tool

Activate the virtualenv and then:

```bash
$ pip install pcr2nc
```

After the install was complete, you still have to install the proper GDAL package,
according to the version of gdal library that is installed on your machine.

E.g.

```bash
$ pip install GDAL==2.1
```

## Usage

> __Note:__ This guide assumes you have installed the program with pip tool.
Expand Down
2 changes: 1 addition & 1 deletion pcr2nc/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5
0.6
6 changes: 6 additions & 0 deletions pcr2nc/bin/pcr2nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

from pcr2nc.main import main_script

if __name__ == '__main__':
main_script()
65 changes: 65 additions & 0 deletions pcr2nc/pcr2nc/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
pcr2nc
A tool that convert PCRaster maps to a netCDF4 mapstack CF-1.7
"""
import sys
import argparse
import json

import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper

from pcr2nc.writer import convert


class ParserHelpOnError(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)

def add_args(self):
required_group = self.add_argument_group('required arguments')
required_group.add_argument('-m', '--metadata', help='Path to json/yaml metadata file for NetCDF',
metavar='metadata', required=True)
required_group.add_argument('-i', '--input', required=True,
help='Path to input dataset. It can be a single PCRaster map, '
'a folder containing PCRaster maps or a path with wildcards '
'to use with glob.glob python function',
metavar='input')
self.add_argument('-o', '--output_file', help='Path to netcdf output file.', default='./file.nc',
metavar='output_file')


def parse_metadata(metadata_file):
if metadata_file.endswith('.yaml') or metadata_file.endswith('.yml'):
with open(metadata_file) as f:
metadata = yaml.load(f, Loader=Loader)
else:
# suppose json format
with open(metadata_file) as f:
metadata = json.load(f)
return metadata


def main_script():
sys.exit(main(sys.argv[1:]))


def main(args):
parser = ParserHelpOnError(description='Convert PCRaster maps to a NetCDF map stack')

parser.add_args()
parsed_args = parser.parse_args(args)
configuration = {'input_set': parsed_args.input,
'output_filename': parsed_args.output_file,
'metadata': parse_metadata(parsed_args.metadata)}
# MAIN METHOD
convert(configuration)


if __name__ == '__main__':
main_script()
18 changes: 16 additions & 2 deletions pcr2nc/pcr2nc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@
import os
import glob
import re
import sys

from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
import numpy as np

try:
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
except (ModuleNotFoundError, ImportError) as e:
print("""
[!] GDAL is not installed. Please, install GDAL binaries and libraries for your system and then install the relative pip package.
[!] Important note: you have to install same version of GDAL for its python interface. You also need to install GDAL C headers.
[!] sudo apt-get install libgdal-dev libgdal
[!] export CPLUS_INCLUDE_PATH=/usr/include/gdal
[!] export C_INCLUDE_PATH=/usr/include/gdal
[!] To know your gdal version execute `gdal-config --version` ex. 2.2.3
[!] pip install GDAL==2.2.3
""")
sys.exit(1)


class PCRasterMap:
"""
Expand Down
64 changes: 32 additions & 32 deletions pcr2nc/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import os
from setuptools import setup, find_packages


packages_deps = ['numpy>=1.15', 'pyyaml', 'netCDF4>=1.3.1']
current_dir = os.path.dirname(os.path.abspath(__file__))
readme_file = os.path.join(current_dir, 'README.md')
version_file = os.path.join(current_dir, 'VERSION')
Expand All @@ -44,36 +42,38 @@
with open(version_file, 'r') as f:
version = f.read()

setup_args = dict(name='pcr2nc',
version=version,
packages=find_packages(),
description='Convert PCRaster files to netCDF4',
long_description=long_description,
long_description_content_type='text/markdown',
install_requires=packages_deps,
author="Domenico Nappo",
author_email="domenico.nappo@gmail.com",
keywords=['netCDF4', 'PCRaster', 'mapstack'],
license='EUPL 1.2',
url='https://github.com/ec-jrc/lisflood-model',
entry_points={'console_scripts': ['pcr2nc = pcr2nc_script:main_script']},
zip_safe=True,
classifiers=[
# complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: Other Audience',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: European Union Public Licence 1.2 (EUPL 1.2)',
'Operating System :: Unix',
'Operating System :: POSIX',
'Operating System :: Microsoft :: Windows',
'Operating System :: MacOS :: MacOS X',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Physics',
],
setup_args = dict(
name='pcr2nc',
version=version,
packages=find_packages(),
description='Convert PCRaster files to netCDF4',
long_description=long_description,
long_description_content_type='text/markdown',
install_requires=['numpy>=1.15', 'pyyaml', 'netCDF4>=1.3.1'],
author="Domenico Nappo",
author_email="domenico.nappo@gmail.com",
keywords=['netCDF4', 'PCRaster', 'mapstack'],
license='EUPL 1.2',
url='https://github.com/ec-jrc/lisflood-model',
# entry_points={'console_scripts': ['pcr2nc = pcr2nc_script:main_script']},
scripts=['bin/pcr2nc'],
zip_safe=True,
classifiers=[
# complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: Other Audience',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: European Union Public Licence 1.2 (EUPL 1.2)',
'Operating System :: Unix',
'Operating System :: POSIX',
'Operating System :: Microsoft :: Windows',
'Operating System :: MacOS :: MacOS X',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Physics',
],
)

setup(**setup_args)

0 comments on commit 288d66f

Please sign in to comment.