1 | """ |
---|
2 | get_weather_stations.py |
---|
3 | =================== |
---|
4 | |
---|
5 | Process get_weather_stations that holds the GetWeatherStations class. |
---|
6 | |
---|
7 | """ |
---|
8 | |
---|
9 | import os, stat, time |
---|
10 | import sys |
---|
11 | import logging |
---|
12 | |
---|
13 | from cows_wps.process_handler.fileset import FileSet, FLAG |
---|
14 | import cows_wps.process_handler.process_support as process_support |
---|
15 | from cows_wps.process_handler.context.process_status import STATUS |
---|
16 | |
---|
17 | import process_modules.lib_get_weather_stations.utils as gws_utils |
---|
18 | |
---|
19 | |
---|
20 | log = logging.getLogger(__name__) |
---|
21 | log.setLevel(logging.DEBUG) |
---|
22 | |
---|
23 | |
---|
24 | class GetWeatherStations(object): |
---|
25 | def __call__(self, context): |
---|
26 | |
---|
27 | startTime = time.time() |
---|
28 | jobId = os.path.basename(context.processDir) |
---|
29 | |
---|
30 | # Parse the inputs |
---|
31 | Counties = context.inputs.get('Counties', []) |
---|
32 | if Counties == []: |
---|
33 | BBox = context.inputs.get("BBox", None) |
---|
34 | if BBox == None: |
---|
35 | raise Exception("Invalid arguments provided. Must provide either a geographical bounding box or a list of counties.") |
---|
36 | |
---|
37 | DataTypes = context.inputs.get("DataTypes", []) |
---|
38 | StartDateTime = context.inputs['StartDateTime'] |
---|
39 | StartDateTime = gws_utils.revertDateTimeToLongString(StartDateTime) |
---|
40 | log.warn("DATE TIME: %s, type = %s" % (StartDateTime, type(StartDateTime))) |
---|
41 | EndDateTime = context.inputs['EndDateTime'] |
---|
42 | EndDateTime = gws_utils.revertDateTimeToLongString(EndDateTime) |
---|
43 | |
---|
44 | context.setStatus(STATUS.STARTED, 'Job is now running', 0) |
---|
45 | |
---|
46 | # Always need a FileSet, even if empty |
---|
47 | self.fileSet = context.outputs['FileSet'] = FileSet() |
---|
48 | |
---|
49 | # Make path to output file |
---|
50 | WFile = 'weather_stations.txt' |
---|
51 | MyFilePath = context.processDir + '/outputs/' + WFile |
---|
52 | |
---|
53 | # Call code to get Weather Stations |
---|
54 | station_list = gws_utils.getStationList(Counties, BBox, DataTypes, StartDateTime, |
---|
55 | EndDateTime, MyFilePath) |
---|
56 | |
---|
57 | context.outputs['ProcessSpecificContent'] = { |
---|
58 | "WeatherStations": " ".join(station_list)} |
---|
59 | context.outputs['job_details']['job_capabilities'] = "send_to_extract_weather_data" |
---|
60 | |
---|
61 | context.log.info('Written output file: %s' % WFile) |
---|
62 | filesize = os.stat(MyFilePath)[stat.ST_SIZE] |
---|
63 | |
---|
64 | fileSet.contents.append(FileSet(FLAG.DATA, WFile, filesize, 'Weather Stations File')) |
---|
65 | |
---|
66 | # context.setStatus(STATUS.COMPLETED, 'The End', 100) |
---|
67 | # completionTime = time.time() |
---|
68 | # process_support.updateJobDetailsAfterCompletion(context, startTime, completionTime) |
---|
69 | |
---|
70 | # Set keep = True so that weather station files are accessible to downstream process |
---|
71 | # without unzipping. This is fine as files are small. |
---|
72 | process_support.finishProcess(context, self.fileSet, startTime, keep = True) |
---|
73 | |
---|
74 | |
---|
75 | def dryRun(self, context): |
---|
76 | |
---|
77 | self.fileSet = context.outputs['FileSet'] = FileSet() |
---|
78 | self.fileSet.contents.append(FileSet(FLAG.DATA, "testfile.txt", 30000, "Some file")) |
---|
79 | |
---|
80 | process_support.finishDryRun(context, [("meta", "data")], self.fileSet, |
---|
81 | 60, acceptedMessage='Dry run complete') |
---|
82 | |
---|