source: TI03-DataExtractor/branches/old_stuff/latest_dx/dx/pydxs/ValidateSelection.py @ 793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/old_stuff/latest_dx/dx/pydxs/ValidateSelection.py@793
Revision 793, 5.7 KB checked in by astephen, 13 years ago (diff)

Put all the old code in the old_stuff branch.

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"""
6validateSelection.py
7====================
8
9Contains the class ValidateSelection used to validate the contents of a
10user's selected task against the known options and limitations.
11
12"""
13
14# Import library modules
15import os, sys
16
17# Import package modules
18from serverConfig import *
19from common import *
20from RequestDict import RequestDict
21from MetadataExtractor import MetadataExtractor
22from DXErrors import *
23from Datasetdb import *
24from DXDMLHandler import *
25
26
27class ValidateSelection(MetadataExtractor):
28    """
29    Class to validate the contents of a user request against the
30    available options.
31    """
32   
33    def __init__(self, sessionObj):
34        """
35        Defines inputs as instance variables.
36        """
37        self.bag=sessionObj
38        self.n=sessionObj["numberOfDatasets"]
39        self.dt=Datasetdb()
40       
41        # Create a list of attributes to delete to clean/validate the session object (bag)
42        self.toDelete=[]
43       
44        self._validate()
45        self._clean()
46       
47        self.status="Validated successfully"
48       
49       
50    def _clean(self):
51        """
52        Deletes all key/value pairs in the session object that are invalid.
53        """
54        for key in self.toDelete:
55            del self.bag[key]
56
57
58    def _validate(self):
59        """
60        Validates options requested.
61        """
62        onceOnlyKeys=["horizontalDomain", "outputFormat"]
63        perDatasetKeysToCheck=["datasetGroup", "dataset", 
64                "variable", "temporalDomain", "verticalDomain"]
65                               
66       
67        if keyPatternMatch(self.bag, "datasetGroup_"): 
68            self._checkDatasetGroups()
69           
70        if keyPatternMatch(self.bag, "dataset_"): 
71            self._checkDatasets()         
72           
73        if keyPatternMatch(self.bag, "variable_"): 
74            self._checkVariables()
75                             
76        if keyPatternMatch(self.bag, "temporalDomain_"): 
77            self._checkTemporalSelection()
78           
79        if keyPatternMatch(self.bag, "verticalDomain_"): 
80            self._checkVerticalSelection()
81
82        if keyPatternMatch(self.bag, "horizontalDomain_"): 
83            self._checkHorizontalSelection()
84                                   
85        if keyPatternMatch(self.bag, "outputFormat"):
86            self._checkOutputFormat()
87           
88           
89    def _checkDatasetGroups(self):
90        """
91        Raises an exception if dataset group not known.
92        """
93        dsgList=self.dt.getDatasetGroups()
94        for i in range(1, self.n+1):
95            key="datasetGroup_%s" % i
96            value=self.bag.get(key, None)
97            if value!=None and value!="User dataset group":
98                if value not in dsgList:
99                    self.toDelete.append(key)
100                    #raise "Dataset group '%s' not in known list or supplied by user." % value
101                   
102                   
103    def _checkDatasets(self):
104        """
105        Raises an exception if dataset not in given dataset group.
106        """
107        for i in range(1, self.n+1):
108            dsgKey=self.bag["datasetGroup_%s" % i]
109            datasets=self.dt.getDatasets(dsgKey)
110   
111            key="dataset_%s" % i
112            ds=self.bag.get(key, None)     
113
114            if ds!=None and ds!="User dataset":
115                if ds not in datasets:
116                    self.toDelete.append(key)
117                    #raise "Dataset '%s' not in known list or supplied by user." % ds     
118               
119               
120    def _checkVariables(self):
121        """
122        Raises an exception if variable not in given dataset.
123        """
124        for i in range(1, self.n+1):
125            dsgKey=self.bag["datasetGroup_%s" % i]
126            dsKey=self.bag["dataset_%s" % i]
127           
128            varList=self.dt.getVariables(dsgKey, dsKey)
129            varIDs=[v[-1] for v in varList]
130               
131            key="variable_%s" % i
132            var=self.bag.get(key, None)     
133
134            if var!=None:
135                if var not in varIDs:
136                    self.toDelete.append(key)
137                    #raise "Variable '%s' not in dataset." % var     
138
139               
140    def _checkTemporalSelection(self):
141        """
142        Raises an error if temporal selection is out of range or holds no values.
143        """
144        # First check if out of range
145       
146        # Then check if no data in range
147       
148   
149    def _checkVerticalSelection(self):
150        """
151        Raises an error if vertical levels are not correctly set.
152        """
153               
154       
155    def _checkHorizontalSelection(self):
156        """
157        Raises an error if horizontal selection is out of range.
158        """
159               
160       
161    def _checkOutputFormat(self):
162        """
163        Raises an error if output format not supported.
164        """
165        if self.bag.has_key("outputFormat"):
166            if self.bag["outputFormat"] not in OUTPUT_FORMATS:
167                raise DXValidationError, "Output format '%s' is not supported. Try one of %s." % \
168                            (self.bag["outputFormat"], ", ".join(OUTPUT_FORMATS))
169       
170if __name__=="__main__":
171    sd={"datasetGroup_1":"Test Data Group 3", "numberOfDatasets":1, 
172        "dataset_1":"Test Dataset 3", "variable_1":"var3", "outputFormat":"NetCDF"}
173    a=ValidateSelection(sd)
174    """
175    a=Datasetdb()
176    print a.getDatasetGroups()
177    print a.getDatasets('Test Data Group 2')
178    print a.getFileNamePrefix('Test Data Group 3')
179    print a.getFileNameSection('Test Data Group 3', 'Test Dataset 3')
180    print a.getDatasetsAndDatasetURIs('Test Data Group 1')
181    print a.getDatasetURIList()
182    print a.getProtectID(datasetGroup='Test Data Group 2', datasetURI='file:/abstractWebService/dxs/testdata/testdata2.xml')
183    print a.getVariables(datasetGroup='Test Data Group 3', dataset='Test Dataset 3')
184    print a.getVariables(datasetGroup='Test Data Group 3', datasetURI='file:/usr/local/dx/testdata/testdata3.xml')   
185    print a.getDomain('Test Data Group 3', 'Test Dataset 3', "var3")
186    print a.getHorizontalDomain('Test Data Group 3', 'Test Dataset 3', "var3")
187    print a.getVerticalSpatialDomain('Test Data Group 3', 'Test Dataset 3', "var3")
188    print a.getTemporalDomain('Test Data Group 3', 'Test Dataset 3', "var3")
189    print a.getCFGlobalAttributes("file:/usr/local/dx/testdata/testdata3.xml")"""
190   
Note: See TracBrowser for help on using the repository browser.