-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxp_plotting.py
More file actions
211 lines (161 loc) · 7.23 KB
/
Copy pathxp_plotting.py
File metadata and controls
211 lines (161 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Created by Ben Kaiser (UNC-Chapel Hill) 2023-06-05.
This should handle the Gaia XP spectra in their weird Hermite polynomial coefficient form and
make it something somewhat palatable. Whether that is a sampled version or just at least some
means of plotting it in pyplot. Just something so I can at least look at the data.
It also seems decently likely that this will handle the retrieval of the spectra too since they
aren't included with the general retrieval by default and have to be retrieved via datalink.
"""
from __future__ import print_function
import matplotlib
#matplotlib.use('pdf')
import numpy as np
#from astroquery.gaia import Gaia
import astropy.units as u
import astropy.coordinates as coord
from astropy.table import Table, QTable
import matplotlib.pyplot as plt
import scipy.stats as scistats
#import seaborn as sns
#import astropy
import gaiaxpy as xpy
import pandas as pd
import sys
#import passband_model_convolution as pmc
#import gaia_extinction
#import wdatmos
import plotting_dicts as pod
#output_dir='GaiaDR3_XP_spectra/WDMS_wide_binaries/'
output_dir='GaiaDR3_XP_spectra/WDMS_wide_binaries_3300to7000A_truncfalse_PDFs/'
#input_file='20210201_DZs_for_J1636paper_gaia_gaiaDR3.csv'
#input_file='dimWDMS_allMS_minsepfunc_eDR3_highconf_notrust_justWD_gaiaDR3_d_sbf.csv'
input_file='SDSSJ1018p4439nearby_gaiaDR3.csv'
#input_file='WDJ1948m1011_gaiaDR3.csv'
#input_file='LTT3218_GaiaDR3.csv'
credentials_file= 'Gaia_credentials.txt'
#output_file='LHS2534_GaiaDR3_XPspectrum.csv'
output_file='wdbinary_GaiaDR3_XPspectrum.csv'
save_file=False
save_plots=False
single_index=3
credentials_frame=np.genfromtxt(credentials_file,dtype=str)
input_table=Table.read(input_file)
input_table.pprint()
has_xp=np.where((np.array(input_table['has_xp_continuous'])=='True' )|( np.array(input_table['has_xp_continuous'])=='true'))
sub_table=input_table[has_xp]
print('sub_table')
sub_table.pprint()
source_list=[]
name_list=[]
for row in sub_table:
try:
source=row['DESIGNATION'].split(' ')[-1]
except KeyError:
source=row['designation'].split(' ')[-1]
source_list.append(source)
name_list.append(row['name'])
#if len(sub_table)>1:
#for row in sub_table:
##source=row['DESIGNATION'].split(' ')[-1]
#source=row['designation'].split(' ')[-1]
#source_list.append(source)
#name_list.append(row['name'])
#else:
#source=sub_table['designation'][0].split(' ')[-1]
#source_list.append(source)
#name_list.append(sub_table['name'][0])
print(source_list)
print(credentials_frame.shape)
print('about to retrieve and calibrate spectra')
#calibrated_spectra, sampling=xpy.calibrate(source_list,username=credentials_frame[0],password=credentials_frame[1],truncation=True)
new_sampling=np.arange(330.,700.,2.)
calibrated_spectra, sampling=xpy.calibrate(source_list,username=credentials_frame[0],password=credentials_frame[1],truncation=False,sampling=new_sampling)
print('spectra retrieved and calibrated')
print(calibrated_spectra)
print('sampling',sampling)
#sampling=new_sampling
conversion_factor=1e18 #multiple by which you have to multiply the flux in W/m^2/nm/s to get to erg/cm^2/Anstrom/s *10^-16 as used in my Goodman spectra
#for row, name in zip(calibrated_spectra,name_list):
#row['source_id']=name
#calibrated_spectra['source_id']=name_list
#xpy.plot_spectra(calibrated_spectra,sampling=sampling,multi=True) #this plots all of the spectra at one time.
##print(calibrated_spectra['flux'].units)
##plt.plot(sampling,calibrated_spectra)
#calibrated_spectra['flux']=calibrated_spectra['flux']*conversion_factor
#calibrated_spectra['flux_error']=calibrated_spectra['flux_error']*conversion_factor
#sampling=sampling*10.
#xpy.plot_spectra(calibrated_spectra,sampling=sampling,multi=True) #this plots all of the spectra at one time.
#print(sampling.shape)
#print(calibrated_spectra['flux'].shape)
#plt.xlabel('Wavelength (Angstroms')
#plt.ylabel('Flux (cgs units)')
#plt.show()
#sys.exit()
print('type(calibrated_spectra)',type(calibrated_spectra))
for index,calibrated_spectrum in calibrated_spectra.iterrows():
print('calibrated_spectrum', calibrated_spectrum)
ra_dec=coord.SkyCoord(ra=sub_table[index]['ra'], dec=sub_table[index]['dec'], unit=(u.deg,u.deg),frame='icrs')
ra_dec_string=ra_dec.to_string(style='hmsdms')
full_radec=ra_dec.to_string(style='hmsdms')
replace_chars=['h','m','d']
for char in replace_chars:
full_radec=full_radec.replace(char,':')
full_radec=full_radec.replace('s','')
ra_dec_list=full_radec.split(' ')
ra_string=ra_dec_list[0][:5]
dec_string=ra_dec_list[1][:6]
ra_string=ra_string.replace(':','')
dec_string=dec_string.replace(':','')
#ra_string.replace('s','')
#dec_string.replace('s','')
#dec_string=dec_string.replace('-','m')
#dec_string=dec_string.replace('+','p')
#plt.title(name_list[index]+' '+calibrated_spectra['source_id']+', '+ra_dec.to_string(style='hmsdms')+', ' +'G='+str(sub_table[index]['phot_g_mean_mag'])[:5])
if save_plots:
plt.figure(figsize=(1036./540*6.,6.))
else:
pass
#plt.title('index:'+str(index)+', '+name_list[index]+' '+str(sub_table[index]['source_id'])+', '+ra_dec.to_string(style='hmsdms')+', ' +'G='+str(sub_table[index]['phot_g_mean_mag'])[:5])
plt.title('index:'+str(index)+', '+'J'+ra_string+dec_string+', '+str(sub_table[index]['source_id'])+', '+full_radec+', ' +'G='+str(sub_table[index]['phot_g_mean_mag'])[:5])
#plt.title(calibrated_spectrum['source_id'])
plt.plot(sampling, calibrated_spectra['flux'][index])
plt.xlabel('Wavelength (Angstroms)')
plt.ylabel('Flux (cgs units)')
#if sub_table[index]['phot_g_mean_mag']<18.:
#plt.plot(sampling, calibrated_spectra['flux'][index])
#plt.xlabel('Wavelength (Angstroms')
#plt.ylabel('Flux (cgs units)')
#plt.errorbar(sampling, calibrated_spectra['flux'][index],yerr=calibrated_spectra['flux_error'][index])
#xpy.plot_spectra(calibrated_spectrum,sampling=sampling,multi=False)
#plt.show()
if save_plots:
print('saving plot')
for char in replace_chars:
ra_dec_string=ra_dec_string.replace(char,'')
ra_dec_list=ra_dec_string.split(' ')
ra_string=ra_dec_list[0][:4]
dec_string=ra_dec_list[1][:5]
ra_string.replace('s','')
dec_string.replace('s','')
dec_string=dec_string.replace('-','m')
dec_string=dec_string.replace('+','p')
plt.savefig(output_dir+'J'+ra_string+dec_string+'_spec'+str(index)+'_wide_binary_truncfalse_DR3XP.pdf')
plt.clf()
else:
print('showing plot')
plt.show()
print(calibrated_spectra['flux'][0])
single_spec_flux=calibrated_spectra['flux'][single_index]
single_spec_error=calibrated_spectra['flux_error'][single_index]*conversion_factor
#single_spec_flux=single_spec_flux.to(u.erg/(u.cm**2)/u.angstrom/u.s)
#single_spec_flux=single_spec_flux.cgs
print(single_spec_flux)
print(calibrated_spectra['source_id'][single_index])
wavelengths=sampling*10. #default sampling from Gaia is in nm and I want Angstroms.
output_spec=np.vstack([wavelengths,single_spec_flux,single_spec_error])
if save_file:
print('saving output spectrum',output_file)
np.savetxt(output_dir+output_file,output_spec)
print('file saved.')
else:
pass