function [power, range_axis, radius] = abdr_reader( file_name,disp_flag,varargin);

%file_name='/Users/ygim/dat/cassini/Abdrs/ta_run5_alt_stuff.abdr';

if exist('varargin','var');
    for i=1:2:length(varargin);
        eval([varargin{i} ' = eval( varargin{i+1} );']);
    end;
end;

%constant
sbdr_length=fix(1272);
lbdr_length=132344;
lable_length= fix(lbdr_length);
sync_code=hex2dec('77746B6A');

if exist('disp_flag','var');
    disp('open lbdr using little endian format');
end;
filetype='l';
fid=fopen(file_name,'r','l');

%move to the end of file
fseek(fid,0,'eof');
position=ftell(fid);
num_record= (position-lable_length)/lbdr_length;

%determine filetype
fseek(fid,lable_length,'bof');
sync_trial=fread(fid,1,'uint32');
if(sync_trial ~= sync_code)
    disp('file type mismatch');   fclose(fid);
    disp('opne sdbr using big endian format');
    fid=fopen(file_name,'r','b');
    filetype='b';
    fseek(fid,lable_length,'bof');
    sync_trial=fread(fid,1,'uint32');
    if(sync_trial ~= sync_code)
        disp('can not determine file type');
        return;
    end
end

%bring the pointer to the first record
fseek(fid,lbdr_length,'bof');
position=ftell(fid);
if(position ~=lbdr_length)
    disp('starting position error')
    position
end

%define vectors for placing data
sab_counter=zeros(1,num_record);
burst_id=zeros(1,num_record);
pri=zeros(1,num_record);
adc=zeros(1,num_record);
pul=zeros(1,num_record);
range_start=zeros(1,num_record);
range_resolution=zeros(1,num_record);

sc_x=zeros(1,num_record);
sc_y=zeros(1,num_record);
sc_z=zeros(1,num_record);
sc_pos=zeros(1,num_record);
amp=zeros(num_record,14*1000);
gain=zeros(1,num_record);
height=zeros(1,num_record);
height_std = zeros(1,num_record);

%footprint information
act_pol=zeros(1, num_record);
act_inc=zeros(1, num_record);
act_az=zeros(1, num_record);
act_cen_lon=zeros(1, num_record);
act_cen_lat=zeros(1, num_record);
act_maj_wid=zeros(1, num_record);
act_min_wid=zeros(1, num_record);
act_pt1_lon=zeros(1, num_record);
act_pt2_lon=zeros(1, num_record);
act_pt3_lon=zeros(1, num_record);
act_pt4_lon=zeros(1, num_record);
act_pt1_lat=zeros(1, num_record);
act_pt2_lat=zeros(1, num_record);
act_pt3_lat=zeros(1, num_record);
act_pt4_lat=zeros(1, num_record);

%radar backscatter information
sigma0_cor = zeros(1, num_record);
sigma0_uncor = zeros(1, num_record);
sigma0_std = zeros(1, num_record);

    %
