subroutine frantic_module_write(filename,ierr)

  use ezcdf
  use frantic_module
  implicit none

  character(len=*), intent(in) :: filename ! NetCDF filename to write
  integer, intent(out) :: ierr             ! completion code, 0=OK

  !-----------------------
  ! WRITE the contents of the frantic module as a NetCDF file
  ! for debugging purposes
  !-----------------------
  ! local:

  integer :: ncid

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

  call cdf_open(ncid,filename,'w',ierr)
  if(ierr.ne.0) then
    ierr=10
    return
  end if

  !-----------------------
  !  data definitions

  !  array dimensions...

  call cdf_define(ncid,'mj',mj,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'mig',mig,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'mix',mix,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'mibs',mibs,ierr); if (ierr.ne.0) go to 997

  !  user grid stuff...

  call cdf_define(ncid,'nx_user',nx_user,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'nxb_user',nxb_user,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'x_user',x_user,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'xb_user',xb_user,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'volxb_user',volxb_user,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'Rmajxb_user',Rmajxb_user,ierr); if (ierr.ne.0) go to 997

  !  data...

  call cdf_define(ncid,'lun6',lun6,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'npro_set',npro_set,ierr); if (ierr.ne.0) go to 997

  call cdf_define(ncid,'ninldx',ninldx,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'nneut',nneut,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'nplas',nplas,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'nimp',nimp,ierr); if (ierr.ne.0) go to 997
  call cdf_define(ncid,'nneut_s0',nneut_s0,ierr); if (ierr.ne.0) go to 997

  call cdf_define(ncid,'esflag',esflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'vsflag',vsflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'mcflag',mcflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'msflag',msflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'lnflag',lnflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'rcflag',rcflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'lbflag',lbflag,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'lzpflag',lzpflag,ierr); if(ierr.ne.0) go to 997

  call cdf_define(ncid,'aplas',aplas,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'xzplas',xzplas,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'nspec',nspec,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'twall',twall,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'fluxi0',fluxi0,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'vphi_wall',vphi_wall,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'rmass',rmass,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'xzbeams',xzbeams,ierr); if(ierr.ne.0) go to 997

  call cdf_define(ncid,'rbound',rbound,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'rmjrmp',rmjrmp,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'tvol',tvol,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'x',x,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'xb',xb,ierr); if(ierr.ne.0) go to 997

  call cdf_define(ncid,'den',den,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'temp',temp,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'vphiz',vphiz,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'bii',bii,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'bcx',bcx,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'bcxs',bcxs,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'volsor',volsor,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'xzimpj',xzimpj,ierr); if(ierr.ne.0) go to 997
  call cdf_define(ncid,'aimpj',aimpj,ierr); if(ierr.ne.0) go to 997

  !-----------------------
  !  data writes

  !  array dimensions...

  call cdf_write(ncid,'mj',mj,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'mig',mig,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'mix',mix,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'mibs',mibs,ierr); if (ierr.ne.0) go to 998

  !  user grid stuff...

  call cdf_write(ncid,'nx_user',nx_user,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'nxb_user',nxb_user,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'x_user',x_user,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'xb_user',xb_user,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'volxb_user',volxb_user,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'Rmajxb_user',Rmajxb_user,ierr); if (ierr.ne.0) go to 998

  !  data...

  call cdf_write(ncid,'lun6',lun6,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'npro_set',npro_set,ierr); if (ierr.ne.0) go to 998

  call cdf_write(ncid,'ninldx',ninldx,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'nneut',nneut,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'nplas',nplas,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'nimp',nimp,ierr); if (ierr.ne.0) go to 998
  call cdf_write(ncid,'nneut_s0',nneut_s0,ierr); if (ierr.ne.0) go to 998

  call cdf_write(ncid,'esflag',esflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'vsflag',vsflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'mcflag',mcflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'msflag',msflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'lnflag',lnflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'rcflag',rcflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'lbflag',lbflag,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'lzpflag',lzpflag,ierr); if(ierr.ne.0) go to 998

  call cdf_write(ncid,'aplas',aplas,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'xzplas',xzplas,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'nspec',nspec,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'twall',twall,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'fluxi0',fluxi0,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'vphi_wall',vphi_wall,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'rmass',rmass,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'xzbeams',xzbeams,ierr); if(ierr.ne.0) go to 998

  call cdf_write(ncid,'rbound',rbound,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'rmjrmp',rmjrmp,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'tvol',tvol,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'x',x,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'xb',xb,ierr); if(ierr.ne.0) go to 998

  call cdf_write(ncid,'den',den,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'temp',temp,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'vphiz',vphiz,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'bii',bii,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'bcx',bcx,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'bcxs',bcxs,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'volsor',volsor,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'xzimpj',xzimpj,ierr); if(ierr.ne.0) go to 998
  call cdf_write(ncid,'aimpj',aimpj,ierr); if(ierr.ne.0) go to 998

  go to 999

  !-----------------------
  !  error handling section

997 continue
  ierr=21    ! cdf_define error
  goto 999

998 continue
  ierr=22    ! cdf_write error
  goto 999

999 continue

  !-----------------------
  call cdf_close(ncid)

  return
