Changeset 3516 for DPPP


Ignore:
Timestamp:
26/02/08 12:48:51 (11 years ago)
Author:
mkochan
Message:

Changed WMSCapabilities.py so that it generates "obs" dataset. Still need to change ViewWholeTimecourse?.

Location:
DPPP/kml/csml2kml
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/WMSCapabilities.py

    r3514 r3516  
    11import os 
     2import re 
    23from pylab import dates     # a very good date/time module from matplotviz -- allows years < 1970 
    34from KML import * 
     
    6667 
    6768    def _parseName(self): 
    68         (modelName, scenarioName, rest) = self.name.split(':') 
    69         timespanDesignator = rest.split('/')[0] 
    70  
    71         if timespanDesignator == 'clim_20' or timespanDesignator == 'change_20': 
    72             sampleTimespan = 20 
    73         elif timespanDesignator == 'clim_30' or timespanDesignator == 'change_30': 
    74             sampleTimespan = 30 
     69        mo = re.match('(.+)\:(.+)\:(.+)', self.name) 
     70        if mo: 
     71            (modelName, scenarioName, rest) = mo.groups() 
    7572        else: 
    76             raise ValueError('Sample timespan designation is incorrect') 
    77  
    78         return (modelName, scenarioName, sampleTimespan) 
     73            (modelName, scenarioName, rest) = (None, None, self.name) 
     74 
     75        mo2 = re.match('(clim|change)\_(\d+)\/(.+)', rest) 
     76        if mo2: 
     77            (type, periodText, description) = mo2.groups() 
     78            period = int(periodText) 
     79        else: 
     80            raise ValueError('Cannot parse in layer name') 
     81 
     82        return (type, period, description, modelName, scenarioName) 
     83 
     84    def getType(self): 
     85        return self._parseName()[0] 
     86 
     87    def getPeriod(self): 
     88        '''@return The period length (integer)''' 
     89        return self._parseName()[1] 
     90     
     91    def getDescription(self): 
     92        return self._parseName()[2] 
    7993 
    8094    def getModelName(self): 
    81         return self._parseName()[0] 
     95        return self._parseName()[3] 
    8296 
    8397    def getScenarioName(self): 
    84         return self._parseName()[1] 
    85  
    86     def getSampleTimespan(self): 
    87         return self._parseName()[2] 
     98        return self._parseName()[4] 
    8899 
    89100    def toKML(self, wmsRequestConfigElement, viewTypes): 
     
    134145            # Create a view that is informed in advance of the timesteps involved in the following categorisation 
    135146            # (most views actually no not need to see the timesteps in advance). 
    136             view = viewFactory(viewType, self.timesteps) 
     147            view = viewFactory(viewType, self) 
    137148 
    138149            # Create a KML folder that represents the view of the layer 
    139             kmlLayerViewFolder = KMLFolder(view.name, [], visible = False, opened = False)             
     150            kmlLayerViewFolder = KMLFolder(view.name, [], visible = False, opened = False) 
    140151 
    141152            # Create a categorisation dictionary, dict, which will contain categories (as returned by 
     
    160171 
    161172            return kmlLayerViewFolder 
    162          
     173 
    163174        # ------------ 
    164175 
     
    170181class View: 
    171182    ''' 
    172     Determines how data can be viewed. That is, in practice, how it can be converted into KML so it can be viewed 
     183    Determines how BottomWMSLayer data can be viewed, i.e. how it can be converted into KML so it can be viewed 
    173184    in Google Earth. In particular, it defines logical transforms of time-points into time-spans. 
    174185    ''' 
    175186 
    176     def __init__(self, timesteps): 
     187    def __init__(self, layer): 
    177188        ''' 
    178189        Initialize the view. 
    179         @param timesteps: Some views (not all) may need to "see" all the timesteps before they 
    180         are asked to provide a category for each; they are passed in here. Most views actually 
    181         ignore the timesteps. 
    182         ''' 
    183         pass 
     190        @param layer: Some views (not all) may need to "see" the layer data (although some ignore it). 
     191        ''' 
     192        self.layer = layer 
    184193 
    185194    def getLogicalTimespan(self, timestep): 
     
    205214        return timestep.replace(day=1) 
    206215 
    207     def _get20thCenturyDecade(self, timestep): 
    208         replYear = (timestep.year - 1900) / 10 + 1       # get decade as a "logical" year 
    209         return timestep.replace(year=replYear, day=1)    # start a month on the 1st of the month 
     216    ##def _get20thCenturyDecade(self, timestep): 
     217    ##    replYear = (timestep.year - 1900) / 10 + 1       # get decade as a "logical" year 
     218    ##    return timestep.replace(year=replYear, day=1)    # start a month on the 1st of the month 
    210219 
    211220    def _getMonthHence(self, timestep): 
     
    218227        return timestep.replace(year=timestep.year+1) 
    219228 
    220     def _getDecadeHence(self, timestep): 
    221         return timestep.replace(year=timestep.year+10) 
    222  
    223 def viewFactory(viewType, timesteps): 
    224     newView = viewType(timesteps) 
     229    ##def _getDecadeHence(self, timestep): 
     230    ##    return timestep.replace(year=timestep.year+10) 
     231 
     232    def _getHalfPeriodEarlier(self, timestep): 
     233        return timestep.replace(year = timestep.year-self.layer.getPeriod()/2) 
     234 
     235    def _getHalfPeriodLater(self, timestep): 
     236        return timestep.replace(year = timestep.year+self.layer.getPeriod()/2) 
     237 
     238def viewFactory(viewType, layer): 
     239    newView = viewType(layer) 
    225240    return newView 
    226241 
    227242class ViewWholeTimecourse(View): 
    228243 
    229     def __init__(self, timesteps): 
     244    def __init__(self, layer): 
    230245        self.name = 'Whole timecourse' 
     246        self.layer = layer 
    231247        yearSet = set() 
    232         for timestep in timesteps: 
     248        for timestep in self.layer.timesteps: 
    233249            yearSet.add(timestep.year) 
    234250        self.sortedYears = list(yearSet); self.sortedYears.sort() 
     
    252268class ViewSplittedByMonth(View): 
    253269 
    254     def __init__(self, timesteps): 
     270    def __init__(self, layer): 
    255271        self.name = 'Compare months' 
     272        self.layer = layer 
    256273 
    257274    def getLogicalTimespan(self, timestep): 
    258         timespanStart = self._getFirstDayOfMonth(timestep) 
    259         timespanEnd = self._getDecadeHence(timespanStart) 
     275        timespanStart = self._getHalfPeriodEarlier(timestep) 
     276        timespanEnd = self._getHalfPeriodLater(timestep) 
    260277        return (timespanStart, timespanEnd) 
    261278 
     
    275292        return monthNames[month-1] 
    276293 
    277 class ViewSplittedByYear(View): 
    278  
    279     def __init__(self, timesteps): 
     294class ViewSplittedByPeriod(View): 
     295 
     296    def __init__(self, layer): 
    280297        self.name = 'Compare period' 
     298        self.layer = layer 
    281299 
    282300    def getLogicalTimespan(self, timestep): 
     
    326344 
    327345        def _convertToKML(wmsLayer): 
    328             viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByYear] 
     346            viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByPeriod] 
    329347            return wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
    330348         
     
    332350            '''recursive''' 
    333351            if currentLevel < self.maxDirDepth: 
    334                 currentDir = parentDir + '/' + wmsLayer.name 
     352                currentDir = parentDir + '/' + wmsLayer.title 
    335353                os.mkdir(currentDir) 
    336354                print 'Created directory "%s"' % currentDir 
     
    342360                kmlDocument = KMLDocument(wmsLayer.name, [])  
    343361                kmlDocument.elements.append( _convertToKML(wmsLayer) ) 
    344                 filename = parentDir + '/' + wmsLayer.name + '.kml' 
     362                filename = parentDir + '/' + wmsLayer.title + '.kml' 
    345363                kmlDocument.save(filename) 
    346364                print 'Saved file "%s"' % filename 
     
    348366                pass 
    349367 
    350         #### topWmsLayer = wmsLayerFactory(topLayerElement) 
    351368        _convertToDirectory(self.topWmsLayer, self.baseKmlOutputDirectory, 0) 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testWMSCapabilities.py

    r3514 r3516  
    1 from cElementTree import ElementTree 
     1from cElementTree import ElementTree, XML 
    22 
    33import utils 
    44from WMSCapabilities import * 
    55 
    6 etree = ElementTree() 
    7 etree.parse('../../../../testdata/ar4-shortened.xml') 
     6configElementTree = ElementTree() 
     7configElementTree.parse('../../../../testdata/obs.conf.xml') 
     8wmsRequestConfigElement = configElementTree.getroot().find('WMSRequest') 
     9 
     10###etree = ElementTree() 
     11###etree.parse('../../../../testdata/ar4-shortened.xml') 
     12###wmsCapabilities = WMSCapabilities() 
     13###wmsCapabilitiesElement = etree.getroot() 
     14###print wmsCapabilities 
     15 
     16wmsCapabilitiesUrl = wmsRequestConfigElement.find('URL').text + '?request=GetCapabilities' 
     17wmsCapabilitiesElement = XML( utils.wget(wmsCapabilitiesUrl) ) 
    818wmsCapabilities = WMSCapabilities() 
    9 wmsCapabilitiesElement = etree.getroot() 
    10  
    1119wmsCapabilities.parseXML(wmsCapabilitiesElement) 
    1220###print wmsCapabilities 
    1321###print 
    1422 
    15 etree2 = ElementTree() 
    16 etree2.parse('../../../../testdata/ar4.conf.xml') 
    17 wmsRequestConfigElement = etree2.getroot().find('WMSRequest') 
    18  
    1923wmsLayersConvertor = WMSLayersConvertor( 
    20     wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-ar4', 1 
     24    wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-obs', 0 
    2125    ) 
    2226wmsLayersConvertor.convert() 
Note: See TracChangeset for help on using the changeset viewer.