function [heado, profo] = rtp_lay_to_lev(headi, profi)

% function [heado, profo] = rtp_lay_to_lev(headi, profi)
%
% Takes an AIRS "layer" (head.ptype=1) or "pseudo-level" (head.ptype=2)
% RTP structure and converts it to an approximate "level" (head.ptype=0)
% RTP structure.  Only the following fields are modified:
%    head.ptype => 0 {level}
%    head.gunit => 10 for all gases {ppmv}
%    head.plevs => level pressures
%    head.plays => -9999 {no data} [only reset if this field exists]
%    head.palts => -9999 {no data} [only reset if this field exists]
%    prof.ptemp => level temperature profile
%    prof.gas_# => level gas mixing ratio profile
%
% Input:
%    headi - {structure} RTP "head" structure
%    profi - {structure} RTP "prof" structure
%
% Output:
%    heado - {structure}  RTP "head" structure
%    profo - {structure}  RTP "prof" structure
%

% Created: S.Hannon, 21 April 2004
% Update: S.Hannon, 05 Oct 2004 - fix conversion error in bottom level gas
%    amounts for ptype=2 (pseudo-levels).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
loschmidt=2.6867775E+19;
kavagadro=6.022142E+26;
tzero=273.15;

% Dry air mixing ratio of CO2
% Note: the CO2 mixing ratio actually drops slightly in the top few layers
mrfdry=370/1E+6;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Required reference profile info
% fixed ref is CO2 amount in kilomoles/cm^2 based on 370 ppmv mixing ratio
%lay# P lay (atm)  P bot (mb)   P top (mb)   delta z (m)   fixed ref     T ref (K)     fx
%---  -----------  -----------  -----------  -----------  -----------  -----------  -----------
x=[1  9.36810E-06  1.61000E-02  5.00000E-03  6.90990E+03  1.46730E-10  1.98164E+02  1.00082E+00
   2  2.53190E-05  3.84000E-02  1.61000E-02  5.44616E+03  2.97060E-10  2.09992E+02  1.00083E+00
   3  5.47150E-05  7.69000E-02  3.84000E-02  4.58282E+03  5.12350E-10  2.21323E+02  1.00025E+00
   4  1.02710E-04  1.37000E-01  7.69000E-02  4.00569E+03  7.98750E-10  2.32761E+02  1.00021E+00
   5  1.74800E-04  2.24400E-01  1.37000E-01  3.57197E+03  1.16020E-09  2.43039E+02  1.00020E+00
   6  2.76900E-04  3.45400E-01  2.24400E-01  3.23887E+03  1.60460E-09  2.52322E+02  1.00023E+00
   7  4.15280E-04  5.06400E-01  3.45400E-01  2.96484E+03  2.13300E-09  2.60488E+02  1.00022E+00
   8  5.96370E-04  7.14000E-01  5.06400E-01  2.72102E+03  2.74790E-09  2.66412E+02  1.00018E+00
   9  8.26910E-04  9.75300E-01  7.14000E-01  2.50605E+03  3.45600E-09  2.70398E+02  1.00019E+00
  10  1.11390E-03  1.29720E+00  9.75300E-01  2.28306E+03  4.25430E-09  2.69425E+02  1.00015E+00
  11  1.46430E-03  1.68720E+00  1.29720E+00  2.06221E+03  5.15080E-09  2.64210E+02  1.00012E+00
  12  1.88550E-03  2.15260E+00  1.68720E+00  1.87117E+03  6.14290E-09  2.58856E+02  1.00012E+00
  13  2.38480E-03  2.70090E+00  2.15260E+00  1.70876E+03  7.23310E-09  2.53937E+02  1.00012E+00
  14  2.96970E-03  3.33980E+00  2.70090E+00  1.56955E+03  8.42400E-09  2.49408E+02  1.00012E+00
  15  3.64780E-03  4.07700E+00  3.33980E+00  1.44890E+03  9.71550E-09  2.45222E+02  1.00012E+00
  16  4.42680E-03  4.92040E+00  4.07700E+00  1.34368E+03  1.11100E-08  2.41341E+02  1.00010E+00
  17  5.31440E-03  5.87760E+00  4.92040E+00  1.25085E+03  1.26040E-08  2.37750E+02  1.00011E+00
  18  6.31830E-03  6.95670E+00  5.87760E+00  1.16872E+03  1.42040E-08  2.34361E+02  1.00013E+00
  19  7.44630E-03  8.16550E+00  6.95670E+00  1.09531E+03  1.59060E-08  2.31164E+02  1.00016E+00
  20  8.70620E-03  9.51190E+00  8.16550E+00  1.03390E+03  1.77100E-08  2.29150E+02  1.00013E+00
  21  1.01060E-02  1.10038E+01  9.51190E+00  9.81059E+02  1.96180E-08  2.27852E+02  1.00015E+00
  22  1.16530E-02  1.26492E+01  1.10038E+01  9.33043E+02  2.16300E-08  2.26636E+02  1.00015E+00
  23  1.33550E-02  1.44559E+01  1.26492E+01  8.89873E+02  2.37440E-08  2.25679E+02  1.00017E+00
  24  1.52210E-02  1.64318E+01  1.44559E+01  8.50406E+02  2.59610E-08  2.24809E+02  1.00018E+00
  25  1.72580E-02  1.85847E+01  1.64318E+01  8.14016E+02  2.82790E-08  2.23980E+02  1.00017E+00
  26  1.94720E-02  2.09224E+01  1.85847E+01  7.80465E+02  3.06980E-08  2.23213E+02  1.00015E+00
  27  2.18740E-02  2.34526E+01  2.09224E+01  7.49355E+02  3.32180E-08  2.22479E+02  1.00015E+00
  28  2.44680E-02  2.61829E+01  2.34526E+01  7.20395E+02  3.58370E-08  2.21771E+02  1.00015E+00
  29  2.72650E-02  2.91210E+01  2.61829E+01  6.93373E+02  3.85560E-08  2.21073E+02  1.00015E+00
  30  3.02700E-02  3.22744E+01  2.91210E+01  6.68098E+02  4.13730E-08  2.20393E+02  1.00015E+00
  31  3.34910E-02  3.56504E+01  3.22744E+01  6.44428E+02  4.42850E-08  2.19737E+02  1.00013E+00
  32  3.69350E-02  3.92566E+01  3.56504E+01  6.22217E+02  4.72930E-08  2.19103E+02  1.00014E+00
  33  4.06100E-02  4.31001E+01  3.92566E+01  6.01361E+02  5.03970E-08  2.18491E+02  1.00014E+00
  34  4.45230E-02  4.71882E+01  4.31001E+01  5.81729E+02  5.35940E-08  2.17900E+02  1.00013E+00
  35  4.86810E-02  5.15278E+01  4.71882E+01  5.63260E+02  5.68810E-08  2.17355E+02  1.00013E+00
  36  5.30910E-02  5.61259E+01  5.15278E+01  5.45924E+02  6.02600E-08  2.16864E+02  1.00013E+00
  37  5.77590E-02  6.09895E+01  5.61259E+01  5.30256E+02  6.37260E-08  2.16700E+02  1.00012E+00
  38  6.26920E-02  6.61252E+01  6.09895E+01  5.15795E+02  6.72830E-08  2.16700E+02  1.00012E+00
  39  6.78970E-02  7.15398E+01  6.61252E+01  5.02014E+02  7.09230E-08  2.16700E+02  1.00012E+00
  40  7.33810E-02  7.72395E+01  7.15398E+01  4.88898E+02  7.46480E-08  2.16700E+02  1.00011E+00
  41  7.91490E-02  8.32310E+01  7.72395E+01  4.76389E+02  7.84560E-08  2.16700E+02  1.00011E+00
  42  8.52090E-02  8.95203E+01  8.32310E+01  4.64449E+02  8.23450E-08  2.16700E+02  1.00010E+00
  43  9.15650E-02  9.61138E+01  8.95203E+01  4.53037E+02  8.63140E-08  2.16700E+02  1.00011E+00
  44  9.82240E-02  1.03017E+02  9.61138E+01  4.42115E+02  9.03590E-08  2.16700E+02  1.00010E+00
  45  1.05190E-01  1.10237E+02  1.03017E+02  4.31666E+02  9.44820E-08  2.16700E+02  1.00009E+00
  46  1.12480E-01  1.17777E+02  1.10237E+02  4.21639E+02  9.86760E-08  2.16700E+02  1.00008E+00
  47  1.20080E-01  1.25646E+02  1.17777E+02  4.12023E+02  1.02940E-07  2.16700E+02  1.00004E+00
  48  1.28010E-01  1.33846E+02  1.25646E+02  4.02786E+02  1.07280E-07  2.16700E+02  1.00007E+00
  49  1.36270E-01  1.42385E+02  1.33846E+02  3.93919E+02  1.11690E-07  2.16700E+02  1.00009E+00
  50  1.44860E-01  1.51266E+02  1.42385E+02  3.85385E+02  1.16160E-07  2.16700E+02  1.00006E+00
  51  1.53800E-01  1.60496E+02  1.51266E+02  3.77163E+02  1.20700E-07  2.16700E+02  1.00010E+00
  52  1.63080E-01  1.70078E+02  1.60496E+02  3.69259E+02  1.25300E-07  2.16700E+02  1.00008E+00
  53  1.72710E-01  1.80018E+02  1.70078E+02  3.61628E+02  1.29960E-07  2.16700E+02  1.00010E+00
  54  1.82700E-01  1.90320E+02  1.80018E+02  3.54272E+02  1.34680E-07  2.16700E+02  1.00010E+00
  55  1.93050E-01  2.00989E+02  1.90320E+02  3.47182E+02  1.39450E-07  2.16707E+02  1.00007E+00
  56  2.03760E-01  2.12028E+02  2.00989E+02  3.40371E+02  1.44280E-07  2.16740E+02  1.00007E+00
  57  2.14840E-01  2.23441E+02  2.12028E+02  3.33796E+02  1.49160E-07  2.16773E+02  1.00006E+00
  58  2.26290E-01  2.35234E+02  2.23441E+02  3.28204E+02  1.54090E-07  2.17325E+02  1.00006E+00
  59  2.38110E-01  2.47409E+02  2.35234E+02  3.25005E+02  1.59080E-07  2.19365E+02  1.00012E+00
  60  2.50320E-01  2.59969E+02  2.47409E+02  3.21981E+02  1.64100E-07  2.21464E+02  1.00008E+00
  61  2.62910E-01  2.72919E+02  2.59969E+02  3.18986E+02  1.69170E-07  2.23529E+02  1.00008E+00
  62  2.75880E-01  2.86262E+02  2.72919E+02  3.16041E+02  1.74290E-07  2.25577E+02  1.00013E+00
  63  2.89240E-01  3.00000E+02  2.86262E+02  3.13125E+02  1.79440E-07  2.27590E+02  1.00012E+00
  64  3.03000E-01  3.14137E+02  3.00000E+02  3.10238E+02  1.84630E-07  2.29576E+02  1.00013E+00
  65  3.17150E-01  3.28675E+02  3.14137E+02  3.07468E+02  1.89860E-07  2.31590E+02  1.00016E+00
  66  3.31700E-01  3.43618E+02  3.28675E+02  3.04729E+02  1.95120E-07  2.33583E+02  1.00018E+00
  67  3.46640E-01  3.58966E+02  3.43618E+02  3.02016E+02  2.00410E-07  2.35543E+02  1.00018E+00
  68  3.61990E-01  3.74724E+02  3.58966E+02  2.99358E+02  2.05740E-07  2.37501E+02  1.00024E+00
  69  3.77750E-01  3.90892E+02  3.74724E+02  2.96741E+02  2.11090E-07  2.39444E+02  1.00026E+00
  70  3.93910E-01  4.07474E+02  3.90892E+02  2.94139E+02  2.16460E-07  2.41355E+02  1.00027E+00
  71  4.10480E-01  4.24470E+02  4.07474E+02  2.91581E+02  2.21860E-07  2.43251E+02  1.00030E+00
  72  4.27450E-01  4.41882E+02  4.24470E+02  2.89081E+02  2.27280E-07  2.45151E+02  1.00034E+00
  73  4.44840E-01  4.59712E+02  4.41882E+02  2.86600E+02  2.32720E-07  2.47021E+02  1.00037E+00
  74  4.62650E-01  4.77961E+02  4.59712E+02  2.84139E+02  2.38180E-07  2.48864E+02  1.00043E+00
  75  4.80860E-01  4.96630E+02  4.77961E+02  2.81732E+02  2.43650E-07  2.50711E+02  1.00046E+00
  76  4.99500E-01  5.15720E+02  4.96630E+02  2.79354E+02  2.49140E-07  2.52540E+02  1.00053E+00
  77  5.18550E-01  5.35232E+02  5.15720E+02  2.76991E+02  2.54640E-07  2.54341E+02  1.00059E+00
  78  5.38010E-01  5.55167E+02  5.35232E+02  2.74657E+02  2.60140E-07  2.56126E+02  1.00062E+00
  79  5.57890E-01  5.75525E+02  5.55167E+02  2.72379E+02  2.65660E-07  2.57915E+02  1.00070E+00
  80  5.78190E-01  5.96306E+02  5.75525E+02  2.70115E+02  2.71190E-07  2.59679E+02  1.00080E+00
  81  5.98910E-01  6.17511E+02  5.96306E+02  2.67865E+02  2.76710E-07  2.61416E+02  1.00087E+00
  82  6.20050E-01  6.39140E+02  6.17511E+02  2.65653E+02  2.82240E-07  2.63150E+02  1.00097E+00
  83  6.41600E-01  6.61192E+02  6.39140E+02  2.63483E+02  2.87780E-07  2.64877E+02  1.00109E+00
  84  6.63570E-01  6.83667E+02  6.61192E+02  2.61319E+02  2.93300E-07  2.66580E+02  1.00118E+00
  85  6.85960E-01  7.06565E+02  6.83667E+02  2.59172E+02  2.98830E-07  2.68258E+02  1.00131E+00
  86  7.08770E-01  7.29886E+02  7.06565E+02  2.57072E+02  3.04350E-07  2.69940E+02  1.00144E+00
  87  7.31990E-01  7.53627E+02  7.29886E+02  2.54998E+02  3.09870E-07  2.71609E+02  1.00159E+00
  88  7.55630E-01  7.77790E+02  7.53627E+02  2.52931E+02  3.15380E-07  2.73254E+02  1.00176E+00
  89  7.79690E-01  8.02371E+02  7.77790E+02  2.50882E+02  3.20870E-07  2.74878E+02  1.00189E+00
  90  8.04150E-01  8.27371E+02  8.02371E+02  2.48867E+02  3.26350E-07  2.76506E+02  1.00205E+00
  91  8.29030E-01  8.52788E+02  8.27371E+02  2.46870E+02  3.31810E-07  2.78119E+02  1.00220E+00
  92  8.54320E-01  8.78620E+02  8.52788E+02  2.44883E+02  3.37250E-07  2.79711E+02  1.00235E+00
  93  8.80020E-01  9.04866E+02  8.78620E+02  2.42911E+02  3.42670E-07  2.81282E+02  1.00249E+00
  94  9.06120E-01  9.31523E+02  9.04866E+02  2.40975E+02  3.48070E-07  2.82858E+02  1.00264E+00
  95  9.32640E-01  9.58591E+02  9.31523E+02  2.39060E+02  3.53450E-07  2.84425E+02  1.00281E+00
  96  9.59550E-01  9.86066E+02  9.58591E+02  2.37157E+02  3.58810E-07  2.85971E+02  1.00299E+00
  97  9.86870E-01  1.01395E+03  9.86066E+02  2.35262E+02  3.64130E-07  2.87497E+02  1.00314E+00
  98  1.01460E+00  1.04223E+03  1.01395E+03  2.33377E+02  3.69420E-07  2.89002E+02  1.00329E+00
  99  1.04270E+00  1.07092E+03  1.04223E+03  2.31502E+02  3.74690E-07  2.90487E+02  1.00347E+00
 100  1.07120E+00  1.10000E+03  1.07092E+03  2.29637E+02  3.79910E-07  2.91953E+02  1.00360E+00];

% Pull out necessary reference profile data
[nlev,npro]=size(profi.gas_1);
if (headi.ptype == 1)
   nlay=max(profi.nlevs) - 1;
else
   nlay=max(profi.nlevs);
end
ii=1:nlay;
patm =x(ii,2)*ones(1,npro);
pbot =x(ii,3);
ptop =x(ii,4);
dzref=x(ii,5)*ones(1,npro);
fref =x(ii,6)*ones(1,npro) * kavagadro;  % convert to molecules/cm^2
tref =x(ii,7)*ones(1,npro);
fx   =x(ii,8);
clear x

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

addpath /asl/matlab/rtptools % for pfields2bits


% Check input RTP head
%
if (~ isfield(headi,'ptype') )
    error('input RTP is missing head.ptype')
end
if (headi.ptype ~= 1 & headi.ptype ~= 2)
   error('Can only convert if head.ptype=1 or 2')
end
%
if (~ isfield(headi,'pfields') )
    error('input RTP is missing head.pfields')
end
[profbit, ircalbit, irobsbit, mwcalbit, mwobsbit]=pfields2bits(headi.pfields);
if (profbit == 0)
   error('Can only convert if input RTP contains profile info')
end
%
if (~ isfield(headi,'glist') )
    error('input RTP is missing head.glist')
