      subroutine hec_ped_write
c
c write pedestals to file
c
      implicit none
c
      include'hec_par.inc'      !shared parameters
      include'hec_datacard.inc' !datacard values
      include'hec_geo.inc'      !geometry correspondence tables
      include'hec_ped.inc'      !pedestal values
      include'hec_runh.inc'     !run header
c
      integer ic                !adc channel number
c
c open file for writing
c
      open (unit=io_ped(4), file='ped_out.dat', status='new', err=2)
c
      write(io_ped(4), *, err=1) run_no
      do ic = 1, i_adc_used
        write(io_ped(4), *, err=1) ic, adc_ped_run(ic), adc_rms_run(ic)
      enddo
      print *, ' *** pedestal file writen to logical unit ', io_ped(4)
      close (unit=io_ped(4))
      return
c
 1    continue
      print *, ' *** error writing pedestal file to unit ', io_ped(4) 
      close (unit=io_ped(4))
      stop
c
 2    continue
      print *, ' *** error opening pedestal file to unit ', io_ped(4) 
      stop
c
      end
