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

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

Last little commit before moving.

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