1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
import tempfile
import os
import sys
import itertools
from operator import mul
import numpy
import cf
import unittest
class CoordinateTest(unittest.TestCase):
print 'cf version', cf.__version__, 'running from', os.path.abspath(cf.__file__)
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'test_file.nc')
chunk_sizes = (17, 34, 300, 100000)[::-1]
def test_Coordinate_squeeze(self):
'''cf.Coordinate.squeeze'''
print
a = numpy.arange(89.5, -90, -1)
b = numpy.empty(a.shape+(2,))
b[:,0] = a+0.5
b[:,1] = a-0.5
c = cf.Coordinate(data=cf.Data(a), bounds=cf.Data(b))
for chunksize in chunk_sizes[::-1]:
cf.CHUNKSIZE(chunksize)
self.assertTrue(c.equals(c.squeeze(), traceback=True))
print "cf.Coordinate.squeeze passed", "pmshape =", c.Data._pmshape
#--- End: def
def test_Coordinate_roll(self):
'''cf.Coordinate.roll'''
print
modulus = 10
for a in (numpy.arange(modulus), numpy.arange(modulus)[::-1]):
c = cf.DimensionCoordinate(data=cf.Data(a, 'km')) # Add bounds
c.period(cf.Data(1000*modulus, 'm'))
pmshape = c.Data._pmshape
for offset in (-16, -10, -6, 0, 6, 10, 16, 20):
d = c + offset
for shift in range(offset-21, offset+22):
if d.direction():
centre = (d.datum(-1)//modulus)*modulus
a0 = d.datum(0) - (shift % modulus)
if a0 <= centre - modulus:
a0 += modulus
a1 = a0 + modulus
step = 1
else:
centre = (d.datum(0)//modulus)*modulus
a0 = d.datum(0) + (shift % modulus)
if a0 >= centre + modulus:
a0 -= modulus
a1 = a0 - modulus
step = -1
e = d.roll(0, shift).array
b = numpy.arange(a0, a1, step)
self.assertTrue(
(e == b).all(),
'%s, shift=%s (%s), %s, %s' % (d.array, shift, shift%modulus, e, b))
#--- End: for
#--- End: for
#--- End: for
print "cf.DimensionCoordinate.roll passed", "pmshape =", pmshape
#--- End: def
def test_Coordinate_flip(self):
'''cf.Coordinate.flip'''
print
a = numpy.arange(89.5, -90, -1)
b = numpy.empty(a.shape+(2,))
b[:,0] = a+0.5
b[:,1] = a-0.5
c = cf.Coordinate(data=cf.Data(a), bounds=cf.Data(b))
for chunksize in chunk_sizes[::-1]:
cf.CHUNKSIZE(chunksize)
d1 = c.flip()
d1.flip(i=True)
self.assertTrue(c.equals(d1, traceback=True))
print "cf.Coordinate.flip passed", "pmshape =", c.Data._pmshape
#--- End: def
def test_Coordinate_transpose(self):
'''cf.Coordinate.flip'''
print
a = numpy.arange(89.5, -90, -1)
b = numpy.empty(a.shape+(2,))
b[:,0] = a+0.5
b[:,1] = a-0.5
c = cf.Coordinate(data=cf.Data(a), bounds=cf.Data(b))
for chunksize in chunk_sizes[::-1]:
cf.CHUNKSIZE(chunksize)
self.assertTrue(c.equals(c.transpose(), traceback=True))
print "cf.Coordinate.transpose passed", "pmshape =", c.Data._pmshape
#--- End: def
#--- End: class
if __name__ == '__main__':
original_chunksize = cf.CHUNKSIZE()
unittest.main()
cf.CHUNKSIZE(original_chunksize)
|