From b855e870b756d384efa26839989a2d46ef2c7f95 Mon Sep 17 00:00:00 2001 From: linarphy Date: Tue, 18 Jul 2023 15:11:49 +0200 Subject: [PATCH] Update to check if file exist and other fix --- ETA.py | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/ETA.py b/ETA.py index f900137..8f5fe89 100755 --- a/ETA.py +++ b/ETA.py @@ -10,7 +10,8 @@ from scipy.signal import convolve as sp_convolve from scipy.signal import find_peaks from scipy.ndimage import rotate -cache , filename , output , calibration , verbose , no_cache = '' , None , None , None , False , False +cache , filename , output , calibration = None , None , None , None +verbose , no_cache = False , False if len( sys.argv ) < 2: raise Exception( 'ETA.py: type \'ETA.py -h\' for more information' ) @@ -96,21 +97,33 @@ if verbose: \n --verbose: True ( default to False)\ \n\ \n===========================================' ) -# TODO: check in advance file to check if exists or writeable +files = {} +if calibration != None: + files[ 'wavelength' ] = pathlib.Path( calibration ) +if cache != None: + files[ 'cache' ] = pathlib.Path( cache ) + +files[ 'filename' ] = pathlib.Path( filename ) + +for name , path in files.items(): + if name in [ + 'wavelength', + ] and not path.is_file(): + raise Exception( + 'ETA.py: could not open the ' + name + ' file' + ) data = utils.load( filename ) if verbose: print( 'data loaded' ) -cache_file = pathlib.Path( cache ) - -if cache_file.is_file() and not no_cache: +if 'cache' in files and files[ 'cache' ].is_file() and not no_cache: if verbose: print( 'using cache' ) - with shelve.open( str( cache_file ) ) as cache: + with shelve.open( str( files[ 'cache' ] ) ) as cache: for key in [ 'data' , 'border' , 'calibrations' ]: if key not in cache: - raise Exception( 'ETA.py: missing data in cache_file' ) + raise Exception( 'ETA.py: missing data in cache file' ) data = cache[ 'data' ] border = cache[ 'border' ] calibrations = cache[ 'calibrations' ] @@ -444,10 +457,10 @@ else: if verbose: print( 'main ETA curved' ) - if not cache_file.exists() and not no_cache: + if 'cache' in files and not files[ 'cache' ].exists() and not no_cache: if verbose: print( 'writing result in cache' ) - with shelve.open( str( cache_file ) ) as cache: + with shelve.open( str( files[ 'cache' ] ) ) as cache: cache[ 'data' ] = data cache[ 'border' ] = border cache[ 'calibrations'] = calibrations @@ -588,8 +601,8 @@ if verbose: bias = { 'top': np.mean( data[ - calibrations[ 'top' ][ 'y' ][ 'min' ] : - calibrations[ 'top' ][ 'y' ][ 'min' ] - 500, + calibrations[ 'top' ][ 'y' ][ 'min' ] - 100: + calibrations[ 'top' ][ 'y' ][ 'min' ] , calibrations[ 'top' ][ 'x' ][ 'min' ] : calibrations[ 'top' ][ 'x' ][ 'max' ] ] , @@ -609,21 +622,17 @@ bias = { mean_bias = sp_convolve( np.mean( [ - bias[ 'down' ] , + bias[ 'top' ], bias[ 'down' ], ] , axis = 0, ) , np.ones( - ( - 50, - ), + 50, ) , 'same' , ) / 50 -print( 'mean_bias ETA: ' + str( mean_bias ) ) - data[ : , border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ]