subroutine trxpl_imasrd2d_cdf(filenam)

  !  read NetCDF file containing data for IMAS database, namely:
  ! 
  !   nR, nZ, & R & Z grids: Rr, Zz, [m]
  !   BR(R,Z), BZ(R,Z), Btor(R,Z), all in  [T]
  !   G(R,Z) -- g-function, R*Bt, [t*m]
  !   psi(R,Z) - poloidal flux, tflux(R,Z) - toroidal flux, in [Wb]
  !   theta(R,Z) - equilibrium poloidal angle, [rad]
  !   curt(R,Z) - total enclosed toroidal current(R,Z) density, [A/m**2]
  !   curpll(R,Z) -  parallel current(R,Z) density , ~<J.B>/|B|', [A/m**2] ...
  ! 

  use netcdf
!  use logmod
  implicit NONE

  character*(*), intent(in) :: filenam  ! filename
  real*8 :: ztime0          ! time of interest
  real*8 :: ztavg           ! +/- delta(t) time averaging
  integer :: nbccw          ! Btoroidal orientation
  integer :: npccw          ! Plasma current orientation
  ! convention is +1 "ccw" means "counter-clockwise looking down from above"
  !               -1  for clockwise orientation
  real*8 dxtend                       ! amount of extension of grid beyond
                                      ! plasma bdy, subject to conditions

  integer :: nR,nZ                    ! gridsizes
  integer :: nbdy                     ! bdy contour size

  real*8, dimension(:), allocatable :: Rr,Zz  ! grids

  real*8, dimension(:), allocatable :: Rbdy,Zbdy    ! plasma bdy sequence

  !  (R,Z) variables, as described above:

  real*8, dimension(:,:), allocatable :: br,bz,bb,bt,psi,tflux,curt,curpll,theta,gfun
  real*8 ::  r_mag_axis,z_mag_axis

  !  constants, as described above, and...

  real*8, parameter :: ZERO = 0.0d0

  !  additional items
  ! local:

  character*100 zfilenam
  character*30 :: cvar
  integer ierr
  integer :: icdf,id
  integer ilf
  !-------------------------------------------------------------------

  ierr = 0

  if(filenam.eq.' ') then
     zfilenam = "trxpl_imas.cdf"
  else
     zfilenam = filenam
  endif
  !  read NetCDF file for IMAS

  call check(nf90_open(trim(adjustl(zfilenam)),NF90_NOWRITE,icdf),trim(adjustl(zfilenam)))

  !  ** NetCDF dimensions **
  nr=0
  nz=0
  nbdy=0
  cvar='nr'
  call check_inq(icdf,trim(cvar),id,ierr)
  call check( nf90_get_var(icdf,id,nr),cvar)
  if(nr.le.0) then
     write(*,*)' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0'
!     call errorLog(' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0')
     ierr=1
     return
  endif
  cvar='nz'
  call check_inq(icdf,trim(cvar),id,ierr)
  call check( nf90_get_var(icdf,id,nz),cvar)
  if(nz.le.0) then
     write(*,*)' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0'
!     call errorLog(' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0')
     ierr=1
     return
  endif
  cvar='nbdy'
  call check_inq(icdf,trim(cvar),id,ierr)
  call check( nf90_get_var(icdf,id,nbdy),cvar)
  if(nbdy.le.0) then
     write(*,*)' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0. DATA for BDY ignored'
!     call errorLog(' trxpl_imasrd2d_cdf:'//trim(cvar)//' le 0. DATA for BDY ignored')
  endif


  cvar='time' !sec
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,ztime0),cvar)

  cvar='avg_delta_t' !sec
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,ztavg),cvar)

  cvar='nbccw' !sec
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,nbccw),cvar)

  if(nbccw.eq.0) then
     write(*,*)' trxpl_imasrd2d_cdf:'//trim(cvar)//' eq 0'
!     call errorLog(' trxpl_imasrd2d_cdf:'//trim(cvar)//' eq 0')
     return
  endif

  cvar='npccw' !sec
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,npccw),cvar)

  if(npccw.eq.0) then
     write(*,*)' trxpl_imasrd2d_cdf:'//trim(cvar)//' eq 0'
