c 
c PROGRAM GMAP
c
c program to convert Nordic format to earth.google kml format.
c
c peter voss at GEUS, 17 apr 2008 version 0.9.6 Beta : added parameters to SEISAN.DEF
c peter voss at GEUS, 26 feb 2008 version 0.9.5 Beta : add -TimeSpan
c peter voss at GEUS, 20 feb 2008 version 0.9.4 Beta : add -color ff0000ff
c                                                    : add -nodata
c peter voss at GEUS, 19 feb 2008 version 0.9.3 Beta : use first available mag in header
c peter voss at GEUS,  8 feb 2008 version 0.9.2 Beta : will handel compact files
c                                                      can handel <,>,&,",' in Title
c peter voss at GEUS, 11 dec 2007 version 0.9.1 Beta
c
c   arguments:
c              -help           print help list
c              -color ff0000ff define color of epicenters
c              -nodata         kml file will only contain header line
c              -out_file       define name of output file (default is gmap.kml)
c              -verbose        be more verbose
c              -version        seisan version
c

       implicit none

C
C    Seisan library inserts and routines...
C    ======================================
C
       include 'libsei.inc'                ! Open file definitions
       include 'seidim.inc'                ! dimentions
C
       external sei open,                  ! Open file routine.
     &          sei close,                 ! Close file routine.
     &          sei code                   ! Error encoder.
C
C    ============= end of list ==========
C
c-- INPUT FILE                                         
      character*80 file
      character*80 out_file               ! full name of output file
      character*120 defdata
      character*80 data(max_data)
      character*80 title
      character*80 url,urle,urlpe,urlo
      character*8 color,colore,colorpe,coloro
      CHARACTER*1 TYPE,EXP

      integer neq, nexp, npexp, nother, i
      integer nstat,nphase,nhead,nrecord

      real MSIZE,XSIZE,YSIZE

      integer narg                            ! number of arguments
      character*80 arg(40)                    ! arguments
      character*80 gmap_append_kml(100)       ! text that can be appended to gmap.kml
      integer n_gmap_append_kml               ! number of text lines

C-- ID LINE NUMBER
      INTEGER ID

      logical visible       ! show event in kml
      logical sdata
      logical compact       ! compact input file or not
      logical datafolder    ! kml file contain no folders with data
      logical ttag          ! kml file contain timespan for scrolling in time domain

c input file unit #
      integer read01,write01
c logical for existing file or not
      logical          b_old
c returned code
      integer          code
c-- unit for file
      integer def_unit
      logical  b_eof                         ! End of file?.

      logical  verbose
c
c print version
c
      include 'version.inc'
      out_version_date='FEB 26, 2008'
      if (version_new) out_version_date=version_date
      call print_ver

      XSIZE=0.5
      XSIZE=0.2
      YSIZE=0.5

      url="http://maps.google.com/mapfiles/kml/pal2/icon26.png"
      urle="http://maps.google.com/mapfiles/kml/shapes/star.png"
      urlpe=
     +"http://maps.google.com/mapfiles/kml/shapes/open-diamond.png"
      urlo="http://maps.google.com/mapfiles/kml/shapes/square.png"

      out_file='gmap.kml'

      color="ff0000ff"
      colore="ff0000ff"
      colorpe="ff0000ff"
      coloro="ff0000ff"

      neq=0
      nexp=0
      npexp=0
      nother=0

      verbose=.FALSE.
      datafolder=.TRUE.
      ttag=.FALSE.

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c   open and read default file
c   ---------------------------
      n_gmap_append_kml=0
c
      call sei get file( open$+ignore$,    ! Find and open without messages.
     &                   def_unit,         ! On file unit.
     &                   code,             ! Condition (n/a).
     &                   'DAT',            ! Alternative directory to search.
     &                   'SEISAN.DEF' )    ! For this file.
c
c   read file if there...
c   ---------------------
c
c     if(code.ne.e_ok$)  return

 333  continue
c
      read(def_unit,'(a)',iostat=code) defdata    ! Read from file.
      call sei code( fort$,                    ! Process fortran i/o condition.
     &               code,                     ! Condition.
     &               def_unit,                 ! On unit.
     &               b_eof )                   ! End of file?.
c
      if( .not.b_eof ) then                    ! Not end of file.
