!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r trans_clean - launches Um_xfer.sh during a job run
*
#include "model_macros_f.h"
*

      subroutine trans_clean 2
*
      implicit none
*
*author   
*     Katja Winger
*
*revision
* v3_11 - Winger K.         - initial version, Dec. 2002
* v3_12 - Winger K.         - Use GETENVC
* v3.20 - Winger K.         - Different behavior following P_clim_clima_L
* v3.30 - Winger K.         - Verify the length of the environement variables
*
*implicits
#include "ptopo.cdk"
#include "path.cdk"
#include "step.cdk"
#include "lctl.cdk"
#include "rstr.cdk"
#include "glb_ld.cdk"
#include "clim.cdk"
*
*modules
**
*
      integer slen_exp, slen_exec, slen_rep, slen_jobn, slen_mod
      integer len_exp, len_exec, len_rep, len_jobn, len_job
      parameter (len_exp=128, len_exec=512, len_rep=128)
      parameter (len_jobn=256, len_job=4096)
      character(len=len_exp)  exper
      character(len=len_exec) execdir
      character(len=len_rep)  reprun
      character(len=len_jobn) jobname
      character(len=len_job)  job
*
      integer i, j, err, unit
      integer steps, rest, cpus
      character Lctl_step_S*9, Rstri_sdon_S*9, cpus_S*3, Step_total_S*9
      character model*10
*
*     ---------------------------------------------------------------
*
* If Step_cleanup equal 0 don't do anything
      if (Step_cleanup.eq.0) return
*
      steps = Step_cleanup
      if (Clim_climat_L) then
        rest = mod(Rstri_sdon,steps)
      else
        rest = mod(Lctl_step,steps)
      end if
      if (rest.eq.0) then
*
* wait for all processors to be here
         call rpn_comm_Barrier("grid", err)
*
* only one process should launch Um_xfer.sh
         if (Ptopo_myproc .eq. 0) then

* get experiment name
            call getenvc('exp',exper)
*
* get model 
            call getenvc('MODEL',model)
*
* get number of cpus
            cpus = Ptopo_npex*Ptopo_npey
*
* get directory names
            call getenvc('REPRUN',reprun)
            call getenvc('EXECDIR',execdir)
*
* convert integer to character
            write (Lctl_step_S  ,'(I8)') Lctl_step
            write (cpus_S       ,'(I3)') cpus
            write (Step_total_S ,'(I8)') Step_total
            Step_total_S = adjustl( Step_total_S )
*
* get real string lengths
            slen_exp  = len_trim( exper   )
            slen_exec = len_trim( execdir )
            slen_mod  = len_trim( model   )
            slen_rep  = len_trim( reprun  )
*
* create jobname
            jobname   = trim( model ) // '_' //
     &                  trim( exper )
            slen_jobn = slen_mod+slen_exp+1
*
* check if names are longer than string lenghs
            if ( slen_exp .ge. len_exp ) then
              print *,' ERROR in trans_clean.f !!!'
              print *,' Your experiment name is longer than its string lengh.'
              print *,' Raise value for parameter len_exp in trans_clean.ftn. '
              call qqexit(1)
            end if
            if ( slen_exec .ge. len_exec ) then
              print *,' ERROR in trans_clean.f !!!'
              print *,' The name of your EXECDIR is longer than its string lengh.'
              print *,' Raise value for parameter len_exec in trans_clean.ftn. '
              call qqexit(1)
            end if
            if ( slen_rep .ge. len_rep ) then
              print *,' ERROR in trans_clean.f !!!'
              print *,' The name of your REPRUN directory is longer than its string lengh.'
              print *,' Raise value for parameter len_rep in trans_clean.ftn. '
              call qqexit(1)
            end if
*
* call Um_xfer.sh         
            job=
     &    ' echo " cd '      // trim( execdir ) // ' ;' //
     &    ' export REPRUN='  // trim( reprun  ) // ' ;' //
     &    ' export EXECDIR=' // trim( execdir ) // ' ;' //
     &    ' export TMPDIR='  // trim( execdir ) // '/xfer_tmpdir ;' //
     &    ' mkdir '          // trim( execdir ) // '/xfer_tmpdir ;' //
     &    ' Um_xfer.sh '     // trim( exper   ) // 
     &    ' -job '           // trim( jobname ) //
     &    ' -endstepno '     // trim( Lctl_step_S ) // 
     &    ' -npe '           // cpus_S  //
     &    ' -status CL  " | /bin/ksh ;' //
     &    ' /bin/rmdir '     // trim( execdir ) // '/xfer_tmpdir'
*
            write (6,*) '!!!!!! job: ', job
            call system(job)
*
         end if
*
* wait for all processors to be here
         call rpn_comm_Barrier("grid", err)
*
      end if
*
*     ---------------------------------------------------------------
*
      return
      end