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

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

Various changes to get DCIP data writing cleanly to CSV files.

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    if hasattr(ax1, "units") and hasattr(ax2, "units"):
43        if ax1.units != ax2.units: return False
44   
45    # Do different comparisons depending on 'is_subset' argument
46    if is_subset == False:
47        # Check lengths and values
48        if (len(ax1) != len(ax2)) or (ax1.getData()[:].tolist() != ax2.getData()[:].tolist()): return False
49    elif is_subset == True:
50        # Check whether values are a subset
51        len1 = len(ax1)
52        len2 = len(ax2)
53
54        # Check length of 1 divides into length of 2
55        if len2 % len1 != 0:
56            return False
57        # Now test if it is subset
58        n = len2 / len1
59
60        for i in range(len(ax1)):
61            ax2_value = ax2[n * i]
62            test_value = ax1[i] 
63            if ax2_value != test_value:
64                return False
65       
66        # If we got here then return len2/len1
67        return n
68
69    # OK, I think they are the same axis!
70    return True
71
72
73def isAxisRegularlySpacedSubsetOf(ax1, ax2):
74    """
75    Returns True if ax1 is same as ax2 except that it is only defined on a
76    subset of regularly spaced values within ax2. Otherwise returns False.
77    """
78    return areAxesIdentical(ax1, ax2, is_subset=True, check_id=False)
79
80
81def isUniformlySpaced(ax):
82    "Returns True is axis values are uniformaly spaced else returns False."
83    if len(ax) == 1: return False
84    incr = ax[1] - ax[0]
85
86    for i in range(1, len(ax)):
87        i1 = ax[i - 1]
88        i2 = ax[i] 
89        if (i2 - i1) != incr:
90            return False
91
92    return True
93
Note: See TracBrowser for help on using the repository browser.