function [x, Tb, rtdf, t, nav, navt] = read_camex(datecode)

% quick and easy (i.e. slow) matlab reader for NAST-M Camex-3 files
%
% usage:  [x, Tb, rtdf, t, nav, navt] = read_camex(datecode)
%
%  datecode is ddMmmyy  (ex: '02Sep98')
%
%  function assumes the file is in your current working directory
%
%  x:    uncalibrated radiances
%  Tb:   calibrated radiances
%  rtdf: rtd temperature measurements
%  t:    UTC timestamps for above (seconds since Jan. 1, 1970)
%  nav:  navigation records collected from ER-2
%  navt: UTC timestamps for above (seconds since Jan. 1, 1970)


NUM_CHANS = 16;   % number of channels
NUM_SPOTS = 25;

filename = ['CAMEX_NASTM_' datecode '.bin'];
nav_filename = ['CAMEX_NASTM_nav_' datecode '.bin'];


if exist(nav_filename)~= 2
	fprintf('It appears that navigation data is not available...skipping.\n');
	NAV = 0;
else
	NAV = 1;
	NUM_NAV_PARAMS = 48;    % number of nav. items per record
end

if NAV
	fprintf('Opening archive for %s.  Input files are:\n%s\n%s\n', datecode, filename, nav_filename);
else
	fprintf('Opening archive for %s.  Input file is:  %s\n', datecode, filename);
end

% Open radiometric stuff
fid = fopen(filename,'rb'); 
if (fid < 0)
	error('Error opening file.  Check to make sure it''s in the current directory')
end

NUM_SCANS = fread(fid,1,'integer*4')
NUM_RTDS  = fread(fid,1,'integer*4')

% pre-allocate matrices
x = zeros(NUM_CHANS,NUM_SPOTS,NUM_SCANS);
Tb = x;
rtdf = zeros(NUM_RTDS, NUM_SCANS);
t = zeros(1,NUM_SCANS);


%  slow, terrible loops, but mnemonic

% read uncalibrated radiances
for i = 1:NUM_SCANS
	for j = 1:NUM_SPOTS
		for k = 1:NUM_CHANS
			x(k,j,i) = fread(fid,1,'integer*2');
		end
	end
fprintf('Loaded record   %d of   %d\r', i, NUM_SCANS); 
end
fprintf('Finished loading uncalibrated radiances.\n');

% read calibrated radiances
for i = 1:NUM_SCANS
	for j = 1:NUM_SPOTS
		for k = 1:NUM_CHANS
			Tb(k,j,i) = fread(fid,1,'float');
		end
	end
fprintf('Loaded record   %d of   %d\r', i, NUM_SCANS); 
end
fprintf('Finished loading calibrated radiances.\n');

% read rtds
for i = 1:NUM_SCANS
	for j = 1:NUM_RTDS
		rtdf(j,i) = fread(fid,1,'float');
	end
fprintf('Loaded record   %d of   %d\r', i, NUM_SCANS); 
end
fprintf('Finished loading RTD readings.           \n');

% read time
for i = 1:NUM_SCANS
	t(i) = fread(fid,1,'integer*8');
	fprintf('Loaded record   %d of   %d\r', i, NUM_SCANS); 
end
fprintf('Finished loading timestamps.             \n');

fclose(fid);

if NAV   % read nav
	fid = fopen(nav_filename,'rb'); 
	if (fid < 0)
		error('Error opening nav file.  Check to make sure it''s in the current directory')
	end

	NUM_NAV_RECORDS = fread(fid,1,'integer*4');
        nav = zeros(NUM_NAV_PARAMS, NUM_NAV_RECORDS);
        navt = zeros(1, NUM_NAV_RECORDS);

	for i = 1:NUM_NAV_RECORDS
		for j = 1:NUM_NAV_PARAMS
			nav(j,i) = fread(fid,1,'float');
		end
	fprintf('Loaded record   %d of   %d\r', i, NUM_NAV_RECORDS); 
	end

	for i = 1:NUM_NAV_RECORDS
		navt(i) = fread(fid,1,'integer*8');
	end

	fclose(fid);
end
	
fprintf('Finished loading.                            \n');