write_output.f90 4.08 KB

!________________________________________________________________________________

                        !DESCRIPTION: This routine create the NETCDF output
                        !file. You must have the FORTRAN NETCDF libraries
                        !installed on your computer  to use this routine.


!________________________________________________________________________________

                        !INTERFACE:
                        subroutine write_output

                        !MODULE USES:
                        use netcdf
                        use global_parameter

                    

                        !LOCAL PARAMETERS:

                        implicit none

                         !INCLUDES:
                        include 'netcdf.inc'

                        integer :: ncid
                        integer :: timeID
                        integer :: depthID
                        integer :: QID
                        integer :: ncatID
                        integer :: time_varID
                        integer :: depth_varID
                        integer :: Q_varID
                        integer :: growth_varID
                        integer :: stat
                        integer :: growthcatID
                        integer :: QcatID               
                        integer :: q_distID
                        integer :: mu_distID

!_________________________________________________________________________________
stat=nf90_create(trim(root)//'/out.nc',cmode=or(nf90_clobber,nf90_64bit_offset),ncid=ncid)

call handle_err(stat)
!__________________dimensions_______________________________


        stat=nf90_def_dim(ncid,"depth", nbin, depthID)
        call handle_err(stat)

        stat=nf90_def_dim(ncid,"time", L_time, timeID)
        call handle_err(stat)
        stat=nf90_def_dim(ncid,"ncat",Ncat, ncatID)
        call handle_err(stat)


!_____________variables__________________________________________
        stat=nf90_def_var(ncid,"depth",nf90_double,depthID,depth_varID)
        call handle_err(stat)
        stat=nf90_def_var(ncid,"growthcat",nf90_double,ncatID,growthcatID)
        call handle_err(stat)
        stat=nf90_def_var(ncid,"Qcat",nf90_double,ncatID,QcatID)
        call handle_err(stat)

        stat=nf90_def_var(ncid,"cell quota",nf90_double,(/ depthID, ncatID, timeID /),Q_varID)
        call handle_err(stat)
        stat=nf90_def_var(ncid,"growth_rate",nf90_double,(/ depthID, ncatID, timeID/),growth_varID)
        call handle_err(stat)

          stat=nf90_def_var(ncid,"cell quota eul",nf90_double,(/ depthID,ncatID,timeID /),q_distID)
        call handle_err(stat)
        stat=nf90_def_var(ncid,"growth_rate eul",nf90_double,(/ depthID,ncatID,timeID/),mu_distID)
        call handle_err(stat)



        stat=nf90_def_var(ncid,"time",nf90_double,timeID,time_varID)
        call handle_err(stat)


        stat=nf90_enddef(ncid)

        call handle_err(stat)
        stat=nf90_put_var(ncid,depth_varID,levels(1:Nlevel-1))
        call handle_err(stat)
        stat=nf90_put_var(ncid,growthcatID,mu_cat)
        call handle_err(stat)
        stat=nf90_put_var(ncid,QcatID,q_cat)
        call handle_err(stat)

        stat=nf90_put_var(ncid,Q_varID,Q_distrib2)
        call handle_err(stat)
        stat=nf90_put_var(ncid,growth_varID,growth_distrib2)
        call handle_err(stat)

           stat=nf90_put_var(ncid,q_distID,q_dist2)
        call handle_err(stat)
        stat=nf90_put_var(ncid,mu_distID,mu_dist2)
        call handle_err(stat)


        stat=nf90_put_var(ncid,time_varID,time_vec)
        call handle_err(stat)
        





        stat=nf90_close(ncid)
        call handle_err(stat)
        
contains

!____________________________________________________________________________________
subroutine handle_err(stat)
        integer, intent(in) :: stat

        if(stat /=nf90_noerr) then
        write(*,*)trim(nf90_strerror(stat))
        STOP 'unable de write the netcdf output' 
        end if
end subroutine handle_err

!____________________________________________________________________________________

end subroutine write_output