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

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

further development

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