Commit 0bf6d8d5 authored by Philipp Sommer's avatar Philipp Sommer

Initial commit

parent 6f9b9521
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
docs/index.doctree
docs/examples/
docs/api/
# PyBuilder
target/
======================================================
ravel-dataset: Simple script to ravel a netCDF dataset
======================================================
Installation with admin privileges
-----------------------------------
```bash
python -m pip install ./
```
Installation and usage without admin privileges
------------------------------------------------
```bash
python -m pip install --user ./
```
import xarray as xr
import argparse
from itertools import chain
def main():
parser = get_parser()
args = parser.parse_args()
out = ravel_dataset(args.infile, args.variables, args.dim, True)
out.to_netcdf(args.outfile)
def get_parser():
parser = argparse.ArgumentParser(
description="Ravel multi-dimensional variables into 1D")
parser.add_argument('infile', help='Input netCDF file',
type=xr.open_dataset)
parser.add_argument('outfile', help='Output netCDF file')
parser.add_argument(
'-v', '--variables', nargs='+', metavar='variable',
help=("Variables to ravel. If not specified, "
"all variables are ravelled"))
parser.add_argument(
'-d', '--dim', help=("Name of the resulting dimension. If not "
"specified, the ravelled dimensions separated by "
"an underscore will be used"))
return parser
def ravel_dataset(ds, variables=None, dim=None, reset_index=False):
if variables is None:
variables = [v for v in ds.variables
if v not in ds.coords and ds[v].ndim > 1]
dimensions = []
for v in variables:
dimensions.extend([d for d in ds[v].dims if d not in dimensions])
if dim is None:
dim = '_'.join(dimensions)
ret = ds.stack(**{dim: dimensions})
return ret.reset_index(dim) if reset_index else ret
if __name__ == '__main__':
main()
from setuptools import setup
from setuptools.command.test import test as TestCommand
import sys
class PyTest(TestCommand):
user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = ''
def run_tests(self):
import shlex
# import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(shlex.split(self.pytest_args))
sys.exit(errno)
def readme():
with open('README.rst') as f:
return f.read()
setup(name='ravel-dataset',
version='1.0.0',
description='Simple script to ravel a netCDF dataset',
long_description=readme(),
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Scientific/Engineering :: Visualization',
'Topic :: Scientific/Engineering :: GIS',
'Topic :: Scientific/Engineering',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Operating System :: OS Independent',
],
keywords='netcdf raster earth-sciences',
url='https://github.com/psyplot/psyplot',
author='Philipp Sommer',
author_email='philipp.sommer@hzg.de.ch',
license="GPLv3",
py_modules=["ravel_ds"],
install_requires=[
'xarray',
'netcdf4'
],
tests_require=['pytest'],
cmdclass={'test': PyTest},
entry_points={'console_scripts': [
'ravel_ds=ravel_ds:main']},
zip_safe=True)
import sys
import os.path as osp
import subprocess as spr
import xarray as xr
import pytest
@pytest.fixture
def testfile():
return osp.join(test_dir, 'test-t2m-u-v.nc')
@pytest.fixture
def script():
return osp.join(test_dir, '..', 'ravel_ds.py')
test_dir = osp.dirname(__file__)
def test_cli(script, testfile, tmpdir):
ofile = osp.join(tmpdir, 'test.nc')
spr.check_call([sys.executable, script, testfile, ofile])
assert osp.exists(ofile)
with xr.open_dataset(ofile) as dsout:
with xr.open_dataset(testfile) as dsin:
dim = '_'.join(dsin['t2m'].dims)
assert dsout['t2m'].dims == (dim, )
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment