C  this program reforms raw data from the pw1710 when different
C  counting times have been used in different parts of the pattern
C  the string *NEW TIME followed by the actual time/step (in seconds)
c  must appear in the data whenever the counting time is changed.
c
      character*80 char,string
      character*50 title(5)
      character*30 infil,outfil
      real newtim
      parameter(nsize = 17001)           
      
      dimension theta(nsize),ict(nsize),time(nsize)
      write(*,10)
10    format(' Enter name of raw data file,  default = COMMAND.RES  '\)
      read(*,20) infil
      if(infil.eq.'          ') infil='COMMAND.RES'
20    format(a)
      open(unit=1,file=infil,status='old')
      iset=0
1     read(1,30,end=7777) char
30    format(a)
      write(*,40) char
40    format(1x,a)
      if(char(1:4).ne.'*HED') go to 1
      read(1,50,end=999) (title(i),i=1,4)
50    format(a)
c
2     read(1,30,end=999) char
      if(char(1:4).ne.'*TOP') go to 2
c
c
      ipt=0
      iset=iset+1
      ratio=1.0
3     ipt=ipt+1
      read(1,50,end=999) string
c
      if(string(1:9).eq.'*NEW TIME') then
        ipt=ipt-1
        read(string,80) newtim
80      format(10x,f6.2)
        write(*,85) newtim
85      format('  New counting time  =',f5.2,' sec')
c
        go to 3
      end if
c
      if(string(1:4).eq.'*END') go to 4
      read(string,90) theta(ipt),ict(ipt)
90    format(f7.3,1x,i8)
      time(ipt)=newtim     
      go to 3
4     ipt=ipt-1
      start=theta(1)
      stop=theta(ipt)
      step=(stop-start)/(ipt-1)
      step1=theta(2)-theta(1)
c
      if(abs(step-step1).gt.0.0001) then
        write(*,100) step,step1
100     format(' Error in calculating step widths ',2f15.12)
     
      end if
c
c     write(*,300)
c 300 format(/,' enter start  step  stop  <cr> = default')
c     write(*,400) start,step,stop
c 400 format(1x,3f8.3,'  are the default values ')
c     read(*,500) st1,st2,st3
c 500 format(3f8.3)
c     if(st1.ne.0.0) start=st1
c     if(st2.ne.0.0) step=st2
c     if(st3.ne.0.0) stop=st3
c
      write(*,110) iset
110   format(' Enter output FILE NAME for data set # ',i2,'  '\)
      read(*,120) outfil
120   format(a)
      open(unit=2,file=outfil,status='new')
      write(*,130)
130   format(/,' enter TITLE for this data set   '\)
      read(*,140) title(5)
140   format(a)
      write(2,145) (title(i),i=1,4)
145   format(a)
      write(2,150) start,step,stop,title(5)
150   format(3f8.3,5x,a)
      write(2,160) (time(i),ict(i),i=1,ipt)
160   format(5(f6.2,i10))
      write(*,170) ipt
170   format(/,5x,i6,' data points processed',/)
      close(unit=2)
      go to 1
999   write(*,200)
200   format(//,'  error during data reading - end of file',/)
7777  write(*,300) iset
300   format(/,5x,i6,' data sets processed in this run',/)
9999  stop
      end

