root = io.find_root_folder(meanfile) savefolder = root / "analysis/flatfield/" label = meanfile.stem.split("_mean")[0] # Get metadata camera = io.load_metadata(root) print("Loaded metadata") # Load the data stdsfile = meanfile.parent / meanfile.name.replace("mean", "stds") mean_raw = np.load(meanfile) stds_raw = np.load(stdsfile) print("Loaded data") # Bias correction mean_bias_corrected = calibrate.correct_bias(root, mean_raw) # Normalise the RGBG2 channels to a maximum of 1 each mean_normalised, stds_normalised = flat.normalise_RGBG2( mean_bias_corrected, stds_raw, camera.bayer_map) print("Normalised data") # Calculate the signal-to-noise ratio (SNR) per pixel SNR = mean_normalised / stds_normalised print("Calculated signal-to-noise-ratio") # Make a histogram of the SNR save_to_histogram_SNR = savefolder / f"data_histogram_SNR_{label}.pdf" SNR_top_percentile = analyse.symmetric_percentiles(SNR)[1] bins_SNR = np.linspace(0, SNR_top_percentile, 100)
import numpy as np from sys import argv from spectacle import io, calibrate from matplotlib import pyplot as plt folder, wvl = io.path_from_input(argv) wvl = wvl.stem root = io.find_root_folder(folder) camera = io.load_metadata(root) m = np.load(folder / f"{wvl}_mean.npy") m = calibrate.correct_bias(root, m) s = np.load(folder / f"{wvl}_stds.npy") s[s < 0.001] = -1 # prevent infinities SNR = m / s m_RGBG, s_RGBG, SNR_RGBG = camera.demosaick(m, s, SNR) mean_stack = m_RGBG.mean(axis=(1, 2)) std_stack = m_RGBG.std(axis=(1, 2)) SNR_stack = mean_stack / std_stack for j, c in enumerate("RGBG"): SNR_here = SNR_RGBG[j].ravel() mean_here = m_RGBG[j].ravel() plt.figure(figsize=(10, 3)) plt.hist(SNR_here, bins=np.arange(0, 100, 1), color=c) plt.xlabel("SNR") plt.axvline(3, c='k', ls="--")
save_to_correction_modelled_calibration = root/"calibration/flatfield_correction_modelled.npy" save_to_parameters_calibration = root/"calibration/flatfield_parameters.npy" # Get metadata camera = io.load_metadata(root) print("Loaded metadata") # Load the data stdsfile = meanfile.parent / meanfile.name.replace("mean", "stds") mean = np.load(meanfile) stds = np.load(stdsfile) print("Loaded data") # Bias correction mean = calibrate.correct_bias(root, mean) # Normalise the RGBG2 channels to a maximum of 1 each mean_normalised, stds_normalised = flat.normalise_RGBG2(mean, stds, camera.bayer_map) print("Normalised data") # Convolve the flat-field data with a Gaussian kernel to remove small-scale variations flat_field_gauss = gaussMd(mean_normalised, 10) # Calculate the correction factor correction = 1 / flat_field_gauss correction_raw = 1 / mean_normalised # Save the correction factor maps np.save(save_to_correction, correction) np.save(save_to_correction_raw, correction_raw)
plt.show() # Get the data folder from the command line file = io.path_from_input(argv) root = io.find_root_folder(file) # Load the iSPEX wavelength solution coefficients = wavelength.load_coefficients( root / "intermediaries/spectral_response/ispex_wavelength_solution.npy") # Load the data img = io.load_raw_file(file) print("Loaded data") # Bias correction values = calibrate.correct_bias(root, img.raw_image.astype(np.float32)) # Flat-field correction - note that this clips the image values = calibrate.correct_flatfield(root, values) # Spectrum edges xmin, xmax = 1900, 3500 ymin_thin, ymax_thin = 450, 800 ymin_thick, ymax_thick = 900, 1220 thin_slit = np.s_[ymin_thin:ymax_thin, xmin:xmax] thick_slit = np.s_[ymin_thick:ymax_thick, xmin:xmax] x = np.arange(xmin, xmax) y_thin = np.arange(ymin_thin, ymax_thin) y_thick = np.arange(ymin_thick, ymax_thick)
import numpy as np from sys import argv from spectacle import raw, plot, io, wavelength, calibrate file = io.path_from_input(argv) root, images, stacks, products, results = io.folders(file) img = io.load_raw_file(file) exif = io.load_exif(file) values = img.raw_image.astype(np.float32) values = calibrate.correct_bias(root, values) image_cut = values[760:1000, 2150:3900] colors_cut = img.raw_colors[760:1000, 2150:3900] x = np.arange(2150, 3900) y = np.arange(760, 1000) RGBG = raw.demosaick(colors_cut, image_cut) plot.show_RGBG(RGBG) coefficients = wavelength.load_coefficients(results / "ispex/wavelength_solution.npy") wavelengths_cut = wavelength.calculate_wavelengths(coefficients, x, y) wavelengths_split = raw.demosaick(colors_cut, wavelengths_cut) lambdarange, all_interpolated = wavelength.interpolate_multi( wavelengths_split, RGBG) stacked = wavelength.stack(lambdarange, all_interpolated) plot.plot_spectrum(stacked[0], stacked[1:],
import numpy as np from sys import argv from spectacle import general, io, plot, wavelength, raw2, calibrate, flat # Get the data folder from the command line file = io.path_from_input(argv) root = io.find_root_folder(file) save_to = root / "intermediaries/spectral_response/ispex_wavelength_solution.npy" # Load the data img = io.load_raw_file(file) print("Loaded data") # Bias correction values = calibrate.correct_bias(root, img.raw_image) # Flat-field correction (includes clipping data) values = calibrate.correct_flatfield(root, values) # Clip the Bayer map to be the same shape bayer_map = flat.clip_data(img.raw_colors) # Cut out the spectrum # Note that these limits are hard-coded xmin, xmax = 1900, 3500 ymin, ymax = 510, 1220 cut = np.s_[ymin:ymax, xmin:xmax] image_cut = values[cut] colors_cut = bayer_map[cut] x = np.arange(xmin, xmax)