!     call errorLog(' trxpl_imasrd2d_cdf:'//trim(cvar)//' eq 0')
     return
  endif

  allocate(rr(nr),zz(nz),br(nr,nz),bz (nr,nz),bt(nr,nz),theta(nr,nz),&
       &psi(nr,nz),tflux(nr,nz),curt(nr,nz),curpll(nr,nz),gfun(nr,nz))
 
  rr=ZERO
  zz=ZERO
  br=ZERO
  bz=ZERO
  bt=ZERO
  theta=ZERO
  psi=ZERO
  tflux=ZERO
  curt=ZERO
  curpll=ZERO
  gfun=ZERO

  cvar='rr' !m
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,rr(1:nr)),cvar)

  cvar='zz' !m
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,zz(1:nz)),cvar)

  cvar='theta' !rad
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,theta(1:nr,1:nz)),cvar)

  cvar='br' !T
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,br(1:nr,1:nz)),cvar)

  cvar='bz' !T
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,bz(1:nr,1:nz)),cvar)

  cvar='bt' !T
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,bt(1:nr,1:nz)),cvar)

  cvar='psi' !Wb
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,psi(1:nr,1:nz)),cvar)

  cvar='tflux' !Wb
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,tflux(1:nr,1:nz)),cvar)

  cvar='gfun' !T*m
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,gfun(1:nr,1:nz)),cvar)

  cvar='curt' !A/m**2
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,curt(1:nr,1:nz)),cvar)

  cvar='curpll' !A/m**2
  call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
  call check( nf90_get_var(icdf,id,curpll(1:nr,1:nz)),cvar)

  !  plasma bdy & scrape-off information
  if(nbdy.gt.0) then
     cvar='r_mag_axis' !m
     call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
     call check( nf90_get_var(icdf,id,r_mag_axis),cvar)

     cvar='z_mag_axis' !m
     call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
     call check( nf90_get_var(icdf,id,z_mag_axis),cvar)
     
     cvar='dxtend' !m
     call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
     call check( nf90_get_var(icdf,id,dxtend),cvar)

     allocate(rbdy(nbdy),zbdy(nbdy))
     rbdy=ZERO
     zbdy=ZERO

     cvar='rbdy' !m
     call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
     call check( nf90_get_var(icdf,id,rbdy(1:nbdy)),cvar)

     cvar='zbdy' !m
     call check_inq(icdf,trim(cvar),id,ierr) !get var. ID
     call check( nf90_get_var(icdf,id,zbdy(1:nbdy)),cvar)

     if(allocated(rbdy)) deallocate(rbdy,zbdy)

  endif
    ierr = nf90_close(icdf)
    if(ierr.ne.0) then
       write(*,*)' trxpl_imasrd2d_cdf: cannot close file: '//trim(zfilenam)
!       call errorLog(' trxpl_imasrd2d_cdf: cannot close file: '//trim(zfilenam))
    endif       
    if(allocated(rr)) deallocate(rr,zz,br,bz ,bt,theta,psi,tflux,curt,curpll,gfun)

    return

contains 

  subroutine check(status,varnam)
    integer, intent ( in) :: status 
    character*(*),intent(in) :: varnam ! dimension name input 
    
    if(status /= nf90_noerr) then 
       write(*,*)' trxpl_imasrd2d_cdf: error:'& 
            &// trim(nf90_strerror(status))//' for var.= '//trim(varnam)
!       call errorLog(' trxpl_imasrd2d_cdf: error:'& 
!            &// trim(nf90_strerror(status))//' for var.= '//trim(varnam))
!       call bad_exit 
    end if
  end subroutine check

  subroutine check_inq_dim(i_cdf,varnam,idim,ierrcdf) 
    integer,intent(in) :: i_cdf
    integer,intent(out) :: ierrcdf 
    character*(*),intent(in) :: varnam ! dimension name input 
    integer, intent(out) :: idim        ! dimension size returned 
    integer ::id !var ID 
    ierrcdf = nf90_inq_dimid(i_cdf,trim(varnam),id) 
    if(ierrcdf.ne.0) then 
       write(*,*)' trxpl_imasrd2d_cdf: NO such dim='//trim(varnam)
!       call errorLog(' trxpl_imasrd2d_cdf: NO such dim='//trim(varnam))
!       call bad_exit 
    endif 
    ierrcdf = nf90_inquire_dimension(i_cdf,id,len=idim) 
    if(ierrcdf.ne.0) then 
       write(*,*)' trxpl_imasrd2d_cdf: error '//nf90_strerror(ierrcdf)//'for var.'//trim(varnam)
!       call errorLog(' trxpl_imasrd2d_cdf: error '//nf90_strerror(ierrcdf)//'for var.'//trim(varnam))
!       call bad_exit 
    endif 
    if(idim.le.0) then 
       write(*,*)' trxpl_imasrd2d_cdf: 0 value for dimension '//trim(varnam)
 !      call errorLog(' trxpl_imasrd2d_cdf: 0 value for dimension '//trim(varnam))
!       call bad_exit 
    endif 
  end subroutine check_inq_dim

  subroutine check_inq(i_cdf,varnam,id,ierrcdf)
    integer,intent(in) :: i_cdf
    integer,intent(out) :: id,ierrcdf
    character*(*),intent(in) :: varnam 
    ierrcdf = nf90_inq_varid(i_cdf,trim(varnam),id) 
    if(ierrcdf.ne.0) then 
       write(*,*)' trxpl_imasrd2d_cdf: NO such var='//trim(varnam)
!       call errorLog(' trxpl_imasrd2d_cdf: NO such var='//trim(varnam))
!       call bad_exit 
    endif 
  end subroutine check_inq 

end subroutine trxpl_imasrd2d_cdf
