C$Procedure ZZGFFVU ( GF, instrument FOV utilities )

      SUBROUTINE ZZGFFVU ( INST,   TSHAPE, RAYDIR, TARGET,
     .                     TFRAME, ABCORR, OBSRVR, TIME,   VISTAT )

C$ Abstract
C
C     SPICE Private routine intended solely for the support of SPICE
C     routines.  Users should not call this routine directly due
C     to the volatile nature of this routine.
C
C     This routine contains the entry points that produce the
C     computations needed for solving for target visibility states
C     in the geometry finding routines.
C
C$ Disclaimer
C
C     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE
C     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S.
C     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE
C     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE
C     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS"
C     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY
C     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A
C     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC
C     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
C     SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
C
C     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA
C     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT
C     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND,
C     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
C     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE
C     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
C
C     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF
C     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY
C     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE
C     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE.
C
C$ Required_Reading
C
C     CK
C     GF
C     IK
C     NAIF_IDS
C     PCK
C     SCLK
C     SPK
C     TIME
C
C$ Keywords
C
C     EVENT
C     FOV
C     GEOMETRY
C     INSTRUMENT
C     SEARCH
C
C$ Declarations

      IMPLICIT NONE

      INCLUDE 'zzabcorr.inc'
      INCLUDE 'zzocced.inc'
      INCLUDE 'gf.inc'

      INTEGER               UBEL
      PARAMETER           ( UBEL = 9 )

      INTEGER               UBPL
      PARAMETER           ( UBPL = 4 )

      CHARACTER*(*)         INST
      CHARACTER*(*)         TSHAPE
      DOUBLE PRECISION      RAYDIR ( 3 )
      CHARACTER*(*)         TARGET
      CHARACTER*(*)         TFRAME
      CHARACTER*(*)         ABCORR
      CHARACTER*(*)         OBSRVR
      DOUBLE PRECISION      TIME
      LOGICAL               VISTAT

C$ Brief_I/O
C
C     VARIABLE  I/O  Entry points
C     --------  ---  --------------------------------------------------
C     UBEL       P   All
C     UBPL       P   All
C     INST       I   ZZGFFVIN
C     TSHAPE     I   ZZGFFVIN
C     RAYDIR     I   ZZGFFVIN
C     TARGET     I   ZZGFFVIN
C     TFRAME     I   ZZGFFVIN
C     ABCORR     I   ZZGFFVIN
C     OBSRVR     I   ZZGFFVIN
C     TIME       I   ZZGFFVST
C     OCSTAT     O   ZZGFFVST
C
C$ Detailed_Input
C
C     See entry points.
C
C$ Detailed_Output
C
C     See entry points.
C
C$ Parameters
C
C     See INCLUDE files
C
C        gf.inc
C        zzgf.inc
C
C$ Exceptions
C
C     See entry points.
C
C$ Files
C
C     Appropriate SPK and instrument kernels must be loaded by the
C     calling program before this routine is called.  PCK, CK and SCLK
C     kernels may be required as well.
C
C     The following data are required:
C
C        - SPK data:  ephemeris data for target and observer that
C          describes the ephemeris of these objects for the period
C          defined by the confinement window, 'CNFINE' must be
C          loaded.  If aberration corrections are used, the states of
C          target and observer relative to the solar system barycenter
C          must be calculable from the available ephemeris data.
C          Typically ephemeris data are made available by loading one
C          or more SPK files via FURNSH.
C
C        - Frame data:  if a frame definition is required to convert
C          the observer and target states to the body-fixed frame of
C          the target, that definition must be available in the kernel
C          pool. Typically the definitions of frames not already
C          built-in to SPICE are supplied by loading a frame kernel.
C
C          Data defining the reference frame associated with the
C          instrument designated by INST must be available in the kernel
C          pool. Additionally the name INST must be associated with an
C          ID code. Normally these data are  made available by loading
C          a frame kernel via FURNSH.
C
C        - IK data: the kernel pool must contain data such that
C          the SPICELIB routine GETFOV may be called to obtain
C          parameters for INST. Normally such data are provided by
C          an IK via FURNSH.
C
C     The following data may be required:
C
C        - PCK data: bodies modeled as triaxial ellipsoids must have
C          orientation data provided by variables in the kernel pool.
C          Typically these data are made available by loading a text
C          PCK file via FURNSH.
C
C          Bodies modeled as triaxial ellipsoids must have semi-axis
C          lengths provided by variables in the kernel pool. Typically
C          these data are made available by loading a text PCK file via
C          FURNSH.
C
C        - CK data: if the instrument frame is fixed to a spacecraft,
C          at least one CK file will be needed to permit transformation
C          of vectors between that frame and both J2000 and the target
C          body-fixed frame.
C
C        - SCLK data:  if a CK file is needed, an associated SCLK
C          kernel is required to enable conversion between encoded SCLK
C          (used to time-tag CK data) and barycentric dynamical time
C          (TDB).
C
C     Kernel data are normally loaded once per program run, NOT every
C     time this routine is called.
C
C$ Particulars
C
C     This routine is designed to determine whether a specified
C     target intersects the space bounded by the FOV of a specified
C     instrument at a specified epoch. The target may be represented
C     by a ray, or the target may be an ephemeris object.
C
C     This routine contains two entry points that support searches
C     for target visibility periods performed using ZZGFSOLV:
C
C        ZZGFFVIN   Saves the user-supplied inputs defining the
C                   visibility computation to be performed.
C                   Initializes the visibility search.
C
C        ZZGFFVST   Returns the visibility state for a specified
C                   time.
C
C$ Examples
C
C     See GFFOVE.
C
C$ Restrictions
C
C     This is a SPICELIB private routine; it should not be called by
C     user applications.
C
C$ Literature_References
C
C     None.
C
C$ Author_and_Institution
C
C     N.J. Bachman   (JPL)
C     L.S. Elson     (JPL)
C     W.L. Taber     (JPL)
C     E.D. Wright    (JPL)
C
C$ Version
C
C-    SPICELIB Version 1.1.0, 04-JUN-2013 (EDW)
C
C        Edit to ZZGFFVIN:
C
C        ABCORR now stripped of all spaces before saving.
C        Specifically, the call
C
C        CALL LJUST ( ABCORR, SVCORR )
C
C        replaced with
C
C        CALL CMPRSS ( ' ', 0, ABCORR, SVCORR )
C
C-    SPICELIB Version 1.0.0, 05-MAR-2009 (NJB) (LSE) (WLT) (EDW)
C
C-&

C
C     SPICELIB functions
C
      DOUBLE PRECISION      DPR
      DOUBLE PRECISION      HALFPI
      DOUBLE PRECISION      VDOT
      DOUBLE PRECISION      VNORM
      DOUBLE PRECISION      VSEP

      INTEGER               ZZOCCED
      INTEGER               ZZWIND2D

      LOGICAL               FAILED
      LOGICAL               RETURN
      LOGICAL               VZERO

C
C     Local parameters
C

C
C     ATOL is a tolerance value for computing FOV angular radius.
C     The angular radius must not exceed pi/2 - ATOL radians.
C
      DOUBLE PRECISION      ATOL
      PARAMETER           ( ATOL  = 1.D-6 )

      INTEGER               BDNMLN
      PARAMETER           ( BDNMLN = 36 )

      INTEGER               FRNMLN
      PARAMETER           ( FRNMLN = 32 )

C
C     Local variables
C
      CHARACTER*(FRNMLN)    SVIFRM
      CHARACTER*(BDNMLN)    SVINAM
      CHARACTER*(SHPLEN)    SVISHP
      CHARACTER*(FRNMLN)    SVTFRM
      CHARACTER*(BDNMLN)    SVTNAM
      CHARACTER*(SHPLEN)    SVTSHP
      CHARACTER*(CORLEN)    SVCORR

      DOUBLE PRECISION      BSITE  ( 3 )
      DOUBLE PRECISION      COORD  ( 2 )
      DOUBLE PRECISION      CTREXT
      DOUBLE PRECISION      DIR    ( 3 )
      DOUBLE PRECISION      ETTARG
      DOUBLE PRECISION      FOVPT  ( 3 )
      DOUBLE PRECISION      FOVRAD ( 3 )
      DOUBLE PRECISION      FVLIMB ( UBEL )
      DOUBLE PRECISION      INSMAT ( 3, 3 )
      DOUBLE PRECISION      L
      DOUBLE PRECISION      LIMB   ( UBEL )
      DOUBLE PRECISION      LT
      DOUBLE PRECISION      M1     ( 3, 3 )
      DOUBLE PRECISION      M2     ( 3, 3 )
      DOUBLE PRECISION      OBSPOS ( 3 )
      DOUBLE PRECISION      PNT2D  ( 3 )
      DOUBLE PRECISION      POS    ( 3 )
      DOUBLE PRECISION      SEMIPT ( 3, 2 )
      DOUBLE PRECISION      SEP
      DOUBLE PRECISION      STOBS  ( 6 )
      DOUBLE PRECISION      SVARAD
      DOUBLE PRECISION      SVBNDS ( 3, MAXVRT )
      DOUBLE PRECISION      SVEDCT ( 3 )
      DOUBLE PRECISION      SVFAXI ( 3 )
      DOUBLE PRECISION      SVFOVM ( 3, 3 )
      DOUBLE PRECISION      SVFPOL ( 2, MAXVRT )
      DOUBLE PRECISION      SVFSMX ( 3, 3 )
      DOUBLE PRECISION      SVFVCT ( 3 )
      DOUBLE PRECISION      SVORIG ( 3 )
      DOUBLE PRECISION      SVPLAN ( UBPL )
      DOUBLE PRECISION      SVRDIR ( 3 )
      DOUBLE PRECISION      SVSEMI ( 3, 2 )
      DOUBLE PRECISION      SVTRAD ( 3 )
      DOUBLE PRECISION      SVXMAG ( 2 )
      DOUBLE PRECISION      TRGCTR ( 3 )
      DOUBLE PRECISION      TRGSMX ( 3, 3 )
      DOUBLE PRECISION      VTEMP  ( 3 )
      DOUBLE PRECISION      VTEMP2 ( 3 )
      DOUBLE PRECISION      X      ( 3 )
      DOUBLE PRECISION      XPT    ( 3 )
      DOUBLE PRECISION      Y      ( 3 )
      DOUBLE PRECISION      Z      ( 3 )

      INTEGER               CLSSID
      INTEGER               FRAMID
      INTEGER               FRCENT
      INTEGER               FRCLSS
      INTEGER               I
      INTEGER               N
      INTEGER               NXPTS
      INTEGER               OCSTAT
      INTEGER               SVINST
      INTEGER               SVNVRT
      INTEGER               SVOBS
      INTEGER               SVTARG
      INTEGER               W

      LOGICAL               ATTBLK ( NABCOR )
      LOGICAL               FOUND
      LOGICAL               SVURAY
      LOGICAL               SVUSTL
      LOGICAL               SVXMIT
      LOGICAL               SVXTRG

C
C     Saved variables
C
      SAVE                  SVARAD
      SAVE                  SVBNDS
      SAVE                  SVCORR
      SAVE                  SVEDCT
      SAVE                  SVFAXI
      SAVE                  SVFOVM
      SAVE                  SVFPOL
      SAVE                  SVFSMX
      SAVE                  SVFVCT
      SAVE                  SVIFRM
      SAVE                  SVINAM
      SAVE                  SVINST
      SAVE                  SVISHP
      SAVE                  SVNVRT
      SAVE                  SVOBS
      SAVE                  SVORIG
      SAVE                  SVPLAN
      SAVE                  SVRDIR
      SAVE                  SVSEMI
      SAVE                  SVTARG
      SAVE                  SVTFRM
      SAVE                  SVTNAM
      SAVE                  SVTRAD
      SAVE                  SVTSHP
      SAVE                  SVURAY
      SAVE                  SVUSTL
      SAVE                  SVXMAG
      SAVE                  SVXMIT
      SAVE                  SVXTRG

C
C     Initial values
C
      DATA                  SVORIG / 3 * 0.D0 /

C
C     Below we initialize the list of visibility types.
C

C
C     This routine should never be called directly.
C
      CALL CHKIN  ( 'ZZGFFVU' )

      CALL SIGERR ( 'SPICE(BOGUSENTRY)' )

      CALL CHKOUT ( 'ZZGFFVU' )
      RETURN

