Skip to content

Commit

Permalink
Merge pull request #4 from U-S-NRL-Marine-Meteorology-Division/3-uplo…
Browse files Browse the repository at this point in the history
…ad-package-to-pypi

3 upload package to pypi

Closes #3 #5 #6
  • Loading branch information
chadhutchins182 authored Sep 29, 2022
2 parents 2a99356 + d148bf7 commit 8dc2238
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 43 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish to PyPI

on:
push:
#On any tag
tags:
- "*"

jobs:
build-and-push-image:
runs-on: ubuntu-latest

name: Build
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: |
python -m pip install --upgrade pip
python setup.py sdist
- name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_TOKEN }}
# - name: Publish package to TestPyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# verbose: true
# password: ${{ secrets.TEST_PYPI_TOKEN }}
# repository_url: https://test.pypi.org/legacy/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
22 changes: 22 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# file GENERATED by distutils, do NOT edit
setup.cfg
setup.py
xnrl\__init__.py
xnrl\cli.py
xnrl\compute.py
xnrl\constant.py
xnrl\flatfile.py
xnrl\generic.py
xnrl\grads.py
xnrl\grib.py
xnrl\hdf5.py
xnrl\internal.py
xnrl\io.py
xnrl\main.py
xnrl\netcdf.py
xnrl\packages.py
xnrl\plot.py
xnrl\select.py
xnrl\tutorial.py
xnrl\util.py
xnrl\visual.py
15 changes: 15 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Things to always exclude
global-exclude .git*
global-exclude .ipynb_checkpoints
global-exclude *.py[co]
global-exclude __pycache__/**

# Top-level Config
include DISTRIBUTION
include LICENSE
include README.md
include VERSION
include MANIFEST.in
include setup.cfg
include environment.yml
include environment_xnrl.yml
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
align="center"
>
# <p style="text-align:center"><img src="images/logo.png" width=250>
# <p style="text-align:center"><img src="https://github.com/U-S-NRL-Marine-Meteorology-Division/xnrl/blob/main/images/logo.png?raw=true" width=250>

<!-- Badges -->

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) ![PyPI](https://img.shields.io/pypi/v/xnrl) [![Publish to PyPI](https://github.com/U-S-NRL-Marine-Meteorology-Division/xnrl/actions/workflows/publish-to-pypi.yml/badge.svg)](https://github.com/U-S-NRL-Marine-Meteorology-Division/xnrl/actions/workflows/publish-to-pypi.yml)

<!-- (Badges) -->
</div>
Expand All @@ -34,6 +34,11 @@
# Install
xNRL requires numpy, pandas, and xarray.

## Install with pip
```bash
pip install xnrl
```

## Clone and install with pip
```bash
git clone https://github.com/U-S-NRL-Marine-Meteorology-Division/xnrl.git
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022.06.13
2022.09.29
11 changes: 7 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@

'''Python setup script for xnrl package'''

from os.path import realpath, join, dirname
from setuptools import setup
from pathlib import Path

with open(join(dirname(realpath(__file__)), 'VERSION'), encoding='utf-8') as version_file:
version = version_file.read().strip()
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()
version = (this_directory / "VERSION").read_text()

setup(
name="xnrl",
version=version,
description="xNRL - Read NRL files into xarray Datasets nested within pandas DataFrames",
long_description=long_description,
long_description_content_type='text/markdown',
packages=["xnrl"],
include_package_data=True,
install_requires=["numpy", "xarray==0.16.2", "dask[delayed]", "metpy"],
install_requires=[ "xarray", "numpy", "dask[delayed]", "metpy"],
keywords=["flatfile", "nrl", "xarray", "dataset", "binary", "hdf5", "grib"],
entry_points={
"console_scripts": [
Expand Down
14 changes: 9 additions & 5 deletions xnrl/internal.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# # Distribution Statement A. Approved for public release. Distribution unlimited.
# #
# #
# # Author:
# # Naval Research Laboratory, Marine Meteorology Division
# #
# #
# # This program is free software:
# # you can redistribute it and/or modify it under the terms
# # of the NRLMMD License included with this program.
# # If you did not receive the license, see
# # https://github.com/U-S-NRL-Marine-Meteorology-Division/
# # for more information.
# #
# #
# # This program is distributed WITHOUT ANY WARRANTY;
# # without even the implied warranty of MERCHANTABILITY
# # or FITNESS FOR A PARTICULAR PURPOSE.
Expand Down Expand Up @@ -957,8 +957,12 @@ def open_dataset(self, paths):
f"Sub-selected for {self._lev_type} "
f"{self._grid_dim} successfully!"
)
ds = ds.transpose(*C.BASE_DIMS + [...])

if "missing_dims" in xr.Dataset.transpose.__code__.co_varnames:
# Xarray 0.16 ignored by default. New versions raise exception by default.
ds = ds.transpose(*C.BASE_DIMS + [...], missing_dims="ignore")
else:
# Backwards Compatible for transpose.
ds = ds.transpose(*C.BASE_DIMS + [...])
num_groups = selc_groupby.ngroups
if self.max_groups is not None and self.max_groups < num_groups:
num_groups = self.max_groups
Expand Down
58 changes: 40 additions & 18 deletions xnrl/io.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# # Distribution Statement A. Approved for public release. Distribution unlimited.
# #
# #
# # Author:
# # Naval Research Laboratory, Marine Meteorology Division
# #
# #
# # This program is free software:
# # you can redistribute it and/or modify it under the terms
# # of the NRLMMD License included with this program.
# # If you did not receive the license, see
# # https://github.com/U-S-NRL-Marine-Meteorology-Division/
# # for more information.
# #
# #
# # This program is distributed WITHOUT ANY WARRANTY;
# # without even the implied warranty of MERCHANTABILITY
# # or FITNESS FOR A PARTICULAR PURPOSE.
Expand Down Expand Up @@ -295,26 +295,49 @@ def export_flatfile(df, out_dir=C.WORK_DIR, show=False, fmt=None, sigma=False):
else:
metadata["grid_dim"] = f"glob{num_x:03d}x{num_y:03d}"

ds_pre = (
ds.expand_dims(expand_dims)
.drop(["sig_w", "sig_m"], errors="ignore")
.stack(**{"dims_": stack_dims})
.transpose("dims_", *grid_dims)
)
if "missing_dims" in xr.Dataset.transpose.__code__.co_varnames:
# Xarray 0.16 ignored by default. New versions raise exception by default.
ds_pre = (
ds.expand_dims(expand_dims)
.drop_vars(["sig_w", "sig_m"], errors="ignore")
.stack(**{"dims_": stack_dims})
.transpose("dims_", *grid_dims, missing_dims="ignore")
)
else:
# Backwards Compatible for transpose.
ds_pre = (
ds.expand_dims(expand_dims)
.drop_vars(["sig_w", "sig_m"], errors="ignore")
.stack(**{"dims_": stack_dims})
.transpose("dims_", *grid_dims)
)

out_fps = set([])
missing_fps = set([])
for field in ds_pre.data_vars:
field_name = field.replace("_", "")
metadata.update(**{"field": field_name})
for slice_ in ds_pre[field]:
slice_ = slice_.transpose(
*[
dim
for dim in C.BASE_DIMS + C.GRID_DIMS + C.XY_DIMS
if dim in slice_.dims
]
)
if "missing_dims" in xr.Dataset.transpose.__code__.co_varnames:
# Xarray 0.16 ignored by default. New versions raise exception by default.
slice_ = slice_.transpose(
*[
dim
for dim in C.BASE_DIMS + C.GRID_DIMS + C.XY_DIMS
if dim in slice_.dims
],
missing_dims="ignore",
)
else:
# Backwards Compatible for transpose.
slice_ = slice_.transpose(
*[
dim
for dim in C.BASE_DIMS + C.GRID_DIMS + C.XY_DIMS
if dim in slice_.dims
]
)

try:
exp, mbr, ini, tau, lev = np.atleast_1d(slice_["dims_"].values)[0]
except ValueError:
Expand All @@ -326,8 +349,7 @@ def export_flatfile(df, out_dir=C.WORK_DIR, show=False, fmt=None, sigma=False):
seconds = tau
mm, ss = divmod(seconds, 60)
hh, mm = divmod(mm, 60)

mbr = mbr if mbr != C.MASK_VALUE else 0
mbr = mbr if mbr != C.MASK_VALUE and np.isnan(mbr) == False else 0
metadata.update(
**{"mbr": mbr, "ini": ini, "hh": hh, "mm": mm, "ss": ss, "lev": lev}
)
Expand Down
16 changes: 11 additions & 5 deletions xnrl/plot.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# # Distribution Statement A. Approved for public release. Distribution unlimited.
# #
# #
# # Author:
# # Naval Research Laboratory, Marine Meteorology Division
# #
# #
# # This program is free software:
# # you can redistribute it and/or modify it under the terms
# # of the NRLMMD License included with this program.
# # If you did not receive the license, see
# # https://github.com/U-S-NRL-Marine-Meteorology-Division/
# # for more information.
# #
# #
# # This program is distributed WITHOUT ANY WARRANTY;
# # without even the implied warranty of MERCHANTABILITY
# # or FITNESS FOR A PARTICULAR PURPOSE.
Expand All @@ -24,6 +24,7 @@
import numpy as np
import pandas as pd
import xnrl.constant as C
import xarray as xr
from mpl_toolkits.axes_grid1 import make_axes_locatable
from xnrl import compute, util

Expand Down Expand Up @@ -222,7 +223,12 @@ def _plot(
ax.grid(color="gray", alpha=0.3)
else:
if y in da_ovl.dims and x in da_ovl.dims:
da_ovl = da_ovl.transpose(y, x)
if("missing_dims" in xr.Dataset.transpose.__code__.co_varnames):
# Xarray 0.16 ignored by default. New versions raise exception by default.
da_ovl = da_ovl.transpose(y, x, missing_dims="ignore")
else:
#Backwards Compatible for transpose.
da_ovl = da_ovl.transpose(y, x)

plot_args = (da_ovl[x], da_ovl[y], da_ovl.values)
plot_kwds = dict(transform=transform, shading="auto")
Expand Down Expand Up @@ -508,4 +514,4 @@ def visualize(
ds, da.sel(**{sep: val}), *plot_args, sep=sep, val=val, **plot_kwds
)
else:
return _plot(ds, da, *plot_args, **plot_kwds)
return _plot(ds, da, *plot_args, **plot_kwds)
37 changes: 29 additions & 8 deletions xnrl/util.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# # Distribution Statement A. Approved for public release. Distribution unlimited.
# #
# #
# # Author:
# # Naval Research Laboratory, Marine Meteorology Division
# #
# #
# # This program is free software:
# # you can redistribute it and/or modify it under the terms
# # of the NRLMMD License included with this program.
# # If you did not receive the license, see
# # https://github.com/U-S-NRL-Marine-Meteorology-Division/
# # for more information.
# #
# #
# # This program is distributed WITHOUT ANY WARRANTY;
# # without even the implied warranty of MERCHANTABILITY
# # or FITNESS FOR A PARTICULAR PURPOSE.
Expand Down Expand Up @@ -516,8 +516,14 @@ def regrid(
if "idx" in ds1.dims:
ds1 = ds1.dropna("idx")

ds1 = ds1.transpose(*[..., "lat", "lon"])
ds2 = ds2.transpose(*[..., "lat", "lon"])
if "missing_dims" in xr.Dataset.transpose.__code__.co_varnames:
# Xarray 0.16 ignored by default. New versions raise exception by default.
ds1 = ds1.transpose(*[..., "lat", "lon"], missing_dims="ignore")
ds2 = ds2.transpose(*[..., "lat", "lon"], missing_dims="ignore")
else:
# Backwards Compatible for transpose.
ds1 = ds1.transpose(*[..., "lat", "lon"])
ds2 = ds2.transpose(*[..., "lat", "lon"])

if locstream_in and method in ["bilinear", "nearest"]:
method = "nearest_s2d"
Expand Down Expand Up @@ -646,10 +652,21 @@ def _interp_rebuild(da, var_y, var_x, data_list, dims, coords, to_irregular=Fals
base_dims = ["tmp_dim", var_y, var_x]

da_interp = xr.DataArray(
data_list, coords=coords, dims=base_dims, name=da.name, attrs=da.attrs,
data_list,
coords=coords,
dims=base_dims,
name=da.name,
attrs=da.attrs,
)
if len(dims) > 0:
da_interp = da_interp.unstack("tmp_dim").transpose(*dims + base_dims[1:])
if "missing_dims" in xr.Dataset.transpose.__code__.co_varnames:
# Xarray 0.16 ignored by default. New versions raise exception by default.
da_interp = da_interp.unstack("tmp_dim").transpose(
*dims + base_dims[1:], missing_dims="ignore"
)
else:
# Backwards Compatible for transpose.
da_interp = da_interp.unstack("tmp_dim").transpose(*dims + base_dims[1:])
else:
da_interp = da_interp.squeeze("tmp_dim")
return da_interp
Expand Down Expand Up @@ -711,7 +728,11 @@ def _cross_section_2d(ds, var_y, var_x, ys, xs, method, num_steps):

coords = np.concatenate((ys, xs))
ds_cross = interpolate.cross_section(
ds, coords[:, 0], coords[:, 1], steps=num_steps, interp_type=method,
ds,
coords[:, 0],
coords[:, 1],
steps=num_steps,
interp_type=method,
).rename({"index": "idx"})
remove_nan_list = [
idx for idx in ds_cross["idx"].values if not np.isnan(ds_cross[var_y][idx])
Expand Down

0 comments on commit 8dc2238

Please sign in to comment.