<s> import unittest <EOL> import os <EOL> import tempfile <EOL> import numpy as np <EOL> from numpy import ma <EOL> from numpy . testing import assert_array_almost_equal <EOL> from netCDF4 import Dataset , default_fillvals <EOL> class SetAutoMaskTestBase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . testfile = tempfile . NamedTemporaryFile ( suffix = '<STR_LIT>' , delete = False ) . name <EOL> self . fillval = default_fillvals [ "<STR_LIT>" ] <EOL> self . v = np . array ( [ self . fillval , <NUM_LIT:5> , <NUM_LIT:4> , - <NUM_LIT> ] , dtype = "<STR_LIT>" ) <EOL> self . v_ma = ma . array ( [ self . fillval , <NUM_LIT:5> , <NUM_LIT:4> , - <NUM_LIT> ] , dtype = "<STR_LIT>" , mask = [ True , False , False , True ] ) <EOL> self . scale_factor = <NUM_LIT> <EOL> self . add_offset = <NUM_LIT> <EOL> self . v_scaled = self . v * self . scale_factor + self . add_offset <EOL> self . v_ma_scaled = self . v_ma * self . scale_factor + self . add_offset <EOL> f = Dataset ( self . testfile , '<STR_LIT:w>' ) <EOL> _ = f . createDimension ( '<STR_LIT:x>' , None ) <EOL> v = f . createVariable ( '<STR_LIT:v>' , "<STR_LIT>" , '<STR_LIT:x>' ) <EOL> v . missing_value = np . array ( - <NUM_LIT> , v . dtype ) <EOL> v [ <NUM_LIT:1> ] = self . v [ <NUM_LIT:1> ] <EOL> v [ <NUM_LIT:2> ] = self . v [ <NUM_LIT:2> ] <EOL> v [ <NUM_LIT:3> ] = v . missing_value <EOL> f . close ( ) <EOL> def tearDown ( self ) : <EOL> os . remove ( self . testfile ) <EOL> class SetAutoMaskFalse ( SetAutoMaskTestBase ) : <EOL> def test_unscaled ( self ) : <EOL> """<STR_LIT>""" <EOL> f = Dataset ( self . testfile , "<STR_LIT:r>" ) <EOL> f . variables [ "<STR_LIT:v>" ] . set_auto_mask ( False ) <EOL> v = f . variables [ "<STR_LIT:v>" ] [ : ] <EOL> self . assertEqual ( v . dtype , "<STR_LIT>" ) <EOL> self . assert_ ( isinstance ( v , np . ndarray ) ) <EOL> self . assert_ ( not isinstance ( v , ma . core . MaskedArray ) ) <EOL> assert_array_almost_equal ( v , self . v ) <EOL> f . close ( ) <EOL> def test_scaled ( self ) : <EOL> """<STR_LIT>""" <EOL> f = Dataset ( self . testfile , "<STR_LIT:a>" ) <EOL> f . variables [ "<STR_LIT:v>" ] . scale_factor = self . scale_factor <EOL> f . variables [ "<STR_LIT:v>" ] . add_offset = self . add_offset <EOL> f . close ( ) <EOL> f = Dataset ( self . testfile , "<STR_LIT:r>" ) <EOL> f . variables [ "<STR_LIT:v>" ] . set_auto_mask ( False ) <EOL> v = f . variables [ "<STR_LIT:v>" ] [ : ] <EOL> self . assertEqual ( v . dtype , "<STR_LIT>" ) <EOL> self . assert_ ( isinstance ( v , np . ndarray ) ) <EOL> self . assert_ ( not isinstance ( v , ma . core . MaskedArray ) ) <EOL> assert_array_almost_equal ( v , self . v_scaled ) <EOL> f . close ( ) <EOL> class SetAutoMaskTrue ( SetAutoMaskTestBase ) : <EOL> def test_unscaled ( self ) : <EOL> """<STR_LIT>""" <EOL> f = Dataset ( self . testfile ) <EOL> f . variables [ "<STR_LIT:v>" ] . set_auto_mask ( True ) <EOL> v_ma = f . variables [ '<STR_LIT:v>' ] [ : ] <EOL> self . assertEqual ( v_ma . dtype , "<STR_LIT>" ) <EOL> self . assert_ ( isinstance ( v_ma , np . ndarray ) ) <EOL> self . assert_ ( isinstance ( v_ma , ma . core . MaskedArray ) ) <EOL> assert_array_almost_equal ( v_ma , self . v_ma ) <EOL> f . close ( ) <EOL> def test_scaled ( self ) : <EOL> """<STR_LIT>""" <EOL> f = Dataset ( self . testfile , "<STR_LIT:a>" ) <EOL> f . variables [ "<STR_LIT:v>" ] . scale_factor = self . scale_factor <EOL> f . variables [ "<STR_LIT:v>" ] . add_offset = self . add_offset <EOL> f . close ( ) <EOL> f = Dataset ( self . testfile ) <EOL> f . variables [ "<STR_LIT:v>" ] . set_auto_mask ( True ) <EOL> v_ma = f . variables [ '<STR_LIT:v>' ] [ : ] <EOL> self . assertEqual ( v_ma . dtype , "<STR_LIT>" ) <EOL> self . assert_ ( isinstance ( v_ma , np . ndarray ) ) <EOL> self . assert_ ( isinstance ( v_ma , ma . core . MaskedArray ) ) <EOL> assert_array_almost_equal ( v_ma , self . v_ma_scaled ) <EOL> f . close ( ) <EOL> class GlobalSetAutoMaskTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . testfile = tempfile . NamedTemporaryFile ( suffix = '<STR_LIT>' , delete = False ) . name <EOL> f = Dataset ( self . testfile , '<STR_LIT:w>' ) <EOL> grp1 = f . createGroup ( '<STR_LIT>' ) <EOL> grp2 = f . createGroup ( '<STR_LIT>' ) <EOL> f . createGroup ( '<STR_LIT>' ) <EOL> f . createVariable ( '<STR_LIT>' , "<STR_LIT>" , ( ) ) <EOL> grp1 . createVariable ( '<STR_LIT>' , '<STR_LIT>' , ( ) ) <EOL> grp2 . createVariable ( '<STR_LIT>' , '<STR_LIT>' , ( ) ) <EOL> f . close ( ) <EOL> def tearDown ( self ) : <EOL> os . remove ( self . testfile ) <EOL> def runTest ( self ) : <EOL> f = Dataset ( self . testfile , "<STR_LIT:r>" ) <EOL> f . set_auto_maskandscale ( False ) <EOL> v0 = f . variables [ '<STR_LIT>' ] <EOL> v1 = f . groups [ '<STR_LIT>' ] . variables [ '<STR_LIT>' ] <EOL> v2 = f . groups [ '<STR_LIT>' ] . variables [ '<STR_LIT>' ] <EOL> self . assertFalse ( v0 . scale ) <EOL> self . assertFalse ( v0 . mask ) <EOL> self . assertFalse ( v1 . scale ) <EOL> self . assertFalse ( v1 . mask ) <EOL> self . assertFalse ( v2 . scale ) <EOL> self . assertFalse ( v2 . mask ) <EOL> f . set_auto_maskandscale ( True ) <EOL> f . set_auto_mask ( False ) <EOL> self . assertTrue ( v0 . scale ) <EOL> self . assertFalse ( v0 . mask ) <EOL> self . assertTrue ( v1 . scale ) <EOL> self . assertFalse ( v1 . mask ) <EOL> self . assertTrue ( v2 . scale ) <EOL> self . assertFalse ( v2 . mask ) <EOL> f . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
