	program ips2020inputm
	character	cFile*80,
     &			cCRT*3		/'CRT'/,
     &			cCED*3		/'CTD'/,
     &			cCET*3		/'CTT'/,
     &			cPEN*3		/'PTN'/,
     &			cPER*3		/'PTR'/,
     &			cCSD*3		/'CRD'/,
     &			cCST*3		/'CDT'/,
     &			cPSN*3		/'PWN'/,
     &			cPSR*3		/'PWR'/,
     &			cCM2D*4		/'CM2D'/,
     &			cCM2T*4		/'CM2T'/,
     &			cPM2N*4		/'PM2N'/,
     &			cPM2R*4		/'PM2R'/,
     &			cSTR*3		/'STR'/,
     &			cValSTR*2,cVar*1,
     &			cValCED*2,cValCET*2,cValPEN*2,cValPER*2,
     &			cValCM2D*5,cValCM2T*5,cValPM2N*5,cValPM2R*5,cValCRT*4,
     &			cValCSD*2,cValCST*2,cValPSN*2,cValPSR*2
	logical		bOpenFile
	include		'dirspec.h'
	include		'openfile.h'

	iVar = 1
	call ForeignInput(' ',iVar,cVar)

	dCSD = 1.0
	dCST = .20
	dPSN = .100
	dPSR = .100

	iValCD = 0
	iValCT = 0
	iValPN = 0
	iValPR = 0

	iValPSN = 0
	iValPSR = 0
	iValCST = 0
	iValCSD = 0

	if(cVar.eq."0") then
	  iValCD = 3
	end if

	if(cVar.eq."1") then
	  iValCT = 3
	end if

	if(cVar.eq."2") then
	  iValPN = 3
	end if

	if(cVar.eq."3") then
	  iValPR = 3
	end if

	print *, 'cVar =', cVar

	i = iGetSymbol(cCED,cValCED)
	i = iGetSymbol(cCET,cValCET)
	i = iGetSymbol(cPEN,cValPEN)
	i = iGetSymbol(cPER,cValPER)

	read (cValCED,'(I2)') iValCED
	read (cValCET,'(I2)') iValCET
	read (cValPEN,'(I2)') iValPEN
	read (cValPER,'(I2)') iValPER

	if (iGetSymbol(cCM2D,cValCM2D) .eq. 0) then
	    cValCM2D = '18.00'
	    i = iSetSymbol(cCM2D,cValCM2D,1)
	end if

	if (iGetSymbol(cCM2T,cValCM2T) .eq. 0) then
	    cValCM2T = '0.400'
	    i = iSetSymbol(cCM2T,cValCM2T,1)
	end if

	if (iGetSymbol(cPM2N,cValPM2N) .eq. 0) then
	    cValPM2N = '0.400'
	    i = iSetSymbol(cPM2N,cValPM2N,1)
	end if

	if (iGetSymbol(cPM2R,cValPM2R) .eq. 0) then
	    cValPM2R = '0.600'
	    i = iSetSymbol(cPM2R,cValPM2R,1)
	end if

	if (iGetSymbol(cCRT,cValCRT) .eq. 0) then
	    cValCRT = '1941'
	    i = iSetSymbol(cCRT,cValCRT,1)
	end if

	read (cValCM2D,'(F5.2)') ValCMD
	read (cValCM2T,'(F5.3)') ValCMT
	read (cValPM2N,'(F5.3)') ValPMN
	read (cValPM2R,'(F5.3)') ValPMR
	read (cValCRT,'(I4)') iValCRT

	if(iValCED.ne.0) then
	    CSD0 = ValCMD -dCSD		! 3? increments including "0" --> CSD=iValCED
	else
	    CSD0 = ValCMD
	end if
	if(iValCET.ne.0) then
	    CST0 = ValCMT -dCST		! 3? increments including "0" --> CST=iValCET
	else
	    CST0 = ValCMT
	end if
	if(iValPEN.ne.0) then
	    PSN0 = ValPMN -dPSN		! 3? increments including "0" --> PSN=iValPEN
	else
	    PSN0 = ValPMN
	end if
	if(iValPER.ne.0) then
	PSR0 = ValPMR -dPSR		! 3? increments including "0" --> PSR=iValPER
	else
	    PSR0 = ValPMR
	end if


	if(iGetSymbol(cSTR,cValSTR) .eq. 0 .or. cValSTR .eq. '-1') then
	  iValPSN = 0
	  iValPSR = 0
	  iValCSD = 0
	  iValCST = 0
	  iValSTR = 0
	print *, 'First time through STR = -1'
	else
	  i = iGetSymbol(cPSN,cValPSN)
 	  i = iGetSymbol(cPSR,cValPSR)
	  i = iGetSymbol(cCSD,cValCSD)
	  i = iGetSymbol(cCST,cValCST)
	  read (cValPSN,'(I2)') iValPSN
	  read (cValPSR,'(I2)') iValPSR
	  read (cValCSD,'(I2)') iValCSD
	  read (cValCST,'(I2)') iValCST
	  read (cValSTR,'(I2)') iValSTR
	  if (iValCD.eq.0.and.iValCT.eq.0.and.iValPN.eq.0.and.iValPR.eq.0) then

	    if (iValPSN .eq. iValPEN) then
	      iValPSN = 0

	      if (iValPSR .eq. iValPER) then
	        iValPSR = 0

	  	if (iValCSD .eq. iValCED) then
	    	  iValCSD = 0
	    	  iValCST = iValCST+1
	  	else
	    	  iValCSD = iValCSD+1
	  	end if

	      else
	        iValPSR = iValPSR+1
	      end if

	    else
	      iValPSN = iValPSN+1
	    end if

	  else

	  if (iValCD.ne.0) then
	    if(iGetSymbol(cSTR,cValSTR) .eq. 0 .or. cValSTR .ne. '-2') then
		iValSTR = -2
		iValPSR = 0
		iValPSN = 0
		iValCST = -1
	    end if
	    if(iValCST.eq.iValCET) then
		iValCST = 0
		if(iValPSR.eq.iValPER) then
		    iValPSR = 0
		    iValPSN = iValPSN + 1
		else
		iValPSR = iValPSR + 1
	        end if
	    else
	    	iValCST = iValCST+1
	    end if
	    if(iValPSN.eq.iValPEN.and.iValPSR.eq.iValPER.and.iValCST.eq.iValCET) then
		iValSTR = 9
	    end if
	  end if

	  if (iValCT.ne.0) then
	    if(iGetSymbol(cSTR,cValSTR) .eq. 0 .or. cValSTR .ne. '-2') then
		iValSTR = -2
		iValPSR = 0
		iValPSN = 0
		iValCSD = -1
	    end if
	    if(iValCSD.eq.iValCED) then
		iValCSD = 0

		if(iValPSR.eq.iValPER) then
		    iValPSR = 0
		    iValPSN = iValPSN + 1
		else
		iValPSR = iValPSR + 1
	        end if
	    else
	    	iValCSD = iValCSD+1
	    end if
	    if(iValPSN.eq.iValPEN.and.iValPSR.eq.iValPER.and.iValCSD.eq.iValCED) then
		iValSTR = 9
	    end if
	  end if

	  if (iValPN.ne.0) then
	    if(iGetSymbol(cSTR,cValSTR) .eq. 0 .or. cValSTR .ne. '-2') then
		iValSTR = -2
		iValPSR = 0
		iValCSD = 0
		iValCST = -1
	    end if
	    if(iValCST.eq.iValCET) then
		iValCST = 0
		if(iValPSR.eq.iValPER) then
		    iValPSR = 0
		    iValCSD = iValCSD + 1
		else
		    iValPSR = iValPSR + 1
		end if
	    else
	    	iValCST = iValCST+1
	    end if
	    if(iValPSR.eq.iValPER.and.iValCSD.eq.iValCED.and.iValCST.eq.iValCET) then
		iValSTR = 9
	    end if
	  end if

	  if (iValPR.ne.0) then
	    if(iGetSymbol(cSTR,cValSTR) .eq. 0 .or. cValSTR .ne. '-2') then
		iValSTR = -2
		iValPSN = 0
		iValCSD = 0
		iValCST = -1
	    end if
	    if(iValCST.eq.iValCET) then
		iValCST = 0
		if(iValPSN.eq.iValPEN) then
		    iValPSN = 0
		    iValCSD = iValCSD + 1
		else
		    iValPSN = iValPSN + 1
		end if
	    else
	    	iValCST = iValCST+1
	    end if
	    if(iValPSN.eq.iValPEN.and.iValCSD.eq.iValCED.and.iValCST.eq.iValCET) then
		iValSTR = 9
	    end if
	  end if

	  end if

	end if

	if (iValCD.eq.0.and.iValCT.eq.0.and.iValPN.eq.0.and.iValPR.eq.0) iValSTR = iValSTR + 1
	CSD = CSD0+iValCSD*dCSD
	CST = CST0+iValCST*dCST
	PSN = PSN0+iValPSN*dPSN
	PSR = PSR0+iValPSR*dPSR

	print *, 'iValCSD, iValCST, iValPSN, iValPSR, iValSTR =', iValCSD, iValCST, iValPSN, iValPSR,'    ', iValSTR
	print *, 'CSD, CST, PSN, PSR', CSD, CST, PSN, PSR

	if (iValS3R.eq.9.and.iValCD.ne.0) then
	  if(iValCET.ne.0) then
	    iValCST = 1
	  else
	    iValCST = 0
	  end if
	  if(iValPEN.ne.0) then
	    iValPSN = 1
	  else
	    iValPSN = 0
	  end if
	  if(iValPER.ne.0) then
	    iValPSR = 1
	  else
	    iValPSR = 0
	  end if
	end if
	if (iValS3R.eq.9.and.iValCT.ne.0) then
	  if(iValCED.ne.0) then
	    iValCSD = 1
	  else
	    iValCSD = 0
	  end if
	  if(iValPEN.ne.0) then
	    iValPSN = 1
	  else
	    iValPSN = 0
	  end if
	  if(iValPER.ne.0) then
	    iValPSR = 1
	  else
	    iValPSR = 0
	  end if
	end if
	if (iValS3R.eq.9.and.iValPN.ne.0) then
	  if(iValCED.ne.0) then
	    iValCSD = 1
	  else
	    iValCSD = 0
	  end if
	  if(iValCET.ne.0) then
	    iValCST = 1
	  else
	    iValCST = 0
	  end if
	  if(iValPER.ne.0) then
	    iValPSR = 1
	  else
	    iValPSR = 0
	  end if
	end if
	if (iValS3R.eq.9.and.iValPR.ne.0) then
	  if(iValCED.ne.0) then
	    iValCSD = 1
	  else
	    iValCSD = 0
	  end if
	  if(iValCET.ne.0) then
	    iValCST = 1
	  else
	    iValCST = 0
	  end if
	  if(iValPEN.ne.0) then
	    iValPSN = 1
	  else
	    iValPSN = 0
	  end if
	end if

	i = iFilePath(cEnvi//'temp',0,' ','ips2020.input',cFile)

	i = OPN__TEXT+OPN__REOPEN+OPN__UNKNOWN
	if (bOpenFile(i,iU,cFile,iRec)) then
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') '18'
	    write (iU,'(A)') '0'
	    write (iU,'(A)') 'both'
	    write (iU,'(A)') 'Nagoya'
	    write (iU,'(A)') 'Nagoya'
	    write (iU,'(A)') 'YES'
	    write (iU,'(A)') '1'
	    write (iU,'(A)') '-10'
	    write (iU,'(A)') '44'
	    write (iU,'(A)') 'YES'
	    write (iU,'(A)') '1'
	    write (iU,'(A)') '-10'
	    write (iU,'(A)') '44'
	    write (iU,'(A)') '1.2'
	    write (iU,'(A)') '9'
	    write (iU,'(A)') 'All velocity data'
	    write (iU,'(A)') '5'
	    write (iU,'(A)') '180'
	    write (iU,'(A)') '90'
	    write (iU,'(A)') '0.00465241003'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') '1200'
	    write (iU,'(A)') '1.2'
	    write (iU,'(A)') '9'
	    write (iU,'(A)') 'All G-level data'
	    write (iU,'(A)') '5'
	    write (iU,'(A)') '180'
	    write (iU,'(A)') '90'
	    write (iU,'(A)') '0.00465241003'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') '4'
	    write (iU,'(A)') '0.2'
	    write (iU,'(A)') '1.0'

	    write (iU,'(A)') '1.0' 	! G-level time hole filter 
	    write (iU,'(A)') '1.0'	! V time hole filter

	    write (iU,'(F6.3)') CSD	! Spatial G-level filter

	    write (iU,'(F6.3)') CSD	! Spatial V filter

	    write (iU,'(F5.3)') CST	! Temporal G-level filter
 
	    write (iU,'(F5.3)') CST	! Temporal V filter

	    if (PWN .lt. 0) then
		write (iU,'(F6.3)') PSN	! G-level power to fit density
	    else

		write (iU,'(F5.3)') PSN
	    end if
	    if (PWN .lt. 0) then
		write (iU,'(F6.3)') PSN	! G-level power to fit velocity
	    else
		write (iU,'(F5.3)') PSN
	    end if
	    if (PWR .lt. 0) then
		write (iU,'(F6.3)') PSR	! Radial G-level falloff to fit G density
	    else
		write (iU,'(F5.3)') PSR
	    end if
	    if (PWR .lt. 0) then
		write (iU,'(F6.3)') PSR	! Radial G-level falloff to fit V density
	    else
		write (iU,'(F5.3)') PSR
	    end if

	    write (iU,'(A)') '400'
	    write (iU,'(A)') '1'
	    write (iU,'(I4)') iValCRT
	    write (iU,'(A)') '1'
	    write (iU,'(A)') '1'

	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') 'YES'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') 'NO'
	    write (iU,'(A)') 'NO'
	    iU = iFreeLun(iU)
	end if

	write (cValCSD,'(I2.2)') iValCSD
	write (cValCST,'(I2.2)') iValCST
	write (cValPSN,'(I2.2)') iValPSN
	write (cValPSR,'(I2.2)') iValPSR
	if(iValSTR.gt.99) iValSTR = 0
	write (cValSTR,'(I2)') iValSTR

	i = iSetSymbol(cCSD,cValCSD,1)
	i = iSetSymbol(cCST,cValCST,1)
	i = iSetSymbol(cPSN,cValPSN,1)
	i = iSetSymbol(cPSR,cValPSR,1)
	i = iSetSymbol(cSTR,cValSTR,1)

	end
