source: TI07-MOLES/trunk/PythonCode/wsgi/renderParameters.py @ 2319

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/renderParameters.py@2319
Revision 2319, 5.7 KB checked in by lawrence, 14 years ago (diff)

Better error handling, better parameter showing. Shell example
javascript as an initial step to using the Vocabserver.

Line 
1def renderParameters(keywords):
2    ''' This provides an html rendering of a DIF style keyword list, with links
3    to the BODC vocabulary service '''
4    #well the vocab bit is still to do :-)
5    k=collapse2(keywords)
6    return htmlCollapse2(k)
7
8def htmlList(keywords):
9    html=keywords[0]
10    for item in keywords[1:]:
11        i=item.replace(' ',' ')
12        html+=',   %s'%i
13    return html
14   
15def collapse(keywords):
16    ''' Take the last element of the tree and put it into a collapsed list '''
17    result=[]
18    for item in keywords:
19        line=item.split('>')
20        while line<>[]:
21            c=line.pop()
22            if c<>'': 
23                result.append(c)
24                line=[]
25    return result
26               
27def collapse2(keywords):
28    ''' Take the last element of a DIF parameter tree entry, and put them in a dictionary
29    under the DIF category '''
30    d={}
31    for item in keywords:
32        line=item.split('>')
33        category=line[0]
34        if category not in d: d[category]=[]
35        while line<>[]:
36            c=line.pop()
37            if c<>'': 
38                if c<>category:
39                    d[category].append(c)
40                line=[]
41    return d
42               
43def htmlCollapse2(d):
44    ''' format the collapse2 responses '''
45    html='<table>'
46    def r(x):
47        return '<tr><td>%s</td></tr>'%x
48    for item in d:
49        html+=r(item)
50        html+=r(htmlList(d[item]))
51    html+='</table>'
52    return html
53
54
55
56def reorder(keywords):
57    ''' Reorder keywords into a tree structure '''
58    #there is probably a more elegant way to do this :-)
59    d={}
60    for item in keywords:
61        line=item.split('>')
62        if line[0] not in d: d[line[0]]={}
63        if line[1] not in d[line[0]]: d[line[0]][line[1]]={}
64        if line[2] not in d[line[0]][line[1]]: d[line[0]][line[1]][line[2]]={}
65        if line[3] not in d[line[0]][line[1]][line[2]]: d[line[0]][line[1]][line[2]][line[3]]=[]
66        d[line[0]][line[1]][line[2]][line[3]].append(line[4])
67    return d
68       
69def tabulate(tree):
70    ''' Take a dictionary output by reorder and turn into an html table '''
71    html='<table>'
72    i=0
73    for i in output:
74       html+='<tr><td>%s</td>'%i
75       for j in output[i]:
76          html+='<td>%s</td>'%j
77          for k in output[i][j]:
78             html+='<td>%s</td>'%k
79             for m in output[i][j][k]:
80                html+='<td>%s</td><td>%s</td>'%(m,output[i][j][k][m][0])
81                       
82def printTree(output):
83    ''' Print out of the reordered tree '''
84    for i in output:
85       print i
86       for j in output[i]:
87           print j
88           for k in output[i][j]:
89              print k
90              for m in output[i][j][k]:
91                 print m,output[i][j][k][m]
92
93import unittest
94
95dummy=['EARTH SCIENCE > Oceans > Ocean Temperature  > Water Temperature  > Temperature of the water column', 'EARTH SCIENCE > Oceans > Salinity>Density  > Salinity  > Salinity of the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Pigments  > Chlorophyll pigment concentrations in the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Carbon Dioxide  > Partial pressure (pCO2) and fugacity (fCO2) of carbon dioxide in the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Inorganic Carbon  > Total dissolved inorganic carbon (TCO2) concentration in the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Alkalinity  > Alkalinity, acidity and pH of the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Oxygen  > Dissolved oxygen parameters in the water column', "EARTH SCIENCE > Oceans > Ocean Chemistry  > 'Other'> halocarbon concentrations in the water column", "EARTH SCIENCE > Oceans > Ocean Chemistry  > Concentration of 'other' > hydrocarbons in the water column", 'EARTH SCIENCE > Oceans > Marine Biology  > Marine Invertebrates  > Acoustic backscatter in the water column', 'EARTH SCIENCE > Biosphere > Ecological Dynamics  > Primary Production  > Primary production in the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Ammonia  > Regenerated production in the water column', 'EARTH SCIENCE > Oceans > Ocean Optics  > Attenuation>Transmission  > Transmittance and attenuance of the water column', 'EARTH SCIENCE > Oceans > Bathymetry>Seafloor Topography  > Water Depth  > Bathymetry and Elevation', 'EARTH SCIENCE > Oceans > Ocean Optics  > Photosynthetically Active Radiation  > Visible waveband radiance and irradiance measurements in the water column', 'EARTH SCIENCE > Atmosphere > Atmospheric Radiation  > Solar Radiation  > Solar Radiation', 'EARTH SCIENCE > Atmosphere > Atmospheric Temperature  > Air Temperature  > Air temperature', 'EARTH SCIENCE > Atmosphere > Atmospheric Pressure  > Surface Pressure  > Air pressure', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Organic Carbon  > Particulate total and organic carbon concentrations in the water column', 'EARTH SCIENCE > Oceans > Ocean Chemistry  > Organic Matter  > Particulate total and organic nitrogen concentrations in the water column', "EARTH SCIENCE > Oceans > Ocean Chemistry  > Nutrients  > Uptake of 'other' nutrients in the water column", 'EARTH SCIENCE > Oceans > Marine Biology  > Marine Plants  > Phytoplankton taxonomy-related abundance per unit volume of the water column', 'EARTH SCIENCE > Biosphere > Ecological Dynamics  > Herbivory  > Zooplankton feeding']
96
97class TestCase(unittest.TestCase):
98
99    def testReorder(self):
100        ''' Test the reorder code '''
101        output=reorder(dummy)
102        #printTree(output)
103       
104    def testrenderParameters(self):
105        ''' test the renderParameters routine which is the only one that really matters '''
106        o=renderParameters(dummy)
107        print o
108       
109
110if __name__=="__main__":
111    unittest.main()   
Note: See TracBrowser for help on using the repository browser.