source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/grid_util.py @ 2580

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/grid_util.py@2580
Revision 2580, 1.3 KB checked in by spascoe, 13 years ago (diff)

Code to convert CDMS grids to uniform grids. Required before they can be
rendered to images with PIL.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Code to handle cdms grids for rendering in WMS.
7
8@author: Stephen Pascoe
9
10"""
11
12import Numeric as N
13import cdms
14
15def is_uniform_grid(obj):
16    """
17    Test whether a cdms object is on a uniform grid, i.e. has equal point spacing.
18
19    """
20    def check_axis(a):
21        delta = a[1:] - a[:-1]
22        if N.alltrue(delta == delta[0]):
23            return True
24        else:
25            return False
26
27    if not check_axis(obj.getLongitude()):
28        return False
29    return check_axis(obj.getLatitude())
30
31def var_to_uniform_grid(var):
32    """
33    Returns a copy of a cdms variable interpolated onto a uniform grid.
34
35    The grid spacing is chosen from the minimum spacing of the source grid.
36
37    """
38    # Longitude
39    lon = var.getLongitude()
40    deltaLon = min(lon[1:] - lon[:-1])
41    nLon = (lon[-1] - lon[0]) / deltaLon
42
43    # Latitude
44    lat = var.getLatitude()
45    deltaLat = min(lat[1:] - lat[:-1])
46    nLat = (lat[-1] - lat[0]) / deltaLat
47
48    g = cdms.createUniformGrid(lat[0], nLat, deltaLat, lon[0], nLon, deltaLon)
49
50    return var.regrid(g)
Note: See TracBrowser for help on using the repository browser.