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

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

Updated nappy code to allow us to update header info better.

Line 
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
18try:
19    import cdms2 as cdms
20except:
21    import cdms
22
23
24def areAxesIdentical(ax1, ax2, is_subset=False, check_id=True):
25    """
26    Takes 2 CDMS axis objects returning True if they are essentially
27    the same and False if not.
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.
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.
32    """
33    for axtype in ("time", "level", "latitude", "longitude"):
34        if cdms.axisMatches(ax1, axtype) and not cdms.axisMatches(ax2, axtype):
35            return False
36
37    # Check ids
38    if check_id:
39        if ax1.id != ax2.id: return False
40
41    # Check units
42    print ax1.id, ax2.id
43    if hasattr(ax1, "units"):
44        if ax1.units != ax2.units: return False
45   
46    # Do different comparisons depending on 'is_subset' argument
47    if is_subset == False:
48        # Check lengths and values
49        if (len(ax1) != len(ax2)) or (ax1.getData()[:].tolist() != ax2.getData()[:].tolist()): return False
50    elif is_subset == True:
51        # Check whether values are a subset
52        len1 = len(ax1)
53        len2 = len(ax2)
54
55        # Check length of 1 divides into length of 2
56        if len2 % len1 != 0:
57            return False
58        # Now test if it is subset
59        n = len2 / len1
60
61        for i in range(len(ax1)):
62            ax2_value = ax2[n * i]
63            test_value = ax1[i] 
64            if ax2_value != test_value:
65                return False
66       
67        # If we got here then return len2/len1
68        return n
69
70    # OK, I think they are the same axis!
71    return True
72
73
74def isAxisRegularlySpacedSubsetOf(ax1, ax2):
75    """
76    Returns True if ax1 is same as ax2 except that it is only defined on a
77    subset of regularly spaced values within ax2. Otherwise returns False.
78    """
79    return areAxesIdentical(ax1, ax2, is_subset=True, check_id=False)
80
81
82def isUniformlySpaced(ax):
83    "Returns True is axis values are uniformaly spaced else returns False."
84    if len(ax) == 1: return False
85    incr = ax[1] - ax[0]
86
87    for i in range(1, len(ax)):
88        i1 = ax[i - 1]
89        i2 = ax[i] 
90        if (i2 - i1) != incr:
91            return False
92
93    return True
94
Note: See TracBrowser for help on using the repository browser.