source: TI02-CSML/trunk/csml/tests/make_data.py @ 2623

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/tests/make_data.py@2623
Revision 2623, 2.8 KB checked in by spascoe, 13 years ago (diff)

Whoops, missed the unit test files.

Line 
1"""
2Create test data on several different grids.
3
4"""
5
6import cdms
7import Numeric as N
8import os
9import random
10from csml import csmlscan
11
12
13
14
15def make_var_old(grid):
16    """
17    Make a variable on a given grid with an X in it.
18    The first column and row are filled with their respective axis values.
19
20    """
21    i = N.indices(grid.shape)
22    a = N.where(i[0] == i[1], 1000., 1.)
23    a = N.where(i[0][-1::-1,:] == i[1], 1000., a)
24
25    a[0] = grid.getLatitude().getValue()
26    a[:,0] = grid.getLongitude().getValue()
27
28    a = N.array([a])
29
30    t = cdms.createAxis(N.array([0]))
31    t.id = 'time'
32    t.units = 'days since 2000-01-01'
33    t.designateTime()
34
35    return cdms.createVariable(a, axes=[t, grid.getLatitude(),
36                                        grid.getLongitude()])
37
38
39def make_var(grid):
40    """
41    Make a variable on a given grid filled with values according to it's axes.
42    Each grid point is filled with the sum of it's lat & lon coordinates     
43    """
44    lat = grid.getLatitude().getValue(); nlat = len(lat)
45    lon = grid.getLongitude().getValue(); nlon = len(lon)
46
47    lat_m = N.ones((nlat, nlon)) * N.reshape(lat, (nlat, 1))
48    lon_m = N.ones((nlat, nlon)) * lon
49
50    a = lat_m + lon_m
51
52    a = N.array([a])
53
54    t = cdms.createAxis(N.array([0]))
55    t.id = 'time'
56    t.units = 'days since 2000-01-01'
57    t.designateTime()
58
59    return cdms.createVariable(a, axes=[t, grid.getLatitude(),
60                                        grid.getLongitude()])
61
62
63def make_grids(here):
64    # 4 common lat/lon orderings
65    grids = {}
66    grids['0_360p'] = cdms.createUniformGrid(-87.5, 36, 5, 5, 36, 10)
67    grids['0_360m'] = cdms.createUniformGrid(87.5, 36, -5, 5, 36, 10)
68    grids['m180_180m'] = cdms.createUniformGrid(87.5, 36, -5, -185, 36, 10)
69    grids['m180_180p'] = cdms.createUniformGrid(-87.5, 36, 5, -185, 36, 10)
70
71    for gname in grids:
72        f = cdms.open('%s/data/test_%s.nc' % (here, gname), 'w')
73        f.write(make_var(grids[gname]), id='test_%s' % gname,
74                #axes=[grids[gname].getLatitude(), grids[gname].getLongitude()],
75                )
76        f.close()
77
78    return grids
79
80def make_csml(here):
81    config = '''
82[features]
83type: GridSeries
84number: many
85
86[files]
87root: %(here)s/data
88mapping: onetoseveral
89output: %(here)s/data/test.csml
90printscreen:0
91
92[spatialaxes]
93spatialstorage:fileextract
94
95[values]
96valuestorage:fileextract
97
98[SpatialReference]
99srs: EPSG 4326
100
101[time]
102timedimension: time
103timestorage:inline
104''' % dict(here=here)
105    open('%s/data/csmlscan.cfg' % here, 'w').write(config)
106
107    csmlscan.main(['csmlscan', '-c', '%s/data/csmlscan.cfg' % here])
108
109
110
111def main(args=None):
112    # This trick ensures we always get the same CSML id's
113    random.seed(42)
114
115    here = os.path.abspath(os.path.dirname(__file__))
116    grids = make_grids(here)
117    make_csml(here)
118
119
120
121
122if __name__ == '__main__':
123    import sys
124    main(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.