function [runlog,spectrum,bin2asc_info] = read_bin2asc_spectrum(filename)

% clear
% [runlog,spectrum,bin2asc_info] = read_bin2asc_spectrum(filename)
%
% Example: filename = '/net/lamont/raid1/spectra/jpl_ifs3/asc_jc20080114sfddaa.008';
%
% runlog = 
%     Spectrum_File_Name: {'jc20080114sfddaa.008'}
%                   Year: 8
%                    Day: 14
%                   Hour: 17.957699999999999
%                  oblat: 34.201999999999998
%                  oblon: -1.181750000000000e+02
%                  obalt: 0.390000000000000
%                   ASZA: 62.893000000000001
%                   POFF: 0
%                    OPD: 45
%                   FOVI: 0.002400000000000
%                   FOVO: 0.002400000000000
%                   AMAL: 0
%                 IFIRST: 530991
%                  ILAST: 1460226
%               DELTA_NU: 0.007533082620000
%                POINTER: 107896
%                    BPW: -4
%                   ZOFF: 0
%                    SNR: 1000
%                    APF: {'X'}
%                   tins: 22.600000000000001
%                   pins: 2.690000000000000
%                   hins: 99.900000000000006
%                   tout: 20.899999999999999
%                   pout: 9.787000000000000e+02
%                   hout: 20.500000000000000
%                   lasf: 1.579801400000000e+04
%                 wavtkr: 9900
%                    sia: 4.153000000000000e+02
%                    sis: 8.100000000000000
%                   aipl: 0.002000000000000
% spectrum = 
%     Frequency: [929236x1 double]
%        Signal: [929236x1 double]

% filename = '/net/lamont/raid3/spectra/lamont_ifs3/asc_oc20080722sfddaa.001';
% filename = '/net/nicolet/raid1/spectra/darwin_ifs2/asc_db20051013s0ccaa.001';
% filename = '/net/ccycle/spectra1/tccon/lamont_ifs3/ascii/asc_oc20080722sfddaa.277';

fid = fopen(filename,'r');
n=str2num(fgetl(fid));
nhead = n(1);
ncol = n(2);

if nhead>4
bin2asc_info = fgetl(fid);
headers = textscan(fgetl(fid),'%s');
headers = headers{1};
else
  bin2asc_info = '';
  headers = textscan(fgetl(fid),'%s');
  headers = headers{1};
end

form = '';

for i=1:length(headers)
    if strcmp(headers{i},'APF')||strcmp(headers{i},'Spectrum_File_Name')
        form = [form '%s'];
    else
        form = [form '%f'];
    end
end

Q = textscan(fgetl(fid),form);
runlog = cell2struct(Q(1:length(headers)),headers,2);

spec_cols = textscan(fgetl(fid),'%s');
spec_cols = spec_cols{1};
spec_cols{1} = 'Frequency';

A = fscanf(fid,'%f%f',[2,inf])';

spectrum.(spec_cols{1}) = A(:,1);
spectrum.(spec_cols{2}) = A(:,2);

% A = textscan(fid,'%f%f');
% spectrum = cell2struct(A,spec_cols,2);
fclose(fid);

% A = importdata(filename,' ',nhead);
% spectrum = struct('Frequency',A.data(:,1),'Signal',A.data(:,2));



