C Program XFAN_SELECT, for use with 77-102A-01N.
C Written in April 1994 by H. K. Hills, using the format given 
C by the data set documentation. Revised Aug. 25, 1994.
C Revised Jan. 26, 1995. 
C   Check for CR and go to prompt for valid date.
C   Open input file as READONLY.
C
C Revised Jan. 30, 1995.
C   Insert EOF check on reading data.
C   Insert messages before termination on EOF, from keyboard or data.
C
C This program requests the name of the input file,
C and it generates its own name for the output file. The program requests a date
C range to be copied, then reads the data set and writes an exact copy of the
C logical records that contain those dates (each logical record is two physical
C records). 
C When the range's data is finished, user has the option to display the
C first record of the next day, and then user has the option to copy that 
C day's data to a separate file (including the first record). If this isn't done
C and the user then continues in the program to choose this next day number,
C then the output copy will not contain the first record of that day (because 
C it was already looked at earlier and ignored).

      Program XFAN_SELECT
      real*4 plasma(10)
      character*1 yesno,filpos(7)
      character*6 itime,iyymmdd,ltime,nothing
      character*12 dataname
      character*60 infilename
 
      equivalence(dataname,filpos),(itime,filpos(7))

      data dataname /'ISEE1.xxxxxx'/
      data nothing /'      '/
      ifiles=0

      write(6,600)
  600 format(/,' For use with the ISEE 1 Solar Wind Ion Moments data',
     1 ' set (77-102A-01N)',/,' from the Cross-Fan (X-Fan) Analyzer.',
     1 '   H. K. Hills, August, 1994.',/,
     2 ' FORTRAN source for this program is entered as',/
     3 ' B44416 in NSSDC''s Technical Reference File (TRF).',//,
     4 ' This copies selected one-day files from the ',
     5 'larger (whole-year) data set.',/,
     6 ' It asks for the input file name, and generates output files',
     7 /, ' named "ISEE1.yymmdd" where yymmdd shows the data date.',//,
     8 ' The program keeps its position within the data set, i.e.,',
     8 ' it does not',/,
     8 ' return to the beginning to search for the next time, so if',/,
     9 ' it says it found day X next: if you want to copy day X then ',
     9 /,' you must say "yes" at that moment. If you wait til the next',
     1 /,' chance to specify full date, the 1st record will be lost.',/,
     2 ' Control Z at keyboard input will terminate the job.',//)

      write(6,*) 'Enter name of input file.'
      read(5,10,end=30) infilename   
   10 format(A60)
      open(unit=10,file=infilename,READONLY,status='old',
     1 access='sequential')      

  330 continue
      write(6,13)
   13 format(' Do you want to automatically copy multiple consecutive',
     1 ' days of data',/,'  (each in its own file)?')
      read(5,103,end=30) yesno
      icontinue=0
      if(yesno.eq.'y'.or.yesno.eq.'Y')  icontinue=1

      itime=nothing
   14 write(6,*) 'Enter first date YYMMDD to be copied.'
      read(5,11,end=30)itime
   11 format(A6)
      if(itime.eq.nothing) then
         write(6,*) ' Must enter a valid date, even if no data for it.'
         go to  14
      endif
      icnt=0

      open(unit=7,file=dataname,access='sequential',
     1   status='new',form='formatted')

      ifiles=ifiles+1

      if(icontinue.eq.1) then
          ltime=nothing
   15     write(6,*) 'Enter last date YYMMDD to copy'
          read(5,11,end=30) ltime
          if(ltime.eq.nothing) then
         write(6,*) ' Must enter a valid date, even if no data for it.'
                    go to 15
          endif
      endif

    2 read(10,1000,end=31) iyymmdd,sec,utime,(plasma(i),i=4,10)
C 1000 format(1x,I6,1x,2(F8.1,1x),5(1PE11.4,1x),15(/,6(1PE11.4,1x)))
 1000 format(1x,A6,1x,2(F8.1,1x),5(1PE11.4,1x),15(/,6(1PE11.4,1x)))

C This seems to be both an input format and an output format.
C I changed to a slightly different output format to replicate the
C original format. The new format is statement number 2000.
C I6 changed to A6 to facilitate construction of output filename.

      if(iyymmdd.lt.itime) go to 2
      if(iyymmdd.eq.itime) then
  500    write(7,2000) iyymmdd,sec,utime,(plasma(i),i=4,10)
 2000 format(1x,A6,2(1x,F8.1),5(1x,1PE11.4),/,2(1PE11.4,1x))
         icnt=icnt+1
         go to 2
      endif

      write(6,12) icnt, itime
   12 format(I8,' logical records (times) copied for day ',A6)
      close(7)
      if(icontinue.eq.1.and.iyymmdd.le.ltime) go to 201

      write(6,102) iyymmdd
  102 format(/' Found day ',A6,' next.')
c  102 format(/' Found day ',A6,' next. Do you want to see 1st record?')
c      read(5,103,end=30) yesno
  103 format(A1)
c      if(yesno.eq.'y'.or.yesno.eq.'Y') then
c         write(6,2001) iyymmdd,sec,utime,(plasma(i),i=4,10)
 2001    format(1x,A6,2(1x,F8.1),4(1x,1PE11.4),/,3(1PE11.4,1x),//)
c      endif
      write(6,*)'Do you want to copy day ',iyymmdd,
     1  ' to a separate file?'
      read(5,103,end=30) yesno
      if(yesno.eq.'y'.or.yesno.eq.'Y') then
  201    itime=iyymmdd
         icnt=0
         open(unit=7,file=dataname,access='sequential',
     1       status='new',form='formatted')
         ifiles=ifiles+1
         go to 500
      endif
      go to 330  ! to select again, without losing position in input file


   31 write(6,12) icnt,itime
      close(7)
      write(6,*)' EOF encountered on input data file.'
      go to 32

   30 write(6,*)' Job terminated from keyboard.'
   32 write(6,2010) ifiles
 2010 format(I5,' output files were opened on unit 7 during this job.')

      stop

      end
