c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
      program stormcard
c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
c     This program breaks out stormcard information from the CARQ 
c     lines of the A-deck of UNIX version ATCF file and puts it into 
c     a format for VICBAR and SHIPS.
c     20001025 F. Horsfall  New ATCF format
c                           Based on Mark DeMaria's stormcard.f for output
c                           Jim Gross' new structures
c
c     Modified August 2014 for global version. 
c     Modified Jan 2023 (KM) to remove goto statements
c
c     Input  file name: comab.dat
c     Output file name: stormcard.dat
c
c     The ATCF format is as follows:
c
c ba, cy, yyyymmddhh, tn, TECH, TAU, LatN,  LonW,vmax, mslp, TY, rad,winc, rad1, rad2, rad3,
c  rad4, radp,  rrp, mrd,gust, eye,xxxx,seas, XXX, dir, spd,stormname,dep,
c
c     where:
c
c     ba    - basin, e.g AL, EP
c     cy    - annual cyclone number: 1 through 99
c     yyyymmddhh - warning date time group (4 digit year)
c     tn    - objective technique sorting number:  00-99
c     TECH  - acronym for each objective technique....CARQ in this case
c     TAU   - forecast period: -24 through 120 hours
c     LatN  - Latitude (tenths of degrees) for the DTG: N/S hemispheric index
c     LonW  - Longitude (tenths of degrees) for the DTG: E/W hemispheric index
c     vmax  - maximum sustained wind speed in knots: 0-300
c     mslp  - minimum sea leave pressure, 1 through 1100 mb
c     TY    - level of tc development:
c           TD - tropical depression
c           TS - tropical storm
c           TY - typhoon
c           ST - super typhoon
c           TC - tropical cyclone
c           HU - hurricane
c           SH - super hurricane
c           SD - subtropical depression
c           SS - subtropical storm
c           EX - extratropical systems
c           IN - inland
c           DS - dissipating
c           LO - low
c           WV - tropical wave
c           ET - extrapolated
c           XX - unknown
c     rad   - wind intensity (kts) for the radii defined 
c             in this record: 35, 50, 65 or 100
c     winc  - radius code:
c           AAA - full circle
c           NNS - north semicircle
c           NES - northeast semicircle
c           EES - east semicircle
c           SES - southeast semicircle
c           SSS - south semicircle
c           SWS - southwest semicircle
c           WWS - west semicircle
c           NWS - northwest semicircle
c           QQQ - quadrant ( NNQ, NEW, EEQ, SEQ, SSQ, SWQ, WWQ, NWQ)
c     rad1  - if fullcircle, radius of specified wind intensity, if semicircle 
c             or quadrant, radius of specified wind intensity of circle portion 
c             specified in radius code. 1-1200 nm
c     rad2  - if full circle this field not used, if semicircle radius (nm) of 
c             specified wind intensity for semicircle not specified in radius 
c             code, if quadrant, radius (nm) of specified wind intensity for 
c             2nd quadrant (counting clockwise from quadrant specified in radius 
c             code). 1-1200 nm
c     rad3  - if full circle or semicircle this field not used, if quadrant, 
c             radius (nm) of specified wind intensity for 3rd quadrant (counting 
c             clockwise from quadrant specified in radius code). 0-1200 nm
c     rad4  - if full circle or semicircle this field not used, if quadrant, 
c             radius (nm) of specified wind intensity for 4th quadrant (counting 
c             clockwise from quadrant specified in radius code). 0-1200 nm
c     radp  - pressure in millibars of the last closed isobar. 900-1050 mb
c     rrp   - radius of the last closed isobar in nm, 0-9999nm
c     mrd   - radius of max winds, 0-999 nm
c     gust  - gusts, 0-995 kts
c     eye   - eye diameter, 0-999 nm
c     xxxx  - unused
c     seas  - max seas: 0-999 ft
c     XXX   - forecaster's initials, used for tau 0 WRNG, up to 3 chars
c     dir   - storm direction, 0-359 degrees
c     spd   - storm speed, 0-999 tenths of kts
c     stormname - literal storm name or TCcyx where:
c                 cy - annual cyclone numbe 01-99
c                 x  - subregion code: W, A, B, S, P, C, E, L
c                      A - Arabian Sea
c                      B - Bay of Bengal
c                      C - Central Pacific 
c                      E - Eastern Pacific
c                      L - Atlantic
c                      P - South Pacific (135E-120W)
c                      S - South IO (20E-120W)
c                      W - Western Pacific
c     dep  - system depth, D-deep, M-medium, S-shallow, X-unknown
c  
c     The format of the output is as follows:
c
c yymmdd
c hh
c lat   lat12
c long  long12
c rm
c ird
c int i12
c dir
c sp
c stname
c alval
c
c     where:
c
c      yymmdd is the year, month, and date
c      hh is the hour (00 or 12)
c      lat is the latitude of the storm in degrees north of the equator (*10)
c      lat12 is the latitude of the storm in degrees north of the equator (*10)
c      long is the longitude of the storm in degrees west of Greenwich (*10)
c      long12 is the longitude of the storm in degrees west of Greenwish (*10)
c      rm is the radius of maximum winds of the storm in nm
c      ird is the radius of the outer closed isobar of the storm in nm
c      int is the current maximum winds of the storm in kt
c      i12 is the previous 12 h maximum winds of the storm in kt
c      dir is the direction of motion of the storm in degrees
c      sp is the speed of motion of the storm in kt
c      stname is the name of the storm
c      alval is the number of the file from which the compute is derived
c
c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
      include 'dataformats.inc'
