[Python-modules-commits] [cf-python] 02/06: Remove special udunits2 database.

Klaus Zimmermann zklaus-guest at moszumanska.debian.org
Thu Sep 22 13:02:34 UTC 2016


This is an automated email from the git hooks/post-receive script.

zklaus-guest pushed a commit to branch master
in repository cf-python.

commit 7823a5f624d309241d939c67097e83520625e899
Author: Klaus Zimmermann <klaus_zimmermann at gmx.de>
Date:   Wed Sep 14 14:42:49 2016 +0200

    Remove special udunits2 database.
    
    This removes the dependence of the program on the special cf udunits2
    database. The changes are now performed programmatically.
    
    Signed-off-by: Klaus Zimmermann <klaus_zimmermann at gmx.de>
---
 cf/units.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 63 insertions(+), 13 deletions(-)

diff --git a/cf/units.py b/cf/units.py
index afbe7bf..ada337c 100644
--- a/cf/units.py
+++ b/cf/units.py
@@ -42,18 +42,6 @@ if sys.platform == 'darwin':
 else:
     # Linux
     _udunits = ctypes.CDLL('libudunits2.so.0')
-
-# Get the name of the XML-formatted unit-database
-_unit_database = os.path.join(os.path.dirname(__file__),
-                              'etc/udunits/udunits2.xml')
-
-if os.path.isfile(_unit_database):
-    # The database included with this module exists, so use it
-    _unit_database = _c_char_p(_unit_database)
-else:
-    # Use the default (non-CF) database
-    print 'WARNING: Using the default (non-CF) Udunits database'
-    _unit_database = None
 #--- End: if
 
 # Suppress "overrides prefixed-unit" messages. This also suppresses
@@ -76,7 +64,7 @@ _ut_read_xml.argtypes = (_c_char_p, )
 _ut_read_xml.restype = _c_void_p
 
 #print 'units: before _udunits.ut_read_xml(',_unit_database,')'
-_ut_system = _ut_read_xml(_unit_database)
+_ut_system = _ut_read_xml(None)
 #print 'units: after  _udunits.ut_read_xml(',_unit_database,')'
 
 # Reinstate the reporting of error messages
@@ -194,6 +182,68 @@ _UT_DEFINITION = 8
 _cv_convert_array = {4: _cv_convert_floats,
                      8: _cv_convert_doubles}
 
+# Some function definitions necessary for the following
+# changes to the unit system.
+_ut_get_unit_by_name = _udunits.ut_get_unit_by_name
+_ut_get_unit_by_name.argtypes = (_c_void_p, _c_char_p)
+_ut_get_unit_by_name.restype = _c_void_p
+_ut_get_status = _udunits.ut_get_status
+_ut_get_status.restype = _c_int
+_ut_unmap_symbol_to_unit = _udunits.ut_unmap_symbol_to_unit
+_ut_unmap_symbol_to_unit.argtypes = (_c_void_p, _c_char_p, _c_int)
+_ut_unmap_symbol_to_unit.restype = _c_int
+_ut_map_symbol_to_unit = _udunits.ut_map_symbol_to_unit
+_ut_map_symbol_to_unit.argtypes = (_c_char_p, _c_int, _c_void_p)
+_ut_map_symbol_to_unit.restype = _c_int
+_ut_map_unit_to_symbol = _udunits.ut_map_unit_to_symbol
+_ut_map_unit_to_symbol.argtypes = (_c_void_p, _c_char_p, _c_int)
+_ut_map_unit_to_symbol.restype = _c_int
+_ut_map_name_to_unit = _udunits.ut_map_name_to_unit
+_ut_map_name_to_unit.argtypes = (_c_char_p, _c_int, _c_void_p)
+_ut_map_name_to_unit.restype = _c_int
+_ut_map_unit_to_name = _udunits.ut_map_unit_to_name
+_ut_map_unit_to_name.argtypes = (_c_void_p, _c_char_p, _c_int)
+_ut_map_unit_to_name.restype = _c_int
+_ut_new_base_unit = _udunits.ut_new_base_unit
+_ut_new_base_unit.argtypes = (_c_void_p, )
+_ut_new_base_unit.restype = _c_void_p
+
+# Change Sv mapping. Both sievert and sverdrup are just aliases,
+# so no unit to symbol mapping needs to be changed.
+# We don't need to remove rem, since it was constructed with
+# the correct sievert mapping in place; because that mapping
+# was only an alias, the unit now doesn't depend on the mapping
+# persisting.
+assert(0 == _ut_unmap_symbol_to_unit(_ut_system, _c_char_p('Sv'), _UT_ASCII))
+assert(0 == _ut_map_symbol_to_unit(_c_char_p('Sv'), _UT_ASCII,
+                                   _ut_get_unit_by_name(_ut_system, _c_char_p('sverdrup'))))
+
+# Add new base unit calendar_year
+calendar_year_unit = _ut_new_base_unit(_ut_system)
+assert(0 == _ut_map_symbol_to_unit(_c_char_p('cY'), _UT_ASCII, calendar_year_unit))
+assert(0 == _ut_map_unit_to_symbol(calendar_year_unit, _c_char_p('cY'), _UT_ASCII))
+assert(0 == _ut_map_name_to_unit(_c_char_p('calendar_year'), _UT_ASCII, calendar_year_unit))
+assert(0 == _ut_map_unit_to_name(calendar_year_unit, _c_char_p('calendar_year'), _UT_ASCII))
+assert(0 == _ut_map_name_to_unit(_c_char_p('calendar_years'), _UT_ASCII, calendar_year_unit))
+
+# Add various aliases useful for CF.
+def add_unit_alias(definition, symbol, singular, plural):
+    unit = _ut_parse(_ut_system, _c_char_p(definition), _UT_ASCII)
+    if symbol is not None:
+        assert(0 == _ut_map_symbol_to_unit(_c_char_p(symbol), _UT_ASCII, unit))
+    if singular is not None:
+        assert(0 == _ut_map_name_to_unit(_c_char_p(singular), _UT_ASCII, unit))
+    if plural is not None:
+        assert(0 == _ut_map_name_to_unit(_c_char_p(plural), _UT_ASCII, unit))
+
+add_unit_alias("1.e-3", "psu", "practical_salinity_unit", "practical_salinity_units")
+add_unit_alias("calendar_year/12", "cM", "calendar_month", "calendar_months")
+add_unit_alias("1", None, "level", "levels")
+add_unit_alias("1", None, "layer", "layers")
+add_unit_alias("1", None, "sigma_level", "sigma_levels")
+add_unit_alias("1", "dB", "decibel", "debicels")
+add_unit_alias("10 dB", None, "bel", "bels")
+
 #_udunits.ut_get_unit_by_name(_udunits.ut_new_base_unit(_ut_system),
 #                             _ut_system, 'calendar_year')
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/cf-python.git



More information about the Python-modules-commits mailing list