<s> from __future__ import print_function , division <EOL> import os <EOL> import warnings <EOL> import numpy as np <EOL> from . . util . constants import c , pi <EOL> from . . util . functions import FreezableClass <EOL> from . . dust import SphericalDust <EOL> from astropy import log as logger <EOL> from . . util . otf_hdf5 import on_the_fly_hdf5 <EOL> from . . grid import CartesianGrid , SphericalPolarGrid , CylindricalPolarGrid , OctreeGrid , AMRGrid , VoronoiGrid <EOL> STOKESD = { } <EOL> STOKESD [ '<STR_LIT:I>' ] = <NUM_LIT:0> <EOL> STOKESD [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> STOKESD [ '<STR_LIT>' ] = <NUM_LIT:2> <EOL> STOKESD [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> LABEL = { } <EOL> LABEL [ '<STR_LIT:I>' ] = '<STR_LIT>' <EOL> LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> LABEL [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> LABEL [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> UNITS_LABEL = { } <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> UNITS_LABEL [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def mc_linear_polarization ( I , sigma_I , Q , sigma_Q , U , sigma_U , N = <NUM_LIT:1000> ) : <EOL> new_shape = ( N , ) + I . shape <EOL> ones_shape = ( N , ) + ( <NUM_LIT:1> , ) * I . ndim <EOL> xi1 = np . random . normal ( loc = <NUM_LIT:0> , scale = <NUM_LIT:1.> , size = ones_shape ) <EOL> xi2 = np . random . normal ( loc = <NUM_LIT:0> , scale = <NUM_LIT:1.> , size = ones_shape ) <EOL> xi3 = np . random . normal ( loc = <NUM_LIT:0> , scale = <NUM_LIT:1.> , size = ones_shape ) <EOL> Is = np . zeros ( new_shape ) <EOL> Qs = np . zeros ( new_shape ) <EOL> Us = np . zeros ( new_shape ) <EOL> Is += sigma_I <EOL> Qs += sigma_Q <EOL> Us += sigma_U <EOL> Is *= xi1 <EOL> Qs *= xi2 <EOL> Us *= xi3 <EOL> Is += I <EOL> Qs += Q <EOL> Us += U <EOL> np . divide ( Qs , Is , out = Qs ) <EOL> np . divide ( Us , Is , out = Us ) <EOL> Ps = np . hypot ( Qs , Us ) <EOL> return np . mean ( Ps , axis = <NUM_LIT:0> ) , np . std ( Ps , axis = <NUM_LIT:0> ) <EOL> def mc_circular_polarization ( I , sigma_I , V , sigma_V , N = <NUM_LIT:1000> ) : <EOL> new_shape = ( N , ) + I . shape <EOL> ones_shape = ( N , ) + ( <NUM_LIT:1> , ) * I . ndim <EOL> xi1 = np . random . normal ( loc = <NUM_LIT:0> , scale = <NUM_LIT:1.> , size = ones_shape ) <EOL> xi2 = np . random . normal ( loc = <NUM_LIT:0> , scale = <NUM_LIT:1.> , size = ones_shape ) <EOL> Is = np . zeros ( new_shape ) <EOL> Vs = np . zeros ( new_shape ) <EOL> Is += sigma_I <EOL> Vs += sigma_V <EOL> Is *= xi1 <EOL> Vs *= xi2 <EOL> Is += I <EOL> Vs += V <EOL> np . abs ( Vs , out = Vs ) <EOL> Ps = np . divide ( Vs , Is ) <EOL> return np . mean ( Ps , axis = <NUM_LIT:0> ) , np . std ( Ps , axis = <NUM_LIT:0> ) <EOL> class ModelOutput ( FreezableClass ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , filename ) : <EOL> if not os . path . exists ( filename ) : <EOL> raise IOError ( "<STR_LIT>" % filename ) <EOL> self . filename = filename <EOL> self . file = None <EOL> def _get_origin_slice ( self , group , component , source_id = None , dust_id = None , n_scat = None ) : <EOL> track_origin = group . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) <EOL> if track_origin == '<STR_LIT>' and component != '<STR_LIT>' : <EOL> raise Exception ( "<STR_LIT>" % component ) <EOL> if track_origin != '<STR_LIT>' : <EOL> if source_id is not None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if dust_id is not None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if track_origin in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if component == '<STR_LIT>' : <EOL> io = <NUM_LIT:0> <EOL> elif component == '<STR_LIT>' : <EOL> io = <NUM_LIT:1> <EOL> elif component == '<STR_LIT>' : <EOL> io = <NUM_LIT:2> <EOL> elif component == '<STR_LIT>' : <EOL> io = <NUM_LIT:3> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( track_origin ) ) <EOL> if track_origin == '<STR_LIT>' : <EOL> if isinstance ( source_id , basestring ) and source_id != '<STR_LIT:all>' : <EOL> try : <EOL> source_id = self . get_available_sources ( ) . index ( source_id ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" . format ( source_id ) ) <EOL> ns = group . attrs [ '<STR_LIT>' ] <EOL> nd = group . attrs [ '<STR_LIT>' ] <EOL> io = ( ( io - ( io + <NUM_LIT:1> ) % <NUM_LIT:2> + <NUM_LIT:1> ) * ns + ( io - io % <NUM_LIT:2> ) * nd ) / <NUM_LIT:2> <EOL> if component . startswith ( '<STR_LIT:source>' ) : <EOL> if source_id is None or source_id == '<STR_LIT:all>' : <EOL> io = ( io , io + ns ) <EOL> else : <EOL> if source_id < <NUM_LIT:0> or source_id >= ns : <EOL> raise ValueError ( "<STR_LIT>" % ( ns - <NUM_LIT:1> ) ) <EOL> io = io + source_id <EOL> else : <EOL> if dust_id is None or dust_id == '<STR_LIT:all>' : <EOL> io = ( io , io + nd ) <EOL> else : <EOL> if dust_id < <NUM_LIT:0> or dust_id >= nd : <EOL> raise ValueError ( "<STR_LIT>" % ( nd - <NUM_LIT:1> ) ) <EOL> io = io + dust_id <EOL> elif track_origin == '<STR_LIT>' : <EOL> if '<STR_LIT>' in group . attrs : <EOL> track_n_scat = group . attrs [ '<STR_LIT>' ] <EOL> else : <EOL> track_n_scat = <NUM_LIT:0> <EOL> if component == '<STR_LIT:source>' : <EOL> io = <NUM_LIT:0> <EOL> elif component == '<STR_LIT>' : <EOL> io = track_n_scat + <NUM_LIT:2> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if n_scat is None : <EOL> io = ( io , io + track_n_scat + <NUM_LIT:2> ) <EOL> else : <EOL> if n_scat >= <NUM_LIT:0> and n_scat <= track_n_scat : <EOL> io += n_scat <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( track_n_scat ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return io <EOL> @ on_the_fly_hdf5 <EOL> def get_sed ( self , stokes = '<STR_LIT:I>' , group = <NUM_LIT:0> , technique = '<STR_LIT>' , <EOL> distance = None , component = '<STR_LIT>' , inclination = '<STR_LIT:all>' , <EOL> aperture = '<STR_LIT:all>' , uncertainties = False , units = None , <EOL> source_id = None , dust_id = None , n_scat = None ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( stokes , basestring ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if distance is not None and stokes in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if technique == '<STR_LIT>' : <EOL> n_groups = len ( self . file [ '<STR_LIT>' ] ) <EOL> if ( group < <NUM_LIT:0> and - group <= n_groups ) or ( group >= <NUM_LIT:0> and group < n_groups ) : <EOL> g = self . file [ '<STR_LIT>' % ( group + <NUM_LIT:1> ) ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % n_groups ) <EOL> else : <EOL> g = self . file [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in g : <EOL> raise Exception ( "<STR_LIT>" % group ) <EOL> if uncertainties and not '<STR_LIT>' in g : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in g [ '<STR_LIT>' ] . attrs and component != '<STR_LIT>' : <EOL> io = self . _get_origin_slice ( g [ '<STR_LIT>' ] , component = component , <EOL> source_id = source_id , dust_id = dust_id , n_scat = n_scat ) <EOL> if '<STR_LIT>' in g . attrs and g . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) . lower ( ) == '<STR_LIT:yes>' : <EOL> nu = g [ '<STR_LIT>' ] . value <EOL> wav = c / nu * <NUM_LIT> <EOL> elif '<STR_LIT>' in g [ '<STR_LIT>' ] . attrs : <EOL> numin = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> numax = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> wavmin , wavmax = c / numax * <NUM_LIT> , c / numin * <NUM_LIT> <EOL> wav = np . logspace ( np . log10 ( wavmax ) , np . log10 ( wavmin ) , g [ '<STR_LIT>' ] . shape [ - <NUM_LIT:1> ] * <NUM_LIT:2> + <NUM_LIT:1> ) [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> nu = c / wav * <NUM_LIT> <EOL> else : <EOL> nu = g [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> wav = c / nu * <NUM_LIT> <EOL> flux = g [ '<STR_LIT>' ] . value <EOL> if uncertainties : <EOL> unc = g [ '<STR_LIT>' ] . value <EOL> try : <EOL> inside_observer = g . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) . lower ( ) == '<STR_LIT:yes>' <EOL> except : <EOL> inside_observer = False <EOL> if inside_observer and distance is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if units is None : <EOL> if distance is None and not inside_observer : <EOL> units = '<STR_LIT>' <EOL> else : <EOL> units = '<STR_LIT>' <EOL> if distance is not None or inside_observer : <EOL> if units == '<STR_LIT>' : <EOL> scale = <NUM_LIT:1.> <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT:1.> / nu <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT> / nu <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT> / nu <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % units ) <EOL> if distance : <EOL> scale *= <NUM_LIT:1.> / ( <NUM_LIT> * pi * distance ** <NUM_LIT:2> ) <EOL> else : <EOL> if units != '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> scale = <NUM_LIT:1.> <EOL> if flux . dtype == np . float32 : <EOL> flux = flux . astype ( np . float64 ) <EOL> if uncertainties and unc . dtype == np . float32 : <EOL> unc = unc . astype ( np . float64 ) <EOL> if stokes in STOKESD : <EOL> flux *= scale <EOL> if uncertainties : <EOL> unc *= scale <EOL> if aperture == '<STR_LIT:all>' : <EOL> pass <EOL> else : <EOL> if not isinstance ( aperture , int ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flux = flux [ : , : , : , aperture ] <EOL> if uncertainties : <EOL> unc = unc [ : , : , : , aperture ] <EOL> if inclination == '<STR_LIT:all>' : <EOL> pass <EOL> else : <EOL> if not isinstance ( inclination , int ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flux = flux [ : , : , inclination ] <EOL> if uncertainties : <EOL> unc = unc [ : , : , inclination ] <EOL> if component == '<STR_LIT>' : <EOL> flux = np . sum ( flux , axis = <NUM_LIT:1> ) <EOL> if uncertainties : <EOL> unc = np . sqrt ( np . sum ( unc ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> elif component in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:source>' ] : <EOL> if type ( io ) is tuple : <EOL> start , end = io <EOL> flux = flux [ : , start : end ] <EOL> if uncertainties : <EOL> unc = unc [ : , start : end ] <EOL> if ( component . startswith ( '<STR_LIT:source>' ) and source_id is None ) or ( component . startswith ( '<STR_LIT>' ) and dust_id is None ) : <EOL> flux = np . sum ( flux , axis = <NUM_LIT:1> ) <EOL> if uncertainties : <EOL> unc = np . sqrt ( np . sum ( unc ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> else : <EOL> flux = flux [ : , io ] <EOL> if uncertainties : <EOL> unc = unc [ : , io ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % component ) <EOL> if flux . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> and stokes != '<STR_LIT:I>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if stokes in STOKESD : <EOL> flux = flux [ STOKESD [ stokes ] ] <EOL> if uncertainties : <EOL> unc = unc [ STOKESD [ stokes ] ] <EOL> elif stokes == '<STR_LIT>' : <EOL> if uncertainties : <EOL> flux , unc = mc_linear_polarization ( flux [ <NUM_LIT:0> ] , unc [ <NUM_LIT:0> ] , flux [ <NUM_LIT:1> ] , unc [ <NUM_LIT:1> ] , flux [ <NUM_LIT:2> ] , unc [ <NUM_LIT:2> ] ) <EOL> else : <EOL> flux = np . sqrt ( ( flux [ <NUM_LIT:1> ] ** <NUM_LIT:2> + flux [ <NUM_LIT:2> ] ** <NUM_LIT:2> ) / flux [ <NUM_LIT:0> ] ** <NUM_LIT:2> ) <EOL> flux [ np . isnan ( flux ) ] = <NUM_LIT:0.> <EOL> elif stokes == '<STR_LIT>' : <EOL> if uncertainties : <EOL> flux , unc = mc_circular_polarization ( flux [ <NUM_LIT:0> ] , unc [ <NUM_LIT:0> ] , flux [ <NUM_LIT:3> ] , unc [ <NUM_LIT:3> ] ) <EOL> else : <EOL> flux = np . abs ( flux [ <NUM_LIT:3> ] / flux [ <NUM_LIT:0> ] ) <EOL> flux [ np . isnan ( flux ) ] = <NUM_LIT:0.> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % stokes ) <EOL> from . sed import SED <EOL> sed = SED ( nu = nu , val = flux , unc = unc if uncertainties else None , units = units ) <EOL> sed . ap_min = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> sed . ap_max = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> try : <EOL> sed . d_min = g . attrs [ '<STR_LIT>' ] <EOL> sed . d_max = g . attrs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> sed . d_min = None <EOL> sed . d_max = None <EOL> sed . distance = distance <EOL> sed . inside_observer = inside_observer <EOL> return sed <EOL> @ on_the_fly_hdf5 <EOL> def get_image ( self , stokes = '<STR_LIT:I>' , group = <NUM_LIT:0> , technique = '<STR_LIT>' , <EOL> distance = None , component = '<STR_LIT>' , inclination = '<STR_LIT:all>' , <EOL> uncertainties = False , units = None , <EOL> source_id = None , dust_id = None , n_scat = None ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( stokes , basestring ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if distance is not None and stokes in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if technique == '<STR_LIT>' : <EOL> n_groups = len ( self . file [ '<STR_LIT>' ] ) <EOL> if ( group < <NUM_LIT:0> and - group <= n_groups ) or ( group >= <NUM_LIT:0> and group < n_groups ) : <EOL> g = self . file [ '<STR_LIT>' % ( group + <NUM_LIT:1> ) ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % n_groups ) <EOL> else : <EOL> g = self . file [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in g : <EOL> raise Exception ( "<STR_LIT>" % group ) <EOL> if uncertainties and not '<STR_LIT>' in g : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in g [ '<STR_LIT>' ] . attrs and component != '<STR_LIT>' : <EOL> io = self . _get_origin_slice ( g [ '<STR_LIT>' ] , component = component , <EOL> source_id = source_id , dust_id = dust_id , n_scat = n_scat ) <EOL> if '<STR_LIT>' in g . attrs and g . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) . lower ( ) == '<STR_LIT:yes>' : <EOL> nu = g [ '<STR_LIT>' ] . value <EOL> wav = c / nu * <NUM_LIT> <EOL> elif '<STR_LIT>' in g [ '<STR_LIT>' ] . attrs : <EOL> numin = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> numax = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> wavmin , wavmax = c / numax * <NUM_LIT> , c / numin * <NUM_LIT> <EOL> wav = np . logspace ( np . log10 ( wavmax ) , np . log10 ( wavmin ) , g [ '<STR_LIT>' ] . shape [ - <NUM_LIT:1> ] * <NUM_LIT:2> + <NUM_LIT:1> ) [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> nu = c / wav * <NUM_LIT> <EOL> else : <EOL> nu = g [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> wav = c / nu * <NUM_LIT> <EOL> flux = g [ '<STR_LIT>' ] . value <EOL> if uncertainties : <EOL> unc = g [ '<STR_LIT>' ] . value <EOL> try : <EOL> inside_observer = g . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) . lower ( ) == '<STR_LIT:yes>' <EOL> except : <EOL> inside_observer = False <EOL> if inside_observer and distance is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if units is None : <EOL> if distance is None and not inside_observer : <EOL> units = '<STR_LIT>' <EOL> else : <EOL> units = '<STR_LIT>' <EOL> ny , nx = flux . shape [ - <NUM_LIT:3> : - <NUM_LIT:1> ] <EOL> if inside_observer : <EOL> x_min = x_max = y_min = y_max = None <EOL> lon_min = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> lon_max = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> lat_min = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> lat_max = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> lon = np . linspace ( np . radians ( lon_min ) , np . radians ( lon_max ) , nx + <NUM_LIT:1> ) <EOL> lat = np . cos ( np . linspace ( np . radians ( <NUM_LIT> - lat_min ) , np . radians ( <NUM_LIT> - lat_max ) , ny + <NUM_LIT:1> ) ) <EOL> dlon = lon [ <NUM_LIT:1> : ] - lon [ : - <NUM_LIT:1> ] <EOL> dlat = lat [ : - <NUM_LIT:1> ] - lat [ <NUM_LIT:1> : ] <EOL> DLON , DLAT = np . meshgrid ( dlon , dlat ) <EOL> pix_area_sr = DLON * DLAT <EOL> else : <EOL> x_min = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> x_max = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> y_min = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> y_max = g [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] <EOL> if distance is not None : <EOL> lon_min_rad = np . arctan ( x_min / distance ) <EOL> lon_max_rad = np . arctan ( x_max / distance ) <EOL> lat_min_rad = np . arctan ( y_min / distance ) <EOL> lat_max_rad = np . arctan ( y_max / distance ) <EOL> pix_dx = abs ( lon_max_rad - lon_min_rad ) / float ( nx ) <EOL> pix_dy = abs ( lat_max_rad - lat_min_rad ) / float ( ny ) <EOL> pix_area_sr = pix_dx * pix_dy <EOL> lon_min = np . degrees ( lon_min_rad ) <EOL> lon_max = np . degrees ( lon_max_rad ) <EOL> lat_min = np . degrees ( lat_min_rad ) <EOL> lat_max = np . degrees ( lat_max_rad ) <EOL> else : <EOL> lon_min = lon_max = lat_min = lat_max = None <EOL> pix_area_sr = None <EOL> if distance is not None or inside_observer : <EOL> if units == '<STR_LIT>' : <EOL> scale = <NUM_LIT:1.> <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT:1.> / nu <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT> / nu <EOL> elif units == '<STR_LIT>' : <EOL> scale = <NUM_LIT> / nu <EOL> elif units == '<STR_LIT>' : <EOL> if inside_observer : <EOL> scale = <NUM_LIT> / nu [ np . newaxis , np . newaxis , : ] / pix_area_sr [ : , : , np . newaxis ] <EOL> else : <EOL> scale = <NUM_LIT> / nu / pix_area_sr <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % units ) <EOL> if distance : <EOL> scale *= <NUM_LIT:1.> / ( <NUM_LIT> * pi * distance ** <NUM_LIT:2> ) <EOL> else : <EOL> if units != '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> scale = <NUM_LIT:1.> <EOL> if flux . dtype == np . float32 : <EOL> flux = flux . astype ( np . float64 ) <EOL> if uncertainties and unc . dtype == np . float32 : <EOL> unc = unc . astype ( np . float64 ) <EOL> if stokes in STOKESD : <EOL> flux *= scale <EOL> if uncertainties : <EOL> unc *= scale <EOL> if inclination == '<STR_LIT:all>' : <EOL> pass <EOL> else : <EOL> if not isinstance ( inclination , int ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flux = flux [ : , : , inclination ] <EOL> if uncertainties : <EOL> unc = unc [ : , : , inclination ] <EOL> if component == '<STR_LIT>' : <EOL> flux = np . sum ( flux , axis = <NUM_LIT:1> ) <EOL> if uncertainties : <EOL> unc = np . sqrt ( np . sum ( unc ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> elif component in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:source>' ] : <EOL> if type ( io ) is tuple : <EOL> start , end = io <EOL> flux = flux [ : , start : end ] <EOL> if uncertainties : <EOL> unc = unc [ : , start : end ] <EOL> if ( component . startswith ( '<STR_LIT:source>' ) and source_id is None ) or ( component . startswith ( '<STR_LIT>' ) and dust_id is None ) : <EOL> flux = np . sum ( flux , axis = <NUM_LIT:1> ) <EOL> if uncertainties : <EOL> unc = np . sqrt ( np . sum ( unc ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> else : <EOL> flux = flux [ : , io ] <EOL> if uncertainties : <EOL> unc = unc [ : , io ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % component ) <EOL> if flux . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> and stokes != '<STR_LIT:I>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if stokes in STOKESD : <EOL> flux = flux [ STOKESD [ stokes ] ] <EOL> if uncertainties : <EOL> unc = unc [ STOKESD [ stokes ] ] <EOL> elif stokes == '<STR_LIT>' : <EOL> if uncertainties : <EOL> flux , unc = mc_linear_polarization ( flux [ <NUM_LIT:0> ] , unc [ <NUM_LIT:0> ] , flux [ <NUM_LIT:1> ] , unc [ <NUM_LIT:1> ] , flux [ <NUM_LIT:2> ] , unc [ <NUM_LIT:2> ] ) <EOL> else : <EOL> flux = np . sqrt ( ( flux [ <NUM_LIT:1> ] ** <NUM_LIT:2> + flux [ <NUM_LIT:2> ] ** <NUM_LIT:2> ) / flux [ <NUM_LIT:0> ] ** <NUM_LIT:2> ) <EOL> flux [ np . isnan ( flux ) ] = <NUM_LIT:0.> <EOL> elif stokes == '<STR_LIT>' : <EOL> if uncertainties : <EOL> flux , unc = mc_circular_polarization ( flux [ <NUM_LIT:0> ] , unc [ <NUM_LIT:0> ] , flux [ <NUM_LIT:3> ] , unc [ <NUM_LIT:3> ] ) <EOL> else : <EOL> flux = np . abs ( flux [ <NUM_LIT:3> ] / flux [ <NUM_LIT:0> ] ) <EOL> flux [ np . isnan ( flux ) ] = <NUM_LIT:0.> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % stokes ) <EOL> from . image import Image <EOL> image = Image ( nu = nu , val = flux , unc = unc if uncertainties else None , units = units ) <EOL> image . x_min = x_min <EOL> image . x_max = x_max <EOL> image . y_min = y_min <EOL> image . y_max = y_max <EOL> try : <EOL> image . d_min = g . attrs [ '<STR_LIT>' ] <EOL> image . d_max = g . attrs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> image . d_min = None <EOL> image . d_max = None <EOL> image . lon_min = lon_min <EOL> image . lon_max = lon_max <EOL> image . lat_min = lat_min <EOL> image . lat_max = lat_max <EOL> image . pix_area_sr = pix_area_sr <EOL> image . distance = distance <EOL> image . inside_observer = inside_observer <EOL> return image <EOL> @ on_the_fly_hdf5 <EOL> def get_available_sources ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . file [ '<STR_LIT>' ] . file != self . file . file : <EOL> g_sources = self . file [ '<STR_LIT>' ] . file [ self . file [ '<STR_LIT>' ] . name ] [ '<STR_LIT>' ] <EOL> else : <EOL> g_sources = self . file [ '<STR_LIT>' ] <EOL> names = [ ] <EOL> for source in sorted ( g_sources . keys ( ) ) : <EOL> names . append ( g_sources [ source ] . attrs [ '<STR_LIT:name>' ] . decode ( '<STR_LIT:utf-8>' ) ) <EOL> return names <EOL> @ on_the_fly_hdf5 <EOL> def get_available_components ( self , iteration = - <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> from . helpers import find_last_iteration <EOL> if iteration == - <NUM_LIT:1> : <EOL> iteration = find_last_iteration ( self . file ) <EOL> components = list ( self . file [ '<STR_LIT>' % iteration ] . keys ( ) ) <EOL> if '<STR_LIT>' in components : <EOL> components . append ( '<STR_LIT>' ) <EOL> return components <EOL> @ on_the_fly_hdf5 <EOL> def get_quantities ( self , iteration = - <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> from . helpers import find_last_iteration <EOL> if '<STR_LIT>' in self . file : <EOL> if self . file [ '<STR_LIT>' ] . file != self . file . file : <EOL> g_grid = self . file [ '<STR_LIT>' ] . file [ self . file [ '<STR_LIT>' ] . name ] [ '<STR_LIT>' ] <EOL> else : <EOL> g_grid = self . file [ '<STR_LIT>' ] <EOL> else : <EOL> g_grid = self . file [ '<STR_LIT>' ] <EOL> coord_type = g_grid [ '<STR_LIT>' ] . attrs [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf-8>' ) <EOL> if coord_type == '<STR_LIT>' : <EOL> g = CartesianGrid ( ) <EOL> elif coord_type == '<STR_LIT>' : <EOL> g = CylindricalPolarGrid ( ) <EOL> elif coord_type == '<STR_LIT>' : <EOL> g = SphericalPolarGrid ( ) <EOL> elif coord_type == '<STR_LIT>' : <EOL> g = AMRGrid ( ) <EOL> elif coord_type == '<STR_LIT>' : <EOL> g = OctreeGrid ( ) <EOL> elif coord_type == '<STR_LIT>' : <EOL> g = VoronoiGrid ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( coord_type ) ) <EOL> g . read_geometry ( g_grid [ '<STR_LIT>' ] ) <EOL> if iteration == - <NUM_LIT:1> : <EOL> iteration = find_last_iteration ( self . file ) <EOL> else : <EOL> iteration = iteration + <NUM_LIT:1> <EOL> if iteration > <NUM_LIT:0> : <EOL> g . read_quantities ( self . file [ '<STR_LIT>' % iteration ] ) <EOL> if not '<STR_LIT>' in g : <EOL> logger . info ( "<STR_LIT>" ) <EOL> g . read_quantities ( g_grid [ '<STR_LIT>' ] , quantities = [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in g : <EOL> n_dust = g . n_dust <EOL> g_dust = self . file [ '<STR_LIT>' ] <EOL> convert_func = [ ] <EOL> for i in range ( n_dust ) : <EOL> dust = g_dust [ '<STR_LIT>' % ( i + <NUM_LIT:1> ) ] <EOL> d = SphericalDust ( dust ) <EOL> convert_func . append ( d . specific_energy2temperature ) <EOL> def specific_energy2temperature ( quantities ) : <EOL> quantities [ '<STR_LIT>' ] = [ ] <EOL> for i in range ( n_dust ) : <EOL> quantities [ '<STR_LIT>' ] . append ( convert_func [ i ] ( quantities [ '<STR_LIT>' ] [ i ] ) ) <EOL> g . add_derived_quantity ( '<STR_LIT>' , specific_energy2temperature ) <EOL> return g <EOL> @ on_the_fly_hdf5 <EOL> def get_physical_grid ( self , name , iteration = - <NUM_LIT:1> , dust_id = '<STR_LIT:all>' ) : <EOL> '''<STR_LIT>''' <EOL> from . helpers import find_last_iteration <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> if name == '<STR_LIT>' : <EOL> if dust_id != '<STR_LIT:all>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> available_components = self . get_available_components ( ) <EOL> if name not in available_components : <EOL> raise Exception ( "<STR_LIT>" % '<STR_LIT:/>' . join ( available_components ) ) <EOL> if iteration == - <NUM_LIT:1> : <EOL> iteration = find_last_iteration ( self . file ) <EOL> if name == '<STR_LIT>' : <EOL> array = np . array ( self . file [ '<STR_LIT>' % iteration ] [ '<STR_LIT>' ] ) <EOL> g_dust = self . file [ '<STR_LIT>' ] <EOL> g_dust = g_dust . file [ g_dust . name ] <EOL> for i in range ( array . shape [ <NUM_LIT:0> ] ) : <EOL> dust = g_dust [ '<STR_LIT>' % ( i + <NUM_LIT:1> ) ] <EOL> d = SphericalDust ( dust ) <EOL> array [ i , : , : , : ] = d . specific_energy2temperature ( array [ i , : , : , : ] ) <EOL> else : <EOL> array = np . array ( self . file [ '<STR_LIT>' % iteration ] [ name ] ) <EOL> if name == '<STR_LIT>' : <EOL> return array <EOL> elif dust_id == '<STR_LIT:all>' : <EOL> return [ array [ i , : , : , : ] for i in range ( array . shape [ <NUM_LIT:0> ] ) ] <EOL> else : <EOL> return array [ dust_id , : , : , : ] </s>
