SUBROUTINE CMIP6_STRATO_AERO

!     PURPOSE.
!     --------
!     Read stratospheric aerosol from CMIP6

!     INTERFACE.
!     ----------
!     CALL *CMIP6_STRATO_AERO* FROM *UPDTIM*

!     AUTHOR.
!     -------
!     M.Ménégoz for EC-EARTH 2017
!
!     Reading the CMIP6 stratospheric aerosol dataset.
!     The vertical interpolation of this data is done off-line.
!     Horizontal and time interpolation are done in
!     cmip6_strato_aero_interp.F90 and cmip6_strato_aero_process.F90,
!     called from radlswr.F90
!     The data is read with a broadcast approach. It could be
!     eventually parallelised?
!     Forcing in the SW (AOD_SUN, AAOD_SUN, REF_AOD_SUN) and in the LW
!     (AAOD_EARTH)

!     MODIFICATIONS.
!     --------------

!---------------------------------------------------------------------

! Modules
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMLUN    ,ONLY : NULOUT, NULERR
USE YOMRIP    ,ONLY : NINDAT, NSTADD
USE YOERAD    ,ONLY : FIRSTYEAR,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME, &
                    & STRATO_CMIP6_ALT,STRATO_CMIP6_LAT,STRATO_CMIP6_SB,STRATO_CMIP6_TB,&
                    & STRATO_CMIP6_NSB,STRATO_CMIP6_NTB, &
                    & AOD_SUN, AAOD_SUN,REFAOD_SUN, &
                    & AOD_SUN_MEAN, AAOD_SUN_MEAN,REFAOD_SUN_MEAN, &
                    & CHECK_SUN_MEAN, CHECK_EARTH_MEAN, &
                    & AAOD_EARTH_MEAN, &
                    & STRATO_CMIP6_NMONTH, &
                    & AAOD_EARTH, &
                    & LSTRATAERO_UPDATED, CCMIP6_STRAT_FULL, &
                    & NVOLCSTOP,LCMIP6_STRATAER_BCKGD
USE YOEAERC  , ONLY : RVOLCDEC
USE YOMMP      ,ONLY : MYPROC
USE MPL_MODULE ,ONLY : MPL_BROADCAST

IMPLICIT NONE

! Local variables:
INTEGER                      :: IUNIT,FORC_MONTH
INTEGER(KIND=JPIM)           :: ILAT, ILEV, ILON, IMONTH,IB, JLEV
INTEGER(KIND=JPIM)           :: IJ0, IM0, IA0, IDAY, IMON, IMODYR, ILMOIS(12)
INTEGER(KIND=JPIM)           :: IYR1, IMV1
INTEGER(KIND=JPIM),SAVE      :: IMONOLD=-999
INTEGER(KIND=JPIM), PARAMETER :: ITAG = 34567
INTEGER(KIND=JPIM), PARAMETER :: RPRC = 1

REAL(KIND=JPRB) :: TIME(STRATO_CMIP6_NTIME)
CHARACTER (LEN=*), PARAMETER :: LAT_NAME='latitude'
CHARACTER (LEN=*), PARAMETER :: ALT_NAME='altitude'
CHARACTER (LEN=*), PARAMETER :: TIME_NAME='month'
CHARACTER (LEN=*), PARAMETER :: SB_NAME='solar_bands'
CHARACTER (LEN=*), PARAMETER :: TB_NAME='terrestrial_bands'
INTEGER :: LAT_VARID, ALT_VARID, TIME_VARID, SB_VARID, TB_VARID, IOS
LOGICAL :: LLOPEN,LLSTRAT

! Triplets of variable for SW and only absorption for LW (because IFS
! is currently not considering aerosol scattering in the LW):

CHARACTER (LEN=*), PARAMETER :: AOD_SUN_NAME='AOD_sun'
INTEGER :: AOD_SUN_VARID
REAL(KIND=JPRB), ALLOCATABLE :: AOD_SUN_ORIG(:,:,:,:)