C$Procedure  ZZGFFVIN ( GF, visibility initialization )

      ENTRY ZZGFFVIN ( INST,   TSHAPE, RAYDIR,
     .                 TARGET, TFRAME, ABCORR, OBSRVR )

C$ Abstract
C
C    Perform initialization functions for visibility state
C    determination.
C
C$ Disclaimer
C
C     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE
C     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S.
C     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE
C     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE
C     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS"
C     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY
C     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A
C     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC
C     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
C     SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
C
C     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA
C     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT
C     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND,
C     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
C     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE
C     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
C
C     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF
C     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY
C     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE
C     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE.
C
C$ Required_Reading
C
C     NAIF_IDS
C     FRAMES
C     PCK
C     SPK
C     TIME
C
C$ Keywords
C
C     SEARCH
C     GEOMETRY
C     VISIBILITY
C
C$ Declarations
C
C     CHARACTER*(*)         INST
C     CHARACTER*(*)         TSHAPE
C     DOUBLE PRECISION      RAYDIR ( 3 )
C     CHARACTER*(*)         TARGET
C     CHARACTER*(*)         TFRAME
C     CHARACTER*(*)         ABCORR
C     CHARACTER*(*)         OBSRVR
C
C$ Brief_I/O
C
C     VARIABLE  I/O  DESCRIPTION
C     --------  ---  --------------------------------------------------
C     INST       I   Name of the instrument.
C     TSHAPE     I   Type of shape model used for target body.
C     RAYDIR     I   Ray's direction vector.
C     TARGET     I   Name of the target body.
C     TFRAME     I   Body-fixed, body-centered frame for target body.
C     ABCORR     I   Aberration correction flag.
C     OBSRVR     I   Name of the observing body.
C
C$ Detailed_Input
C
C
C     INST       indicates the name of the instrument, such as a
C                spacecraft-mounted framing camera, the field of view
C                (FOV) of which is to be used for a target intersection
C                search: times when the specified target intersects the
C                region of space corresponding to the FOV are sought.
C
C                The position of the instrument designated by INST is
C                considered to coincide with that of the ephemeris
C                object designated by the input argument OBSRVR (see
C                description below).
C
C                INST must have a corresponding NAIF ID and a frame
C                defined, as is normally done in a frame kernel. It
C                must also have an associated reference frame and a FOV
C                shape, boresight and boundary vertices (or reference
C                vector and reference angles) defined, as is usually
C                done in an instrument kernel.
C
C                See the header of the SPICELIB routine GETFOV for a
C                description of the required parameters associated with
C                an instrument.
C
C
C     TSHAPE     is a string indicating the geometric model used to
C                represent the location and shape of the target body.
C                The target body may be represented by either an
C                ephemeris object or a ray emanating from the observer.
C
C                The supported values of TSHAPE are:
C
C                   'ELLIPSOID'     The target is an ephemeris object.
C
C                                   The target's shape is represented
C                                   using triaxial ellipsoid model,
C                                   with radius values provided via the
C                                   kernel pool. A kernel variable
C                                   having a name of the form
C
C                                      'BODYnnn_RADII'
C
C                                   where nnn represents the NAIF
C                                   integer code associated with the
C                                   body, must be present in the kernel
C                                   pool. This variable must be
C                                   associated with three numeric
C                                   values giving the lengths of the
C                                   ellipsoid's X, Y, and Z semi-axes.
C
C                   'POINT'         The target is an ephemeris object.
C                                   The body is treated as a single
C                                   point.
C
C                   'RAY'           The target is NOT an ephemeris
C                                   object. Instead, the target is
C                                   considered to be represented by the
C                                   ray emanating from the observer's
C                                   location and having direction
C                                   vector RAYDIR. The target is
C                                   considered to be visible if and
C                                   only if the ray is contained within
C                                   the space bounded by the instrument
C                                   FOV.
C
C                Case and leading or trailing blanks are not
C                significant in the string TSHAPE.
C
C
C     RAYDIR     is the direction vector associated with a ray
C                representing the target. RAYDIR is used if and only
C                if TSHAPE (see description above) indicates the
C                target is modeled as a ray.
C
C
C     TARGET     is the name of the target body, the appearances of
C                which in the specified instrument's field of view are
C                sought. The body must be an ephemeris object.
C
C                Optionally, you may supply the integer NAIF ID code
C                for the body as a string. For example both 'MOON' and
C                '301' are legitimate strings that designate the Moon.
C
C                Case and leading or trailing blanks are not
C                significant in the string TARGET.
C
C                The input argument TARGET is used if and only if the
C                target is NOT modeled as ray; equivalently, the input
C                argument TSHAPE (see description above) does not
C                contain a string equivalent---that is, ignoring case
C                and leading and trailing blanks---to 'RAY'.
C
C                TARGET may be set to a blank string if the target is
C                modeled as a ray.
C
C
C     TFRAME     is the name of the reference frame associated with the
C                target. Examples of such names are 'IAU_SATURN'
C                (for Saturn) and 'ITRF93' (for the Earth).
C
C                If the target is an ephemeris object modeled as an
C                ellipsoid, TFRAME must designate a body-fixed
C                reference frame centered on the target body.
C
C                If the target is an ephemeris object modeled as a
C                point, TFRAME is ignored; TFRAME should be left blank.
C
C                If the target is modeled as a ray, TFRAME may
C                designate any reference frame. Since light time
C                corrections are not supported for rays, the
C                orientation of the frame is always evaluated at the
C                epoch associated with the observer, as opposed to the
C                epoch associated with the light-time corrected
C                position of the frame center.
C
C                Case and leading or trailing blanks bracketing a
C                non-blank frame name are not significant in the string
C                TFRAME.
C
C
C     ABCORR     indicates the aberration corrections to be applied
C                when computing the target's position and orientation.
C                The supported values of ABCORR depend on the target
C                representation.
C
C                If the target is represented by a ray, the aberration
C                correction options are
C
C                   'NONE'          No correction.
C                   'S'             Stellar aberration correction,
C                                   reception case.
C                   'XS'            Stellar aberration correction,
C                                   transmission case.
C
C                If the target is an ephemeris object, the aberration
C                correction options are those supported by the SPICE
C                SPK system. For remote sensing applications, where the
C                apparent position and orientation of the target seen
C                by the observer are desired, normally either of the
C                corrections
C
C                   'LT+S'
C                   'CN+S'
C
C                should be used. These and the other supported options
C                are described below.
C
C                Supported aberration correction options for
C                observation (case where radiation is received by
C                observer at ET) are:
C
C                   'NONE'       No correction.
C                   'LT'         Light time only
C                   'LT+S'       Light time and stellar aberration.
C                   'CN'         Converged Newtonian (CN) light time.
C                   'CN+S'       CN light time and stellar aberration.
C
C                Supported aberration correction options for
C                transmission (case where radiation is emitted from
C                observer at ET) are:
C
C                   'XLT'        Light time only.
C                   'XLT+S'      Light time and stellar aberration.
C                   'XCN'        Converged Newtonian (CN) light time.
C                   'XCN+S'      CN light time and stellar aberration.
C
C                For detailed information, see the geometry finder
C                required reading, gf.req.
C
C                Case, leading and trailing blanks are not significant
C                in the string ABCORR.
C
C
C     OBSRVR     is the name of the body from which the target is
C                observed. The instrument designated by INST is treated
C                as if it were co-located with the observer.
C
C                Optionally, you may supply the integer NAIF ID code
C                for the body as a string.
C
C                Case and leading or trailing blanks are not
C                significant in the string OBSRVR.
C
C$ Detailed_Output
C
C     None.
C
C$ Parameters
C
C     None.
C
C$ Exceptions
C
C     1)  If the observer's name cannot be mapped to an ID code, the
C         error SPICE(IDCODENOTFOUND) is signaled.
C
C     2)  If the target is an ephemeris object and its name cannot be
C         mapped to an ID code, the error SPICE(IDCODENOTFOUND) is
C         signaled. If the target is represented by a ray, the input
C         target name argument is ignored.
C
C     3)  If target is an ephemeris object, and the observer and
C         target have the same ID codes, the error
C         SPICE(BODIESNOTDISTINCT) is signaled.
C
C     4)  If target is an ephemeris object, and the target shape
C         is not equivalent to PTSHAP (point) or EDSHAP (ellipsoid),
C         the error SPICE(INVALIDSHAPE) is signaled.
C
C     5)  If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the reference frame
C         argument TFRAME is blank, the error SPICE(INVALIDFRAME) is
C         signaled.
C
C     6)  If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the reference frame
C         argument TFRAME cannot be mapped to a frame ID code, the
C         error SPICE(INVALIDFRAME) is signaled.
C
C     7)  If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the reference frame
C         argument TFRAME's ID cannot be mapped to a frame description,
C         the error SPICE(FRAMEINFONOTFOUND) is signaled.
C
C     8)  If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the reference frame
C         specified by TFRAME is not centered on the target body, the
C         error SPICE(INVALIDFRAME) is signaled.
C
C     9)  If the target is represented by a ray and the aberration
C         correction flag calls for light time correction, the error
C         SPICE(INVALIDOPTION) is signaled.
C
C     10) If target is an ephemeris object and the aberration
C         correction flag calls for a correction not supported by
C         the SPICE SPK system, the error is diagnosed by a routine
C         in the call tree of this routine.
C
C     11) If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the kernel pool
C         does not contain radii for the target body,
C         not  target body, the error is diagnosed by a routine
C         in the call tree of this routine.
C
C     12) If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the kernel pool
C         contains the wrong number of radii for the target body, the
C         error SPICE(INVALIDDIMENSION) is signaled.
C
C     13) If target is an ephemeris object, the target shape is
C         equivalent to EDSHAP (ellipsoid), and the kernel pool
C         contains one or more non-positive radii for the target body,
C         the error SPICE(BADAXISLENGTH) is signaled.
C
C     14) If the target is represented by a ray and the ray's
C         direction vector is zero, the error SPICE(ZEROVECTOR) is
C         signaled.
C
C     15) If the instrument name INST cannot be mapped to an ID code,
C         the error SPICE(IDCODENOTFOUND) is signaled.
C
C     16) If an error occurs while fetching the instrument parameters
C         from the kernel pool, the error will be diagnosed by a
C         routine in the call tree of this routine.
C
C     17) If any ray defined by the observer's position and one of
C         the instrument FOV's boundary vectors fails to intersect
C         the "FOV plane"---a plane normal to the instrument FOV axis
C         and intersected by the FOV axis at distance 1 km from the
C         observer---the error SPICE(DEGENERATECASE) is signaled.
C
C     18) If the FOV is circular or elliptical and the FOV's radius
C         or one of the FOV's semi-axis lengths is zero, the error
C         SPICE(DEGENERATECASE) is signaled.
C
C     19) If the maximum angular separation of the instrument
C         FOV axis and any FOV boundary vector exceeds the limit
C         (which is slightly less than 90 degrees), either the error
C         SPICE(FOVTOOWIDE) will be signaled or the error will be
C         diagnosed by a routine in the call tree of this routine.
C
C
C$ Files
C
C     See the header of the umbrella routine ZZGFFVU.
C
C$ Particulars
C
C     This entry point initializes the parameters needed by the
C     occultation state determination entry point ZZGFFVST.
C
C$ Examples
C
C     See implementation of GFFOVE.
C
C$ Restrictions
C
C     1) The reference frame associated with INST must be
C        centered at the observer or must be inertial. No check is done
C        to ensure this.
C
C     2) This is a SPICELIB private routine; it should not be called by
C        user applications.
C
C$ Literature_References
C
C     None.
C
C$ Author_and_Institution
C
C     N.J. Bachman   (JPL)
C     L.S. Elson     (JPL)
C     W.L. Taber     (JPL)
C     E.D. Wright    (JPL)
C
C$ Version
C
C-    SPICELIB Version 1.1.0, 04-JUN-2013 (EDW)
C
C        ABCORR now stripped of all spaces before saving.
C        Specifically, the call
C
C        CALL LJUST ( ABCORR, SVCORR )
C
C        replaced with
C
C        CALL CMPRSS ( ' ', 0, ABCORR, SVCORR )
C
C-    SPICELIB Version 1.0.0, 05-MAR-2009 (NJB) (LSE) (WLT) (EDW)
C
C-&

