source: nappy/trunk/nappy/cdms_utils/var_utils.py @ 3441

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

Fixes to make NC to NA route work.

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"""
7var_utils.py
8=============
9
10Holds utilities involving CDMS variable objects.
11
12"""
13
14
15print "MERGE var_utils.py with other parts of cdms_utils in DDP libraries and Theo code"
16
17# Imports from python standard library
18import re
19
20# Import external packages
21import cdms
22cdms.setAutoBounds("off") 
23
24# Import local modules
25import nappy.cdms_utils.axis_utils
26
27def areDomainsIdentical(var1, var2):
28    """
29    Compares two cdms variables to see if they are defined on identical
30    axes. Returns 1 if true, 0 if not.
31    """
32    try:
33        for i in range(len(var1.getAxisList())):
34            ax1 = var1.getAxis(i)
35            ax2 = var2.getAxis(i)       
36            if nappy.cdms_utils.axis_utils.areAxesIdentical(ax1, ax2) == True:
37                return 0
38    except:
39        return 0
40    return 1
41
42
43def getBestName(var): 
44    """
45    Returns the most appropriate variable name for a NASA Ames header.
46    """
47    name = None
48    att_order = ("id", "shortname", "name", "title", "standard_name", "long_name")
49    # Deal with object that has attributes
50    for att in att_order:
51        if hasattr(var, att):   name = getattr(var, att)
52   
53    # Deal with object that has dictionary lookup - not sure why we do both
54    if hasattr(var, "has_key"):
55        for att in att_order:
56            if var.has_key(att):   name = var[att]
57
58    if hasattr(var, "units") and not re.match("^\s+$", var.units): 
59        units = var.units.strip()
60        name = "%s (%s)" % (name, units)   
61
62        if name.count("(%s)" % units) > 1:
63            name = name.replace("(%s)" % units, "")
64            name = "%s (%s)" % (name, units)
65       
66    if name[-2:] == "()": name = name[:-2]
67    return name
68
69
70def getMissingValue(var):
71    """
72    Returns the missing value or defaults to 1.E20.
73    """
74    miss = None
75    if hasattr(var, "missing_value"):  miss = var.missing_value
76    if hasattr(var, "_fill_value"):    miss = var._fill_value
77    if hasattr(var, "_FillValue"):     miss = var._FillValue
78
79    if miss == None:
80        try:
81            miss = var.getMissing()
82        except:
83            miss = 1.E20
84
85    return miss
86
87
Note: See TracBrowser for help on using the repository browser.