Skip to content

Commit

Permalink
Merge pull request #29 from ocefpaf/autopublish
Browse files Browse the repository at this point in the history
add pypi autopublish
  • Loading branch information
ocefpaf authored May 24, 2022
2 parents 1370610 + eee0b30 commit 98df2f3
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 34 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: PyPI

on:
pull_request:
push:
branches:
- main
release:
types:
- published

defaults:
run:
shell: bash

jobs:
packages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# Should be enough for setuptools-scm
fetch-depth: 100
persist-credentials: false

- name: Get tags
run: git fetch origin 'refs/tags/*:refs/tags/*'

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

- name: Install build tools
run: |
python -m pip install --upgrade pip build
- name: Build binary wheel
run: python -m build --sdist --wheel . --outdir dist

- name: CheckFiles
run: |
ls -lh dist
- name: Test wheels
run: |
cd dist && python -m pip install odvc*.whl
python -m twine check *
- name: Publish a Python distribution to PyPI
if: success() && github.event_name == 'release'
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_version.py
*.egg-info/
.coverage
.ipynb_checkpoints/
build/
dist/
2 changes: 2 additions & 0 deletions odvc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Ocean Dimensionless Vertical Coordinates."""

try:
from ._version import __version__
except ImportError:
Expand Down
14 changes: 8 additions & 6 deletions odvc/formulas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Ocean Dimensionless Vertical Coordinates formulas."""

import numpy as np


def ocean_double_sigma_coordinate(sigma, depth, z1, z2, a, href, k_c):
"""
Creates a dimensioned version of ocean double sigma.
Create a dimensioned version of ocean double sigma.
Definition:
for k <= k_c
Expand All @@ -24,7 +26,7 @@ def ocean_double_sigma_coordinate(sigma, depth, z1, z2, a, href, k_c):

def ocean_sigma_z_coordinate(sigma, eta, depth, depth_c, nsigma, zlev):
"""
Creates a dimensioned version of ocean sigma over z.
Create a dimensioned version of ocean sigma over z.
Definition:
for k <= nsigma
Expand All @@ -46,7 +48,7 @@ def ocean_sigma_z_coordinate(sigma, eta, depth, depth_c, nsigma, zlev):

def ocean_sigma_coordinate(sigma, eta, depth):
"""
Creates a dimensioned version of ocean sigma coordinate.
Create a dimensioned version of ocean sigma coordinate.
z(n, k, j, i) = eta(n, j, i) + sigma(k) *
(depth(j, i) + eta(n, j, i))
Expand All @@ -57,7 +59,7 @@ def ocean_sigma_coordinate(sigma, eta, depth):

def ocean_s_coordinate(s, eta, depth, a, b, depth_c):
"""
Creates a dimensioned version of ocean s-coordinate.
Create a dimensioned version of ocean s-coordinate.
z(n,k,j,i) = eta(n,j,i)*(1+s(k)) + depth_c*s(k) +
(depth(j,i)-depth_c)*C(k)
Expand All @@ -75,7 +77,7 @@ def ocean_s_coordinate(s, eta, depth, a, b, depth_c):

def ocean_s_coordinate_g1(s, c, eta, depth, depth_c):
"""
Creates a dimensioned version of ocean s-coordinate generic form 1.
Create a dimensioned version of ocean s-coordinate generic form 1.
z(n,k,j,i) = S(k,j,i) + eta(n,j,i) * (1 + S(k,j,i) / depth(j,i))
Expand All @@ -89,7 +91,7 @@ def ocean_s_coordinate_g1(s, c, eta, depth, depth_c):

def ocean_s_coordinate_g2(s, eta, depth, depth_c, c):
"""
Creates a dimensioned version of s-coordinate generic form 2.
Create a dimensioned version of s-coordinate generic form 2.
z(n,k,j,i) = eta(n,j,i) + (eta(n,j,i) + depth(j,i)) * S(k,j,i)
Expand Down
38 changes: 15 additions & 23 deletions odvc/utils.py → odvc/parse_formula_terms.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
"""Functions to parse the formula terms and prepare arrays."""

from collections import OrderedDict

