'tabs=4
'
' Create Starry Night database from GCVS iii.dat.txt standard data.
'
Option Explicit

Const ROOT = "E:\StarryNight Catalogs\GCVS Names and Markers\"
Dim FSO, TB, GF, SF, U, RX, Buf, VarTyp, Period, SpecTyp
TB = Chr(9)								' Tab for delimiter

'
' General Class Converter
'
Dim Eru, Pul, Rot, Cat, Ecl, Xry
Eru = "|FU|GCAS|I|IA|IB|IN|INA|INB|INT|IT|IN(YY)|IS|ISA|ISB|RCB|RS|SDOR|UV|UVN|WR|"
Pul = "|ACYG|BCEP|BCEPS|CEP|CEP(B)|CW|CWA|CWB|DCEP|DCEPS|DSCT|DSCTC|L|LB|LC|M|PVTEL" & _
	  "|RR|RR(B)|RRAB|RRC|RV|RVA|RVB|SR|SRA|SRB|SRC|SRD|SXPHE|ZZ|ZZA|ZZB|"
Rot = "|ACV|ACVO|BY|ELL|FKCOM|PSR|SXARI|"
Cat = "|N|NA|NB|NC|NL|NR|SN|SNI|SNII|UG|UGSS|UGSU|UGZ|ZAND|"
Ecl = "|E|EA|EB|EW|GS|PN|RS|WD|WR|AR|D|DM|DS|DW|K|KE|KW|SD|"
Xry = "|X|XB|XF|XI|XJ|XND|XNG|XP|XPR|XPRM|XM|"

'
' Return Class name or "" given naked Type
'
Function FindClass(BaseType)
	Dim Buf
	Buf = "|" & BaseType & "|"
	If InStr(Eru, Buf) > 0 Then FindClass = "Eruptive" : Exit Function
	If InStr(Pul, Buf) > 0 Then FindClass = "Pulsating" : Exit Function
	If InStr(Rot, Buf) > 0 Then FindClass = "Rotating" : Exit Function
	If InStr(Cat, Buf) > 0 Then FindClass = "Cataclysmic" : Exit Function
	If InStr(Ecl, Buf) > 0 Then FindClass = "Eclipsing" : Exit Function
	If InStr(Xry, Buf) > 0 Then FindClass = "X-Ray" : Exit Function
	FindClass = ""
End Function

'
' VarTyp can be of the form XX, XX/YY, or XX+YY and may end in a ':'
' which I cannot find the meaning of. In any case, XX is simple, just
' look for it in the tables. XX/YY both belong to te same class, so 
' strip off the / and all after, then use what's left to look in the
' tables. XX+YY are split at the + and form a composite class of the 
' form class1,class2.
'
Function VarClass(VarTyp)
	Dim Buf, Buf1, Buf2, I
	
	Buf = Replace(VarTyp, ":", "")							' Copy for modification, remove any ":"
	I = InStr(Buf, "/")
	If I > 0 Then Buf = Left(Buf, I-1)						' If has /, remove / and all following
	I = InStr(Buf, "+")										' Look for a +
	If I = 0 Then											' No +
		VarClass = FindClass(Buf)							' Return class name
		Exit Function										' **EXIT**
	End If
	'
	' Must have + in the string, split and return comma separated list
	'
	Buf1 = Left(Buf, I-1)									' First type
	Buf2 = Mid(Buf, I+1)									' Second type
	VarClass = FindClass(Buf1) & "," & FindClass(Buf2)		' Return class1,class2

End Function
'
' MAIN PROGRAM
'
Set FSO = CreateObject("Scripting.FileSystemObject")
Set U = CreateObject("DriverHelper.Util")


Set GF = FSO.OpenTextFile(ROOT & "Latest Data\iii.dat.txt")
Set SF = FSO.CreateTextFile(ROOT & "GCVS-sn-in.txt")
Set RX = New RegExp