end subroutine frantic_module_write


subroutine frantic_module_read(filename,ierr)

  use ezcdf
  use frantic_module
  implicit none

  character(len=*), intent(in) :: filename ! NetCDF filename to read
  integer, intent(out) :: ierr             ! completion code, 0=OK

  !-----------------------
  ! READ the contents of the frantic module as a NetCDF file
  ! for debugging purposes
  !-----------------------
  ! local:

  integer :: ncid
  integer :: imj,imig,imix,imibs   ! array dimensions to read...
  integer :: isb

  integer :: idims(10)
  character(len=10) :: eztype
  !-----------------------

  call cdf_open(ncid,filename,'r',ierr)
  if(ierr.ne.0) then
    ierr=11
    return
  end if

  !  array dimensions...

  call cdf_read(ncid,'mj',imj,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'mig',imig,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'mix',imix,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'mibs',imibs,ierr); if(ierr.ne.0) go to 998

  !  allocate arrays...
  !  this also sets the dimensioning info in the frantic module

  call frantic_species_alloc(imig,imix-imig,imibs)
  call frantic_profile_alloc(imj-2)     ! cf nzonec = mj-2 in subroutine

  !  user grid stuff...

  call cdf_read(ncid,'nx_user',nx_user,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'nxb_user',nxb_user,ierr); if (ierr.ne.0) go to 998

  allocate(x_user(nx_user),xb_user(nxb_user))
  allocate(volxb_user(nxb_user),Rmajxb_user(nxb_user))

  call cdf_read(ncid,'x_user',x_user,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'xb_user',xb_user,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'volxb_user',volxb_user,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'Rmajxb_user',Rmajxb_user,ierr); if (ierr.ne.0) go to 998

  !  now read the data...
  !    (lun6 read is skipped -- defaults to 6, may be reset by caller).

  npro_set=.FALSE.
  call cdfInqVar(ncid,'npro_set',idims,eztype)
  call cdf_read(ncid,'npro_set',npro_set(1:idims(1)),ierr)
  if (ierr.ne.0) go to 998

  call cdf_read(ncid,'ninldx',ninldx,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'nneut',nneut,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'nplas',nplas,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'nimp',nimp,ierr); if (ierr.ne.0) go to 998
  call cdf_read(ncid,'nneut_s0',nneut_s0,ierr); if (ierr.ne.0) go to 998

  call cdf_read(ncid,'esflag',esflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'vsflag',vsflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'mcflag',mcflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'msflag',msflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'lnflag',lnflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'rcflag',rcflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'lbflag',lbflag,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'lzpflag',lzpflag,ierr); if(ierr.ne.0) go to 998

  call cdf_read(ncid,'aplas',aplas,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'xzplas',xzplas,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'nspec',nspec,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'twall',twall,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'fluxi0',fluxi0,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'rmass',rmass,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'xzbeams',xzbeams,ierr); if(ierr.ne.0) go to 998

  nsbeam = 0
  do isb=1,mibs
    if(xzbeams(isb).gt.0.0_fp) nsbeam=isb
  enddo

  call cdfInqVar(ncid,'vphi_wall',idims,eztype)
  if(eztype.ne.'??') then
    call cdf_read(ncid,'vphi_wall',vphi_wall,ierr)
    if(ierr.ne.0) go to 998
  else
    vphi_wall=0.0_fp
  end if

  call cdf_read(ncid,'rbound',rbound,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'rmjrmp',rmjrmp,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'tvol',tvol,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'x',x,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'xb',xb,ierr); if(ierr.ne.0) go to 998

  call cdf_read(ncid,'den',den,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'temp',temp,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'vphiz',vphiz,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'bii',bii,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'bcx',bcx,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'bcxs',bcxs,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'volsor',volsor,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'xzimpj',xzimpj,ierr); if(ierr.ne.0) go to 998
  call cdf_read(ncid,'aimpj',aimpj,ierr); if(ierr.ne.0) go to 998

  aminor=rbound(nzonec+1)

  go to 999  ! success

  !-----------------------
  !  error handling section

998 continue
  ierr=32    ! cdf_read error
  goto 999

999 continue

  !-----------------------
  call cdf_close(ncid)

  return
end subroutine frantic_module_read


subroutine frantic_module_io_error(ilun,ierr_in)

  implicit none

  integer, intent(in) :: ilun     ! Fortran LUN where to write error message
  integer, intent(in) :: ierr_in  ! integer code specifying which error.

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

  if(ierr_in.eq.10) then
    write(ilun,*) ' error opening NetCDF file for write.'
  else if(ierr_in.eq.11) then
    write(ilun,*) ' error opening NetCDF file for read.'
  else if(ierr_in.eq.21) then
    write(ilun,*) ' NetCDF file: cdf_define error.'
  else if(ierr_in.eq.22) then
    write(ilun,*) ' NetCDF file: cdf_write error.'
  else if(ierr_in.eq.32) then
    write(ilun,*) ' NetCDF file: cdf_read error.'
  else
    write(ilun,*) ' EZCDF (NetCDF) error (unspecified).'
  end if

  return
end subroutine frantic_module_io_error
