// reference implementation of calibration of lucifer data using dpuser
// there has to be a directory where the files to calibrate are stored
// this directory has to be specified with the variable "path"
// in the same path has to be a file "namelist.txt" with the names of the
// files to process. The ".fits" extension must be omitted.

@lucidpl.fnc
@lucicaly.fnc
@luciinterpoly.fnc
@luciidentify.fnc
@lucicalx.fnc
@luciinterpolx.fnc

// LOAD DATA /////////////////////////////////////////
path = "/scratch/lucifer/"
dark = readfits(path + "dark.fits")
linelist = import("luci_oh_list.dat")
path += "ds/"     // "ds/" "cc_ds/"
names = import(path+"namelist.txt",/text)
// END LOAD DATA /////////////////////////////////////////

// SET PARAMETERS /////////////////////////////////////////
intermediate = 1                    // 1: save intermediate output fits, 0: don't save
plot=""                             // plotdevice: plot analysis of identified lines, "": don't plot

ycalpars = fits(5)
ycalpars[1] = 520                   // bottom_lo range
ycalpars[2] = 570                   // bottom_hi range
ycalpars[3] = 1425                  // upper_lo range
ycalpars[4] = 1525                  // upper_hi range
ycalpars[5] = 900                   // new height of interpolated longslit

edgedetectpars = fits(6)
edgedetectpars[1] = 150             // number of pixels to omit at left end
edgedetectpars[2] = 0               // number of pixels to omit at right end
edgedetectpars[3] = 50              // size of moving window to median
edgedetectpars[4] = 31              // order of polynomial to fit locally to the edge (in the moving window)
edgedetectpars[5] = 4               // order of the polynomial to fit to the whole edge
edgedetectpars[6] = 10              // sigma-factor for rejection

yinterpolpars = fits(2)
yinterpolpars[1] = ycalpars[5]      // new height of interpolated longslit
yinterpolpars[2] = 0                // 0 (default), increase this value to 3 or 5 in order to see the border of the longslit
                                    // although this is not recommended since fitting the lines will fail in this region

identifylinespars = fits(6)
identifylinespars[1] = 0.00017125   // disp: expected dispersion
identifylinespars[2] = 50           // midgausssize: size of sub-longslit to median
identifylinespars[3] = 0.26         // disp_tol: tolerance of the expected dispersion
identifylinespars[4] = 0.05         // interval_tol: tolerance for interval ratio comparison
identifylinespars[5] = 30           // omitpixels: number of pixels to omit at left and right end of midline
identifylinespars[6] = 200          // maxlines: maximum number of lines to expect (200 should be enough)

method = "polynomial"               // background removal method:
                                    // "polynomial": fit background with polynomial (should be default)
                                    // "boxcar": subtract boxcar'd signal 
                        
//boxcarbkgrpars = fits(3)
//boxcarbkgrpars[1] = 2               // simga threshold
//boxcarbkgrpars[2] = 5               // boxfactor
//boxcarbkgrpars[3] = 5               // smoothfactor
//bkgrpars = boxcarbkgrpars

polybkgrpars = fits(6)
polybkgrpars[1] = 0.5               // simga threshold
polybkgrpars[2] = 2                 // smoothfactor
polybkgrpars[3] = 50                // rejection window
polybkgrpars[4] = 3                 // sigma for rejection
polybkgrpars[5] = 6                 // number of iterations for rejection
polybkgrpars[6] = 8                 // order for background polynomial fit
bkgrpars = polybkgrpars

xcalpars = fits(6)
xcalpars[1] = 20                    // height of region around gauss to fit
xcalpars[2] = 5                     // half width of region around gauss to fit (to the left and right)
xcalpars[3] = 10                    // offset for gauss fit running in y-direction from bottom to top
xcalpars[4] = 2                     // order of polynomial to fit to lines (y-direction)
xcalpars[5] = 2                     // order of polynomial to fit to spectrum (x-direction)
xcalpars[6] = 10                    // sigma-factor for rejection

xinterpolpars = fits(3)
xinterpolpars[1] = 0.0001665        // new dispersion of interpolated longslit
xinterpolpars[2] = 1.9498           // new start wavelength of interpolated longslit
xinterpolpars[3] = 2300             // new width of interpolated longslit
// END SET PARAMETERS /////////////////////////////////////

// START PROCESSING /////////////////////////////////////
linelist = reform(linelist,nelements(linelist))

curname = stringarray(2)
// loop namelist in pairs
//for (i=1; i<=nelements(names); i+=2) {
for (i=1; i<=2; i+=2) {
    curname[1] = simplifywhitespace(names[i])
    curname[2] = simplifywhitespace(names[i+1])

    // do this twice for first and second file
    for j=1,2 {
    //for j=1,1 {
        image = readfits(path+curname[j]+".fits")
        setwcs image, 1, 1, 1, 1, 1, 1

        dplimage = lucidpl(image, dark)
        flip dplimage, 1
        if (intermediate == 1) writefits path+"c_"+curname[j]+".fits", dplimage

        // calibrate spatially
        caly = lucicaly(dplimage, ycalpars, edgedetectpars)
        if (intermediate == 1) writefits path+"caly_"+curname[j]+".fits", caly

        // calibrate & crop in y-direction
        interpoly = luciinterpoly(dplimage, caly, yinterpolpars)
        if (intermediate == 1) writefits path+"yc_"+curname[j]+".fits", interpoly

        id = luciidentify(interpoly, linelist, identifylinespars, method, bkgrpars, plot)
        if (intermediate == 1) writefits path+"id_"+curname[j]+".fits", id

        // calibrate spectrally
        calx = lucicalx(interpoly, id, xcalpars)
        if (intermediate == 1) writefits path+"calx_"+curname[j]+".fits", calx

        interpolx = luciinterpolx(interpoly, calx, xinterpolpars)
        if (intermediate == 1) writefits path+"xyc_"+curname[j]+".fits", interpolx

        if (j==1) {
            imgA = interpolx
        } else {
            imgB = interpolx
        }
    }

    res1 = imgA-imgB
    writefits path+"ab_xyc_"+curname[1]+".fits", res1
    
    res2 = imgB-imgA
    writefits path+"ab_xyc_"+curname[2]+".fits", res2
    
    print "#############################################################"
    print "  Processed: " + path+curname[1] + ".fits & " + path+curname[2] + ".fits"
    print "#############################################################"
}

print ">>> Finished doitallxy!"
// END PROCESSING /////////////////////////////////////

//@shiftcalibrated.dpuser
