      subroutine klojob(ok)

C*************************************************
C
C This routine is very similar to blojob
C in that it reads lines from a
C file (or stdin) with file handle fhandl, and
C parses them, setting up all of the parameters
C which kylie needs to perform her calculations.
C Any error conditions
C result in a report written out to file handle
C chandl; when clojob returns, ok is set up to
C be .true. for an ok exit, or .false. for an
C error condition. Most of the variables used
C are declared in common block include files -
C these files contain all the documentation regarding
C what the variables are. Note that in this
C modification the wavelength grid is set up, rather
C than being read in from an external file
C
C Started :25/9/95
C Modified:11/10/95
C
C***************************************************** 

C Turn of implicit typing

      implicit none

C Define the hard-wired parameters

      include "kylie.compar_define.inc"

C Load in the File control common block

      include "kylie.filcon_common.inc"

C Load in the File names common block

      include "kylie.filnam_common.inc"

C Load in the wavelength grid & field info common block

      include "kylie.wavfld_common.inc"

C Declare the passed variables

      logical ok		! The exit status of the subroutine

C Declare the in-program variables

      character*120 commnd       ! Character string to hold commands read
      character*120 params(9)    ! Array to hold the parameters read
      
      integer stat              ! Returned status value from lparse
      integer nline             ! Current line being parsed in input file
      integer parcnt		! A counter used to read the parameters in
      integer wcount		! Counter used to set up a waveband definition

      real WStart     		! The start wavelength for a waveband definition
      real WEnd			! The end wavelength for a waveband definition
      real WRes			! The wavelength resolution for a waveband definition
      real WavLen 		! The wavelength for a wavepoint definition

C Declare the in-program control flags

      logical fiecom		! Flag to check that an fields command is found
      logical outcom		! Flag to check that only one output command is used

      logical Dmread		! Flag set when the dump filestub is read
      logical NFread		! Flag set when the number of fields is read
      logical SWread		! Flag set when a start wavelength is read
      logical FWread		! Flag set when a finish wavelength is read
      logical WRread		! Flag set when the wavelength resolution is read
      logical WLread		! Flag set when a wavelength point is read
      logical LBread		! Flag set when the limb-darkening beta is read

C Include the constants file and the stellar file

      include 'constants.inc'
      include 'angles.inc'

C Initialise any variables which need to be done before reading
C the parameters

      ok=.false.

      fiecom=.false.
      TimFil='**none**'
      WrtTim=.false.

      NWDef=0

      LimbB=-1.

      write(chandl,'('' Parsing file....'')')

C Now we read lines from the file, using lparse, which parses the
C the input file and gets command/parameter pairs. parser returns
C a value in stat which remarks on the success of the read.

C First initialise nline to be 0...

      nline=0

 1000 continue

C Set up stat to be equal to the dimension of params

      stat=9 

      call parser(fhandl,chandl,commnd,params,stat,nline)

C Now, if stat is greater than zero, interpret the command
C in a huge if_then loop. Note that indenting is not used here.

      if(stat.gt.0) then

C************************************************************************

C OK, deal with the fields command

      if(commnd.eq.'FIELDS') then

C Check that this is the only fields command - if not, bum out

      if(fiecom) then
        write(chandl,
     :  '(''*** A spurious #Fields command was found at line '',
     :  I2)') nline
        goto 9000
      else
        fiecom=.true.
      endif

C Initialise the parameters

      Dmread=.false.
      NFread=.false.
      LBread=.false.

C Interpret all of the parameters

      do parcnt=1,stat,1

      if(params(parcnt)(1:14).eq.
     :  'DUMP_FILESTUB:') then
        if(Dmread) goto 6500
        read(params(parcnt)(15:),'(A)') DmStub
        Dmread=.true.
      elseif(params(parcnt)(1:17).eq.
     :  'NUMBER_OF_FIELDS:') then
        if(NFread) goto 6500
        read(params(parcnt)(18:),'(I5)',err=8500) NField
        NFread=.true.
      elseif(params(parcnt)(1:16).eq.
     :  'LIMB_U_OVERRIDE:') then
        if(LBread) goto 6500
        read(params(parcnt)(17:),'(F20.0)',err=8500) LimbB
        LBread=.true.
      else
        goto 7500
      endif
     
      enddo

