1 |
FUNCTION FUSK, icasea=icasea |
2 |
|
3 |
if (n_elements(icasea) eq 0) then icasea=2 |
4 |
|
5 |
idldir='$JSOCROOT/proj/globalhs/scripts/idl/' |
6 |
|
7 |
if (icasea le 2) then begin |
8 |
iasym=0 |
9 |
nm=24 |
10 |
paroff=7 |
11 |
sigoff=5 |
12 |
aoff=12 |
13 |
endif else begin |
14 |
iasym=1 |
15 |
nm=26 |
16 |
paroff=8 |
17 |
sigoff=6 |
18 |
aoff=14 |
19 |
end |
20 |
|
21 |
infile='xxx' |
22 |
nin=0l |
23 |
outfile='xxx' |
24 |
read,infile,prompt='Input file: ' |
25 |
;read,nin,prompt='#input: ' |
26 |
spawn,'wc -l '+infile,help |
27 |
reads,help,nin |
28 |
read,outfile,prompt='Output file: ' |
29 |
lmax=310 |
30 |
qj=dblarr(4,5396) |
31 |
openr,1,idldir+'jjj2' |
32 |
readf,1,qj |
33 |
close,1 |
34 |
wj=where(qj(0,*) le lmax,cj) |
35 |
qj=qj(*,wj) |
36 |
lj=reform(qj(0,*)) |
37 |
nj=reform(qj(1,*)) |
38 |
fj=reform(qj(2,*)) |
39 |
massj=reform(qj(3,*)) |
40 |
massa=dblarr(lmax+1,max(nj)+1) |
41 |
massa(lj,nj)=massj |
42 |
fa=dblarr(lmax+1,max(nj)+1) |
43 |
fa(lj,nj)=fj |
44 |
w0=where(lj eq 0) |
45 |
f0=reform(qj(2,w0)) |
46 |
mass0=reform(qj(3,w0)) |
47 |
; Set output arrays |
48 |
fjx=dblarr(cj) |
49 |
aj=dblarr(6,cj) |
50 |
ampj=dblarr(cj) |
51 |
widthj=dblarr(cj) |
52 |
backj=dblarr(cj) |
53 |
; |
54 |
; Set target modes |
55 |
;cout=2173 |
56 |
;qout=dblarr(4,cout) |
57 |
;openr,1,'freq2a' |
58 |
;readf,1,qout |
59 |
;close,1 |
60 |
; |
61 |
; Get input modes |
62 |
cin=nin |
63 |
qin=dblarr(nm,cin) |
64 |
openr,1,infile |
65 |
readf,1,qin |
66 |
close,1 |
67 |
; |
68 |
; Use all of Joergens modes |
69 |
qout=qj |
70 |
; First do the p-modes |
71 |
wp=where(qout(1,*) ne 0,cout) |
72 |
lout=reform(qout(0,wp)) |
73 |
nout=reform(qout(1,wp)) |
74 |
fout=fa(lout,nout) |
75 |
dfout=reform(qout(2,wp))-fout |
76 |
awxout=alog10(fout/(lout+0.5)) |
77 |
massout=massa(lout,nout) |
78 |
; Find the p-modes in the input |
79 |
w=where(qin(1,*) ne 0,np) |
80 |
l=reform(qin(0,w)) |
81 |
n=reform(qin(1,w)) |
82 |
f=fa(l,n) |
83 |
sig=reform(qin(2+sigoff,w)) |
84 |
df=reform(qin(2,w))-f |
85 |
mass=massa(l,n) |
86 |
awx=alog10(f/(l+0.5)) |
87 |
mx=exp(interpol(alog(mass0),f0,f)) |
88 |
mxout=exp(interpol(alog(mass0),f0,fout)) |
89 |
qnl=mass/mx |
90 |
qnlout=massout/mxout |
91 |
dff=df/f |
92 |
sigf=sig/f |
93 |
dfx=qnl*df/f |
94 |
sigx=qnl*sig/f |
95 |
|
96 |
; Fit model containing: |
97 |
; A piecewise linear function in frequency with nf terms |
98 |
; A piecewise linear function in log(f/L) |
99 |
; A constant for each of n=1,...,nn0 |
100 |
; A separate linear term in l for n=1,...,nn1 |
101 |
muf=1e-3 |
102 |
mua=1d-3 |
103 |
nf0=30 |
104 |
na0=60 |
105 |
nn0=4 |
106 |
nn1=4 |
107 |
chi=dblarr(21) |
108 |
;for ii=0,20 do begin |
109 |
;nf0=2+2*ii |
110 |
;na=round(2.0^(1.0+ia/2.0)) |
111 |
nf=nf0+2 |
112 |
na=na0+2 |
113 |
nn=nn0+nn1 |
114 |
npar=nf+na+nn |
115 |
sigmin=0 |
116 |
a=dblarr(np,npar) |
117 |
aout=dblarr(cout,npar) |
118 |
if (nf ne 0) then begin |
119 |
sf=sort(f) |
120 |
qf=dblarr(np) |
121 |
qf(0)=1./(sigx(sf(0))>sigmin)^2 |
122 |
for i=1,np-1 do qf(i)=qf(i-1)+1./(sigx(sf(i))>sigmin)^2 |
123 |
; fi=interpol(f(sf),qf,min(qf)+(max(qf)-min(qf))*dindgen(nf0)/(nf0-1)) |
124 |
fi=min(f)+(max(f)-min(f))*dindgen(nf0)/(nf0-1) |
125 |
; for i=0,nfx-1 do begin |
126 |
; dfi=fi(1:nf0+i-1)-fi(0:nf0+i-2) |
127 |
; sx=sort(-dfi) |
128 |
; ix=sx(0) |
129 |
; fix=(fi(ix+1)+fi(ix))/2 |
130 |
; fi=[fi,fix] |
131 |
; fi=fi(sort(fi)) |
132 |
; end |
133 |
; fi=[min(f)-1000,fi] |
134 |
; fi=[fi,max(f)+1000] |
135 |
fi=[2*fi(0)-fi(1),fi,2*fi(nf0-1)-fi(nf0-2)] |
136 |
for i=0,nf-1 do begin |
137 |
fmin=min(f)-1000 |
138 |
if (i gt 0) then fmin=fi(i-1) |
139 |
fmax=max(f)+1000 |
140 |
if (i lt (nf-1)) then fmax=fi(i+1) |
141 |
xi=dblarr(nf) |
142 |
xi(i)=1.0 |
143 |
w=where((f ge fmin-1d-10) and (f le fmax+1d-10),nw) |
144 |
; a(*,i)=interpol(xi,fi,f) |
145 |
; dfi=(max(f)-min(f))/(nf0-1) |
146 |
; a(*,i)=interpolate(xi,(f-min(fi))/dfi,/cubic) |
147 |
if (nw ne 0) then a(w,i)=interpol(xi,fi,f(w)) |
148 |
w=where((fout ge fmin-1d-10) and (fout le fmax+1d-10)) |
149 |
aout(w,i)=interpol(xi,fi,fout(w)) |
150 |
end |
151 |
end ; if (nf ne 0) |
152 |
|
153 |
if (na ne 0) then begin |
154 |
sa=sort(awx) |
155 |
qa=dblarr(np) |
156 |
qa(0)=1./(sigx(sa(0))>sigmin)^2 |
157 |
for i=1,np-1 do qa(i)=qa(i-1)+1./(sigx(sa(i))>sigmin)^2 |
158 |
; ai=interpol(awx(sa),qa,min(qa)+(max(qa)-min(qa))*dindgen(na0)/(na0-1)) |
159 |
ai=min(awx)+(max(awx)-min(awx))*dindgen(na0)/(na0-1) |
160 |
; for i=0,nax-1 do begin |
161 |
; dai=ai(1:na0+i-1)-ai(0:na0+i-2) |
162 |
; sx=sort(-dai) |
163 |
; ix=sx(0) |
164 |
; aix=(ai(ix+1)+ai(ix))/2 |
165 |
; ai=[ai,aix] |
166 |
; ai=ai(sort(ai)) |
167 |
; end |
168 |
ai=[min(awx)-1,ai] |
169 |
ai=[ai,max(awx)+1] |
170 |
for i=0,na-1 do begin |
171 |
amin=min(awx)-1 |
172 |
if (i gt 0) then amin=ai(i-1) |
173 |
amax=max(awx)+1 |
174 |
if (i lt (na-1)) then amax=ai(i+1) |
175 |
xi=dblarr(na) |
176 |
xi(i)=1.0 |
177 |
w=where((awx ge amin) and (awx le amax),nw) |
178 |
; a(*,i+nf)=interpol(xi,ai,awx) |
179 |
if (nw ne 0) then a(w,i+nf)=interpol(xi,ai,awx(w)) |
180 |
w=where((awxout ge amin) and (awxout le amax),nw) |
181 |
if (nw ne 0) then aout(w,i+nf)=interpol(xi,ai,awxout(w)) |
182 |
end |
183 |
end |
184 |
|
185 |
; Constant for each n. Improves fit significantly: |
186 |
for in=1,nn0 do begin |
187 |
w=where(n eq in,nw) |
188 |
if (nw ge 1) then a(w,na+nf+in-1)=1 ; Need at least one point for constant |
189 |
end |
190 |
for in=1,nn0 do aout(where(nout eq in),na+nf+in-1)=1 |
191 |
|
192 |
; Something linear in l for each n |
193 |
for in=1,nn1 do begin |
194 |
w=where(n eq in,nw) |
195 |
if (nw ge 2) then begin ; Need at least two points to fit linear. |
196 |
lav=total(rebin(l(w),1)) ; Average l |
197 |
a(w,na+nf+nn0+in-1)=l(w)-lav |
198 |
; a(w,na+nf+nn0+in-1)=1./l(w)^2-1./lav^2 |
199 |
endif else begin |
200 |
lav=0l |
201 |
end |
202 |
w=where(nout eq in) |
203 |
aout(w,na+nf+nn0+in-1)=lout(w)-lav |
204 |
; aout(w,na+nf+nn0+in-1)=1./lout(w)^2-1./lav^2 |
205 |
end |
206 |
|
207 |
;for i=0,npar-1 do a(*,i)=a(*,i)/sqrt(total(a(*,i)^2)) |
208 |
ap=a/rebin(reform(sigx,np,1),np,npar) |
209 |
y=dfx |
210 |
yp=y/sigx |
211 |
b=transpose(ap)#ap |
212 |
; Add first derivative regularization to nu term |
213 |
bfmax=0.0 |
214 |
for i=0,nf-1 do bfmax=bfmax>b(i,i) |
215 |
b(0,0)=b(0,0)+muf*bfmax |
216 |
b(nf-1,nf-1)=b(nf-1,nf-1)+muf*bfmax |
217 |
for i=1,nf-2 do b(i,i)=b(i,i)+2*muf*bfmax |
218 |
for i=0,nf-2 do b(i,i+1)=b(i,i+1)-muf*bfmax |
219 |
for i=0,nf-2 do b(i+1,i)=b(i+1,i)-muf*bfmax |
220 |
; Force first frequency correction to 0 to remove degeneracy |
221 |
;b(0,0)=1.001*b(0,0) |
222 |
b(0,0)=b(0,0)+muf*bfmax |
223 |
; Add first derivative regularization to nu/L term |
224 |
bamax=0.0 |
225 |
for i=nf,nf+na-1 do bamax=bamax>b(i,i) |
226 |
b(nf,nf)=b(nf,nf)+mua*bamax |
227 |
b(nf+na-1,nf+na-1)=b(nf+na-1,nf+na-1)+mua*bamax |
228 |
for i=nf+1,nf+na-2 do b(i,i)=b(i,i)+2*mua*bamax |
229 |
for i=nf,nf+na-2 do b(i,i+1)=b(i,i+1)-mua*bamax |
230 |
for i=nf,nf+na-2 do b(i+1,i)=b(i+1,i)-mua*bamax |
231 |
;b(nf,nf)=b(nf,nf)+mua*bamax |
232 |
|
233 |
; Take care of case where low ns are missing. |
234 |
; Setting diagonal to 1 makes added term zero |
235 |
for in=1,nn0 do begin |
236 |
w=where(n eq in,nw) |
237 |
if (nw lt 1) then b(na+nf+in-1,na+nf+in-1)=1 |
238 |
end |
239 |
for in=1,nn1 do begin |
240 |
w=where(n eq in,nw) |
241 |
if (nw lt 2) then b(na+nf+nn0+in-1,na+nf+nn0+in-1)=1 |
242 |
end |
243 |
|
244 |
ib=invert(b) |
245 |
c=transpose(ap)#yp |
246 |
x=ib#c |
247 |
; Extrapolate the correction functions |
248 |
x(0)=x(1)-(fi(1)-fi(0))/(fi(2)-fi(1))*(x(2)-x(1)) |
249 |
x(nf-1)=x(nf-2)-(fi(nf-2)-fi(nf-1))/(fi(nf-3)-fi(nf-2))*(x(nf-3)-x(nf-2)) |
250 |
x(nf+0)=x(nf+1)-(ai(1)-ai(0))/(ai(2)-ai(1))*(x(nf+2)-x(nf+1)) |
251 |
x(nf+na-1)=x(nf+na-2)-(ai(na-2)-ai(na-1))/(ai(na-3)-ai(na-2))*(x(nf+na-3)-x(nf+na-2)) |
252 |
modelx=a#x |
253 |
modelf=modelx/qnl |
254 |
model=modelf*f |
255 |
moutx=aout#x |
256 |
moutf=moutx/qnlout |
257 |
mout=moutf*fout |
258 |
fjx(wp)=fj(wp)+mout |
259 |
print,nf0,nf,na0,na,nn0,nn1,total(rebin((dfx/sigx)^2,1)),total(rebin(((dfx-modelx)/sigx)^2,1)),total(rebin(((dfx-modelx)/sigx)^2,1))/total(rebin((dfx/sigx)^2,1)) |
260 |
;chi(ii)=total(rebin(((dfx-modelx)/sigx)^2,1)) |
261 |
;plot,f,(dfx-modelx)/sigx,psym=1,yrange=[-50,50] |
262 |
;end |
263 |
|
264 |
; Now do the f modes |
265 |
wf=where(qout(1,*) eq 0,cout) |
266 |
lout=reform(qout(0,wf)) |
267 |
nout=reform(qout(1,wf)) |
268 |
fout=fa(lout,nout) |
269 |
dfout=reform(qout(2,wf))-fout |
270 |
awxout=alog10(fout/(lout+0.5)) |
271 |
massout=massa(lout,nout) |
272 |
; |
273 |
; Find the f-modes in the input |
274 |
w=where(qin(1,*) eq 0,nfmode) |
275 |
if (nfmode ge 1) then begin |
276 |
l=reform(qin(0,w)) |
277 |
n=reform(qin(1,w)) |
278 |
f=fa(l,n) |
279 |
sig=reform(qin(2+sigoff,w)) |
280 |
df=reform(qin(2,w))-f |
281 |
dfx=df/f |
282 |
sigx=sig/f |
283 |
; |
284 |
npar0=10 |
285 |
chi=dblarr(25) |
286 |
;or npar0=2,20 do begin |
287 |
npar=npar0+1 |
288 |
a=dblarr(nfmode,npar) |
289 |
li=min(l)+(max(l)-min(l))*dindgen(npar0)/(npar0-1) |
290 |
li=[2*li(0)-li(1),li] |
291 |
;li=[2*li(0)-li(1),li,2*li(npar-1)-li(npar-2)] |
292 |
for i=0,npar-1 do begin |
293 |
qmin=min(l)-1000 |
294 |
if (i gt 0) then qmin=li(i-1) |
295 |
qmax=max(l)+1000 |
296 |
if (i lt (npar-1)) then qmax=li(i+1) |
297 |
xi=dblarr(npar) |
298 |
xi(i)=1.0 |
299 |
w=where((l ge qmin) and (l le qmax),nw) |
300 |
if (nw ne 0) then a(w,i)=interpol(xi,li,l(w)) |
301 |
end |
302 |
ap=a/rebin(reform(sigx,nfmode,1),nfmode,npar) |
303 |
y=dfx |
304 |
yp=y/sigx |
305 |
b=transpose(ap)#ap |
306 |
|
307 |
; Add first derivative regularization to nu term |
308 |
bfmax=0.0 |
309 |
for i=0,npar-1 do bfmax=bfmax>b(i,i) |
310 |
b(0,0)=b(0,0)+muf*bfmax |
311 |
b(npar-1,npar-1)=b(npar-1,npar-1)+muf*bfmax |
312 |
for i=1,npar-2 do b(i,i)=b(i,i)+2*muf*bfmax |
313 |
for i=0,npar-2 do b(i,i+1)=b(i,i+1)-muf*bfmax |
314 |
for i=0,npar-2 do b(i+1,i)=b(i+1,i)-muf*bfmax |
315 |
|
316 |
ib=invert(b) |
317 |
c=transpose(ap)#yp |
318 |
x=ib#c |
319 |
; Extrapolate the correction functions |
320 |
;x(0)=x(1)-(fi(1)-fi(0))/(fi(2)-fi(1))*(x(2)-x(1)) |
321 |
;x(npar-1)=x(npar-2)-(fi(npar-2)-fi(npar-1))/(fi(npar-3)-fi(npar-2))*(x(npar-3)-x(npar-2)) |
322 |
modelx=a#x |
323 |
model=model*f |
324 |
moutx=interpol(x,li,lout) |
325 |
mout=moutx*fout |
326 |
print,npar,total(rebin((dfx/sigx)^2,1)),total(rebin(((dfx-modelx)/sigx)^2,1)),total(rebin(((dfx-modelx)/sigx)^2,1))/total(rebin((dfx/sigx)^2,1)) |
327 |
chi(npar)=total(rebin(((dfx-modelx)/sigx)^2,1)) |
328 |
endif else begin |
329 |
mout=0 |
330 |
end |
331 |
fjx(wf)=fj(wf)+mout |
332 |
;end |
333 |
; Finally done with the frequencies |
334 |
|
335 |
; Now it is time for a-coeff |
336 |
; First get base values from model |
337 |
lout=reform(qout(0,*)) |
338 |
nout=reform(qout(1,*)) |
339 |
fout=fa(lout,nout) |
340 |
awxout=alog10(fout/(lout+0.5)) |
341 |
cout=cj |
342 |
qa=dblarr(6,4627) |
343 |
openr,1,idldir+'1d.split' |
344 |
readf,1,qa |
345 |
close,1 |
346 |
aa=dblarr(3,lmax+1,max(nj)+1)-1000 |
347 |
for i=0,4626 do aa(*,qa(0,i),qa(1,i))=qa(3:5,i) |
348 |
; Extend to lmax for those ridges for which there are values for l>=280 |
349 |
for in=0,max(nout) do begin |
350 |
w1=where(aa(0,280:lmax,in) ne -1000,n1)+280 |
351 |
w2=where(aa(0,280:lmax,in) eq -1000)+280 |
352 |
if (n1 gt 0) then for i=0,2 do aa(i,w2,in)=poly(w2,poly_fit(w1,aa(i,w1,in),1)) |
353 |
end |
354 |
; Get rid of the -1000's |
355 |
aa(where(aa eq -1000))=0 |
356 |
aj0=dblarr(6,cj) |
357 |
for i=0,cout-1 do aj0([0,2,4],i)=aa(*,lout(i),nout(i)) |
358 |
; Get input a-coeff |
359 |
ain=qin(aoff:aoff+5,*) |
360 |
sain=qin(aoff+6:aoff+11,*) |
361 |
; Get base values for input modes |
362 |
l=qin(0,*) |
363 |
n=qin(1,*) |
364 |
f=fa(l,n) |
365 |
awx=alog10(f/(l+0.5)) |
366 |
ajin=dblarr(6,nin) |
367 |
for i=0,nin-1 do ajin([0,2,4],i)=aa(*,l(i),n(i)) |
368 |
dain=ain-ajin |
369 |
|
370 |
; Do the odd a-coeff |
371 |
for i=0,4,2 do begin |
372 |
; w=where(sain(i,*) ne 0) |
373 |
; Now explicitly exclude nonsensical a-coeff. I.e. a_n for n>2l |
374 |
w=where((sain(i,*) ne 0) and ((2*l) ge (i+1))) |
375 |
di=dain(i,w) |
376 |
si=sain(i,w) |
377 |
p=polyfitw(awx(w),di,1/si,2) |
378 |
chi0=total(sqrt(rebin((di/si)^2,1))) |
379 |
chi1=total(sqrt(rebin(((di-poly(awx(w),p))/si)^2,1))) |
380 |
print,i,chi0,chi1 |
381 |
aj(i,*)=aj0(i,*)+poly(awxout,p) |
382 |
; Zero nonsensical a-coeff. I.e. a_n for n>2l |
383 |
w=where((2*lout) lt (i+1),nw) |
384 |
if (nw gt 0) then aj(i,w)=0 |
385 |
end |
386 |
; Now do the even a |
387 |
mass=massa(l,n) |
388 |
mx=exp(interpol(alog(mass0),f0,f)) |
389 |
qnl=mass/mx |
390 |
massout=massa(lout,nout) |
391 |
mxout=exp(interpol(alog(mass0),f0,fout)) |
392 |
qnlout=massout/mxout |
393 |
for i=1,5,2 do begin |
394 |
; w=where(sain(i,*) ne 0) |
395 |
; Now explicitly exclude nonsensical a-coeff. I.e. a_n for n>2l |
396 |
w=where((sain(i,*) ne 0) and ((2*l) ge (i+1))) |
397 |
di=dain(i,w) |
398 |
fi=f(w) |
399 |
qi=qnl(w) |
400 |
si=sain(i,w) |
401 |
p=polyfitw(fi,di/qi,qi/si,3) |
402 |
chi0=total(sqrt(rebin((di/si)^2,1))) |
403 |
chi1=total(sqrt(rebin(((di-qi*poly(fi,p))/si)^2,1))) |
404 |
print,i,chi0,chi1 |
405 |
aj(i,*)=aj0(i,*)+qnlout*poly(fout,p) |
406 |
; Zero nonsensical a-coeff. I.e. a_n for n>2l |
407 |
w=where((2*lout) lt (i+1),nw) |
408 |
if (nw gt 0) then aj(i,w)=0 |
409 |
end |
410 |
; Get rid of any outliers |
411 |
aj=(aj>(-9000))<9000 |
412 |
; |
413 |
; Amplitudes |
414 |
w=where((qin(0,*) ge 10) and (qin(0,*) le 50),count) |
415 |
l=reform(qin(0,w)) |
416 |
n=reform(qin(1,w)) |
417 |
f=fa(l,n) |
418 |
loga=reform(alog(qin(3,w))) |
419 |
siga=reform(qin(3+sigoff,w)/qin(3,w)) |
420 |
; |
421 |
mua=1e-5 |
422 |
npar0=15 |
423 |
chi=dblarr(25) |
424 |
;for npar0=2,20 do begin |
425 |
npar=npar0+2 |
426 |
a=dblarr(count,npar) |
427 |
fi=min(f)+(max(f)-min(f))*dindgen(npar0)/(npar0-1) |
428 |
fi=[2*fi(0)-fi(1),fi,2*fi(npar0-1)-fi(npar0-2)] |
429 |
for i=0,npar-1 do begin |
430 |
qmin=min(f)-1000 |
431 |
if (i gt 0) then qmin=fi(i-1) |
432 |
qmax=max(f)+1000 |
433 |
if (i lt (npar-1)) then qmax=fi(i+1) |
434 |
xi=dblarr(npar) |
435 |
xi(i)=1.0 |
436 |
w=where((f ge qmin) and (f le qmax),cw) |
437 |
if (cw ne 0) then a(w,i)=interpol(xi,fi,f(w)) |
438 |
end |
439 |
ap=a/rebin(reform(siga,count,1),count,npar) |
440 |
y=loga |
441 |
yp=y/siga |
442 |
b=transpose(ap)#ap |
443 |
; Add first derivative regularization to nu term |
444 |
bfmax=0.0 |
445 |
for i=0,npar-1 do bfmax=bfmax>b(i,i) |
446 |
b(0,0)=b(0,0)+mua*bfmax |
447 |
b(npar-1,npar-1)=b(npar-1,npar-1)+mua*bfmax |
448 |
for i=1,npar-2 do b(i,i)=b(i,i)+2*mua*bfmax |
449 |
for i=0,npar-2 do b(i,i+1)=b(i,i+1)-mua*bfmax |
450 |
for i=0,npar-2 do b(i+1,i)=b(i+1,i)-mua*bfmax |
451 |
ib=invert(b) |
452 |
c=transpose(ap)#yp |
453 |
x=ib#c |
454 |
; Extrapolate the correction functions |
455 |
x(0)=x(1)-(fi(1)-fi(0))/(fi(2)-fi(1))*(x(2)-x(1)) |
456 |
x(npar-1)=x(npar-2)-(fi(npar-2)-fi(npar-1))/(fi(npar-3)-fi(npar-2))*(x(npar-3)-x(npar-2)) |
457 |
modelx=a#x |
458 |
model=exp(modelx) |
459 |
print,npar,total(rebin((loga/siga)^2,1)),total(rebin(((loga-modelx)/siga)^2,1)),total(rebin(((loga-modelx)/siga)^2,1))/total(rebin((loga/siga)^2,1)) |
460 |
chi(npar)=total(rebin(((loga-modelx)/siga)^2,1)) |
461 |
;end |
462 |
l=reform(qin(0,*)) |
463 |
n=reform(qin(1,*)) |
464 |
f=fa(l,n) |
465 |
loga=reform(alog(qin(3,*))) |
466 |
siga=reform(qin(3+sigoff,*)/qin(3,*)) |
467 |
logai=interpol(x,fi,f) |
468 |
da=loga-logai |
469 |
dax=poly(l,poly_fit(l,da,3)) |
470 |
logaout=interpol(x,fi,fout) |
471 |
for in=min(n),max(n) do begin |
472 |
w=where(n eq in,c) |
473 |
; Get rid of the worst outliers |
474 |
w=where((n eq in) and (abs(da-dax) le 0.25),c) |
475 |
if (c ne 0) then begin |
476 |
w1=where(nout eq in,c1) |
477 |
if (c ge 10) then begin |
478 |
npol=2 |
479 |
; if (in eq 0) then npol=4 |
480 |
lav=total(rebin(l(w),1)) |
481 |
logaout(w1)=logaout(w1)+poly(lout(w1)-lav,poly_fit(l(w)-lav,da(w),npol)) |
482 |
endif else begin |
483 |
logaout(w1)=logaout(w1)+total(rebin(da(w),1)) |
484 |
end |
485 |
end |
486 |
end |
487 |
ampj=exp(logaout) |
488 |
|
489 |
; |
490 |
; Linewidths |
491 |
w=where((qin(0,*) ge 10) and (qin(0,*) le 50),count) |
492 |
l=reform(qin(0,w)) |
493 |
n=reform(qin(1,w)) |
494 |
f=fa(l,n) |
495 |
logw=reform(alog(qin(4,w))) |
496 |
sigw=reform(qin(4+sigoff,w)/qin(4,w)) |
497 |
; |
498 |
muw=1e-5 |
499 |
npar0=15 |
500 |
chi=dblarr(25) |
501 |
;for npar0=2,20 do begin |
502 |
npar=npar0+2 |
503 |
a=dblarr(count,npar) |
504 |
fi=min(f)+(max(f)-min(f))*dindgen(npar0)/(npar0-1) |
505 |
fi=[2*fi(0)-fi(1),fi,2*fi(npar0-1)-fi(npar0-2)] |
506 |
for i=0,npar-1 do begin |
507 |
qmin=min(f)-1000 |
508 |
if (i gt 0) then qmin=fi(i-1) |
509 |
qmax=max(f)+1000 |
510 |
if (i lt (npar-1)) then qmax=fi(i+1) |
511 |
xi=dblarr(npar) |
512 |
xi(i)=1.0 |
513 |
w=where((f ge qmin) and (f le qmax),cw) |
514 |
if (cw ne 0) then a(w,i)=interpol(xi,fi,f(w)) |
515 |
end |
516 |
ap=a/rebin(reform(sigw,count,1),count,npar) |
517 |
y=logw |
518 |
yp=y/sigw |
519 |
b=transpose(ap)#ap |
520 |
; Add first derivative regularization to nu term |
521 |
bfmax=0.0 |
522 |
for i=0,npar-1 do bfmax=bfmax>b(i,i) |
523 |
b(0,0)=b(0,0)+muw*bfmax |
524 |
b(npar-1,npar-1)=b(npar-1,npar-1)+muw*bfmax |
525 |
for i=1,npar-2 do b(i,i)=b(i,i)+2*muw*bfmax |
526 |
for i=0,npar-2 do b(i,i+1)=b(i,i+1)-muw*bfmax |
527 |
for i=0,npar-2 do b(i+1,i)=b(i+1,i)-muw*bfmax |
528 |
ib=invert(b) |
529 |
c=transpose(ap)#yp |
530 |
x=ib#c |
531 |
; Extrapolate the correction functions |
532 |
x(0)=x(1)-(fi(1)-fi(0))/(fi(2)-fi(1))*(x(2)-x(1)) |
533 |
x(npar-1)=x(npar-2)-(fi(npar-2)-fi(npar-1))/(fi(npar-3)-fi(npar-2))*(x(npar-3)-x(npar-2)) |
534 |
modelx=a#x |
535 |
model=exp(modelx) |
536 |
print,npar,total(rebin((logw/sigw)^2,1)),total(rebin(((logw-modelx)/sigw)^2,1)),total(rebin(((logw-modelx)/sigw)^2,1))/total(rebin((logw/sigw)^2,1)) |
537 |
chi(npar)=total(rebin(((logw-modelx)/sigw)^2,1)) |
538 |
;end |
539 |
l=reform(qin(0,*)) |
540 |
n=reform(qin(1,*)) |
541 |
f=fa(l,n) |
542 |
logw=reform(alog(qin(4,*))) |
543 |
sigw=reform(qin(4+sigoff,*)/qin(4,*)) |
544 |
lav0=total(rebin(l,1)) |
545 |
logwi=interpol(x,fi,f) |
546 |
dw=logw-logwi |
547 |
dwx=poly(l,poly_fit(l,dw,3)) |
548 |
logwout=interpol(x,fi,fout) |
549 |
for in=min(n),max(n) do begin |
550 |
w=where(n eq in,c) |
551 |
; Get rid of the worst outliers |
552 |
w=where((n eq in) and (abs(dw-dwx) le 0.5),c) |
553 |
if (c ne 0) then begin |
554 |
w1=where(nout eq in) |
555 |
if (c ge 10) then begin |
556 |
lav=total(rebin(l(w),1)) |
557 |
wcor=poly(lout(w1)-lav,poly_fit(l(w)-lav,dw(w),2)) |
558 |
i1=where(lout(w1) ge max(l(w))) |
559 |
wcor(i1)=wcor(min(i1)) |
560 |
logwout(w1)=logwout(w1)+wcor |
561 |
endif else begin |
562 |
logwout(w1)=logwout(w1)+total(rebin(dw(w),1)) |
563 |
end |
564 |
end |
565 |
; if (in eq 3) then stop |
566 |
end |
567 |
widthj=exp(logwout) |
568 |
|
569 |
; |
570 |
; Background |
571 |
w=where((qin(0,*) ge 10) and (qin(0,*) le 50),count) |
572 |
l=reform(qin(0,w)) |
573 |
n=reform(qin(1,w)) |
574 |
f=fa(l,n) |
575 |
back=reform(qin(5,w)) |
576 |
sigb=reform(qin(5+sigoff,w)) |
577 |
; |
578 |
mub=1e-5 |
579 |
npar0=15 |
580 |
chi=dblarr(25) |
581 |
;for npar0=2,20 do begin |
582 |
npar=npar0+2 |
583 |
a=dblarr(count,npar) |
584 |
fi=min(f)+(max(f)-min(f))*dindgen(npar0)/(npar0-1) |
585 |
fi=[2*fi(0)-fi(1),fi,2*fi(npar0-1)-fi(npar0-2)] |
586 |
for i=0,npar-1 do begin |
587 |
qmin=min(f)-1000 |
588 |
if (i gt 0) then qmin=fi(i-1) |
589 |
qmax=max(f)+1000 |
590 |
if (i lt (npar-1)) then qmax=fi(i+1) |
591 |
xi=dblarr(npar) |
592 |
xi(i)=1.0 |
593 |
w=where((f ge qmin) and (f le qmax),cw) |
594 |
if (cw ne 0) then a(w,i)=interpol(xi,fi,f(w)) |
595 |
end |
596 |
ap=a/rebin(reform(sigb,count,1),count,npar) |
597 |
y=back |
598 |
yp=y/sigb |
599 |
b=transpose(ap)#ap |
600 |
; Add first derivative regularization to nu term |
601 |
bfmax=0.0 |
602 |
for i=0,npar-1 do bfmax=bfmax>b(i,i) |
603 |
b(0,0)=b(0,0)+mub*bfmax |
604 |
b(npar-1,npar-1)=b(npar-1,npar-1)+mub*bfmax |
605 |
for i=1,npar-2 do b(i,i)=b(i,i)+2*mub*bfmax |
606 |
for i=0,npar-2 do b(i,i+1)=b(i,i+1)-mub*bfmax |
607 |
for i=0,npar-2 do b(i+1,i)=b(i+1,i)-mub*bfmax |
608 |
ib=invert(b) |
609 |
c=transpose(ap)#yp |
610 |
x=ib#c |
611 |
; Extrapolate the correction functions |
612 |
x(0)=x(1)-(fi(1)-fi(0))/(fi(2)-fi(1))*(x(2)-x(1)) |
613 |
x(npar-1)=x(npar-2)-(fi(npar-2)-fi(npar-1))/(fi(npar-3)-fi(npar-2))*(x(npar-3)-x(npar-2)) |
614 |
modelx=a#x |
615 |
model=exp(modelx) |
616 |
print,npar,total(rebin((back/sigb)^2,1)),total(rebin(((back-modelx)/sigb)^2,1)),total(rebin(((back-modelx)/sigb)^2,1))/total(rebin((back/sigb)^2,1)) |
617 |
chi(npar)=total(rebin(((back-modelx)/sigb)^2,1)) |
618 |
;end |
619 |
l=reform(qin(0,*)) |
620 |
n=reform(qin(1,*)) |
621 |
f=fa(l,n) |
622 |
back=reform(qin(5,*)) |
623 |
sigb=reform(qin(5+sigoff,*)) |
624 |
backi=interpol(x,fi,f) |
625 |
db=back-backi |
626 |
backout=interpol(x,fi,fout) |
627 |
for in=min(n),max(n) do begin |
628 |
w=where(n eq in,c) |
629 |
if (c ne 0) then begin |
630 |
w1=where(nout eq in) |
631 |
if (c ge 10) then begin |
632 |
lav=total(rebin(l(w),1)) |
633 |
backout(w1)=backout(w1)+poly(lout(w1)-lav,poly_fit(l(w)-lav,db(w),2)) |
634 |
endif else begin |
635 |
backout(w1)=backout(w1)+total(rebin(db(w),1)) |
636 |
end |
637 |
end |
638 |
end |
639 |
backj=backout>(-9) |
640 |
|
641 |
; Do asymmetry |
642 |
|
643 |
if (iasym ne 0) then begin |
644 |
asym=qin(7,*) |
645 |
dasym=qin(7+sigoff,*) |
646 |
asymj=dblarr(cj) |
647 |
; Set asymmetry to something for the high n modes with few input measurements |
648 |
w=where(n ge 20) |
649 |
asymj=poly(fj,polyfitw(qin(2,w),qin(7,w),1/qin(13,w),2)) |
650 |
for nx=0,max(n) do begin |
651 |
w=where(n eq nx,c) |
652 |
wj=where(nj eq nx) |
653 |
if (c ge 5) then begin |
654 |
; Fit second order polynomial if enough points |
655 |
asymj(wj)=poly(fj(wj),polyfitw(f(w),asym(w),1/dasym(w),2)) |
656 |
; Replace with last point outside of ends to avoid unreasonable values |
657 |
lx=max(l(w)) |
658 |
asymj(wj(where(lj(wj) ge lx)))=asymj(wj(where(lj(wj) eq lx))) |
659 |
lx=min(l(w)) |
660 |
asymj(wj(where(lj(wj) le lx)))=asymj(wj(where(lj(wj) eq lx))) |
661 |
end |
662 |
end |
663 |
end |
664 |
|
665 |
;asymj(where((nj eq 0) and (lj ge 150)))=50 |
666 |
|
667 |
parin=dblarr(12+iasym,cin) |
668 |
parin(0,*)=qin(1,*) |
669 |
parin(1,*)=qin(0,*) |
670 |
parin(2:4,*)=qin(2:4,*) |
671 |
parin(11,*)=qin(5,*) |
672 |
parin(5:10,*)=qin(aoff:aoff+5,*)/1000 |
673 |
if (iasym ne 0) then parin(12,*)=atan(qin(7,*)) |
674 |
parmodel=dblarr(12+iasym,cj) |
675 |
parmodel(0,*)=nout |
676 |
parmodel(1,*)=lout |
677 |
parmodel(2,*)=fjx |
678 |
parmodel(3,*)=ampj |
679 |
parmodel(4,*)=widthj>(1e-4) |
680 |
parmodel(5:10,*)=aj/1000 |
681 |
parmodel(11,*)=backj |
682 |
if (iasym ne 0) then parmodel(12,*)=atan(asymj)<1.57 |
683 |
ix=intarr(lmax+1,max(nj)+1) |
684 |
for i=0,cj-1 do ix(lout(i),nout(i))=i |
685 |
ix1=intarr(cin) |
686 |
for i=0,cin-1 do ix1(i)=ix(qin(0,i),qin(1,i)) |
687 |
parout=parmodel |
688 |
parout(*,ix1)=parin |
689 |
;parout(12,where((nj eq 0) and (lj ge 150)))=atan(50.) |
690 |
openw,1,outfile |
691 |
if (iasym eq 0) then begin |
692 |
for i=0,cj-1 do printf,1,parout(*,i),format='(i2,i4,f11.4,99g16.8)' |
693 |
endif else begin |
694 |
for i=0,cj-1 do printf,1,parout(*,i),format='(i2,i4,f11.4,99g16.8)' |
695 |
end |
696 |
close,1 |
697 |
p=parout |
698 |
|
699 |
return,p |
700 |
|
701 |
end |
702 |
|