-
Notifications
You must be signed in to change notification settings - Fork 5
/
preload_qsos.m
83 lines (64 loc) · 2.9 KB
/
preload_qsos.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% preload_qsos: loads spectra from SDSS FITS files, applies further
% filters, and applies some basic preprocessing such as normalization
% and truncation to the region of interest
% load QSO catalog
variables_to_load = {'z_qsos', 'plates', 'mjds', 'fiber_ids', 'filter_flags'};
load(sprintf('%s/catalog', processed_directory(release)), ...
variables_to_load{:});
num_quasars = numel(z_qsos);
all_wavelengths = cell(num_quasars, 1);
all_flux = cell(num_quasars, 1);
all_noise_variance = cell(num_quasars, 1);
all_pixel_mask = cell(num_quasars, 1);
all_normalizers = zeros(num_quasars, 1);
for i = 1:num_quasars
if (filter_flags(i) > 0)
continue;
end
[this_wavelengths, this_flux, this_noise_variance, this_pixel_mask] ...
= file_loader(plates(i), mjds(i), fiber_ids(i));
this_rest_wavelengths = emitted_wavelengths(this_wavelengths, z_qsos(i));
% normalize flux
ind = (this_rest_wavelengths >= normalization_min_lambda) & ...
(this_rest_wavelengths <= normalization_max_lambda) & ...
(~this_pixel_mask);
this_median = nanmedian(this_flux(ind));
% bit 2: cannot normalize (all normalizing pixels are masked)
if (isnan(this_median))
filter_flags(i) = bitset(filter_flags(i), 3, true);
continue;
end
ind = (this_rest_wavelengths >= min_lambda) & ...
(this_rest_wavelengths <= max_lambda) & ...
(~this_pixel_mask);
% bit 3: not enough pixels available
if (nnz(ind) < min_num_pixels)
filter_flags(i) = bitset(filter_flags(i), 4, true);
continue;
end
all_normalizers(i) = this_median;
this_flux = this_flux / this_median;
this_noise_variance = this_noise_variance / this_median^2;
ind = (this_rest_wavelengths >= loading_min_lambda) & ...
(this_rest_wavelengths <= loading_max_lambda);
% add one pixel on either side
available_ind = find(~ind & ~this_pixel_mask);
ind(min(available_ind(available_ind > find(ind, 1, 'last' )))) = true;
ind(max(available_ind(available_ind < find(ind, 1, 'first')))) = true;
all_wavelengths{i} = this_wavelengths(ind);
all_flux{i} = this_flux(ind);
all_noise_variance{i} = this_noise_variance(ind);
all_pixel_mask{i} = this_pixel_mask(ind);
fprintf('loaded quasar %i of %i (%i/%i/%04i)\n', ...
i, num_quasars, plates(i), mjds(i), fiber_ids(i));
end
variables_to_save = {'loading_min_lambda', 'loading_max_lambda', ...
'normalization_min_lambda', 'normalization_max_lambda', ...
'min_num_pixels', 'all_wavelengths', 'all_flux', ...
'all_noise_variance', 'all_pixel_mask', ...
'all_normalizers'};
save(sprintf('%s/preloaded_qsos', processed_directory(release)), ...
variables_to_save{:}, '-v7.3');
% write new filter flags to catalog
save(sprintf('%s/catalog', processed_directory(release)), ...
'filter_flags', '-append');