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

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@2707
Revision 2707, 2.7 KB checked in by spascoe, 12 years ago (diff)

Basic WMS serving pre-generated images is working. Most of
the configuration stuff is in models/ddp_data.py for now.

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
30data_bbox = (-16.5, 47.0, 4.5, 61.5)
31
32# The image directory from the paste configuration
33image_dir = request.environ['paste.config']['app_conf']['ddp_image_dir']
34
35# The prefix of the image files
36image_prefix = 'rotgrid'
37
38def get_image(variable, time):
39    # Will raise ValueError if time is not correct
40    i = time_domain.index(time)
41
42    filename = '%s/%s_%s_%s.png' % (image_dir, image_prefix, variable, i)
43    return Image.open(filename)
44
45
46
47
48def get_image_subset(variable, time, r_bbox, width, height):
49    # Get the PIL image for this variable/time
50    img = get_image(variable, time)
51    (img_width, img_height) = img.size
52
53    x1, y1, x2, y2 = r_bbox
54    d_x1, d_y1, d_x2, d_y2 = data_bbox
55
56    x_res = (d_x2 - d_x1) / img_width
57    y_res = (d_y2 - d_y1) / img_height
58
59    logger.info('Image: size = (%s, %s).  Res = (%s, %s)' % (img_width, img_height,
60                                                             x_res, y_res))
61
62
63    # Transform to image coordinates
64    img_left = int((x1 - d_x1) / x_res)
65    img_lower = img_height - int((y1 - d_y1) / y_res)
66    img_right = int((x2 - d_x1) / x_res)
67    img_upper = img_height - int((y2 - d_y1) / y_res)
68
69    logger.info('Image coordinates (%s, %s, %s, %s)' %
70                (img_left, img_upper, img_right, img_lower))
71
72    # Subset.
73    #!TODO: Any non-overlapping area may contain random pixels.  We need to fix this.
74    img2 = img.crop((img_left, img_upper, img_right, img_lower))
75
76    # Resize resulting image
77    img2 = img2.resize((width, height))
78
79    return img2
80
Note: See TracBrowser for help on using the repository browser.