CHARACTER (LEN=*), PARAMETER :: AAOD_SUN_NAME='AAOD_sun'
INTEGER :: AAOD_SUN_VARID
REAL(KIND=JPRB), ALLOCATABLE :: AAOD_SUN_ORIG(:,:,:,:)

CHARACTER (LEN=*), PARAMETER :: REFAOD_SUN_NAME='REFAOD_sun'
INTEGER :: REFAOD_SUN_VARID
REAL(KIND=JPRB), ALLOCATABLE :: REFAOD_SUN_ORIG(:,:,:,:)

CHARACTER (LEN=*), PARAMETER :: AAOD_EARTH_NAME='AAOD_earth'
INTEGER :: AAOD_EARTH_VARID
REAL(KIND=JPRB), ALLOCATABLE :: AAOD_EARTH_ORIG(:,:,:,:)


! Read the nectdf data with an adequate format and order.
INTEGER, DIMENSION(4) :: START, FINAL, FINAL_LW, FINAL_MEAN, FINAL_LW_MEAN

#include "netcdf.inc"
#include "abor1.intfb.h"
#include "updcal.intfb.h"
#include "fcttim.h"

WRITE(NULOUT,*)'Entering Subroutine CMIP6_STRATO_AERO'

! What is the date please?
IJ0=NDD(NINDAT)
IM0=NMM(NINDAT)
IA0=NCCAA(NINDAT)
CALL UPDCAL(IJ0,IM0,IA0,NSTADD,IDAY,IMON,IMODYR,ILMOIS,NULOUT)

! 2-year exponential decay of volcanic aerosols after NVOLCSTOP=YYYYMM
IF ((100*IMODYR+IMON).GE.NVOLCSTOP) THEN
  IYR1=INT(NVOLCSTOP/100)
  IMV1=NVOLCSTOP-100*IYR1
  RVOLCDEC=EXP(-(REAL(IMODYR-IYR1)+REAL(IMON-IMV1)/12.)/2.)
  IMODYR=IYR1
  IMON=IMV1
  IDAY=16
  WRITE(NULOUT,*) 'CMIP6_STRATO_AERO.F90: NVOLCSTOP',NVOLCSTOP
  WRITE(NULOUT,*) 'CMIP6_STRATO_AERO.F90: RVOLCDEC=',RVOLCDEC
  WRITE(NULOUT,*) 'CMIP6_STRATO_AERO.F90: LCMIP6_STRATAER_BCKGD=',LCMIP6_STRATAER_BCKGD
ELSE
  RVOLCDEC=1.
ENDIF