end
ih2o=find(headi.glist == 1);
if (length(ih2o) ~= 1)
   error('Can only convert if input RTP contains water')
end
%
if (~ isfield(headi,'gunit') )
    error('input RTP is missing head.gunit')
end
ibad=setdiff(unique(headi.gunit), [1 2]);
if (length(ibad) > 0)
   error('Can only convert if gunit=1 or 2 for all gases')
end


% Check input RTP prof
if (~ isfield(profi,'nlevs') )
    error('input RTP is missing prof.nlevs')
end
if (~ isfield(profi,'plat') )
    error('input RTP is missing prof.plat')
end
if (~ isfield(profi,'ptemp') )
    error('input RTP is missing prof.ptemp')
end
if (~ isfield(profi,'spres') )
    error('input RTP is missing prof.spres')
end
if (~ isfield(profi,'gas_1') )
    error('input RTP is missing prof.gas_1')
end


% Create output structures
heado=headi;
heado.ptype=0;
heado.gunit(:)=10;
%
profo=profi;
% Make dummy prof.plevs if it does not already exist
if (~ isfield(profi,'plevs') )
   profo.plevs=zeros(nlev,npro);
end
profo.plevs(ii,:)=patm*1013.25;


% Calc fmult
% This section based on "calf0.m"

pmult=0.58;

