
Simple routines in this directory can be used to make a linelist
based on MgII absorption:

1)  Read in data from 2d_v4_8 directory and apply a continuum fit.
    Use pratical choices of zmin, zmax to trim sample:
    Select [plates] and optional mjds from plate list

getqso, [plates], [mjds], mgspec=mgspec, zmin=0.7, zmax=4.0


2)  Convert flux to Equivalent Width:  EW = 1.0 - Flux/Cont

rline_findew, mgspec, ew, ewinv, fopt, foptinv, sigma=1.0

3)  Identify significant ew peaks, let's try 3sigma and see how many
    false detections we can expect. 
    Npeak is Maximum number of peaks returned per spectrum, 
    we should double check to make sure this limit is not reached.
    threshold is sigma limit for detection:

peak_list = rline_findpeaks(ew, ewinv, npeak=80, threshold=3.0)

4)  Now loop through peak_list to identify MgII and other associated 
    Fe,MgI,Ca,... lines at common wavelength.  

id_list   = qso_sortpeaks(peak_list, mgspec, linelist='mgII.lines')

5)  You will see a print out of the lines identified.  To 
    plot the lines identified in a spectrum (i.e. 29), use:
    (Red diamonds are pixels rejected during continuum fit procedure)

quickplot, mgspec, 29, id_list


6)  Now we need to summarize the results. An ascii table is best:

;i   Plate-MJD-Fiber  RA   DEC    ZQSO    ZABS    EW_2796   EW_2803  EW_2852 ...
;  The Equivalength Widths are output as "Milli-pixels"

id_summarize, id_list, mgspec, summary=s


Summary of commands here:

readcol, 'platelist.txt', plates, mjds
getqso, plates, mjds, mgspec=mgspec, zmin=0.7, zmax=4.0
rline_findew, mgspec, ew, ewinv, fopt, foptinv, sigma=1.0
peak_list = rline_findpeaks(ew, ewinv, npeak=80, threshold=3.0)
id_list   = qso_sortpeaks(peak_list, mgspec, linelist='mgII.lines')
id_summarize, id_list, mgspec, summary=s
print_idsummary,s
ui = s[uniq(s.index)].index
mgspec3 = mgspec[ui]
save, mgspec, filename='~/QSO/Lens/plates_mgii.dat'

nabs = mgii_pathlength(s, mgspec)
propfrac, s.zabs, s.zqso, ldist=ldist, sdist=sdist
s.rank = ldist * s.ew2796 * (20.0 - mgspec[s.index].plug.mag[3])
print_idsummary,s, filename='mgii_table.ascii'


