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

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

Tiny modifications to discovery to handle multiple formats for parameter
lists ...

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    print keywords
6    k=collapse(keywords)
7    print k
8    return htmlList(k)
9
10def htmlList(keywords):
11    html=keywords[0]
12    for item in keywords[1:]:
13        i=item.replace(' ',' ')
14        html+=',   %s'%i
15    return html
16   
17def collapse(keywords):
18    ''' Take the last element of the tree and put it into a collapsed list '''
19    result=[]
20    for item in keywords:
21        line=item.split('>')
22        while line<>[]:
23            c=line.pop()
24            if c<>'': 
25                result.append(c)
26                line=[]
27    return result
28
29def reorder(keywords):
30    ''' Reorder keywords into a tree structure '''
31    #there is probably a more elegant way to do this :-)
32    d={}
33    for item in keywords:
34        line=item.split('>')
35        if line[0] not in d: d[line[0]]={}
36        if line[1] not in d[line[0]]: d[line[0]][line[1]]={}
37        if line[2] not in d[line[0]][line[1]]: d[line[0]][line[1]][line[2]]={}
38        if line[3] not in d[line[0]][line[1]][line[2]]: d[line[0]][line[1]][line[2]][line[3]]=[]
39        d[line[0]][line[1]][line[2]][line[3]].append(line[4])
40    return d
41       
42def tabulate(tree):
43    ''' Take a dictionary output by reorder and turn into an html table '''
44    html='<table>'
45    i=0
46    for i in output:
47       html+='<tr><td>%s</td>'%i
48       for j in output[i]:
49          html+='<td>%s</td>'%j
50          for k in output[i][j]:
51             html+='<td>%s</td>'%k
52             for m in output[i][j][k]:
53                html+='<td>%s</td><td>%s</td>'%(m,output[i][j][k][m][0])
54                       
55def printTree(output):
56    ''' Print out of the reordered tree '''
57    for i in output:
58       print i
59       for j in output[i]:
60           print j
61           for k in output[i][j]:
62              print k
63              for m in output[i][j][k]:
64                 print m,output[i][j][k][m]
65
66import unittest
67
68dummy=['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']
69
70class TestCase(unittest.TestCase):
71
72    def testReorder(self):
73        ''' Test the reorder code '''
74        output=reorder(dummy)
75        #printTree(output)
76       
77    def testrenderParameters(self):
78        ''' test the renderParameters routine which is the only one that really matters '''
79        o=renderParameters(dummy)
80       
81
82if __name__=="__main__":
83    unittest.main()   
Note: See TracBrowser for help on using the repository browser.