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

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

CEDAR (BADC and NEODC) specific MOLES creation software

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, filecount = 0
47
48    #set up constants
49    schemeID='NDG-B0'
50
51    #set up lists to hold stuff
52    all_ids=[]
53
54    #connect to db (in separate db functions module)
55    connection = db_funcs.db_connect()
56
57    #get a list of all the db keys for DataEntitys which match the repositoryID.
58    select_string="select a.fld_data_provider_local_id from ndgb.tbl_data_entities as a where a.fld_data_provider_id=%s" %repositoryID
59    cursor     = connection.cursor()
60    cursor.execute(select_string)
61    all_ids = cursor.fetchall()
62    reccount= len(all_ids)
63
64    #for each of these DataEntitys in the db
65    for ID in all_ids:
66        localID = ID[0]
67        # create MOLES skeleton document:
68        M=MRW.MolesDoc()
69        M._createClasses()
70
71        ''' The MOLES document:
72        create your moles doc by setting attributes of "M.className"
73        the classNames used must be declared in molesWriter.MolesDoc.classList'''
74
75        #MetaData ID
76        dgMID=M.dgMetadataID(schemeIdentifier=schemeID, repositoryIdentifier=repositoryID, localIdentifier=localID)
77
78        #create data granules
79        datagranules=[]
80        DMid=M.dataModelID(schemeIdentifier='URI', repositoryIdentifier=repositoryID,localIdentifier= localID)
81        DG  = M.dgDataGranule(dataModelID=DMid)
82        datagranules.append(DG)
83
84        #create coverage
85        dgBB=M.dgBoundingBox(LimitNorth = '79', LimitSouth='-79',LimitEast='180', LimitWest='-180')
86        dgSc=M.dgSpatialCoverage(dgBoundingBox=dgBB)
87        dgTc=M.dgTemporalCoverage()
88        dgST=M.dgSpatioTemporalCoverage(dgSpatialCoverage = dgSc, dgTemporalCoverage=dgTc)
89        dgCv=M.dgCoverage(dgSpatioTemporalCoverage=dgST)
90
91        #create data summary:
92        DS = M.dgDataSummary(dgCoverage=dgCv, dgParameterSummary=parameterSummaries)
93        #create data entity:
94        dgDE= M.dgDataEntity(dgDataGranule=datagranules, dgDataSummary=DS)
95
96        #create metadata description:
97        mdID=M.metadataDescriptionID(schemeIdentifier='1',repositoryIdentifier='2', localIdentifier='3')
98        dgMD=M.dgMetadataDescription(metadataDescriptionID=mdID)
99        #create metadata record
100        dgMRW=M.dgMetadataRecord(dgMetadataID=dgMID, dgDataEntity=dgDE, dgMetadataDescription=dgMD)
101        dgMeta=MRW.dgMetadata(dgMetadataRecord=dgMRW)
102
103        #round trip
104        #produce XML  and save to file
105        molestree=dgMeta.toXML
106        outputfile= 'moles' + localID +'.xml'
107        f=open(outputfile,'w')
108        f.write(molestree)
109        f.close()
110        filecount +=1
111        #produce XML again (round trip)
112        #tree=cElementTree.ElementTree(file=outputfile)
113        #dgMeta=MRW.dgMetadata()
114        #dgMeta.fromXML(tree.getroot())
115        #molestree=dgMeta.toXML()
116        #print molestree
117
118    print 'postgresdb2moles ran to end. db records = %s. files output= %s' %(reccount, filecount)
119
120if __name__=='__main__':
121    main()
Note: See TracBrowser for help on using the repository browser.