! Open the file only if we begin a new month and I am the master process
IF (IMON/=IMONOLD) THEN
    IMONOLD=IMON
   
  IF (MYPROC==RPRC) THEN

    ! Forcing file includes 1980 months since 01/1850
    FORC_MONTH=(IMODYR-FIRSTYEAR)*12+IMON
    WRITE(NULOUT,*)'CMIP6_STRATO_AERO; FORCING MONTH = ',FORC_MONTH,'/1980, starting from the year 1850'

    WRITE(NULOUT,*) 'CCMIP6_STRAT_FULL: ',TRIM(CCMIP6_STRAT_FULL)

    LLOPEN=.FALSE.
    IOS= 0
    INQUIRE(FILE=CCMIP6_STRAT_FULL,IOSTAT=IOS,EXIST=LLSTRAT,OPENED=LLOPEN)
    LLSTRAT = LLSTRAT .AND. (IOS == 0) .AND. .NOT.LLOPEN
    
    IF (.NOT.LLSTRAT) THEN
      WRITE(NULERR,'('' ERROR IN CMIP6_STRATO_AERO.F90 :'',&
       & '' THE FORCING FILE IS NOT AVAILABLE !'')')
      CALL ABOR1('CMIP6_STRATO_AERO')
    ENDIF

    IF (IMODYR>=1850 .AND. IMODYR<=2014 .AND. .NOT. LCMIP6_STRATAER_BCKGD .AND. FORC_MONTH/=1 .AND. FORC_MONTH/=STRATO_CMIP6_NTIME) THEN

      START=(/1, 1, 1, FORC_MONTH-1/)
      FINAL=(/STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH/)
      FINAL_LW=(/STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH/)
    
      ! Allocate temporary arrays
      ALLOCATE(AOD_SUN_ORIG   (STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH))
      ALLOCATE(AAOD_SUN_ORIG  (STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH))
      ALLOCATE(REFAOD_SUN_ORIG(STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH))
      ALLOCATE(AAOD_EARTH_ORIG(STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NMONTH))

      ! Initialise arrays:
      AOD_SUN_ORIG(:,:,:,:)=0.
      AAOD_SUN_ORIG(:,:,:,:)=0.
      REFAOD_SUN_ORIG(:,:,:,:)=0.
      AAOD_EARTH_ORIG(:,:,:,:)=0.
    
      ! OPEN NETCDF FILE
      CALL CHECK( NF_OPEN(CCMIP6_STRAT_FULL,NF_NOWRITE,IUNIT) )
  
      ! Reading latitudes needed for the interpolation.
      CALL CHECK( NF_INQ_VARID(IUNIT, LAT_NAME, LAT_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, LAT_VARID, 1, STRATO_CMIP6_NLAT, STRATO_CMIP6_LAT) )
      
      ! READ 4-D FIELDS
      CALL CHECK( NF_INQ_VARID(IUNIT, AOD_SUN_NAME, AOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AOD_SUN_VARID, START, FINAL, AOD_SUN_ORIG) )
  
      CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_SUN_NAME, AAOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_SUN_VARID, START, FINAL, AAOD_SUN_ORIG) )
  
      CALL CHECK( NF_INQ_VARID(IUNIT, REFAOD_SUN_NAME, REFAOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, REFAOD_SUN_VARID, START, FINAL, REFAOD_SUN_ORIG) )
  
      CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_EARTH_NAME, AAOD_EARTH_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_EARTH_VARID, START, FINAL_LW, AAOD_EARTH_ORIG) )
  
      ! CLOSE NETCDF FILE
      CALL CHECK( NF_CLOSE(IUNIT) )
  
      ! Fill the data for the current month and reorder the arrays
      DO ILEV=1,STRATO_CMIP6_NALT
        JLEV=STRATO_CMIP6_NALT-ILEV+1
        DO ILAT=1,STRATO_CMIP6_NLAT
          ! SW parameters
          DO IB=1,STRATO_CMIP6_NSB
            ! We keep three months to make a smoothed forcing (linear interpolation along the axis time)
            DO IMONTH=1,STRATO_CMIP6_NMONTH
              AOD_SUN(ILEV,ILAT,IB,IMONTH)=AOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH)
              AAOD_SUN(ILEV,ILAT,IB,IMONTH)=AAOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH)
              REFAOD_SUN(ILEV,ILAT,IB,IMONTH)=REFAOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH)
            ENDDO
          ENDDO
          ! LW parameter
          DO IB=1,STRATO_CMIP6_NTB
            DO IMONTH=1,STRATO_CMIP6_NMONTH
              AAOD_EARTH(ILEV,ILAT,IB,IMONTH)=AAOD_EARTH_ORIG(IB,ILAT,JLEV,IMONTH)
            ENDDO
          ENDDO
        ENDDO
      ENDDO
      
      DEALLOCATE(AOD_SUN_ORIG   )
      DEALLOCATE(AAOD_SUN_ORIG  )
      DEALLOCATE(REFAOD_SUN_ORIG)
      DEALLOCATE(AAOD_EARTH_ORIG)

    ELSE IF (.NOT. LCMIP6_STRATAER_BCKGD .AND. (FORC_MONTH==1 .OR. FORC_MONTH==STRATO_CMIP6_NTIME)) THEN

      WRITE(NULOUT,*)'CMIP6_STRATO_AERO; computing climatological stratospherical forcing'

      START=(/1, 1, 1, 1/)
      FINAL=(/STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME/)
      FINAL_LW=(/STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME/)
    
      ! Allocate temporary arrays
      ALLOCATE(AOD_SUN_ORIG (STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
      ALLOCATE(AAOD_SUN_ORIG(STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
      ALLOCATE(REFAOD_SUN_ORIG(STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
      ALLOCATE(AAOD_EARTH_ORIG(STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))

      ! Initialise arrays:
      AOD_SUN_ORIG(:,:,:,:)=0.
      AAOD_SUN_ORIG(:,:,:,:)=0.
      REFAOD_SUN_ORIG(:,:,:,:)=0.
      AAOD_EARTH_ORIG(:,:,:,:)=0.
    
      ! OPEN NETCDF FILE
      CALL CHECK( NF_OPEN(CCMIP6_STRAT_FULL,NF_NOWRITE,IUNIT) )
  
      ! Reading latitudes needed for the interpolation.
      CALL CHECK( NF_INQ_VARID(IUNIT, LAT_NAME, LAT_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, LAT_VARID, 1, STRATO_CMIP6_NLAT, STRATO_CMIP6_LAT) )
    
      ! READ 4-D FIELDS
      CALL CHECK( NF_INQ_VARID(IUNIT, AOD_SUN_NAME, AOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AOD_SUN_VARID, START, FINAL, AOD_SUN_ORIG) )
   
      CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_SUN_NAME, AAOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_SUN_VARID, START, FINAL, AAOD_SUN_ORIG) )
   
      CALL CHECK( NF_INQ_VARID(IUNIT, REFAOD_SUN_NAME, REFAOD_SUN_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, REFAOD_SUN_VARID, START, FINAL, REFAOD_SUN_ORIG) )
  
      CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_EARTH_NAME, AAOD_EARTH_VARID) )
      CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_EARTH_VARID, START, FINAL_LW, AAOD_EARTH_ORIG) )
  
      ! CLOSE NETCDF FILE
      CALL CHECK( NF_CLOSE(IUNIT) )
 
      ! Climatology computed on-line, only one time.
      WRITE(NULOUT,*)'CMIP6_STRATO_AERO; transition between climatological and time-resolved stratospherical forcing'
      DO ILEV=1,STRATO_CMIP6_NALT
        JLEV=STRATO_CMIP6_NALT-ILEV+1
        DO ILAT=1,STRATO_CMIP6_NLAT
          ! SW parameters
          DO IB=1,STRATO_CMIP6_NSB

            AOD_SUN_MEAN(ILEV,ILAT,IB)=SUM(AOD_SUN_ORIG(IB,ILAT,JLEV,:))/STRATO_CMIP6_NTIME
            AAOD_SUN_MEAN(ILEV,ILAT,IB)=SUM(AAOD_SUN_ORIG(IB,ILAT,JLEV,:))/STRATO_CMIP6_NTIME
            REFAOD_SUN_MEAN(ILEV,ILAT,IB)=SUM(REFAOD_SUN_ORIG(IB,ILAT,JLEV,:))/STRATO_CMIP6_NTIME

            IF (FORC_MONTH==1) THEN

              AOD_SUN(ILEV,ILAT,IB,1)=AOD_SUN_MEAN(ILEV,ILAT,IB)
              AAOD_SUN(ILEV,ILAT,IB,1)=AAOD_SUN_MEAN(ILEV,ILAT,IB)
              REFAOD_SUN(ILEV,ILAT,IB,1)=REFAOD_SUN_MEAN(ILEV,ILAT,IB)

              DO IMONTH=2,STRATO_CMIP6_NMONTH
                AOD_SUN(ILEV,ILAT,IB,IMONTH)=AOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH-1)
                AAOD_SUN(ILEV,ILAT,IB,IMONTH)=AAOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH-1)
                REFAOD_SUN(ILEV,ILAT,IB,IMONTH)=REFAOD_SUN_ORIG(IB,ILAT,JLEV,IMONTH-1)
              ENDDO

            ELSE IF (FORC_MONTH==STRATO_CMIP6_NTIME) THEN

              AOD_SUN(ILEV,ILAT,IB,STRATO_CMIP6_NMONTH)=AOD_SUN_MEAN(ILEV,ILAT,IB)
              AAOD_SUN(ILEV,ILAT,IB,STRATO_CMIP6_NMONTH)=AAOD_SUN_MEAN(ILEV,ILAT,IB)
              REFAOD_SUN(ILEV,ILAT,IB,STRATO_CMIP6_NMONTH)=REFAOD_SUN_MEAN(ILEV,ILAT,IB)
              DO IMONTH=1,STRATO_CMIP6_NMONTH-1
                AOD_SUN(ILEV,ILAT,IB,IMONTH)=AOD_SUN_ORIG(IB,ILAT,JLEV,STRATO_CMIP6_NTIME+IMONTH-2)
                AAOD_SUN(ILEV,ILAT,IB,IMONTH)=AAOD_SUN_ORIG(IB,ILAT,JLEV,STRATO_CMIP6_NTIME+IMONTH-2)
                REFAOD_SUN(ILEV,ILAT,IB,IMONTH)=REFAOD_SUN_ORIG(IB,ILAT,JLEV,STRATO_CMIP6_NTIME+IMONTH-2)
              ENDDO

            ENDIF
          ENDDO

          ! LW parameters
          DO IB=1,STRATO_CMIP6_NTB
            AAOD_EARTH_MEAN(ILEV,ILAT,IB)=SUM(AAOD_EARTH_ORIG(IB,ILAT,JLEV,:))/STRATO_CMIP6_NTIME
            IF (FORC_MONTH==1) THEN
              AAOD_EARTH(ILEV,ILAT,IB,1)=AAOD_EARTH_MEAN(ILEV,ILAT,IB)
              DO IMONTH=2,STRATO_CMIP6_NMONTH
                AAOD_EARTH(ILEV,ILAT,IB,IMONTH)=AAOD_EARTH_ORIG(IB,ILAT,JLEV,IMONTH-1)
              ENDDO
            ELSE IF (FORC_MONTH==STRATO_CMIP6_NTIME) THEN
              AAOD_EARTH(ILEV,ILAT,IB,STRATO_CMIP6_NMONTH)=AAOD_EARTH_MEAN(ILEV,ILAT,IB)
              DO IMONTH=1,STRATO_CMIP6_NMONTH-1
                AAOD_EARTH(ILEV,ILAT,IB,IMONTH)=AAOD_EARTH_ORIG(IB,ILAT,JLEV,STRATO_CMIP6_NTIME+IMONTH-2)
              ENDDO
            ENDIF
          ENDDO

        ENDDO
      ENDDO

      DEALLOCATE(AOD_SUN_ORIG   )
      DEALLOCATE(AAOD_SUN_ORIG  )
      DEALLOCATE(REFAOD_SUN_ORIG)
      DEALLOCATE(AAOD_EARTH_ORIG)
  
    ELSE !(IMODYR<1850 .OR. IMODYR>2014 .OR. LCMIP6_STRATAER_BCKGD)     

      IF (MINVAL(AOD_SUN_MEAN)==CHECK_SUN_MEAN .OR. MINVAL(AAOD_SUN_MEAN)==CHECK_SUN_MEAN &
        & .OR. MINVAL(REFAOD_SUN_MEAN)==CHECK_SUN_MEAN .OR. MINVAL(AAOD_EARTH_MEAN)==CHECK_EARTH_MEAN) THEN
     
        WRITE(NULOUT,*)'CMIP6_STRATO_AERO; computing climatological stratospherical forcing'

        START=(/1, 1, 1, 1/)
        FINAL=(/STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME/)
        FINAL_LW=(/STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME/)
    
        ! Allocate temporary arrays
        ALLOCATE(AOD_SUN_ORIG (STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
        ALLOCATE(AAOD_SUN_ORIG(STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
        ALLOCATE(REFAOD_SUN_ORIG(STRATO_CMIP6_NSB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))
        ALLOCATE(AAOD_EARTH_ORIG(STRATO_CMIP6_NTB,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME))

        ! Initialise arrays:
        AOD_SUN_ORIG(:,:,:,:)=0.
        AAOD_SUN_ORIG(:,:,:,:)=0.
        REFAOD_SUN_ORIG(:,:,:,:)=0.
        AAOD_EARTH_ORIG(:,:,:,:)=0.
    
        ! OPEN NETCDF FILE
        CALL CHECK( NF_OPEN(CCMIP6_STRAT_FULL,NF_NOWRITE,IUNIT) )
  
        ! Reading latitudes needed for the interpolation.
        CALL CHECK( NF_INQ_VARID(IUNIT, LAT_NAME, LAT_VARID) )
        CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, LAT_VARID, 1, STRATO_CMIP6_NLAT, STRATO_CMIP6_LAT) )
      
        ! READ 4-D FIELDS
        CALL CHECK( NF_INQ_VARID(IUNIT, AOD_SUN_NAME, AOD_SUN_VARID) )
        CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AOD_SUN_VARID, START, FINAL, AOD_SUN_ORIG) )
   
        CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_SUN_NAME, AAOD_SUN_VARID) )
        CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_SUN_VARID, START, FINAL, AAOD_SUN_ORIG) )
   
        CALL CHECK( NF_INQ_VARID(IUNIT, REFAOD_SUN_NAME, REFAOD_SUN_VARID) )
        CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, REFAOD_SUN_VARID, START, FINAL, REFAOD_SUN_ORIG) )
  
        CALL CHECK( NF_INQ_VARID(IUNIT, AAOD_EARTH_NAME, AAOD_EARTH_VARID) )
        CALL CHECK( NF_GET_VARA_DOUBLE(IUNIT, AAOD_EARTH_VARID, START, FINAL_LW, AAOD_EARTH_ORIG) )
  
        ! CLOSE NETCDF FILE
        CALL CHECK( NF_CLOSE(IUNIT) )
 
        ! Climatology computed on-line, only one time.
        DO ILEV=1,STRATO_CMIP6_NALT
          JLEV=STRATO_CMIP6_NALT-ILEV+1
          DO ILAT=1,STRATO_CMIP6_NLAT
            ! SW parameters
            DO IB=1,STRATO_CMIP6_NSB
              AOD_SUN_MEAN(JLEV,ILAT,IB)=SUM(AOD_SUN_ORIG(IB,ILAT,ILEV,:))/STRATO_CMIP6_NTIME
              AAOD_SUN_MEAN(JLEV,ILAT,IB)=SUM(AAOD_SUN_ORIG(IB,ILAT,ILEV,:))/STRATO_CMIP6_NTIME
              REFAOD_SUN_MEAN(JLEV,ILAT,IB)=SUM(REFAOD_SUN_ORIG(IB,ILAT,ILEV,:))/STRATO_CMIP6_NTIME
            ENDDO
            ! LW parameters
            DO IB=1,STRATO_CMIP6_NTB
              AAOD_EARTH_MEAN(JLEV,ILAT,IB)=SUM(AAOD_EARTH_ORIG(IB,ILAT,ILEV,:))/STRATO_CMIP6_NTIME
            ENDDO
          ENDDO
        ENDDO

        DEALLOCATE(AOD_SUN_ORIG   )
        DEALLOCATE(AAOD_SUN_ORIG  )
        DEALLOCATE(REFAOD_SUN_ORIG)
        DEALLOCATE(AAOD_EARTH_ORIG)
  
      ENDIF

      WRITE(NULOUT,*)'CMIP6_STRATO_AERO; using climatological stratospherical forcing'
      DO IMONTH=1,STRATO_CMIP6_NMONTH
        ! SW parameters
        AOD_SUN(:,:,:,IMONTH)=AOD_SUN_MEAN(:,:,:)
        AAOD_SUN(:,:,:,IMONTH)=AAOD_SUN_MEAN(:,:,:)
        REFAOD_SUN(:,:,:,IMONTH)=REFAOD_SUN_MEAN(:,:,:)
        ! LW parameters
        AAOD_EARTH(:,:,:,IMONTH)=AAOD_EARTH_MEAN(:,:,:)
      ENDDO

    ENDIF !(IMODYR>=1850 .AND. IMODYR<=2014 .AND. .NOT. LCMIP6_STRATAER_BCKGD)
  
  ENDIF ! (MYPROC==RPRC)
  
  CALL BROADCAST_AER_CMIP6_PROP
  LSTRATAERO_UPDATED = .TRUE.

