Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
Expand All @@ -18,9 +13,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
Expand All @@ -30,7 +27,7 @@ jobs:
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[test]"
- name: Run tests
run: pytest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*.swp
*__pycache__
python/*.pyc
gfdlvitals.egg-info/
*.egg-info/
.venv/
testing/test_data
testing/db
testing/db-esm2
Expand Down
9 changes: 7 additions & 2 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ version: 2
sphinx:
configuration: docs/source/conf.py

# Optionally set the version of Python and requirements required to build your docs
build:
os: ubuntu-22.04
tools:
python: "3.11"

python:
version: 3.8
install:
- method: pip
path: .
- requirements: docs/requirements.txt
7 changes: 4 additions & 3 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
cftime<=1.2.1
cftime
ipython
jupyter
matplotlib>=3.3.3
nc-time-axis<=1.2.0
netCDF4
nc-time-axis
netCDF4
numpy
pandas
scipy
sphinx-rtd-theme
xarray
11 changes: 6 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
print(os.getcwd())
sys.path.insert(0, os.path.abspath('../../'))
from importlib.metadata import version as _get_version


# -- Project information -----------------------------------------------------
Expand All @@ -23,7 +21,8 @@
author = 'John Krasting'

# The full version, including alpha/beta/rc tags
release = '3.0a1'
release = _get_version("gfdlvitals")
version = release


# -- General configuration ---------------------------------------------------
Expand Down Expand Up @@ -67,4 +66,6 @@

#-- Build api
from sphinx.ext.apidoc import main
main(['-f', '-M', '-e', '-T', '../../gfdlvitals', '-o', 'api' ])
import gfdlvitals
_pkg_dir = os.path.dirname(gfdlvitals.__file__)
main(['-f', '-M', '-e', '-T', _pkg_dir, '-o', 'api' ])
1 change: 1 addition & 0 deletions docs/source/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ options to smooth the data, overlay a trend line, and align offset
time axes.

.. ipython:: python
:okexcept:

import gfdlvitals

Expand Down
1 change: 1 addition & 0 deletions docs/source/vitals_data_frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ In the example below, the test historical dataset is artifically split
into two 20-year epochs for comparison.

.. ipython:: python
:okexcept:

df_hist_t0 = df_hist[-40:-20]
df_hist_t1 = df_hist[-20::]
Expand Down
7 changes: 6 additions & 1 deletion gfdlvitals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""gfdlvitals - a package for computing global mean metrics"""

from .version import __version__
from importlib.metadata import version, PackageNotFoundError

try:
__version__ = version("gfdlvitals")
except PackageNotFoundError:
__version__ = "unknown"

from . import averagers
from . import cli
Expand Down
9 changes: 8 additions & 1 deletion gfdlvitals/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import os
import shutil
import subprocess
import sys
import tempfile
import gfdlvitals

__all__ = ["arguments", "process_year", "run"]
__all__ = ["arguments", "process_year", "run", "main"]


def arguments(args=None):
Expand Down Expand Up @@ -195,3 +196,9 @@ def run(args):
# -- Clean up
os.chdir(cwd)
shutil.rmtree(tempdir)


def main():
"""Entry point for the gfdlvitals command"""
sys.stdout.reconfigure(line_buffering=True)
run(sys.argv[1:])
25 changes: 7 additions & 18 deletions scripts/db2nc → gfdlvitals/cli_db2nc.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python3

""" Command line utility to convert SQLite to NetCDF """
"""Command line utility to convert SQLite to NetCDF"""

import argparse
import os
Expand All @@ -12,7 +10,7 @@


def arguments():
"""Function captures the command-line aguments passed to this script"""
"""Function captures the command-line arguments passed to this script"""

description = """
Program for converting .db file format to NetCDF format.
Expand All @@ -24,12 +22,10 @@ def arguments():
description=description, formatter_class=argparse.RawTextHelpFormatter
)

# -- Input tile
parser.add_argument(
"infile", type=str, help="Input file. Format must be sqlite (*.db)"
)

# -- Output file
parser.add_argument(
"-o",
"--outfile",
Expand Down Expand Up @@ -122,9 +118,6 @@ def write_nc(
ncfile = nc.Dataset(outfile, "w", format=ncformat)
ncfile.setncattr("source_file", dbfile)
ncfile.setncattr("created", timestamp_str)
# ncfile.setncattr('experiment',expName)
# ncfile.setncattr('type',plotType)
# ncfile.setncattr('region',region)
_ = ncfile.createDimension("time", 0)
time = ncfile.createVariable("time", "f4", ("time",))
time.calendar = "noleap"
Expand All @@ -134,9 +127,6 @@ def write_nc(
if table not in ["long_name", "units", "cell_measure"]:
data_array = np.ma.ones(len(years)) + 1.0e20
data_array.mask = True
# if 'Land' in plotType:
# extract_list = ['avg','sum']
# else:
extract_list = ["value"]
for k in extract_list:
count = 0
Expand Down Expand Up @@ -178,16 +168,15 @@ def write_nc(
ncfile.close()


if __name__ == "__main__":

# read command line arguments
def main():
"""Entry point for the db2nc command"""
args = arguments()
# get the full path of the db file
infile = os.path.realpath(args.infile)
# get list of variables and years in a file
_tables, _years = tables_and_years(infile)
write_nc(
infile, args.outfile, _tables, _years, clobber=args.force, verbose=args.verbose
)

sys.exit()

if __name__ == "__main__":
main()
15 changes: 7 additions & 8 deletions scripts/plotdb → gfdlvitals/cli_plotdb.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#!/usr/bin/env python

""" CLI script for plotting SQLite files """
"""CLI script for plotting SQLite files"""

import argparse
import os

import matplotlib.pyplot as plt

import gfdlvitals

COUNT = 1

def arguments():
"""
Expand Down Expand Up @@ -79,7 +74,11 @@ def arguments():
return args



if __name__ == "__main__":
def main():
"""Entry point for the plotdb command"""
cliargs = arguments()
gfdlvitals.plot.run_plotdb(cliargs)


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions gfdlvitals/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import nc_time_axis

import numpy as np
import pkg_resources as pkgr
from importlib.resources import files

import matplotlib
import matplotlib.pyplot as plt
Expand All @@ -21,7 +21,7 @@
def set_font():
"""Sets font style to Roboto"""
# Add Roboto font
fonts_dir = pkgr.resource_filename("gfdlvitals", "resources/fonts")
fonts_dir = str(files("gfdlvitals").joinpath("resources/fonts"))

font_dirs = [fonts_dir]
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
Expand Down
6 changes: 3 additions & 3 deletions gfdlvitals/sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Sample db files for demonstration """

import pkg_resources as pkgr
from importlib.resources import files

historical = pkgr.resource_filename("gfdlvitals", "resources/historical.db")
picontrol = pkgr.resource_filename("gfdlvitals", "resources/picontrol.db")
historical = str(files("gfdlvitals").joinpath("resources/historical.db"))
picontrol = str(files("gfdlvitals").joinpath("resources/picontrol.db"))
15 changes: 5 additions & 10 deletions gfdlvitals/util/gmeantools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import math
import pickle
import sqlite3
import sys
import warnings

import numpy as np
import pkg_resources as pkgr
from importlib.resources import files

__all__ = [
"get_web_vars_dict",
Expand All @@ -29,9 +30,7 @@ def get_web_vars_dict():
dict
LM3 variable module mappings and metadata
"""
mapping_file = pkgr.resource_filename(
"gfdlvitals", "resources/LM3_variable_dictionary.pkl"
)
mapping_file = str(files("gfdlvitals").joinpath("resources/LM3_variable_dictionary.pkl"))
return pickle.load(
open(
mapping_file,
Expand Down Expand Up @@ -285,16 +284,12 @@ def write_sqlite_data(
# check if result is a nan and replace with a defined missing value
if varmean is not None:
if math.isnan(float(varmean)):
warnings.warn(
f"Could not update {sqlfile} variable {varname} with mean={varmean}"
)
print(f" WARNING: {varname} mean is NaN in {sqlfile}, writing missing value", file=sys.stderr)
varmean = missing_value

if varsum is not None:
if math.isnan(float(varsum)):
warnings.warn(
f"Could not update {sqlfile} variable {varname} with mean={varsum}"
)
print(f" WARNING: {varname} sum is NaN in {sqlfile}, writing missing value", file=sys.stderr)
varsum = missing_value

conn = sqlite3.connect(sqlfile)
Expand Down
5 changes: 3 additions & 2 deletions gfdlvitals/util/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ def in_mem_xr(data):
In-memory xarray dataset object
"""

time_coder = xr.coders.CFDatetimeCoder(use_cftime=True)
if isinstance(data, netCDF4._netCDF4.Dataset):
dfile = xr.open_dataset(xr.backends.NetCDF4DataStore(data), use_cftime=True)
dfile = xr.open_dataset(xr.backends.NetCDF4DataStore(data), decode_times=time_coder, decode_timedelta=False)
else:
dfile = xr.open_dataset(data, use_cftime=True)
dfile = xr.open_dataset(data, decode_times=time_coder, decode_timedelta=False)

return dfile

Expand Down
2 changes: 1 addition & 1 deletion gfdlvitals/util/xrtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ def xr_weighted_avg(dset, weights):
_dset_weighted[x] = _dset_weighted[x].astype(dset[x].dtype)
_dset_weighted[x].attrs = dset[x].attrs

result = result.merge(_dset_weighted)
result = result.merge(_dset_weighted, compat="override")

return result
3 changes: 0 additions & 3 deletions gfdlvitals/version.py

This file was deleted.

Loading