Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #74 from andersy005/master
Browse files Browse the repository at this point in the history
Add function to flexibly compute weights and dimensions to use in statistical ops
  • Loading branch information
andersy005 authored Feb 25, 2019
2 parents 299ef8e + bbd3af9 commit eb650be
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 244 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ jobs:
flake8
echo "[black]"
black --check .
black --check --line-length=100 -S --exclude='esmlab/_version.py|versioneer.py' .
echo "[isort]"
isort --recursive --check-only .
isort --recursive -w 100 --check-only .
- run:
name: Check documentation build
command: |
Expand Down
14 changes: 10 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
rev: 18.9b0
hooks:
- id: black
args: ["--line-length", "100", "--skip-string-normalization",
"--exclude", '''/(esmlab/_version.py|versioneer.py)/''']


- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.4.3
hooks:
- id: autopep8
entry: autopep8
args: ["--in-place", "--aggressive"]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.0.0
rev: v2.1.0
hooks:
- id: flake8

- repo: https://github.com/asottile/seed-isort-config
rev: v1.5.0
rev: v1.6.0
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.4
hooks:
- id: isort
- id: isort
args: ["line_length=100"]
4 changes: 1 addition & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass
# [howto, manual, or own class]).
latex_documents = [
(master_doc, "esmlab.tex", u"esmlab Documentation", author, "manual")
]
latex_documents = [(master_doc, "esmlab.tex", u"esmlab Documentation", author, "manual")]


# -- Options for manual page output ------------------------------------
Expand Down
9 changes: 4 additions & 5 deletions esmlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env python
"""Top-level package for esmlab."""
"""Top-level module for esmlab."""


import sys
from .config import set_options, get_options

from . import climatology, datasets, statistics
from ._version import get_versions
from . import climatology
from . import statistics
from . import datasets
from .config import get_options, set_options

if sys.version_info > (3, 0):
from .regrid import regridder
Expand Down
18 changes: 4 additions & 14 deletions esmlab/climatology.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ def compute_mon_climatology(dset, time_coord_name=None):

if tm.time_bound is not None:
computed_dset[tm.tb_name] = tm.time_bound - tm.time_bound[0, 0]
computed_dset[time_coord_name].values = (
computed_dset[tm.tb_name].mean(tm.tb_dim).values
)
computed_dset[time_coord_name].values = computed_dset[tm.tb_name].mean(tm.tb_dim).values

encoding[tm.tb_name] = {"dtype": "float", "_FillValue": None}
attrs[tm.tb_name] = {
"long_name": tm.tb_name,
"units": "days since 0001-01-01 00:00:00",
}
attrs[tm.tb_name] = {"long_name": tm.tb_name, "units": "days since 0001-01-01 00:00:00"}

attrs[time_coord_name] = {
"long_name": time_coord_name,
Expand Down Expand Up @@ -145,10 +140,7 @@ def compute_mon_anomaly(dset, slice_mon_clim_time=None, time_coord_name=None):

# Put the attributes, encoding back
computed_dset = set_metadata(
computed_dset,
attrs,
encoding,
additional_attrs={"month": {"long_name": "Month"}},
computed_dset, attrs, encoding, additional_attrs={"month": {"long_name": "Month"}}
)

computed_dset = tm.restore_dataset(computed_dset)
Expand Down Expand Up @@ -192,9 +184,7 @@ def compute_ann_mean(dset, weights=None, time_coord_name=None):
# Compute weights
if weights:
if len(weights) != len(dset[time_coord_name]):
raise ValueError(
"weights and dataset time values must be of the same length"
)
raise ValueError("weights and dataset time values must be of the same length")
else:
dt = xr.ones_like(dset[time_coord_name], dtype=bool)
dt.values = weights
Expand Down
9 changes: 2 additions & 7 deletions esmlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def _check_exists(value):
return os.path.exists(value)


_VALIDATORS = {
GRIDFILE_DIRECTORY: _check_path_write_access,
GRID_DEFITIONS_FILE: _check_exists,
}
_VALIDATORS = {GRIDFILE_DIRECTORY: _check_path_write_access, GRID_DEFITIONS_FILE: _check_exists}

_SETTERS = {GRIDFILE_DIRECTORY: _full_path}

Expand All @@ -72,9 +69,7 @@ def __init__(self, **kwargs):
)
)
if key in _VALIDATORS and not _VALIDATORS[key](val):
raise ValueError(
"{val} is not a valid value for {key}".format(key=key, val=val)
)
raise ValueError("{val} is not a valid value for {key}".format(key=key, val=val))
self.old[key] = SETTINGS[key]
self._apply_update(kwargs)

Expand Down
18 changes: 4 additions & 14 deletions esmlab/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class regridder(object):
"""Class to enable regridding between named grids.
"""

def __init__(
self, name_grid_src, name_grid_dst, method="bilinear", overwrite_existing=False
):
def __init__(self, name_grid_src, name_grid_dst, method="bilinear", overwrite_existing=False):
"""
Parameters
----------
Expand Down Expand Up @@ -181,9 +179,7 @@ def regrid_dataarray(
# pull data, dims and coords from incoming DataArray
data_src = da_in.values
non_lateral_dims = da_in.dims[:-2]
copy_coords = {
d: da_in.coords[d] for d in non_lateral_dims if d in da_in.coords
}
copy_coords = {d: da_in.coords[d] for d in non_lateral_dims if d in da_in.coords}

# if renormalize == True, remap a field of ones
if renormalize:
Expand All @@ -206,11 +202,7 @@ def regrid_dataarray(

# reform into xarray.DataArray
da_out = xr.DataArray(
data_dst,
name=da_in.name,
dims=da_in.dims,
attrs=da_in.attrs,
coords=copy_coords,
data_dst, name=da_in.name, dims=da_in.dims, attrs=da_in.attrs, coords=copy_coords
)
da_out.attrs["regrid_method"] = self.method

Expand All @@ -231,9 +223,7 @@ def regrid_dataarray(
# apply a missing-values mask
if apply_mask is not None:
if apply_mask.dims != da_in.dims:
logger.warning(
f"masking {apply_mask.dims}; " f"data have dims: {da_in.dims}"
)
logger.warning(f"masking {apply_mask.dims}; " f"data have dims: {da_in.dims}")
da_out = da_out.where(apply_mask)

# apply a post_method
Expand Down
Loading

0 comments on commit eb650be

Please sign in to comment.