c$Log$
c =====================================================================
c
c     SUBROUTINE construct_dyeflux
c
c     PURPOSE: constructs a three dimensional array containing
c                 the dyefluxes on the basis of the region arrays
c                 and the structure arrays
c
c     VARIABLES:
c 
c     IN:      through modules
c
c     OUT:     through modules
c
c     AUTHOR:  Nicolas Gruber (gruber@splash.princeton.edu)  
c
c     VERSION: MOM3
c
c     REVISIONS:
c
c     date     author   remarks
c     
c    29.06.99  ng      started coding
c    06.05.03  sf      removed absolute value
c    27.05.03  sf      generalized and added comments for sharing
c      
c =====================================================================
c
 
      subroutine construct_dyeflux       
c    
c
c      implicit none
c
c-----------------------------------------------------------------------
c     arguments
c-----------------------------------------------------------------------
c
c
c-----------------------------------------------------------------------
c     common blocks
c-----------------------------------------------------------------------
c
c
c-----------------------------------------------------------------------
c     local variables
c-----------------------------------------------------------------------
c
      integer  :: i, j, jrw, m, n
      integer  :: lun
      real, allocatable, dimension(:) :: sum
      real, allocatable, dimension(:) :: regionarea
      real, allocatable, dimension(:,:) :: area
      real      :: num_days
      real      :: flux_sum(ndyetrac)
      integer	:: year, month, day, hour, minute, second
c
c =====================================================================
c     begin of executable code
c =====================================================================
c
       print *,  '--CONSTRUCT_DYEFLUX: computing ',
     $     'three dimensional dyeflux array...'
c
c-----------------------------------------------------------------------
c     allocate variables
c-----------------------------------------------------------------------
c
      allocate(sum(0:ndyetrac))
      allocate(regionarea(0:ndyetrac))
      allocate(area(imt,jmt))
c
c-----------------------------------------------------------------------
c     Compute first the regionally integrated fluxes from the pattern
c           file
c     area is cm2, therefore regionarea as well
c-----------------------------------------------------------------------
c
      do m = 0,ndyetrac
         sum(m) = 0.
         regionarea(m) = 0.
      enddo
      
      do jrw = 2,jmtm1
         do i = 2,imtm1
            area(i,jrw) = cst(jrw)*dxt(i)*dyt(jrw)
          do n=1,12             !sum over months per year 
            sum(region(i,jrw)) = sum(region(i,jrw)) + 
     $           (pattern(i,jrw,n)) * area(i,jrw)
          enddo
            regionarea(region(i,jrw)) = regionarea(region(i,jrw)) +
     $           area(i,jrw)
         enddo
      enddo
c
c-----------------------------------------------------------------------
c     checking
c-----------------------------------------------------------------------
c
      do m = 1,ndyetrac
         if (sum(m) .eq. 0.) then
            write(stdout,*) '--CONSTRUCT_DYEFLUX: problem with',
     $           ' the integrated pattern. It is supposed to be',
     $           ' non-zero !'
            stop
         endif
      enddo
            
c
c-----------------------------------------------------------------------
c     get the number of seconds per year
c-----------------------------------------------------------------------
c
      call get_date(model_time,year, month, day, hour, minute, second)
      num_days = days_in_year(model_time)
      nb_seconds_per_year = num_days * 3600 * 24
c     
c-----------------------------------------------------------------------
c     Construct dyeflux array
c       units are mol/cm2/s
c       the fluxes are scaled in such a manner that the total 
c       emissions from one source is 1e18 mol yr-1
c-----------------------------------------------------------------------
c         
      do m = 1,ndyetrac
         do jrw = 2,jmtm1
            do i = 2,imtm1
               if (region(i,jrw) .eq. m) then
                  dyeflux(i,jrw,m) = (pattern(i,jrw,month))
     $                 /sum(m) 
     $                 * 1.e18 / float(nb_seconds_per_year) 
               else
                  dyeflux(i,jrw,m) = c0
               endif
            enddo
         enddo
      enddo
c     
c
c-----------------------------------------------------------------------
c     end of subroutine
c-----------------------------------------------------------------------
c
#endif
      return
      end subroutine  construct_dyeflux  !}