import dask
import dask.array as da
import netCDF4

# Remote THREDDS access must be single threaded.
# This should be an option in `odvc` so local files are not penalized too.
dask.config.set(scheduler="single-threaded")


def get_formula_terms(var):
"""
Return a `formula_terms` dict mapping var_names to variables.
The input can be a netCDF variable (`var`) holding the `formula_terms`
attribute or the attribute itself.
Expand All @@ -26,11 +23,7 @@ def get_formula_terms(var):


def get_formula_terms_variables(nc):
"""
Return a list with all variables from the `nc` object that holds the
`formula_terms` attribute.
"""
"""Return a list with all variables that holds the formula_terms attribute."""

def func(v):
return v is not None
Expand All @@ -46,23 +39,15 @@ def func(v):


def get_formula_terms_dims(nc, formula_terms):
"""
Returns an OrderedDict object `dims` holding the `formula_terms` dimensions
listed in the netCDF4-python object `nc`
"""
"""Return an OrderedDict object `dims` holding the `formula_terms` dimensions."""
dims = OrderedDict()
for k, v in formula_terms.items():
dims.update({k: nc[v].dimensions})
return dims


def z_shape(nc, dims):
"""
Returns the vertical coordinate `shape` based on
the combined dimensions of the formula_terms `dims`.
"""
"""Return the vertical coordinate `shape` based on the combined dimensions of the formula_terms `dims`."""
all_dims = (
dims.get("eta"),
dims.get("depth"),
Expand All @@ -87,7 +72,8 @@ def z_shape(nc, dims):
return tuple(shape)


def reshape(arr, new_shape):
def _reshape(arr, new_shape):
"""Reshape arrays according to the expected dimensions."""
shape = arr.shape
dims = [k for k in shape]
slicer = slice(None, None, None)
Expand All @@ -101,6 +87,9 @@ def select(dim):


def prepare_arrays(nc, formula_terms, new_shape):
"""Prepare arrays for formulas."""
import dask.array as da

arrays = {}
for term, var in formula_terms.items():
var = nc[var]
Expand All @@ -116,20 +105,23 @@ def prepare_arrays(nc, formula_terms, new_shape):
if term == "eta" and var.ndim == 2:
chunks = (1,) + var.shape[1:]
arr = da.from_array(var, chunks=chunks)
arr = reshape(arr, new_shape)
arr = _reshape(arr, new_shape)
arrays.update({term: arr})
return arrays


def _filter_none(seq):
"""Filter out None in a list."""
return [x for x in seq if x is not None]


def _flatten(seq):
"""Flatten a list."""
return [item for sublista in seq for item in sublista]


def _remove_duplicate(seq):
"""Drop duplicated from a list."""
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=41.2", "setuptools_scm", "wheel"]
requires = ["setuptools>=41.2", "setuptools_scm", "wheel", "twine"]
build-backend = "setuptools.build_meta"

[tool.interrogate]
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
black
dask
flake8-builtins
flake8-comprehensions
flake8-mutable
flake8-print
isort
matplotlib
nbsphinx
pycodestyle
pylint
Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ classifiers =
zip_safe = True
include_package_data = True
install_requires =
dask >=1
netcdf4
numpy
python_requires = >=3.7
Expand All @@ -43,7 +42,6 @@ formats = gztar
ignore =
*.yml
.coveragerc
Makefile
docs
docs/*
notebooks
Expand Down
4 changes: 3 additions & 1 deletion tests/test_ocean_s_coordinate_g1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Test ocean_s_coordinate_g1"""

import os
import unittest

Expand All @@ -6,7 +8,7 @@
from netCDF4 import Dataset

from odvc import ocean_s_coordinate_g1
from odvc.utils import (
from odvc.parse_formula_terms import (
get_formula_terms,
get_formula_terms_dims,
get_formula_terms_variables,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py → tests/test_parse_formula_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pytest

from odvc.utils import (
from odvc.parse_formula_terms import (
get_formula_terms,
get_formula_terms_dims,
get_formula_terms_variables,
Expand Down

0 comments on commit 98df2f3

Please sign in to comment.