-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsdb_csv2xdr.pro
More file actions
162 lines (144 loc) · 4.96 KB
/
sdb_csv2xdr.pro
File metadata and controls
162 lines (144 loc) · 4.96 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
;+
;
; NAME:
; sdb_csv2xdr
;
; PURPOSE:
; Load a text/csv file of photometry and stellar parameters
;
; CATEGORY:
; sdb
;
; EXPLANATION:
; Load a csv file generated by sdb_getphot.py into a IDL save file. These are of
; the format sdbid-rawphot.txt, and the output will be sdbid.xdr
;
; SYNTAX:
; sdb_csv2xdr(file,xdr=xdr)
;
; INPUTS:
; file: csv file to load
;
; MODIFICATION HISTORY:
; created from sedfit_loadcsv early Sept 2016
;
;-
pro sdb_csv2xdr,file
;; do recursively if many files given
if N_ELEMENTS(file) gt 1 then begin
for i=0,N_ELEMENTS(file)-1 do sdb_csv2xdr,file[i]
return
endif
;; figure save file name and whether we need to update xdr file
base = STR_REPLACE(file,'-rawphot.txt','')
xdr = base+'.xdr'
if FILE_TEST(xdr) then begin
print,"File exists,"
print,xdr
print,"Checking if update needed"
t_txt = FILE_INFO(file)
t_xdr = FILE_INFO(xdr)
if t_xdr.mtime gt t_txt.mtime then begin
print,"xdr newer than txt, not overwriting "+xdr
return
endif else print,"Rawphot file more recent, updating xdr"
endif else print,"Creating ",xdr
;; get stuff in text file
nl = FILE_LINES(file[0])
OPENR,lun,file,/GET_LUN
ls = STRARR(nl)
READF,lun,ls
CLOSE,lun
FREE_LUN,lun
;; mark lines we want, format is IPAC so "\ bla" is comment, "\x=y" is keyword
info = INTARR(nl) ; 0 for photometry, 1 for stellar, 2 for nothing
for i=0,nl-1 do begin
if STREGEX(ls[i],'^\\[0-9a-zA-Z]',/BOOLEAN) then info[i] = 1 ; keywords
if STREGEX(ls[i],'^\\ |^\\$',/BOOL) then info[i] = -1 ; comments
if STREGEX(ls[i],'^\|',/BOOL) then info[i] = 2 ; header lines
; if STREGEX(ls[i],'^[\|]*[ \t]*#|^[ \t]*$',/BOOL) then info[i] = 2 ; comments and header lines
endfor
;; defaults
name = STRING('')
spty = STRING('')
teff = !VALUES.F_NAN
tefferr = !VALUES.F_NAN
logg = !VALUES.F_NAN
mh = !VALUES.F_NAN
av = !VALUES.F_NAN
dist = !VALUES.F_NAN
delvarx,irsfile
;; loop and get stuff
sys = -1
for i=0,nl-1 do begin
;; decide whether systematic errors were given (first header line only)
if info[i] eq 2 and sys lt 0 then begin
if STREGEX(ls[i],'sys',/FOLD_CASE,/BOOLEAN) then sys = 1 else sys = 0
CONTINUE ; comment lines
endif
if info[i] eq 1 then begin
ls[i] = STR_REPLACE(ls[i],'^\\','') ; remove leading \
if STREGEX(ls[i],'^id',/FOLD_CASE,/BOOLEAN) then begin
tmp = STRSPLIT(ls[i],'=',/EXTRACT)
name = tmp[1]
endif
if STREGEX(ls[i],'sp_type',/FOLD_CASE,/BOOLEAN) then begin
tmp = STRSPLIT(ls[i],'=',/EXTRACT)
spty = tmp[1]
endif
if STREGEX(ls[i],'plx_value',/FOLD_CASE,/BOOLEAN) then begin
tmp = STRSPLIT(ls[i],'=',/EXTRACT)
if tmp[1] eq 'None' then dist = !VALUES.F_NAN else dist = 1e3/FLOAT(tmp[1])
endif
if STREGEX(ls[i],'irsstare',/FOLD_CASE,/BOOLEAN) then begin
tmp = STRSPLIT(ls[i],'=',/EXTRACT)
push,irsfile,tmp[1]
endif
endif
if info[i] eq 0 then begin
; tmp = STRSPLIT(ls[i],'[ | ]*\|[ | ]*',/REGEX,/EXTRACT,COUNT=n)
tmp = STRSPLIT(ls[i],' ',/EXTRACT,COUNT=n)
push,band,tmp[0]
push,meas,FLOAT(tmp[1])
push,errs,FLOAT(tmp[2])
if KEYWORD_SET(sys) then begin
push,syserr,FLOAT(tmp[3])
push,lim,ROUND(FLOAT(tmp[4]))
push,unit,tmp[5]
push,ref,tmp[6]
;; if n gt 7 then push,note1,tmp[7] else push,note1,''
;; if n gt 8 then push,note2,tmp[8] else push,note2,''
;; if n gt 9 then push,id,tmp[9] else push,id,''
endif else begin
push,syserr,0.0
push,lim,ROUND(FLOAT(tmp[3]))
push,unit,tmp[4]
push,ref,tmp[5]
;; if n gt 6 then push,note1,tmp[6] else push,note1,''
;; if n gt 7 then push,note2,tmp[7] else push,note2,''
;; if n gt 8 then push,id,tmp[8] else push,id,''
endelse
endif
endfor
nphot = N_ELEMENTS(band)
if nphot eq 0 then begin
print,"No photometry in "+file+", so no xdr (may be spectra)"
return
endif
;; rawdata
rawdata = ls
flux = meas
err = errs
rawphot = {nphot:nphot,band:band,meas:meas,err:err,syserr:syserr,unit:unit,lim:lim,ref:ref,note1:STRARR(nphot),note2:STRARR(nphot),id:STRARR(nphot),irs:{ok:0,other:''}}
if N_ELEMENTS(xdr) eq 0 then STOP,'SEDFIT_LOADCSV: couldnt assign a name for the xdr file'
;; align and then add IRS spectra, just use first one here
if N_ELEMENTS(irsfile) ne 0 then begin
irsfile = irsfile[0]
base = base+'-irsstare'
align_cassis_spec,fits=irsfile,name=name,filebase=base
rawphot = sedfit_addirs(rawphot,irsfile=base+'.txt')
endif
stinf = {ntries:0,name:name,spty:spty,teff:teff,tefferr:tefferr,logg:logg,mh:mh,av:av,dist:dist}
;; save it
save,filename=xdr,rawdata,rawphot,stinf
end