      SUBROUTINE FINAL

*********************************************************************  
*                                                                   *
*       Writes the data into 'in1.dat' for future use               *
*       The data is written in the same format as that read         *
*       by the optional "Old Calculations" routine in INIT.         *
*                                                                   *
*       Writes density, perturbations, and potential to a tecplot-  *
*       friendly file. (www.tecplot.com)                            *
*                                                                   *
*       Version 0.5 - Last Modified 7/14/04                         *
*                                                                   *
*********************************************************************

        INCLUDE "fpvar.inc"
        REAL DELTAN(MOZ,MOX),NE
        REAL TIME,LAT,LONG,YEAR,MONTH,DAY
        REAL ETIME, ELAPSED(2)

        OPEN(STATUS='UNKNOWN',UNIT=9,FILE='in1.dat')
        WRITE(9,*)T
        DO 500, I=1,MOZ
           DO 500, J=1,MOX
 500          WRITE(9,*)N(I,J),PHI(I,J)
        CLOSE(9)

*********************************************************************
*                                                                   *
*       Calculates average densities and relative perturbations     *
*                                                                   *
*********************************************************************

	DO 510 I=1,MOZ
           DO 510 J=1,MOX
 510          DELTAN(I,J)=(N(I,J)-NE(I))*100./NE(I)

*********************************************************************
*                                                                   *
*       Writes data to a Tecplot-friendly file (www.tecplot.com).   *
*       Data is printed "x-axis, altitude, density, perturbations,  *
*       potential".                                                 *
*                                                                   *
*********************************************************************

        OPEN(STATUS='UNKNOWN',UNIT=9,FILE="output.dat")
        WRITE(9,*) 'TITLE="fp0.5.f ~ t =',T,' s"'
        WRITE(9,*)'# Lat=',LAT,', Long=',LONG,', Year=',YEAR
        WRITE(9,*)'# Month=', MONTH,', Day=',DAY,', Time=',TIME
        WRITE(9,*) 'VARIABLES="Latitude","Altitude","Density (m<sup>-3</
     $sup>)","<greek>d</greek>N/N (%)","Potential"'
        WRITE(9,*) 'ZONE F=POINT I=',mox,',J=101'
        DO 520, J=1,MOX
           DO 520, I=11,111
              XX=(DX*REAL(J-1)-LX)/1000.
              ZZ=160.+DZ*REAL(I-1)/1000.
 520          WRITE(9,*)XX,ZZ,N(I,J),DELTAN(I,J),PHI(I,J)
        CLOSE(9) 

          WRITE(6,*)
          WRITE(6,*)'Program is finished'        
          WRITE(6,*)
          WRITE(6,*)'Elapsed Time is ',ETIME(ELAPSED),' seconds'
          WRITE(6,*)
          RETURN
          END

