Index: /trunk/LMDZ.MARS/README
===================================================================
--- /trunk/LMDZ.MARS/README	(revision 828)
+++ /trunk/LMDZ.MARS/README	(revision 829)
@@ -1815,2 +1815,9 @@
    If ndynstep is not specified or is negative, nday is used. Otherwise nday value is discarded.
    The problem with nday alone is that one can only run the GCM for a decimal fraction of one sol.
+
+== 06/11/2012 == EM
+>> update of zrecast utility: now the planetary constants (radius, gravity,
+   reduced gas constant) are read from the "controle" array in the file and
+   not hard coded. That way zrecast can also be used for the generic model.
+   Note that the "distance to center of Mars" and "MCD" options still contain
+   some hard coded Mars constants.
Index: /trunk/LMDZ.MARS/util/zrecast.F90
===================================================================
--- /trunk/LMDZ.MARS/util/zrecast.F90	(revision 828)
+++ /trunk/LMDZ.MARS/util/zrecast.F90	(revision 829)
@@ -1,3 +1,9 @@
-
+module planet_const
+! planetary constants (set via init_planet_const routine)
+implicit none
+real :: a0 ! Mean planetary radius (a0=3396.E3 for Mars)
+real :: g0 ! gravity at a0  (Mars: g0=3.7257964 ; Lemoine et al. 2001)
+real :: Rmean ! reduced gaz constant (Mars: R=191)
+end module planet_const
 
 program zrecast
@@ -51,4 +57,6 @@
 ! EM 03/2011 : Add possibility to have outputs as distance to center
 !              of planet
+! EM 11/2012 : Adapted so it can be used on "generic" model outputs; planet
+!              constants (radius, R, etc.) are now read from file
 implicit none
 
@@ -154,4 +162,8 @@
 endif
 
+! load planet constants (radius, gravity, ...)
+
+call init_planet_const(infid)
+ 
 !===============================================================================
 ! 1.2 Get # and names of variables in input file
@@ -1743,4 +1755,57 @@
 end program
 
+!===============================================================================
+
+subroutine init_planet_const(infid)
+! initialize planetary constants using the "controle" array in the file
+! if "cointrole" array not found in file, look for it in "diagfi.nc"
+use planet_const
+use netcdf
+implicit none
+
+integer,intent(in) :: infid ! input file ID
+
+! local variables
+character(len=100) :: varname
+integer :: varid ! variable ID
+integer :: status
+integer :: infid2 ! for an alternate input file
+real :: controle(100) ! to store "controle" array
+
+! look for "controle" in input file
+infid2=infid ! initialization
+varname="controle" 
+status=nf90_inq_varid(infid,trim(varname),varid)
+if (status.ne.nf90_noerr) then
+  write(*,*) "init_planet_const: Failed to find ",trim(varname)
+  write(*,*) " looking for it in file diagfi.nc"
+  status=nf90_open("diagfi.nc",NF90_NOWRITE,infid2)
+  if (status.ne.nf90_noerr) then
+    write(*,*) " no diafi.nc file ... looking for diagfi1.nc"
+    status=nf90_open("diagfi1.nc",NF90_NOWRITE,infid2)
+    if (status.ne.nf90_noerr) then
+      write(*,*) "might as well stop here..."
+      stop
+    endif
+  endif
+  status=nf90_inq_varid(infid2,trim(varname),varid)
+  if (status.ne.nf90_noerr) then
+    write(*,*) " Failed to find ",trim(varname)," in file!!"
+    stop
+  endif
+  write(*,*) "OK, found ",trim(varname)
+endif
+status=nf90_get_var(infid2,varid,controle)
+if (status.ne.nf90_noerr) then
+  write(*,*) "init_planet_const: Failed to load ",trim(varname)
+  stop
+endif
+
+! now that we have "controle"; extract relevent informations
+a0=controle(5) ! = radius of planet (m)
+g0=controle(7) ! = gravity (m.s-2) at a0
+Rmean=1000.*8.314511/controle(8) ! controle(8)=mugaz = molar mass (g.mol-1) of atm.
+
+end subroutine init_planet_const
 
 !===============================================================================
@@ -1752,4 +1817,5 @@
 !          surface altitudes" corresponding to GCM atmospheric levels
 !==============================================================================
+use planet_const, only : a0,g0
 implicit none
 !==============================================================================
@@ -1785,7 +1851,7 @@
 
 ! Parameters needed to integrate hydrostatic equation:
-real,parameter :: g0=3.7257964
+!real,parameter :: g0=3.7257964
 !g0: exact mean gravity at radius=3396.km (Lemoine et al. 2001)
-real,parameter :: a0=3396.E3
+!real,parameter :: a0=3396.E3
 !a0: 'mean' Mars radius=3396.km
 real :: gz 
@@ -1871,5 +1937,4 @@
 !===============================================================================
 
-!#include"build_gcm_alt.F90"
 subroutine build_gcm_za(lonlength,latlength,altlength,timelength, &
                          phis,ps,press,temp,rho,za_gcm)
@@ -1878,4 +1943,5 @@
 !          altitudes" corresponding to GCM atmospheric levels
 !==============================================================================
+use planet_const, only : a0,g0
 implicit none
 !==============================================================================
@@ -1911,7 +1977,7 @@
 
 ! Parameters needed to integrate hydrostatic equation:
-real,parameter :: g0=3.7257964
+!real,parameter :: g0=3.7257964
 !g0: exact mean gravity at radius=3396.km (Lemoine et al. 2001)
-real,parameter :: a0=3396.E3
+!real,parameter :: a0=3396.E3
 !a0: 'mean' Mars radius=3396.km
 real :: gz 
