Skip to content

Instantly share code, notes, and snippets.

@matshofman
Created November 26, 2012 19:48
Show Gist options
  • Save matshofman/4150213 to your computer and use it in GitHub Desktop.
Save matshofman/4150213 to your computer and use it in GitHub Desktop.
Star data to JSON
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RADECtoALTAZ
{
/// <summary>
/// Parse star data from 9000+ stars magnitude 6.5 and brighter and convert them to the following JSON
/// [
/// { "id" : "1", "RA" : "1.25", "Dec" : "45.2166666666667", "Magnitude" : "6.7" },
/// { "id" : "2", "RA" : "1.25", "Dec" : "-0.5", "Magnitude" : "6.2" },
/// etc...
/// ]
/// Data from the Yale Bright Star Catalogue
/// Download from http://tdc-www.harvard.edu/catalogs/bsc5.html bsc5.dat.gz [ASCII catalog]
/// </summary>
class StarDataToJson
{
public static void Process()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
StreamReader streamReader = new StreamReader("bsc5.dat");
StreamWriter streamWriter = new StreamWriter("C://stars.json");
streamWriter.WriteLine("[");
string line = streamReader.ReadLine();
while(line != null)
{
string nextLine = streamReader.ReadLine();
try
{
string lineId = line.Substring(0, 4);
string lineMagnitude = line.Substring(102, 4);
string lineRAh = line.Substring(75, 2);
string lineRAm = line.Substring(77, 2);
string lineDecPM = line.Substring(83, 1);
string lineDech = line.Substring(84, 2);
string lineDecm = line.Substring(86, 2);
int Id = Convert.ToInt32(lineId.Trim());
double magnitude = Convert.ToDouble(lineMagnitude);
double RAh = Convert.ToDouble(lineRAh);
double RAm = Convert.ToDouble(lineRAm);
double RA = (RAh + RAm / 60.0) * 15;
double Dech = Convert.ToDouble(lineDech);
double Decm = Convert.ToDouble(lineDecm);
double Dec = Dech + Decm / 60.0;
if (lineDecPM.Equals("-"))
Dec *= -1;
string jsonLine = "{ \"id\" : \"" + Id + "\", \"RA\" : \"" + RA + "\", \"Dec\" : \"" + Dec + "\", \"Magnitude\" : \"" + magnitude + "\" }";
if (nextLine != null)
jsonLine += ",";
streamWriter.WriteLine(jsonLine);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
line = nextLine;
}
streamWriter.WriteLine("]");
streamWriter.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment