# Starmap - plot a file of star coordinates and constellations # # Jay Summet # CS 1301 - Georgia Tech # Public Domain, Spring 2009 # # # This program plots a field of stars from the stars.txt taken from # http://nifty.stanford.edu/2009/reid-starmap/ # from myro import * #This function takes as input a string representing one star from the #stars.txt data file. It is also passed in a starTuple that has already #extracted data about the star in it, and a dictionary. This function #will parse out the (0 or more) names associated with this star, and #associate those (0 or more) names with the starTuple in the nameDict. def getNames(starLine, starTuple, nameDict): #Find the name(s) (if any!) for the star #Star names are at the end, after 5 spaces... countOfSpaces = 0 index = 0 #Count 5 spaces, or hit the end of the line. while( countOfSpaces < 6 and index < len(starLine) ): letter = starLine[index] #print "index is:", index, "letter is", letter #print "countOfSpaces is:", countOfSpaces if letter == " ": countOfSpaces= countOfSpaces + 1 index = index + 1 nameText = starLine[index:] #If this star HAS a name or names.... if (len(nameText) > 0): #More than 1 name are split by ";"'s.... listOfNames = nameText.split(";") for name in listOfNames: nameTemp1 = name.rstrip() nameTemp2 = nameTemp1.lstrip() #Associate the starTuple with this name. nameDict[nameTemp2] = starTuple #Although we were passed a refernece to the dictionary, #and presumably the calling function knows which dictionary it #passed us, sometimes it's nice to return a reference to the dictionary #as well when we are done. return nameDict #Opens the specified file and reads in the X and Y coordinates of each star #along with the stars brightness (magnatude) value. #Returns a list of tuples with the X and Y coordinates and magnitude. def readStarData(filename): nameDict = {} #Dictionary of star names with star tuples. starList = [] #List of all star tuples (even those without names) myFile = open(filename, "r") starData = myFile.readlines() for starLine in starData: tempList = starLine.split() xStr = tempList[0] yStr = tempList[1] magStr = tempList[4] x = float( xStr) y = float( yStr) mag = float(magStr) starTuple = (x,y,mag) starList.append( starTuple ) getNames(starLine, starTuple, nameDict) myTuple = (starList, nameDict) return( myTuple ) #Given a file that contains pairs of comma seperated star names, this #function will look up the X/Y position of each star and connect them via #lines on the GraphWin. def plotConst(filename, namedStarDict, GraphWin): myFile = open(filename,"r") constLines = myFile.readlines() myFile.close() newList = [] for line in constLines: starsList = line.split(",") newItem = [ starsList[0], starsList[1].rstrip() ] #print newItem newList.append(newItem) #plot the lines! #for each pair of stars, find the X,Y coordinate #first guy! for lines in newList: nameFirst = lines[0] nameSecond = lines[1] xyFirst = namedStarDict[nameFirst] xySecond = namedStarDict[nameSecond] #print "Star 1 is:", nameFirst, "located at:", xyFirst line = Line( Point(xyFirst[0], xyFirst[1]), Point(xySecond[0], xySecond[1]) ) line.setFill( color_rgb(255,255,255) ) line.draw(GraphWin) # Creates a graphWindow and draws all stars into it. Stars in the starData # parameter are assumed to be in the format generated by the readStarData # function (above) def plotStarData(starData, namedStarList, winSize): myWin = GraphWin("StarMap", winSize,winSize) myWin.setCoords(-1.0 , 1.0, 1.0, -1.0) myWin.setBackground( color_rgb(0,0,0) ) for star in starData: X = star[0] Y = star[1] mag = star[2] point = Point(X,Y) #Note that the brightest star has a NEGATIVE magnatude! (-1.47 Sirius) #A magnatude of 6.5 is the faintest star that can be seen with the #unaided human eye. This function converts -1.47 to 255, and 7 to zero. #Because many stars have a magnatude under 7, we enforce a minimum #color value of zero using the MAX function. BV = -(mag-7) * 103.0 BV = max(BV,0) point.setFill( color_rgb(BV,BV,BV)) #To plot all stars at the same brightness, use this line instead: #point.setFill( color_rgb(255,255,255)) point.draw(myWin) return myWin #To draw a star map, you have to do two things: #First, read the data: myStarTuple = readStarData("stars.txt") myStarList = myStarTuple[0] namedStarDict = myStarTuple[1] print "Number of Stars total:", len(myStarList) print "Number of Star Names:", len(namedStarDict) #Then, plot the data! myWin = plotStarData( myStarList,namedStarDict,700) print "All finished plotting stars!" plotConst("Cas_lines.txt", namedStarDict, myWin) plotConst("Cyg_lines.txt", namedStarDict, myWin) plotConst("BigDipper_lines.txt", namedStarDict, myWin) plotConst("Bootes_lines.txt", namedStarDict, myWin) plotConst("Gemini_lines.txt", namedStarDict, myWin) #plotConst("UrsaMajor_lines.txt", namedStarDict, myWin) #plotConst("UrsaMinor_lines.txt", namedStarDict, myWin)