      subroutine begin(unit)
      integer   unit
      character rolex*10
      character today*11
      character image*80
      integer   li
      call str_write(unit,'!!_BEGIN')
      call image_name(image,li)
      call str_write(unit,'!! MODIFIED BY '//image(1:li)//' AT '//
     1 today()//rolex())
      return
      end
      subroutine log_add(line)
      character line*(*)
      integer   log_unit
      common /log_common/ log_unit
      if(log_unit.eq.0) then
        call log_open(' ')
      endif
      if(log_unit.gt.0) then
        call str_write(log_unit,line)
      endif
      return
      end
      subroutine log_close
      integer   log_unit
      common /log_common/ log_unit
      if(log_unit.gt.0) then
        close(log_unit)
        log_unit = 0
      endif
      return
      end
      subroutine log_open(name)
      character name*(*)
      integer   log_unit
      common /log_common/ log_unit
      data log_unit /0/
      character rolex*10
      character today*11
      integer   length
      character filename*80
      integer   lf
      character image*80
      integer   li
      integer   ier
      if(log_unit.gt.0) close(log_unit)
      call get_lun(log_unit,ier)
      if(ier.ne.0) then
        log_unit=-1
      else
        filename = name
        lf = length(filename)
        if(lf.eq.0) then
          filename = 'prepdist.log'
          lf = length(filename)
        endif
        open(unit=log_unit,file=filename(1:lf),status='unknown',
     1   access='append',carriagecontrol='list',iostat=ier)
        if(ier.ne.0) then
          log_unit=-1
        else
          call image_name(image,li)
          call str_write(log_unit,'Opened by '//image(1:li)//
     1     ' at '//today()//rolex())
        endif
      endif
      return
      end
      subroutine read_collect(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      integer   length
      character line*80
      character s*20
      integer   i
      message = 'error subroutine read_collect'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:5).eq.'SCAN ') then
          call str_extract(line,2,collect_scan)
        else if(line(1:4).eq.'ROT ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(collect_rot(i),s)
          enddo
        else if(line(1:4).eq.'INC ') then
          call str_extract(line,2,s)
          call get_real(collect_inc,s)
        else if(line(1:5).eq.'TIME ') then
          call str_extract(line,2,s)
          call get_real(collect_time,s)
        else if(line(1:5).eq.'DARK ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_dark(i),s)
          enddo
        else if(line(1:7).eq.'FILNUM ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_filnum(i),s)
          enddo
        else if(line(1:7).eq.'NUFTYP ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_nuftyp(i),s)
          enddo
        else if(line(1:5).eq.'EVAL ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          call get_integer(collect_eval,s)
          do i=1,3
            call str_header(line,s,line)
            call get_real(collect_eval_data(i),s)
          enddo
        else if(line(1:7).eq.'RECENT ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_recent(i),s)
          enddo
        else if(line(1:5).eq.'SIZE ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_size(i),s)
          enddo
        else if(line(1:6).eq.'BATCH ') then
          call str_extract(line,2,s)
          call get_real(collect_batch,s)
        else if(line(1:4).eq.'PAD ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_pad(i),s)
          enddo
        else if(line(1:5).eq.'WAIT ') then
          call str_extract(line,2,s)
          call get_real(collect_wait,s)
        else if(line(1:4).eq.'SUB ') then
          call str_extract(line,2,s)
          call get_real(collect_sub,s)
        else if(line(1:7).eq.'EXPTIM ') then
          call str_extract(line,2,s)
          call get_real(collect_exptim,s)
        else if(line(1:5).eq.'BORD ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_integer(collect_bord(i),s)
          enddo
        else if(line(1:7).eq.'ERRLIM ') then
          read(unit,100,iostat=ier) line
          nin = nin + 1
          if(ier.ne.0) return
          call str_header(line,s,line)
          call get_real(collect_errlim_yms,s)
          call str_header(line,s,line)
          call get_real(collect_errlim_zms,s)
          call str_header(line,s,line)
          call get_real(collect_errlim_rot,s)
          read(unit,100,iostat=ier) line
          nin = nin + 1
          if(ier.ne.0) return
          call str_header(line,s,line)
          call get_real(collect_errlim_bg,s)
          call str_header(line,s,line)
          call get_real(collect_errlim_bg_sd,s)
          read(unit,100,iostat=ier) line
          nin = nin + 1
          if(ier.ne.0) return
          call str_header(line,s,line)
          call get_integer(collect_errlim_bogus,s)
          read(unit,100,iostat=ier) line
          nin = nin + 1
          if(ier.ne.0) return
          call str_header(line,s,line)
          call get_real(collect_errlim_ratio,s)
          read(unit,100,iostat=ier) line
          nin = nin + 1
          if(ier.ne.0) return
          call str_header(line,s,line)
          call get_integer(collect_errlim_spot(1),s)
          call str_header(line,s,line)
          call get_integer(collect_errlim_spot(2),s)
        else if(line(1:7).eq.'SMOOTH ') then
          call str_extract(line,2,s)
          call get_integer(collect_smooth,s)
        else if(line(1:5).eq.'MASK ') then
          call str_header(line,s,line)
          do i=1,4
            call str_header(line,s,line)
            call get_integer(collect_mask(i),s)
          enddo
        else if(line(1:7).eq.'BKTYPE ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(collect_bktype(i),s)
          enddo
        else if(line(1:5).eq.'DUMP ') then
          call str_extract(line,2,s)
          call get_integer(collect_dump,s)
        else if(line(1:7).eq.'SDFACT ') then
          call str_extract(line,2,s)
          call get_real(collect_sdfact,s)
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_find ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_det(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      character line*80
      character s*20
      integer   i
      message = 'error subroutine read_det'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:7).eq.'SPDFIL ') then
          call str_leading(line(8:),det_spdfil)
        else if(line(1:7).eq.'SETDET ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          call get_integer(det_hvca,s)
          call str_header(line,s,line)
          call get_integer(det_hvii,s)
          call str_header(line,s,line)
          call get_integer(det_adcz,s)
          call str_header(line,s,line)
          call get_integer(det_adcr,s)
          call str_header(line,s,line)
          call get_integer(det_bls,s)
        else if(line(1:8).eq.'! THETA ') then
          line = line(3:)
          call str_extract(line,2,s)
          call get_real(det_theta,s)
        else if(line(1:7).eq.'! DIST ') then
          line = line(3:)
          call str_extract(line,2,s)
          call get_real(det_dist,s)
        else if(line(1:6).eq.'DATUM ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          call get_real(det_omega,s)
          call str_header(line,s,line)
          call get_real(det_kappa,s)
          call str_header(line,s,line)
          call get_real(det_phi,s)
        else if(line(1:5).eq.'GAIN ') then
          call str_extract(line,2,s)
          call get_real(det_gain,s)
        else if(line(1:5).eq.'OVFL ') then
          call str_extract(line,2,s)
          call get_integer(det_ovfl,s)
        else if(line(1:6).eq.'DCPOS ') then
          call str_header(line,s,line)
          do i=1,4
            call str_header(line,s,line)
            call get_integer(det_dcpos(i),s)
          enddo
        else if(line(1:7).eq.'DCFLIM ') then
          call str_extract(line,2,s)
          call get_real(det_dcflim,s)
        else if(line(1:7).eq.'BETERR ') then
          call str_extract(line,2,s)
          call get_real(det_beterr,s)
        else if(line(1:7).eq.'DET HEADER ') then
          call str_extract(line,2,s)
          call get_real(det_dcflim,s)
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_det ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_det_bnl(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      character line*80
      character s*20
      message = 'error subroutine read_det_bnl'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:7).eq.'IONFIL ') then
          call str_header(line,s,bnl_ionfil)
        else if(line(1:5).eq.'TIME ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          call get_real(bnl_time,s)
        else if(line(1:7).eq.'SATRAT ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          call get_real(bnl_satrat,s)
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_det_bnl ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_det_header(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      character line*80
      character s*20
 
      message = 'error subroutine read_det_header'
      read(unit,100,iostat=ier) line
100   format(a)
 
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:3).eq.'KV ') then
          call str_header(line,s,header_kv)
        else if(line(1:3).eq.'MA ') then
          call str_header(line,s,header_ma)
        else if(line(1:6).eq.'ATTEN ') then
          call str_header(line,s,header_atten)
        else if(line(1:6).eq.'FOCAL ') then
          call str_header(line,s,header_focal)
        else if(line(1:7).eq.'COLLIM ') then
          call str_header(line,s,header_collim)
        else if(line(1:5).eq.'MONO ') then
          call str_header(line,s,header_mono)
        else if(line(1:5).eq.'DATA ') then
          call str_header(line,s,header_data)
        else if(line(1:5).eq.'ANAL ') then
          call str_header(line,s,header_anal)
        else if(line(1:5).eq.'USER ') then
          call str_header(line,s,header_user)
        else if(line(1:5).eq.'SITE ') then
          call str_header(line,s,header_site)
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_det_header ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_find(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      character line*80
      character s*20
      integer   i
      message = 'error subroutine read_find'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:7).eq.'LIMITS ') then
          call str_header(line,s,line)
          do i=1,4
            call str_header(line,s,line)
            call get_integer(find_limits(i),s)
          enddo
        else if(line(1:6).eq.'NEIGH ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(find_neigh(i),s)
          enddo
        else if(line(1:7).eq.'PKCRIT ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_integer(find_pkcrit(i),s)
          enddo
        else if(line(1:6).eq.'SIGMA ') then
          call str_extract(line,2,s)
          call get_real(find_sigma,s)
        else if(line(1:7).eq.'CUTOFF ') then
          call str_extract(line,2,s)
          call get_integer(find_cutoff,s)
        else if(line(1:4).eq.'ROT ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(find_rot(i),s)
          enddo
        else if(line(1:4).eq.'INC ') then
          call str_extract(line,2,s)
          call get_real(find_inc,s)
        else if(line(1:5).eq.'TIME ') then
          call str_extract(line,2,s)
          call get_real(find_time,s)
        else if(line(1:7).eq.'NUFTYP ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(find_nuftyp(i),s)
          enddo
        else if(line(1:7).eq.'FILNUM ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_integer(find_filnum(i),s)
          enddo
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_find ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_orient(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      integer   length
      character line*80
      character s*20
      integer   i
      message = 'error subroutine read_orient'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:3).eq.'V1 ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(orient_v1(i),s)
          enddo
        else if(line(1:3).eq.'V2 ') then
          call str_header(line,s,line)
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(orient_v2(i),s)
          enddo
        else if(line(1:3).eq.'L1 ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(orient_l1(i),s)
          enddo
        else if(line(1:3).eq.'L2 ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(orient_l2(i),s)
          enddo
        else if(line(1:5).eq.'MODE ') then
          call str_extract(line,2,orient_mode)
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_find ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_predict(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      character line*80
      character s*20
      integer   i,j
      message = 'error subroutine read_predict'
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:5).eq.'CELL ') then
          call str_header(line,s,line)
          do i=1,6
            call str_header(line,s,line)
            call get_real(predict_cell(i),s)
          enddo
        else if(line(1:7).eq.'SPCGRP ') then
          call str_extract(line,2,s)
          call get_integer(predict_spcgrp,s)
        else if(line(1:5).eq.'AXES ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_integer(predict_axes(i),s)
          enddo
        else if(line(1:7).eq.'MISSET ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(predict_misset(i),s)
          enddo
        else if(line(1:7).eq.'CAMERA ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(predict_camera(i),s)
          enddo
        else if(line(1:5).eq.'DIST ') then
          call str_extract(line,2,s)
          call get_real(predict_dist,s)
        else if(line(1:4).eq.'TAU ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(predict_tau(i),s)
          enddo
        else if(line(1:3).eq.'MU ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(predict_mu(i),s)
          enddo
        else if(line(1:4).eq.'EPS ') then
          call str_extract(line,2,s)
          call get_real(predict_eps,s)
        else if(line(1:7).eq.'LAMBDA ') then
          call str_extract(line,2,s)
          call get_real(predict_lambda,s)
        else if(line(1:6).eq.'DISPR ') then
          call str_extract(line,2,s)
          call get_real(predict_dispr,s)
        else if(line(1:7).eq.'POLARZ ') then
          call str_header(line,s,line)
          do i=1,4
            call str_header(line,s,line)
            call get_real(predict_polarz(i),s)
          enddo
        else if(line(1:4).eq.'SYN ') then
          call str_header(line,s,line)
          do i=1,4
            call str_header(line,s,line)
            call get_real(predict_syn(i),s)
          enddo
        else if(line(1:5).eq.'RESO ') then
          call str_extract(line,2,s)
          call get_real(predict_reso,s)
        else if(line(1:5).eq.'RMIN ') then
          call str_extract(line,2,s)
          call get_real(predict_rmin,s)
        else if(line(1:5).eq.'RMAX ') then
          call str_extract(line,2,s)
          call get_real(predict_rmax,s)
        else if(line(1:5).eq.'WMAX ') then
          call str_extract(line,2,s)
          call get_real(predict_wmax,s)
        else if(line(1:4).eq.'ROT ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(predict_rot(i),s)
          enddo
        else if(line(1:7).eq.'SETOVL ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(predict_setovl(i),s)
          enddo
        else if(line(1:20).eq.'! Orientation matrix') then
          do i=1,3
            read(unit,100,iostat=ier) line
            nin = nin + 1
            if(ier.ne.0) return
            call str_header(line,s,line)
            do j=1,3
              call str_header(line,s,line)
              call get_real(predict_amat(i,j),s)
            enddo
          enddo
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_find ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_refine(unit,nin,message,ier)
      integer   unit
      integer   nin
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      integer   length
      character line*80
      character s*20
      integer   i
      message = 'error subroutine read_refine'
      refine_nfix = 0
      refine_nnofix = 0
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        call str_compress(line,line)
        if(line(1:7).eq.'REJLIM ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(refine_rejlim(i),s)
          enddo
        else if(line(1:5).eq.'RESO ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(refine_reso(i),s)
          enddo
        else if(line(1:7).eq.'RMSCHK ') then
          call str_header(line,s,line)
          do i=1,2
            call str_header(line,s,line)
            call get_real(refine_rmschk(i),s)
          enddo
        else if(line(1:5).eq.'KEEP ') then
          call str_extract(line,2,s)
          call get_integer(refine_keep,s)
        else if(line(1:5).eq.'NCYC ') then
          call str_extract(line,2,s)
          call get_integer(refine_ncyc,s)
        else if(line(1:8).eq.'LSWEIGH ') then
          call str_header(line,s,line)
          do i=1,3
            call str_header(line,s,line)
            call get_real(refine_lsweigh(i),s)
          enddo
        else if(line(1:7).eq.'SIGCUT ') then
          call str_extract(line,2,s)
          call get_real(refine_sigcut,s)
        else if(line(1:4).eq.'FIX') then
          call str_header(line,s,line)
          do while(length(line).gt.0)
            refine_nfix = refine_nfix + 1
            call str_header(line,refine_fix(refine_nfix),line)
          enddo
        else if(line(1:6).eq.'NOFIX ') then
          call str_header(line,s,line)
          do while(length(line).gt.0)
            refine_nnofix = refine_nnofix + 1
            call str_header(line,refine_nofix(refine_nnofix),line)
          enddo
        else if(line(1:5).eq.'EXIT ') then
          message = 'read_find ok'
          return
        endif
        read(unit,100,iostat=ier) line
      enddo
      return
      end
      subroutine read_sav(file,message,ier)
      character file*(*)
      character message*(*)
      integer   ier
      character filename*80
      integer   lf
      integer   unit
      character line*80
      call build_file(file,'MADNES','.SAV',filename,lf)
      call open_file(filename(1:lf),unit,message,ier)
      if(ier.ne.0) return
      read(unit,100,iostat=ier) line
100   format(a)
      if(ier.ne.0) then
        call str_integer(ier,'Error # reading line 1 '//
     1   'from header block',message)
        return
      endif
      if(line(2:12).ne.'! !!! Start') then
        message = 'Sav file header not found'
        ier = 1
        return
      endif
      call read_sav_block(unit,message,ier)
      message = filename(1:lf)//':'//message
      close(unit)
      return
      end
      subroutine read_sav_block(unit,message,ier)
      integer   unit
      character message*(*)
      integer   ier
      character title*80
      character comment_1*80
      character comment_2*80
      character comment_3*80
      character deri*4
      character xtal*4
      character nuffil*80
      character wrtfil*80
      character intfil*80
      integer   intopt
      character template*80
      character xdfile*80
      common /madnes_madnes/ title,comment_1,comment_2,comment_3,deri,
     1 xtal,nuffil,wrtfil,intfil,intopt,template,xdfile
 
      character det_spdfil*80
      integer   det_hvca
      integer   det_hvii
      integer   det_adcz
      integer   det_adcr
      integer   det_bls
      integer   det_setdet(5)
      real      det_theta
      real      det_dist
      real      det_omega,det_kappa,det_phi
      real      det_datum(3)
      real      det_gain
      integer   det_ovfl
      integer   det_dcpos(4)
      real      det_dcflim
      real      det_beterr
      equivalence (det_hvca,det_setdet(1))
      equivalence (det_omega,det_datum(1))
      common /madnes_det/ det_spdfil,det_hvca,det_hvii,det_adcz,
     1 det_adcr,det_bls,det_theta,det_dist,det_omega,det_kappa,
     2 det_phi,det_gain,det_ovfl,det_dcpos,det_dcflim,det_beterr
 
      character header_kv*80
      character header_ma*80
      character header_Atten*80
      character header_focal*80
      character header_collim*80
      character header_mono*80
      character header_data*80
      character header_anal*80
      character header_user*80
      character header_site*80
      common /madnes_header/ header_kv,header_ma,header_Atten,
     1 header_focal,header_collim,header_mono,header_data,
     2 header_anal,header_user,header_site
 
      character bnl_ionfil*80
      real      bnl_time
      real      bnl_satrat
      common /madnes_bnl/ bnl_ionfil,bnl_time,bnl_satrat
 
      integer   find_limits(4)
      integer   find_neigh(2)
      integer   find_pkcrit(3)
      real      find_sigma
      integer   find_cutoff
      real      find_rot(2)
      real      find_inc
      real      find_time
      integer   find_nuftyp(2)
      integer   find_filnum(2)
      common /madnes_find/ find_limits,find_neigh,find_pkcrit,
     1 find_sigma,find_cutoff,find_rot,find_inc,find_time,
     2 find_nuftyp,find_filnum
 
      real       predict_cell(6)
      integer    predict_spcgrp
      integer    predict_axes(3)
      real       predict_misset(3)
      real       predict_camera(3)
      real       predict_dist
      real       predict_tau(3)
      real       predict_mu(2)
      real       predict_eps
      real       predict_lambda
      real       predict_dispr
      real       predict_polarz(4)
      real       predict_syn(4)
      real       predict_reso
      real       predict_rmin
      real       predict_rmax
      real       predict_wmax
      real       predict_rot(2)
      real       predict_setovl(3)
      real       predict_amat(3,3)
      common /madnes_predict/ predict_cell,predict_spcgrp,predict_axes,
     1 predict_misset,predict_camera,predict_dist,predict_tau,
     2 predict_mu,predict_eps,predict_lambda,predict_dispr,
     3 predict_polarz,predict_syn,predict_reso,predict_rmin,
     4 predict_rmax,predict_wmax,predict_rot,predict_setovl,
     5 predict_amat
 
      real       refine_rejlim(3)
      real       refine_reso(2)
      real       refine_rmschk(2)
      integer    refine_keep
      integer    refine_ncyc
      real       refine_lsweigh(3)
      real       refine_sigcut
      integer    refine_nnofix
      character  refine_nofix(19)*4
      integer    refine_nfix
      character  refine_fix(19)*4
      common /madnes_refine/ refine_rejlim,refine_reso,refine_rmschk,
     1 refine_keep,refine_ncyc,refine_lsweigh,refine_sigcut,
     2 refine_nnofix,refine_nofix,refine_nfix,refine_fix
 
      real       orient_v1(3)
      real       orient_v2(3)
      real       orient_l1(3)
      real       orient_l2(3)
      character  orient_mode*4
      common /madnes_orient/ orient_v1,orient_v2,orient_l1,orient_l2,
     1 orient_mode
 
      character  collect_scan*20
      real       collect_rot(2)
      real       collect_inc
      real       collect_time
      integer    collect_dark(2)
      integer    collect_filnum(2)
      integer    collect_nuftyp(2)
      integer    collect_eval
      real       collect_eval_data(3)
      integer    collect_recent(2)
      integer    collect_size(2)
      real       collect_batch
      integer    collect_pad(2)
      real       collect_wait
      real       collect_sub
      real       collect_exptim
      integer    collect_bord(3)
      real       collect_errlim_yms
      real       collect_errlim_zms
      real       collect_errlim_rot
      real       collect_errlim_bg
      real       collect_errlim_bg_sd
      integer    collect_errlim_bogus
      real       collect_errlim_ratio
      integer    collect_errlim_spot(2)
      integer    collect_smooth
      integer    collect_mask(4)
      integer    collect_bktype(2)
      integer    collect_dump
      real       collect_sdfact
 
      common /madnes_collect/ collect_scan,collect_rot,collect_inc,
     1 collect_time,collect_dark,collect_filnum,
     2 collect_nuftyp,collect_eval,collect_eval_data,collect_recent,
     3 collect_size,collect_batch,collect_pad,collect_wait,collect_sub,
     4 collect_exptim,collect_bord,collect_errlim_yms,
     5 collect_errlim_zms,collect_errlim_rot,collect_errlim_bg,
     6 collect_errlim_bg_sd,collect_errlim_bogus,collect_errlim_ratio,
     7 collect_errlim_spot,collect_smooth,collect_mask,collect_bktype,
     8 collect_dump,collect_sdfact
      integer   lenstr
      character line*80
      integer   nin
      character s*20
      nin = 0
      read(unit,100,iostat=ier) line
100   format(a)
      do while(ier.eq.0)
        nin = nin + 1
        if(line(1:7).eq.' TITLE') then
          read(unit,100,iostat=ier) title
          nin = nin + 1
          if(ier.ne.0) goto 900
        else if(line(1:8).eq.' COMMENT') then
          read(unit,100,iostat=ier) comment_1
          nin = nin + 1
          if(ier.ne.0) goto 900
          read(unit,100,iostat=ier) comment_2
          nin = nin + 1
          if(ier.ne.0) goto 900
          read(unit,100,iostat=ier) comment_3
          nin = nin + 1
          if(ier.ne.0) goto 900
        else if(line(1:5).eq.' DERI') then
          call str_leading(line(6:),deri)
        else if(line(1:5).eq.' XTAL') then
          call str_leading(line(6:),xtal)
        else if(line(1:7).eq.' NUFFIL') then
          call str_leading(line(8:),nuffil)
        else if(line(1:7).eq.' WRTFIL') then
          call str_leading(line(8:),wrtfil)
        else if(line(1:7).eq.' INTFIL') then
          call str_leading(line(8:),intfil)
        else if(line(1:7).eq.' INTOPT') then
          call str_extract(line,2,s)
          call get_integer(intopt,s)
        else if(line(1:9).eq.' TEMPLATE') then
          call str_leading(line(10:),template)
        else if(line(1:7).eq.' XDFILE') then
          call str_leading(line(8:),xdfile)
        else if(line.eq.' DET') then
          call read_det(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' DET HEADER') then
          call read_det_header(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' DET BNL') then
          call read_det_bnl(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' FIND') then
          call read_find(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' PREDICT') then
          call read_predict(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' REFINE') then
          call read_refine(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' ORIENT') then
          call read_orient(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' COLLECT') then
          call read_collect(unit,nin,message,ier)
          if(ier.ne.0) return
        else if(line.eq.' ! !!! End of save file') then
          goto 10
        endif
        read(unit,100,iostat=ier) line
      enddo
10    continue
      call str_num(nin,'# lines from header block',message)
      return
900   continue
      call str_2integer(ier,nin,'Error # reading line ## '//
     1 'from header block',message)
      return
      end
