Add file check before loading them

This commit is contained in:
linarphy 2023-07-12 16:11:02 +02:00
parent 4a07b37e49
commit bdc8a82ded
No known key found for this signature in database
GPG key ID: 3D4AAAC3AD16E79C

View file

@ -11,7 +11,7 @@ import pathlib
from scipy.ndimage import rotate
from astropy.io import fits
cache , filename , output , calibration , intensity_calibration , verbose , no_cache = '' , None , None , None , None , False, False
cache , filename , output , calibration , intensity_calibration , verbose , no_cache = None , None , None , None , None , False, False
if len( sys.argv ) < 2:
raise Exception( 'spectrum.py: type \'spectrum.py -h\' for more information' )
@ -110,6 +110,26 @@ if verbose:
\n===========================================' )
# TODO: check in advance file to check if exists or writeable
files = {}
if calibration != None:
files[ 'wavelength' ] = pathlib.Path( calibration )
if intensity != None:
files[ 'intensity' ] = pathlib.Path( intensity )
if cache != None:
files[ 'cache' ] = pathlib.Path( cache )
files[ 'filename' ] = pathlib.Path( filename )
for name , path in files.items():
if name in [
'wavelength',
'intensity' ,
'spectrum' ,
] and not path.is_file():
raise Exception(
'spectrum.py: could not open the ' + name + ' file'
)
hdul = fits.open( filename )
data = hdul[0].data
head = hdul[0].header
@ -117,12 +137,10 @@ hdul.close()
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( 'spectrum.py: missing data in cache file' )
@ -473,10 +491,10 @@ else:
if verbose:
print( 'calibration isolation finished' )
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[ 'spectrum' ] = spectrum
@ -562,6 +580,17 @@ bias = {
mean_bias = np.mean( [ bias[ 'top' ] , bias[ 'down' ] ] , axis = 0 )
plt.imshow(
data[
calibrations[ 'down' ][ 'y' ][ 'max' ] :
calibrations[ 'down' ][ 'y' ][ 'max' ] + 200,
calibrations[ 'down' ][ 'x' ][ 'min' ] :
calibrations[ 'down' ][ 'x' ][ 'max' ]
] ,
aspect = 'auto',
)
plt.show()
if verbose:
print( 'bias substraction finished' )