bio_save.F90 5.92 KB
!$Id: bio_save.F90,v 1.8 2007-03-14 12:46:07 kbk Exp $
#include"cppdefs.h"
!-----------------------------------------------------------------------
!BOP
! !ROUTINE: Storing the results
!
! !INTERFACE:
   subroutine bio_save(nlev,totn)
!
! !DESCRIPTION:
! Here, the output of biogeochemical parameters either as ascii or as
! NetCDF files is managed.
!
! !USES:
   use bio_var
   use output,  only: out_fmt,ts
#ifdef NETCDF_FMT
   use ncdfout, only: ncid
   use ncdfout, only: lon_dim,lat_dim,z_dim,time_dim,dims
   use ncdfout, only: define_mode,new_nc_variable,set_attributes,store_data
#endif
   IMPLICIT NONE
#ifdef NETCDF_FMT
#include "netcdf.inc"
!   use netcdf
#endif
!
! !INPUT PARAMETERS:
   integer, intent(in)                 :: nlev
   REALTYPE, intent(in)                :: totn
!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard & Karsten Bolding
!
! !LOCAL VARIABLES:
   logical, save             :: first=.true.
   integer, save             :: nn
   integer, save             :: totn_id
   integer                   :: iret
   integer                   :: out_unit=67
   REALTYPE                  :: zz
   integer                   :: i,j,n
!EOP
!-----------------------------------------------------------------------
!BOC
   if (init_saved_vars) then
      init_saved_vars=.false.
      first=.true.
   end if
   select case (out_fmt)
      case (ASCII)
         if(first) then
            open(out_unit,file='bio.out',status='unknown')
            nn = ubound(cc(1,:),1)
            first = .false.
         end if
         write(out_unit,*)
         write(out_unit,*) trim(ts)
         zz = _ZERO_
         do i=nn,1,-1
            zz=zz+0.5*h(i)
            write(out_unit,115) zz,(cc(j,i) , j=1,numc)
            zz=zz+0.5*h(i)
         end do
115 format(F10.4,100(1x,E10.4E2))

      case (NETCDF)
#ifdef NETCDF_FMT
         if(first) then
            first = .false.
            dims(1) = lon_dim
            dims(2) = lat_dim
            dims(3) = z_dim
            dims(4) = time_dim

            iret = define_mode(ncid,.true.)

            do n=1,numc
               iret = new_nc_variable(ncid,var_names(n),NF_REAL, &
                                      4,dims,var_ids(n))
               iret = set_attributes(ncid,var_ids(n),       &
                                     units=var_units(n),    &
                                     long_name=var_long(n))
            end do

            !CHG2 Diagnostic de la vitesse de chute du phytoplancton (wp)
            iret = new_nc_variable(ncid,'wp',NF_REAL,4,dims,wp_id)
            iret = set_attributes(ncid,wp_id,units='m/day', &
                                  long_name='phytoplancton settling velocity')

            !CHG4 Diagnostic du PAR
            iret = new_nc_variable(ncid,'par',NF_REAL,4,dims,par_id)
            iret = set_attributes(ncid,par_id,units='W/m2',long_name='PAR')

            !DD Diagnostic des fonctions de croissance
            iret = new_nc_variable(ncid,'lumlim1',NF_REAL,4,dims,lumlim1_id)
            iret = set_attributes(ncid,lumlim1_id,units='1/day', &
                                  long_name='light limited growth rate for picophyto')
            iret = new_nc_variable(ncid,'nitlim1',NF_REAL,4,dims,nitlim1_id)
            iret = set_attributes(ncid,nitlim1_id,units='',long_name='nitrate limitation for picophyto')
            iret = new_nc_variable(ncid,'ammlim1',NF_REAL,4,dims,ammlim1_id)
            iret = set_attributes(ncid,ammlim1_id,units='',long_name='ammonium limitation for picophyto')
            iret = new_nc_variable(ncid,'lumlim2',NF_REAL,4,dims,lumlim2_id)
            iret = set_attributes(ncid,lumlim2_id,units='1/day', &
                                  long_name='light limited growth rate for microphyto')
            iret = new_nc_variable(ncid,'nitlim2',NF_REAL,4,dims,nitlim2_id)
            iret = set_attributes(ncid,nitlim2_id,units='',long_name='nitrate limitation for microphyto')
            iret = new_nc_variable(ncid,'ammlim2',NF_REAL,4,dims,ammlim2_id)
            iret = set_attributes(ncid,ammlim2_id,units='',long_name='ammonium limitation for microphyto')

            !DD Diagnostic de npar (nb de particules lagrangiennes) pour bebogage
            !iret = new_nc_variable(ncid,'npar',NF_REAL,4,dims,npar_id)
            !iret = set_attributes(ncid,npar_id,units='', &
            !                      long_name='nb of particles per level')

            dims(1) = time_dim
            iret = new_nc_variable(ncid,'totn',NF_REAL,1,dims,totn_id)
            iret = set_attributes(ncid,totn_id,units='mmol/m**2',    &
                   long_name='total N')

            iret = define_mode(ncid,.false.)
         end if

         do n=1,numc
            iret = store_data(ncid,var_ids(n),XYZT_SHAPE,nlev,array=cc(n,:))
         end do

         !CHG2 Vitesse de chute du phytoplancton (wp = ws(1,:))
         iret = store_data(ncid,wp_id,XYZT_SHAPE,nlev,array=86400*ws(1,:))

         !CHG4 PAR (par = par(:))
         iret = store_data(ncid,par_id,XYZT_SHAPE,nlev,array=par(:))

         !DD
         iret = store_data(ncid,lumlim1_id,XYZT_SHAPE,nlev,array=lumlim1(:))
         iret = store_data(ncid,nitlim1_id,XYZT_SHAPE,nlev,array=nitlim1(:))
         iret = store_data(ncid,ammlim1_id,XYZT_SHAPE,nlev,array=ammlim1(:))
         iret = store_data(ncid,lumlim2_id,XYZT_SHAPE,nlev,array=lumlim2(:))
         iret = store_data(ncid,nitlim2_id,XYZT_SHAPE,nlev,array=nitlim2(:))
         iret = store_data(ncid,ammlim2_id,XYZT_SHAPE,nlev,array=ammlim2(:))

         !DD
         !iret = store_data(ncid,npar_id,XYZT_SHAPE,nlev,array=npar(:))

         iret = store_data(ncid,totn_id,T_SHAPE,1,scalar=totn)
#endif
      case default
         FATAL 'A non valid output format has been chosen'
         stop 'bio_save'
   end select

   return
   end subroutine bio_save
!EOC

!-----------------------------------------------------------------------
! Copyright by the GOTM-team under the GNU Public License - www.gnu.org
!-----------------------------------------------------------------------