c
c   Look for GMAP parameters
c
       if (defdata(1:15).eq.'GMAP_ICON_QUAKE') then
	    read(defdata(41:80),'(a)') url(1:40)
       elseif (defdata(1:19).eq.'GMAP_ICON_EXPLOSION') then
	    read(defdata(41:80),'(a)') urle(1:40)
       elseif (defdata(1:19).eq.'GMAP_ICON_PROB_EXPL') then
	    read(defdata(41:80),'(a)') urlpe(1:40)
       elseif (defdata(1:22).eq.'GMAP_ICON_OTHER_EVENTS') then
	    read(defdata(41:80),'(a)') urlo(1:40)
       elseif (defdata(1:15).eq.'GMAP_ICON_MSIZE') then
            read(defdata(41:55),'(f15.5)') MSIZE
       elseif (defdata(1:15).eq.'GMAP_ICON_XSIZE') then
            read(defdata(41:55),'(f15.5)') XSIZE
       elseif (defdata(1:15).eq.'GMAP_ICON_YSIZE') then
            read(defdata(41:55),'(f15.5)') YSIZE
       elseif (defdata(1:15).eq.'GMAP_APPEND_KML') then
	    n_gmap_append_kml=n_gmap_append_kml+1
            gmap_append_kml(n_gmap_append_kml)=defdata(41:120)
       endif
c
c   go to next line
c
      goto 333
      endif
      call sei close( close$, def_unit, code ) ! Close (Default stop on error).
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


      call get_arguments(narg,arg)
      if(narg.ge.1) then
      do i=1,narg
      if( arg(i)(1:8) .eq. '-verbose' ) verbose=.TRUE.
      if( arg(i)(1:7) .eq. '-nodata' ) datafolder=.FALSE.
      if( arg(i)(1:9) .eq. '-timespan' ) ttag=.TRUE.
      if( arg(i)(1:9) .eq. '-out_file' ) then
	out_file=arg(i+1)
      endif
      if( arg(i)(1:6) .eq. '-color' ) then
	color=arg(i+1)
	colore=arg(i+1)
	colorpe=arg(i+1)
	coloro=arg(i+1)
        if( arg(i+1)(1:4) .eq. 'blue' ) then
          color="ffff0000"
          colore="ffff0000"
          colorpe="ffff0000"
          coloro="ffff0000"
        endif
        if( arg(i+1)(1:5) .eq. 'green' ) then
          color="ff00ff00"
          colore="ff00ff00"
          colorpe="ff00ff00"
          coloro="ff00ff00"
        endif
        if( arg(i+1)(1:6) .eq. 'yellow' ) then
          color="ffffff00"
          colore="ffffff00"
          colorpe="ffffff00"
          coloro="ffffff00"
        endif
        if( arg(i+1)(1:5) .eq. 'black' ) then
          color="ff000000"
          colore="ff000000"
          colorpe="ff000000"
          coloro="ff000000"
        endif
        if( arg(i+1)(1:5) .eq. 'white' ) then
          color="ffffffff"
          colore="ffffffff"
          colorpe="ffffffff"
          coloro="ffffffff"
        endif
      endif
      if( arg(i)(1:5).eq.'-help'.or.arg(i)(1:3).eq.'-h') then
        write(6,*)' '
        write(6,*)
     +' The GMAP program converts Nordic format to the KML format.'
        write(6,*)
     +' The output file gmap.kml can be opened with Google Earth.'
        write(6,*)
     +' The program prompts for a input file in nordic format, the'
        write(6,*)' input file can be compact.'
        write(6,*)' '
        write(6,*)' Usage: gmap [options]'
        write(6,*)'        input file    '
        write(6,*)'        Title used for kml folder'
        write(6,*)' '
        write(6,*)' ## Options ##'
        write(6,'(a17,a15)')'    -help        '
     +,'Print this list'
        write(6,'(a17,a13)')'    -h           '
     +,'Same as -help'
        write(6,'(a17,a46)')'    -color       '
     +,'Define color of epicenters [blue/green/yellow/'
        write(6,'(a17,a47)')'                 '
     +,'black/white] Default color is red              '
        write(6,'(a17,a44)')'                 '
     +,'To uses other colors see describtion below  '
        write(6,'(a17,a44)')'    -timespan    '
     +,'Events gets timetag scroll in time domain   '
        write(6,'(a17,a44)')'    -nodata      '
     +,'kml file will only contain header infomation'
        write(6,'(a17,a48)')'    -out_file    '
     +,'Define name of output file (default is gmap.kml)'
        write(6,'(a17,a15)')'    -verbose     '
     +,'Be more verbose'
        write(6,'(a17,a15)')'    -version     '
     +,'Seisan version '
        write(6,*)' '
        write(6,*)' Scale:'
        write(6,*)' The scale of the icons is set by the SEISAN.DEF '
        write(6,*)' parameters GMAP_ICON_MSIZE, GMAP_ICON_XSIZE and '
        write(6,*)' GMAP_ICON_YSIZE, see the manual for details.'
        write(6,*)' '
        write(6,*)' Color:'
        write(6,*)' Color and opacity (alpha) values are expressed in '
	write(6,*)' hexadecimal notation. The range of values for any '
	write(6,*)' one color is 0 to 255 (00 to ff). For alpha, 00 is'
	write(6,*)' fully transparent and ff is fully opaque. The order'
	write(6,*)' of expression is aabbggrr, where aa=alpha (00 to ff);'
	write(6,*)' bb=blue (00 to ff); gg=green (00 to ff); '
	write(6,*)' rr=red (00 to ff). For example, if you want to apply '
	write(6,*)' a blue color with 50 percent opacity to an overlay, '
	write(6,*)' you would specify the following: '
	write(6,*)' <color>7fff0000</color>, where alpha=0x7f, blue=0xff,'
	write(6,*)' green=0x00, and red=0x00. See also:'
        write(6,*)' http://code.google.com/apis/kml/'
	write(6,*)' documentation/kml_tags_21.html#color '
        write(6,*)' '
        write(6,*)' Examples:'
        write(6,*)'           gmap -color blue -nodata'
        write(6,*)'           gmap -timespan -color 7eee00ee'
        write(6,666)
     +'            echo ','collect.out\\nDK events\\n',
     +' | gmap -out_file dk.kml'
        write(6,*)' '
      goto 999
      endif
      enddo
      endif

  666 FORMAT(a17,'"',a24,'"',a24,1x)

      if(verbose) write(6,*)' GMAP: narg=',narg

 101  continue
      write(6,*)' INPUT FILE NAME'
      read(5,'(a)') file

