def save(output_file_path, data_array, header_list, dtype_list): print(output_file_path) if output_file_path.lower().endswith('.csv'): # See http://docs.astropy.org/en/stable/io/fits/usage/table.html np.savetxt( output_file_path, data_array, fmt= "%s", # See http://stackoverflow.com/questions/16621351/how-to-use-python-numpy-savetxt-to-write-strings-and-float-number-to-an-ascii-fi delimiter=",", header=",".join(header_list), comments= "" # String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# '. ) elif output_file_path.lower().endswith(('.fits', '.fit')): table = astropy.table.Table(names=header_list, dtype=dtype_list) for row in data_array: table.add_row(row) #print(table) table.write(output_file_path, overwrite=True) else: raise Exception('Unknown output format.')
def save(output_file_path, data_array, header_list, dtype_list): print(output_file_path) if output_file_path.lower().endswith('.csv'): # See http://docs.astropy.org/en/stable/io/fits/usage/table.html np.savetxt(output_file_path, data_array, fmt="%s", # See http://stackoverflow.com/questions/16621351/how-to-use-python-numpy-savetxt-to-write-strings-and-float-number-to-an-ascii-fi delimiter=",", header=",".join(header_list), comments="" # String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# '. ) elif output_file_path.lower().endswith(('.fits', '.fit')): table = astropy.table.Table(names=header_list, dtype=dtype_list) for row in data_array: table.add_row(row) #print(table) table.write(output_file_path, overwrite=True) else: raise Exception('Unknown output format.')
def add_source(table, time, mag, mag_error, ysovarid, fname, sname = None, ra = None, dec = None, useful = 1, kind = 0, note = ''): '''Add info about a fake source to the table. If ra or dec ad None, then look if a source with same ID exisits in the table already. If so, copy their value, if not, generate randomnly. ''' if len(time) != len(mag): raise ValueError('Time and mag need to have same number of elements') if len(mag) != len(mag_error): raise ValueError('mag and mag_error need to have some number of elements') if ra is None: ind = np.nonzero(tab['ysovarid'] == ysovarid)[0] if len(ind) > 0: ra = table['ra'][ind][0] else: ra = np.random.random() * 360. if dec is None: ind = np.nonzero(tab['ysovarid'] == ysovarid)[0] if len(ind) > 0: dec = table['de'][ind][0] else: dec = (np.random.random() * 180) - 90. sname = sname or 'test_'+str(abs(ysovarid)) for i in range(len(time)): table.add_row({"ysovarid": ysovarid,"sname": sname,"ra":ra,"de":dec,"hmjd":time[i],"fname": fname,"mag1":mag[i],"emag1":mag_error[i],"useful":useful,"kind":kind,"note":note})
def add_parinfo_to_table(table, parinfo, chi2, dof, opt_chi2, opt_red_chi2, ra, dec, radius, frontback="", frontbackbest="", name=""): new_row = [name] new_row.append(0) new_row += [frontback, frontbackbest, chi2, dof, opt_chi2, opt_red_chi2, ra, dec, radius] for pp in parinfo[:8]: new_row.append(pp.value) new_row.append(np.nan if pp.fixed else pp.error) table.add_row(new_row) if len(parinfo)>8: new_row = [name] new_row.append(1) new_row += [frontback, frontbackbest, chi2, dof, opt_chi2, opt_red_chi2, ra, dec, radius] for pp in parinfo[8:]: new_row.append(pp.value) new_row.append(np.nan if pp.fixed else pp.error) table.add_row(new_row)
def build_table_from_output_dict(output_dict): """ Traverses the output dict to build a plottable table. """ table = astropy.table.Table() column_names = ("trial_number", "noise_added", "n_clouds", "total_mass", 'inner_larson_A', 'inner_larson_beta', 'outer_larson_A', 'outer_larson_beta', 'inner_M0', 'inner_N0', 'inner_gamma', 'outer_M0', 'outer_N0', 'outer_gamma') for name in column_names: col = astropy.table.Column(data=[], name=name) table.add_column(col) for key, value in output_dict.items(): # we are gonna try and add things row by row preamble = value[0:4] larson, mspec = value[-2:] larson_names = [ 'inner_larson_A', 'inner_larson_beta', 'outer_larson_A', 'outer_larson_beta' ] larson_tuple = tuple(larson[name] for name in larson_names) mspec_names = [ 'inner_M0', 'inner_N0', 'inner_gamma', 'outer_M0', 'outer_N0', 'outer_gamma' ] mspec_tuple = tuple(mspec[name] for name in mspec_names) table.add_row((preamble + larson_tuple + mspec_tuple)) return table
# Documentation: # - http://docs.astropy.org/en/stable/table/index.html#getting-started # - http://www.astropy.org/astropy-tutorials/FITS-tables.html # - http://www.astropy.org/astropy-tutorials/FITS-header.html import argparse from astropy.io import fits import numpy as np import astropy.table # PARSE OPTIONS ############################################################### parser = argparse.ArgumentParser(description="An astropy snippet") parser.add_argument("filearg", nargs=1, metavar="FILE", help="the output FITS file") args = parser.parse_args() file_path = args.filearg[0] # WRITE DATA ################################################################## table = astropy.table.Table(names=("column1", "column2", "column3")) table.add_row([1, 2, 3]) table.add_row([10, 20, 30]) table.add_row([100, 200, 300]) print(table) table.write(file_path, overwrite=True)
"""Generate an astropy-readable .ecsv files for `butler ingest-files`, to ingest an existing gen2 refcat. The `refcat_dir` variable needs to be modified for each refcat being converted. """ import os import glob import astropy.table refcat_dir = "ref_cats/gaia_dr2_20200414" out_dir = "." out_file = f"{out_dir}/{os.path.basename(refcat_dir)}.ecsv" table = astropy.table.Table(names=("filename", "htm7"), dtype=("str", "int")) files = glob.glob(f"{refcat_dir}/[0-9]*.fits") for i, file in enumerate(files): # running status, overwriting each print statement as it proceeds print(f"{i}/{len(files)} ({100*i/len(files):0.1f}%)", end="\r") # extract file index; add row to table file_index = int(os.path.basename(os.path.splitext(file)[0])) table.add_row((file, file_index)) table.write(out_file) print(f"Saving to: {out_file}")
column_names = [ 'Inner edge (pc)', 'Outer edge (pc)', 'GMC index', 'R', 'p', 'Truncation mass (M$_\odot$)', 'Largest cloud (M$_\odot$)', '5th largest cloud (M$_\odot$)' ] column_types = ['f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4'] table = Table(names=column_names, dtype=column_types) for inneredge, outeredge in zip(inneredge, outeredge): idx = np.where((t['RADIUS_PC'] >= inneredge) & (t['RADIUS_PC'] < outeredge)) mass = t['MASS_EXTRAP'][idx].data #don't have to create an index for the xmin mass - defined in the fit_subset fit = powerlaw.Fit(mass) fit_subset = powerlaw.Fit(mass, xmin=3e5) R, p = fit_subset.distribution_compare('power_law', 'truncated_power_law') table.add_row() table[-1]['Inner edge (pc)'] = inneredge table[-1]['Outer edge (pc)'] = outeredge table[-1]['GMC index'] = -fit_subset.alpha table[-1]['R'] = R table[-1]['p'] = p table[-1][ 'Truncation mass (M$_\odot$)'] = 1 / fit_subset.truncated_power_law.parameter2 table[-1]['Largest cloud (M$_\odot$)'] = mass.max() table[-1]['5th largest cloud (M$_\odot$)'] = np.sort( t['MASS_EXTRAP'][idx])[-5] print(table) #print(-fit.alpha, -fit_subset.alpha, R, p, 1/fit_subset.truncated_power_law.parameter2) table.write('m83bininfo.fits', overwrite=True)
column_names = ['Inner edge (pc)', 'Outer edge (pc)', 'GMC index', 'R', 'p', 'Truncation mass (M$_\odot$)', 'Largest cloud (M$_\odot$)', '5th largest cloud (M$_\odot$)'] column_types = ['f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4'] table = Table(names=column_names, dtype=column_types) for inneredge, outeredge in zip(inneredge, outeredge): idx = np.where((t['RADIUS_PC'] >= inneredge) & (t['RADIUS_PC'] < outeredge)) mass = t['MASS_GCORR'][idx].data optresult = plf.plfit_adstat(mass / minmass) pvec = optresult.x # don't have to create an index for the xmin mass - defined in the fit_subset table.add_row() table[-1]['Inner edge (pc)'] = inneredge table[-1]['Outer edge (pc)'] = outeredge table[-1]['GMC index'] = pvec[0] table[-1]['R'] = 0 table[-1]['p'] = 0 table[-1]['Truncation mass (M$_\odot$)'] = pvec[1] * minmass table[-1]['Largest cloud (M$_\odot$)'] = mass.max() table[-1]['5th largest cloud (M$_\odot$)'] =\ np.sort(t['MASS_GCORR'][idx])[-5] print(table) table.write('m83bininfo_ad.fits', overwrite=True)
def data(galaxyname, data, n_bins=1, r_nuc=0): # Import the libraries. from galaxies import Galaxy from astropy.table import Table from astropy.table import Column import astropy import powerlaw import numpy as np import astropy.table import astropy.units as u import matplotlib.pyplot as plt import matplotlib as mpl # Load its FITS file. t = Table.read(data) # Load the information about the galaxy. gxy = Galaxy(galaxyname) # Calculate the galaxy's properties. distance = np.asarray(gxy.distance) inclination = np.asarray(gxy.inclination) rgal = gxy.radius(ra=(t['XPOS']), dec=(t['YPOS'])) rgal = rgal.to(u.kpc) rpgal = np.asarray(rgal) # Append these to the FITS table. col_rgal = Column(name='RADIUS_KPC', data=(rgal)) t.add_column(col_rgal) # Sort the masses according to galactocentric radius. mass = t['MASS_EXTRAP'].data i_sorted = np.argsort(rgal) rgal_sorted = np.asarray(rgal[i_sorted]) mass_sorted = np.asarray(mass[i_sorted]) # Initiate a loop to calculate the bin boundaries and the indeces of these boundaries in the sorted list. totmass = np.sum(mass) / n_bins edge_f = 1.1 * np.max(rgal_sorted) edges = np.zeros(n_bins - 1) start = 0 mass_equiv = [0] #indeces for the sorted mass bins of equal mass mass_area = [0] #indeces for the sorted mass bins of equal area rgal_equiv = [ 0, 2, 8**0.5, 12**0.5, 4, 20**0.5, 24**0.5, 28**0.5, 32**0.5, 6 ] r = 1 #equal-area radial index e = 0 #edge index f = 0 #loop flag to skip the mass_area loop c = 0 #loop counter for i in range(len(mass_sorted)): #Find the indeces for bins of equal mass (equivalent to totmass) if np.sum(mass_sorted[start:i]) > totmass: edges[e] = 0.5 * (rgal_sorted[i] + rgal_sorted[i - 1]) start = i mass_equiv = np.append(mass_equiv, i) e = e + 1 #Find the indeces for bins of equal area (4pi kpc^2) if rgal_sorted[i] > rgal_equiv[r] and not f: if rgal_sorted[i] < rgal_equiv[3] and not f: mass_area = np.append(mass_area, i) r = r + 1 c = 0 if rgal_sorted[i] > rgal_equiv[3]: f = 1 mass_area = np.append(mass_area, i) mass_equiv = np.append(mass_equiv, i) inneredge = np.concatenate(([0.000000], edges)) outeredge = np.concatenate((edges, [edge_f])) # Create a template for a new table. column_names = [ 'Inner edge (kpc)', 'Outer edge (kpc)', 'GMC index', 'R', 'p', 'Truncation mass ($M_\mathrm{\odot}$)', 'Largest cloud ($M_\mathrm{\odot}$)', '5th largest cloud ($M_\mathrm{\odot}$)' ] column_types = ['f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4'] table = Table(names=column_names, dtype=column_types) # Fill the table. for inneredge, outeredge in zip(inneredge, outeredge): idx = np.where((t['RADIUS_KPC'] >= inneredge) & (t['RADIUS_KPC'] < outeredge)) mass = t['MASS_EXTRAP'][idx].data fit = powerlaw.Fit(mass) fit_subset = powerlaw.Fit(mass, xmin=3e5) R, p = fit.distribution_compare('power_law', 'truncated_power_law') table.add_row() table[-1]['R'] = R table[-1]['p'] = p table[-1]['GMC index'] = -fit.alpha table[-1]['Inner edge (kpc)'] = inneredge table[-1]['Outer edge (kpc)'] = outeredge table[-1]['Largest cloud ($M_\mathrm{\odot}$)'] = np.nanmax(mass) table[-1][ 'Truncation mass ($M_\mathrm{\odot}$)'] = 1 / fit.truncated_power_law.parameter2 table[-1]['5th largest cloud ($M_\mathrm{\odot}$)'] = np.sort( t['MASS_EXTRAP'][idx])[-5] # Write the data to a FITS file. table.write('../Data/' + galaxyname + '_data.fits', overwrite=True) # Plot the mass distribution trends for equal-mass bins. t = Table.read('../Data/' + galaxyname + '_data.fits') inneredge = t['Inner edge (kpc)'].data outeredge = t['Outer edge (kpc)'].data subplot_label = ('(a)', '(b)', '(c)', '(d)', '(e)', '(f)') for i in range(len(mass_equiv) - 1): binmass = mass_sorted[mass_equiv[i]:mass_equiv[i + 1]] myfit = powerlaw.Fit(binmass) R, p = myfit.distribution_compare('power_law', 'truncated_power_law') fig = myfit.truncated_power_law.plot_ccdf(label='Truncated\nPower Law') myfit.power_law.plot_ccdf(label='Power Law', ax=fig) myfit.plot_ccdf(drawstyle='steps', label='Data', ax=fig) # Format the plot. plt.legend(loc=0) #plt.title(galaxyname+'Equal-mass Mass Distribution, bin '+repr(i+1)) plt.ylim(ymin=10**-3) plt.xlabel(r'$M_\mathrm{\odot}$', fontsize=20) plt.ylabel('CCDF', fontsize=20) mpl.rc('xtick', labelsize=16) mpl.rc('ytick', labelsize=16) plt.text(0.01, 0.5, subplot_label[i], ha='left', va='center', transform=fig.transAxes, fontsize=16) plt.text( 0.35, 0.01, r'$M_{bin}\ =\ %e M_\mathrm{\odot}$' % (totmass) + '\n' + r'$R_{gal}\ =\ %5.4f\ \mathrm{kpc}\ \mathrm{to}\ %5.4f\ \mathrm{kpc}$' % (inneredge[i], outeredge[i]) + '\n' + r'$\mathrm{R}\ =\ %5.4f,\ \mathrm{p}\ =\ %5.4f$' % (R, p) + '\n' + r'$\alpha\ =\ %5.4f,\ M_\mathrm{0}\ =\ %5.4eM_\mathrm{\odot}$' % (-myfit.alpha, 1 / myfit.truncated_power_law.parameter2), ha='left', va='bottom', transform=fig.transAxes, fontsize=16) plt.savefig('../Data/' + galaxyname + '_power_law_equal_mass_' + repr(i + 1) + '.png') plt.close() # Plot the mass distribution trend for equal-area bins. for i in range(len(mass_area) - 1): f = 0 #loop flag if mass_area[i + 1] - mass_area[i] < 3: f = 1 if not f: binmass = mass_sorted[mass_area[i]:mass_area[i + 1]] myfit = powerlaw.Fit(binmass) R, p = myfit.distribution_compare('power_law', 'truncated_power_law') fig = myfit.truncated_power_law.plot_ccdf( label='Truncated\nPower Law') myfit.power_law.plot_ccdf(label='Power Law', ax=fig) myfit.plot_ccdf(drawstyle='steps', label='Data', ax=fig) # Format the plot. plt.legend(loc=0) #plt.title(galaxyname+'Equal-area Mass Distribution, bin '+repr(i+1)) plt.ylim(ymin=10**-3) plt.xlabel(r'$M_\mathrm{\odot}$', fontsize=20) plt.ylabel('CCDF', fontsize=20) mpl.rc('xtick', labelsize=16) mpl.rc('ytick', labelsize=16) plt.text(0.01, 0.5, subplot_label[i], ha='left', va='center', transform=fig.transAxes, fontsize=16) plt.text( 0.35, 0.01, r'$R_{gal}\ =\ %5.4f\ \mathrm{kpc}\ \mathrm{to}\ %5.4f\ \mathrm{kpc}$' % (rgal_equiv[i], rgal_equiv[i + 1]) + '\n' + r'$\mathrm{R}\ =\ %5.4f,\ \mathrm{p}\ =\ %5.4f$' % (R, p) + '\n' + r'$\alpha\ =\ %5.4f,\ M_\mathrm{0}\ =\ %5.4eM_\mathrm{\odot}$' % (-myfit.alpha, 1 / myfit.truncated_power_law.parameter2), ha='left', va='bottom', transform=fig.transAxes, fontsize=16) plt.savefig('../Data/' + galaxyname + '_power_law_equal_area_' + repr(i + 1) + '.png') plt.close() return [distance, inclination]
import argparse from astropy.io import fits import numpy as np import astropy.table # PARSE OPTIONS ############################################################### parser = argparse.ArgumentParser(description="An astropy snippet") parser.add_argument("filearg", nargs=1, metavar="FILE", help="the output FITS file") args = parser.parse_args() file_path = args.filearg[0] # WRITE DATA ################################################################## name_list = ("column1", "column2", "column3") dtype_list = ("S128", # column1 -> 128 bytes string "i4", # column2 -> 4 bytes integer "f8") # column3 -> 8 bytes float table = astropy.table.Table(names=name_list, dtype=dtype_list) table.add_row(["A", 1, 1.1]) table.add_row(["B", 2, 2.2]) table.add_row(["C", 3, 3.3]) print(table) table.write(file_path, overwrite=True)
import numpy as np import astropy.table # PARSE OPTIONS ############################################################### parser = argparse.ArgumentParser(description="An astropy snippet") parser.add_argument("filearg", nargs=1, metavar="FILE", help="the output FITS file") args = parser.parse_args() file_path = args.filearg[0] # WRITE DATA ################################################################## name_list = ("column1", "column2", "column3") dtype_list = ( "S128", # column1 -> 128 bytes string "i4", # column2 -> 4 bytes integer "f8") # column3 -> 8 bytes float table = astropy.table.Table(names=name_list, dtype=dtype_list) table.add_row(["A", 1, 1.1]) table.add_row(["B", 2, 2.2]) table.add_row(["C", 3, 3.3]) print(table) table.write(file_path, overwrite=True)