Add logging & fix

- Add --verbose option
- Fix calibration
This commit is contained in:
linarphy 2023-05-26 16:22:11 +02:00
parent d1b02301aa
commit 6eaf141f1f
No known key found for this signature in database
GPG key ID: 3D4AAAC3AD16E79C

104
ETA.py
View file

@ -8,7 +8,7 @@ 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 = '' , None , None , None cache , filename , output , calibration , verbose , no_cache = '' , None , None , None , 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' )
@ -23,6 +23,8 @@ while i < len( argv ):
arg = '--help' arg = '--help'
elif arg == '-v': elif arg == '-v':
arg = '--version' arg = '--version'
elif arg == '-l':
arg = '--verbose'
elif arg == '-n': elif arg == '-n':
arg == '--no-cache' arg == '--no-cache'
elif arg == '-c': elif arg == '-c':
@ -55,14 +57,17 @@ while i < len( argv ):
\n -n --no-cache do not use cache and rewrite it\ \n -n --no-cache do not use cache and rewrite it\
\n -o --output output file, default to standard output\ \n -o --output output file, default to standard output\
\n -v --version show version number and quit\ \n -v --version show version number and quit\
\n -l --verbose show more information to help debugging\
\n\ \n\
\nParse a naroo ETA fits' ) \nParse a naroo ETA fits' )
exit() exit()
elif arg == '--version': elif arg == '--version':
print( '0.2' ) print( '0.2' )
exit() exit()
elif arg == '--verbose':
verbose = True
elif arg == '--no-cache': elif arg == '--no-cache':
cache = '' # hack, empty string mean this dir, so not a file no_cache = True
elif len( arg ) > 8 and arg[ : 8 ] == '--cache=': elif len( arg ) > 8 and arg[ : 8 ] == '--cache=':
cache = arg[ 8 : ] cache = arg[ 8 : ]
elif len( arg ) > 9 and arg[ : 9 ] == '--output=': elif len( arg ) > 9 and arg[ : 9 ] == '--output=':
@ -78,21 +83,39 @@ while i < len( argv ):
i += 1 i += 1
if filename == None: if filename == None:
raise Exception( 'ETA.py: filename should be given' ) raise Exception( 'ETA.py: filename should be given' )
if verbose:
cache, filename, output, calibration, verbose
print( f'ETA.py: launching now with parameters:\
\n --filename: {filename}\
\n --cache: {cache} ( default: \'\' )\
\n --calibration: {calibration} ( default to None )\
\n --output: {output} ( default to None )\
\n --verbose: True ( default to False)\
\n\
\n===========================================' )
# TODO: check in advance file to check if exists or writeable # TODO: check in advance file to check if exists or writeable
data = utils.load( filename ) data = utils.load( filename )
if verbose:
print( 'data loaded' )
cache_file = pathlib.Path( cache ) cache_file = pathlib.Path( cache )
if cache_file.is_file(): if cache_file.is_file() and not no_cache:
if verbose:
print( 'using cache' )
with shelve.open( str( cache_file ) ) as cache: with shelve.open( str( cache_file ) ) as cache:
for key in [ 'rotated_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' ]
else: else:
if verbose:
print( 'not using cache' )
print( 'starting first zoom' )
""" """
find fill points find fill points
""" """
@ -182,6 +205,10 @@ else:
}, },
} }
if verbose:
print( 'first zoom finished' )
print( 'starting label deletion' )
""" """
label deletion label deletion
""" """
@ -228,6 +255,10 @@ else:
else: else:
raise Exception( 'for an unkown reason, label_x[ \'min\' ] > label_x[ \'max\' ]' ) raise Exception( 'for an unkown reason, label_x[ \'min\' ] > label_x[ \'max\' ]' )
if verbose:
print( 'label section deleted' )
print( 'starting rotation' )
""" """
Rotation Rotation
""" """
@ -281,6 +312,10 @@ else:
border[ 'y' ][ 'min' ] -= diff_y border[ 'y' ][ 'min' ] -= diff_y
border[ 'y' ][ 'max' ] -= diff_y border[ 'y' ][ 'max' ] -= diff_y
if verbose:
print( 'rotation finished' )
print( 'starting isolating calibration' )
""" """
Calibration Calibration
""" """
@ -351,6 +386,10 @@ else:
}, },
} }
if verbose:
print( 'calibration lines isolated' )
print( 'starting curving main ETA' )
""" """
stripes curves detection stripes curves detection
""" """
@ -393,15 +432,25 @@ else:
) ) ) )
data[ data[
calibrations[ 'top' ][ 'y' ][ 'max' ] : calibrations[ 'down' ][ 'y' ][ 'min' ] + np.max( y_diff ), calibrations[ 'top' ][ 'y' ][ 'max' ] : calibrations[ 'down' ][ 'y' ][ 'min' ],
border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ] border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ]
] = results ] = results[
: results.shape[0] - np.max( y_diff ),
:
]
if verbose:
print( 'main ETA curved' )
if not cache_file.exists(): if not cache_file.exists():
if verbose:
print( 'writing result in cache' )
with shelve.open( str( cache_file ) ) as cache: with shelve.open( str( cache_file ) ) as cache:
cache[ 'data' ] = data cache[ 'data' ] = data
cache[ 'border' ] = border cache[ 'border' ] = border
cache[ 'calibrations'] = calibrations cache[ 'calibrations'] = calibrations
if verbose:
print( 'cache saved' )
size_x = border[ 'x' ][ 'max' ] - border[ 'x' ][ 'min' ] size_x = border[ 'x' ][ 'max' ] - border[ 'x' ][ 'min' ]
size_y = calibrations[ 'down' ][ 'y' ][ 'min' ] - calibrations[ 'top' ][ 'y' ][ 'max' ] size_y = calibrations[ 'down' ][ 'y' ][ 'min' ] - calibrations[ 'top' ][ 'y' ][ 'max' ]
@ -409,6 +458,8 @@ size_y = calibrations[ 'down' ][ 'y' ][ 'min' ] - calibrations[ 'top' ][ 'y' ][
# Calibration # Calibration
if calibration != None: if calibration != None:
if verbose:
print( 'starting calibration' )
import wavelength_calibration as wave_calib import wavelength_calibration as wave_calib
peaks_calib = np.loadtxt( calibration ) # sorted list peaks_calib = np.loadtxt( calibration ) # sorted list
peaks_calib = np.sort( peaks_calib ) peaks_calib = np.sort( peaks_calib )
@ -465,6 +516,10 @@ if calibration != None:
3 , 3 ,
) )
if verbose:
print( 'x pixel to wavelenth calibration finished' )
print( 'starting y pixel to x pixel calibration' )
def diff_calc( x , list_y ): def diff_calc( x , list_y ):
""" """
give "good" x list for a given x and y give "good" x list for a given x and y
@ -498,6 +553,9 @@ if calibration != None:
calibrations[ 'down' ][ 'x' ][ 'max' ] -= padding calibrations[ 'down' ][ 'x' ][ 'max' ] -= padding
border[ 'x' ][ 'max' ] -= padding border[ 'x' ][ 'max' ] -= padding
if verbose:
print( 'y to x pixel calibrated' )
polyval_wavelength = np.polyfit( # x = 0 begins at the start of polyval_wavelength = np.polyfit( # x = 0 begins at the start of
peaks_up , # calibrations[ 'top' ][ 'x' ][ 'min' ] peaks_up , # calibrations[ 'top' ][ 'x' ][ 'min' ]
peaks_calib, peaks_calib,
@ -511,13 +569,28 @@ if calibration != None:
1 , 1 ,
) )
) )
if verbose:
print( 'end of calibration' )
else: else:
if verbose:
print( 'no calibration' )
padding = 0 padding = 0
wavelength = np.arange( size_y ) wavelength = np.arange( size_y )
if verbose:
print( 'starting to flatten the ETA' )
list_ = np.convolve( list_ = np.convolve(
np.gradient( np.gradient(
np.mean( data , axis = 1 ), np.mean(
data[
calibrations[ 'top' ][ 'y' ][ 'max' ] : calibrations[ 'down' ][ 'y' ][ 'min' ],
border[ 'x' ][ 'min' ] : border[ 'x' ][ 'max' ]
] ,
axis = 1,
),
) , ) ,
np.ones( 50 ), np.ones( 50 ),
'same' , 'same' ,
@ -529,20 +602,29 @@ fall = np.array( [
] + [ ] + [
consecutive[0] + np.argmin( consecutive[0] + np.argmin(
list_[ consecutive[0] : consecutive[-1] ] list_[ consecutive[0] : consecutive[-1] ]
) for consecutive in fall ) for consecutive in fall if len( consecutive ) > 20 # the value might change
] ).astype( int ) ] ).astype( int )
stairs = np.zeros( ( size_y , size_x ) ) stairs = np.zeros( ( size_y , size_x ) )
for x in range( size_x ): for x in range( size_x ):
stairs[ : fall[0] , x ] = 0 # TODO: put the mean stairs[ : fall[0] , x ] = 0 # TODO: put the mean
for i in range( len( fall ) - 1 ): for i in range( len( fall ) - 1 ):
stairs[ fall[ i ] : fall[ i + 1 ] , x ] = np.mean( data[ fall[ i ] : fall[ i + 1 ] , x ] ) stairs[ fall[ i ] : fall[ i + 1 ] , x ] = np.mean( data[
calibrations[ 'top' ][ 'y' ][ 'max' ] + fall[ i ] : calibrations[ 'top' ][ 'y' ][ 'max' ] + fall[ i + 1 ],
border[ 'x' ][ 'min' ] + x
] )
stairs[ fall[ -1 ] : , x ] = 0 stairs[ fall[ -1 ] : , x ] = 0
np.imshow( stairs , aspect = 'auto' ) if verbose:
np.show() print( 'ETA flattened' )
print( 'outputting data' )
if output == None: if output == None:
print( stairs ) print( stairs )
else: else:
np.savetxt( output , stairs ) np.savetxt( output , stairs )
if verbose:
print( 'output sent' )
print( '===========================================\
\nend of ETA.py' )