c
c   open and check if file available
c
            call sei open(old$,                  ! Open an old file.
     &                    ' ',                   ! Prompt file name (n/a).
     &                    file,                  ! File name
     &                    read01,                ! Read unit #1
     &                    b_old,                 ! Already exists? (n/a).
     &                    code)                  ! Returned condition.
c
      if(.not.b_old) goto 101     ! try again
c
c   check type
c
      call nortype(read01,compact)
      if(verbose.and.compact) write(6,*)' GMAP: Input file is compact'
c if input file is compact copy it to gmap.tmp with extra empty line for subroutine INDATA
      if(compact) then
        call sei open(unknown$+warn$,        ! Open a unknown status file.
     &                    ' ',               ! Prompt file name (n/a).
     &                    'gmap.tmp',        ! File name
     &                    write01,            ! Read unit #1
     &                    b_old,             ! Already exists? (n/a).
     &                    code)              ! Returned condition.
 501    read(read01,'(a)',end=509) data(1)
        write(write01,'(a)') data(1)
        write(write01,'(a)')' '
	goto 501
 509    continue
	close(read01)
	rewind write01
	read01=write01
      endif
 
      if(verbose) 
     +write(6,*)' GMAP: The title must contain maximum 80 characters!'
      write(6,*)' Title:'
      read(5,'(a)') title

      if(verbose) 
     +write(6,*)' GMAP: open out_file :',out_file
      open(2,file=out_file,status='unknown')

      write(2,'(a38,1x)')'<?xml version="1.0" encoding="UTF-8"?>'
      write(2,'(a45,1x)')'<kml xmlns="http://earth.google.com/kml/2.1">'
      write(2,'(a10,1x)')'<Document>'
      write(2,'(a23,1x)')'<name><![CDATA['
      if(verbose) 
     +write(6,*)' GMAP: Change special characteres in Title to KML'
      do i=1,80
        if(title(i:i).eq.'"')then
          write(2,'(a,$)')"&#34;"
        elseif(title(i:i).eq."&")then
          write(2,'(a,$)')"&#38;"
        elseif(title(i:i).eq."'")then
          write(2,'(a,$)')"&#39;"
        elseif(title(i:i).eq."<")then
          write(2,'(a,$)')"&#60;"
        elseif(title(i:i).eq.">")then
          write(2,'(a,$)')"&#62;"
        else
          write(2,'(a,$)')title(i:i)
        endif
      enddo
      write(2,'(a21,1x)')']]></name>'
      write(2,'(a18,1x)')'<open>1</open>'
