source: TI02-CSML/trunk/newparser/test.py @ 680

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/newparser/test.py@680
Revision 680, 4.6 KB checked in by domlowe, 15 years ago (diff)

UnitDefinition?(s), Dictionary, TimeCoordinateSystem? added

Line 
1"""Test for GML/CSML parsing code
2"""
3
4from cElementTree import *
5from Parser import *
6from xml.dom.minidom import parseString
7import elementtree.ElementTree as etree
8import sys
9
10
11#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
12etree._namespace_map.update({
13    'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'})
14
15
16def removeInlineNS(csmlstring):
17    # removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
18    #remove all
19    csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
20    csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
21    csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
22    csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
23    csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
24   
25    #remove cmsl: prefixes
26    csmlstring=csmlstring.replace('<csml:','<')
27    csmlstring=csmlstring.replace('</csml:','</')
28    #add namespace declarations at top of document, after gml namespace declaration
29    csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml">', 'xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd">')
30
31   
32   
33   
34
35   
36    return csmlstring
37##############################################################################
38# ElementTree-based parser test
39##############################################################################
40# 1. Test parsing from CSML file
41tree = ElementTree(file='example.xml')
42#tree = ElementTree(file='/home/users/domlowe/CSML/Coapec/Coapec.v2.xml')
43#tree = ElementTree(file='era40gridseries.xml')
44dataset = Dataset()
45dataset.fromXML(tree.getroot())
46#csml = dataset.toXML(Element('Dataset'))
47csml = dataset.toXML()
48
49strCSML= parseString(tostring(csml)).toprettyxml()
50strCSML=removeInlineNS(strCSML)
51print strCSML
52sys.exit() # stop here.
53
54##############################################################################
55#2. Test construction of objects from scratch
56
57# AW changes made to DL code:
58#  (NB: all these derive from the underlying UML model)
59#  * AbstractFileExtract.arraySize is an array []
60#  * Dataset.metaDataProperty is an array []
61#  * Omit empty attributes entirely
62#  * Dataset.arrayDescriptors is an array holding all file extract objects
63
64#MetaDataProperty Object
65md = MetaDataProperty()
66setattr(md, 'href', 'http://ndg.nerc.ac.uk/Metadata/home/users/domlowe/CSML/demodata')
67
68#An empty list to hold a collection of NASAAmesExtracts:
69nalist=[]
70
71#Create a NASAAmesExtract
72na1 = NASAAmesExtract()
73#set attributes:
74setattr(na1, 'id', 'naextract_one')
75setattr(na1, 'variableName', 'temp')
76setattr(na1, 'index', '1')
77setattr(na1, 'fileName', '/blah/testfile.na')
78setattr(na1, 'arraySize', [50])
79setattr(na1, 'uom', 'celsius')
80#setattr(na1, 'numericType', None)
81#setattr(na1, 'numericTransform', None)
82#setattr(na1,'regExpTransform',None)
83#append NASAAmesExtract to list
84nalist.append(na1)
85
86#Create a second NASAAmesExtract
87na2 = NASAAmesExtract()
88#set attributes:
89setattr(na2, 'id', 'naextract_two')
90setattr(na2, 'variableName', 'pressure')
91setattr(na2, 'index', '1')
92setattr(na2, 'fileName', '/blah/testfile2.na')
93setattr(na2, 'arraySize', [50])
94setattr(na2, 'uom', 'mb')
95#setattr(na2, 'numericType', None)
96#setattr(na2, 'numericTransform', None)
97#setattr(na2,'regExpTransform',None)
98#append second NASAAmesExtract to list
99nalist.append(na2)
100
101#An empty list to hold a collection of NetCDFExtracts:
102nclist=[]
103
104#Create a NetCDFExtract
105nc1 = NetCDFExtract()
106#set attributes:
107setattr(nc1, 'id', 'ncextract_one')
108setattr(nc1, 'variableName', 'temp2')
109setattr(nc1, 'fileName', '/blah/testfile.nc')
110setattr(nc1, 'arraySize', [100])
111setattr(nc1, 'uom', 'celsius')
112#setattr(nc1, 'numericType', None)
113#setattr(nc1, 'numericTransform', None)
114#setattr(nc1,'regExpTransform',None)
115#append NetCDFExtract to list
116nclist.append(nc1)
117
118
119
120#Create an Empty Dataset
121ds = Dataset()
122#Set objects as attributes of dataset
123setattr(ds, 'metaDataProperty', [md])
124setattr(ds, 'arrayDescriptors', nalist+nclist)
125
126
127csml = ds.toXML()
128print parseString(tostring(csml)).toprettyxml()
Note: See TracBrowser for help on using the repository browser.