@@ -2062,4 +2128,5 @@
 !          surface altitudes" corresponding to GCM atmospheric levels
 !==============================================================================
+use planet_const, only : a0,g0,Rmean
 implicit none
 !==============================================================================
@@ -2086,5 +2153,5 @@
 !===============================================================================
 real,dimension(:),allocatable :: sigma ! GCM sigma levels
-real,parameter :: R=191 ! molecular gas constant
+!real,parameter :: R=191 ! molecular gas constant
 !real,dimension(:,:,:,:),allocatable :: zlocal ! local above surface altitude
 real :: Tmean ! "mean" value of temperature for a given level
@@ -2092,7 +2159,7 @@
 
 ! Parameters needed to integrate hydrostatic equation:
-real,parameter :: g0=3.7257964
+!real,parameter :: g0=3.7257964
 !g0: exact mean gravity at radius=3396.km (Lemoine et al. 2001)
-real,parameter :: a0=3396.E3
+!real,parameter :: a0=3396.E3
 !a0: 'mean' Mars radius=3396.km
 real :: gz 
@@ -2114,5 +2181,5 @@
       sigma(1)=press(iloop,jloop,1,tloop)/ps(iloop,jloop,tloop)
       zs_gcm(iloop,jloop,1,tloop)=-log(sigma(1))* &
-                                               R*temp(iloop,jloop,1,tloop)/g0
+                                           Rmean*temp(iloop,jloop,1,tloop)/g0
 !      za_gcm(iloop,jloop,1,tloop)=zlocal(iloop,jloop,1,tloop)+ &
 !                                  phis(iloop,jloop)/g0
@@ -2139,5 +2206,5 @@
         ! compute zs_gcm(iloop,jloop,kloop,tloop)
         zs_gcm(iloop,jloop,kloop,tloop)=zs_gcm(iloop,jloop,kloop-1,tloop)- &
-                log(sigma(kloop)/sigma(kloop-1))*R*Tmean/gz
+                log(sigma(kloop)/sigma(kloop-1))*Rmean*Tmean/gz
         
         ! compute za_gcm(kloop)
@@ -2157,5 +2224,4 @@
 !===============================================================================
 
-!#include"crude_gcm_alt.F90"
 subroutine crude_gcm_za(lonlength,latlength,altlength,timelength, &
                          phis,ps,press,temp,za_gcm)
@@ -2164,4 +2230,5 @@
 !          altitudes" corresponding to GCM atmospheric levels
 !==============================================================================
+use planet_const, only : a0,g0,Rmean
 implicit none
 !==============================================================================
@@ -2188,5 +2255,5 @@
 !===============================================================================
 real,dimension(:),allocatable :: sigma ! GCM sigma levels
-real,parameter :: R=191 ! molecular gas constant
+!real,parameter :: R=191 ! molecular gas constant
 real,dimension(:,:,:,:),allocatable :: zlocal ! local above surface altitude
 real :: Tmean ! "mean" value of temperature for a given level
@@ -2194,7 +2261,7 @@
 
 ! Parameters needed to integrate hydrostatic equation:
-real,parameter :: g0=3.7257964
+!real,parameter :: g0=3.7257964
 !g0: exact mean gravity at radius=3396.km (Lemoine et al. 2001)
-real,parameter :: a0=3396.E3
+!real,parameter :: a0=3396.E3
 !a0: 'mean' Mars radius=3396.km
 real :: gz 
@@ -2216,5 +2283,5 @@
       sigma(1)=press(iloop,jloop,1,tloop)/ps(iloop,jloop,tloop)
       zlocal(iloop,jloop,1,tloop)=-log(sigma(1))* &
-                                               R*temp(iloop,jloop,1,tloop)/g0
+                                            Rmean*temp(iloop,jloop,1,tloop)/g0
       za_gcm(iloop,jloop,1,tloop)=zlocal(iloop,jloop,1,tloop)+ &
                                   phis(iloop,jloop)/g0
@@ -2239,5 +2306,5 @@
         ! compute zlocal(kloop)
         zlocal(iloop,jloop,kloop,tloop)=zlocal(iloop,jloop,kloop-1,tloop)- &
-                log(sigma(kloop)/sigma(kloop-1))*R*Tmean/gz
+                log(sigma(kloop)/sigma(kloop-1))*Rmean*Tmean/gz
         
         ! compute za_gcm(kloop)
@@ -2332,5 +2399,4 @@
 !===============================================================================
 
-!#include"za_coord_interp.F90"
 subroutine z_coord_interp(lonlen,latlen,altlen,tlen,newaltlen, &
                           missing,z_gcm,gcmdata,flag,z_new,newdata)
@@ -2462,4 +2528,5 @@
 !       if flag=0, and extrapolated (exponentially) if flag=1.
 !==============================================================================
+use planet_const, only : a0,g0,Rmean
 implicit none
 !==============================================================================
@@ -2503,9 +2570,9 @@
 real :: x,y ! temporary variables
 integer :: iloop,jloop,kloop,tloop
-real,parameter :: g0=3.7257964
+!real,parameter :: g0=3.7257964
 !g0: exact mean gravity at radius=3396.km (Lemoine et al. 2001)
-real,parameter :: a0=3396.E3
+!real,parameter :: a0=3396.E3
 !a0: 'mean' Mars radius=3396.km
-real,parameter :: Rmean=191 ! molecular gas constant
+!real,parameter :: Rmean=191 ! molecular gas constant
 real :: gz 
 ! gz: gravitational acceleration at a given (above areoid) altitude
