LCOV - code coverage report
Current view: | top level - chemistry/utils - solar_euv_data.F90 (source / functions) |  |  | Hit | Total | Coverage
Test: | coverage.info |  | Lines: | 5 | 52 | 9.6 %
Date: | 2024-12-17 22:39:59 |  | Functions: | 2 | 2 | 100.0 %

              Line data    Source code
           1             : !-----------------------------------------------------------------------
           2             : ! Solar EUV irradiance data
           3             : !-----------------------------------------------------------------------
           4             : module solar_euv_data
           5             :   use shr_kind_mod,     only: r8 => shr_kind_r8
           6             :   use spmd_utils,       only: masterproc
           7             :   use cam_abortutils,   only: endrun
           8             :   use cam_pio_utils,    only: cam_pio_openfile
           9             :   use cam_logfile,      only: iulog
          10             :   use pio,              only: pio_get_var, pio_inq_varid, pio_inq_dimid, pio_inq_dimlen, &
          11             :                               file_desc_t
          12             :   use input_data_utils, only : time_coordinate
          13             :
          14             :   implicit none
          15             :
          16             :   save
          17             :   private
          18             :   public :: solar_euv_init
          19             :   public :: solar_euv_advance
          20             :   public :: solar_euv_data_etf
          21             :   public :: solar_euv_data_active
          22             :
          23             :   real(r8), target, allocatable :: solar_euv_data_etf(:)
          24             :   logical, protected :: solar_euv_data_active = .false.
          25             :
          26             :   integer :: nbins
          27             :   real(r8), allocatable :: irradi(:,:)
          28             :
          29             :   type(file_desc_t) :: file_id
          30             :   integer :: ssi_vid
          31             :
          32             :   logical :: initialized = .false.
          33             :
          34             :   real(r8), allocatable :: dellam(:)
          35             :   real(r8), allocatable :: lambda(:)
          36             :   real(r8), allocatable :: we(:)
          37             :
          38             :   integer, parameter :: nrecords = 2
          39             :   logical, parameter :: debug = .false.
          40             :
          41             :   type(time_coordinate) :: time_coord
          42             :
          43             : contains
          44             :
          45             : !-----------------------------------------------------------------------
          46             : !-----------------------------------------------------------------------
          47        1536 :   subroutine solar_euv_init(filepath, fixed, fixed_ymd, fixed_tod)
          48             :
          49             :     use ioFileMod, only : getfil
          50             :
          51             :     ! arguments
          52             :     character(len=*), intent(in) :: filepath
          53             :     logical, intent(in) :: fixed
          54             :     integer, intent(in) :: fixed_ymd
          55             :     integer, intent(in) :: fixed_tod
          56             :
          57             :     ! local variables
          58             :     integer :: astat, dimid, vid
          59             :     character(len=256) :: filen
          60             :
          61             :     integer :: ierr
          62             :
          63        1536 :     solar_euv_data_active = (filepath.ne.'NONE')
          64        1536 :     if ( .not.solar_euv_data_active ) return
          65             :
          66           0 :     call time_coord%initialize( filepath, fixed=fixed, fixed_ymd=fixed_ymd, fixed_tod=fixed_tod )
          67             :
          68           0 :     call getfil( filepath, filen, 0 )
          69           0 :     call cam_pio_openfile( file_id, filen, 0 )
          70             :
          71           0 :     if(masterproc)  write(iulog,*)'solar_euv_data_init: data file = ',trim(filen)
          72             :
          73           0 :     ierr = pio_inq_varid( file_id, 'ssi', ssi_vid )
          74           0 :     ierr = pio_inq_dimid( file_id, 'bin', dimid )
          75           0 :     ierr = pio_inq_dimlen( file_id, dimid, nbins )
          76             :
          77           0 :     allocate(irradi(nbins,nrecords), stat=astat )
          78           0 :     if( astat /= 0 ) then
          79           0 :        write(iulog,*) 'solar_euv_data_init: failed to allocate irradi; error = ',astat
          80           0 :        call endrun('solar_data_init')
          81             :     end if
          82             :
          83           0 :     allocate(lambda(nbins), stat=astat )
          84           0 :     if( astat /= 0 ) then
          85           0 :        write(iulog,*) 'solar_euv_data_init: failed to allocate lambda; error = ',astat
          86           0 :        call endrun('solar_euv_data_init')
          87             :     end if
          88           0 :     allocate(dellam(nbins), stat=astat )
          89           0 :     if( astat /= 0 ) then
          90           0 :        write(iulog,*) 'solar_euv_data_init: failed to allocate dellam; error = ',astat
          91           0 :        call endrun('solar_euv_data_init')
          92             :     end if
          93           0 :     allocate(solar_euv_data_etf(nbins), stat=astat )
          94           0 :     if( astat /= 0 ) then
          95           0 :        write(iulog,*) 'solar_euv_data_init: failed to allocate solar_euv_data_etf; error = ',astat
          96           0 :        call endrun('solar_euv_data_init')
          97             :     end if
          98             :
          99           0 :     ierr = pio_inq_varid( file_id, 'wavelength', vid )
         100           0 :     ierr = pio_get_var( file_id, vid, lambda )
         101           0 :     ierr = pio_inq_varid( file_id, 'band_width', vid  )
         102           0 :     ierr = pio_get_var( file_id, vid, dellam )
         103             :
         104           0 :     allocate(we(nbins+1), stat=astat )
         105           0 :     if( astat /= 0 ) then
         106           0 :        write(iulog,*) 'solar_euv_data_init: failed to allocate we; error = ',astat
         107           0 :        call endrun('solar_euv_data_init')
         108             :     end if
         109             :
         110           0 :     we(:nbins)  = lambda(:nbins) - 0.5_r8*dellam(:nbins)
         111           0 :     we(nbins+1) = lambda(nbins)  + 0.5_r8*dellam(nbins)
         112             :
         113           0 :     deallocate(lambda)
         114           0 :     deallocate(dellam)
         115             :
         116             :     ! need to force data loading when the model starts at a time =/ 00:00:00.000
         117             :     ! -- may occur in restarts also
         118           0 :     call solar_euv_advance()
         119           0 :     initialized = .true.
         120             :
         121             :   end subroutine solar_euv_init
         122             :
         123             : !-----------------------------------------------------------------------
         124             : ! Reads in the ETF data for the current date.
         125             : !-----------------------------------------------------------------------
         126      370944 :   subroutine solar_euv_advance()
         127             :
         128             :     integer  :: index
         129             :     logical  :: read_data
         130             :     integer  :: ierr
         131             :     integer  :: offset(2), count(2)
         132             :     real(r8) :: delt
         133             :
         134      370944 :     if (.not.solar_euv_data_active) return
         135             :
         136           0 :     index = -1
         137             :
         138           0 :     read_data = time_coord%read_more() .or. .not.initialized
         139           0 :     call time_coord%advance()
         140             :
         141           0 :     if ( read_data ) then
         142             :
         143           0 :        index = time_coord%indxs(1)
         144             :
         145             :        ! get the surrounding time slices
         146           0 :        offset = (/ 1, index /)
         147           0 :        count =  (/ nbins, nrecords /)
         148             :
         149           0 :        ierr = pio_get_var( file_id, ssi_vid, offset, count, irradi )
         150             :     endif
         151             :
         152           0 :     delt = time_coord%wghts(2)
         153             :
         154           0 :     solar_euv_data_etf(:) = irradi(:,1) + delt*( irradi(:,2) - irradi(:,1) )
         155             :
         156             :   end subroutine solar_euv_advance
         157             :
         158             : end module solar_euv_data

Generated by: LCOV version 1.14