<s> from __future__ import print_function , division <EOL> import string <EOL> import random <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import h5py <EOL> import numpy as np <EOL> from . constants import h , c , k <EOL> from astropy import log as logger <EOL> MAX_FLOAT = np . log ( np . finfo ( '<STR_LIT:d>' ) . max ) <EOL> def random_id ( length = <NUM_LIT:32> ) : <EOL> return '<STR_LIT>' . join ( random . sample ( string . ascii_letters + string . digits , length ) ) <EOL> def virtual_file ( ) : <EOL> return h5py . File ( random_id ( ) , driver = '<STR_LIT>' , backing_store = False ) <EOL> def str2bool ( value ) : <EOL> return value . lower ( ) [ <NUM_LIT:0> : <NUM_LIT:1> ] . decode ( '<STR_LIT:ascii>' ) == '<STR_LIT:y>' <EOL> def bool2str ( value ) : <EOL> return np . string_ ( '<STR_LIT:yes>' . encode ( '<STR_LIT:utf-8>' ) ) if value else np . string_ ( '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def link_or_copy ( group , name , link , copy , absolute_paths = False ) : <EOL> '''<STR_LIT>''' <EOL> if copy : <EOL> f = h5py . File ( link . filename , '<STR_LIT:r>' ) <EOL> f . copy ( link . path , group , name = name ) <EOL> f . close ( ) <EOL> else : <EOL> if absolute_paths : <EOL> group [ name ] = h5py . ExternalLink ( os . path . abspath ( link . filename ) , link . path ) <EOL> else : <EOL> group [ name ] = h5py . ExternalLink ( os . path . relpath ( link . filename , os . path . dirname ( group . file . filename ) ) , link . path ) <EOL> try : <EOL> group [ name ] <EOL> except KeyError : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> del group [ name ] <EOL> f = h5py . File ( link . filename , '<STR_LIT:r>' ) <EOL> f . copy ( link . path , group , name = name ) <EOL> f . close ( ) <EOL> class FreezableClass ( object ) : <EOL> _frozen = False <EOL> _final = False <EOL> _attributes = [ ] <EOL> def __init__ ( self ) : <EOL> super ( FreezableClass , self ) . __init__ ( ) <EOL> def _freeze ( self ) : <EOL> object . __setattr__ ( self , '<STR_LIT>' , True ) <EOL> def _finalize ( self ) : <EOL> object . __setattr__ ( self , '<STR_LIT>' , True ) <EOL> def isfrozen ( self ) : <EOL> return self . _frozen <EOL> def isfinal ( self ) : <EOL> return self . _final <EOL> def __setattr__ ( self , key , value ) : <EOL> if self . _final : <EOL> raise Exception ( "<STR_LIT>" % key ) <EOL> if self . _frozen and not key in self . _attributes : <EOL> raise AttributeError ( "<STR_LIT>" % key ) <EOL> self . _attributes . append ( key ) <EOL> object . __setattr__ ( self , key , value ) <EOL> def planck_nu_range ( tmin , tmax = None ) : <EOL> alpha = <NUM_LIT> <EOL> nu_peak_min = alpha / h * k * tmin <EOL> if tmax is None : <EOL> nu_peak_max = alpha / h * k * tmin <EOL> else : <EOL> nu_peak_max = alpha / h * k * tmax <EOL> nu_min = np . log10 ( nu_peak_min / <NUM_LIT> ) <EOL> nu_max = np . log10 ( nu_peak_max * <NUM_LIT> ) <EOL> n_nu = int ( ( nu_max - nu_min ) * <NUM_LIT> ) <EOL> return np . logspace ( nu_min , nu_max , n_nu ) <EOL> def nu_common ( nu1 , nu2 ) : <EOL> nu_common = np . hstack ( [ nu1 , nu2 ] ) <EOL> nu_common . sort ( ) <EOL> keep = ( nu_common [ <NUM_LIT:1> : ] - nu_common [ : - <NUM_LIT:1> ] ) / nu_common [ : - <NUM_LIT:1> ] > <NUM_LIT> <EOL> keep = np . hstack ( [ keep , True ] ) <EOL> nu_common = nu_common [ keep ] <EOL> return nu_common <EOL> class extrap1d_log10 ( object ) : <EOL> def __init__ ( self , x , y ) : <EOL> self . x = np . log10 ( x ) <EOL> self . y = np . log10 ( y ) <EOL> def __call__ ( self , x ) : <EOL> xval = np . log10 ( x ) <EOL> if self . x [ - <NUM_LIT:1> ] > self . x [ <NUM_LIT:0> ] : <EOL> top = xval > self . x [ - <NUM_LIT:1> ] <EOL> bot = xval < self . x [ <NUM_LIT:0> ] <EOL> else : <EOL> top = xval < self . x [ - <NUM_LIT:1> ] <EOL> bot = xval > self . x [ <NUM_LIT:0> ] <EOL> if type ( xval ) == np . ndarray : <EOL> yval = np . zeros ( xval . shape ) <EOL> yval [ top ] = <NUM_LIT> ** ( self . y [ - <NUM_LIT:1> ] + ( xval [ top ] - self . x [ - <NUM_LIT:1> ] ) * ( self . y [ - <NUM_LIT:1> ] - self . y [ - <NUM_LIT:2> ] ) / ( self . x [ - <NUM_LIT:1> ] - self . x [ - <NUM_LIT:2> ] ) ) <EOL> yval [ bot ] = <NUM_LIT> ** ( self . y [ <NUM_LIT:0> ] + ( xval [ bot ] - self . x [ <NUM_LIT:0> ] ) * ( self . y [ <NUM_LIT:0> ] - self . y [ <NUM_LIT:1> ] ) / ( self . x [ <NUM_LIT:0> ] - self . x [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> if top : <EOL> yval = <NUM_LIT> ** ( self . y [ - <NUM_LIT:1> ] + ( xval - self . x [ - <NUM_LIT:1> ] ) * ( self . y [ - <NUM_LIT:1> ] - self . y [ - <NUM_LIT:2> ] ) / ( self . x [ - <NUM_LIT:1> ] - self . x [ - <NUM_LIT:2> ] ) ) <EOL> elif bot : <EOL> yval = <NUM_LIT> ** ( self . y [ <NUM_LIT:0> ] + ( xval - self . x [ <NUM_LIT:0> ] ) * ( self . y [ <NUM_LIT:0> ] - self . y [ <NUM_LIT:1> ] ) / ( self . x [ <NUM_LIT:0> ] - self . x [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return yval <EOL> def B_nu ( nu , T ) : <EOL> x = h * nu / k / T <EOL> f = np . zeros ( nu . shape ) <EOL> main = ( <NUM_LIT> <= x ) & ( x < MAX_FLOAT ) <EOL> f [ main ] = <NUM_LIT> * h * nu [ main ] ** <NUM_LIT> / c ** <NUM_LIT> / ( np . exp ( x [ main ] ) - <NUM_LIT:1.> ) <EOL> small = x < <NUM_LIT> <EOL> f [ small ] = <NUM_LIT> * h * nu [ small ] ** <NUM_LIT> / c ** <NUM_LIT> / x [ small ] <EOL> return f <EOL> def dB_nu_dT ( nu , T ) : <EOL> b = B_nu ( nu , T ) <EOL> x = h * nu / k / T <EOL> f = np . zeros ( nu . shape ) <EOL> main = x >= <NUM_LIT> <EOL> f [ main ] = x [ main ] / T / ( <NUM_LIT:1> - np . exp ( - x [ main ] ) ) * b [ main ] <EOL> small = x < <NUM_LIT> <EOL> f [ small ] = b [ small ] / T <EOL> return f <EOL> def filename2hdf5 ( filename ) : <EOL> ext = os . path . splitext ( filename ) [ <NUM_LIT:1> ] <EOL> if ext in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return filename . replace ( ext , '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ext ) <EOL> def create_dir ( dir_name ) : <EOL> delete_dir ( dir_name ) <EOL> os . mkdir ( dir_name ) <EOL> def delete_dir ( dir_name ) : <EOL> if os . path . exists ( dir_name ) : <EOL> reply = raw_input ( "<STR_LIT>" % dir_name ) <EOL> if reply == '<STR_LIT:y>' : <EOL> shutil . rmtree ( dir_name ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> def delete_file ( file_name ) : <EOL> if os . path . exists ( file_name ) : <EOL> reply = raw_input ( "<STR_LIT>" % file_name ) <EOL> if reply == '<STR_LIT:y>' : <EOL> os . remove ( file_name ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> def is_numpy_array ( variable ) : <EOL> return issubclass ( variable . __class__ , ( np . ndarray , <EOL> np . core . records . recarray , <EOL> np . ma . core . MaskedArray ) ) <EOL> def monotonically_increasing ( array ) : <EOL> for i in range ( len ( array ) - <NUM_LIT:1> ) : <EOL> if not array [ i + <NUM_LIT:1> ] > array [ i ] : <EOL> return False <EOL> return True <EOL> try : <EOL> asstr = np . compat . asstr <EOL> except AttributeError : <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> def asstr ( s ) : <EOL> if isinstance ( s , bytes ) : <EOL> return s . decode ( '<STR_LIT>' ) <EOL> return str ( s ) <EOL> else : <EOL> asstr = str </s>
