Changeset 3185


Ignore:
Timestamp:
08/01/08 17:18:26 (12 years ago)
Author:
mkochan
Message:

Added logical years to the prototype. Now all first decade of the 20th Century is in year 1900, the next in year 1901 etc.

Location:
DPPP/kml/csml2kml/python
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python

    • Property svn:ignore set to
      *.pyc
  • DPPP/kml/csml2kml/python/buildGridSeries.py

    r3174 r3185  
    88''' demo script to show how the csmlwrappers and kmlfeatures modules might be used. This example puts KML for all timesteps for all features from one CSML document into a single file. This may not be practical for large model runs (not tested)''' 
    99 
     10def indent(elem, level=0): 
     11    i = "\n" + level*"  " 
     12    if len(elem): 
     13        if not elem.text or not elem.text.strip(): 
     14            elem.text = i + "  " 
     15        for child in elem: 
     16            indent(child, level+1) 
     17        if not child.tail or not child.tail.strip(): 
     18            child.tail = i 
     19        if not elem.tail or not elem.tail.strip(): 
     20            elem.tail = i 
     21    else: 
     22        if level and (not elem.tail or not elem.tail.strip()): 
     23            elem.tail = i 
    1024 
    1125#the name of the csml file (this is hard coded for demonstration purposes) 
     
    5670    timesteps=gsFeature.getTimeSteps() 
    5771    timebounds=gsFeature.getTimeBounds() 
    58          
     72    print timebounds 
     73     
    5974    #wmsendpoint e.g. http://ndgbeta.badc.rl.ac.uk/wcs/badc.nerc.ac.uk__NDG-A0__AWQX8gTc? 
    6075    wmsendpoint='tba' 
     
    7388    #and append the folder to the kml document. 
    7489    kml_document.append(kml_folder) 
    75      
    76     #Just do one feature for test purposes... remove break to do all features 
     90 
    7791    break 
    78      
    7992     
    8093#Now attach the Document element as a subelement of the root 'kml' element 
     
    8295kml_root.append(kml_document) 
    8396 
    84 #print 'This is the output KML:' 
    85 #print dump(kml_root) 
    8697#which could be written to a file or served from a url 
     98indent(kml_root, 0) 
    8799tree = ElementTree(kml_root) 
     100print 'This is the output KML:' 
    88101outputfile='gstest.kml' 
    89102f=open(outputfile, 'w') 
     
    91104f.close() 
    92105print 'Written to %s'%outputfile 
     106 
     107 
  • DPPP/kml/csml2kml/python/kmlfeatures.py

    r3174 r3185  
    11from cElementTree import ElementTree, Element, SubElement 
     2import re 
    23 
    34class GridSeries_KML(object): 
     
    2223        self.timebounds=timebounds 
    2324         
    24     #def _getWSMTime(self, timestep): 
    25         #1905-2-15T0:0:0.0Z 
     25    def _getLogicalTimeSpan(self, timestep): 
     26        ''' Translate timestep to a timespan that cover a full month in a year corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8''' 
     27        matchObject = re.match("(\d+)\-(\d+)\-(\d+)T", timestep) 
     28        (sYear, sMonth, sDay) = matchObject.groups() 
     29        year = (int(sYear) - 1900) / 10  # get decade ("logical" year) 
     30        month = int(sMonth) 
     31        daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 
     32        isLeapYear = (year % 4) == 0  # even logical years can be leap years :) 
     33        if isLeapYear: 
     34            daysInMonth[1] = 29 
     35        startDate = "%04d-%02d-%02d" % (year, month, 1) 
     36        endDate = "%04d-%02d-%02d" % (year, month, daysInMonth[month-1]) 
     37         
     38        return (startDate, endDate) 
    2639     
    2740    def _buildWMSRequest(self, timestep): 
     
    2942        #hardcoded to ipcc development site for demo purposes - in NERC portals endpoint should be of form: http://host/wms/identifier 
    3043        wmsrequest='       http://www-devel.ipcc-data.org/maps/wms/obs?request=GetMap&SERVICE=1.1.1&FORMAT=image/png&LAYERS=%s&BBOX=-180,-90,180,90&WIDTH=640&HEIGHT=480&SRS=EPSG:4326&TIME=%s'%(self.wmslayer, timestep+'Z') 
    31         
     44 
    3245        return wmsrequest 
    3346 
     
    5366            timespan=SubElement(go, 'TimeSpan') 
    5467            #TODO - need a method for retrieving time bounds 
    55             SubElement(timespan, 'begin').text=timestep 
    56             SubElement(timespan, 'end').text=timestep 
     68            (startTime, endTime) = self._getLogicalTimeSpan(timestep) 
     69            SubElement(timespan, 'begin').text=startTime 
     70            SubElement(timespan, 'end').text=endTime 
     71            print "%s..%s" % (startTime, endTime) 
    5772            icon=SubElement(go,'icon') 
    5873            SubElement(icon, 'href').text=self._buildWMSRequest(timestep) 
     
    6378            SubElement(latlonbox, 'east').text=self.bbox.east 
    6479            SubElement(latlonbox, 'west').text=self.bbox.west 
    65             #if tempcount==5:# just do a few timesteps for testing purposes 
    66                 #break   
    67          
    68          
     80 
    6981        return folder 
    7082 
Note: See TracChangeset for help on using the changeset viewer.