C$ Revisions
C
C     None.
C
C-&

C
C     Standard SPICE error handling.
C
      IF ( RETURN () ) THEN
         RETURN
      END IF

      CALL CHKIN ( 'ZZGFFVIN' )

C
C     To avoid portability problems, initialize all
C     saved variables that aren't initialized via DATA
C     statements and aren't guaranteed to be initialized
C     for all cases.
C
      CALL CLEARD ( 3*MAXVRT, SVBNDS )
      CALL CLEARD ( 3,        SVEDCT )
      CALL CLEARD ( 3,        SVFAXI )
      CALL CLEARD ( 2*MAXVRT, SVFPOL )
      CALL CLEARD ( 9,        SVFSMX )
      CALL CLEARD ( UBPL,     SVPLAN )
      CALL CLEARD ( 3,        SVRDIR )
      SVTARG = 0
      SVTFRM = ' '
      SVTNAM = ' '
      CALL CLEARD ( 3,        SVTRAD )
      SVUSTL = .FALSE.
      SVXMIT = .FALSE.

C
C     Find the NAIF ID for OBSRVR.
C
      CALL BODS2C ( OBSRVR, SVOBS, FOUND )

      IF ( .NOT. FOUND ) THEN

         CALL SETMSG ( 'The observer, '
     .   //            '''#'', is not a recognized name for an '
     .   //            'ephemeris object. The cause of this '
     .   //            'problem may be that you need an updated '
     .   //            'version of the SPICE Toolkit. '           )
         CALL ERRCH  ( '#', OBSRVR                                )
         CALL SIGERR ( 'SPICE(IDCODENOTFOUND)'                    )
         CALL CHKOUT ( 'ZZGFFVIN'                                 )
         RETURN

      END IF

C
C     Process the target shape specifier here.
C
C     Save a left-justified, upper case version of the target shape
C     specifier.
C
      CALL LJUST ( TSHAPE, SVTSHP )
      CALL UCASE ( SVTSHP, SVTSHP )

C
C     Note for maintenance programmer: these checks will
C     require modification to handle DSK-based shapes.
C
      IF (      ( SVTSHP .NE. PTSHAP )
     .    .AND. ( SVTSHP .NE. EDSHAP )
     .    .AND. ( SVTSHP .NE. RYSHAP )  ) THEN

         CALL SETMSG ( 'The target shape specification, '
     .   //            '''#'', is not recognized.'       )
            CALL ERRCH  ( '#', TSHAPE                    )
         CALL SIGERR ( 'SPICE(INVALIDSHAPE)'             )
         CALL CHKOUT ( 'ZZGFFVIN'                        )
         RETURN

      END IF

C
C     We'll use the logical variable USERAY to indicate that the
C     target is modeled as ray.
C
      SVURAY = SVTSHP .EQ. RYSHAP

C
C     Indicate whether we have an extended target. SVXTRG is .TRUE.
C     if and only we have one.
C
      SVXTRG = SVTSHP .EQ. EDSHAP

C
C     If the target is an ephemeris object, obtain its ID code.
C     Save the target object's name, if applicable.
C
      IF ( .NOT. SVURAY ) THEN

         CALL BODS2C ( TARGET, SVTARG, FOUND )

         IF ( .NOT. FOUND ) THEN

            CALL SETMSG ( 'The target object, '
     .      //            '''#'', is not a recognized name for an '
     .      //            'ephemeris object. The cause of this '
     .      //            'problem may be that you need an updated '
     .      //            'version of the SPICE Toolkit. '           )
            CALL ERRCH  ( '#', TARGET                                )
            CALL SIGERR ( 'SPICE(IDCODENOTFOUND)'                    )
            CALL CHKOUT ( 'ZZGFFVIN'                                 )
            RETURN

         END IF

C
C        Save the target's name.
C
         SVTNAM = TARGET

C
C        Make sure the observer and target are distinct.
C
         IF ( SVTARG .EQ. SVOBS ) THEN

            CALL SETMSG ( 'The observer and target must be '
     .      //            'distinct objects, but are not: '
     .      //            'OBSRVR = #; TARGET = #;'         )
            CALL ERRCH  ( '#', OBSRVR                       )
            CALL ERRCH  ( '#', TARGET                       )
            CALL SIGERR ( 'SPICE(BODIESNOTDISTINCT)'        )
            CALL CHKOUT ( 'ZZGFFVIN'                        )
            RETURN

         END IF

      END IF

C
C     Process the target frame. The target frame is defined except
C     when the target is an ephemeris object modeled as a point.
C
      IF ( SVURAY .OR. SVXTRG ) THEN
C
C        We'll use the target frame argument. Look up the target
C        frame's ID code. But first, check for a blank frame name,
C        since this may be a common problem for the GF FOV system.
C
         IF ( TFRAME .EQ. ' ' ) THEN

            CALL SETMSG ( 'The target is not modeled as a '
     .      //            'point, but the associated '
     .      //            'frame name is blank.'           )
            CALL SIGERR ( 'SPICE(INVALIDFRAME)'            )
            CALL CHKOUT ( 'ZZGFFVIN'                       )
            RETURN

         END IF

         CALL NAMFRM ( TFRAME, FRAMID )

         IF ( FRAMID .EQ. 0 ) THEN

            CALL SETMSG ( 'The target frame name # is not recognized.' )
            CALL ERRCH  ( '#',  TFRAME                                 )
            CALL SIGERR ( 'SPICE(INVALIDFRAME)'                        )
            CALL CHKOUT ( 'ZZGFFVIN'                                   )
            RETURN

         END IF

C
C        Save the target frame name.
C
         CALL LJUST ( TFRAME, SVTFRM )
         CALL UCASE ( SVTFRM, SVTFRM )

C
C        Obtain the center of the frame. If the target is an ephemeris
C        object, we must verify the frame center is the target.
C
         CALL FRINFO ( FRAMID, FRCENT, FRCLSS, CLSSID, FOUND )

         IF ( .NOT. FOUND ) THEN
C
C           Since we mapped the frame name to an ID code, we expect to
C           find the frame info. Getting here may be a sign of an
C           invalid frame kernel.
C
            CALL SETMSG ( 'Frame ID found for # body-fixed '
     .      //            'frame # but FRINFO couldn''t find frame '
     .      //            'info. This may be due to a frame kernel '
     .      //            'error.'                                  )
            CALL ERRCH  ( '#',  TARGET                              )
            CALL SIGERR ( 'SPICE(FRAMEINFONOTFOUND)'                )
            CALL CHKOUT ( 'ZZGFFVIN'                                )
            RETURN

         END IF

         IF ( SVXTRG ) THEN
C
C           We have an extended target. Check the target frame's center.
C
            IF ( FRCENT .NE. SVTARG ) THEN
C
C              The supposed body-fixed frame for the target isn't
C              actually centered on the target.
C
               CALL SETMSG ( 'Supposed body-fixed frame # for '
     .         //            'target # is actually centered '
     .         //            'on body #.'                       )
               CALL ERRCH  ( '#',  TFRAME                       )
               CALL ERRCH  ( '#',  TARGET                       )
               CALL ERRINT ( '#',  FRCENT                       )
               CALL SIGERR ( 'SPICE(INVALIDFRAME)'              )
               CALL CHKOUT ( 'ZZGFFVIN'                         )
               RETURN

            END IF

         END IF

      END IF

C
C     Process the aberration correction specifier.
C
      IF ( SVURAY ) THEN
C
C        The target is represented by a ray. Check and save the
C        aberration correction.
C
         CALL ZZPRSCOR ( ABCORR, ATTBLK )

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVIN' )
            RETURN
         END IF

C
C        Reject aberration correction flags calling for any type of
C        light time correction. However, stellar aberration corrections
C        are allowed: note this is the reverse of the situation for
C        ephemeris objects. The allowed aberration correction flags are
C
C           'NONE', 'S', 'XS'
C
         IF ( ATTBLK(LTIDX) ) THEN

            CALL SETMSG ( 'Aberration correction flag # calls for '
     .      //            'light time corrections; these are not '
     .      //            'supported for targets represented by '
     .      //            'rays.'                                  )
            CALL ERRCH  ( '#', ABCORR                              )
            CALL SIGERR ( 'SPICE(INVALIDOPTION)'                   )
            CALL CHKOUT ( 'ZZGFFVIN'                               )
            RETURN

         END IF

C
C        Save flags indicating whether to use stellar aberration
C        corrections and indicating the sense of radiation travel.
C
         SVUSTL = ATTBLK(STLIDX)
         SVXMIT = ATTBLK(XMTIDX)

      ELSE
C
C        The target is an ephemeris object.
C
C        Check the aberration correction. If SPKEZR can't handle it,
C        neither can we.
C
         CALL ZZVALCOR ( ABCORR, ATTBLK )

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVIN' )
            RETURN
         END IF

      END IF

C
C     Remove all spaces from ABCORR then convert to uppercase. Save
C     this version of the aberration correction specifier.
C
      CALL CMPRSS ( ' ', 0, ABCORR, SVCORR )
      CALL UCASE ( SVCORR, SVCORR )

C
C     Process the target body's radii, if applicable.
C
      IF ( SVXTRG ) THEN
C
C        Fetch and check the radii.
C
         CALL BODVCD ( SVTARG, 'RADII', 3, N, SVTRAD )

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVIN' )
            RETURN
         END IF

C
C        Check the count of the radii.
C
         IF ( N .NE. 3 )  THEN

            CALL SETMSG ( 'Target # should have 3 radii but '
     .      //            'actually has #. This may be due '
     .      //            'to an error in a PCK file used '
     .      //            'to provide the radii.'             )
            CALL ERRCH  ( '#', TARGET                         )
            CALL ERRINT ( '#', N                              )
            CALL SIGERR ( 'SPICE(INVALIDDIMENSION)'           )
            CALL CHKOUT ( 'ZZGFFVIN'                          )
            RETURN

         END IF

C
C        Check to make sure the current target has 3 positive
C        semi-axis lengths.
C
         IF (     ( SVTRAD(1) .LE. 0.D0 )
     .       .OR. ( SVTRAD(2) .LE. 0.D0 )
     .       .OR. ( SVTRAD(3) .LE. 0.D0 ) ) THEN

            CALL SETMSG ( 'One or more semi-axis lengths of '
     .      //            'the target body # are non-'
     .      //            'positive: 1 = #, 2 = #, 3 = #. '  )
            CALL ERRCH  ( '#', TARGET                        )
            CALL ERRDP  ( '#', SVTRAD (1)                    )
            CALL ERRDP  ( '#', SVTRAD (2)                    )
            CALL ERRDP  ( '#', SVTRAD (3)                    )
            CALL SIGERR ( 'SPICE(BADAXISLENGTH)'             )
            CALL CHKOUT ( 'ZZGFFVIN'                         )
            RETURN

         END IF

C
C        Checks of radii have been completed.
C
      ELSE
C
C        We don't have an extended target body: zero out radius values
C        for this target.
C
         CALL CLEARD ( 3, SVTRAD )

      END IF

C
C     Check the direction vector, if applicable.
C
      IF ( SVURAY ) THEN
C
C        Make sure the direction vector is non-zero. Save a unit-length
C        copy of the vector.
C
         IF ( VZERO( RAYDIR ) ) THEN

            CALL SETMSG ( 'Input ray direction was the zero vector; '
     .      //            'this vector must be non-zero.'            )
            CALL SIGERR ( 'SPICE(ZEROVECTOR)'                        )
            CALL CHKOUT ( 'ZZGFFVIN'                                 )
            RETURN

         END IF

         CALL VHAT ( RAYDIR, SVRDIR )

      END IF

C
C     Look up the instrument's ID code.
C
      CALL BODS2C ( INST, SVINST, FOUND )

      IF ( .NOT. FOUND ) THEN

         CALL SETMSG ( '''#'' is not a recognized name for an '
     .   //            'instrument. The cause of this '
     .   //            'problem may be that you have not loaded '
     .   //            'a required frame kernel or instrument '
     .   //            'kernel.'                                  )
         CALL ERRCH  ( '#', INST                                  )
         CALL SIGERR ( 'SPICE(IDCODENOTFOUND)'                    )
         CALL CHKOUT ( 'ZZGFFVIN'                                 )
         RETURN

      END IF

C
C     Save the instrument's name.
C
      CALL LJUST ( INST,   SVINAM )
      CALL UCASE ( SVINAM, SVINAM )

C
C     Look up the instrument parameters.
C
      CALL GETFOV ( SVINST, MAXVRT, SVISHP,
     .              SVIFRM, BSITE,  SVNVRT, SVBNDS )

      IF ( FAILED() ) THEN
         CALL CHKOUT ( 'ZZGFFVIN' )
         RETURN
      END IF

C
C     Make sure the intrument shape specifier is left-justified
C     and in upper case.
C
      CALL LJUST ( SVISHP, SVISHP )
      CALL UCASE ( SVISHP, SVISHP )

C
C     If the instrument's shape is 'RECTANGLE', map it to
C     'POLYGON'
C
      IF (  SVISHP .EQ. RECFOV ) THEN

         SVISHP = POLFOV

      END IF

C
C     Save an axis vector for the FOV. For circular and ellipsoidal
C     FOVs, the boresight serves as this axis. For polygonal FOVs
C     (rectangular FOVs are included), we'll generate an axis vector.
C
      IF ( SVISHP .EQ. POLFOV ) THEN

         CALL ZZFOVAXI ( INST,  SVNVRT, SVBNDS, SVFAXI )

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVIN' )
            RETURN
         END IF

      ELSE

         CALL VEQU ( BSITE, SVFAXI )

      END IF

C
C     Check the angular radius of the FOV.
C
C     Compute the angular radius of the FOV. We'll use this to define a
C     "bounding cone" centered on the FOV axis and having its apex at
C     the observer. This cone will be used for a preliminary FOV
C     exclusion test.
C
      SVARAD = 0.D0

      DO I = 1, SVNVRT

         SVARAD = MAX (  SVARAD,  VSEP( SVBNDS(1,I), SVFAXI )  )

      END DO

C
C     Our algorithms can't handle FOVs with angular radius of 90
C     degrees.
C
      IF (  SVARAD   .GT. ( HALFPI()-ATOL )  ) THEN

         CALL SETMSG ( 'FOV angular radius of # degrees '
     .   //            'exceeds limit of # degrees.'     )
         CALL ERRDP  ( '#',  SVARAD*DPR()                )
         CALL ERRDP  ( '#',  (HALFPI()-ATOL)*DPR()       )
         CALL SIGERR ( 'SPICE(FOVTOOWIDE)'               )
         CALL CHKOUT ( 'ZZGFFVIN'                        )
         RETURN

      END IF

C
C     Convert the FOV shape specifier to a left-justified, upper
C     case form.
C
      CALL LJUST ( SVISHP, SVISHP )
      CALL UCASE ( SVISHP, SVISHP )

C
C     We can make the search more efficient by computing any
C     required, time-invariant quantities here in the initialization
C     routine.
C
C     Compute the FOV plane SVPLAN, which is represented in the
C     instrument frame. The origin will be considered to be located at
C     the observer. The plane is normal to the FOV axis, at distance 1
C     unit from the observer.
C
      CALL NVC2PL ( SVFAXI, 1.D0, SVPLAN )

C
C     Find the point on the plane closest to the origin. This is
C     the center of the FOV.
C
      CALL VHAT ( SVFAXI, SVFVCT )

C
C     If applicable, perform the computations required for an
C     elliptical FOV, where the target representation is arbitrary, or
C     a circular FOV when the target is an extended object.
C
      IF (        ( SVISHP .EQ. ELLFOV )
     .     .OR. ( ( SVISHP .EQ. CIRFOV ) .AND. SVXTRG )  ) THEN
C
C        Also compute the center, semi-axis vectors, and semi-axis
C        lengths of the FOV. If the FOV is circular, we create an
C        artificial, second semi-axis vector.
C
         IF ( SVISHP .EQ. CIRFOV ) THEN
C
C           We have a circular FOV. We'll create an artificial, second
C           boundary vector, which will give rise to a second
C           semi-axis.
C
            CALL VROTV ( SVBNDS(1,1), SVFAXI, HALFPI(), SVBNDS(1,2) )

         END IF

C
C        Now find the endpoints of the semi-axes in this plane.
C
         DO I = 1, 2

            CALL INRYPL ( SVORIG, SVBNDS(1,I),
     .                    SVPLAN, NXPTS,       SEMIPT(1,I) )

            IF ( NXPTS .NE. 1 ) THEN

               CALL SETMSG ( 'Error creating FOV semi-axis '
     .         //            'vectors, NXPTS = #. This '
     .         //            'may indicate an error in the '
     .         //            'IK parameters for #.'         )
               CALL ERRINT ( '#', NXPTS                     )
               CALL ERRCH  ( '#', INST                      )
               CALL SIGERR ( 'SPICE(DEGENERATECASE)'        )
               CALL CHKOUT ( 'ZZGFFVIN'                     )
               RETURN

            END IF

C
C           Compute and find the length of each semi-axis vector.
C
            CALL VSUB ( SEMIPT(1,I), SVFVCT, SVSEMI(1,I) )

            SVXMAG(I) = VNORM ( SVSEMI(1,I) )

            IF ( SVXMAG(I) .EQ. 0.D0 ) THEN

               CALL SETMSG ( 'FOV semi-axis #* for @ '
     .         //            'has zero length.'        )
               CALL ERRINT ( '*', I                    )
               CALL ERRCH  ( '@', INST                 )
               CALL SIGERR ( 'SPICE(DEGENERATECASE)'   )
               CALL CHKOUT ( 'ZZGFFVIN'                )
               RETURN

            END IF

         END DO

      END IF

C
C     If we have an ellipsoidal target, and the FOV is circular or
C     elliptical, we'll create an ellipsoid whose limb coincides with
C     the FOV. This allows use to later use ZZOCCED to determine the
C     target's visibility.
C
      IF (   (      ( SVISHP .EQ. CIRFOV )
     .         .OR. ( SVISHP .EQ. ELLFOV ) )  .AND.  SVXTRG  ) THEN
C
C        Create an ellipsoid whose semi-axes are consistent with
C        ellipse in SVPLAN defined by SEMIPT. To start out, select the
C        center of the ellipsoid. We place the center along the
C        direction defined by the FOV axis, at a distance beyond
C        SVPLAN (that is, on the side of the plane opposite the
C        observer), such that a sphere centered at this point would
C        have a limb consisting of a circle of radius SVXMAG(1). If
C        CTREXT is the distance of the ellipsoid center from SVFVCT,
C        then the limb geometry requires
C
C           CTREXT / SVXMAG(1) = SVXMAG(1) / 1
C
C
         CTREXT = SVXMAG(1) ** 2

C
C        The ellipsoid's center is SVEDCT.
C
         CALL VSCL ( 1.D0 + CTREXT, SVFVCT, SVEDCT )

C
C        NOTE: in the code and discussion that follow, there are
C        references to both the FOV center SVFVCT and the ellipsoid
C        center SVEDCT. Note that the directions of the ellipsoid's
C        semi-axes point from the FOV center, NOT the ellipsoid center,
C        toward the intercepts of the FOV boundary vectors on the
C        FOV plane.
C
C        Compute the radius of the sphere centered at SVEDCT. The
C        ellipsoid's semi-axes pointing in the FOV axis direction and
C        in the direction from SVFVCT toward SEMIPT(*,1) will have this
C        length.
C
         FOVRAD(3) = SVXMAG(1) * SQRT ( 1.D0 + SVXMAG(1)**2.D0 )
         FOVRAD(1) = FOVRAD(3)

C
C        Compute the corresponding columns of the FOV semi-axis matrix.
C
C        The ellipsoid's third axis points along the FOV axis:
C
         CALL VSCL ( FOVRAD(3), SVFVCT, SVFSMX(1,3) )

C
C        The first ellipsoid semi-axis is associated with SEMIPT(*,1)
C        and also has length FOVRAD(3):
C
         CALL VHAT ( SVSEMI(1,1), VTEMP )
         CALL VSCL ( FOVRAD(1),   VTEMP,  SVFSMX(1,1) )

C
C        The ellipsoid's second semi-axis points from SVFVCT toward
C        SEMIPT(*,2). The ratio of its length to that of the other
C        semi-axis is the ratio of the length of the FOV's second
C        semi-axis to that of its first. Note that we've already ruled
C        out divide-by-zero errors here.
C
         FOVRAD(2) = ( SVXMAG(2)/SVXMAG(1) ) * FOVRAD(3)

C
C        We define the third axis using a cross product to
C        ensure we produce a matrix with positive determinant.
C
         CALL UCRSS  ( SVFSMX(1,3), SVFSMX(1,1), VTEMP )
         CALL VSCL   ( FOVRAD(2),   VTEMP,       SVFSMX(1,2) )

      END IF

      IF ( ( SVISHP .EQ. CIRFOV ) .AND. ( .NOT. SVXTRG ) ) THEN
C
C        We have a circular FOV and a point or ray target model.
C        In this case, our FOV inclusion test is simple as can
C        be: we just compare the angular separation of the
C        target and FOV axis against the angular radius of the
C        FOV. Compute and save this angular radius.
C
         SVARAD = VSEP ( SVFAXI, SVBNDS(1,1) )

      ELSE IF (       (      ( SVISHP .EQ. RECFOV )
     .                  .OR. ( SVISHP .EQ. POLFOV ) )
     .          .AND. ( .NOT.  SVXTRG               )   ) THEN
C
C        We have a rectangular or polygonal FOV and a ray or point
C        target.
C
C        We're going to represent the FOV boundary by a polygon
C        in the FOV plane SVPLAN. We want to be able to use a
C        2-dimensional winding number computation to decide whether
C        the target is within the FOV. We'll need a reference
C        frame with the Z-axis parallel to the FOV axis vector;
C        we'll represent the intersections of the boundary vectors
C        with the FOV plane in this frame. Then our 2D polygon
C        will have vertices given by the (X,Y) components of each
C        intersection.
C
         CALL VEQU ( SVFAXI, Z )

         CALL FRAME ( Z, X, Y )

         DO I = 1, 3
            SVFOVM(1,I) = X(I)
            SVFOVM(2,I) = Y(I)
            SVFOVM(3,I) = Z(I)
         END DO

C
C        Compute the intersections of the FOV boundary vectors with the
C        FOV plane. For each intercept, find the vector pointing from
C        the FOV center to that intercept. Transform each such
C        difference vector into the FOV frame. Save the projection onto
C        the FOV frame's X-Y plane.
C
         DO I = 1, SVNVRT

            CALL INRYPL ( SVORIG, SVBNDS(1,I),
     .                    SVPLAN, NXPTS,       XPT)

            IF ( NXPTS .NE. 1 ) THEN

               CALL SETMSG ( 'Error finding FOV plane intercept '
     .         //            'of FOV boundary vector #, NXPTS = #. '
     .         //            'This may indicate an error '
     .         //            'in the IK parameters for #.'          )
               CALL ERRINT ( '#', I                                 )
               CALL ERRINT ( '#', NXPTS                             )
               CALL ERRCH  ( '#', INST                              )
               CALL SIGERR ( 'SPICE(DEGENERATECASE)'                )
               CALL CHKOUT ( 'ZZGFFVIN'                             )
               RETURN

            END IF

            CALL VSUB ( XPT,   SVFVCT, VTEMP  )
            CALL MXV ( SVFOVM, VTEMP,  VTEMP2 )

            SVFPOL(1,I) = VTEMP2(1)
            SVFPOL(2,I) = VTEMP2(2)

         END DO

      END IF

      CALL CHKOUT ( 'ZZGFFVIN' )
      RETURN

C$Procedure ZZGFFVST ( GF, "is target in FOV?"  )

      ENTRY ZZGFFVST ( TIME, VISTAT )

C$ Abstract
C
C     Indicate whether the target is currently in the instrument FOV.
C
C$ Disclaimer
C
C     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE
C     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S.
C     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE
C     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE
C     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS"
C     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY
C     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A
C     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC
C     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
C     SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
C
C     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA
C     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT
C     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND,
C     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
C     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE
C     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
C
C     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF
C     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY
C     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE
C     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE.
C
C$ Required_Reading
C
C     SPK
C     TIME
C
C$ Keywords
C
C     SEARCH
C     GEOMETRY
C
C$ Declarations
C
C     DOUBLE PRECISION      TIME
C     LOGICAL               VISTAT
C
C$ Brief_I/O
C
C     VARIABLE  I/O  DESCRIPTION
C     --------  ---  --------------------------------------------------
C     TIME       I   TDB epoch (in seconds past J2000)
C     VISTAT     O   .TRUE. if the object is visible, .FALSE.
C                    otherwise.
C
C$ Detailed_Input
C
C     TIME       is the epoch of interest in TDB seconds past the
C                J2000 epoch.
C
C$ Detailed_Output
C
C     VISTAT     is a logical flag indicating the state of visibility.
C                If the target is in the instrument FOV at epoch TIME,
C                where target and instrument are those specified by the
C                last call to ZZGFFVIN, VISTAT is returned with the
C                value .TRUE.; otherwise VISTAT is .FALSE.
C
C$ Parameters
C
C     None.
C
C$ Exceptions
C
C     1) If any SPK lookup fails, the error will be diagnosed by
C        routines in the call tree of this routine.
C
C     2) If any frame transformation lookup fails, the error will be
C        diagnosed by routines in the call tree of this routine.
C
C     3) If the FOV is polygonal, the target is an ellipsoid,
C        and while testing whether the target is visible, an error
C        occurs due to FOV errors not detected in the initialization
C        step, the error will be diagnosed by routines in the call tree
C        of this routine.
C
C     4) If the FOV is circular or elliptical, the target is an
C        ellipsoid, and while testing whether the target is visible, an
C        error occurs due to degenerate geometry of the limb, FOV, or
C        both, the error will be diagnosed by routines in the call tree
C        of this routine.
C
C     5) If the target shape is not recognized, the error will be
C        diagnosed by routines in the call tree of this routine.
C
C$ Files
C
C     See the Files header section of the umbrella routine ZZGFFVU.
C
C$ Particulars
C
C     This routine determines the visibility state of the
C     configuration specified by the last call to ZZGFFVIN and the
C     input time value.
C
C$ Examples
C
C     See the umbrella routine ZZGFFVU.
C
C$ Restrictions
C
C     This is a SPICELIB private routine; it should not be called by
C     user applications.
C
C$ Literature_References
C
C     None.
C
C$ Author_and_Institution
C
C     N.J. Bachman   (JPL)
C     L.S. Elson     (JPL)
C     W.L. Taber     (JPL)
C     E.D. Wright    (JPL)
C
C$ Version
C
C-    SPICELIB Version 1.0.0, 05-MAR-2009 (NJB) (LSE) (WLT) (EDW)
C
C-&

C
C     Standard SPICE error handling.
C
      IF ( RETURN () ) THEN
         RETURN
      END IF

      CALL CHKIN  ( 'ZZGFFVST' )

C
C     Initialize the state output.
C
      VISTAT = .FALSE.

C
C     The algorithm for the state determination depends on the
C     target model and the FOV shape.
C
      IF ( SVXTRG ) THEN
C
C        The target is an ephemeris object modeled as an extended
C        body. There are two branches here: one for a rectangular/
C        polygonal FOV and one for a circular/elliptical FOV.
C
C        Start by finding the observer-target position vector in the
C        target body-fixed frame.
C
         CALL SPKEZP ( SVTARG, TIME, SVTFRM, SVCORR, SVOBS, POS, LT )

C
C        Compute the target epoch.
C
         CALL ZZCOREPC ( SVCORR, TIME, LT, ETTARG )

C
C        Find the transformation from the target frame at ETTARG to the
C        instrument frame at TIME. We'll need to use J2000 as an
C        intermediate frame.
C
         CALL PXFORM ( SVTFRM,  'J2000', ETTARG, M1 )
         CALL PXFORM ( 'J2000', SVIFRM,  TIME,   M2 )

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVST' )
            RETURN
         END IF

         CALL MXM ( M2, M1, INSMAT )

         IF ( ( SVISHP .EQ. RECFOV ) .OR. ( SVISHP .EQ. POLFOV ) ) THEN
C
C           The FOV is a rectangle or other polygon; we treat both
C           cases the same way.
C
C           Negate POS to obtain the position of the observer with
C           respect to the target.
C
            CALL VMINUS ( POS, OBSPOS )

C
C           Find the limb in the target body-fixed frame.
C
            CALL EDLIMB ( SVTRAD(1), SVTRAD(2), SVTRAD(3),
     .                    OBSPOS,    LIMB                  )

C
C           Transform the limb from the target frame at ETTARG
C           to the instrument frame at TIME. The matrix INSMAT
C           effects just this transformation. We unpack the center
C           and semi-axis vectors of LIMB, transform them, and
C           pack the results into FVLIMB. Below, M1 and M2 are
C           simply temporary 3x3 matrices.
C
            CALL EL2CGV ( LIMB, M1(1,1), M1(1,2), M1(1,3) )

C
C           Before performing the frame transformation on the
C           limb's center, translate the center so that the
C           observer is at the origin. Since POS is expressed
C           in the target body-fixed frame, this is a convenient
C           place for the translation.
C
            CALL VADD ( POS,   M1(1,1), VTEMP )
            CALL VEQU ( VTEMP, M1(1,1)        )

            DO I = 1, 3
               CALL MXV ( INSMAT, M1(1,I), M2(1,I) )
            END DO

            CALL CGV2EL (  M2(1,1), M2(1,2), M2(1,3), FVLIMB )

C
C           All geometric objects in the following call are expressed
C           in the instrument reference frame.
C
C           The target is in the FOV if and only if ZZELVUPY finds an
C           intersection, so we use VISTAT as the "found" flag.
C
            CALL ZZELVUPY ( FVLIMB, SVORIG, SVFAXI,
     .                      SVNVRT, SVBNDS, VISTAT )

         ELSE IF (      ( SVISHP .EQ. CIRFOV )
     .             .OR. ( SVISHP .EQ. ELLFOV )  ) THEN
C
C           The FOV is a circle or ellipse. For both FOV shapes,
C           we represent the FOV by an ellipsoid in the FOV
C           frame. We can then use ZZOCCED to determine whether
C           there's any overlap of this ellipsoid and the target.
C
C           We'll perform the occultation test in the instrument frame,
C           so we'll need to represent the observer-target position
C           and target semi-axes in that frame.
C
C           Transform the target position to the instrument frame.
C
            CALL MXV ( INSMAT, POS, TRGCTR )

C
C           The columns of INSMAT are the target body's semi-axis
C           direction vectors; we scale these by the target radii
C           to obtain the semi-axis matrix for the target.
C
            DO I = 1, 3
               CALL VSCL ( SVTRAD(I), INSMAT(1,I), TRGSMX(1,I) )
            END DO

            OCSTAT = ZZOCCED ( SVORIG, SVEDCT, SVFSMX, TRGCTR, TRGSMX )

C
C           A return code of zero indicates no occultation. Any other
C           return code indicates a non-empty intersection of the
C           target and FOV.
C
            VISTAT = OCSTAT .NE. 0

         ELSE
C
C           This is an unexpected FOV shape. We should have prevented
C           this problem in the initialization step, but we repeat the
C           check here for safety.
C
            CALL SETMSG ( 'The target body # has shape #; the only '
     .      //            'supported shapes are ELLIPSOID, POINT, '
     .      //            'and RAY.'                                )
            CALL ERRCH  ( '#',  SVTNAM                              )
            CALL ERRCH  ( '#',  SVISHP                              )
            CALL SIGERR ( 'SPICE(INVALIDSHAPE)'                     )
            CALL CHKOUT ( 'ZZGFFVST'                                )
            RETURN

         END IF

C
C        This is the end of the ellipsoidal target case. At this
C        point, VISTAT is set.
C

      ELSE
C
C        The target is a ray or an ephemeris object modeled as a point.
C        In either case, we want to obtain the aberration-corrected
C        observer-target vector.
C
         IF ( SVURAY ) THEN
C
C           The target is represented by a ray expressed in the
C           frame SVTFRM.
C
C           Normally we'd need to correct the orientation of SVTFRM
C           for light time between the center of that frame and the
C           observer. But since light time corrections are not allowed
C           for targets represented by rays, we evaluate SVTFRM
C           at the current epoch TIME.
C
            CALL PXFORM ( SVTFRM, SVIFRM, TIME, INSMAT )

            IF ( FAILED() ) THEN
               CALL CHKOUT ( 'ZZGFFVST' )
               RETURN
            END IF

C
C           Transform the ray's direction vector to the instrument
C           frame.
C
            CALL MXV ( INSMAT, SVRDIR, DIR )

C
C           If we need to correct the ray's direction for stellar
C           aberration, do it now.
C
            IF ( SVUSTL ) THEN
C
C              Find the state of the observer relative to the
C              solar system barycenter in the J2000 frame.
C
               CALL SPKSSB ( SVOBS, TIME, 'J2000', STOBS )

C
C              Convert the direction vector to the J2000 frame.
C
               CALL PXFORM ( SVIFRM, 'J2000', TIME, M1 )

               IF ( FAILED() ) THEN
                  CALL CHKOUT ( 'ZZGFFVST' )
                  RETURN
               END IF

               CALL MXV ( M1, DIR, VTEMP )

C
C              Apply the stellar aberration correction.
C
               IF ( SVXMIT ) THEN
C
C                 Use the transmission correction.
C
                  CALL STLABX ( VTEMP, STOBS(4), VTEMP2 )
               ELSE
                  CALL STELAB ( VTEMP, STOBS(4), VTEMP2 )
               END IF

C
C              Map the direction vector back to the instrument
C              frame.
C
               CALL MTXV ( M1, VTEMP2, DIR )

            END IF
C
C           The target direction in the instrument frame DIR has
C           been computed.
C

         ELSE
C
C           The target is an ephemeris object. Look up the
C           target's position relative to the observer.
C
C           Note for the maintenance programmer: don't think of
C           changing this call to look up the position in the
C           instrument frame. :) Since we don't have a guarantee that
C           the instrument frame is centered on the observer (the frame
C           could be J2000, for example), and since we don't want to
C           correct the orientation of the instrument frame for light
C           time, we look up the direction vector in the J2000 frame
C           and then map it to the instrument frame.
C
            CALL SPKEZP ( SVTARG,  TIME,   'J2000',
     .                    SVCORR,  SVOBS,  VTEMP, LT )

            CALL PXFORM ( 'J2000', SVIFRM, TIME,  M1 )

            IF ( FAILED() ) THEN
               CALL CHKOUT ( 'ZZGFFVST' )
               RETURN
            END IF

            CALL MXV ( M1, VTEMP, DIR )

         END IF

         IF ( FAILED() ) THEN
            CALL CHKOUT ( 'ZZGFFVST' )
            RETURN
         END IF

C
C        The observer-target direction vector DIR is set.
C
C        The determination of whether the ray is in the FOV depends
C        on the FOV shape.
C
         SEP = VSEP( DIR, SVFAXI )

         IF ( SVISHP .EQ. CIRFOV ) THEN
C
C           Just compare the angular separation of POS with the
C           FOV axis direction against the FOV angular radius SVARAD.
C
            VISTAT =  SEP .LE. SVARAD

         ELSE IF ( SEP .GT. SVARAD ) THEN
C
C           The FOV is an ellipse or polygon.
C
C           The angular separation of target and FOV axis is
C           greater than the angular radius of the exclusion
C           cone. The target can't be seen.
C
            VISTAT = .FALSE.

         ELSE
C
C           The FOV is an ellipse or polygon.
C
C           The angular separation of target and FOV axis is
C           less than or equal to than the angular radius of the
C           exclusion code, so the target may be visible.
C
C           Find the intersection of the ray emanating from the
C           observer, and having direction vector POS, with the FOV
C           plane.
C
            CALL INRYPL ( SVORIG, DIR, SVPLAN, NXPTS, XPT )

C
C           If there's no intersection, the target isn't visible.
C
            IF ( NXPTS .EQ. 0 ) THEN

               VISTAT = .FALSE.

            ELSE IF ( NXPTS .NE. 1 ) THEN
C
C              "This can't happen." :)
C
               CALL SETMSG ( 'By construction, the vertex of '
     .         //            'the observer-target ray can''t '
     .         //            'lie in the FOV plane. If '
     .         //            'somehow it does, we have a serious '
     .         //            'problem.'                           )
               CALL SIGERR ( 'SPICE(BUG)'                         )
               CALL CHKOUT ( 'ZZGFFVST'                           )
               RETURN

            ELSE
C
C              NXPTS is 1.
C
C              Find the vector from the center of the FOV to XPT.
C              Call this vector FOVPT.
C
               CALL VSUB ( XPT, SVFVCT, FOVPT )

               IF ( SVISHP .EQ. ELLFOV ) THEN
C
C                 The FOV shape is elliptical. To decide whether FOVPT
C                 is within the FOV, compute the level surface
C                 parameter
C
C                                   2              2
C                    L  =  ( x / a )   +  ( y / b )
C
C                 and compare L to 1. We'll use the variable COORD
C                 to represent the coordinates (x,y).
C
C                 We've already eliminated zero divisors in the
C                 initialization routine.
C
                  DO I = 1, 2
                     COORD(I) = VDOT( FOVPT, SVSEMI(1,I) )  /  SVXMAG(I)
                  END DO

                  L =   ( COORD(1) / SVXMAG(1) )**2.D0
     .                + ( COORD(2) / SVXMAG(2) )**2.D0

C
C                 The target is visible if FOVPT is inside the FOV
C                 ellipse; this condition is indicated by L <= 1.
C
                  VISTAT = L .LE. 1.D0

               ELSE IF ( SVISHP .EQ. POLFOV ) THEN
C
C                The FOV is a polygon. Convert FOVPT to the FOV frame,
C                then find the winding number of the FOV about the X-Y
C                projection of FOVPT.
C
                  CALL MXV ( SVFOVM, FOVPT, VTEMP )

                  PNT2D(1) = VTEMP(1)
                  PNT2D(2) = VTEMP(2)

                  W        = ZZWIND2D ( SVNVRT, SVFPOL, PNT2D )

C
C                 Any non-zero winding number indicates that the
C                 FOV polygon wraps around the point representing
C                 the intercept of the target direction with the
C                 FOV plane.
C
                  VISTAT   = W .NE. 0

               ELSE
C
C                 This is an unexpected FOV shape. We should have
C                 prevented this problem in the initialization step,
C                 but we repeat the check here for safety.
C
                  CALL SETMSG ( 'Instrument #''s FOV has shape #; '
     .            //            'the only supported shapes are '
     .            //            'ELLIPSE, CIRCLE, and POLYGON.'    )
                  CALL ERRCH  ( '#',  SVINAM                       )
                  CALL ERRCH  ( '#',  SVISHP                       )
                  CALL SIGERR ( 'SPICE(INVALIDSHAPE)'              )
                  CALL CHKOUT ( 'ZZGFFVST'                         )
                  RETURN

               END IF
C
C              We've performed visibility tests for elliptical or
C              polygonal FOVs. VISTAT is set.
C
            END IF
C
C           We've processed the intercept found by the INRYPL call,
C           or, if the intercept count was not 1, indicated that the
C           target is not visible. VISTAT is set.
C
         END IF
C
C        We've processed both the ray and point ephemeris object
C        cases. VISTAT is set.
C
      END IF
C
C     We've processed all target representation/FOV shape cases.
C

      CALL CHKOUT ( 'ZZGFFVST' )
      RETURN
      END


	 SUBROUTINE INPUT2(NSTA,KLAS,MMAX,IW,MSTA,PRMK,W,JMIN,P,S,
     &  SRMK,WS,AMX,PRX,CALX,RMK,DT,FMP,AZRES,SYM,QRMK,KDX,LDX,JDX,
     &  TP,WRK,KSMP,PMIN,TS,CALR,TIME1,TIME2,MDATE,
     &  MHRMN,NR,MJUMP)
	include 'H_param.f'
C------- INPUT PHASE LIST ----------------------------------------------
      CHARACTER*1 IW(N_s),SYM(N_d),QRMK(N_d)
      CHARACTER*3 RMK(N_d)
      CHARACTER*4 AS,IPRO,NSTA(N_s),ISW
      CHARACTER*48 AHEAD
      CHARACTER*4 MSTA(N_d),PRMK(N_d),SRMK(N_d),AZRES(N_d),WRK(N_d)
      CHARACTER*80 ICARD
      INTEGER*4 JMIN(N_d),KDX(N_d),LDX(N_d)
      INTEGER*4 JDX(N_s),KSMP(N_s),KLAS(N_s),MDATE(N_s),MHRMN(N_s)
      REAL*4 W(N_d),P(N_d),S(N_d),WS(N_d),AMX(N_d),PRX(N_d),CALX(N_d)
      REAL*4 DT(N_d),FMP(N_d),TP(N_d),TS(N_d)
      REAL*4 CALR(N_s)
      REAL*8 TIME1,TIME2
      COMMON/C3/ AHEAD,IPRO,ISW
      COMMON/C4/ NL,NS,KDATE,KHR,NEAR,IEXIT,IDXS
	common/dts/stdt(N_s)
C-----------------------------------------------------------------------
   10 PMIN=9999.
      IDXS=0
      DO 20 I=1,NS
      KSMP(I)=0
   20 JDX(I)=0
      L=1
   30 read(12,'(A)',END=300) ICARD
      read(ICARD,35)
     1 MSTA(L),PRMK(L),W(L),JTIME,jz,JMIN(L),P(L),S(L)
     2,SRMK(L),WS(L),AMX(L),PRX(L),CALP,CALX(L),RMK(L),DT(L),FMP(L)
     3,AZRES(L),SYM(L),AS,QRMK(L),IPRO
	jtime=jtime*100+jz
   35 FORMAT(2A4,T8,F1.0,T10,I6,i2,I2,F5.2,T32,F5.2,A4,T40,F1.0,T44,F4.0
     1,F3.2,F4.1,T59,F4.1,A3,F5.2,F5.0,T21,A4,T7,A1,T32,A4,T63,A1,T5,A4)
	if(msta(l).eq.'$   ')then
		mjump=3
		return
	endif
      IF ((MSTA(L).EQ.' ***').OR.(MSTA(L).EQ.' $$$').OR.
     &   (MSTA(L).EQ.' ###')) GOTO 300
      IF (MSTA(L).EQ.'    ') GO TO 350
      IF (CALX(L) .LT. 0.01) CALX(L)=CALP
      IF (AS .EQ. '    ') S(L) = 999.99
      DO 40 I=1,NS
      IF (MSTA(L) .EQ. NSTA(I)) GO TO 50
   40 CONTINUE
      WRITE(8,45) ICARD,MSTA(L)
   45 FORMAT(///,' ***** ',A80,' ***** DELETED: ',A4,' NOT ON STATION L'
     1,'IST')
      GO TO 30
   50 KDX(L)=I
      LDX(L)=0
      JDX(I)=1
	dt(l)=dt(l)+stdt(i)
      IF (FMP(L) .LE. 0.) FMP(L)=0.
      IF (L .GT. 1) GO TO 60
      KTIME=JTIME
      KDATE=KTIME/100
      KHR=KTIME-KDATE*100
   60 IF (JTIME .EQ. KTIME) GO TO 70
      WRITE(8,65) ICARD
   65 FORMAT(///,' ***** ',A80,' ***** DELETED: WRONG TIME')
      GO TO 30
   70 IF (RMK(L) .EQ. 'CAL') GO TO 200
      W(L)=(4.-W(L))/4.
      IF (IW(I) .EQ. '*') W(L)=0.
      TP(L)=60.*JMIN(L)+P(L)+DT(L)
      WRK(L)='    '
      IF (W(L) .EQ. 0.) GO TO 90
      IF (W(L) .GT. 0.) GO TO 89
C------- SMP DATA: RESET WEIGHT ----------------------------------------
      W(L)=(4.-WS(L))/4.
      KSMP(L)=1
      IF(TP(L).GE.PMIN) GO TO 95
      PMIN=TP(L)
      NEAR=L
      GO TO 95
   89 IF (TP(L) .GE. PMIN) GO TO 90
      PMIN=TP(L)
      NEAR=L
   90 IF (AS .EQ. '    ') GO TO 100
C------- S DATA --------------------------------------------------------
      IDXS=1
      LDX(L)=1
      WS(L)=(4.-WS(L))/4.
      IF (IW(I) .EQ. '*') WS(L)=0.
   95 TS(L)=60.*JMIN(L)+S(L)+DT(L)
  100 L = L + 1
      IF (L .LT. MMAX) GO TO 30
      WRITE(8,105)
  105 FORMAT(///,' ***** ERROR: PHASE LIST EXCEEDS ARRAY DIMENSION; EXTR
     1A DATA TREATED AS NEXT EARTHQUAKE')
      GO TO 350
C------- CALIBRATION CHANGE IN STATION LIST ----------------------------
  200 IF (P(L) .NE. 0.) KLAS(I)=P(L)
      CALR(I)=CALX(L)
      TIME2=1.D+06*KDATE+1.D+04*KHR+1.D+02*JMIN(L)
      IF (TIME2 .GE. TIME1) GO TO 250
      WRITE(8,205)
  205 FORMAT(///,' ********** THE FOLLOWING EVENT IS OUT OF CHRONOLOGICA
     1L ORDER **********')
  250 WRITE(8,255) KDATE,KHR,JMIN(L),MSTA(L),KLAS(I),CALR(I)
  255 FORMAT(///,' ***** ',I6,1X,2I2,' ***** CALIBRATION CHANGE FOR ',A4
     1,': KLAS = ',I1,', CALR = ',F4.1)
      MDATE(I)=KDATE
      MHRMN(I)=100*KHR+JMIN(L)
      TIME1=TIME2
      GO TO 10
  300 MJUMP=1
      NR=L-1
      RETURN
  350 MJUMP=0
      NR=L-1
      RETURN
      END
      SUBROUTINE OUTPUT(TEST,KNO,IW,INS,IEW,DLY,FMGC,XMGC,
     &  KLAS,PRR,CALR,ICAL,FLT,QSPA,MSTA,
     &  PRMK,JMIN,P,S,SRMK,AMX,PRX,CALX,RMK,DT,FMP,AZRES,QRMK,KDX,LDX,
     &  WT,TP,T,WRK,KSMP,TS,TIME1,TIME2,DELTA,DX,DY,AVXM,
     &  XMAG,AVFM,FMAG,MAG,FNO,X,B,Y,SE,AF,AZ,AIN,ANIN,TEMP,KEY)
	include 'H_param.f'
C------- OUTPUT HYPOCENTER ---------------------------------------------
      CHARACTER*1 RMKO,RMK2,RMK3,RMK4,Q,QS,QD,SYM3
      CHARACTER*1 CLASS(4),SYMBOL(5),QRMK(N_d),IW(N_s),INS(N_s),IEW(N_s)
      CHARACTER*3 RMK(N_d)
      CHARACTER*4 ISW,XMAGOU,FMAGOU,SWTOUT,FMPOUT,RMK5,IPRO
      CHARACTER*4 MSTA(N_d),PRMK(N_d),WRK(N_d),AZRES(N_d),SRMK(N_d)
      CHARACTER*5 ERHOUT,SE3OUT
      CHARACTER*6 MAGOUT,SKOUT,TSKOUT,SRESOU,DTKOUT,X4KOUT,TSKCAL
      character*6 stmp
      CHARACTER*13 Coord_Cartes
      CHARACTER*48 AHEAD
      CHARACTER*80 SUCARD
      INTEGER*4 JMIN(N_d),KDX(N_d),LDX(N_d),KEY(N_d)
      INTEGER*4 KSMP(N_s),ICAL(N_s),KLAS(N_s)
      REAL LAT2,LON2,LATEP,LONEP,MAG,LATR,LONR
      REAL*4 AF(3),B(4),Y(4),SE(4),TEST(20),X(4,N_d),QSPA(9,40)
      REAL*4 FLT(2,N_s),DLY(2,N_s)
      REAL*4 AMX(N_d),PRX(N_d),CALX(N_d),CAL(N_d),XMAG(N_d)
      REAL*4 FMP(N_d),FMAG(N_d),TEMP(N_d),DEMP(N_d)
      REAL*4 DELTA(N_d),DX(N_d),DY(N_d),ANIN(N_d),AIN(N_d),AZ(N_d)
      REAL*4 WT(N_d),T(N_d),P(N_d),TP(N_d),DT(N_d),S(N_d),TS(N_d)
      REAL*4 FMGC(N_s),XMGC(N_s),PRR(N_s),CALR(N_s)
      REAL*8 TIME1,TIME2
	real*8 xut8,yut8
      COMMON/C1/IQ,KMS,KFM,IPUN,IMAG,IR,IPRN,KPAPER,KTEST,KAZ,KSORT,KSEL
      COMMON/C2/ ZTR,XNEAR,XFAR,POS,LATR,LONR,ONF,FLIM
      COMMON/C3/ AHEAD,IPRO,ISW
      COMMON/C4/ NL,NS,KDATE,KHR,NEAR,IEXIT,IDXS
      COMMON/C5/ PMIN,XFN
      COMMON/O1/ NI,INST,KNST,IPH,JPH,NDEC,JMAX,JAV,NR,NRP,KF,KP,KZ,KKF
      COMMON/O2/ AVRPS,DMIN,RMSSQ,ADJSQ,LATEP,LONEP,Z,ZSQ,AVR,AAR,ORG
      COMMON/O3/ SUCARD
      common/stdio/lui,luo,lua1,lua2
      DATA CLASS/'A','B','C','D'/
      DATA SYMBOL/' ','1','2','Q','*'/
c these next 2 vars are undefined in the code, added in EW7.5
      CHARACTER*1 Auto_Manuel
      CHARACTER*6 Fichier_Origine
	data is2/0/
C-----------------------------------------------------------------------
      IF ((IPRN.GE.2) .OR. (KP.EQ.1)) CALL XFMAGS
     & (TEST,FMGC,XMGC,KLAS,PRR,CALR,ICAL,IMAG,IR,QSPA,
     &  AMX,PRX,CALX,FMP,KDX,DELTA,ZSQ,NRP,CAL,NM,AVXM,SDXM,XMAG,NF,
     &  AVFM,SDFM,FMAG,MAG)
      LAT1=LATEP/60.
      LAT2=LATEP-60.*LAT1
      LON1=LONEP/60.
      LON2=LONEP-60.*LON1
      ADJ=SQRT(ADJSQ)
      RMS=SQRT(RMSSQ)
      JHR=KHR
      OSAVE = ORG
      IF (ORG .GE. 0.) GO TO 5
      ORG=ORG+3600.
      KHR=KHR-1
    5 KMIN=ORG/60.0
      SEC=ORG-60.0*KMIN
      ERH=SQRT(SE(1)**2+SE(2)**2)
      NO=FNO
      RMK2=' '
      RMKO=' '
C---- KZ=1 FOR FIXED DEPTH; ONF=0 FOR ORIGIN TIME BASED ON SMP'S
      IF (ONF .EQ. 0.) RMKO='*'
      IF (KZ .EQ. 1) RMK2='*'
      JMAX=0
      DO 10 I=1,NRP
      DXI=DX(I)
      DYI=DY(I)
      IF ((DXI.EQ.0.).AND.(DYI.EQ.0.)) GO TO 6
      JI=KDX(I)
      IF (INS(JI) .EQ. 'S') DYI=-DYI
      IF (IEW(JI) .EQ. 'W') DXI=-DXI
      AZ(I)=AMOD(ATAN2(DXI,DYI)*57.29578 + 360., 360.)
      GO TO 7
    6 AZ(I)= 999.
    7 CONTINUE
      AIN(I)=ASIN(ANIN(I))*57.29578
      IF (AIN(I) .LT. 0.) AIN(I)=180.+AIN(I)
      AIN(I)=180.-AIN(I)
      SWT=0.
      IF (LDX(I) .EQ. 0.) GO TO 8
      KK=LDX(I)
      SWT=WT(KK)
    8 IF ((WT(I).EQ.0.).AND.(SWT.EQ.0.)) GO TO 10
      JMAX=JMAX+1
      TEMP(JMAX)=AZ(I)
   10 CONTINUE
      CALL SORT(TEMP,KEY,JMAX)
      GAP=TEMP(1)+360.-TEMP(JMAX)
      DO 20 I=2,JMAX
      DTEMP=TEMP(I)-TEMP(I-1)
      IF (DTEMP .GT. GAP) GAP=DTEMP
   20 CONTINUE
      IGAP=GAP+0.5
      DO 25 I=1,NRP
   25 DEMP(I)=DELTA(I)
      CALL SORT(DEMP,KEY,NRP)
      DO 27 I=1,NRP
      K=KEY(I)
      SWT=0.
      IF (LDX(K) .EQ. 0.) GO TO 26
      KK=LDX(K)
      SWT=WT(KK)
   26 IF ((WT(K).GT.0.).OR.(SWT.GT.0.)) GO TO 28
   27 CONTINUE
   28 DMIN=DEMP(I)
      IDMIN=DMIN+0.5
      OFD=Z
      TFD=2.*Z
      IF (OFD .LT. 5.) OFD=5.
      IF (TFD .LT. 10.) TFD=10.
      JS=4
      IF ((RMS.LT.0.50).AND.(ERH.LE.5.0)) JS=3
      IF ((RMS.LT.0.30).AND.(ERH.LE.2.5).AND.(SE(3).LE.5.0)) JS=2
      IF ((RMS.LT.0.15).AND.(ERH.LE.1.0).AND.(SE(3).LE.2.0)) JS=1
      JD=4
      IF (NO .LT. 6) GO TO 30
      IF ((GAP.LE.180.).AND.(DMIN.LE.50.)) JD=3
      IF ((GAP.LE.135.).AND.(DMIN.LE.TFD)) JD=2
      IF ((GAP.LE. 90.).AND.(DMIN.LE.OFD)) JD=1
   30 JAV=(JS+JD+1)/2
      Q=CLASS(JAV)
      QS=CLASS(JS)
      QD=CLASS(JD)
      TIME2=SEC+1.D+02*KMIN+1.D+04*KHR+1.D+06*KDATE
      IF(IPRN .EQ. 0) GO TO 52
      IF(NI .NE. 1) GO TO 60
      IF(NDEC .GE. 1) GO TO 60
      IF (JPH .EQ. 1) GO TO 60
   52 KKYR=KDATE/10000
      KKMO=(KDATE-KKYR*10000)/100
      KKDAY=(KDATE-KKYR*10000-KKMO*100)
      JPH=1
      IF(KSEL) 501,501,505
  501 WRITE(8,502)
  502 FORMAT(///)
      GO TO 535
  505 WRITE(8,506)
  506 FORMAT(1H1)
      WRITE(8,53) AHEAD,KKYR,KKMO,KKDAY,KHR,KMIN
   53 FORMAT(/,30X,A48,T112,I2,'/',I2,'/',I2,4X,I2,':',I2)
  535 IF( TIME2 - TIME1 .GT. -20.)GO TO 60
      WRITE(8,54)
   54 FORMAT(' ***** FOLLOWING EVENT IS OUT OF ORDER *****')
   60 IF ((KP.EQ.1) .AND. (IPRN.EQ.0)) GO TO 67
      IF (IPH .EQ. 1) GO TO 62
      WRITE(8,61) INS(1),IEW(1)
   61 FORMAT(/,59X,'  ADJUSTMENTS (KM)  PARTIAL F-VALUES  STANDARD '
     +,'ERRORS  ADJUSTMENTS TAKEN',/
     +,'  I  ORIG  LAT ',A1
     2,'    LONG ',A1,'   DEPTH  DM  RMS AVRPS SKD   CF   '
     3,'DLAT  DLON    DZ  DLAT  DLON    DZ  DLAT  DLON    DZ  '
     4,'DLAT  DLON    DZ')
      IF (IPRN .EQ. 1) IPH=1
   62 WRITE(8,63) NI,SEC,LAT1,LAT2,LON1,LON2,Z,RMK2,IDMIN,RMS,AVRPS,
     1 QS,KF,QD,FLIM,B(2),B(1),B(3),AF(2),AF(1),AF(3),SE(2),SE(1),
     2 SE(3),Y(2),Y(1),Y(3)
   63 FORMAT(I3,F6.2,I3,'-',F5.2,I4,'-',F5.2,F6.2,A1,I3,F5.2,F6.2,
     1 1X,A1,I1,A1,13F6.2)
      IF (KP .EQ. 0) GO TO 100
   67 JNST=KNST*10+INST
      IF (NM .EQ. 0) AVXM=0.
      IF (NF .EQ. 0) AVFM=0.
      MAGOUT = '      '
      IF (MAG .NE. 99.9) WRITE(MAGOUT,68) MAG
   68 FORMAT(F6.2)
      SE3OUT = '     '
      IF (SE(3) .NE. 0.) WRITE(SE3OUT,70) SE(3)
   70 FORMAT(F5.1)
      ERHOUT = '     '
      IF (ERH .NE. 0.) WRITE(ERHOUT,70) ERH
      WRITE(8,75) INS(1),IEW(1)
   75 FORMAT(//,'  DATE    ORIGIN    LAT ',A1,'    LONG ',A1,'    DEPTH'
     1,'    MAG NO DM GAP M  RMS  ERH  ERZ Q SQD  ADJ IN NR  AVR  AAR'
     2,'  NM AVXM SDXM NF AVFM SDFM I')
      WRITE(8,86) KDATE,RMKO,KHR,KMIN,SEC,LAT1,LAT2,LON1,LON2
     1,Z,RMK2,MAGOUT,NO,IDMIN,IGAP,KNO,RMS,ERHOUT,SE3OUT,Q,QS
     2,QD,ADJ,JNST,NR,AVR,AAR,NM,AVXM,SDXM,NF,AVFM,SDFM,NI
   86 FORMAT(1X,I6,A1,2I2,F6.2,I3,'-',F5.2,I4,'-',F5.2,1X,F6.2,A1,A6
     1,2I3,I4,I2,F5.2,2A5,2(1X,A1),'|',A1,F5.2,2I3,2F5.2,2(I3,2F5.1),I2)
      IF ((QRMK(1).NE.SYMBOL(4)).AND.(QRMK(1).NE.SYMBOL(5)))
     1QRMK(1)=SYMBOL(1)
      SYM3=SYMBOL(KNO+1)
      IF (IPUN .EQ. 0) GO TO 100
      if(lua2.ne.0)
     1 WRITE(lua2,87) KDATE,KHR,KMIN,SEC,LAT1,LAT2,iabs(LON1),
     2 abs(LON2),Z,RMK2,MAGOUT,NO,IGAP,DMIN,RMS,ERHOUT,
     3 SE3OUT,QRMK(1),Q,SYM3
      WRITE(7,87) KDATE,KHR,KMIN,SEC,LAT1,LAT2,iabs(LON1),abs(LON2)
     1,Z,RMK2,MAGOUT,NO,IGAP,DMIN,RMS,ERHOUT,SE3OUT,QRMK(1)
     2,Q,SYM3
   87 FORMAT(I6,1X,2I2,F6.2,I3,'-',F5.2,I4,'-',F5.2,1X,F6.2,A1,A6,I3
     1,I4,F5.1,F5.2,2A5,3A1)
  100 CONTINUE
      WRITE(SUCARD,87) KDATE,KHR,KMIN,SEC,LAT1,LAT2,LON1,LON2
     1,Z,RMK2,MAGOUT,NO,IGAP,DMIN,RMS,ERHOUT,SE3OUT,QRMK(1),Q,SYM3
      IF (KP .EQ. 1) GO TO 105
      IF(IPRN .LE. 1) GO TO 300
  105 WRITE(8,110)
  110 FORMAT(/,' STN  DIST AZM AIN PRMK HRMN P-SEC TPOBS TPCAL DLY/H1 P'
     1,'-RES P-WT AMX PRX CALX K XMAG RMK FMP FMAG SMK S-SEC TSOBS'
     2,' S-RES S-WT    DT')
c-------------------
C
C     Transformation des coordonnees Geographiques de Degres Minutes
C         decimales en coordonnees cartesiennes :
C             -par defaut en UTM avec pour fuseau le fuseau du premier
C                 passage
C             -suivant les valeur de Test(16) et Test(17)
C                 Test(16)=1 : UTM avec fuseau No Test(17) si non nul
C                 Test(16)=5 : Lambert avec Reference No Test(17)
C                 Test(16)=6 : Gauss-Laborde Reunion
C
      No_Systeme_Final=Test(16)
      No_Ref_Final=Test(17)
      IF(No_Systeme_Final.EQ.6)THEN
          Coord_Cartes='Gauss-Laborde'
          No_Ref_Final=0
      ELSE
          IF(No_Systeme_Final.EQ.5)THEN
              Coord_Cartes='  Lambert .. '
              IF(No_Ref_Final.LT.1..OR.No_Ref_Final.GT.10)No_Ref_Final=1
              WRITE(Coord_Cartes(11:12),'(I2)')No_Ref_Final
          ELSE
              No_Systeme_Final=1
              Coord_Cartes='   U-T-M ..  '
              IF(Test(16).EQ.1..AND.Test(17).GE.20.)THEN
                  No_Ref_Final=Test(17)
              ELSE
                  No_Ref_Final=0
              ENDIF
          ENDIF
      ENDIF
c      IF(No_Systeme_Final.NE.1)THEN
          IF(iew(1).EQ.'W')LonEp=-LonEp
          IF(ins(1).EQ.'S')LatEp=-LatEp
c      ENDIF
      CALL TRANSF(DBLE(LonEp/60.),DBLE(LatEp/60.),
     *    xut8,yut8,3,No_Systeme_Final,1,No_Ref_Final)
	X_Cartes=xut8
	Y_Cartes=yut8
      IF(No_Systeme_Final.EQ.1)THEN
          WRITE(Coord_Cartes(10:11),'(I2)')No_Ref_Final
      ELSE
          IF(iew(1).EQ.'W')LonEp=-LonEp
          IF(ins(1).EQ.'S')LatEp=-LatEp
      ENDIF
      if(lua1.ne.0)then
c------- Cartes hyp.tri
		if(se(2).gt.99.) se(2)= 99.99
		if(se(3).gt.99.) se(3)= 99.99
          WRITE(lua1,'(2i6,2f8.2,6f6.2)',ERR=80)KDATE,
     *        KHR*100+kmin,X_Cartes,Y_Cartes,z,sec,se(2),se(1),
     *        se(3),rms
      endif
c-------------------
Clu8  WRITE(LU_8,'(/" STN  DIST AZM AIN PRMK HRMN P-SEC TPOBS TPCAL ",
Clu8 *    "DLY/H1 P-RES P-WT AMX PRX CALX K XMAG RMK FMP FMAG SMK S-",
Clu8 *    "SEC TSOBS S-RES S-WT    DT")',ERR=80)
C
              Altitude=-Z-Test(14)
	  WRITE(8   ,601,ERR=80)
     *        KDATE,KHR,kmin,sec,NI,Lat1,
     *        Lat2,ins(1),Coord_Cartes,X_Cartes,SE(1),
     *        Lon1, Lon2,iew(1),Coord_Cartes,Y_Cartes,
     *        SE(2),Z,Altitude,SE(3),RMS,magout,Q
C
601   format  (' Date   Heure Minute Seconde'/3I6,F9.2,T60,'Nb',
     *    ' Iterations :',I3/'Latitude',9X,':',I3,F6.2,1X,A1,T38,
     *    'X ',A13,' =',F8.2,' +/-',F8.2,' Km'/'Longitude',8X,':',I3,
     *     F6.2,1X,A1,T38,'Y ',A13,' =',F8.2,' +/-',F8.2,' Km'/
     *     'Profondeur / Ref :',F9.2,' Km',T38,'Altitude / mer ',T54,
     *     '=',F8.2,' +/-',F8.2,' Km'/'RMS',14X,':',F9.2,' s',
     *	   T38,'Magnitude',t54,'=  ',a6,
     *     T66,'Qualite :  ',A1//'Sta   Dist  Az Inc P   HhMn   Sec',
     *     '  Calc   O-C P-WT S    Sec   Calc   O-C  S-WT')
c Sta  Dist  Az Inc  P   HhMn   Sec  Calc  O-C   dly   WT
cCAG    0.6  27 161EPU1  943 15.64  0.98  0.00 1.93
cTAG    0.9 198 149IPD0  943 15.60  0.94  0.01 1.29 S 4 15.90  1.25 -0.44  0.0

	  WRITE(*   ,601,ERR=80)
     *        KDATE,KHR,kmin,sec,NI,Lat1,
     *        Lat2,ins(1),Coord_Cartes,X_Cartes,SE(1),
     *        Lon1, Lon2,iew(1),Coord_Cartes,Y_Cartes,
     *        SE(2),Z,Altitude,SE(3),RMS,magout,Q
C
      nobsp = 0
      nobss = 0
      DO No1=1,NRP
          No2=No1
          IF(KSORT.EQ.1)No2=KEY(No1)
          KJI=KDX(No2)
          TPK=TP(No2)-ORG
          IF(TPK.LT.0.)TPK=TPK+3600.
          WRITE(X4KOUT,'(F6.2)')X(4,No2)
          IF((AZRES(No2).EQ.' .  ').OR.(AZRES(No2).EQ.'    ').OR.
     *        (AZRES(No2).EQ.'0.  '))X4KOUT = '      '
          RMK3=' '
          IF(XMAG(No2).NE.99.9)THEN
              IF(ABS(XMAG(No2)-AVXM).GE.0.5)RMK3='*'
          ENDIF
          RMK4=' '
          IF(FMAG(No2).NE.99.9)THEN
              IF(ABS(FMAG(No2)-AVFM).GE.0.5)RMK4='*'
          ENDIF
          XMAGOU='    '
          IF(XMAG(No2).NE.99.9)WRITE(XMAGOU,'(F4.1)')XMAG(No2)
          FMAGOU='    '
          IF(FMAG(No2).NE.99.9)WRITE(FMAGOU,'(F4.1)')FMAG(No2)
          IAZ=AZ(No2)+0.5
          IAIN=AIN(No2)+0.5
          IAMX=AMX(No2)
          IPRX=100.*PRX(No2)+0.5
          FMPOUT='    '
          IFMPK=FMP(No2)
          IF(FMP(No2).NE.0.)WRITE(FMPOUT,'(I4)')IFMPK
c------------------------- on somme les temps avec poids > 0.1
          if(wt(No2).gt.0.1)nobsp=nobsp+1
c-------------------------
          IF(LDX(No2).EQ.0)THEN
C-----CHECK FOR SMP DATA
              IF(KSMP(No2).EQ.0)THEN
                  SKOUT = '      '
                  TSKOUT = '      '
		  TSKCAL='      '
                  SRESOU='      '
                  RMK5='    '
                  SWTOUT='    '
              ELSE
                  WRITE(SRESOU,'(F6.2)')X(4,No2)
                  RMK5='    '
                  SWTOUT='****'
                  TSK=S(No2)-P(No2)
                  WRITE(TSKOUT,'(F6.2)')TSK
              ENDIF
          ELSE
              No3=LDX(No2)
              RMK5=WRK(No3)
              SWT=WT(No3)
c------------------------ on somme les temps S avec poids > 0.1
              if(swt.gt.0.1)nobss=nobss+1
c------------------------
              TSK=TS(No2)-ORG
              WRITE(SRESOU,'(F6.2)')X(4,No3)
              WRITE(SWTOUT,'(F4.1)')SWT
              WRITE(TSKOUT,'(F6.2)')TSK
              WRITE(TSKCAL,'(F6.2)')T(no3)
          ENDIF
          DLYK=DLY(KNO,KJI)
Cisw      IF(ISW.EQ.'1   ')DLYK=FLT(KNO,KJI)
          DTKOUT='      '
          IF(DT(No2).NE.0.)WRITE(DTKOUT,'(F6.2)')DT(No2)
          IF(S(No2).NE.999.99)WRITE(SKOUT,'(F6.2)')S(No2)

c      WRITE(8,176) MSTA(K),DELTA(K),IAZ,IAIN,PRMK(K),JHR,JMIN(K),P(K)
c     1,      TPK,T(K),DLYK,X4KOUT,WRK(K),WT(K),IAMX,IPRX,CAL(K)
c     2,KLAS(KJI),XMAGOU,RMK3,RMK(K),FMPOUT,FMAGOU,RMK4,SRMK(K),SKOUT
c     3,TSKOUT,SRESOU,RMK5,SWTOUT,DTKOUT,IW(KJI)
c  176 FORMAT(A4,F6.1,2I4,1X,A4,1X,2I2,4F6.2,A6,A2,F4.2,I4,I3,
cF6.2,I2 , A4,A1,1X,A3,A4,A4,A1,A4,3A6,A2,A4,A6,A6,A1)

	write(8,*)'---------------'
	write(8,*)p(no2),tpk,t(no2),dlyk,x4kout,wrk(no2),wt(no2)
	write(8,*)srmk(no2),skout,tskout,sresou,swtout,rmk5
	write(8,*)t(no3),tskcal,ts(no2),s(no2)
	write(8,*)'---------------'
	WRITE(8,'(A4,F6.1,2I4,1X,A4,1X,2I2,3F6.2,A6,f6.2,A2,F4.2,I4,I3,
     *        F6.2,I2,A4,/,5x,A1,1X,A3,A4,A4,A1,
     +        A4,5x,3A6,a6,A6,4x,A6,A6,A6,A1)',ERR=80)
     +		MSTA(No2),DELTA(No2),IAZ,IAIN,
     +		PRMK(No2),JHR,JMIN(No2),P(No2),TPK,T(No2),
     +		X4KOUT,dlyk,WRK(No2),WT(No2),
     +		IAMX,IPRX,CAL(No2),KLAS(KJI),
     +		XMAGOU,RMK3,RMK(No2),FMPOUT,FMAGOU,RMK4,
     +		SRMK(No2),SKOUT,TSKOUT,tskcal,SRESOU,SWTOUT,RMK5,
     +          DTKOUT,IW(KJI)
	      WRITE(*,'(A4,F6.1,2I4,A4,I3,I2,2F6.2,A6,F5.2,A4,3A6,1X,
     *            A4)',ERR=80)MSTA(No2),delta(No2),iaz,iain,
     *            prmk(No2),jhr,JMIN(No2),p(No2),t(No2),x4kout,
     *            wt(No2),srmk(No2),skout,tskout,sresou,swtout
c             IF(WRK(No2).NE.'    '.OR.RMK5.NE.'    ')WRITE(LU_n,'(46X,
c    *            A4,23X,A4)',ERR=80)WRK(No2),RMK5
	  stmp=srmk(No2)
          IF(lua1.NE.0)THEN
c100   FORMAT(A4,1X,A1,a1,I1,1X,I6,2I2,F5.2,7X,F5.2,1X,A1,1X,I1,
c     *2F6.2,T66,F6.2)
       WRITE(Lua1,'(a4,a4,1x,i6,2i2,f5.2,6x,a6,a4,2a6,f7.3,t66,f6.2)',
     +   ERR=80)
     *            MSTA(No2),prmk(No2),KDATE,jhr,
     *            JMIN(No2),p(No2),skout,stmp(:4),x4kout,sresou,
     +	delta(no2),dt(no2)
          ENDIF
          IF(IPUN.EQ.2)THEN
              ISEC=100.*SEC
              WRITE(7   ,'(A4,3F6.1,1X,A4,F6.2,A6,F5.1,A6,1X,A3,A6,I7,
     *            2I2,2I4,A1)',ERR=80)MSTA(No2),DELTA(No2),
     *            AZ(No2),AIN(No2),PRMK(No2),TPK,X4KOUT,WT(No2),XMAGOU,
     *            RMK(No2),FMAGOU,KDATE,KHR,KMIN,ISEC,KJI,
     *            SYM3
          ENDIF
      ENDDO
      IF(lua1.ne.0)WRITE(lua1,'(1X)',ERR=80)
c*********************************************************************
c Cartes analogue a hypo71 de l`ipg sur unite 9
      numer=0
c the following 2 variables were not defined!
      Auto_Manuel = ' '
      Fichier_Origine = '      '
      WRITE(8   ,'(i6,2i3,f5.2,i3,f6.2,i4,2f6.2,f5.1,f6.2,f5.2,2f5.1,5x,
     *    i4,a1,2i2,A1,3F6.2,A6)',ERR=80)KDATE,KHR,kmin,sec,
     *    Lat1,Lat2,Lon1,Lon2,z,se(3),mag,rms,
     *    se(1),se(2),numer,q,nobsp,nobss,Auto_Manuel,X_Cartes,Y_Cartes,
     *    Altitude,Fichier_Origine
c*********************************************************************
      IF(IPUN.EQ.2)THEN
	  WRITE(7  ,*,ERR=80) ' $$$'
      ENDIF
  300 KHR=JHR
      ORG=OSAVE
C
      RETURN
C
   80 continue
	write(*,*) 'Write error'
	return
      END