c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
      character*1  latNS,lonEW
      character*2  bb
      character*10 stname, aymdh
      character*20 fnab,fndat
c
      data fnab /'comab.dat'/
      data fndat /'stormcard.dat'/
      data luab,ludat /21,31/
c
c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
      type ( AID_DATA ) comRcd, tauData
c -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
c
c     Open the input and output files
      open(unit=luab, file=fnab, form='formatted',
     +     status='old',err=900,iostat=istat)
      open(unit=ludat,file=fndat,form='formatted',
     +     status='replace',err=900,iostat=istat)
c
c     Read the compute data
c
      call getARecord (luab, "CARQ", comRcd, istat )
      if ( istat .eq. 0 ) then
  901    print*, ' Error reading data record.  iostat = ', istat
         stop
      endif
c
c     Read the compute data
c 
      call getSingleTAU ( comRcd, 0, tauData, istat ) 
      if ( istat .ne. 1 ) then
  902    print*, 'Error reading a single TAU.  iostat = ', istat
         stop
      endif
c
c     Read in the current required data
c
      aymdh  = tauData%aRecord(1)%DTG
      read ( aymdh(3:4),  '(i2)' ) iyy
      read ( aymdh(5:6),  '(i2)' ) imm
      read ( aymdh(7:8),  '(i2)' ) idd
      read ( aymdh(9:10), '(i2)' ) ihh
c                
      rlat   = tauData%aRecord(1)%lat
      rlon   = tauData%aRecord(1)%lon
      latNS  = tauData%aRecord(1)%NS
      lonEW  = tauData%aRecord(1)%EW
      irm    = tauData%aRecord(1)%mrd
      ird    = tauData%aRecord(1)%rrp
      inte   = tauData%aRecord(1)%vmax
      idir   = tauData%aRecord(1)%dir
      isp    = tauData%aRecord(1)%speed
      stname = ' '
      stname = tauData%aRecord(1)%stormname
      bb     = tauData%aRecord(1)%basin
      nn     = tauData%aRecord(1)%cyNum
c
      lat    = int (rlat * 10 + .5)
      long   = int (rlon * 10 + .5)
c
c     Check year for ATCF file name for S. hemisphere storms
      iyya = iyy
      if (bb .eq. 'SH') then
         if (imm .ge. 7 .and. imm .le. 12) then
            iyya = iyya + 1
            if (iyya .eq. 100) iyya = 00
         endif
      endif
c
c     Fix the stname to account for left-justification
      call stfix(stname)
c
c     Read in the minus 12 hour old required data
c
      call getSingleTAU ( comRcd, -12, tauData, istat ) 
      if ( istat .ne. 1 ) then
  903    print*, 'Error reading a single TAU.  iostat = ', iostat
         stop
      endif
c     
      rlat12 = tauData%aRecord(1)%lat
      rlon12 = tauData%aRecord(1)%lon
      i12    = tauData%aRecord(1)%vmax
c
      lat12  = int (rlat12 * 10 + .5)
      long12 = int (rlon12 * 10 + .5)
c
c     Write the output file
      write(ludat,200) iyy,imm,idd
      write(ludat,205) ihh
      write(ludat,210) lat,lat12,latNS
      write(ludat,210) long,long12,lonEW
      write(ludat,215) irm
      write(ludat,215) ird
      write(ludat,220) inte,i12
      write(ludat,215) idir
      write(ludat,215) isp
      write(ludat,225) stname
      write(ludat,230) bb,nn,iyya
  200 format(3(i2.2))
  205 format(i2.2)
  210 format(i4,1x,i4,1x,a1)
  215 format(i3)
  220 format(i3,1x,i4)
  225 format(1x,a10)
  230 format(a2,i2.2,i2.2)
c
      close(luab)
      close(ludat)
      stop
c
  900 continue
      print*,  ' Error opening file in stormcard.f program'
      print*,  ' iostat=',istat
      stop
c
      end
      subroutine stfix(stname)
c     This routine adjusted stname to account for 
c     the right justification
c
      character *1 let(26)
c
      character *10 stname
      character *10 sttemp
c
      data let /'A','B','C','D','E','F','G','H','I','J','K','L','M',
     +          'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'/
c
      sttemp = stname
c
      stname = ' '
      icount = 0
      do k=1,10
         do l=1,26
            if (sttemp(k:k) .eq. let(l)) then
               icount = icount + 1
               stname(icount:icount) = sttemp(k:k)
               exit !inner loop
            endif
         enddo
      enddo
c
      return
      end