source: nappy/trunk/nappy/cdms_utils/axis_utils.py @ 5368

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/cdms_utils/axis_utils.py@5368
Revision 5368, 2.8 KB checked in by astephen, 11 years ago (diff)

Replace print with logging

RevLine 
[3331]1#!/usr/bin/env python
2#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
3#   This software may be distributed under the terms of the
4#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5
6"""
7axis_utils.py
8=============
9
10Holds utilities involving CDMS axis objects.
11
12"""
13
14
15# Imports from python standard library
16
17# Import external packages
[3966]18try:
19    import cdms2 as cdms
20except:
21    import cdms
[3331]22
[3966]23
[3482]24def areAxesIdentical(ax1, ax2, is_subset=False, check_id=True):
[3331]25    """
[3351]26    Takes 2 CDMS axis objects returning True if they are essentially
27    the same and False if not.
[3442]28    If is_subset == True then return True if ax1 is same as ax2 except that it is
29    only defined on a subset of regularly spaced values within ax2.
[3482]30    If is_subset is used then return value is False or (len(ax2)/len(ax1)).
31    If check_id == False then don't compare the ids of the axes.
[3331]32    """
33    for axtype in ("time", "level", "latitude", "longitude"):
34        if cdms.axisMatches(ax1, axtype) and not cdms.axisMatches(ax2, axtype):
[3351]35            return False
[3331]36
37    # Check ids
[3482]38    if check_id:
39        if ax1.id != ax2.id: return False
40
[3331]41    # Check units
[4698]42    if hasattr(ax1, "units") and hasattr(ax2, "units"):
[4700]43        if ax1.units != ax2.units: 
44            return False
[3442]45   
46    # Do different comparisons depending on 'is_subset' argument
47    if is_subset == False:
48        # Check lengths and values
[4700]49        if (len(ax1) != len(ax2)):
50            return False
51
52        if ax1[:].tolist() != ax2[:].tolist(): 
53            return False
54
[3442]55    elif is_subset == True:
56        # Check whether values are a subset
57        len1 = len(ax1)
58        len2 = len(ax2)
[3482]59
[3442]60        # Check length of 1 divides into length of 2
61        if len2 % len1 != 0:
62            return False
63        # Now test if it is subset
64        n = len2 / len1
65
66        for i in range(len(ax1)):
67            ax2_value = ax2[n * i]
68            test_value = ax1[i] 
69            if ax2_value != test_value:
70                return False
[3482]71       
72        # If we got here then return len2/len1
73        return n
[3442]74
[3331]75    # OK, I think they are the same axis!
[3351]76    return True
[3331]77
[3482]78
[3442]79def isAxisRegularlySpacedSubsetOf(ax1, ax2):
80    """
81    Returns True if ax1 is same as ax2 except that it is only defined on a
82    subset of regularly spaced values within ax2. Otherwise returns False.
83    """
[3482]84    return areAxesIdentical(ax1, ax2, is_subset=True, check_id=False)
85
86
87def isUniformlySpaced(ax):
88    "Returns True is axis values are uniformaly spaced else returns False."
89    if len(ax) == 1: return False
90    incr = ax[1] - ax[0]
91
92    for i in range(1, len(ax)):
93        i1 = ax[i - 1]
94        i2 = ax[i] 
95        if (i2 - i1) != incr:
96            return False
97
98    return True
99
Note: See TracBrowser for help on using the repository browser.