@@ -0,0 +1,2600 @@
+from numpy.random.mtrand import uniform%0Afrom netCDF4 import Dataset, _quantize%0Afrom numpy.testing import assert_almost_equal%0Aimport os, tempfile, unittest%0A%0Andim = 100000%0Anfiles = 5%0Afiles = %5Btempfile.mktemp(%22.nc%22) for nfile in range(nfiles)%5D%0Aarray = uniform(size=(ndim,))%0Alsd = 3%0A%0Adef write_netcdf(filename,zlib,least_significant_digit,data,dtype='f8',shuffle=False,chunking='seq',complevel=6):%0A    file = Dataset(filename,'w')%0A    file.createDimension('n', ndim)%0A    foo = file.createVariable('data', dtype,('n'),zlib=zlib,least_significant_digit=least_significant_digit,shuffle=shuffle,chunking=chunking,complevel=complevel)%0A    foo%5B:%5D = data%0A    file.close()%0A    file = Dataset(filename)%0A    data = file.variables%5B'data'%5D%5B:%5D%0A%0Aclass CompressionTestCase(unittest.TestCase):%0A%0A    def setUp(self):%0A        self.files = files%0A        # no compression%0A        write_netcdf(self.files%5B0%5D,False,None,array)%0A        # compressed, lossless, no shuffle.%0A        write_netcdf(self.files%5B1%5D,True,None,array)%0A        # compressed, lossless, with shuffle.%0A        write_netcdf(self.files%5B2%5D,True,None,array,shuffle=True)%0A        # compressed, lossy, no shuffle.%0A        write_netcdf(self.files%5B3%5D,True,lsd,array)%0A        # compressed, lossy, with shuffle.%0A        write_netcdf(self.files%5B4%5D,True,lsd,array,shuffle=True)%0A%0A    def tearDown(self):%0A        # Remove the temporary files%0A        for file in self.files:%0A            os.remove(file)%0A%0A    def runTest(self):%0A        %22%22%22testing zlib and shuffle compression filters%22%22%22%0A        uncompressed_size = os.stat(self.files%5B0%5D).st_size%0A        # check compressed data.%0A        f = Dataset(self.files%5B1%5D)%0A        size = os.stat(self.files%5B1%5D).st_size%0A        assert_almost_equal(array,f.variables%5B'data'%5D%5B:%5D)%0A        assert(size %3C 0.95*uncompressed_size)%0A        f.close()%0A        # check compression with shuffle%0A        f = Dataset(self.files%5B2%5D)%0A        size = os.stat(self.files%5B2%5D).st_size%0A        assert_almost_equal(array,f.variables%5B'data'%5D%5B:%5D)%0A        assert(size %3C 0.85*uncompressed_size)%0A        # check lossy compression without shuffle%0A        f = Dataset(self.files%5B3%5D)%0A        size = os.stat(self.files%5B3%5D).st_size%0A        checkarray = _quantize(array,lsd)%0A        assert_almost_equal(checkarray,f.variables%5B'data'%5D%5B:%5D)%0A        assert(size %3C 0.26*uncompressed_size)%0A        # check lossy compression with shuffle%0A        f = Dataset(self.files%5B4%5D)%0A        size = os.stat(self.files%5B4%5D).st_size%0A        assert_almost_equal(checkarray,f.variables%5B'data'%5D%5B:%5D)%0A        assert(size %3C 0.20*uncompressed_size)%0A%0Aif __name__ == '__main__':%0A    unittest.main()%0A

add unittest for compression filters.