!--------------------------------------- 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 --------------------------------------


subroutine add_cloudprms(lobsSpaceData) 1,7
!*
!***s/r add_cloudprms
!*
!*Author  : M. Buehner, June 2013
!*
!**    Purpose: Add cloud parameters to the burp file for infrared satellite radiances.
!*
  use obsSpaceData_mod
  use mpivar_mod
  use tovs_nl_mod ,only : nsensors,platform,satellite,instrument
  use burpFiles_mod
  implicit none

  type(struct_obs) :: lobsSpaceData
  integer jfile,krtid

  character(len=256) :: BURP_SPLIT_VAR
  integer length_burp_split, ier
  logical :: ldone=.false.

  ier = 0
  call get_environment_variable('ARMA_BURP_SPLIT',BURP_SPLIT_VAR,length_burp_split,ier,.true.)
  if (ier.gt.1) then
     write(*,*) 'add_cloudprms: Problem when getting the environment variable ARMA_BURP_SPLIT'
  end if
  if (ier.eq.1) then
     write(*,*) 'add_cloudprms: The environment variable ARMA_BURP_SPLIT has not been detected so we read global observation files'
     ! if files not split then modification of burp file is only done on processor 0
     IF(mpi_myid /= 0) RETURN
  else
     write(*,*) 'add_cloudprms: The environment variable ARMA_BURP_SPLIT has been detected so we read splitted observation files'
  end if

  do jfile=1,burp_nfiles
    do krtid = 1, nsensors

      ! For AIRS data
       if( platform(krtid) .eq. 9 .and.  &
            instrument(krtid) .eq. 11 ) then
        if(jfile == 1) then
          write(*,*) 'INPUT FILE TO AIRSABRP = ', burp_cfilnam(jfile)
          call hir_cldprm_to_brp(lobsspacedata,burp_cfilnam(jfile))
        else
          write(*,*) 'ERROR! THERE SEEM TO BE MORE THAN ONE AIRS FILE ???'
        endif
      endif

      ! For IASI data
      if( platform(krtid) .eq. 10 .and.  &
          instrument(krtid) .eq. 16 ) then
        if(jfile == 1) then
         if (.not. ldone) then
             write(*,*) 'INPUT FILE TO IASIABRP = ', burp_cfilnam(jfile)
             call hir_cldprm_to_brp(lobsspacedata,burp_cfilnam(jfile))
             ldone=.true.
         endif 
        else
          write(*,*) 'ERROR! THERE SEEM TO BE MORE THAN ONE IASI FILE ???'
        endif
      endif

      ! For CrIS data
      if( platform(krtid) .eq. 17 .and. &
          instrument(krtid) .eq. 27 ) then
        if(jfile == 1) then
         if (.not. ldone) then
            write(*,*) 'INPUT FILE TO CRISABRP = ', burp_cfilnam(jfile)
            call hir_cldprm_to_brp(lobsspacedata,burp_cfilnam(jfile))
            ldone=.true.
          endif
        else
          write(*,*) 'ERROR! THERE SEEM TO BE MORE THAN ONE CRIS FILE ???'
        endif
      endif

    enddo
  enddo

end subroutine add_cloudprms