!  FSPLIT - split the assimilation grid and corresponding observations into
!  strips so that the pieces can be analyzed on separate
!  processors.  This manual splitting provides perfect speedup, modulo
!  the disk and memory traffic to send the data to the cpus.
!
!  Modification history:
!  10/11/04 - version to accommodate new model grid format.  Eliminated
!             option for longitudinal splitting.
!  10/17/03 - version to optionally split along longitudes for
!             testing/compatibility with old versions of the data assimilation
!             code
!  08/31/03 - version to split along latitudes instead of longitudes
!             and to add support for centering patches at poles.
!
    module fsplit
    use grid_io
    use split_defs
    use command_line_support
    contains
!-----------------------------------------------------------------------
    subroutine grid_split(egrid,gridname,nsol,nsplit,ywin,opthdr)
!  GRID_SPLIT - split GRID into NSPLIT latitudinal strips and create
!  a family of output files.
!  If NSPLIT > 1, then gridname is a template name.
!  If OPTHDR is nonblank, then we write use it as the output file header
!  when NSPLIT > 1.
!
    implicit none
    integer,intent(in)::nsol,ywin
    real(GRIDKIND),intent(in)::egrid(LONF,NAVARS,LATG,LEVS,nsol)
    character(*),intent(in)::gridname,opthdr
    integer,intent(inout)::nsplit
!
!  Local variables
!
    character(len(gridname)+6)::gname
    character(4)::hdr
    integer::n,nstrips
!
!  SOUTH(k) is the southernmost latitude contained in the split grid file
!  sent to the kth processor; similarly for NORTH(k).  ISTART(k) is the
!  southernmost latitude at which the data assimilation should begin.
!  We need a separate index because the latitude strips overlap near the
!  poles.
!
    integer,allocatable::list(:),south(:),north(:),psouth(:),pnorth(:)
!
!  Decide how to partition the data.
!
    nstrips=LATG
    call imod(nsplit,list,nstrips,ywin,south,north,psouth,pnorth)
    if(sum(list(1:nsplit)).ne.nstrips) &
       call fabort('grid_split: internal error splitting latitudes')
    if(verbosity().gt.VERB_TERSE) then
       write(LOGFILE,115) nsplit,list(1:nsplit)
    endif
115 format(' number of processors used: ',i4/ &
       ' latitudes per processor: '/(' ',19i4))
!
!  Create one file for each processor.  If NSPLIT=1, just write out the grid
!  without any name decoration or header if there's no splitting necessary.
!  Otherwise, use gridname as a template and let grid_manager handle
!  the output.
!
    if(nsplit.eq.1) then
       call write_grid_slice(gridname,'none',1,LATG,egrid,1,LATG,nsol,1,LATG)
    else 
       if(verbosity().gt.VERB_SILENT) write(LOGFILE,130) seqid()
130    format('grid_split: sequence id: ',i0)
       if(len_trim(opthdr).gt.0) then
          hdr=opthdr
       else
          hdr='grid'
       endif
       do n=1,nsplit
          call numeric_filename(gridname,'%n',n,gname)
          if(verbosity().gt.VERB_TERSE) &
             write(LOGFILE,131) trim(gname),south(n),north(n),psouth(n), &
                pnorth(n),nsol
          call write_grid_slice(gname,hdr,south(n),north(n), &
             egrid,1,LATG,nsol,psouth(n),pnorth(n))
       enddo
131    format('grid_split: writing ',a,' south:',i3,' north:',i3, &
          ' psouth:',i3,' pnorth:',i3,' nsol: ',i3)
    endif
    return
    end subroutine grid_split
!-------------------------------
    end module fsplit
