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

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

Fixed and broken - got 2110 and 2310 writing from NetCDF. But the solution breaks the basic readNA/writeNA cycle. The reason is that the new code writes the data from NX, DX and X rather than A and NAUXV. Maybe the data really needs to be encoded to NAUXV and A in order for this to work unformly. Hmmmm.

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