C Check we have all of the parameters...

      if(.not.Dmread) then
        write(chandl,
     :   '(''*** Dump_Filestub was not defined'',
     :   '' in #Fields command'')')
        goto 9000
      elseif(.not.NFread) then
        write(chandl,
     :   '(''*** Number_of_Fields was not defined'',
     :   '' in #Fields command'')')
        goto 9000
      endif

C Check the number of fields is ok...

      if(NField.le.0) then
        write(chandl,
     :  '(''*** Number_of_Fields was less than 1 '',
     :  ''in #Fields command'')')
        goto 9000
      elseif(NField.gt.maxfld) then
        write(chandl,
     :  '(''*** Number_of_Fields was too large (maximum '',
     :  I2,'') in #Fields command'')')
        goto 9000
      endif   

C**************************************************************************

C Now deal with a waveband command. We do not need to check that this is the
C only waveband command, since we are allowed more than one...

      elseif(commnd.eq.'WAVEBAND') then

C Initialise the flags for the variables which must be read

      SWread=.false.
      FWread=.false.
      WRread=.false.

C Interpret all of the parameters

      do parcnt=1,stat,1

      if(params(parcnt)(1:17).eq.
     :  'START_WAVELENGTH:') then
        if(SWread) goto 6500
        read(params(parcnt)(18:),'(F20.0)',err=8500) WStart
        SWread=.true.
      elseif(params(parcnt)(1:18).eq.
     :  'FINISH_WAVELENGTH:') then
        if(FWread) goto 6500
        read(params(parcnt)(19:),'(F20.0)',err=8500) WEnd
        FWread=.true.
      elseif(params(parcnt)(1:22).eq.
     :  'WAVELENGTH_RESOLUTION:') then
        if(WRread) goto 6500
        read(params(parcnt)(23:),'(F20.0)',err=8500) WRes
        WRread=.true.
      else
        goto 7500
      endif

      enddo

C Check we have all of the parameters...

      if(.not.SWread) then
        write(chandl,
     :   '(''*** Start_Wavelength was not defined'',
     :   '' in #Waveband command'')')
        goto 9000
      elseif(.not.FWread) then
        write(chandl,
     :   '(''*** Finish_Wavelength was not defined'',
     :   '' in #Waveband command'')')
        goto 9000
      elseif(.not.WRread) then
        write(chandl,
     :   '(''*** Wavelength_Resolution was not defined'',
     :   '' in #Waveband command'')')
        goto 9000
      endif

C Check that WRes is not zero

      if(WRes.eq.0.) then
        write(chandl,
     :   '(''*** Wavelength_Resolution was 0 in #Waveband '',
     :   ''command'')')
        goto 9000
      endif

C OK, now update the wavelength grid accordingly. First check we have enough
C space in the wavelength grid to put the new band in...

      if(NWDef+int((WEnd-WStart)/WRes)+2.gt.NWGrid) then
        write(chandl,
     :   '('' *** There is not enough space in the wavelength'',
     :   '' grid for the #Waveband command on line '',I3,
     :   ''('',I5,'' points) - please request a recompile'')')
     :   nline,NWGrid
         goto 9000
      endif

C If there is enough space, update the grid accordingly

      do wcount=0,int((WEnd-WStart)/WRes)+1
        NWDef=NWDef+1
        WGrid(NWDef)=WStart+wcount*WRes
      enddo

C**************************************************************************

C Now deal with a wavepoint command. We do not need to check that this is the
C only wavepoint command, since we are allowed more than one...

      elseif(commnd.eq.'WAVEPOINT') then

C Initialise the flags for the variables which must be read

      WLread=.false.

C Interpret all of the parameters

      do parcnt=1,stat,1

      if(params(parcnt)(1:11).eq.
     :  'WAVELENGTH:') then
        if(WLread) goto 6500
        read(params(parcnt)(12:),'(F20.0)',err=8500) WavLen
        WLread=.true.
      else
        goto 7500
      endif

      enddo

C Check we have all of the parameters...

      if(.not.WLread) then
        write(chandl,
     :   '(''*** Wavelength was not defined'',
     :   '' in #Wavepoint command'')')
        goto 9000
      endif

C OK, now update the wavelength grid accordingly. First check we have enough
C space in the wavelength grid to put the new band in...

      if(NWDef+1.gt.NWGrid) then
        write(chandl,
     :   '('' *** There is not enough space in the wavelength'',
     :   '' grid for the #Wavepoint command on line '',I3,
     :   ''('',I5,'' points) - please request a recompile'')')
     :   nline,NWGrid
         goto 9000
      endif

C If there is enough space, update the grid accordingly

      NWDef=NWDef+1
      WGrid(NWDef)=WavLen

C**************************************************************************

C Do the (optional) output command

      elseif(commnd.eq.'OUTPUT') then

C Check that this is the only fields command - if not, bum out

      if(outcom) then
        write(chandl,
     :  '(''*** A spurious #Output command was found at line '',
     :  I2)') nline
        goto 9000
      else
        outcom=.true.
      endif

C Interpret all of the parameters

      do parcnt=1,stat,1

      if(params(parcnt)(1:17).eq.
     :  'TIMINGS_FILENAME:') then
        if(WrtTim) goto 6500
        read(params(parcnt)(18:),'(A)') TimFil
        WrtTim=.true.
      else
        goto 7500
      endif

      enddo


C**************************************************************************

C This part is for unrecognised commands.....

      else
        print *,commnd
        write(6,
     : '(''*** An unrecognised command was found at line '',I2)') nline
        goto 9000
      endif

C**************************************************************************

C Now we deal with the cases where stat <= 0

C Checks to see if stat was zero - if it was, jump out...

      elseif(stat.eq.0) then
        goto 5000

C Otherwise, we have had an error - give a report...

      elseif(stat.eq.-1) then
        write(chandl,
     :  '(''*** Unknown command found at line '',I2)') nline
        stop
      elseif(stat.eq.-2) then
        write(chandl,
     :  '(''*** Command too long at line '',I2)') nline
        stop
      elseif(stat.eq.-3) then
        write(chandl,
     :  '(''*** A command was not followed by { on line '',I2)') nline
        stop
      elseif(stat.eq.-4) then
        write(chandl,
     :  '(''*** A spurious { was found at line '',I2)') nline
        stop
      elseif(stat.eq.-5) then
        write(chandl,
     :  '(''*** There were too many parameters (runaway list?) at
     : line '',I2)') nline
        stop
      elseif(stat.eq.-6) then
        write(chandl,
     :  '(''*** Parameter too long at line '',I2)') nline
        stop
      elseif(stat.eq.-7) then
        write(chandl,
     :  '(''*** End-of-file found when reading parameters at '',
     :  ''line '',I2)') nline
        stop
      elseif(stat.eq.-8) then
        write(chandl,
     :  '(''*** There was a general error reading line '',I2)') nline
      else
        write(chandl,
     :  '(''*** There was an unspecified error reading line '',
     :  I2)') nline
        stop
      endif

C Loop back around to read the next line...
 
      goto 1000

C This is the jump-in point for a `legal' end-of-file

 5000 continue

C Check that we found all the necessary commands

      if(.not.fiecom) then
        write(chandl,
     :  '('' *** There was no #Fields command found'')')
        goto 9000
      endif 

C Check that at least one wavelength point was defined

      if(NWDef.le.0) then
        write(chandl,
     :  '('' *** There were no wavelength points defined'')')
        goto 9000
      endif

C Set up ok for a normal exit status...

      ok=.true.

      goto 9000

C This is the jump in point for a duplicated parameter...

 6500 continue

      write(chandl,
     :  '(''*** An duplicated parameter was found in the #'',A,
     :''command at line '',I2,'' : '',A)') 
     : commnd(1:index(commnd,' ')),nline,params(parcnt)

        goto 9000

C This is the jump-in point for an unknown parameter error

 7500   write(chandl,
     :  '(''*** An undefined parameter was found in the #'',A,
     :''command at line '',I2,'' : '',A)') 
     :  commnd(1:index(commnd,' ')),nline,params(parcnt)
        goto 9000

C This is the jump-in point for float read errors

 8500 continue

      write(chandl,
     :  '(''*** There was an error reading a number at line'',
     :  I3)') nline

C This is the jump-in point for exiting

 9000 continue

      return

      end
