Update to check if file exist and other fix

This commit is contained in:
linarphy 2023-07-18 15:11:49 +02:00
parent 98efc87ee4
commit b855e870b7
No known key found for this signature in database
GPG key ID: 3D4AAAC3AD16E79C

43
ETA.py
View file

@ -10,7 +10,8 @@ from scipy.signal import convolve as sp_convolve
from scipy.signal import find_peaks from scipy.signal import find_peaks
from scipy.ndimage import rotate 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: if len( sys.argv ) < 2:
raise Exception( 'ETA.py: type \'ETA.py -h\' for more information' ) raise Exception( 'ETA.py: type \'ETA.py -h\' for more information' )
@ -96,21 +97,33 @@ if verbose:
\n --verbose: True ( default to False)\ \n --verbose: True ( default to False)\
\n\ \n\
\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 ) data = utils.load( filename )
if verbose: if verbose:
print( 'data loaded' ) print( 'data loaded' )
cache_file = pathlib.Path( cache ) if 'cache' in files and files[ 'cache' ].is_file() and not no_cache:
if cache_file.is_file() and not no_cache:
if verbose: if verbose:
print( 'using cache' ) 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' ]: for key in [ 'data' , 'border' , 'calibrations' ]:
if key not in cache: 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' ] data = cache[ 'data' ]
border = cache[ 'border' ] border = cache[ 'border' ]
calibrations = cache[ 'calibrations' ] calibrations = cache[ 'calibrations' ]
@ -444,10 +457,10 @@ else:
if verbose: if verbose:
print( 'main ETA curved' ) 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: if verbose:
print( 'writing result in cache' ) 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[ 'data' ] = data
cache[ 'border' ] = border cache[ 'border' ] = border
cache[ 'calibrations'] = calibrations cache[ 'calibrations'] = calibrations
@ -588,8 +601,8 @@ if verbose:
bias = { bias = {
'top': np.mean( 'top': np.mean(
data[ data[
calibrations[ 'top' ][ 'y' ][ 'min' ] : calibrations[ 'top' ][ 'y' ][ 'min' ] - 100:
calibrations[ 'top' ][ 'y' ][ 'min' ] - 500, calibrations[ 'top' ][ 'y' ][ 'min' ] ,
calibrations[ 'top' ][ 'x' ][ 'min' ] : calibrations[ 'top' ][ 'x' ][ 'min' ] :
calibrations[ 'top' ][ 'x' ][ 'max' ] calibrations[ 'top' ][ 'x' ][ 'max' ]
] , ] ,
@ -609,21 +622,17 @@ bias = {
mean_bias = sp_convolve( mean_bias = sp_convolve(
np.mean( np.mean(
[ [
bias[ 'down' ] , bias[ 'top' ],
bias[ 'down' ], bias[ 'down' ],
] , ] ,
axis = 0, axis = 0,
) , ) ,
np.ones( np.ones(
( 50,
50,
),
) , ) ,
'same' , 'same' ,
) / 50 ) / 50
print( 'mean_bias ETA: ' + str( mean_bias ) )
data[ data[
: , : ,
border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ] border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ]