
import numpy as np

__version__ = "$Id: definefov.py 32 2013-12-19 22:13:00Z fergalm $"
__URL__ = "$URL: http://svn.code.sf.net/p/keplertwowheel/code/py/definefov.py $"


"""
Code to create, and load, unit vectors representing the
channel corners in the field of view
"""

def generateVectors():
    """Convert the known ra/decs of the channel corners
    into unit vectors. This code creates the conents of the
    function loadOriginVectors() (below)
    """

    ra_deg = 290.66666667
    dec_deg = +44.5
    #rollAngle_deg = 33.0
    rollAngle_deg = +123.
    boresight = r.vecFromRaDec(ra_deg, dec_deg)

    #Read in prime mission coords and convert to vectors
    inFile = "../etc/fov.txt"
    inFile = np.loadtxt(inFile)
    radecs = np.zeros( (len(inFile), 3))
    rotate = radecs*0
    for i, row in enumerate(inFile):
        radecs[i, :] = r.vecFromRaDec(inFile[i, 4], inFile[i,5])
        rotate[i, :] = r.rotateAroundVector(radecs[i], boresight,\
            -rollAngle_deg)




    #Slew to ra/dec of zero
    Ra = r.rightAscensionRotationMatrix(-ra_deg)
    Rd = r.declinationRotationMatrix(-dec_deg)
    R = np.dot(Rd, Ra)
    origin = rotate*0
    for i, row in enumerate(rotate):
        origin[i] = np.dot(R, rotate[i])

    mp.plot(origin[:,0], origin[:,1])

    #Print out the results
    #import pdb; pdb.set_trace()
    print "["
    for i in range(len(inFile)):
        ch = channelFromModOut(inFile[i,0], inFile[i,1])
        print "[%3i., %3i., %3i., %13.7f, %13.7f, %13.7f], \\" %( \
            inFile[i, 0], inFile[i, 1], ch, \
            origin[i, 0], origin[i, 1], origin[i,2])
    print "]"





def loadOriginVectors():
    """Vectors defining corners of channels

    If the spacecraft was pointed at (ra, dec) = (0,0) with mod3
    pointed north, r.raDecFromVec() of these values would give
    The ra and decs of the corners* of the modules.

    *For the FGS sensors, returned vectors are for the corners of
    the modules, within my ability to define that term. For the
    science pixels, the values returned at 5 pixels inside the
    corners of the science collection parts of the CCD (as opposed
    to smear, overscan etc., columns)
    """

    #Columns are mod, out, channel, x, y, z
    #x,y and z make up a unit vector
    return np.array(\
        [
            [  2.,   1.,   1.,     0.9943511,     0.0708017,     0.0790761], \
            [  2.,   1.,   1.,     0.9956156,     0.0498513,     0.0791475], \
            [  2.,   1.,   1.,     0.9938789,     0.0498206,     0.0986038], \
            [  2.,   1.,   1.,     0.9926144,     0.0707811,     0.0985222], \
            [  2.,   2.,   2.,     0.9964468,     0.0286667,     0.0791962], \
            [  2.,   2.,   2.,     0.9956262,     0.0496395,     0.0791481], \
            [  2.,   2.,   2.,     0.9938894,     0.0496086,     0.0986044], \
            [  2.,   2.,   2.,     0.9947111,     0.0286258,     0.0986426], \
            [  2.,   3.,   3.,     0.9923909,     0.0285854,     0.1197627], \
            [  2.,   3.,   3.,     0.9915692,     0.0495596,     0.1197261], \
            [  2.,   3.,   3.,     0.9937189,     0.0496034,     0.1003108], \
            [  2.,   3.,   3.,     0.9945398,     0.0286204,     0.1003563], \
            [  2.,   4.,   4.,     0.9902968,     0.0707233,     0.1196261], \
            [  2.,   4.,   4.,     0.9915587,     0.0497715,     0.1197255], \
            [  2.,   4.,   4.,     0.9937084,     0.0498154,     0.1003102], \
            [  2.,   4.,   4.,     0.9924448,     0.0707762,     0.1002197], \
            [  3.,   1.,   5.,     0.9966269,     0.0211550,     0.0792924], \
            [  3.,   1.,   5.,     0.9968499,     0.0001681,     0.0793111], \
            [  3.,   1.,   5.,     0.9951106,     0.0001748,     0.0987669], \
            [  3.,   1.,   5.,     0.9948882,     0.0211718,     0.0987379], \
            [  3.,   2.,   6.,     0.9966284,    -0.0210311,     0.0793067], \
            [  3.,   2.,   6.,     0.9968499,    -0.0000440,     0.0793112], \
            [  3.,   2.,   6.,     0.9951106,    -0.0000374,     0.0987669], \
            [  3.,   2.,   6.,     0.9948897,    -0.0210346,     0.0987524], \
            [  3.,   3.,   7.,     0.9925746,    -0.0210429,     0.1198032], \
            [  3.,   3.,   7.,     0.9927943,    -0.0000531,     0.1198312], \
            [  3.,   3.,   7.,     0.9949457,    -0.0000568,     0.1004148], \