source: TI03-DataExtractor/trunk/dxs/dxvv/VVDoubler.py @ 1715

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/trunk/dxs/dxvv/VVDoubler.py@1715
Revision 1715, 3.1 KB checked in by astephen, 13 years ago (diff)

Merged with titania version.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6
7VVDoubler.py
8============
9
10Holds the VVDoubler class that calculates a virtual variable that is 2 times
11another variable.
12
13The intention is that you can incorporate any code you like here as long as it
14provides a way of holding the output data in memory in a CDMS-like array/variable.
15
16The underlying algorithm can include as much complexity and old code as you wish.
17
18The varDict dictionary is key to providing metadata.
19
20"""
21
22# Import standard library modules
23import os
24
25# Import package modules
26import cdms
27from pydxs.CDMSDataHandler import *
28
29
30class VVDoubler:
31    """
32    Class to virtual variables that are double another variable.
33    """
34   
35    def __init__(self, variableID, datasetURI, axisSelectionDict, timeStep=None):
36        """
37        Takes in the variable ID of the virtual variable along with the CDML
38        that describes it. An axis selection dictionary is also provided and
39        possibly a time step. Using the varDict dictionary the code works out
40        the algorithm to derive the virtual variable from one or more existing
41        variables. varDict also provides a dictionary of potential attributes
42        that will be added to the output variable. Note that these are identified
43        by the name beginning with "vv_". This will be removed in the actual
44        attribute set in the file.
45        """
46        if variableID[-5:]=="_dxvv": variableID=variableID[:-5]
47        varDict={("twotimespqn", "file:/usr/local/test/dxs/testdata/testdata1.xml"):
48                {"inputVarList":[["pqn","file:/usr/local/test/dxs/testdata/testdata1.xml"]],
49                 "vv_long_name":"Doubled marvellous quantity", "vv_units":"K", "vv_comment":"I'm a comment", "vv_name":""}}
50                 
51        varDetails=varDict[(variableID, datasetURI)]
52        inputVarList=varDetails["inputVarList"]
53        #inputVarID=varDetails["inputVarID"]
54        #inputURI=varDetails["inputURI"]
55       
56        #(inputVar, fileURI)=varDict[(variableID, datasetURI)]
57       
58        # Call code to generate virtual variable
59        self.var=self._generateVirtualVar(inputVarList, axisSelectionDict, timeStep)
60
61        #infile=cdms.open(fileURI)
62        #invar=infile(inputVar)
63       
64        # Set some basic attributes
65        self.var.id=variableID
66       
67        for key in varDetails.keys():
68            if key[:3]=="vv_":
69                name=key[3:]
70                value=varDetails[key]
71                setattr(self.var, name, value)
72       
73
74    def _generateVirtualVar(self, inputVarList, axisSelectionDict, timeStep):
75        """
76        Reads in input variables and derives a virtual variable from them.
77        """
78        dataFileHandler=CDMSDataHandler()
79        (inputVarID, inputURI)=inputVarList[0]
80        invar=dataFileHandler.readVariableSubsetIntoMemory(inputURI, inputVarID, axisSelectionDict, timeStep)
81        newvar=2*invar
82        # What about all the output metadata???
83        self._cloneAttributes(invar, newvar)   
84        return newvar
85       
86
87    def _cloneAttributes(self, invar, outvar):
88        """
89        Adds attributes from input variable to output variable
90        """
91        return
92       
93
94if __name__=="__main__":
95    x=VVDoubler("twotimespqn",  "file:/usr/local/test/dxs/testdata/testdata1.xml", {}).var
96    print x, x.shape, type(x), x.attributes
Note: See TracBrowser for help on using the repository browser.