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

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

More fixes to nappy to get all DCIP stuff writing properly.

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