c
c Earthquakes :
c
      visible=.TRUE.
      sdata=.FALSE.
      write(2,'(1x)')
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a30,1x)')'<name>Earthquakes</name>'
      if(verbose)
     +write(6,*)' GMAP: construct folder with earthquakes'
      if(verbose) 
     +write(6,*)
     +' GMAP: check number of : eq, exp, pexp and other events'
  10  continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)

      if(nrecord.eq.0) goto 19
      if(EXP.eq.' ')neq=neq+1
      if(EXP.eq.'E')nexp=nexp+1
      if(EXP.eq.'P')npexp=npexp+1
      if(EXP.ne.' '.AND.EXP.ne.'E'.AND.EXP.ne.'P')nother=nother+1
      if(EXP.eq.' ') then
       CALL wkml(2,data,max_data,NRECORD,url,visible,color,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 10
  19  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')

c
c Earthquakes and data
c
      if(datafolder)then
      if(verbose)
     +write(6,*)' GMAP: construct folder with earthquakes and data'
         rewind read01
      visible=.FALSE.
      sdata=.TRUE.
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a39,1x)'),
     +'<name>Earthquakes and data</name>'
   21 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 29
      if(EXP.eq.' ') then
       CALL wkml(2,data,max_data,NRECORD,url,visible,color,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 21
  29  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')
      endif

c
c Explosions
c
      if(nexp.gt.0)then
      if(verbose)
     +write(6,*)' GMAP: construct folder with explosions'
      visible=.FALSE.
      sdata=.FALSE.
      rewind read01
      visible=.FALSE.
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a29,1x)'),
     +'<name>Explosions</name>'
   31 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 39
      if(EXP.eq.'E') then
       CALL wkml(2,data,max_data,NRECORD,urle,visible,colore,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 31
  39  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')

c
c Explosions and data
c
      if(datafolder)then
      if(verbose)
     +write(6,*)' GMAP: construct folder with explosions and data'
      rewind read01
      visible=.FALSE.
      sdata=.TRUE.
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a38,1x)'),
     +'<name>Explosions and data</name>'
   41 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 49
      if(EXP.eq.'E') then
       CALL wkml(2,data,max_data,NRECORD,urle,visible,colore,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 41
  49  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')
      endif
      endif

c
c Probable Explosions
c
      visible=.FALSE.
      sdata=.FALSE.
      if(npexp.gt.0)then
      if(verbose)
     +write(6,*)' GMAP: construct folder with prob. explosions'
        rewind read01
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a38,1x)'),
     +'<name>Probable Explosions</name>'
   51 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 59
      if(EXP.eq.'P') then
        CALL wkml(2,data,max_data,
     +NRECORD,urlpe,visible,colorpe,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 51
  59  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')

c
c Probable Explosions and data
c
      if(datafolder)then
      if(verbose)
     +write(6,*)' GMAP: construct folder with prob. explosions and data'
      rewind read01
      visible=.FALSE.
      sdata=.TRUE.
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a47,1x)'),
     +'<name>Probable Explosions and data</name>'
   61 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 69
      if(EXP.eq.'P') then
        CALL wkml(2,data,max_data,
     +NRECORD,urlpe,visible,colorpe,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 61
  69  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')
      endif
      endif

c
c Other events
c
      if(nother.gt.0)then
        rewind read01

      if(verbose)
     +write(6,*)' GMAP: construct folder with other events'
      visible=.FALSE.
      sdata=.FALSE.
      write(2,'(a12,1x)')'<Folder>'
      write(2,'(a31,1x)'),
     +'<name>Other events</name>'
   71 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 79
      if(EXP.ne.' '.and.EXP.ne.'E'.and.EXP.ne.'P') then
       CALL wkml(2,data,max_data,NRECORD,urlo,visible,coloro,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 71
  79  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')

c
c Other events and data
c
      if(datafolder)then
         rewind read01
      if(verbose)
     +write(6,*)' GMAP: construct folder with other events and data'
      visible=.FALSE.
      sdata=.TRUE.
      write(2,'(a16,1x)')'<Folder>'
      write(2,'(a40,1x)'),
     +'<name>Other events and data</name>'
   81 continue
      CALL INDATA(read01,NSTAT,NPHASE,NHEAD,NRECORD,TYPE,EXP,DATA,ID)
      if(nrecord.eq.0) goto 89
      if(EXP.ne.' '.and.EXP.ne.'E'.and.EXP.ne.'P') then
       CALL wkml(2,data,max_data,NRECORD,urlo,visible,coloro,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      endif
      goto 81
  89  continue
      write(2,'(a13,1x)')'</Folder>'
      write(2,'(1x)')
      endif
      endif

      close(read01)  

c
c info and links
c

      if(n_gmap_append_kml.ge.1)then
	do i=1,n_gmap_append_kml
          write(2,'(a80,1x)'),gmap_append_kml(i)
	enddo
      endif
c     write(2,'(a29,1x)'),'<ScreenOverlay id="LOGO">'
c     write(2,'(a33,1x)'),
c    +'<name>Info and links</name>'
c     write(2,'(a29,1x)'),'<description> <![CDATA['
c     write(2,'(a68,1x)'),
c    +'Data is in Nordic format. The format is described in the Seisan '
c     write(2,'(a26,1x)'),
c    +'manual at UIB.<br>'
c     write(2,'(a51,1x)'),
c    +'UIB:  http://www.geo.uib.no/seismo/<br> ]]>'
c     write(2,'(a20,1x)'),'</description>'
c     write(2,'(a12,1x)'),'<Icon>'
c     write(2,'(a55,1x)'),
c    +'<href>http://www.gfy.ku.dk/~pv/geus.png</href>'
c     write(2,'(a13,1x)'),'</Icon>'
c     write(2,'(a66,1x)'),
c    +'<overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>'
c     write(2,'(a53,a19,1x)'),
c    +'<screenXY x="-0.01" y="0.99" xunits="fraction" ',
c    +'yunits="fraction"/>'
c     write(2,'(a67,1x)'),
c    +'<rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>'
c     write(2,'(a65,1x)'),
c    +'<size x="0.1" y="0.1" xunits="fraction" yunits="fraction"/>'
c     write(2,'(a20,1x)'),'</ScreenOverlay>'
      write(2,'(a13,1x)'),'</Document>'
      write(2,'(a6,1x)'),'</kml>'
      close(2)  

      write(6,*)"Number of Earthquakes         :",neq
      write(6,*)"          Explosions          :",nexp
      write(6,*)"          Probable Explosions :",npexp
      write(6,*)"          Other events        :",nother
      write(6,*)"Output file is ",out_file

 999  continue

      end


      SUBROUTINE wkml(io,text,max_data,nr,url,visible,color,sdata,ttag,
     +MSIZE,XSIZE,YSIZE)
      character*80 text(max_data)
      character*80 dummy,url
      character*8 color
      character*7 magnitude
      integer io,i,j
      real mag
      real MSIZE,XSIZE,YSIZE
      logical visible, sdata
      logical ttag			! timetag / timespan

      magnitude="       "

c remove chars like backspace
      do i=1,nr
        do j=1,80
          if(ichar(text(i)(j:j)).lt.32) text(i)(j:j)=" "
        enddo
      enddo

c get magnitude
      if(text(1)(73:79).ne.'       ')magnitude=text(1)(73:79)
      if(text(1)(65:71).ne.'       ')magnitude=text(1)(65:71)
      if(text(1)(57:63).ne.'       ')magnitude=text(1)(57:63)

      if(text(1)(7:7).eq.' ')text(1)(7:7)='0'
      if(text(1)(9:9).eq.' ')text(1)(9:9)='0'
      if(text(1)(17:17).eq.' ')text(1)(17:17)='0'
      if(text(1)(28:28).eq.' ')text(1)(28:28)='0'
      if(text(1)(29:29).eq.' ')text(1)(29:29)='0'
      if(text(1)(30:30).eq.' ')text(1)(30:30)='0'
      if(text(1)(36:36).eq.' ')text(1)(36:36)='0'
      if(text(1)(37:37).eq.' ')text(1)(37:37)='0'
      if(text(1)(38:38).eq.' ')text(1)(38:38)='0'
c mag is size of circle
      if(magnitude.ne."       ")then
       mag=ichar(magnitude(1:1))-48.0
       mag=mag+0.1*(ichar(magnitude(3:3))-48.0)
      else
       mag=MSIZE
      endif
      if(mag.lt.MSIZE)mag=MSIZE
      mag=XSIZE*mag**YSIZE
      if(text(1)(23:23).ne." ")mag=mag*2.0
      write(io,'(a17,1x)'),'<Placemark>'
      if(visible) then
	write(io,'(a34,1x)'),'<visibility>1</visibility>'
      else
	write(io,'(a34,1x)'),'<visibility>0</visibility>'
      endif
      if(sdata) then
      write(io,'(a14,a4,a1,a2,a1,a2,a1,a2,a1,a2,a1,a6,a1,a7,a7,1x)'),
     +'<name>',text(1)(2:5),'-',text(1)(7:8),'-',text(1)(9:10),'_',
     +text(1)(12:13),':',text(1)(14:15),':',text(1)(17:22),'_',
     +text(1)(57:63),'</name>'
      else
c     write(io,'(a21,a3,a2,a1,a14,1x)'),'<description>',
c    +text(1)(57:59),' M',text(1)(60:60),' ',
      write(io,
     +'(a21,a3,a2,a1,a1,a4,a1,a2,a1,a2,a1,a2,a1,a2,a1,a4,a14,1x)'),
     +'<description>',
     +magnitude(1:3),' M',magnitude(4:4),' ',
     +text(1)(2:5),'-',text(1)(7:8),'-',text(1)(9:10),'_',
     +text(1)(12:13),':',text(1)(14:15),':',text(1)(17:20),
     +'</description>'
      endif
      write(io,'(a15,1x)'),'<Style>'
      write(io,'(a21,1x)'),'<IconStyle>'
      write(io,'(a19,a8,a8,1x)'),'<color>',color,'</color>'
      write(io,'(a41,1x)'),'<colorMode>normal</colorMode>'
      write(io,'(a19,f6.2,a8,1x)'),'<scale>',mag,'</scale>'
      write(io,'(a18,1x)'),'<Icon>'
      write(io,'(a20)'),'<href>'
      write(io,'(a16,a80)'),"                ",url
      write(io,'(a21,1x)'),'</href>'
      write(io,'(a19,1x)'),'</Icon>'
      write(io,'(a22,1x)'),'</IconStyle>'
      write(io,'(a16,1x)'),'</Style>'
      write(io,'(a15,1x)'),'<Point>'
      do i=24,38
        dummy(i:i)=text(1)(i:i)
c       if(text(1)(i:i).eq.' ')dummy(i:i)='0'
      enddo
      write(io,111),dummy(31:38),dummy(24:30)
      write(io,'(a16,1x)'),'</Point>'
      if(ttag) then
        write(io,'(a36,1x)'),'<TimeSpan>'
        write(io,'(a16,a4,a1,a2,a1,a2,a9,1x)'),
     +'<begin>',text(1)(2:5),'-',text(1)(7:8),'-',text(1)(9:10),
     +'</begin>'
        write(io,'(a16,a4,a1,a2,a1,a2,a6,1x)'),
     +'<end>',text(1)(2:5),'-',text(1)(7:8),'-',text(1)(9:10),
     +'</end>'
        write(io,'(a36,1x)'),'</TimeSpan>'
      endif
      if(sdata) then
        write(io,'(a36,1x)'),'<description> <![CDATA[<pre>'
        do i=1,nr
          write(io,'(a80)'),text(i)(1:80)
        enddo
        write(io,'(a31,1x)'),'</pre>]]></description>'
      endif
      write(io,'(a18,1x)'),'</Placemark>'

 111  FORMAT('          <coordinates>',a8,',',a7,',0,</coordinates>')
      end