% Calc the fixed gases gravity correction factor
gscal=( 9.78050518 + 0.0518017*( cos((profi.plat - 90.0)*pi/180) ).^2 )/9.80683613;

if (headi.gunit(ih2o) == 1)
   wpro=profi.gas_1(ii,:);
else
   wpro=kavagadro * profi.gas_1(ii,:);
end
%
tpro=profi.ptemp(ii,:);
if (headi.ptype == 2)
   % convert pseudo-level T to layer mean T
   % note: bottom layer mean T is for the full layer 
   tpro(2:nlay,:) = 0.5*( profi.ptemp(1:(nlay-1),:) + profi.ptemp(2:nlay,:) );
   profo.ptemp(ii,:)=tpro;
else
   profo.nlevs=profi.nlevs - 1;
end

% Calc the fixed gases correction term for this layer
pwater = wpro .* tpro ./ (loschmidt*tzero*100 * dzref);
fmult = ( 1 - (pmult*pwater)./patm ) ./ ...
        ( (fx*ones(1,npro)) .* (ones(nlay,1)*gscal) );

% Calc approximate layer thickness
%%
% how did i get this equation come in "mr_from_amount.txt""
%dzcal = ( fmult.*fref + mrfdry*wpro ) ./ ( fref.*tref./(tpro.*dzref) );
%%%
dzcal = ( fmult.*fref + mrfdry*wpro ) ./ ...
   (mrfdry * loschmidt*tzero*100 * patm./tpro);

% Calc approximate water vapor partial pressure (in atm)
ppwcal= wpro .* tpro ./ (loschmidt*tzero*100 * dzcal);

% Dry air pressure (in atm)
pdry=patm - ppwcal;

% Calculate water mixing ratio (in ppmv)
xfac=1E+6 ./ pdry;
profo.gas_1(ii,:) = xfac .* ppwcal;


% Convert all other gases
for ig=1:headi.ngas
   id=headi.glist(ig);
   idstr=int2str(id);
   if (id ~= 1)
      if (headi.gunit(ig) == 1)
         eval(['wpro=profi.gas_' idstr '(ii,:);'])
      else
         eval(['wpro=kavagadro * profi.gas_' idstr '(ii,:);'])
      end
      eval(['profo.gas_' idstr '(ii,:) = xfac .* tpro .* wpro ./ (loschmidt*tzero*100 * dzcal);'])
   end
end


%%% end of function %%%