% read sab counter, adc, pul, and pri from LBDR: readl all the records
%
for i=  1 : num_record
    %read sab counter
    %byte length to sab number: 64
    start_position=ftell(fid);
    fseek(fid, start_position+8,'bof');
    burst_id(i) = fread(fid,1,'uint32');%4byte
    fseek(fid, start_position+64,'bof');
    sab_counter(i)=fread(fid,1,'uint32');%4byte
    sab_counter(i);
    fseek(fid,start_position+144,'bof');% 144
    adc(i)=fread(fid,1,'float');
    fseek(fid,start_position+152,'bof');
    gain(i)=fread(fid,1,'float');
    fseek(fid,start_position+1144,'bof');% 208-144-4=64
    pul(i)=fread(fid,1,'uint32');
    fseek(fid,start_position+220,'bof');%220
    pri(i)=fread(fid,1,'float');
    fseek(fid,start_position+1244,'bof');%
    range_start(i)=fread(fid,1,'float');
    fseek(fid,start_position+1172,'bof');
    height(i)=fread(fid,1,'float');
    fseek(fid,start_position+1176,'bof');
    height_std(i)=fread(fid,1,'float');
    fseek(fid,start_position+1248,'bof');%
    range_resolution(i)=fread(fid,1,'float');
    fseek(fid,start_position+808,'bof');
    sc_x(i)=fread(fid,1,'double');
    sc_y(i)=fread(fid,1,'double');
    sc_z(i)=fread(fid,1,'double');
    sc_pos(i)=sqrt(sc_x(i).^2 + sc_y(i).^2 + sc_z(i).^2);
    %grab the surface footprint information
    fseek(fid,start_position+1184,'bof');
    act_pol(i) = fread(fid,1,'float');
    fseek(fid,start_position+1188,'bof');
    act_inc(i) = fread(fid,1,'float');
    fseek(fid,start_position+1192,'bof');
    act_az(i) = fread(fid,1,'float');
    fseek(fid,start_position+1196,'bof');
    act_cen_lon(i) = fread(fid,1,'float');
    fseek(fid,start_position+1200,'bof');
    act_cen_lat(i) = fread(fid,1,'float');
    fseek(fid,start_position+1204,'bof');
    act_maj_wid(i) = fread(fid,1,'float');
    fseek(fid,start_position+1208,'bof');
    act_min_wid(i) = fread(fid,1,'float');
    fseek(fid,start_position+1212,'bof');
    act_pt1_lon(i) = fread(fid,1,'float');
    fseek(fid,start_position+1216,'bof');
    act_pt2_lon(i) = fread(fid,1,'float');
    fseek(fid,start_position+1220,'bof');
    act_pt3_lon(i) = fread(fid,1,'float');
    fseek(fid,start_position+1224,'bof');
    act_pt4_lon(i) = fread(fid,1,'float');
    fseek(fid,start_position+1228,'bof');
    act_pt1_lat(i) = fread(fid,1,'float');
    fseek(fid,start_position+1232,'bof');
    act_pt2_lat(i) = fread(fid,1,'float');
    fseek(fid,start_position+1236,'bof');
    act_pt3_lat(i) = fread(fid,1,'float');
    fseek(fid,start_position+1240,'bof');
    act_pt4_lat(i) = fread(fid,1,'float');
    
    %grab passive radiometry information
    fseek(fid,start_position+1068,'bof');
    antenna_temp(i) = fread(fid,1,'float');
    fseek(fid,start_position+1072,'bof');
    receiver_temp(i) = fread(fid,1,'float');
    fseek(fid,start_position+1056,'bof');
    norm_cnt_radio(i) = fread(fid,1,'float');
    
    %radar backscatter information
    fseek(fid,start_position+1160,'bof');
    sigma0_uncor(i) = fread(fid,1,'float');
        fseek(fid,start_position+1164,'bof');
    sigma0_cor(i) = fread(fid,1,'float');
        fseek(fid,start_position+1168,'bof');
    sigma0_std(i) = fread(fid,1,'float');
    
    fseek(fid,start_position+lbdr_length,'bof');
end

%check whether there is a valid sab number for an input sab number
%record_id=find(sab_counter==input_sab_counter)

% read echo, recompute radius axis, and power
echo=zeros(num_record,14*1000);
range_axis=zeros(num_record,1000);
radius=zeros(num_record,1000);
for i=1: num_record

    fseek(fid,0,'bof');
    fseek(fid, i*lbdr_length,'bof');

    start_position=ftell(fid);
    fseek(fid, start_position+64,'bof');
    current_sab=fread(fid,1,'uint32');%4byte

    fseek(fid,start_position + sbdr_length,'bof');
    
    echo(i,:)=fread(fid,14*1000,'float');


    for k=1:1000
        range_axis(i,k)= range_start(i) + range_resolution(i)*(k-1);
        radius(i,k)= sc_pos(i)-range_axis(i,k);
    end
end

power=zeros(num_record,1000);
for i=1:num_record
    for k=1: 14
        power(i,:)= power(i,:) + echo(i,(k-1)*1000+1:k*1000).*echo(i,(k-1)*1000+1:k*1000)/gain(i)/pul(i);
    end
end

%calculate the along track distance from the centroid of the lat lon
%ellipses
along_track = zeros(1,num_record);
R_titan = 2575.5;
for i=2:num_record;
    dlat = ( act_cen_lat(i) - act_cen_lat(i-1) ).*pi/180;
    dlon = ( act_cen_lon(i) - act_cen_lon(i-1) ).*pi/180;
    atemp = sin(dlat./2).^2 + ...
        cos( act_cen_lat(i).*pi/180 ) .* cos( act_cen_lat(i-1).*pi/180) .* ...
        sin( dlon./2).^2;
    ctemp = 2.*asin( min([1, sqrt(atemp)] ) );
    along_track(i) = along_track(i-1)+R_titan.*ctemp;
end;

%save a mat file with the read information
ext_pos = strfind(file_name,'.abdr');
if length(ext_pos) == 0;
    ext_pos = strfind(file_name,'.TAB');
end;
if length(ext_pos) == 0;
    ext_pos = strfind(file_name,'.lbdr');
end;
file_out = [file_name(1:(ext_pos-1)) '_info.mat'];
save(file_out);


