from astropy.io import ascii import numpy as np import os, subprocess import time # This code takes in inputs that will make an orbit double star list. The file it is printed to requires certian # columns and this code will space the inputs correctly and cut off extra characters. There are no protections to # stop bad data entry. This code is just for formatting. The only cool feature this code has is if the star name is # not found but an A, AB ... version is found it will show you and provide you the options to retype it. If you know # the star is not on the WDS leave star name blank or none to skip the search. Another feature is the units of the # period, periastron, and semi major axis. You can leave them it blank will defualt to year,year, arcsec respectively. # This code puts the period in the correct spot of the columns. ##### Quick Start Guide ##### # 1. To run this code open command prompt and navigate to the folder that contents this program. # - Make sure a up to date WDS list is in the same folder as this program (or you can change the file path # in the function (load_file_WDS()) # 2. To run the code on windows 'py FormatOrbitInfo.py', mac 'python3 FormatOrbitInfo.py' # 3. Input all the data from the source (paper). If there is no data just hit enter and the program will auto fill the # spacing. # - Star Name: will load in data from WDS Catalog and when found will auto print precise coordinates and WDS name # * WHEN left blank or 'none' is inputted the user will be prompted to input the precise coordinates, WDS name and # star name manually # * If a star name is missing or incorrectly typed the program may show an alternative an and will always # provide another chance to input another name (either one shown or the user manually inputs) and not # have to retype all the data. # - Units: Units when left blank will be Period/'y',Periastron/'y', Arcsec/'a', then the user will can put in there # own unit character and will only take the first character. # - Paper: So one useful thing I am leaving a input for paper and variable declaration and then you can pick one. # This just makes it easier if you are taking in many data from the same paper you dont have to keep typing it. # 4. The program will run and then will open the text file for the user to check over. # # THIS IS FOR AUTO ENTRY FROM A COMPUTER READBALE TABLE # Load in the data set and make the the first line of the document has the proper column sizes # an example in the top line have AA13.2AAA instead of 13.2 helps with the fixed width # this code does not read the first line # check to make sure the spacing is right on the des name in the text file # make sure the columns are lined up and set the variables to the correct column start_time = time.time() ############# Variables to change ############## file_path = "C:\\Users\\Ginkl\\Desktop\\NavySummer2021\\Ellipse thing\\" # location of the WDS file textfilename = 'paper_orbits_for_real12.txt' # name of the orbit file def load_tok_data(): data = ascii.read('Tok2020ea.txt',format='fixed_width',delimiter= ' ') print(data) #ascii.write(data,'testtok.txt',delimiter = ' ',format='fixed_width') return data ''' # Input Perameters from papers star_name = input("Star Name: ex: TOK1777: ") # designated star name ADS = input("ADS: ") HD = input("HD: ") HIP = input("HIP: ") # V1_11 = input("V1_11: ") # V2_22 = input("V2_22: ") p = input("P: ") p_error = input("P error: ") p_unit = input("P unit: (blank is y) ") a = input("a: ") a_error = input("a error: ") a_unit = input("a unit: (blank is a) ") i = input("i: ") i_error = input("i error: ") n = input("N: ") n_error = input("N error: ") t = input("T: ") t_error = input("T error: ") t_unit = input("T unit: (blank is y) ") e = input("e: ") e_error = input("e error: ") o = input("O: ") o_error = input("O error: ") EQNX = input("EQNX: ") Last = input("Last: ") EPHEMRS = input('EPHEMRS: ') ''' # This function will check the input to be the correct length and where the period should go and fix the spacing and # if the spacing goes in the front or after the input def check_input(input1, length, no_length, index_period): # takes the string input, how long it should be, the output # if there is no input, where the period should go (-1 is no period), (index > 0) # print(input) if index_period == -1: # run when there is no period in the output if input1 == '': # when there is no input input1 = no_length elif len(input1) < length: # When the length is too short space is added to the front of the input input1 = addspace(length, len(input1), input1, ' ') if len(input1) > length: # When the length is too long the extra characters will be removed input1 = wronglength(length, len(input1), input1) else: # run when there is a period in the output # everything in this statement goes one after another so that all the parameters can be checked if the input # needs a period and space added to the front and length cut off the end. # print("enter else") if input1 == '': # when there is no input input1 = no_length elif '.' not in input1: # if the input does not have a period add it to the end (helps will spacing) input1 = input1 + "." if input1.index('.') != index_period: # if the index of the period is too early to where is should be # code will add spaces to the front of the input. # print("should correct space") input1 = period_space_add(input1, length, index_period, ' ') if len(input1) < length: # If the length is too short then spaces are added to the end of the input input1 = addspace_back(length, len(input1), input1, ' ') # print(len(input)) if len(input1) > length: # if the input is too long then the extra length characters are cut off input1 = wronglength(length, len(input1), input1) # print("final lenght") # print(len(input)) return str(input1) # Loads in the WDS columns for precise coordinates, WDS name, and Designators name def load_files(): WDS_Stars = ascii.read(file_path + "WDS_Stars.txt", format="fixed_width", data_start=3, fill_values=(' . ', ' . '), delimiter=' ', col_starts=(0, 10, 112, 58, 64, 107), col_ends=(9, 21, 129, 62, 68, 110)) # print(WDS_Stars) # WDS NAME, DES Name, Precise, V1, V2, notes return WDS_Stars # This method will delete extra character from a string to ensure that it fits into the log def wronglength(correct, length, string): # takes the correct length needed, the current length and the string that will be changed remove = -1 * int(correct - length) # gets the incorrect length and makes it positive for i in range(remove): # for all the range of remove the string will shorten 1 character string = string[:-1] # shortens the string by one character return str(string) # This method will add character to the start of a string to make it longer def addspace(correct, length, string, char): # what is the correct string length, what is the current length, the string to be changed spaces = correct - length # gets the amount of space that need to be added for i in range(spaces): # for however many character need to be added 1 will be added each loop string = char + string # adds the char to the front of the string return str(string) # This method will add character to the end of a string to make it longer def addspace_back(correct, length, string, char): # what is the correct string length, what is the current length, the string to be changed spaces = correct - length # gets the amount of space that need to be added for i in range(spaces): # for however many character need to be added 1 will be added each loop string = string + char # adds the char to the end of the string return str(string) # This method will make sure the period is in the correct location by adding space to the front. # This does not account for if the period is higher then the correct index def period_space_add(string, length, index_period, char): # string input, length the string needs to be, index of the # period, and the character to add the spaces for i in range(len(string)): # checks for every element of the string if a space needs to be added or if the # period is in the correct spot if string.index('.') < index_period: string = char + string if len(string) < length: # once the period is in the correct spot then the string is made to be the correct length # by adddins space to the back string = addspace_back(length, len(string), string, char) return string def period_space_remove(string, length, index_period, char): # string input, length the string needs to be, index of the # period, and the character to remove spaces for i in range(len( string)): # checks for every element of the string if a space needs to be removed from the front or if the # period is in the correct spot if string.index('.') > index_period: string = string[1:] if len(string) < length: # once the period is in the correct spot then the string is made to be the correct length # by adddins space to the back string = addspace_back(length, len(string), string, char) return string # This function will search through the WDS catalog and get the star name that matches the user input or the # stars that contain the user input. Then it will make sure the WDS has a value and if it doesnt will correctly make the # spacing for the output to the file. WHen this function is done the spacing is correct and is read for text output. def org_data(star_name): #print("RUN") WDS_Stars = load_files() # loads in the data once WDS_name = [] # creates some arrays Star_name = [] percise_coor = [] star_name_list = [] V1_11 = [] V2_22 = [] notes = [] index = "a" # makes the input invalid to begin with for i in range(len(WDS_Stars)): # this loops through every star and makes lists of all the columns to later input # the index # print(i) if WDS_Stars[i][2] == '. .': # This is checking the precise coordinates for blank entries percise_coor.append(' . . ') elif len(WDS_Stars[i][2]) != 18: # makes sure if it was too short or too long then it will make it the right # length percise_coor.append(addspace_back(18, len(WDS_Stars[i][2]), WDS_Stars[i][2], ' ')) elif len(WDS_Stars[i][2]) == 18: # When it is the correct length rock and roll percise_coor.append(WDS_Stars[i][2]) if WDS_Stars[i][0] == '': # This is checking the WDS name for blank entries WDS_name.append(' . ') elif len(WDS_Stars[i][0]) != 10: # makes sure if it was too short or too long then it will make it the right # length WDS_name.append(addspace_back(10, len(WDS_Stars[i][0]), WDS_Stars[i][0], ' ')) elif len(WDS_Stars[i][0]) == 10: # When it is the correct length rock and roll WDS_name.append(WDS_Stars[i][0]) if WDS_Stars[i][1] == '': # This is checking the des name for blank entries Star_name.append(' . ') elif len(WDS_Stars[i][1]) != 14: # makes sure if it was too short or too long then it will make it the right # length Star_name.append(addspace_back(14, len(WDS_Stars[i][1]), WDS_Stars[i][1], ' ')) elif len(WDS_Stars[i][1]) == 14: # When it is the correct length rock and roll Star_name.append(WDS_Stars[i][1]) # print(star_name) if WDS_Stars[i][3] == ' . ' or WDS_Stars[i][3]=='': # This is checking the V1 for blank entries V1_11.append(' . ') elif len(WDS_Stars[i][3]) != 5: # makes sure if it was too short or too long then it will make it the right # length V1_11.append(addspace_back(5, len(WDS_Stars[i][3]), WDS_Stars[i][3], ' ')) elif len(WDS_Stars[i][3]) == 5: # When it is the correct length rock and roll V1_11.append(WDS_Stars[i][3]) if WDS_Stars[i][4] == ' . ' or WDS_Stars[i][4] == ' ': # This is checking the V2 for blank entries V2_22.append(' . ') elif len(WDS_Stars[i][4]) == 5: # When it is the correct length rock and roll V2_22.append(WDS_Stars[i][4]) elif len(WDS_Stars[i][4]) != 5: # makes sure if it was too short or too long then it will make it the right # length # print("???") # print(i) # print(WDS_Stars[i][4]) V2_22.append(addspace_back(5, len(WDS_Stars[i][4]), WDS_Stars[i][4], ' ')) if WDS_Stars[i][5] == '' or WDS_Stars[i][5] == ' ': # This is checking the V1 for blank entries notes.append('n') elif len(WDS_Stars[i][5]) != 1: # makes sure if it was too short or too long then it will make it the right # length notes.append(addspace_back(1, len(WDS_Stars[i][5]), WDS_Stars[i][5], ' ')) elif len(WDS_Stars[i][5]) == 1: # When it is the correct length rock and roll notes.append(WDS_Stars[i][5]) if star_name == WDS_Stars[i][1]: # gets the index when the star name correctly matches index = i # makes the index # index = WDS_Stars[i][1].index(star_name) elif WDS_Stars[i][1].find(star_name) >= 0: # if the name is contained save the names to an array star_name_list.append(WDS_Stars[i][1]) # print(Star_name) # print(index) return percise_coor, WDS_name, Star_name, index, star_name_list, V1_11, V2_22, notes # create the png at the end of the file def png(WDS_name): png_file = 'wds' + str(WDS_name) + 'a.png' return png_file # time units def P_T_unit(user_input): if user_input == '': # if blank set to this output = 'y' else: output = wronglength(1, len(user_input), user_input) # makes it just one length return output # axis units def semi_major_axis_unit(user_input): if user_input == '': # if blank set to this output = 'a' else: output = wronglength(1, len(user_input), user_input) return output def note_fix(note): # makes sure the note has a default and is only one character if len(note) > 0: output = wronglength(1, len(note), note) if output.isalpha() == False: output = 'n' else: output = 'n' return output # this writes to the file def write_line(): #try: # try using the index percise_coor = np.array(from_WDS[0]) WDS_star_name = np.array(from_WDS[1]) star_name = np.array(from_WDS[2]) index = from_WDS[3] V1_11 = np.array(from_WDS[5]) V2_22 = np.array(from_WDS[6]) notes = np.array(from_WDS[7]) V2_22_value =check_input(V2_22[index], 6, " . ",2) if V2_22_value == ' .': V2_22_value = period_space_remove(V2_22_value,6,2,' ') # makes the spacing and uses the index and line = str(percise_coor[index]) + ' ' + str(WDS_star_name[index]) + ' ' + str( star_name[index]) + ' ' + check_input( ADS, 5, ". ", -1) + ' ' + check_input(HD, 6, ". ", -1) + ' ' + check_input(HIP, 6, ". ", -1) \ + ' ' + check_input(V1_11[index], 6, " ", 2) + ' ' + V2_22_value + ' ' + \ check_input(p, 12, ' . ', 5) + str(P_T_unit(p_unit)) + check_input(p_error, 11, ' . ', 4) + ' ' + \ check_input(a, 9, ' . ', 3) + str(semi_major_axis_unit(a_unit)) + ' ' + \ check_input(a_error, 8, ' . ', 2) + ' ' + \ check_input(i, 8, ' . ', 3) + ' ' + check_input(i_error, 8, ' . ', 3) + ' ' + \ check_input(n, 8, ' . ', 3) + ' ' + check_input(n_error, 8, ' . ', 3) + ' ' + \ check_input(t, 12, ' . ', 5) + str(P_T_unit(t_unit)) + check_input(t_error, 11, ' . ', 4) \ + ' ' + check_input(e, 8, ' . ', 1) + ' ' + check_input(e_error, 8, ' . ', 1) + ' ' \ + check_input(o, 8, ' . ', 3) + ' ' + check_input(o_error, 8, ' . ', 3) + ' ' + check_input( EQNX, 4, ' ', -1) \ + ' ' + check_input(Last, 4, ' ', -1) + ' ' + check_input( EPHEMRS, 7, ' ', -1) + ' 0.0 - ' + note_fix(notes[index]) + ' ' + check_input(Paper, 8, 'no name ', -1) + ' ' + png( WDS_star_name[index]) '''except: # when there is no index print("Name not in WDS") # gets new inputs percise_coor_input = input("Percise Coor: ") WDS_name = input("WDS name: ") star_name = input("Star Name: ex: TOK1777: ") V1_11 = input("V1_11: ") V2_22 = input("V2_22: ") # notes = input("Notes: ") # creates the line for printing to the text line = check_input(percise_coor_input, 18, ' ', -1) + ' ' + \ check_input(WDS_name, 10, ' ', -1) + ' ' + \ check_input(addspace_back(14, len(star_name), star_name, ' '), 14, ' ', -1) \ + ' ' + check_input(ADS, 5, ". ", -1) + ' ' + check_input(HD, 6, ". ", -1) + ' ' + \ check_input(HIP, 6, ". ", -1) + ' ' + check_input(V1_11, 6, " . ", 2) + ' ' + \ check_input(V2_22, 6, " . ", 2) + ' ' + check_input(p, 12, ' . ', 5) + \ str(P_T_unit(p_unit)) + check_input(p_error, 11, ' . ', 4) + ' ' + \ check_input(a, 9, ' . ', 3) + str(semi_major_axis_unit(a_unit)) + ' ' + \ check_input(a_error, 8, ' . ', 2) + ' ' + check_input(i, 8, ' . ', 3) + ' ' + \ check_input(i_error, 8, ' . ', 3) + ' ' + check_input(n, 8, ' . ', 3) \ + ' ' + check_input(n_error, 8, ' . ', 3) + ' ' + check_input(t, 12, ' . ', 5) + str( P_T_unit(t_unit)) + check_input(t_error, 11, ' . ', 4) + ' ' + check_input(e, 8, ' . ', 1) \ + ' ' + check_input(e_error, 8, ' . ', 1) + ' ' + check_input(o, 8, ' . ', 3) + ' ' + \ check_input(o_error, 8, ' . ', 3) + ' ' + check_input(EQNX, 4, ' ', -1) + ' ' + \ check_input(Last, 4, ' ', -1) + ' ' + check_input(EPHEMRS, 7, ' ', -1) \ + ' 0.0 - - ' + \ check_input(Paper, 8, 'no name ', -1) + ' ' + png(WDS_name) ''' print(line) f = open(textfilename, 'a+') # opens the file f.write(line) # writes the name f.write('\n') # gets to the next line f.close() try: os.startfile(textfilename) # opens the file on windows computer except: try: subprocess.call(["open", textfilename]) # opens the file on mac except: subprocess.call(["xdg-open", textfilename]) # opnes the file on linux data= load_tok_data() # where you set the columns for auto entry for j in range(0,len(data)): star_name =str(data[j][1]) print('name',star_name) ADS = '' HD = '' HIP = '' p = str(data[j][2]) print(p) p_error = '' p_unit = 'y' a = str(data[j][5]) print(a) a_error = '' a_unit = 'a' i = str(data[j][8]) print(i) i_error = '' n = str(data[j][6]) print(n) n_error = '' t = str(data[j][3]) print('time',t) t_error = '' t_unit = 'y' e = str(data[j][4]) print(e) e_error = '' o = str(data[j][7]) print(o) o_error = '' EQNX = '' Last = '' EPHEMRS = '' ##### Change the paper from input to hard coded variable ############ # Paper = input("Paper Ref: ") Paper = "TOK2020e" print("Code Running...") # Lets you know it's cooking # start_time = time.time() Paper = addspace_back(8, len(Paper), Paper, ' ') global from_WDS #print(star_name) from_WDS = org_data(star_name) # only calls the search function when a name needs to be searched #print(from_WDS[3]) if from_WDS[3] == "a": # if the index is invalid print(from_WDS[4]) # prints the alternative names star_name1 = input("Star Name: ex: TOK1777: ") # give another input chance # print("after",star_name) from_WDS = org_data(star_name1) # runs with an updated name write_line() else: write_line() #print("--- %s seconds ---" % (time.time() - start_time))