bio_save.F90 15.4 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)
!-----------------------------------------------------------------------------------
! Ascii format for the output
!-----------------------------------------------------------------------------------
      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))

!-----------------------------------------------------------------------------------
! NETCDF format for the output
!-----------------------------------------------------------------------------------
      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
!-----------------------------------------------------------------------------------
!Prepare the new variable for the NETCDF output file as well as informations
!-----------------------------------------------------------------------------------
    
!Density of the fluid at each level
            iret = new_nc_variable(ncid,'rho',NF_REAL,4,dims,rho_id)
            iret = set_attributes(ncid,rho_id,units='kg.m-3',long_name='Density')

! Sedimentation or swimming rate of particulate matter
            ! Living phytoplankton
            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')

           ! Living zooplankton
        !Nocera Model
            if (bio_model .eq. 8) then
               iret = new_nc_variable(ncid,'wz',NF_REAL,4,dims,wz_id)
               iret = set_attributes(ncid,wz_id,units='m/day', &
                                  long_name='zooplancton swimming velocity')
            end if
         
         !Polynow model
            if (bio_model .eq. 10) then
            ! Living zooplankton
               iret = new_nc_variable(ncid,'wz',NF_REAL,4,dims,wz_id)
               iret = set_attributes(ncid,wz_id,units='m/day', &
                                  long_name='Zooplancton swimming velocity')
            ! Dead Phytoplankton
               iret = new_nc_variable(ncid,'wd1',NF_REAL,4,dims,wd1_id)
               iret = set_attributes(ncid,wd1_id,units='m/day', &
                                  long_name='Dead Phytoplankton settling velocity')
            ! Dead zooplankton
               iret = new_nc_variable(ncid,'wd2',NF_REAL,4,dims,wd2_id)
               iret = set_attributes(ncid,wd2_id,units='m/day', &
                                  long_name='Dead Zooplankton settling velocity')
            ! Fecals pellets
               iret = new_nc_variable(ncid,'wd3',NF_REAL,4,dims,wd3_id)
               iret = set_attributes(ncid,wd3_id,units='m/day', &
                                  long_name='Fecal pellets settling velocity')
            ! Marine Snow
               iret = new_nc_variable(ncid,'wd4',NF_REAL,4,dims,wd4_id)
               iret = set_attributes(ncid,wd4_id,units='m/day', &
                                  long_name='Marine snow settling velocity (Calc)')

       ! Stickiness
            ! 2 Living phytoplankton
               iret = new_nc_variable(ncid,'sti_2p',NF_REAL,4,dims,sti_2p_id)
               iret = set_attributes(ncid,sti_2p_id, &
                                 long_name='Stickiness Phy & Phy')
            ! Living Phytoplankton with dead phytoplankton
               iret = new_nc_variable(ncid,'sti_pdph',NF_REAL,4,dims,sti_pdph_id)
               iret = set_attributes(ncid,sti_pdph_id, &
                                 long_name='Stickiness Phy & Dph')
            ! Living Phytoplankton with dead zooplankton
               iret = new_nc_variable(ncid,'sti_pdzo',NF_REAL,4,dims,sti_pdzo_id)
               iret = set_attributes(ncid,sti_pdzo_id, &
                                 long_name='Stickiness Phy & Dzo')
            ! Living Phytoplankton with fecal pellets
               iret = new_nc_variable(ncid,'sti_pfp',NF_REAL,4,dims,sti_pfp_id)
               iret = set_attributes(ncid,sti_pfp_id, &
                                 long_name='Stickiness Phy & Fp')
            ! 2 Dead phytoplankton
               iret = new_nc_variable(ncid,'sti_2dph',NF_REAL,4,dims,sti_2dph_id)
               iret = set_attributes(ncid,sti_2dph_id, &
                                 long_name='Stickiness Dph & Dph')
            ! Dead Phytoplankton with dead zooplankton
               iret = new_nc_variable(ncid,'sti_dphdzo',NF_REAL,4,dims,sti_dphdzo_id)
               iret = set_attributes(ncid,sti_dphdzo_id, &
                                 long_name='Stickiness Dph & Dzo')
            ! Dead  Phytoplankton with fecal pellets
               iret = new_nc_variable(ncid,'sti_dphfp',NF_REAL,4,dims,sti_dphfp_id)
               iret = set_attributes(ncid,sti_dphfp_id, &
                                 long_name='Stickiness Dph & Fp')
            ! 2 Dead zooplankton
               iret = new_nc_variable(ncid,'sti_2dzo',NF_REAL,4,dims,sti_2dzo_id)
               iret = set_attributes(ncid,sti_2dzo_id, &
                                 long_name='Stickiness Dzo & Dzo')
            ! Dead  zooplankton with fecal pellets
               iret = new_nc_variable(ncid,'sti_dzofp',NF_REAL,4,dims,sti_dzofp_id)
               iret = set_attributes(ncid,sti_dzofp_id, &
                                 long_name='Stickiness Dzo & Fp')
            ! 2 fecal pellets
               iret = new_nc_variable(ncid,'sti_2fp',NF_REAL,4,dims,sti_2fp_id)
               iret = set_attributes(ncid,sti_2fp_id, &
                                 long_name='Stickiness Fp & Fp')

              end if

