
%!rcp arm1:/home3/waynef/public_html/camexiii/archive/aeri/retrievals/*AG.cdf . 

[s,w] = unix('ls -1 *AG.cdf');
no_files = length(w)/13;
w=reshape(w,13,length(w)/13)';
w=w(:,1:12);

NSS1970 = [];
ALT = [];
P1 = [];
H1 = [];
T1 = [];
CBH = [];
TPW = [];
IMRT = [];

fid=fopen('aerigoes_values.asc','w');

fprintf(fid,' \n');
fprintf(fid,' AERI/GOES parameters for CAMEX3\n');
fprintf(fid,' from Andros Island (lon=-77.7687,lat=24.6998)\n');
fprintf(fid,' \n');
fprintf(fid,'  column 1: time (# seconds since 1970) \n');
fprintf(fid,'  column 2: readable date and time (UTC) \n');
fprintf(fid,'  column 3: altitude (meters above sea level) \n');
fprintf(fid,'  column 4: first level pressure (mbar) \n');
fprintf(fid,'  column 5: first level height (km) \n');
fprintf(fid,'  column 6: first level temperature (K) \n');
fprintf(fid,'  column 7: cloud base height (km, <0 = no cloud)\n');
fprintf(fid,'  column 8: pwv (cm) \n');
fprintf(fid,'  column 9: integrated mean radiating temperature (K) \n');
fprintf(fid,' \n');

for i = 1:no_files

  fname = w(i,:);

  [s,w2] = unix(['wc -l ' fname]);
  ind = findstr(w2,fname);
  eval(['filelength = ' w2(1:ind-1) ';'])
  if filelength ~= 0

    ret = rd_netcdf(fname);  

    for j = 1:length(ret.time_offset)
    
      btime = ret.base_time+ret.time_offset(j);
      time = datestr(dn(ret.base_time+ret.time_offset(j)));
      nsurftemp = ret.ambientTemp(j,1);
      nsurfpres = ret.pressure(j,1);
      nsurfheight = ret.height(1);
      cbh = ret.cloudBaseHeight(j);
      lat = ret.lat;
      lon = ret.lon;
      alt = ret.alt;
      ind=find(ret.pressure(j,:) >= 0);

      if ~isempty(ind)

      tpw = -pwv(ret.pressure(j,ind),ret.WaterVaporMixingRatio(j,ind));
      
      arg1=integral(ret.height(ind)',ret.pressure(j,ind)./ret.ambientTemp(j,ind));
      arg2=integral(ret.height(ind)',ret.pressure(j,ind)./(ret.ambientTemp(j,ind).^2));
      imt = arg1/arg2;
    
      fprintf(fid,' %10i %s %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n',...
	    btime,time,ret.alt,nsurfpres,nsurfheight,nsurftemp,cbh,tpw,imt);
%      !tail -1 aerigoes_values.asc


      NSS1970 = [NSS1970 ret.base_time+ret.time_offset(j)];
      ALT = [ALT ret.alt];
      P1 = [P1 nsurfpres];
      H1 = [H1 nsurfheight];
      T1 = [T1 nsurftemp];
      CBH = [CBH cbh];
      TPW = [TPW tpw];
      IMRT = [IMRT imt];

      end

    end
  end
end

fclose(fid);
     
ag.nss1970 = NSS1970;
ag.pwv = TPW;
ag.alt = ALT;
ag.p1 = P1;
ag.h1 = H1;
ag.t1 = T1;
ag.cbh = CBH;
ag.imrt = IMRT;

save aerigoes_values ag