ELSE ! (IMON/=IMONOLD)

  WRITE(NULOUT,*)'Same month in CMIP6_STRATO_AERO -> nothing to update '
  LSTRATAERO_UPDATED = .FALSE.

ENDIF

CONTAINS

  SUBROUTINE CHECK(STATUS)
    INTEGER, INTENT ( IN) :: STATUS

    IF(STATUS /= NF_NOERR) THEN
      CALL ABOR1('CMIP6_STRATO_AERO: '//TRIM(NF_STRERROR(STATUS)))
    END IF
  END SUBROUTINE CHECK

!--------------------------------------------------------------------------------------------------------------

 SUBROUTINE BROADCAST_AER_CMIP6_PROP
    !--- inherited types, functions and data
    USE YOMHOOK, ONLY    : LHOOK, DR_HOOK
    USE MPL_MODULE, ONLY : MPL_BROADCAST

    USE YOERAD    ,ONLY : FIRSTYEAR,STRATO_CMIP6_NLAT,STRATO_CMIP6_NALT,STRATO_CMIP6_NTIME, &
                    & STRATO_CMIP6_ALT,STRATO_CMIP6_LAT,STRATO_CMIP6_SB,STRATO_CMIP6_TB,&
                    & STRATO_CMIP6_NSB,STRATO_CMIP6_NTB, &
                    & AOD_SUN, AAOD_SUN,REFAOD_SUN, &
                    & AAOD_EARTH

    IMPLICIT NONE

    !---local data
    REAL(KIND=JPRB) :: ZHOOK_HANDLE

    !---executable procedure
    IF (LHOOK) CALL DR_HOOK('BROADCAST_AER_CMIP6_PROP',0,ZHOOK_HANDLE)

    CALL MPL_BROADCAST(AOD_SUN,KTAG=ITAG+1,KROOT=RPRC,CDSTRING='AER_CMIP6_PROP-1: ')
    CALL MPL_BROADCAST(AAOD_SUN,KTAG=ITAG+2,KROOT=RPRC,CDSTRING='AER_CMIP6_PROP-2: ')
    CALL MPL_BROADCAST(REFAOD_SUN,KTAG=ITAG+3,KROOT=RPRC,CDSTRING='AER_CMIP6_PROP-3: ')
    CALL MPL_BROADCAST(AAOD_EARTH,KTAG=ITAG+4,KROOT=RPRC,CDSTRING='AER_CMIP6_PROP-4: ')
    CALL MPL_BROADCAST(STRATO_CMIP6_LAT,KTAG=ITAG+5,KROOT=RPRC,CDSTRING='AER_CMIP6_PROP-5: ')

    IF (LHOOK) CALL DR_HOOK('BROADCAST_AER_CMIP6_PROP',1,ZHOOK_HANDLE)
  END SUBROUTINE BROADCAST_AER_CMIP6_PROP

END SUBROUTINE CMIP6_STRATO_AERO
