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

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

Improved parameter handling, and better test cases.

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