' WARNING! Watch it, there are tabs in these strings!
SF.WriteLine "//	Revision 1 20-May-2005 (R. B. Denny)"
SF.WriteLine "HEADER	DataName	GCVS"
SF.WriteLine "// Stars group"
SF.WriteLine "HEADER	OptionsGroup	3000"
SF.WriteLine "HEADER	Description	Current improved electronic version of the GCVS4 Volumes I-V, combined with Name-Lists of Variable Stars Nos.67-76. (R.B. Denny 20-May-2005)"
SF.WriteLine "HEADER	NumberOfObjects	38528"	'********* CHANGE THIS TO MATCH!!! **********
SF.WriteLine "HEADER	DrawDepth	8600"
SF.WriteLine "HEADER	CataloguePreamble	1	GCVS"
SF.WriteLine "HEADER	KindName	1	GCVS Variable"
SF.WriteLine "HEADER	KindColour	1	0.75	0.75	0.0"
SF.WriteLine "HEADER	AuxillaryLabel	1	General Variability Class"
SF.WriteLine "HEADER	AuxillaryLabel	2	Specific Variability Type"
SF.WriteLine "HEADER	AuxillaryLabel	3	Bright Magnitude"
SF.WriteLine "HEADER	AuxillaryLabel	4	Faint Mag/Ampl"
SF.WriteLine "HEADER	AuxillaryLabel	5	Magnitude Basis"
SF.WriteLine "HEADER	AuxillaryLabel	6	Period (days)"
SF.WriteLine "HEADER	AuxillaryLabel	7	Spectral Type"
SF.WriteLine "HEADER	AuxillaryLabel	8	."
SF.WriteLine "//"
SF.WriteLine "//  Cat# CatKind Mag Kind RA-J2K Dec-J2K Name VarClass VarType BrtMag FntMag MagBasis Period SpecType"
SF.WriteLine "//  -------------------------------------------------------------------------------------------------"

Do While Not GF.AtEndOfStream
	Buf = GF.ReadLine										' Read a line of GCVS
	If Trim(Mid(Buf, 38, 15)) <> "" Then					' Only if coordinates are given (no '=' support)
		SF.Write Trim(Mid(Buf, 1, 6)) & TB & "1" & TB		' Cat# and Catkind
		If Mid(Buf, 66, 1) = "(" Then						' If MaxMag is an amplitude (???)
			SF.Write Trim(Mid(Buf, 77, 6))					' Use MinMag for "Mag"
		Else
			SF.Write Trim(Mid(Buf, 67, 6))					' MaxMag for Mag
		End If
		SF.Write TB & "1" & TB								' Finishing tab And Kind (1)
		SF.Write U.HMSToDegrees(Mid(Buf, 38, 2) & " " & Mid(Buf, 40, 2) & " " & Mid(Buf, 42, 4)) & TB	' RA, degrees J2K
		SF.Write U.DMSToDegrees(Mid(Buf, 46, 3) & " " & Mid(Buf, 49, 2) & " " & Mid(Buf, 51, 2)) & TB	' Dec, degrees J2K
		SF.Write Trim(Mid(Buf, 9, 6)) & " " & Mid(Buf, 15, 3) & TB	' Name with multiple blanks taken out
		VarTyp = Trim(Mid(Buf, 55, 10))						' Variability type code		
		SF.Write VarClass(VarTyp) & TB						' 1 - General variability class
		SF.Write VarTyp & TB								' 2 - Specific variability type code
		SF.Write Trim(Mid(Buf, 66, 9)) & TB					' 3 - Complete max mag field
		SF.Write Trim(Mid(Buf, 76, 12)) & TB				' 4 - Complete min mag field
		SF.Write Mid(Buf, 89, 2) & TB						' 5 - Magnitude basis code
		Period = Trim(Mid(Buf, 111, 20)) 					' 6 - Period or "Unknown"
		If Period <> "" Then
			SF.Write Period
		Else
			SF.Write "Unknown"
		End If
		SF.Write TB
		SpecTyp = Trim(Mid(Buf, 138, 17))					' 7 - Spectral Type or "Unknown"
		If SpecTyp <> "" Then
			SF.Write SpecTyp
		Else
			SF.Write "Unknown"
		End If
		SF.Write VbCrLf										' Last field
	End If
Loop
GF.Close
SF.Close


	
	
	
	
	
