<s> """<STR_LIT>""" <EOL> import getpass <EOL> import datetime <EOL> import platform <EOL> import warnings <EOL> import numpy as np <EOL> import netCDF4 <EOL> from . . config import FileMetadata <EOL> from . common import stringarray_to_chararray , _test_arguments <EOL> from . . core . radar import Radar <EOL> from . . lazydict import LazyLoadDict <EOL> _INSTRUMENT_PARAMS_DIMS = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:time>' , ) , <EOL> } <EOL> def read_cfradial ( filename , field_names = None , additional_metadata = None , <EOL> file_field_names = False , exclude_fields = None , <EOL> delay_field_loading = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _test_arguments ( kwargs ) <EOL> filemetadata = FileMetadata ( '<STR_LIT>' , field_names , additional_metadata , <EOL> file_field_names , exclude_fields ) <EOL> ncobj = netCDF4 . Dataset ( filename ) <EOL> ncvars = ncobj . variables <EOL> metadata = dict ( [ ( k , getattr ( ncobj , k ) ) for k in ncobj . ncattrs ( ) ] ) <EOL> if '<STR_LIT>' in metadata : <EOL> metadata [ '<STR_LIT>' ] = '<STR_LIT:false>' <EOL> if '<STR_LIT>' in ncvars : <EOL> metadata [ '<STR_LIT>' ] = int ( ncvars [ '<STR_LIT>' ] [ : ] ) <EOL> else : <EOL> metadata [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> global_vars = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> for var , default_value in global_vars . items ( ) : <EOL> if var in ncvars : <EOL> metadata [ var ] = str ( netCDF4 . chartostring ( ncvars [ var ] [ : ] ) ) <EOL> else : <EOL> metadata [ var ] = default_value <EOL> time = _ncvar_to_dict ( ncvars [ '<STR_LIT:time>' ] ) <EOL> _range = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> latitude = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> longitude = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> altitude = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ncvars : <EOL> altitude_agl = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> altitude_agl = None <EOL> sweep_mode = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> fixed_angle = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> sweep_start_ray_index = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> sweep_end_ray_index = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ncvars : <EOL> sweep_number = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> nsweeps = len ( sweep_start_ray_index [ '<STR_LIT:data>' ] ) <EOL> sweep_number = filemetadata ( '<STR_LIT>' ) <EOL> sweep_number [ '<STR_LIT:data>' ] = np . arange ( nsweeps , dtype = '<STR_LIT>' ) <EOL> warnings . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in ncvars : <EOL> target_scan_rate = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> target_scan_rate = None <EOL> if '<STR_LIT>' in ncvars : <EOL> rays_are_indexed = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> rays_are_indexed = None <EOL> if '<STR_LIT>' in ncvars : <EOL> ray_angle_res = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> ray_angle_res = None <EOL> mode = str ( netCDF4 . chartostring ( sweep_mode [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] ) ) <EOL> if mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> scan_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in mode : <EOL> scan_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in mode : <EOL> scan_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in mode : <EOL> scan_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in mode : <EOL> scan_type = '<STR_LIT>' <EOL> else : <EOL> scan_type = '<STR_LIT>' <EOL> azimuth = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> elevation = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ncvars : <EOL> scan_rate = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> scan_rate = None <EOL> if '<STR_LIT>' in ncvars : <EOL> antenna_transition = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> antenna_transition = None <EOL> if '<STR_LIT>' in ncvars : <EOL> rotation = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> rotation = None <EOL> if '<STR_LIT>' in ncvars : <EOL> tilt = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> tilt = None <EOL> if '<STR_LIT>' in ncvars : <EOL> roll = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> roll = None <EOL> if '<STR_LIT>' in ncvars : <EOL> drift = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> drift = None <EOL> if '<STR_LIT>' in ncvars : <EOL> heading = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> heading = None <EOL> if '<STR_LIT>' in ncvars : <EOL> pitch = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> pitch = None <EOL> if '<STR_LIT>' in ncvars : <EOL> georefs_applied = _ncvar_to_dict ( ncvars [ '<STR_LIT>' ] ) <EOL> else : <EOL> georefs_applied = None <EOL> if '<STR_LIT>' in ncvars : <EOL> keys = [ k for k , v in ncvars . items ( ) <EOL> if v . dimensions == ( '<STR_LIT>' , ) ] <EOL> else : <EOL> keys = [ k for k , v in ncvars . items ( ) <EOL> if v . dimensions == ( '<STR_LIT:time>' , '<STR_LIT>' ) ] <EOL> fields = { } <EOL> for key in keys : <EOL> field_name = filemetadata . get_field_name ( key ) <EOL> if field_name is None : <EOL> if exclude_fields is not None and key in exclude_fields : <EOL> continue <EOL> field_name = key <EOL> fields [ field_name ] = _ncvar_to_dict ( ncvars [ key ] , delay_field_loading ) <EOL> if '<STR_LIT>' in ncvars : <EOL> shape = ( len ( ncvars [ '<STR_LIT:time>' ] ) , len ( ncvars [ '<STR_LIT>' ] ) ) <EOL> ray_n_gates = ncvars [ '<STR_LIT>' ] [ : ] <EOL> ray_start_index = ncvars [ '<STR_LIT>' ] [ : ] <EOL> for dic in fields . values ( ) : <EOL> _unpack_variable_gate_field_dic ( <EOL> dic , shape , ray_n_gates , ray_start_index ) <EOL> keys = [ k for k in _INSTRUMENT_PARAMS_DIMS . keys ( ) if k in ncvars ] <EOL> instrument_parameters = dict ( ( k , _ncvar_to_dict ( ncvars [ k ] ) ) for k in keys ) <EOL> if instrument_parameters == { } : <EOL> instrument_parameters = None <EOL> keys = _find_all_meta_group_vars ( ncvars , '<STR_LIT>' ) <EOL> radar_calibration = dict ( ( k , _ncvar_to_dict ( ncvars [ k ] ) ) for k in keys ) <EOL> if radar_calibration == { } : <EOL> radar_calibration = None <EOL> if not delay_field_loading : <EOL> ncobj . close ( ) <EOL> return Radar ( <EOL> time , _range , fields , metadata , scan_type , <EOL> latitude , longitude , altitude , <EOL> sweep_number , sweep_mode , fixed_angle , sweep_start_ray_index , <EOL> sweep_end_ray_index , <EOL> azimuth , elevation , <EOL> instrument_parameters = instrument_parameters , <EOL> radar_calibration = radar_calibration , <EOL> altitude_agl = altitude_agl , <EOL> scan_rate = scan_rate , <EOL> antenna_transition = antenna_transition , <EOL> target_scan_rate = target_scan_rate , <EOL> rays_are_indexed = rays_are_indexed , ray_angle_res = ray_angle_res , <EOL> rotation = rotation , tilt = tilt , roll = roll , drift = drift , heading = heading , <EOL> pitch = pitch , georefs_applied = georefs_applied ) <EOL> def _find_all_meta_group_vars ( ncvars , meta_group_name ) : <EOL> """<STR_LIT>""" <EOL> return [ k for k , v in ncvars . items ( ) if '<STR_LIT>' in v . ncattrs ( ) and <EOL> v . meta_group == meta_group_name ] <EOL> def _ncvar_to_dict ( ncvar , lazydict = False ) : <EOL> """<STR_LIT>""" <EOL> d = dict ( ( k , getattr ( ncvar , k ) ) for k in ncvar . ncattrs ( ) <EOL> if k not in [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> data_extractor = _NetCDFVariableDataExtractor ( ncvar ) <EOL> if lazydict : <EOL> d = LazyLoadDict ( d ) <EOL> d . set_lazy ( '<STR_LIT:data>' , data_extractor ) <EOL> else : <EOL> d [ '<STR_LIT:data>' ] = data_extractor ( ) <EOL> return d <EOL> class _NetCDFVariableDataExtractor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ncvar ) : <EOL> """<STR_LIT>""" <EOL> self . ncvar = ncvar <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . ncvar [ : ] <EOL> if data is np . ma . masked : <EOL> self . ncvar . set_auto_mask ( False ) <EOL> data = np . ma . masked_equal ( self . ncvar [ : ] , self . ncvar [ <NUM_LIT:0> ] ) <EOL> return np . atleast_1d ( data ) <EOL> def _unpack_variable_gate_field_dic ( <EOL> dic , shape , ray_n_gates , ray_start_index ) : <EOL> """<STR_LIT>""" <EOL> fdata = dic [ '<STR_LIT:data>' ] <EOL> data = np . ma . masked_all ( shape , dtype = fdata . dtype ) <EOL> for i , ( gates , idx ) in enumerate ( zip ( ray_n_gates , ray_start_index ) ) : <EOL> data [ i , : gates ] = fdata [ idx : idx + gates ] <EOL> dic [ '<STR_LIT:data>' ] = data <EOL> return <EOL> def write_cfradial ( filename , radar , format = '<STR_LIT>' , time_reference = None , <EOL> arm_time_variables = False ) : <EOL> """<STR_LIT>""" <EOL> dataset = netCDF4 . Dataset ( filename , '<STR_LIT:w>' , format = format ) <EOL> max_str_len = len ( radar . sweep_mode [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] ) <EOL> for k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if ( ( radar . instrument_parameters is not None ) and <EOL> ( k in radar . instrument_parameters ) ) : <EOL> sdim_length = len ( radar . instrument_parameters [ k ] [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] ) <EOL> max_str_len = max ( max_str_len , sdim_length ) <EOL> str_len = max ( max_str_len , <NUM_LIT:32> ) <EOL> dataset . createDimension ( '<STR_LIT:time>' , None ) <EOL> dataset . createDimension ( '<STR_LIT>' , radar . ngates ) <EOL> dataset . createDimension ( '<STR_LIT>' , radar . nsweeps ) <EOL> dataset . createDimension ( '<STR_LIT>' , str_len ) <EOL> metadata_copy = dict ( radar . metadata ) <EOL> global_variables = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> for var in global_variables : <EOL> if var in metadata_copy : <EOL> metadata_copy . pop ( var ) <EOL> if '<STR_LIT>' in metadata_copy : <EOL> history = metadata_copy . pop ( '<STR_LIT>' ) <EOL> else : <EOL> user = getpass . getuser ( ) <EOL> node = platform . node ( ) <EOL> time_str = datetime . datetime . now ( ) . isoformat ( ) <EOL> t = ( user , node , time_str ) <EOL> history = '<STR_LIT>' % ( t ) <EOL> dataset . setncatts ( metadata_copy ) <EOL> if '<STR_LIT>' not in dataset . ncattrs ( ) : <EOL> dataset . setncattr ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in dataset . ncattrs ( ) : <EOL> dataset . setncattr ( '<STR_LIT>' , '<STR_LIT:U+002CU+0020>' . join ( radar . fields . keys ( ) ) ) <EOL> dataset . setncattr ( '<STR_LIT>' , history ) <EOL> if arm_time_variables : <EOL> dt = netCDF4 . num2date ( radar . time [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] , radar . time [ '<STR_LIT>' ] ) <EOL> td = dt - datetime . datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> base_time = { <EOL> '<STR_LIT:data>' : np . array ( [ td . seconds + td . days * <NUM_LIT> * <NUM_LIT> ] , '<STR_LIT>' ) , <EOL> '<STR_LIT:string>' : dt . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _create_ncvar ( base_time , dataset , '<STR_LIT>' , ( ) ) <EOL> time_offset = { <EOL> '<STR_LIT:data>' : radar . time [ '<STR_LIT:data>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : radar . time [ '<STR_LIT>' ] . replace ( '<STR_LIT:T>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _create_ncvar ( time_offset , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> _create_ncvar ( radar . time , dataset , '<STR_LIT:time>' , ( '<STR_LIT:time>' , ) ) <EOL> _create_ncvar ( radar . range , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> _create_ncvar ( radar . azimuth , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> _create_ncvar ( radar . elevation , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . scan_rate is not None : <EOL> _create_ncvar ( radar . scan_rate , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . antenna_transition is not None : <EOL> _create_ncvar ( radar . antenna_transition , dataset , <EOL> '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> for field , dic in radar . fields . items ( ) : <EOL> _create_ncvar ( dic , dataset , field , ( '<STR_LIT:time>' , '<STR_LIT>' ) ) <EOL> _create_ncvar ( radar . sweep_number , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> _create_ncvar ( radar . fixed_angle , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> _create_ncvar ( radar . sweep_start_ray_index , dataset , <EOL> '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> _create_ncvar ( radar . sweep_end_ray_index , dataset , <EOL> '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> _create_ncvar ( radar . sweep_mode , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if radar . target_scan_rate is not None : <EOL> _create_ncvar ( radar . target_scan_rate , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) ) <EOL> if radar . rays_are_indexed is not None : <EOL> _create_ncvar ( radar . rays_are_indexed , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if radar . ray_angle_res is not None : <EOL> _create_ncvar ( radar . ray_angle_res , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) ) <EOL> if ( ( radar . instrument_parameters is not None ) and <EOL> ( '<STR_LIT>' in radar . instrument_parameters . keys ( ) ) ) : <EOL> size = len ( radar . instrument_parameters [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] ) <EOL> dataset . createDimension ( '<STR_LIT>' , size ) <EOL> if radar . instrument_parameters is not None : <EOL> for k in radar . instrument_parameters . keys ( ) : <EOL> if k in _INSTRUMENT_PARAMS_DIMS : <EOL> dim = _INSTRUMENT_PARAMS_DIMS [ k ] <EOL> _create_ncvar ( radar . instrument_parameters [ k ] , dataset , k , dim ) <EOL> else : <EOL> message = ( "<STR_LIT>" % ( k ) + <EOL> "<STR_LIT>" ) <EOL> warnings . warn ( message ) <EOL> if radar . radar_calibration is not None and radar . radar_calibration != { } : <EOL> size = [ len ( d [ '<STR_LIT:data>' ] ) for k , d in radar . radar_calibration . items ( ) <EOL> if k not in [ '<STR_LIT>' , '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> dataset . createDimension ( '<STR_LIT>' , size ) <EOL> for key , dic in radar . radar_calibration . items ( ) : <EOL> if key == '<STR_LIT>' : <EOL> dims = ( '<STR_LIT:time>' , ) <EOL> elif key == '<STR_LIT>' : <EOL> dims = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> dims = ( '<STR_LIT>' , ) <EOL> _create_ncvar ( dic , dataset , key , dims ) <EOL> if radar . latitude [ '<STR_LIT:data>' ] . size == <NUM_LIT:1> : <EOL> _create_ncvar ( radar . latitude , dataset , '<STR_LIT>' , ( ) ) <EOL> _create_ncvar ( radar . longitude , dataset , '<STR_LIT>' , ( ) ) <EOL> _create_ncvar ( radar . altitude , dataset , '<STR_LIT>' , ( ) ) <EOL> if radar . altitude_agl is not None : <EOL> _create_ncvar ( radar . altitude_agl , dataset , '<STR_LIT>' , ( ) ) <EOL> else : <EOL> _create_ncvar ( radar . latitude , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> _create_ncvar ( radar . longitude , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> _create_ncvar ( radar . altitude , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . altitude_agl is not None : <EOL> _create_ncvar ( radar . altitude_agl , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT:time>' , ) ) <EOL> time_dim = ( '<STR_LIT>' , ) <EOL> units = radar . time [ '<STR_LIT>' ] <EOL> start_dt = netCDF4 . num2date ( radar . time [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] , units ) <EOL> if start_dt . microsecond != <NUM_LIT:0> : <EOL> start_dt -= datetime . timedelta ( microseconds = start_dt . microsecond ) <EOL> end_dt = netCDF4 . num2date ( radar . time [ '<STR_LIT:data>' ] [ - <NUM_LIT:1> ] , units ) <EOL> if end_dt . microsecond != <NUM_LIT:0> : <EOL> end_dt += ( datetime . timedelta ( seconds = <NUM_LIT:1> ) - <EOL> datetime . timedelta ( microseconds = end_dt . microsecond ) ) <EOL> start_dic = { '<STR_LIT:data>' : np . array ( start_dt . isoformat ( ) + '<STR_LIT>' , dtype = '<STR_LIT:S>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> end_dic = { '<STR_LIT:data>' : np . array ( end_dt . isoformat ( ) + '<STR_LIT>' , dtype = '<STR_LIT:S>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> _create_ncvar ( start_dic , dataset , '<STR_LIT>' , time_dim ) <EOL> _create_ncvar ( end_dic , dataset , '<STR_LIT>' , time_dim ) <EOL> if time_reference is None : <EOL> if radar . time [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> time_reference = False <EOL> else : <EOL> time_reference = True <EOL> if time_reference : <EOL> ref_dic = { '<STR_LIT:data>' : np . array ( radar . time [ '<STR_LIT>' ] [ - <NUM_LIT:20> : ] , dtype = '<STR_LIT:S>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> _create_ncvar ( ref_dic , dataset , '<STR_LIT>' , time_dim ) <EOL> vol_dic = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if '<STR_LIT>' in radar . metadata : <EOL> vol_dic [ '<STR_LIT:data>' ] = np . array ( [ radar . metadata [ '<STR_LIT>' ] ] , <EOL> dtype = '<STR_LIT>' ) <EOL> else : <EOL> vol_dic [ '<STR_LIT:data>' ] = np . array ( [ <NUM_LIT:0> ] , dtype = '<STR_LIT>' ) <EOL> _create_ncvar ( vol_dic , dataset , '<STR_LIT>' , ( ) ) <EOL> if '<STR_LIT>' in radar . metadata : <EOL> dic = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : np . array ( radar . metadata [ '<STR_LIT>' ] , dtype = '<STR_LIT:S>' ) } <EOL> _create_ncvar ( dic , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> if '<STR_LIT>' in radar . metadata : <EOL> dic = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : np . array ( radar . metadata [ '<STR_LIT>' ] , dtype = '<STR_LIT:S>' ) } <EOL> _create_ncvar ( dic , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> if '<STR_LIT>' in radar . metadata : <EOL> dic = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : np . array ( radar . metadata [ '<STR_LIT>' ] , dtype = '<STR_LIT:S>' ) } <EOL> _create_ncvar ( dic , dataset , '<STR_LIT>' , ( '<STR_LIT>' , ) ) <EOL> if radar . rotation is not None : <EOL> _create_ncvar ( radar . rotation , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . tilt is not None : <EOL> _create_ncvar ( radar . tilt , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . roll is not None : <EOL> _create_ncvar ( radar . roll , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . drift is not None : <EOL> _create_ncvar ( radar . drift , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . heading is not None : <EOL> _create_ncvar ( radar . heading , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . pitch is not None : <EOL> _create_ncvar ( radar . pitch , dataset , '<STR_LIT>' , ( '<STR_LIT:time>' , ) ) <EOL> if radar . georefs_applied is not None : <EOL> _create_ncvar ( radar . georefs_applied , dataset , '<STR_LIT>' , <EOL> ( '<STR_LIT:time>' , ) ) <EOL> dataset . close ( ) <EOL> def _create_ncvar ( dic , dataset , name , dimensions ) : <EOL> """<STR_LIT>""" <EOL> data = dic [ '<STR_LIT:data>' ] <EOL> if isinstance ( data , np . ndarray ) is not True : <EOL> warnings . warn ( "<STR_LIT>" % name ) <EOL> data = np . array ( data ) <EOL> if data . dtype . char is '<STR_LIT>' : <EOL> data = data . astype ( '<STR_LIT:S>' ) <EOL> if data . dtype . char is '<STR_LIT:S>' and data . dtype != '<STR_LIT>' : <EOL> data = stringarray_to_chararray ( data ) <EOL> special_keys = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> kwargs = { '<STR_LIT>' : True } <EOL> for dic_key , kwargs_key in special_keys . items ( ) : <EOL> if dic_key in dic : <EOL> kwargs [ kwargs_key ] = dic [ dic_key ] <EOL> if '<STR_LIT>' in dic : <EOL> dtype = np . dtype ( dic [ '<STR_LIT>' ] ) <EOL> if np . issubdtype ( dtype , np . integer ) : <EOL> if '<STR_LIT>' not in dic and '<STR_LIT>' not in dic : <EOL> scale , offset , fill = _calculate_scale_and_offset ( dic , dtype ) <EOL> dic [ '<STR_LIT>' ] = scale <EOL> dic [ '<STR_LIT>' ] = offset <EOL> dic [ '<STR_LIT>' ] = fill <EOL> kwargs [ '<STR_LIT>' ] = fill <EOL> else : <EOL> dtype = data . dtype <EOL> ncvar = dataset . createVariable ( name , dtype , dimensions , ** kwargs ) <EOL> if '<STR_LIT>' in dic . keys ( ) : <EOL> ncvar . setncattr ( '<STR_LIT>' , dic [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in dic . keys ( ) : <EOL> ncvar . setncattr ( '<STR_LIT>' , dic [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ncvar . ncattrs ( ) : <EOL> fv = ncvar . _FillValue <EOL> ncvar . delncattr ( '<STR_LIT>' ) <EOL> ncvar . setncattr ( '<STR_LIT>' , fv ) <EOL> for key , value in dic . items ( ) : <EOL> if key in special_keys . keys ( ) : <EOL> continue <EOL> if key in [ '<STR_LIT:data>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> ncvar . setncattr ( key , value ) <EOL> if data . shape == ( ) : <EOL> data . shape = ( <NUM_LIT:1> , ) <EOL> if data . dtype == '<STR_LIT>' : <EOL> if ncvar . ndim == <NUM_LIT:1> : <EOL> ncvar [ : data . shape [ - <NUM_LIT:1> ] ] = data [ : ] <EOL> else : <EOL> ncvar [ ... , : data . shape [ - <NUM_LIT:1> ] ] = data [ : ] <EOL> else : <EOL> ncvar [ : ] = data [ : ] <EOL> def _calculate_scale_and_offset ( dic , dtype , minimum = None , maximum = None ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in dic : <EOL> fillvalue = dic [ "<STR_LIT>" ] <EOL> else : <EOL> fillvalue = np . NaN <EOL> data = dic [ '<STR_LIT:data>' ] . copy ( ) <EOL> data = np . ma . array ( data , mask = ( ~ np . isfinite ( data ) | ( data == fillvalue ) ) ) <EOL> if minimum is None : <EOL> minimum = np . amin ( data ) <EOL> if maximum is None : <EOL> maximum = np . amax ( data ) <EOL> if maximum < minimum : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( maximum , minimum ) ) <EOL> elif maximum == minimum : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( maximum , minimum ) ) <EOL> maximum = minimum + <NUM_LIT:1> <EOL> maxi = np . iinfo ( dtype ) . max <EOL> mini = np . iinfo ( dtype ) . min + <NUM_LIT:1> <EOL> scale = float ( maximum - minimum ) / float ( maxi - mini ) <EOL> offset = minimum - mini * scale <EOL> return scale , offset , np . iinfo ( dtype ) . min </s>
