<s> '''<STR_LIT>''' <EOL> from __future__ import division , print_function , absolute_import <EOL> import os <EOL> from os . path import join as pjoin , dirname <EOL> import shutil <EOL> import tempfile <EOL> import warnings <EOL> from io import BytesIO <EOL> from glob import glob <EOL> from contextlib import contextmanager <EOL> import numpy as np <EOL> from numpy . testing import ( assert_ , assert_allclose , assert_raises , <EOL> assert_equal , run_module_suite ) <EOL> from scipy . io . netcdf import netcdf_file <EOL> from scipy . _lib . _tmpdirs import in_tempdir <EOL> TEST_DATA_PATH = pjoin ( dirname ( __file__ ) , '<STR_LIT:data>' ) <EOL> N_EG_ELS = <NUM_LIT:11> <EOL> VARTYPE_EG = '<STR_LIT:b>' <EOL> @ contextmanager <EOL> def make_simple ( * args , ** kwargs ) : <EOL> f = netcdf_file ( * args , ** kwargs ) <EOL> f . history = '<STR_LIT>' <EOL> f . createDimension ( '<STR_LIT:time>' , N_EG_ELS ) <EOL> time = f . createVariable ( '<STR_LIT:time>' , VARTYPE_EG , ( '<STR_LIT:time>' , ) ) <EOL> time [ : ] = np . arange ( N_EG_ELS ) <EOL> time . units = '<STR_LIT>' <EOL> f . flush ( ) <EOL> yield f <EOL> f . close ( ) <EOL> def check_simple ( ncfileobj ) : <EOL> '''<STR_LIT>''' <EOL> assert_equal ( ncfileobj . history , b'<STR_LIT>' ) <EOL> time = ncfileobj . variables [ '<STR_LIT:time>' ] <EOL> assert_equal ( time . units , b'<STR_LIT>' ) <EOL> assert_equal ( time . shape , ( N_EG_ELS , ) ) <EOL> assert_equal ( time [ - <NUM_LIT:1> ] , N_EG_ELS - <NUM_LIT:1> ) <EOL> def assert_mask_matches ( arr , expected_mask ) : <EOL> '''<STR_LIT>''' <EOL> mask = np . ma . getmaskarray ( arr ) <EOL> assert_equal ( mask , expected_mask ) <EOL> def test_read_write_files ( ) : <EOL> cwd = os . getcwd ( ) <EOL> try : <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> os . chdir ( tmpdir ) <EOL> with make_simple ( '<STR_LIT>' , '<STR_LIT:w>' ) as f : <EOL> pass <EOL> with netcdf_file ( '<STR_LIT>' , '<STR_LIT:a>' ) as f : <EOL> check_simple ( f ) <EOL> f . _attributes [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> with netcdf_file ( '<STR_LIT>' ) as f : <EOL> assert_ ( f . use_mmap ) <EOL> check_simple ( f ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> with netcdf_file ( '<STR_LIT>' , '<STR_LIT:a>' ) as f : <EOL> assert_ ( not f . use_mmap ) <EOL> check_simple ( f ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> with netcdf_file ( '<STR_LIT>' , mmap = False ) as f : <EOL> assert_ ( not f . use_mmap ) <EOL> check_simple ( f ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as fobj : <EOL> with netcdf_file ( fobj ) as f : <EOL> assert_ ( not f . use_mmap ) <EOL> check_simple ( f ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as fobj : <EOL> with netcdf_file ( fobj , mmap = True ) as f : <EOL> assert_ ( f . use_mmap ) <EOL> check_simple ( f ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fobj : <EOL> with netcdf_file ( fobj , '<STR_LIT:a>' ) as f : <EOL> assert_ ( not f . use_mmap ) <EOL> check_simple ( f ) <EOL> f . createDimension ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> var = f . createVariable ( '<STR_LIT>' , '<STR_LIT:i>' , ( '<STR_LIT>' , ) ) <EOL> var [ : ] = <NUM_LIT> <EOL> with netcdf_file ( '<STR_LIT>' ) as f : <EOL> check_simple ( f ) <EOL> assert_equal ( f . variables [ '<STR_LIT>' ] [ : ] , <NUM_LIT> ) <EOL> except : <EOL> os . chdir ( cwd ) <EOL> shutil . rmtree ( tmpdir ) <EOL> raise <EOL> os . chdir ( cwd ) <EOL> shutil . rmtree ( tmpdir ) <EOL> def test_read_write_sio ( ) : <EOL> eg_sio1 = BytesIO ( ) <EOL> with make_simple ( eg_sio1 , '<STR_LIT:w>' ) as f1 : <EOL> str_val = eg_sio1 . getvalue ( ) <EOL> eg_sio2 = BytesIO ( str_val ) <EOL> with netcdf_file ( eg_sio2 ) as f2 : <EOL> check_simple ( f2 ) <EOL> eg_sio3 = BytesIO ( str_val ) <EOL> assert_raises ( ValueError , netcdf_file , eg_sio3 , '<STR_LIT:r>' , True ) <EOL> eg_sio_64 = BytesIO ( ) <EOL> with make_simple ( eg_sio_64 , '<STR_LIT:w>' , version = <NUM_LIT:2> ) as f_64 : <EOL> str_val = eg_sio_64 . getvalue ( ) <EOL> eg_sio_64 = BytesIO ( str_val ) <EOL> with netcdf_file ( eg_sio_64 ) as f_64 : <EOL> check_simple ( f_64 ) <EOL> assert_equal ( f_64 . version_byte , <NUM_LIT:2> ) <EOL> eg_sio_64 = BytesIO ( str_val ) <EOL> with netcdf_file ( eg_sio_64 , version = <NUM_LIT:2> ) as f_64 : <EOL> check_simple ( f_64 ) <EOL> assert_equal ( f_64 . version_byte , <NUM_LIT:2> ) <EOL> def test_read_example_data ( ) : <EOL> for fname in glob ( pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) ) : <EOL> with netcdf_file ( fname , '<STR_LIT:r>' ) as f : <EOL> pass <EOL> with netcdf_file ( fname , '<STR_LIT:r>' , mmap = False ) as f : <EOL> pass <EOL> def test_itemset_no_segfault_on_readonly ( ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> filename = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( filename , '<STR_LIT:r>' ) as f : <EOL> time_var = f . variables [ '<STR_LIT:time>' ] <EOL> assert_raises ( RuntimeError , time_var . assignValue , <NUM_LIT> ) <EOL> def test_write_invalid_dtype ( ) : <EOL> dtypes = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if np . dtype ( '<STR_LIT:int>' ) . itemsize == <NUM_LIT:8> : <EOL> dtypes . append ( '<STR_LIT:int>' ) <EOL> if np . dtype ( '<STR_LIT>' ) . itemsize == <NUM_LIT:8> : <EOL> dtypes . append ( '<STR_LIT>' ) <EOL> with netcdf_file ( BytesIO ( ) , '<STR_LIT:w>' ) as f : <EOL> f . createDimension ( '<STR_LIT:time>' , N_EG_ELS ) <EOL> for dt in dtypes : <EOL> assert_raises ( ValueError , f . createVariable , '<STR_LIT:time>' , dt , ( '<STR_LIT:time>' , ) ) <EOL> def test_flush_rewind ( ) : <EOL> stream = BytesIO ( ) <EOL> with make_simple ( stream , mode = '<STR_LIT:w>' ) as f : <EOL> x = f . createDimension ( '<STR_LIT:x>' , <NUM_LIT:4> ) <EOL> v = f . createVariable ( '<STR_LIT:v>' , '<STR_LIT>' , [ '<STR_LIT:x>' ] ) <EOL> v [ : ] = <NUM_LIT:1> <EOL> f . flush ( ) <EOL> len_single = len ( stream . getvalue ( ) ) <EOL> f . flush ( ) <EOL> len_double = len ( stream . getvalue ( ) ) <EOL> assert_ ( len_single == len_double ) <EOL> def test_dtype_specifiers ( ) : <EOL> with make_simple ( BytesIO ( ) , mode = '<STR_LIT:w>' ) as f : <EOL> f . createDimension ( '<STR_LIT:x>' , <NUM_LIT:4> ) <EOL> f . createVariable ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT:x>' ] ) <EOL> f . createVariable ( '<STR_LIT>' , np . int16 , [ '<STR_LIT:x>' ] ) <EOL> f . createVariable ( '<STR_LIT>' , np . dtype ( np . int16 ) , [ '<STR_LIT:x>' ] ) <EOL> def test_ticket_1720 ( ) : <EOL> io = BytesIO ( ) <EOL> items = [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> with netcdf_file ( io , '<STR_LIT:w>' ) as f : <EOL> f . history = '<STR_LIT>' <EOL> f . createDimension ( '<STR_LIT>' , <NUM_LIT:10> ) <EOL> float_var = f . createVariable ( '<STR_LIT>' , '<STR_LIT:f>' , ( '<STR_LIT>' , ) ) <EOL> float_var [ : ] = items <EOL> float_var . units = '<STR_LIT>' <EOL> f . flush ( ) <EOL> contents = io . getvalue ( ) <EOL> io = BytesIO ( contents ) <EOL> with netcdf_file ( io , '<STR_LIT:r>' ) as f : <EOL> assert_equal ( f . history , b'<STR_LIT>' ) <EOL> float_var = f . variables [ '<STR_LIT>' ] <EOL> assert_equal ( float_var . units , b'<STR_LIT>' ) <EOL> assert_equal ( float_var . shape , ( <NUM_LIT:10> , ) ) <EOL> assert_allclose ( float_var [ : ] , items ) <EOL> def test_mmaps_segfault ( ) : <EOL> filename = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:error>" ) <EOL> with netcdf_file ( filename , mmap = True ) as f : <EOL> x = f . variables [ '<STR_LIT>' ] [ : ] <EOL> del x <EOL> def doit ( ) : <EOL> with netcdf_file ( filename , mmap = True ) as f : <EOL> return f . variables [ '<STR_LIT>' ] [ : ] <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> x = doit ( ) <EOL> x . sum ( ) <EOL> def test_zero_dimensional_var ( ) : <EOL> io = BytesIO ( ) <EOL> with make_simple ( io , '<STR_LIT:w>' ) as f : <EOL> v = f . createVariable ( '<STR_LIT>' , '<STR_LIT>' , [ ] ) <EOL> assert v . isrec is False , v . isrec <EOL> f . flush ( ) <EOL> def test_byte_gatts ( ) : <EOL> with in_tempdir ( ) : <EOL> filename = '<STR_LIT>' <EOL> f = netcdf_file ( filename , '<STR_LIT:w>' ) <EOL> f . _attributes [ '<STR_LIT>' ] = b'<STR_LIT>' <EOL> f . _attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> f . close ( ) <EOL> f = netcdf_file ( filename , '<STR_LIT:r>' ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> def test_open_append ( ) : <EOL> with in_tempdir ( ) : <EOL> filename = '<STR_LIT>' <EOL> f = netcdf_file ( filename , '<STR_LIT:w>' ) <EOL> f . _attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> f . close ( ) <EOL> f = netcdf_file ( filename , '<STR_LIT:a>' ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> f . _attributes [ '<STR_LIT>' ] = b'<STR_LIT>' <EOL> f . close ( ) <EOL> f = netcdf_file ( filename , '<STR_LIT:r>' ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> assert_equal ( f . _attributes [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> def test_maskandscale ( ) : <EOL> t = np . linspace ( <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:15> ) <EOL> t [ <NUM_LIT:3> ] = <NUM_LIT:100> <EOL> tm = np . ma . masked_greater ( t , <NUM_LIT> ) <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> Temp = f . variables [ '<STR_LIT>' ] <EOL> assert_equal ( Temp . missing_value , <NUM_LIT> ) <EOL> assert_equal ( Temp . add_offset , <NUM_LIT:20> ) <EOL> assert_equal ( Temp . scale_factor , np . float32 ( <NUM_LIT> ) ) <EOL> found = Temp [ : ] . compressed ( ) <EOL> del Temp <EOL> expected = np . round ( tm . compressed ( ) , <NUM_LIT:2> ) <EOL> assert_allclose ( found , expected ) <EOL> with in_tempdir ( ) : <EOL> newfname = '<STR_LIT>' <EOL> f = netcdf_file ( newfname , '<STR_LIT:w>' , maskandscale = True ) <EOL> f . createDimension ( '<STR_LIT>' , len ( tm ) ) <EOL> temp = f . createVariable ( '<STR_LIT>' , '<STR_LIT:i>' , ( '<STR_LIT>' , ) ) <EOL> temp . missing_value = <NUM_LIT> <EOL> temp . scale_factor = <NUM_LIT> <EOL> temp . add_offset = <NUM_LIT:20> <EOL> temp [ : ] = tm <EOL> f . close ( ) <EOL> with netcdf_file ( newfname , maskandscale = True ) as f : <EOL> Temp = f . variables [ '<STR_LIT>' ] <EOL> assert_equal ( Temp . missing_value , <NUM_LIT> ) <EOL> assert_equal ( Temp . add_offset , <NUM_LIT:20> ) <EOL> assert_equal ( Temp . scale_factor , np . float32 ( <NUM_LIT> ) ) <EOL> expected = np . round ( tm . compressed ( ) , <NUM_LIT:2> ) <EOL> found = Temp [ : ] . compressed ( ) <EOL> del Temp <EOL> assert_allclose ( found , expected ) <EOL> def test_read_withValuesNearFillValue ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , True , False ] ) <EOL> def test_read_withNoFillValue ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , False , False ] ) <EOL> assert_equal ( vardata , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_read_withFillValueAndMissingValue ( ) : <EOL> IRRELEVANT_VALUE = <NUM_LIT> <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ True , False , False ] ) <EOL> assert_equal ( vardata , [ IRRELEVANT_VALUE , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_read_withMissingValue ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , True , False ] ) <EOL> def test_read_withFillValNaN ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , True , False ] ) <EOL> def test_read_withChar ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , True , False ] ) <EOL> def test_read_with2dVar ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = True ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ [ True , False ] , [ False , False ] , [ False , True ] ] ) <EOL> def test_read_withMaskAndScaleFalse ( ) : <EOL> fname = pjoin ( TEST_DATA_PATH , '<STR_LIT>' ) <EOL> with netcdf_file ( fname , maskandscale = False , mmap = False ) as f : <EOL> vardata = f . variables [ '<STR_LIT>' ] [ : ] <EOL> assert_mask_matches ( vardata , [ False , False , False ] ) <EOL> assert_equal ( vardata , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
