source: TI10-WorkingGrid/trunk/CEDAR_specific/postgresdb2moles.py @ 1687

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI10-WorkingGrid/trunk/CEDAR_specific/postgresdb2moles.py@1687
Revision 1687, 5.0 KB checked in by selatham, 14 years ago (diff)

Uses latest molesReadWrite. Inserted PrettyPrint? code as function.

Line 
1#!/usr/bin/env python
2
3import sys
4import xml.dom.ext
5import cElementTree as ET
6import elementtree.ElementTree as etree
7import datetime
8import molesReadWrite as MRW
9import db_funcs
10
11def _strToDate(ymd):
12    '''given a string in form '2006-01-01' returns a datetime object'''
13    return datetime.date(*map(int, ymd.split('-')))
14
15def PrettyPrint(elem,indent='',html=0,space='   '):
16        '''Lightweight pretty printing of elementTree elements'''
17        def estrip(elem):
18                ''' Just want to get rid of unwanted whitespace '''
19                if elem is None:
20                        return ''
21                else:
22                        return elem.strip()
23        strAttrib=''
24        for att in elem.attrib:
25                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
26        result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text))
27        children=len(elem)
28        if children:
29                for item in elem:
30                        result+='\n'+PrettyPrint(item,indent=indent+space)
31                result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag)
32        else:
33                result+='</%s>'%(elem.tag)
34        return result
35
36class StandardName(object):
37    def __init__(self, somename):
38        '''given some name, attempt to resolve it into a standard name type structure. by stripping out any URLs etc'''
39        #types of names:
40        #myrandomvar
41        #http://some.namespace/localparams#somevar  #conventions?
42        #http://some.namespace/something#somevar  # standard name
43        #from these extract text (description), termid and vocabid where possible.       
44        #for starters split at '#'
45        #this can't be finalised till we agree on a convention.
46
47        sections=somename.split('#')
48        self.text=''
49        if len(sections)==2:
50            self.vocab=sections[0]
51            self.term=sections[1]
52        else:
53            self.term=somename
54            self.vocab=''
55
56
57
58
59def main(args=None):
60    '''Get command line arguments, should be a repository ID indicating which set of records to produce
61    (db could be holding a number of 'DataProviders' records e.g. badc or neodc, rdb name and host
62    i,e, python  postgresdb2moles.py badc.nerc.ac.uk rdbname rdbhost.ac.uk'''
63
64    repositoryID=sys.argv[1]
65    rdbname= sys.argv[2]
66    rdbhost= sys.argv[3]
67    reccount = 0
68    filecount = 0
69
70    #set up constants
71    schemeID='NDG-B0'
72    mNS='http://ndg.nerc.ac.uk/moles'
73
74    #set up lists to hold stuff
75    all_ids=[]
76
77
78    #connect to db (in separate db functions module)
79    connection = db_funcs.db_connect()
80
81    #get a list of all the db keys for DataEntitys which match the repositoryID.
82    select_string="select a.fld_data_provider_local_id from ndgb.tbl_data_entities as a where a.fld_data_provider_id='%s'" %repositoryID
83    cursor     = connection.cursor()
84    cursor.execute(select_string)
85    all_ids = cursor.fetchall()
86    reccount= len(all_ids)
87
88    #for each of these DataEntitys in the db
89    for ID in all_ids:
90        localID = ID[0]
91        # create MOLES skeleton document:
92        M=MRW.MolesDoc()
93        M._createClasses()
94
95        ''' The MOLES document:
96        create your moles doc by setting attributes of "M.className"
97        the classNames used must be declared in molesWriter.MolesDoc.classList'''
98
99        #MetaData ID
100        dgMID=M.dgMetadataID(schemeIdentifier=schemeID, repositoryIdentifier=repositoryID, localIdentifier=localID)
101
102        #create data granules
103        dataGranules=[]
104        DMid=M.dataModelID(schemeIdentifier='URI', repositoryIdentifier=repositoryID,localIdentifier= localID)
105        DG  = M.dgDataGranule(dataModelID=DMid)
106        dataGranules.append(DG)
107
108        #create coverage
109        dgBB=M.dgBoundingBox(LimitNorth = '79', LimitSouth='-79',LimitEast='180', LimitWest='-180')
110        dgSc=M.dgSpatialCoverage(dgBoundingBox=dgBB)
111        dgTc=M.dgTemporalCoverage()
112        dgST=M.dgSpatioTemporalCoverage(dgSpatialCoverage = dgSc, dgTemporalCoverage=dgTc)
113        dgCv=M.dgCoverage(dgSpatioTemporalCoverage=dgST)
114
115        #create parameter summaries
116        parameterSummaries=[]
117        VTID=M.dgValidTermID(ParentListID='1', TermID='1')
118        SPM=M.dgStdParameterMeasured(dgValidTerm= 'a standard name', dgValidTermID=VTID)
119        RDP=M.dgRangeDataParameter(HighValue='', LowValue='')
120        PS  = M.dgParameterSummary(dgRangeDataParameter=RDP, dgStdParameterMeasured=SPM)
121        parameterSummaries.append(PS)
122
123        #create data summary:
124        DS = M.dgDataSummary(dgCoverage=dgCv, dgParameterSummary=parameterSummaries)
125        #create data entity:
126        dgDE= M.dgDataEntity(dgDataGranule=dataGranules, dgDataSummary=DS)
127
128        #create metadata description:
129        mdID=M.metadataDescriptionID(schemeIdentifier=schemeID,repositoryIdentifier='2', localIdentifier='3')
130        dgMD=M.dgMetadataDescription(metadataDescriptionID=mdID)
131        #create metadata record
132        dgMRW=M.dgMetadataRecord(dgMetadataID=dgMID, dgDataEntity=dgDE, dgMetadataDescription=dgMD)
133        dgMeta=MRW.dgMetadata(dgMetadataRecord=dgMRW)
134        dir(dgMeta)
135
136        #produce XML  and save to file
137        molestree=dgMeta.toXML()
138        strMoles=PrettyPrint(molestree)
139        outputfile= 'moles' + localID +'.xml'
140        f=open(outputfile,'w')
141        f.write(strMoles)
142        f.close()
143        filecount +=1
144        #produce XML again (round trip)
145        #tree=cElementTree.ElementTree(file=outputfile)
146        #dgMeta=MRW.dgMetadata()
147        #dgMeta.fromXML(tree.getroot())
148        #molestree=dgMeta.toXML()
149        #print molestree
150
151    print 'postgresdb2moles ran to end. db records = %s. files output= %s' %(reccount, filecount)
152
153if __name__=='__main__':
154    main()
Note: See TracBrowser for help on using the repository browser.