path = "cc_ds/polynomial646/"
prefix = "ab_t"
//path = "ds/"
//prefix = "ab_xyc_"

names = import(path + "namelist.txt",/text)
shiftsimages = import(path+"shiftsimages.txt",/number)
shiftsblocks = import(path+"shiftsblocks.txt",/number)

//shiftsimages /= 1.0358
//for i=1, nelements(shiftsimages) shiftsimages[i] = round(shiftsimages[i])
//shiftsblocks /= 1.0358

//shiftsblocks = [0,4.89,4.9,6.49,0,6.27]

nrnames = nelements(names)
nrshiftsimages = nelements(shiftsimages)
nrshiftblocks = nelements(shiftsblocks)

if (nrnames != nrshiftsimages) {
    print ">>> ERROR: #names ("+ nrnames +") and #shiftsimages ("+ nrshiftsimages +") isn't the same!!!"
}

blockstart = [ 1,  9, 15, 21, 25, 29]
blockend =   [ 8, 14, 20, 24, 28, 34]

first = 1
for i=blockstart[1], blockend[1] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockA = tmpimg
        first = 0
    } else {
        blockA += tmpimg
    }
}
writefits path+"blockA.fits", blockA

first = 1
for i=blockstart[2], blockend[2] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockB = tmpimg
        first = 0
    } else {
        blockB += tmpimg
    }
}
writefits path+"blockB.fits", blockB

first = 1
for i=blockstart[3], blockend[3] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockC = tmpimg
        first = 0
    } else {
        blockC += tmpimg
    }
}
writefits path+"blockC.fits", blockC

first = 1
for i=blockstart[4], blockend[4] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockD = tmpimg
        first = 0
    } else {
        blockD += tmpimg
    }
}
writefits path+"blockD.fits", blockD

first = 1
for i=blockstart[5], blockend[5] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockE = tmpimg
        first = 0
    } else {
        blockE += tmpimg
    }
}
writefits path+"blockE.fits", blockE

first = 1
for i=blockstart[6], blockend[6] {
    nn = path + prefix + simplifywhitespace(names[i]) + ".fits"
    tmpimg = readfits(nn)
    shift tmpimg, 0, shiftsimages[i], /poly3
    if (first==1) {
        blockF = tmpimg
        first = 0
    } else {
        blockF += tmpimg
    }
}
writefits path+"blockF.fits", blockF

final = shift(blockA, 0, shiftsblocks[1], /poly3)
final += shift(blockB, 0, shiftsblocks[2], /poly3)
final += shift(blockC, 0, shiftsblocks[3], /poly3)
final += shift(blockD, 0, shiftsblocks[4], /poly3)
//final += shift(blockE, 0, shiftsblocks[5], /poly3)
final += shift(blockF, 0, shiftsblocks[6], /poly3)
writefits path + "final.fits", final

print ">>> Finished shifting!"
