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

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

Cleaning up edge cases in discovery so that while NDG browse isn't
enabled, what you see still makes sense :-).

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