' Translated from presumed F77 to VBScript code 2002.12.31 15:27:01
' Source file: C:\data\Projects\f772vbs\f77atm.f
' 2003.01.01 - created "ambience" object
Option Explicit ' because it's always a good idea...
dim sig, altitudes, altitude, Atm

' number of significant digits we'll use in output.
sig = 6



altitudes = Array(0, 15243.90244, 30487.80488)

for each altitude in altitudes

	set Atm = Atmosphere(altitude)
	
	wscript.echo FormatData(Atm.altitude, sig), _
		FormatData(Atm.ratioTemperature, sig), _
		FormatData(Atm.ratioDensity, sig), _
		FormatData(Atm.ratioPressure, sig)
		
next




' =================================================
'   Created Functions
' =================================================

Function Atmosphere(altitude)
	'   ^^^^^ * 1976 Standard Atmosphere ^^^^^
	' VBScript conversion of Mason's Fortran conversion of Mason's Basic routine
	'     Fortran version - SEPT. 1, 1989
	'     VBScript version - 2002.12.31, via f2vbs script and hand parsing.
	'     VALIDATION:
	'     validation was done via comparison to data on pages E-31 and E-32 of
	'     Mason's "Applied Computational Aerodynamics", using electronic version
	'     in PDF format dated January 21, 1997 (CAtxtAppE7.pdf). Scaled quantities
	'     matched to 6 significant digits for T/T0, R/R0, P/P0.
	'
	'     INPUT
	'     altitude  - altitude in meters
	'     OUTPUT:
	'     (0) temperature in degrees Kelvin
	'     (1) pressure in N/m^2
	'     (2) density in kg/m^3
	'     (3) speed for mach 1 in medium in m/s
	'     (4) viscosity in kg/m/s
	'
	'     (5) ratioTemperature - ratio of temperature to sea level temperature
	'     (6) ratioDensity - ratio of air density to sea level air density
	'     (7) ratioPressure - ratio of pressure to sea level pressure 
	'     (8) reynoldsPerMachLength - reynolds number per mach per unit of length
	'     (9) dynamicPressure - dynamic pressure/mach^2

	' no explicit types in VBScript, of course - in this case, pretty much
	'  everything will be coerced into a double precision real.
	
	dim hsConstant, geoPotentialHeight, viscosity, Atm, mu0, seaLevelMach1
	dim earthRadius, seaLevelTemp, seaLevelPressure, seaLevelDensity
	dim temperature, ratioPressure
	
	hsConstant = 34.1631947 ' in kelvin/km
	earthRadius = 6356766 ' in meters
	seaLevelTemp = 288.15: seaLevelPressure = 101325: seaLevelDensity = 1.225
	seaLevelMach1 = 340.294 ' m/s

	mu0 = 1.458e-6 ' m^2/s - Sutherland constant?
	' geoPotentialHeight -  Allows us to handle atmospheric condition equations
	' with simpler equations; this transforms the altitude into an "adjusted" one
	' which quietly handles the decrease of g with altitude.
	geoPotentialHeight = (altitude / (1 + altitude/earthRadius)) / 1000

	Select Case true ' <-- because VBScript doesn't test expressions...

		case geoPotentialHeight <= 11
			temperature = 288.15 - 6.5 * geoPotentialHeight
			ratioPressure = (288.15 / temperature)^( -hsConstant/6.5)

		case geoPotentialHeight <= 20
			temperature = 216.65
			ratioPressure = .22336 * exp( _
				-hsConstant * (geoPotentialHeight - 11)/216.65)

		case geoPotentialHeight <= 32000
			temperature = 216.65 + (geoPotentialHeight - 20)
			ratioPressure = .054032 * (216.65 / temperature)^hsConstant

		case geoPotentialHeight <= 47
			temperature = 228.65 + 2.8 * (geoPotentialHeight - 32)
			ratioPressure = .0085666 * (228.65 / temperature)^(hsConstant/2.8)

		case geoPotentialHeight <=  51
			temperature = 270.65
			ratioPressure = 1.0945e-3 * exp( _
				-hsConstant * (geoPotentialHeight - 47) /270.65)

		case geoPotentialHeight <= 71
			temperature = 270.65 - 2.8 * (geoPotentialHeight - 51)
			ratioPressure = 6.6063e-4 * (270.65 / temperature)^( -hsConstant/2.8)

		case geoPotentialHeight <= 84.852
			temperature = 214.65 - 2 * (geoPotentialHeight - 71)
			ratioPressure = 3.9046e-5 * (214.65 / temperature)^( -hsConstant/2)

		case else
			err.raise vbObjectError + 1, "density lookup", "altitude " _
				& altitude & " km above allowed maximum of 84.852 km."

	End Select

	' now get the data into an object as we go...
	set Atm = New typeAmbience
	Atm.altitude = altitude
	Atm.ratioPressure = ratioPressure
	Atm.ratioDensity = ratioPressure / (temperature / 288.15)
	Atm.viscosity = mu0 * temperature^1.5 / (temperature + 110.4)
	Atm.ratioTemperature = temperature / 288.15
	Atm.mach = seaLevelMach1 * sqr(Atm.ratioTemperature)
	Atm.temperature = seaLevelTemp * Atm.ratioTemperature
	Atm.density = seaLevelDensity * Atm.ratioDensity
	Atm.pressure = seaLevelPressure * ratioPressure
	Atm.reynoldsPerMachLength = Atm.density * Atm.mach / Atm.viscosity
	Atm.dynamicPressure = .7 * Atm.pressure
	set Atmosphere = Atm
