'#!/usr/bin/env python' #! /sw/bin/python # Program: GalacticMSPs.py # Version: 1.0 # Author: DAS # History: # 20110921: Adapted from Xian Hou shklovkii_PM.py and David Smith MarelliXraysFits.py # 20121228: Adapted from PdH Distances/Distance_ascii2fits.py # Reads Duncan Lorimer's file, # http://astro.phys.wvu.edu/GalacticMSPs/GalacticMSPs.txt # and outputs a fits file for BigFile convienience. import os,sys #from os.path import join import numpy as np import pyfits #from time import localtime,strftime fitsname = 'GalacticMSPs.fits' cmd='rm -rf %s' %fitsname os.system(cmd) # Read data and references dataname = 'GalacticMSPs.txt' GalMSPs=np.genfromtxt(dataname, dtype=None, names=['PSRJ','P0','DM','Gl','Gb','Pb','SemiMajX','When','Who'],comments='#') psrj,p0,dm,gl,gb = [], [], [], [], [] pb, x, when, who = [], [], [], [] for i, item in enumerate(GalMSPs): psrj.append(item['PSRJ']) p0.append(item['P0']) dm.append(item['DM']) gl.append(item['Gl']) gb.append(item['Gb']) pb.append(item['Pb']) x.append(item['SemiMajX']) when.append(item['When']) who.append(item['Who']) # DEFINE COLUMNS of the data table #E: single precision, D: double precision, A: string, #E10.2: floating point exponential format (uppercase),length 10, decimal numbers 2, F8.2:floating point decimal format. names=['PSRJ','P0','DM','Gl','Gb','Pb','SemiMajX','When','Who'] formats=['11A','E','E','E','E','10A','10A','6A','20A'] displays=['11A','F8.3','F8.3','F8.3','F8.3','10A','10A','6A','20A'] units=['','ms','pc/cc','deg','deg','days','light sec','Year','tags'] arrays=[psrj, p0, dm, gl, gb, pb, x, when, who] cols=[] for i in range(len(names)): cols.append(pyfits.Column(name=names[i],format=formats[i],unit=units[i],array=arrays[i])) #=============> create a ColDefs (column-definitions) object for all columns cols1=pyfits.ColDefs(cols[0:len(cols)]) #=============> create a new binary table HDU object tbhdu_1 = pyfits.new_table(cols1) tbhdu_1.name = 'MilkyWayMSPs' #name of the 1st Extension #=============> create an empty PrimaryHDU hdu = pyfits.PrimaryHDU() #=============> create a HDUList containing both the primary HDU and the newly created table extension, #and write to a new file listhdu = pyfits.HDUList([hdu, tbhdu_1]) #=============> add comments to the headers ### for the 1st EXTENSION #listhdu = pyfits.open(fitsname) hduhdr1 = listhdu[1].header # get the header of the 1st EXTENSION, i.e. 'DISTDATA' # list of key values tvalues=names #TTYPE values fvalues=formats #TFORM values uvalues=[] #TUNIT values for i,unit in enumerate(units): if unit !='': uvalues.append(unit) # list of keys list_keys1=hduhdr1.ascardlist().keys() print list_keys1 tkeys1=[] fkeys1=[] ukeys1=[] for i, key in enumerate(list_keys1): if 'TTYPE' in key: tkeys1.append(key) if 'TFORM' in key: fkeys1.append(key) if 'TUNIT' in key: ukeys1.append(key) #list of comments for keys tcomment1=['Pulsar Name','MSP period','Dispersion Measure DM','Galactic latitude','Galactic longitude','Orbital period','projected semi major axis','Discovery year','Notes'] fcomment1=['','','','','','','','',''] ucomment1=['','','','','','','','',''] #print len(tkeys1), len(tvalues), len(tcomment1) for i in range(len(tkeys1)): #update the TTYPE keys hduhdr1.update(key=tkeys1[i],value=tvalues[i], comment=tcomment1[i]) for i in range(len(fkeys1)): #update the TFORM keys hduhdr1.update(key=fkeys1[i],value=fvalues[i], comment=fcomment1[i]) for i in range(len(ukeys1)): #update the TUNIT keys hduhdr1.update(key=ukeys1[i],value=uvalues[i], comment=ucomment1[i]) list_cards1=hduhdr1.ascardlist() listhdu.writeto(fitsname) # print out information of the specified FITS file print 'pyfits.info(fitsname)' pyfits.info(fitsname) listhdu.close()