import sys
import unittest
import os
import tempfile
import numpy as NP
from numpy import ma
from numpy.testing import assert_array_equal, assert_array_almost_equal
from numpy.random.mtrand import uniform
import netCDF4

# test automatic conversion of masked arrays, and
# packing/unpacking of short ints.

# create an n1dim by n2dim random ranarr.
FILE_NAME = tempfile.mktemp(".nc")
ndim = 10
ranarr = 100.*uniform(size=(ndim))
ranarr2 = 100.*uniform(size=(ndim))
packeddata = 10.*uniform(size=(ndim))
missing_value = -9999.
missing_value2 = NP.nan
ranarr[::2] = missing_value
ranarr2[::2] = missing_value2
maskedarr = ma.masked_values(ranarr,missing_value)
maskedarr2 = ma.masked_values(ranarr2,missing_value2)
scale_factor = (packeddata.max()-packeddata.min())/(2.*32766.)
add_offset = 0.5*(packeddata.max()+packeddata.min())
packeddata2 = NP.around((packeddata-add_offset)/scale_factor).astype('i2')

class PrimitiveTypesTestCase(unittest.TestCase):

    def setUp(self):
        self.file = FILE_NAME
        file = netCDF4.Dataset(self.file,'w')
        file.createDimension('n', ndim)
        foo = file.createVariable('maskeddata', 'f8', ('n',))
        foo2 = file.createVariable('maskeddata2', 'f8', ('n',))
        foo.missing_value = missing_value
        foo.set_auto_maskandscale(True)
        foo2.missing_value = missing_value2
        foo2.set_auto_maskandscale(True)
        bar = file.createVariable('packeddata', 'i2', ('n',))
        bar.set_auto_maskandscale(True)
        bar.scale_factor = scale_factor
        bar.add_offset = add_offset
        foo[:] = maskedarr
        foo2[:] = maskedarr2
        bar[:] = packeddata
        # added to test fix to issue 46
        doh = file.createVariable('packeddata2','i2','n')
        doh.scale_factor = 0.1
        doh.add_offset = 0.
        doh[0] = 1.1
        file.close()

    def tearDown(self):
        # Remove the temporary files
        os.remove(self.file)

    def runTest(self):
        """testing auto-conversion of masked arrays and packed integers"""
        file = netCDF4.Dataset(self.file)
        datamasked = file.variables['maskeddata']
        datamasked2 = file.variables['maskeddata2']
        datapacked = file.variables['packeddata']
        datapacked2 = file.variables['packeddata2']
        # check missing_value, scale_factor and add_offset attributes.
        assert datamasked.missing_value == missing_value
        assert datapacked.scale_factor == scale_factor
        assert datapacked.add_offset == add_offset
        # no auto-conversion.
        datamasked.set_auto_maskandscale(False)
        datamasked2.set_auto_maskandscale(False)
        datapacked.set_auto_maskandscale(False)
        assert_array_equal(datapacked[:],packeddata2)
        assert_array_almost_equal(datamasked[:],ranarr)
        assert_array_almost_equal(datamasked2[:],ranarr2)
        # auto-conversion
        datamasked.set_auto_maskandscale(True)
        datamasked2.set_auto_maskandscale(True)
        datapacked.set_auto_maskandscale(True)
        datapacked2.set_auto_maskandscale(False)
        assert_array_almost_equal(datamasked[:].filled(),ranarr)
        assert_array_almost_equal(datamasked2[:].filled(),ranarr2)
        assert_array_almost_equal(datapacked[:],packeddata,decimal=4)
        # added to test fix to issue 46 (result before r865 was 10)
        assert_array_equal(datapacked2[0],11)
        file.close()

if __name__ == '__main__':
    unittest.main()

@@ -458,16 +458,200 @@
 (ndim))%0A
+# used for checking vector missing_values%0Aarr3 = NP.linspace(0,9,ndim)%0Amask = NP.zeros(ndim,NP.bool); mask%5B-1%5D=True; mask%5B-2%5D=True%0Amarr3 = NP.ma.array(arr3, mask=mask, dtype=NP.int32)%0A
 packedda
@@ -727,16 +727,39 @@
  NP.nan%0A
+missing_value3 = %5B8,9%5D%0A
 ranarr%5B:
@@ -1410,32 +1410,96 @@
 , 'f8', ('n',))%0A
+        foo3 = file.createVariable('maskeddata3', 'i4', ('n',))%0A
         foo.miss
@@ -1641,32 +1641,117 @@
 kandscale(True)%0A
+        foo3.missing_value = missing_value3%0A        foo3.set_auto_maskandscale(True)%0A
         bar = fi
@@ -1968,16 +1968,39 @@
 kedarr2%0A
+        foo3%5B:%5D = arr3%0A
         
@@ -2544,32 +2544,84 @@
 %5B'maskeddata2'%5D%0A
+        datamasked3 = file.variables%5B'maskeddata3'%5D%0A
         datapack
@@ -3107,32 +3107,32 @@
 andscale(False)%0A
-
         assert_a
@@ -3169,16 +3169,65 @@
 ddata2)%0A
+        assert_array_equal(datamasked3%5B:%5D,marr3)%0A
         

add test to verify that correct masked arrays are returned when vector missing_value attribute is present.