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

Revision 3626, 2.8 KB checked in by domlowe, 14 years ago (diff)

updated test script

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, -175, 36, 10)
69    grids['m180_180p'] = cdms.createUniformGrid(-87.5, 36, 5, -175, 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[dataset]
83dsID:testdataset123
84
85[features]
86type: GridSeries
87number: many
88
89[files]
90root: %(here)s/data
91mapping: onetoseveral
92output: %(here)s/data/test.csml
93printscreen:0
94
95[spatialaxes]
96spatialstorage:fileextract
97
98[values]
99valuestorage:fileextract
100
101
102[time]
103timedimension: time
104timestorage:inline
105''' % dict(here=here)
106    open('%s/data/csmlscan.cfg' % here, 'w').write(config)
107
108    csmlscan.main(['csmlscan', '-c', '%s/data/csmlscan.cfg' % here])
109
110
111
112def main(args=None):
113    # This trick ensures we always get the same CSML id's
114    random.seed(42)
115
116    here = os.path.abspath(os.path.dirname(__file__))
117    grids = make_grids(here)
118    make_csml(here)
119
120
121
122
123if __name__ == '__main__':
124    import sys
125    main(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.