function data = readaeri(filename,flag);

%
% function data = readaeri(filename,flag);
% 
% read in an aeri .cdf file produced with dmvtocdf and return some 
% fields in a structure
%
% Inputs:
%
% 1) If the filename is not defined in the function call or if the file
%    isn't found, the user is prompted for one.
%    If the filename has a .rnc (or .RNC) extension, a cdf file is produced by
%    spawning a dmvtocdf job.  The cdf file is then loaded in.
%
% 2) the value of flag determines what fields are returned:
%     = 1              :  all fields
%     = 2              :  mean_rad, dateYYMMDD, timeHHMMSS, wnum1, time_offset
%     = 3              :  #2 plus sceneMirrorAngle
%
%    to use flag w/o a filename, use i.e. aeri = readaeri([],2);
%
% Note: 1) field names used here are from the native AERI processing, not 
%          from the PNL QME processing.
%       2) no attributes are returned 
%

if nargin < 2;flag = 1;end

if ~isempty('filename') | ~(exist('filename') == 0)
  [filename,pathname]=uigetfile('*.*');
  filename = [pathname filename];
end

if ~isempty('filename') & lower(filename(length(filename)-3:length(filename))) == '.rnc'
  rnc_filename = filename;
  filename = [rnc_filename(1:length(rnc_filename)-4) '.cdf'];
  % something like :
  eval(['!dmvtocdf < ' rnc_filename ' 1 9999 ' filename])
end

if exist('flag') & flag == 1
  data = rd_netcdf2(filename);
end

if exist('flag') & flag == 2
  data = rd_netcdf2(filename,'mean_rad','dateYYMMDD','timeHHMMSS','wnum1','time_offset');
end

if exist('flag') & flag == 3
  data = rd_netcdf2(filename,'mean_rad','dateYYMMDD','timeHHMMSS','wnum1','time_offset','sceneMirrorAngle');
end

data.filename = filename;
data.index = (1:length(data.time_offset))';

% define various plotting parameters and fields
% (anything you define in here can be plotted with rncbrowser.m)
if data.channelNumber(1) == 1
  ind=find(data.wnum1 >= 0650 & data.wnum1 <= 0660);
  data.rad0650to0660 = mean(data.mean_rad(:,ind)')';data.bt0650to0660=radtot(655,data.rad0650to0660/1e3);
  ind=find(data.wnum1 >= 0870 & data.wnum1 <= 0872);
  data.rad0870to0872 = mean(data.mean_rad(:,ind)')';data.bt0870to0872=radtot(871,data.rad0870to0872/1e3);
  ind=find(data.wnum1 >= 1020 & data.wnum1 <= 1027);
  data.rad1020to1027 = mean(data.mean_rad(:,ind)')';data.bt1020to1027=radtot(1023,data.rad1020to1027/1e3);
  ind=find(data.wnum1 >= 1135 & data.wnum1 <= 1140);
  data.rad1135to1140 = mean(data.mean_rad(:,ind)')';data.bt1135to1140=radtot(1137,data.rad1135to1140/1e3);
  ind=find(data.wnum1 >= 1410 & data.wnum1 <= 1420);
  data.rad1410to1420 = mean(data.mean_rad(:,ind)')';data.bt1410to1420=radtot(1405,data.rad1410to1420/1e3);
elseif data.channelNumber(1) == 2
  ind=find(data.wnum1 >= 2290 & data.wnum1 <= 2295);
  data.rad2290to2295 = mean(data.mean_rad(:,ind)')';data.bt2290to2295=radtot(2292,data.rad2290to2295/1e3);
  ind=find(data.wnum1 >= 2405 & data.wnum1 <= 2410);
  data.rad2405to2410 = mean(data.mean_rad(:,ind)')';data.bt2405to2410=radtot(2407,data.rad2405to2410/1e3);
  ind=find(data.wnum1 >= 2505 & data.wnum1 <= 2510);
  data.rad2505to2510 = mean(data.mean_rad(:,ind)')';data.bt2505to2510=radtot(2507,data.rad2505to2510/1e3);
end
