
fid = fopen('airsheights_upper.param','w');
string = 'c from KCARTADATA/NLTE/UA';
fprintf(fid,'%s \n',string);
string = 'c 100 layer heights for the DEFINITION of the kCARTA database';
fprintf(fid,'%s \n',string);
string = 'c this is for the upper atm';
fprintf(fid,'%s \n',string);

string = '       REAL DatabaseHeight(kMaxLayer)';
fprintf(fid,'%s \n',string);
string = '       INTEGER IPHEIGHT';
fprintf(fid,'%s \n',string);

string = 'c note program expects heights in km, so these numbers will have';
fprintf(fid,'%s \n',string);
string = 'c to be read in and divided by 1000.0';
fprintf(fid,'%s \n',string);


string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = 'C      Load Pheight with AIRS layer hts US Standard Profile (in m)';
fprintf(fid,'%s \n',string);
string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = '       DATA  (DatabaseHEIGHT(IPHEIGHT), IPHEIGHT = 1,100,1 )';
fprintf(fid,'%s \n',string);
fprintf(fid,'     $ / \n');
for jj = 1 : 24
  ind = (1:4) + (jj-1)*4;
  data = raHavg(ind)*1000;
  fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e, \n',data); 
  end
jj = 25;
ind = (1:4) + (jj-1)*4;
data = raHavg(ind)*1000;
fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e \n',data); 
fprintf(fid,'     $ / \n');

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fid = fopen('airslevelheights_upper.param','w');
string = 'c from KCARTADATA/NLTE/UA';
fprintf(fid,'%s \n',string);
string = 'c the heights of the pressure levels for the kCARTA DATABASE';
fprintf(fid,'%s \n',string);
string = 'c this is for the upper atm';
fprintf(fid,'%s \n',string);

string = '       REAL DATABASELEVHEIGHTS(101)';
fprintf(fid,'%s \n',string);

string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = 'C PLEV with the AIRS layer boundary pressure heights (in km)';
fprintf(fid,'%s \n',string);
string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = '       DATA  (DATABASELEVHEIGHTS(IPLEV), IPLEV = 101, 1, -1 )';
fprintf(fid,'%s \n',string);
fprintf(fid,'     $ / \n');

hnewFLIP = fliplr(hnew);
for jj = 1 : 25
  ind = (1:4) + (jj-1)*4;
  ind = ind;
  data = hnewFLIP(ind);
  fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e, \n',data); 
  end
ind = 101;
data = hnewFLIP(ind);
fprintf(fid,'     $ %12.8e \n',data); 

fprintf(fid,'     $ / \n');

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fid = fopen('airslevels_upper.param','w');
string = 'c from KCARTADATA/NLTE/UA';
fprintf(fid,'%s \n',string);
string = 'c 101 pressure levels for the DEFINITION of the kCARTA database';
fprintf(fid,'%s \n',string);
string = 'c this is for the upper atm';
fprintf(fid,'%s \n',string);

string = '       REAL DATABASELEV(101)';
fprintf(fid,'%s \n',string);
string = '       INTEGER IPLEV';
fprintf(fid,'%s \n',string);

string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = 'C PLEV with the AIRS layer boundary pressure levels (in mb)';
fprintf(fid,'%s \n',string);
string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = '       DATA  (DATABASELEV(IPLEV), IPLEV = 101, 1, -1 )';
fprintf(fid,'%s \n',string);
fprintf(fid,'     $ / \n');

PlevUpFLIP = fliplr(PlevUp);
for jj = 1 : 25
  ind = (1:4) + (jj-1)*4;
  ind = ind;
  data = PlevUpFLIP(ind);
  fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e, \n',data); 
  end
ind = 101;
data = PlevUpFLIP(ind);
fprintf(fid,'     $ %12.8e \n',data); 

fprintf(fid,'     $ / \n');

fclose(fid);

%%% save the pressure (mb) VS height (km) for the UPPER levels 0.05 - 2.5e-5 mb
fid = fopen('cbplev_airs_ua_nlte.dat','w');
array = [raHavg;raPavg];
%array = array(:,5:length(array));
fprintf(fid,' %12.8e  %12.8e \n',array);  
fclose(fid); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameterize hgt vs press
yy = hnew * 1000; xx = log(PlevUp);
[P,S] = polyfit(xx,yy,3);
Y = polyval(P,xx);
plot(xx,yy,xx,Y,'r')
P

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the new klayers from 0-83 km, but in 120 pressure levels
fid = fopen('cbplev_airs_nlte.f','w');
string = 'c from KCARTADATA/NLTE/UA';
fprintf(fid,'%s \n',string);
string = 'c 120 pressure levels for using kLAYERS from 0-83 km';
fprintf(fid,'%s \n',string);
string = 'c but with layers <= 1.5 km thick everywhere';
fprintf(fid,'%s \n',string);

string = '       REAL PLEV(120)';
fprintf(fid,'%s \n',string);
string = '       INTEGER I';
fprintf(fid,'%s \n',string);

string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = 'C PLEV with the AIRS layer boundary pressure levels (in mb)';
fprintf(fid,'%s \n',string);
string = 'C      ------------------------------------------------------------';
fprintf(fid,'%s \n',string);
string = '       DATA  (PLEV(I), I = 120, 1, -1 )';
fprintf(fid,'%s \n',string);
fprintf(fid,'     $ / \n');

PlevUpFLIP = fliplr(klayers_Plev);
for jj = 1 : 29
  ind = (1:4) + (jj-1)*4;
  data = PlevUpFLIP(ind);
  fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e, \n',data); 
  end

jj = 30;
ind = (1:4) + (jj-1)*4;
data = PlevUpFLIP(ind);
fprintf(fid,'     $ %12.8e,  %12.8e, %12.8e, %12.8e \n',data); 

fprintf(fid,'     $ / \n');

string = '      END';
fprintf(fid,'%s \n',string);

fclose(fid);

%%% save the pressure (mb) VS height (km)  for the 120 levels for kLayers
%%% 1100 - 0.005 mb in 120 levels
fid = fopen('cbplev_airs_nlte.dat','w');
array = [heights_Plev; klayers_Plev];
array = array(:,5:length(array));
fprintf(fid,' %12.8e  %12.8e \n',array);  
fclose(fid); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
