      SUBROUTINE TVWRITE(LTV,MR,NR,IGAS,TVPATH,VSTATE,ZR,TV)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C 
C THIS ROUTINE OUTPUTS THE ATMOSPHERE VIBRATIONAL TEMPERATURES
C FOR EACH SIMULATED GAS/ISOTOPE/TRANSITION.
C
C INPUT PARAMETERS:
C
C   TVPATH --- DIRECTORY PATH TO INPUT VIBRATIONAL TEMPERATURES.
C   LTV    --- UNIT NUMBER OF INPUT VIBRATIONAL TEMPERATURES.
C   MR     --- MAXIMUM LEVELS ALLOWED IN INPUT ATMOSPHERE.
C   NR     --- NUMBER OF LEVELS IN INPUT ATMOSPHERE.
C   IGAS   --- PSEUDO GAS ID'S OF SIMULATED GASES/ISOTOPES/TRANSITIONS.
C   VSTATE --- RIGHT-JUSTIFIED VIBRATIONAL STATE DESIGNATION.
C
C OUTPUT
C   ZR     --- OUTPUT ATMOSPHERE ALTITUDES (KM).
C   TV     --- OUTPUT MODEL VIBRATIONAL TEMPERATURES (K) TABULATED AT
C              THE ZR'S
C
C
C FILE FORMAT:
C
C   THE VIBRATIONAL TEMPERATURE FILE NAMES MUST HAVE THE FOLLOWING 
C   FORMAT:  GASNAME_GASID_VSTATE.ALT
C   EXAMPLE: THE FIRST EXCITED NU2 STATE OF THE CO2 626 ISOTOPE.
C            CO2_201_01101.ALT
C
C SOFTWARE WRITTEN BY Christopher J. Mertens
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C      (c) Copyright 1987-1999 by GATS, Inc.
C          11864 Canon Blvd., Suite 101, Newport News, VA 23606
C          Phone: (757) 873-5920
C
C  All Rights Reserved. No part of this software or publication may be
C  reproduced, stored in a retrieval system, or transmitted, in any form
C  or by any means, electronic, mechanical, photocopying, recording, or
C  otherwise without the prior written permission of GATS, Inc.
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      PARAMETER(NFILE=35,NCHAR8=8,NCHAR4=4)
      DIMENSION ZR(MR),TV(MR)
      CHARACTER*(*) TVPATH,DESCR*10
      CHARACTER*8 NAME(NFILE),GASNAME,VSTATE,GATSID*4
      CHARACTER*200 TVFILE,BLANCK*1,NULL*1
      INTEGER L
      DATA BLANCK/' '/
      DATA NULL/'\0'/
      DATA NAME/ 'H2O', 'CO2', 'O3', 'N2O', 'CO',
     & 'CH4', 'O2', 'NO', 'SO2', 'NO2', 'NH3',
     & 'HNO3', 'OH', 'HF', 'HCL', 'HBR', 'HI',
     & 'CLO', 'OCS', 'H2CO', 'HOCL', 'N2',
     & 'HCN', 'CH3CL', 'H2O2', 'C2H2', 'C2H6', 'PH3',
     & 'COF2', 'SF6', 'H2S', 'HCOOH', 'CLONO2', 'CCl3F',
     & 'CCl2F2'/

      IF(IGAS.GT.10000) THEN
        IDG=IGAS/10000
      ELSE
        IDG=IGAS
      ENDIF        
      IVM=IDG/100
      GASNAME=NAME(IVM)
      
      NCHAR=LEN(TVPATH)
      DO IC=1,NCHAR
        ICC1=IC
        IF((TVPATH(IC:IC).NE.BLANCK).AND.
     &(TVPATH(IC:IC).NE.NULL)) GO TO 5
      ENDDO
 5    DO IC=ICC1,NCHAR
        IF((TVPATH(IC:IC).EQ.BLANCK).OR.
     &(TVPATH(IC:IC).EQ.NULL)) GO TO 10
        ICC2=IC
      ENDDO
 10   DO IC=1,NCHAR8
        IF((GASNAME(IC:IC).EQ.BLANCK).OR.
     &(GASNAME(IC:IC).EQ.NULL)) GO TO 15
        ICC3=IC
      ENDDO
 15   WRITE(GATSID,'(I4)') IDG
      DO IC=1,NCHAR4
        ICC4=IC
        IF((GATSID(IC:IC).NE.BLANCK).AND.
     &(GATSID(IC:IC).NE.NULL)) GO TO 20
      ENDDO
 20   DO IC=ICC4,NCHAR4
        IF((GATSID(IC:IC).EQ.BLANCK).OR.
     &(GATSID(IC:IC).EQ.NULL)) GO TO 25
        ICC5=IC
      ENDDO
 25   DO IC=1,NCHAR8
        ICC6=IC
        IF((VSTATE(IC:IC).NE.BLANCK).AND.
     &(VSTATE(IC:IC).NE.NULL)) GO TO 30
      ENDDO
 30   DO IC=ICC6,NCHAR8
        IF((VSTATE(IC:IC).EQ.BLANCK).OR.
     &(VSTATE(IC:IC).EQ.NULL)) GO TO 35
        ICC7=IC
      ENDDO        
 35   DESCR=GATSID(ICC4:ICC5)//'_'//VSTATE(ICC6:ICC7)
      TVFILE=TVPATH(ICC1:ICC2)//GASNAME(1:ICC3)//'_'//GATSID(ICC4:ICC5)
     &//'_'//VSTATE(ICC6:ICC7)//'.ALT\0'

c      call stringlength(tvfile,' ',' ',' ',L)
c      print*,'|',tvfile(1:L),'|'
 
c      OPEN(LTV,FILE=TVFILE(1:L),ERR=900,status='old')
      OPEN(LTV,FILE=TVFILE,status='unknown')
    
      WRITE(LTV,'(A20,A10)') 'ALT(km)   Tv(K)---',DESCR 
      DO I=1,NR    
        WRITE(LTV,*) ZR(I),TV(I)
      ENDDO
      CLOSE(LTV)
      RETURN
      END
