0001 % This script turns a set of spectrographs into averaged, corrected, and
0002 % cropped spectrographs and saves them as mat files. This script only
0003 % needs to run once on a new set of images. Once those images have been
0004 % processed you should work with the resulting mat files. This prevents
0005 % lots of extra computation from reprocessing the image files every time
0006 % you wish to produce a spectrogram. However, the source images remain
0007 % unchanged and the functions are capable of processing them if for some
0008 % reason you want to process them individually with your own scripts,
0009 % for an example of this see:
0010 % examples/example01_cyan_LED_spectrogram_not_preprocessed.m
0011 %
0012 % The region of interest (roi) is the region a spectrograph is cropped to.
0013 
0014 initialize_spectrometer_workspace
0015 
0016 dark = [];
0017 flat = [];
0018 
0019 % uncomment these if you don't want to use real darks, flats, and/or perform
0020 % tone linearization.
0021 %dark = 0;
0022 %flat = 1;
0023 
0024 so.dir_light = [so.bd s 'light_frames' s so.exposure s so.sdate];
0025 
0026 % If you are using an irradiance sensor then the center of your region of
0027 % interest should be level with the center of the irradiance sensor.
0028 % y0 must be odd so that the filter pattern of the cropped image data remains
0029 % the same as filter pattern for the entire image (assuming the Bayer filter
0030 % pattern is 2x2).
0031 y0 = 1175; % center of irradiance sensor located at y0+(h/2).
0032 h = 500; % set height of region of interest, h;
0033 
0034 %roi_light = [0, 0]; % set roi to zeroes if you want to use the entire image.
0035 roi_light = [y0, h, 0, 0]; % region of interest
0036 
0037 % load master frames
0038 g = waitbar(0, ['Loading ' so.ftype ' master frames and ' ...
0039                 'processing reference frames. ']);
0040 waitbar(0.001, g); % Octave's buggy waitbar doesn't like 0 or 1
0041 Ns = 4; % number of waitbar steps
0042 
0043 if isempty(dark)
0044     load([so.bd s 'dark_frames' s 'light_darks' s so.ftype ...
0045           '_master_dark_' so.exposure '.mat']);
0046 end
0047 waitbar(1/Ns, g);
0048 
0049 if isempty(flat)
0050     load([so.bd s 'flat_frames' s so.ftype '_master_flat.mat']);
0051 end
0052 waitbar(2/Ns, g);
0053 
0054 
0055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0056 % Process spectral and radiometric reference spectrographs                     %
0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0058 % process spectral_ref
0059 imgdir = [so.dir_light s 'reference' s 'spectral' s so.ftype];
0060 spectral_ref = ensemble_average(imgdir);
0061 
0062 spectral_ref = spectral_ref - dark;
0063 spectral_ref = flat_correct(spectral_ref, flat);
0064 spectral_ref = crop_image(spectral_ref, roi_light);
0065 
0066 if strcmpi(so.ftype, 'PGM')
0067     spectral_ref = bayer_demosaic(spectral_ref);
0068 end
0069 
0070 [ign, pdirpath] = pop_dirname(imgdir);
0071 metadata = process_metadata_file(pdirpath);
0072 
0073 matname = [so.ftype '_spectral_ref.mat'];
0074 save('-V6', [pdirpath s matname], 'spectral_ref', 'metadata');
0075 waitbar(3/Ns, g);
0076 
0077 % process radiometric_ref
0078 imgdir = [so.dir_light s 'reference' s 'radiometric' s so.ftype];
0079 radiometric_ref = ensemble_average(imgdir);
0080 
0081 radiometric_ref = radiometric_ref - dark;
0082 radiometric_ref = flat_correct(radiometric_ref, flat);
0083 radiometric_ref = crop_image(radiometric_ref, roi_light);
0084 
0085 if strcmpi(so.ftype, 'PGM')
0086     radiometric_ref = bayer_demosaic(radiometric_ref);
0087 end
0088 
0089 [ign, pdirpath] = pop_dirname(imgdir);
0090 metadata = process_metadata_file(pdirpath);
0091 
0092 matname = [so.ftype '_radiometric_ref.mat'];
0093 save('-V6', [pdirpath s matname], 'radiometric_ref', 'metadata');
0094 clear spectral_ref radiometric_ref;
0095 waitbar((4/Ns)-0.001, g);
0096 
0097 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0098 % Process spectrographs of interest                                            %
0099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0100 g = waitbar(0, ['Processing ' so.ftype ' spectrographs. ' ...
0101                 'This may take several minutes.']);
0102 waitbar(0.001, g); % Octave's buggy waitbar doesn't like 0 or 1
0103 dl = list_dir([so.dir_light s 'spectrographs'], so.ftype, 3);
0104 Ns = length(dl);
0105 
0106 for jj=1:Ns
0107     waitbar(((jj-1)/Ns)-0.001, g);
0108     fl = list_dir(dl{jj}, ['*.' so.ftype], 1);
0109     if isempty(fl)
0110         continue;
0111     end
0112     spctgrph = ensemble_average(dl{jj});
0113     spctgrph = spctgrph - dark;
0114 
0115     spctgrph = flat_correct(spctgrph, flat);
0116     spctgrph = crop_image(spctgrph, roi_light);
0117 
0118     if strcmpi(so.ftype, 'PGM')
0119         spctgrph = bayer_demosaic(spctgrph);
0120     end
0121 
0122     [ign, pdirpath] = pop_dirname(dl{jj});
0123     metadata = process_metadata_file(pdirpath);
0124 
0125     [ensemble_number, pdirpath] = pop_dirname(pdirpath);
0126     spectrographs_descriptor = pop_dirname(pdirpath);
0127     matname = [so.ftype '_' spectrographs_descriptor '_' ensemble_number ...
0128                '.mat'];
0129     save('-V6', [pdirpath s matname], 'spctgrph', 'metadata');
0130 
0131 end
0132 waitbar(1-0.001, g);
0133 
0134 disp('')
0135 disp([mfilename() ' finished.'])
0136 clear all