source: cows_wps/trunk/process_modules/get_weather_stations.py @ 6116

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/process_modules/get_weather_stations.py@6116
Revision 6116, 2.9 KB checked in by astephen, 11 years ago (diff)

more fixes and improvements

Line 
1"""
2get_weather_stations.py
3===================
4
5Process get_weather_stations that holds the GetWeatherStations class.
6
7"""
8
9import os, stat, time
10import sys
11import logging
12
13from cows_wps.process_handler.fileset import FileSet, FLAG
14import cows_wps.process_handler.ddp_process_support as ddp_process_support
15from cows_wps.process_handler.context.process_status import STATUS
16
17# Import local modules
18sys.path.append("/home/badc/software/datasets/ukmo-midas/scripts/extract")
19# MIDAS Station search code
20import getStations
21
22log = logging.getLogger(__name__)
23log.setLevel(logging.DEBUG)
24
25
26class GetWeatherStations(object):
27    def __call__(self, context):
28       
29        startTime = time.time()
30        jobId = os.path.basename(context.processDir)
31
32        # Parse the inputs
33        Username = context.inputs['Username']
34        Counties = context.inputs.get('Counties', [])
35        if Counties == []:
36            BBox = context.inputs.get("BBox", None)
37            if BBox == None:
38                raise Exception("Invalid arguments provided. Must provide either a bounding box or a list of counties.")
39
40        DataTypes = []
41        StartDateTime = context.inputs['StartDateTime']
42        StartDateTime = self._revertDateTimeToLongString(StartDateTime)
43        log.warn("DATE TIME: %s, type = %s" % (StartDateTime, type(StartDateTime)))
44        EndDateTime = context.inputs['EndDateTime']
45        EndDateTime = self._revertDateTimeToLongString(EndDateTime)
46       
47        context.setStatus(STATUS.STARTED, 'Job is now running', 0)
48
49        # Always need a FileSet, even if empty
50        fileSet = context.outputs['FileSet'] = FileSet()
51
52        # Make path to output file
53        WFile = 'weather_stations.txt'
54        MyFilePath = context.processDir + '/outputs/' + WFile
55
56        # Call code to get Weather Stations
57        st_getter = getStations.StationIDGetter(Counties, bbox=BBox, dataTypes=DataTypes, 
58                       startTime=StartDateTime, endTime=EndDateTime, outputFile=MyFilePath, noprint=1) 
59
60        context.outputs['ProcessSpecificContent'] = {
61                                    "WeatherStations": " ".join(st_getter.stList)} 
62
63        context.log.info('Written output file: %s' % WFile)
64        filesize = os.stat(MyFilePath)[stat.ST_SIZE]
65
66        fileSet = context.outputs['FileSet'] = FileSet()
67        fileSet.contents.append(FileSet(FLAG.DATA, WFile, filesize, 'Weather Stations File'))
68
69        context.setStatus(STATUS.COMPLETED, 'The End', 100)
70       
71        completionTime = time.time()
72        ddp_process_support.updateJobDetailsAfterCompletion(context, startTime, completionTime)
73
74    def _revertDateTimeToLongString(self, dt):
75        """
76        Turns a date/time into a long string as needed by midas code.
77        """
78        return str(dt).replace("-", "").replace(" ", "").replace("T", "").replace(":", "") 
79
80    def dryRun(self, context):
81
82        # Not implemented for sync jobs
83        pass
Note: See TracBrowser for help on using the repository browser.