source: TI02-CSML/branches/csml-cdms2/tests/make_data.py @ 3627

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/branches/csml-cdms2/tests/make_data.py@3627
Revision 3627, 2.9 KB checked in by spascoe, 12 years ago (diff)

This branch contains CSML converted to use cdat_lite-5.

  • convertcdms was run on the source
  • the MA.set_print_limit call was changed to the numpy equivilent
  • The tests were changed to account for the existence of numpy scalar types.

All tests, except the two known to fail, pass on i686 ubuntu.

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