!
! $Header: /home/cvsroot/LMDZ4/libf/phylmd/write_histins.h,v 1.1.1.1 2004/05/19 12:53:09 lmdzadmin Exp $
!
      IF (ok_instan) THEN
c
      ndex2d = 0
      ndex3d = 0
      zx_tmp_2d = 0.
      zx_tmp_3d = 0.
      zx_tmp_fi2d=0.
      zx_tmp_fi3d=0.
c
          zsto = dtime * FLOAT(ecrit_ins)
          zout = dtime * FLOAT(ecrit_ins)
         itau_w = itau_phy + itap

c
c-------------------------------------------------------
      IF(lev_histday.GE.1) THEN
c
ccccccccccccc 2D fields, invariables
c
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,pphis,zx_tmp_2d)
      CALL histwrite(nid_ins,"phis",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
C
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,airephy,zx_tmp_2d)
      CALL histwrite(nid_ins,"aire",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
ccccccc axe Ls ... Faudrait le reduire a axe temporel seulement...
      do j=1,jjmp1
       do i=1,iim
        zx_tmp_2d(i,j)=zls*180./RPI      ! zls est en radians !!
       enddo
      enddo
      CALL histwrite(nid_ins,"ls",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
ccccccccccccc 2D fields, variables
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, ftsol,zx_tmp_2d)
      CALL histwrite(nid_ins,"tsol",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      DO i = 1, klon
         zx_tmp_fi2d(i) = paprs(i,1)
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_ins,"psol",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(1, klon,iim,jjmp1, ue,zx_tmp_2d)
c     CALL histwrite(nid_ins,"ue",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(1, klon,iim,jjmp1, ve,zx_tmp_2d)
c     CALL histwrite(nid_ins,"ve",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      ENDIF !lev_histday.GE.1
c
c-------------------------------------------------------
      IF(lev_histday.GE.2) THEN
c
ccccccccccccc 3D fields, basics
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, t_seri, zx_tmp_3d)
      CALL histwrite(nid_ins,"temp",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, pplay, zx_tmp_3d)
      CALL histwrite(nid_ins,"pres",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, zphi, zx_tmp_3d)
      CALL histwrite(nid_ins,"geop",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, u_seri, zx_tmp_3d)
      CALL histwrite(nid_ins,"vitu",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, v_seri, zx_tmp_3d)
      CALL histwrite(nid_ins,"vitv",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, omega, zx_tmp_3d)
      CALL histwrite(nid_ins,"vitw",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, topsw,zx_tmp_2d)
      CALL histwrite(nid_ins,"tops",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(klev,klon,iim,jjmp1, d_u_vdf, zx_tmp_3d)
c     CALL histwrite(nid_ins,"duvdf",itau_w,zx_tmp_3d,
c    .                                   iim*jjmp1*klev,ndex3d)
c
c     CALL gr_fi_ecrit(klev,klon,iim,jjmp1, d_u_dyn, zx_tmp_3d)
c     CALL histwrite(nid_ins,"dudyn",itau_w,zx_tmp_3d,
c    .                                   iim*jjmp1*klev,ndex3d)
c
      ENDIF !lev_histday.GE.2
c
c-------------------------------------------------------
      IF(lev_histday.GE.3) THEN
c
cccccccccccccccccc  Tracers
c
         if (iflag_trac.eq.1) THEN
          if (microfi.eq.1) then
           DO iq=1,nmicro
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, qaer(1,1,iq), zx_tmp_3d)
       CALL histwrite(nid_ins,tname(iq),itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
           ENDDO
	  endif
	  if (nmicro.lt.nqmax) then
           DO iq=nmicro+1,nqmax
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1,tr_seri(1,1,iq),zx_tmp_3d)
       CALL histwrite(nid_ins,tname(iq),itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
           ENDDO
	  endif
         endif
c
cccccccccccccccccc  Radiative transfer
c
c 2D
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, toplw,zx_tmp_2d)
      CALL histwrite(nid_ins,"topl",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, solsw,zx_tmp_2d)
      CALL histwrite(nid_ins,"sols",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, sollw,zx_tmp_2d)
      CALL histwrite(nid_ins,"soll",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c 3D
c
      zx_tmp_fi3d(1:klon,1:klev)=swnet(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"SWnet",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      zx_tmp_fi3d(1:klon,1:klev)=lwnet(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"LWnet",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
c --------------
c ----- OPACITE BRUME
       do k=7,NSPECV,10
         do i=1,klon
         do l=1,klev
           t_tauhvd(i,l)=TAUHVD(i,klev-l+1,k)
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_tauhvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"thv"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECV 

       do k=8,NSPECI,10
         do i=1,klon
         do l=1,klev
           t_tauhvd(i,l)=TAUHID(i,klev-l+1,k)
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_tauhvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"thi"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECI 
c --------------
c ----- EXTINCTION BRUME
       do k=7,NSPECV,10
         do i=1,klon
         do l=1,klev
          if(l.ne.klev)
     s     t_khvd(i,l)=TAUHVD(i,klev-l+1,k)
     s                -TAUHVD(i,klev-l+1-1,k)

          if(l.eq.klev)
     s     t_khvd(i,l)=TAUHVD(i,klev-l+1,k)

         t_khvd(i,l)=t_khvd(i,l)/(zzlev(i,l+1)-zzlev(i,l))
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_khvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"khv"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECV 

       do k=8,NSPECI,10
         do i=1,klon
         do l=1,klev
          if(l.ne.klev)
     s     t_khvd(i,l)=TAUHID(i,klev-l+1,k)
     s                -TAUHID(i,klev-l+1-1,k)

          if(l.eq.klev)
     s     t_khvd(i,l)=TAUHID(i,klev-l+1,k)

         t_khvd(i,l)=t_khvd(i,l)/(zzlev(i,l+1)-zzlev(i,l))
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_khvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"khi"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECI 
c --------------
c ----- OPACITE GAZ
       do k=7,NSPECV,10
         do i=1,klon
         do l=1,klev
           t_tauhvd(i,l)=TAUGVD(i,klev-l+1,k)
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_tauhvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"tgv"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECV 

       do k=8,NSPECI,10
         do i=1,klon
         do l=1,klev
           t_tauhvd(i,l)=TAUGID(i,klev-l+1,k)
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_tauhvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"tgi"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECI 
c --------------
c ----- EXTINCTION GAZ
       do k=7,NSPECV,10
         do i=1,klon
         do l=1,klev
          if(l.ne.klev)
     s     t_khvd(i,l)=TAUGVD(i,klev-l+1,k)
     s                -TAUGVD(i,klev-l+1-1,k)

          if(l.eq.klev)
     s     t_khvd(i,l)=TAUGVD(i,klev-l+1,k)

         t_khvd(i,l)=t_khvd(i,l)/(zzlev(i,l+1)-zzlev(i,l))
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_khvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"kgv"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECV 

       do k=8,NSPECI,10
         do i=1,klon
         do l=1,klev
          if(l.ne.klev)
     s     t_khvd(i,l)=TAUGID(i,klev-l+1,k)
     s                -TAUGID(i,klev-l+1-1,k)

          if(l.eq.klev)
     s     t_khvd(i,l)=TAUGID(i,klev-l+1,k)

         t_khvd(i,l)=t_khvd(i,l)/(zzlev(i,l+1)-zzlev(i,l))
         enddo
         enddo
         write(str1,'(i2.2)') k
      zx_tmp_fi3d(1:klon,1:klev)=t_khvd(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"kgi"//str1,itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
       enddo      ! fin boucle NSPECI 

      ENDIF !lev_histday.GE.3
c
c-------------------------------------------------------
      IF(lev_histday.GE.4) THEN
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, d_t_dyn, zx_tmp_3d)
      CALL histwrite(nid_ins,"dtdyn",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1, d_t, zx_tmp_3d)
      CALL histwrite(nid_ins,"dtphy",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c K/s
      zx_tmp_fi3d(1:klon,1:klev)=d_t_vdf(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"dtvdf",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
c K/s
      zx_tmp_fi3d(1:klon,1:klev)=d_t_ajs(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"dtajs",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
c K/s
      zx_tmp_fi3d(1:klon,1:klev)=heat(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"dtswr",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c
c K/s      
      zx_tmp_fi3d(1:klon,1:klev)=-1.*cool(1:klon,1:klev)
      CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
      CALL histwrite(nid_ins,"dtlwr",itau_w,zx_tmp_3d,
     .                                   iim*jjmp1*klev,ndex3d)
c K/s      
c     zx_tmp_fi3d(1:klon,1:klev)=d_t_ec(1:klon,1:klev)
c     CALL gr_fi_ecrit(klev,klon,iim,jjmp1,zx_tmp_fi3d,zx_tmp_3d)
c     CALL histwrite(nid_ins,"dtec",itau_w,zx_tmp_3d,
c    .                                   iim*jjmp1*klev,ndex3d)
c
c     CALL gr_fi_ecrit(klev,klon,iim,jjmp1, d_v_vdf, zx_tmp_3d)
c     CALL histwrite(nid_ins,"dvvdf",itau_w,zx_tmp_3d,
c    .                                   iim*jjmp1*klev,ndex3d)
c
      ENDIF !lev_histday.GE.4
c
c-------------------------------------------------------
      IF(lev_histday.GE.5) THEN
c
c
c      CALL gr_fi_ecrit(1, klon,iim,jjmp1, fluxu , zx_tmp_2d)
c      CALL histwrite(nid_ins,"taux_",itau_w,
c    $      zx_tmp_2d,iim*jjmp1,ndex2d) 
c      
c      CALL gr_fi_ecrit(1, klon,iim,jjmp1, fluxv , zx_tmp_2d)
c      CALL histwrite(nid_ins,"tauy_",itau_w,
c    $      zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(1, klon,iim,jjmp1, cdragm,zx_tmp_2d)
c     CALL histwrite(nid_ins,"cdrm",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(1, klon,iim,jjmp1, cdragh,zx_tmp_2d)
c     CALL histwrite(nid_ins,"cdrh",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      ENDIF !lev_histday.GE.5
c-------------------------------------------------------
c
      if (ok_sync) then
        call histsync(nid_ins)
      endif
      ENDIF
