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