Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
finam
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Code
Merge requests
4
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FINAM
finam
Commits
b2c9d3db
Commit
b2c9d3db
authored
9 months ago
by
Sebastian Müller
🐈
Browse files
Options
Downloads
Patches
Plain Diff
copy fix for cf_units from
https://github.com/xarray-contrib/cf-xarray/pull/523
parent
87fb450c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!282
fix for cf_units for pint>=0.24
Pipeline
#216338
failed with stages
in 2 minutes and 29 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/finam/data/cf_units.py
+56
-47
56 additions, 47 deletions
src/finam/data/cf_units.py
with
56 additions
and
47 deletions
src/finam/data/cf_units.py
+
56
−
47
View file @
b2c9d3db
...
@@ -3,60 +3,60 @@
...
@@ -3,60 +3,60 @@
Source: cf-xarray (https://github.com/xarray-contrib/cf-xarray/blob/main/cf_xarray/units.py)
Source: cf-xarray (https://github.com/xarray-contrib/cf-xarray/blob/main/cf_xarray/units.py)
License: Apache License 2.0 (https://github.com/xarray-contrib/cf-xarray/blob/main/LICENSE)
License: Apache License 2.0 (https://github.com/xarray-contrib/cf-xarray/blob/main/LICENSE)
"""
"""
import
functools
import
functools
import
re
import
re
import
warnings
import
warnings
import
pint
import
pint
from
packaging.version
import
Version
@pint.register_unit_format
(
"
cf
"
)
def
short_formatter
(
unit
,
registry
,
**
options
):
"""
Return a CF-compliant unit string from a `pint` unit.
Parameters
----------
unit : pint.UnitContainer
Input unit.
registry : pint.UnitRegistry
The associated registry
**options
Additional options (may be ignored)
Returns
-------
out : str
Units following CF-Convention, using symbols.
"""
# pint 0.24.1 gives this for dimensionless units
if
unit
==
{
"
dimensionless
"
:
1
}:
return
""
# If u is a name, get its symbol (same as pint's "~" pre-formatter)
# otherwise, assume a symbol (pint should have already raised on invalid units before this)
unit
=
pint
.
util
.
UnitsContainer
(
{
registry
.
_get_symbol
(
u
)
if
u
in
registry
.
_units
else
u
:
exp
for
u
,
exp
in
unit
.
items
()
}
)
# pylint: disable-next=unused-import
# Change in formatter signature in pint 0.24
from
pint
import
DimensionalityError
,
UndefinedUnitError
,
UnitStrippedWarning
if
Version
(
pint
.
__version__
)
<
Version
(
"
0.24
"
):
args
=
(
unit
.
items
(),)
# from `xclim`'s unit support module with permission of the maintainers
else
:
try
:
# Numerators splitted from denominators
args
=
(
((
u
,
e
)
for
u
,
e
in
unit
.
items
()
if
e
>=
0
),
((
u
,
e
)
for
u
,
e
in
unit
.
items
()
if
e
<
0
),
)
@pint.register_unit_format
(
"
cf
"
)
# pylint: disable-next=unused-argument
out
=
pint
.
formatter
(
*
args
,
as_ratio
=
False
,
product_fmt
=
"
"
,
power_fmt
=
"
{}{}
"
)
def
short_formatter
(
unit
,
registry
,
**
options
):
# To avoid potentiel unicode problems in netCDF. In both case, this unit is not recognized by udunits
"""
Return a CF-compliant unit string from a :mod:`pint` unit.
return
out
.
replace
(
"
Δ°
"
,
"
delta_deg
"
)
Parameters
----------
unit : pint.UnitContainer
Input unit.
registry : pint.UnitRegistry
the associated registry
**options
Additional options (may be ignored)
Returns
-------
out : str
Units following CF-Convention, using symbols.
"""
# convert UnitContainer back to Unit
unit
=
registry
.
Unit
(
unit
)
# Print units using abbreviations (millimeter -> mm)
s
=
f
"
{
unit
:
~
D
}
"
# Search and replace patterns
pat
=
r
"
(?P<inverse>(?:1 )?/ )?(?P<unit>\w+)(?: \*\* (?P<pow>\d))?
"
def
repl
(
m
):
i
,
u
,
p
=
m
.
groups
()
p
=
p
or
(
1
if
i
else
""
)
neg
=
"
-
"
if
i
else
""
return
f
"
{
u
}{
neg
}{
p
}
"
out
,
_n
=
re
.
subn
(
pat
,
repl
,
s
)
# Remove multiplications
out
=
out
.
replace
(
"
*
"
,
"
"
)
# Delta degrees:
out
=
out
.
replace
(
"
Δ°
"
,
"
delta_deg
"
)
return
out
.
replace
(
"
percent
"
,
"
%
"
)
except
ImportError
:
pass
# ------
# ------
# Reused with modification from MetPy under the terms of the BSD 3-Clause License.
# Reused with modification from MetPy under the terms of the BSD 3-Clause License.
...
@@ -75,7 +75,13 @@ units = pint.UnitRegistry(
...
@@ -75,7 +75,13 @@ units = pint.UnitRegistry(
],
],
force_ndarray_like
=
True
,
force_ndarray_like
=
True
,
)
)
# ----- end block copied from metpy
# need to insert to make sure this is the first preprocessor
# This ensures we convert integer `1` to string `"1"`, as needed by pint.
units
.
preprocessors
.
insert
(
0
,
str
)
# -----
units
.
define
(
"
percent = 0.01 = %
"
)
units
.
define
(
"
percent = 0.01 = %
"
)
# Define commonly encountered units (both CF and non-CF) not defined by pint
# Define commonly encountered units (both CF and non-CF) not defined by pint
...
@@ -98,6 +104,8 @@ units.define(
...
@@ -98,6 +104,8 @@ units.define(
units
.
define
(
units
.
define
(
"
degrees_east = degree = degrees_east = degrees_E = degreesE = degree_east = degree_E = degreeE
"
"
degrees_east = degree = degrees_east = degrees_E = degreesE = degree_east = degree_E = degreeE
"
)
)
# degrees for grid_longitude / grid_latitude for grid_mappings
units
.
define
(
"
degrees = degree = degrees
"
)
units
.
define
(
"
[speed] = [length] / [time]
"
)
units
.
define
(
"
[speed] = [length] / [time]
"
)
# ----- end block copied from xclim
# ----- end block copied from xclim
...
@@ -110,7 +118,8 @@ try:
...
@@ -110,7 +118,8 @@ try:
except
ImportError
:
except
ImportError
:
warnings
.
warn
(
warnings
.
warn
(
"
Import(s) unavailable to set up matplotlib support...skipping this portion
"
"
Import(s) unavailable to set up matplotlib support...skipping this portion
"
"
of the setup.
"
"
of the setup.
"
,
UserWarning
,
)
)
# end of vendored code from MetPy
# end of vendored code from MetPy
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment