source: TI03-DataExtractor/trunk/pydxs/DatasetFormatDecider.py @ 1715

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/trunk/pydxs/DatasetFormatDecider.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"""
6DatasetFormatDecider.py
7=======================
8
9Holds the DatasetFormatDecider class used to determine which format
10is being used and so which I/O classes are needed.
11
12"""
13
14# import standard library modules
15import sys
16
17# import local modules
18from common import *
19from DXDMLHandler import *
20from DXErrors import *
21from CDMSDataHandler import *
22from CSMLDataHandler import *
23from MIDASDataHandler import *
24from ECMWFGRIB1NoSubsetDataHandler import *
25
26
27class DatasetFormatDecider:
28    """
29    DatasetFormatDecider class used to determine which format
30    is being used and so which I/O classes are needed.
31    """
32   
33    def __init__(self, datasetGroup=None, dataset=None, datasetURI=None):
34        """
35        Works out the dataset format type that has been found, from:
36        CDMS, CSML and MIDAS. Note that fileURI can actually be the location of
37        a class within the local python installation when the Input is controlled
38        be code rather than by reading data files. The class is identified by the
39        path:  /<package>/<module>/<class>, /<module>/<class> or just <class>.
40        The information is stored in the instance variable self.datasetFormat
41        """
42        self.datasetFormat=None
43
44        #print "INPUTS to DatasetFormatDecider:", datasetGroup, dataset, datasetURI
45
46        if not datasetURI:
47            DXDML=DXDMLHandler()
48            for item in DXDML.getDatasetsAndDatasetURIs(datasetGroup):
49                if item[0]==dataset:
50                    datasetURI=item[1]
51       
52        if datasetURI==None:
53            print "HELP HELP HELP\n\n\n", datasetGroup, dataset, "\n\n"
54            raise DXError, "Cannot determine the datasetURI."
55           
56        datasetURI=str(datasetURI).split(":")[-1]
57       
58        if not os.path.isfile(datasetURI):
59            # check if it is a class
60            if datasetURI.find("MIDAS")>-1:
61                self.datasetFormat=MIDASDataHandler()
62            else:
63                parts=datasetURI.split("/")
64                raise DXError, "Checking for class not yet implemented."
65        else:
66            header=open(datasetURI).read(1000)
67            if header.find("GRIBNoSubset")>-1:
68                self.datasetFormat=ECMWFGRIB1NoSubsetDataHandler()
69            elif header.lower().find("cdms")>-1:
70                self.datasetFormat=CDMSDataHandler()
71            elif header.lower().find("csml")>-1:
72                self.datasetFormat=CSMLDataHandler()
73   
74        if self.datasetFormat==None:
75            raise DXError, "Could not locate dataset format type for file: %s" % datasetURI   
76
77
78if __name__=="__main__":
79    print  DatasetFormatDecider("ECMWF ERA-40 Re-analyses","6-hourly Analyses on Model Levels (T159 Spectral)").datasetFormat
80    print "CDMS file..."
81    print DatasetFormatDecider("Test Data Group 3", "Test Dataset 3").datasetFormat
82    print "\nCSML file..."
83    print DatasetFormatDecider("Test Data Group 3", "Test Dataset 3").datasetFormat
Note: See TracBrowser for help on using the repository browser.