| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# ----------------------------------------------------------------------------- |
| 4 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 5 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# ----------------------------------------------------------------------------- |
| 20 |
# This is illustrative code developed for tutorial purposes, it is not |
| 21 |
# intended for scientific use and is not guarantied to be accurate or correct. |
| 22 |
""" |
| 23 |
Usage: |
| 24 |
post-process SITE TIME |
| 25 |
|
| 26 |
Arguments: |
| 27 |
SITE: The name of the location to process data for. |
| 28 |
TIME: The time ahead of the cycle point to produce the forecast for in |
| 29 |
minutes. |
| 30 |
|
| 31 |
Environment Variables: |
| 32 |
DOMAIN: The area in which to generate forecasts for in the format |
| 33 |
(lng1, lat1, lng2, lat2). |
| 34 |
RESOLUTION: The length/width of each grid cell in degrees. |
| 35 |
|
| 36 |
""" |
| 37 |
|
| 38 |
from datetime import datetime, timedelta |
| 39 |
import os |
| 40 |
import sys |
| 41 |
|
| 42 |
import util |
| 43 |
|
| 44 |
|
| 45 |
SITE_LOCATIONS = { |
| 46 |
"exeter": (-3.533, 50.717), |
| 47 |
"london": (-0.1275, 51.507), |
| 48 |
"cardiff": (-3.183, 51.483), |
| 49 |
"edinburgh": (-3.189, 55.953), |
| 50 |
"belfast": (-5.930, 54.597) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def main(site_name, time): |
| 55 |
resolution = float(os.environ['RESOLUTION']) |
| 56 |
domain = util.parse_domain(os.environ['DOMAIN']) |
| 57 |
lng, lat = SITE_LOCATIONS[site_name] |
| 58 |
|
| 59 |
# Load forecast data. |
| 60 |
filename = '+PT%02dH%02dM.csv' % (time // 60, time % 60) |
| 61 |
try: |
| 62 |
# The path to the forecast data file. |
| 63 |
forecast = util.read_csv(os.path.join( |
| 64 |
os.environ['CYLC_WORKFLOW_WORK_DIR'], |
| 65 |
os.environ['CYLC_TASK_CYCLE_POINT'], |
| 66 |
'forecast', |
| 67 |
filename |
| 68 |
)) |
| 69 |
except IOError: |
| 70 |
sys.exit('Could not find forecast "%s".' % filename) |
| 71 |
|
| 72 |
# Calculate the time for which this forecast is valid. |
| 73 |
forecast_time = datetime.strptime( |
| 74 |
os.environ['CYLC_TASK_CYCLE_POINT'], '%Y%m%dT%H%MZ' |
| 75 |
) + timedelta(minutes=time) |
| 76 |
|
| 77 |
# Extract forecast for specified location. |
| 78 |
ind_x, ind_y = util.get_grid_coordinates(lng, lat, domain, resolution) |
| 79 |
predicted_rainfall = forecast[ind_y][ind_x] |
| 80 |
|
| 81 |
# Use forecast to choose an appropriate message. |
| 82 |
if predicted_rainfall < 0.1: |
| 83 |
message = 'Clear skies ahead!' |
| 84 |
elif predicted_rainfall < 0.5: |
| 85 |
message = 'A slim change of light rain.' |
| 86 |
elif predicted_rainfall < 2: |
| 87 |
message = 'Chance of a light rain.' |
| 88 |
elif predicted_rainfall < 3: |
| 89 |
message = 'Better take an umbrella.' |
| 90 |
elif predicted_rainfall < 4: |
| 91 |
message = 'Nice weather for ducks.' |
| 92 |
elif predicted_rainfall < 5: |
| 93 |
message = "Looks like it'll be chucking it down." |
| 94 |
else: |
| 95 |
message = 'Torrential downpour :(' |
| 96 |
|
| 97 |
# Write out forecast summary. |
| 98 |
with open('summary.txt', 'w+') as summary_file: |
| 99 |
summary_file.write('The outlook at %s UTC for %s is "%s"' % ( |
| 100 |
forecast_time.strftime('%Y-%m-%dT%H:%M'), site_name, message)) |
| 101 |
|
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
util.sleep() |
| 105 |
try: |
| 106 |
args = (sys.argv[1].lower(), int(sys.argv[2])) |
| 107 |
except IndexError: |
| 108 |
print(__doc__) |
| 109 |
sys.exit(1) |
| 110 |
main(*args) |