!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')
            iret = new_nc_variable(ncid,'ppnet',NF_REAL,4,dims,ppnet_id)
            iret = set_attributes(ncid,ppnet_id,units='1/day',long_name='net primary production rate')

 !Polynow model
            if (bio_model .eq. 10) then
       ! Total flux of particle reaching msn
            iret = new_nc_variable(ncid,'flux_msn',NF_REAL,4,dims,flux_msn_id)
            iret = set_attributes(ncid,flux_msn_id,units='1/day',long_name='flux_msn')
       ! Flux of Phyto going to msn
            iret = new_nc_variable(ncid,'Flux_P',NF_REAL,4,dims,Flux_P_id)
            iret = set_attributes(ncid,Flux_P_id,units='1/day',long_name='Flux_P')
       ! Flux of DPH going to msn
            iret = new_nc_variable(ncid,'Flux_D1',NF_REAL,4,dims,Flux_D1_id)
            iret = set_attributes(ncid,Flux_D1_id,units='1/day',long_name='Flux_D1')
       ! Flux of DZO going to msn
            iret = new_nc_variable(ncid,'Flux_D2',NF_REAL,4,dims,Flux_D2_id)
            iret = set_attributes(ncid,Flux_D2_id,units='1/day',long_name='Flux_D2')
       ! Flux of FP going to msn
            iret = new_nc_variable(ncid,'Flux_D3',NF_REAL,4,dims,Flux_D3_id)
            iret = set_attributes(ncid,Flux_D3_id,units='1/day',long_name='Flux_D3')
            endif

 !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

!-----------------------------------------------------------------------------------
!Prepare to store the data in the NetCDF file
!-----------------------------------------------------------------------------------
         do n=1,numc
            iret = store_data(ncid,var_ids(n),XYZT_SHAPE,nlev,array=cc(n,:))
         end do


         !Density of the fluid at each level
         iret = store_data(ncid,rho_id,XYZT_SHAPE,nlev,array=rho)


         ! Sedimentation rate of phytoplankton
         iret = store_data(ncid,wp_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(1,:))

         ! Swimming velocity of zootoplankton
!Nocera model
         if (bio_model .eq. 8) then
            iret = store_data(ncid,wz_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(2,:))
         end if

!Polynow model - [Values fond un ws(x,:) --> x represent the variables attribution made in bio_polynow.
!As example : :  p=1,z=2,b=3,d1(dph)=4,n=5,a=6,l=7,d2(dzo)=8,d3(fp)=9,d4(msn)=10
  
 if (bio_model .eq. 10) then

! Here it is needed to multiply by 86400(secs_pr_day) in order to have the model ouptu (in s) convert in per day
            ! Living zooplankton
            iret = store_data(ncid,wz_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(2,:))
            ! Dead Phytoplankton
            iret = store_data(ncid,wd1_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(4,:))
            ! Dead zooplankton
            iret = store_data(ncid,wd2_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(8,:))
            ! Fecal pellets
            iret = store_data(ncid,wd3_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(9,:))
            ! Marine Snow
            iret = store_data(ncid,wd4_id,XYZT_SHAPE,nlev,array=secs_pr_day*ws(10,:))

       ! Stickiness
            ! 2 Living phytoplankton
             iret = store_data(ncid,sti_2p_id,XYZT_SHAPE,nlev,array=sti_2p(:))

            ! Living Phytoplankton with dead phytoplankton
             iret = store_data(ncid,sti_pdph_id,XYZT_SHAPE,nlev,array=sti_pdph(:))

            ! Living Phytoplankton with dead zooplankton
              iret = store_data(ncid,sti_pdzo_id,XYZT_SHAPE,nlev,array=sti_pdzo(:))

            ! Living Phytoplankton with fecal pellets
              iret = store_data(ncid,sti_pfp_id,XYZT_SHAPE,nlev,array=sti_pfp(:))

            ! 2 Dead phytoplankton
              iret = store_data(ncid,sti_2dph_id,XYZT_SHAPE,nlev,array=sti_2dph(:))

            ! Dead Phytoplankton with dead zooplankton
              iret = store_data(ncid,sti_dphdzo_id,XYZT_SHAPE,nlev,array=sti_dphdzo(:))

            ! Dead  Phytoplankton with fecal pellets
              iret = store_data(ncid,sti_dphfp_id,XYZT_SHAPE,nlev,array=sti_dphfp(:))

            ! 2 Dead zooplankton
              iret = store_data(ncid,sti_2dzo_id,XYZT_SHAPE,nlev,array=sti_2dzo(:))

            ! Dead  zooplankton with fecal pellets
              iret = store_data(ncid,sti_dzofp_id,XYZT_SHAPE,nlev,array=sti_dzofp(:))

            ! 2 fecal pellets
             iret = store_data(ncid,sti_2fp_id,XYZT_SHAPE,nlev,array=sti_2fp(:))

         ! Rho_F
    !     iret = store_data(ncid,rho_F_id,XYZT_SHAPE,nlev,array=)

  end if

         ! 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(:))
         iret = store_data(ncid,ppnet_id,XYZT_SHAPE,nlev,array=ppnet(:))

 if (bio_model .eq. 10) then
         iret = store_data(ncid,flux_msn_id,XYZT_SHAPE,nlev,array=secs_pr_day*flux_msn(:))
         iret = store_data(ncid,Flux_P_id,XYZT_SHAPE,nlev,array=secs_pr_day*Flux_P(:))
         iret = store_data(ncid,Flux_D1_id,XYZT_SHAPE,nlev,array=secs_pr_day*Flux_D1(:))
         iret = store_data(ncid,Flux_D2_id,XYZT_SHAPE,nlev,array=secs_pr_day*Flux_D2(:))
         iret = store_data(ncid,Flux_D3_id,XYZT_SHAPE,nlev,array=secs_pr_day*Flux_D3(:))
endif


         !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
!-----------------------------------------------------------------------