//
// 
// Project:      COSPAR Workshop on Planetary Sciences (Montevideo, July/August 2007)
// Author:       jvazquez
// File:         vmc_fov.cpp
// Created on    Mon Jul 16 21:08:48 CEST 2007


#include <iostream>
using namespace std;

#include <SpiceUsr.h>

int main( char argc, char *argv[] ) {

	// Load the needed kernels
	furnsh_c( "../code/kernels/naif0008.tls" );
	furnsh_c( "../code/kernels/de414.bsp" );
	furnsh_c( "../code/kernels/ormm__041101000000_00100.bsp" );
	furnsh_c( "../code/kernels/atnm_p030602191822_00135.bc" );
	furnsh_c( "../code/kernels/mex_v08.tf" );
	furnsh_c( "../code/kernels/mex_070605_step.tsc" );
	furnsh_c( "../code/kernels/pck00008.tpc" );
	furnsh_c( "../code/kernels/mex_v08.tf" );
	furnsh_c( "../code/kernels/mex_hrsc_v03.ti" );
	
	
	// Get the ID for the HRSC superresolution filter. It also can 
	// be found in the MEX frames kernel
	SpiceInt hrsc_id;
	SpiceBoolean found;
	bodn2c_c( "MEX_HRSC_SRC", &hrsc_id, &found );

	ConstSpiceInt SIZE = 100;
	ConstSpiceInt MAX_VECTORS = 4;
	SpiceChar shape[ SIZE ];
	SpiceChar frame[ SIZE ];
	SpiceDouble bsight[ 3 ];
	SpiceDouble bounds[ MAX_VECTORS ][ 3 ];
	SpiceInt n;
	
	// Get the field fo view for HRSC
	getfov_c( hrsc_id, MAX_VECTORS, SIZE, SIZE, shape, frame, bsight, &n, bounds );
	
	SpiceDouble et;
	// Get the Ephemeris time
	utc2et_c( "2004-11-12T04:43:11.594", &et );
	
	SpiceDouble matrix[ 3 ][ 3 ];
	// Get the transformation matrix
	pxform_c( frame, "IAU_MARS", et, matrix );
	
	SpiceDouble new_bs[ 3 ];
	// Transform the boresight to MARS IAU
	mxv_c( matrix, bsight, new_bs );
	
	SpiceDouble mex_pos[ 3 ];
	SpiceDouble lt;
	// Get the position of MEX
	spkpos_c( "MEX", et, "IAU_MARS", "LT+S", "MARS", mex_pos, &lt );
	
	SpiceInt mars_id;
	// Calculate the NAIF ID for Mars
	bodn2c_c( "MARS", &mars_id, &found );

	SpiceDouble mars_r[ 3 ];
	SpiceInt dim;	
	// Calculate the radii of Mars
	bodvcd_c( mars_id, "RADII", 3, &dim, mars_r );
	
	SpiceDouble point[ 3 ];
	// Get the intersection of the boresight with the Mars surface
	surfpt_c( mex_pos, new_bs, mars_r[ 0 ], mars_r[ 1 ], mars_r[ 2 ], point, &found );
	if ( found == SPICEFALSE ) {
		cout << "Boresight doesn't intersect the Mars surface" << endl;
		return 0;	
	}
	
	SpiceDouble vdistance[ 3 ];
	SpiceDouble distance;
	// Get the distance vector
	vsub_c( mex_pos, point, vdistance );
	distance = vnorm_c( vdistance );
	
	SpiceInt how_many;
	SpiceDouble ifov;
	// Get the field of view of a SRC pixel
	gdpool_c( "INS-41220_IFOV", 0, 1, &how_many, &ifov, &found );
	if ( found == SPICEFALSE ) {
		cout << "IFOV not found" << endl;
		return 0;
	}
	
	SpiceDouble resolution;
	// Get the resolution
	resolution = ifov * distance;
	
	cout << "Resolution: " << resolution << " Km/pixel" << endl;
	
	return 0;

}

