source: TI05-delivery/ows_framework/branches/ows_framework-ddp/ows_server/ows_server/models/ddp_data.py @ 2747

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-ddp/ows_server/ows_server/models/ddp_data.py@2747
Revision 2747, 2.9 KB checked in by spascoe, 12 years ago (diff)

Added Mercator stuff for Ag's demo.

Line 
1"""
2This module contains information needed to serve the pre-prepared demo images
3as real datasets.  Eventually 99% of this should come directly from the data.
4
5"""
6
7from pylons import request
8import Image
9
10import logging
11logger = logging.getLogger('ddp_data')
12
13# The variables for which we have image data
14variables = ['csolr', 'm1s1i207', 'm1s1i208', 'm1s1i209', 'm1s1i235',
15             'm1s3i463', 'olr', 'ps', 'snowdepth', 'temp', 'x-wind', 'y-wind' ]
16
17# The time domain of each variable
18time_domain = [
19    '1983-8-1T12:0:0.0Z', '1983-8-2T12:0:0.0Z', '1983-8-3T12:0:0.0Z', '1983-8-4T12:0:0.0Z',
20    '1983-8-5T12:0:0.0Z', '1983-8-6T12:0:0.0Z', '1983-8-7T12:0:0.0Z', '1983-8-8T12:0:0.0Z',
21    '1983-8-9T12:0:0.0Z', '1983-8-10T12:0:0.0Z', '1983-8-11T12:0:0.0Z', '1983-8-12T12:0:0.0Z',
22    '1983-8-13T12:0:0.0Z', '1983-8-14T12:0:0.0Z', '1983-8-15T12:0:0.0Z', '1983-8-16T12:0:0.0Z',
23    '1983-8-17T12:0:0.0Z', '1983-8-18T12:0:0.0Z', '1983-8-19T12:0:0.0Z', '1983-8-20T12:0:0.0Z',
24    '1983-8-21T12:0:0.0Z', '1983-8-22T12:0:0.0Z', '1983-8-23T12:0:0.0Z', '1983-8-24T12:0:0.0Z',
25    '1983-8-25T12:0:0.0Z', '1983-8-26T12:0:0.0Z', '1983-8-27T12:0:0.0Z', '1983-8-28T12:0:0.0Z',
26    '1983-8-29T12:0:0.0Z', '1983-8-30T12:0:0.0Z'
27    ]
28
29# The CRS:84 bounding box of these images
30#data_bbox = (-16.5, 47.0, 4.5, 61.5)
31
32# The bounding box in Mercator coordinates
33#!TODO: What is the CRS/SRS for this?
34data_bbox = (18180362.0, 65482228.0, 20515454.0, 68278566.0)
35
36# The image directory from the paste configuration
37image_dir = request.environ['paste.config']['app_conf']['ddp_image_dir']
38
39# The prefix of the image files
40image_prefix = 'rotgrid'
41
42def get_image(variable, time):
43    # Will raise ValueError if time is not correct
44    i = time_domain.index(time)
45
46    filename = '%s/%s_%s_%s.png' % (image_dir, image_prefix, variable, i)
47    return Image.open(filename)
48
49
50
51
52def get_image_subset(variable, time, r_bbox, width, height):
53    # Get the PIL image for this variable/time
54    img = get_image(variable, time)
55    (img_width, img_height) = img.size
56
57    x1, y1, x2, y2 = r_bbox
58    d_x1, d_y1, d_x2, d_y2 = data_bbox
59
60    x_res = (d_x2 - d_x1) / img_width
61    y_res = (d_y2 - d_y1) / img_height
62
63    logger.info('Image: size = (%s, %s).  Res = (%s, %s)' % (img_width, img_height,
64                                                             x_res, y_res))
65
66
67    # Transform to image coordinates
68    img_left = int((x1 - d_x1) / x_res)
69    img_lower = img_height - int((y1 - d_y1) / y_res)
70    img_right = int((x2 - d_x1) / x_res)
71    img_upper = img_height - int((y2 - d_y1) / y_res)
72
73    logger.info('Image coordinates (%s, %s, %s, %s)' %
74                (img_left, img_upper, img_right, img_lower))
75
76    # Subset.
77    #!TODO: Any non-overlapping area may contain random pixels.  We need to fix this.
78    img2 = img.crop((img_left, img_upper, img_right, img_lower))
79
80    # Resize resulting image
81    img2 = img2.resize((width, height))
82
83    return img2
84
Note: See TracBrowser for help on using the repository browser.