% Routine to save raw .nef files into the desired 4 channels
% used by ISET in the RGBG format
% Written by Joan Moh
% -------------------------------------------------------------

data = nefread('data\640-1.nef');
data = double(data);

rearranged_data = zeros(2014, 3040);

% Swapping data around into Bayer GRBG as used in ISET
% Original data 1, 2, 3, 4 correspond to R,G,B,G
% in BGGR format where BGGR = 3-2-4-1

rearranged_data(2:2:end, 2:2:end) = data(:,:,2);
rearranged_data(1:2:end, 2:2:end) = data(:,:,1);
rearranged_data(1:2:end, 1:2:end) = data(:,:,4);
rearranged_data(2:2:end, 1:2:end) = data(:,:,3);

volts = rearranged_data/2047;
save 640pixel volts;