end function



function FormatData(value, significance)
	' this is a format function for scientific work and display.
	' it formats the output in terms of the number of significant
	' digits of data to display, unlike any of the internal formatted
	' types already available in script.
	dim dig, ratio, scaled, rounded
	if value<>0 then
		dig = Int(Log(value)/Log(10))
		ratio = 10^(significance - dig - 1)
		scaled = value*ratio
		rounded = round(scaled)
		FormatData = rounded/ratio
	else
		FormatData = 0
	end if
end function




' =================================================
'   Class Definition
' =================================================


Class typeAmbience
	' class-based access even within the same script is horribly slow
	' compared to array-based storage.  In the case of scripting use
	' of numerical methods, though, we are already paying the price of
	' slower performance in the interests of prototyping or pedagogy.
	'
	' This class is really just a simple "container" allowing us to
	' create a structured set of data for mnemonic purposes; it really
	' serves the role of a VB data type or C++ struct. The members are
	' all ambient properties of the atmosphere at a given altitude.
	
	' Properties of the "type-class":
	'   density - 
	'   dynamicPressure - 
	'   mach - 
	'   pressure - 
	'   ratioDensity - 
	'   ratioPressure - 
	'   ratioTemperature - 
	'   reynoldsPerMachLength - 
	'   temperature - 
	'   viscosity - 

	Dim m_density, m_dynamicPressure, m_mach, m_pressure, _
		m_ratioDensity, m_ratioPressure, m_ratioTemperature, _
		m_reynoldsPerMachLength, m_temperature, m_viscosity, _
		m_altitude

	public property let density(value)
		m_density = value
	end property


	public property get density()
	density = m_density
	end property


	public property let altitude(value)
		m_altitude = value
	end property


	public property get altitude()
	altitude = m_altitude
	end property


	public property let dynamicPressure(value)
		m_dynamicPressure = value
	end property


	public property get dynamicPressure()
		dynamicPressure = m_dynamicPressure
	end property


	public property let mach(value)
		m_mach = value
	end property


	public property get mach()
	mach = m_mach
	end property


	public property let pressure(value)
		m_pressure = value
	end property


	public property get pressure()
	pressure = m_pressure
	end property


	public property let ratioDensity(value)
		m_ratioDensity = value
	end property


	public property get ratioDensity()
		ratioDensity = m_ratioDensity
	end property


	public property let ratioPressure(value)
		m_ratioPressure = value
	end property


	public property get ratioPressure()
		ratioPressure = m_ratioPressure
	end property


	public property let ratioTemperature(value)
		m_ratioTemperature = value
	end property


	public property get ratioTemperature()
	ratioTemperature = m_ratioTemperature
	end property


	public property let reynoldsPerMachLength(value)
		m_reynoldsPerMachLength = value
	end property


	public property get reynoldsPerMachLength()
	reynoldsPerMachLength = m_reynoldsPerMachLength
	end property


	public property let temperature(value)
		m_temperature = value
	end property


	public property get temperature()
	temperature = m_temperature
	end property


	public property let viscosity(value)
		m_viscosity = value
	end property


	public property get viscosity()
		viscosity = m_viscosity	
	end property


end class