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

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

Fixes for ticket:664

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        if d[item]!=[]:
51            html+=r(htmlList(d[item]))
52    html+='</table>'
53    return html
54
55
56
57def reorder(keywords):
58    ''' Reorder keywords into a tree structure '''
59    #there is probably a more elegant way to do this :-)
60    d={}
61    for item in keywords:
62        line=item.split('>')
63        if line[0] not in d: d[line[0]]={}
64        if line[1] not in d[line[0]]: d[line[0]][line[1]]={}
65        if line[2] not in d[line[0]][line[1]]: d[line[0]][line[1]][line[2]]={}
66        if line[3] not in d[line[0]][line[1]][line[2]]: d[line[0]][line[1]][line[2]][line[3]]=[]
67        d[line[0]][line[1]][line[2]][line[3]].append(line[4])
68    return d
69       
70def tabulate(tree):
71    ''' Take a dictionary output by reorder and turn into an html table '''
72    html='<table>'
73    i=0
74    for i in output:
75       html+='<tr><td>%s</td>'%i
76       for j in output[i]:
77          html+='<td>%s</td>'%j
78          for k in output[i][j]:
79             html+='<td>%s</td>'%k
80             for m in output[i][j][k]:
81                html+='<td>%s</td><td>%s</td>'%(m,output[i][j][k][m][0])
82                       
83def printTree(output):
84    ''' Print out of the reordered tree '''
85    for i in output:
86       print i
87       for j in output[i]:
88           print j
89           for k in output[i][j]:
90              print k
91              for m in output[i][j][k]:
92                 print m,output[i][j][k][m]
93
94import unittest
95
96dummy=['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']
97
98class TestCase(unittest.TestCase):
99
100    def testReorder(self):
101        ''' Test the reorder code '''
102        output=reorder(dummy)
103        #printTree(output)
104       
105    def testrenderParameters(self):
106        ''' test the renderParameters routine which is the only one that really matters '''
107        o=renderParameters(dummy)
108        print o
109       
110
111if __name__=="__main__":
112    unittest.main()   
Note: See TracBrowser for help on using the repository browser.