<s> from io import BytesIO <EOL> import numpy as np <EOL> import warnings <EOL> from . . import Variable <EOL> from . . conventions import cf_encoder <EOL> from . . core . pycompat import iteritems , basestring , OrderedDict <EOL> from . . core . utils import Frozen , FrozenOrderedDict <EOL> from . . core . indexing import NumpyIndexingAdapter <EOL> from . common import WritableCFDataStore <EOL> from . netcdf3 import ( is_valid_nc3_name , encode_nc3_attr_value , <EOL> encode_nc3_variable ) <EOL> def _decode_string ( s ) : <EOL> if isinstance ( s , bytes ) : <EOL> return s . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> return s <EOL> def _decode_attrs ( d ) : <EOL> return OrderedDict ( ( k , v if k == '<STR_LIT>' else _decode_string ( v ) ) <EOL> for ( k , v ) in iteritems ( d ) ) <EOL> class ScipyArrayWrapper ( NumpyIndexingAdapter ) : <EOL> def __init__ ( self , netcdf_file , variable_name ) : <EOL> self . netcdf_file = netcdf_file <EOL> self . variable_name = variable_name <EOL> @ property <EOL> def array ( self ) : <EOL> return self . netcdf_file . variables [ self . variable_name ] . data <EOL> @ property <EOL> def dtype ( self ) : <EOL> return np . dtype ( self . array . dtype . kind + str ( self . array . dtype . itemsize ) ) <EOL> def __getitem__ ( self , key ) : <EOL> data = super ( ScipyArrayWrapper , self ) . __getitem__ ( key ) <EOL> copy = self . netcdf_file . use_mmap <EOL> data = np . array ( data , dtype = self . dtype , copy = copy ) <EOL> return data <EOL> class ScipyDataStore ( WritableCFDataStore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename_or_obj , mode = '<STR_LIT:r>' , format = None , group = None , <EOL> writer = None , mmap = None ) : <EOL> import scipy <EOL> import scipy . io <EOL> if mode != '<STR_LIT:r>' and scipy . __version__ < '<STR_LIT>' : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % scipy . __version__ , ImportWarning ) <EOL> if group is not None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if format is None or format == '<STR_LIT>' : <EOL> version = <NUM_LIT:2> <EOL> elif format == '<STR_LIT>' : <EOL> version = <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % format ) <EOL> if ( isinstance ( filename_or_obj , basestring ) and <EOL> filename_or_obj . startswith ( '<STR_LIT>' ) ) : <EOL> filename_or_obj = BytesIO ( filename_or_obj ) <EOL> self . ds = scipy . io . netcdf_file ( <EOL> filename_or_obj , mode = mode , mmap = mmap , version = version ) <EOL> super ( ScipyDataStore , self ) . __init__ ( writer ) <EOL> def open_store_variable ( self , name , var ) : <EOL> return Variable ( var . dimensions , ScipyArrayWrapper ( self . ds , name ) , <EOL> _decode_attrs ( var . _attributes ) ) <EOL> def get_variables ( self ) : <EOL> return FrozenOrderedDict ( ( k , self . open_store_variable ( k , v ) ) <EOL> for k , v in iteritems ( self . ds . variables ) ) <EOL> def get_attrs ( self ) : <EOL> return Frozen ( _decode_attrs ( self . ds . _attributes ) ) <EOL> def get_dimensions ( self ) : <EOL> return Frozen ( self . ds . dimensions ) <EOL> def set_dimension ( self , name , length ) : <EOL> if name in self . dimensions : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % type ( self ) . __name__ ) <EOL> self . ds . createDimension ( name , length ) <EOL> def _validate_attr_key ( self , key ) : <EOL> if not is_valid_nc3_name ( key ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def set_attribute ( self , key , value ) : <EOL> self . _validate_attr_key ( key ) <EOL> value = encode_nc3_attr_value ( value ) <EOL> setattr ( self . ds , key , value ) <EOL> def prepare_variable ( self , name , variable , check_encoding = False ) : <EOL> variable = encode_nc3_variable ( variable ) <EOL> if check_encoding and variable . encoding : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % list ( variable . encoding ) ) <EOL> self . set_necessary_dimensions ( variable ) <EOL> data = variable . data <EOL> self . ds . createVariable ( name , data . dtype , variable . dims ) <EOL> scipy_var = self . ds . variables [ name ] <EOL> for k , v in iteritems ( variable . attrs ) : <EOL> self . _validate_attr_key ( k ) <EOL> setattr ( scipy_var , k , v ) <EOL> return scipy_var , data <EOL> def sync ( self ) : <EOL> super ( ScipyDataStore , self ) . sync ( ) <EOL> self . ds . flush ( ) <EOL> def close ( self ) : <EOL> self . ds . close ( ) <EOL> def __exit__ ( self , type , value , tb ) : <EOL> self . close ( ) </s>
