# include <dvo.h>

/* convert panstarrs-format measures to internal measures */
Measure *Measure_Panstarrs_DEV_1_ToInternal (Measure_Panstarrs_DEV_1 *in, off_t Nvalues) {

  off_t i;
  Measure *out;

  ALLOCATE (out, Measure, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR         = in[i].dR;
    out[i].dD         = in[i].dD;
    out[i].M          = in[i].M;
    out[i].dM         = in[i].dM;
    out[i].Mcal       = in[i].Mcal;

    out[i].airmass    = in[i].airmass;
    out[i].az         = in[i].az;
    out[i].dt         = in[i].dt;
    out[i].FWx 	      = in[i].FWx;
    out[i].FWy 	      = in[i].FWy;
    out[i].theta      = in[i].theta;
    out[i].photcode   = in[i].photcode;
    out[i].t          = in[i].t;
    out[i].averef     = in[i].averef;
    out[i].Xccd       = in[i].Xccd;
    out[i].Yccd       = in[i].Yccd;
    out[i].dXccd      = in[i].dXccd;
    out[i].dYccd      = in[i].dYccd;
    out[i].Sky        = in[i].Sky;
    out[i].dSky       = in[i].dSky;

    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags

    // changed or added for PS1_DEV_1 (2008.02.26)
    out[i].psfQual    = (in[i].psfQual == NAN_S_SHORT) ? NAN : in[i].psfQual;
    out[i].dbFlags    = in[i].flags;
    out[i].detID      = in[i].detID_lo;
    out[i].imageID    = in[i].imageID_lo;
    out[i].psfChisq   = 0;
    out[i].crNsigma   = 0;
    out[i].extNsigma  = 0;

    // changed or added for PS1_DEV_2
    out[i].Map        = in[i].Mgal;
    out[i].dMcal      = 0; 

    // changed or added for PS1_V1
    out[i].photFlags  = in[i].dophot << 16;
    out[i].t_msec     = 0;
    out[i].extID      = 0;
    out[i].objID      = 0;
    out[i].catID      = 0;
    out[i].Mxx 	      = 0.0;
    out[i].Mxy 	      = 0.0;
    out[i].Myy        = 0.0;
    out[i].posangle   = 0;
    out[i].pltscale   = 0;
    out[i].psfNdof    = 0;
    out[i].psfNpix    = 0;
  }
  return (out);
}

/* convert internal measures to panstarrs-format measures */
Measure_Panstarrs_DEV_1 *MeasureInternalTo_Panstarrs_DEV_1 (Measure *in, off_t Nvalues) {

  off_t i;
  Measure_Panstarrs_DEV_1 *out;

  ALLOCATE (out, Measure_Panstarrs_DEV_1, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR         = in[i].dR;
    out[i].dD         = in[i].dD;
    out[i].M          = in[i].M;
    out[i].dM         = in[i].dM;
    out[i].Mcal       = in[i].Mcal;
    out[i].airmass    = in[i].airmass;
    out[i].az         = in[i].az;
    out[i].dt         = in[i].dt;
    out[i].FWx 	      = in[i].FWx;
    out[i].FWy 	      = in[i].FWy;
    out[i].theta      = in[i].theta;
    out[i].photcode   = in[i].photcode;
    out[i].t          = in[i].t;
    out[i].averef     = in[i].averef;
    out[i].Xccd       = in[i].Xccd;
    out[i].Yccd       = in[i].Yccd;
    out[i].dXccd      = in[i].dXccd;
    out[i].dYccd      = in[i].dYccd;
    out[i].Sky        = in[i].Sky;
    out[i].dSky       = in[i].dSky;

    // changed or added for PS1_DEV_1 (2008.02.26)
    out[i].flags      = in[i].dbFlags;
    out[i].psfQual    = isnan(in[i].psfQual) ? NAN_S_SHORT : in[i].psfQual;
    out[i].detID_hi   = 0;
    out[i].detID_lo   = in[i].detID;
    out[i].imageID_hi = 0;
    out[i].imageID_lo = in[i].imageID;

    // changed or added for PS1_DEV_2
    out[i].Mgal       = in[i].Map;

    // changed or added for PS1_V1
    // out[i].photFlags  = in[i].photFlags & 0x0000ffff; (only dophot pre PS1_DEV_1)
    out[i].dophot     = in[i].photFlags >> 16;
  }
  return (out);
}

/* convert panstarrs-format averages to internal averages */
// 'primary is needed to conform with the API for Loneos and Elixir, but is not used
Average *Average_Panstarrs_DEV_1_ToInternal (Average_Panstarrs_DEV_1 *in, off_t Nvalues, SecFilt **primary) {

  off_t i;
  Average *out;

  ALLOCATE (out, Average, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].R        	 = in[i].R;      
    out[i].D        	 = in[i].D;      
    out[i].Xp       	 = in[i].Xp;     
    out[i].dR       	 = in[i].dR;
    out[i].dD       	 = in[i].dD;
    out[i].uR       	 = in[i].uR;
    out[i].uD       	 = in[i].uD;
    out[i].duR      	 = in[i].duR;
    out[i].duD      	 = in[i].duD;
    out[i].P        	 = in[i].P;
    out[i].dP       	 = in[i].dP;
    out[i].objID 	 = in[i].objID;
    out[i].catID 	 = in[i].catID;

    // changed or added for PS1_DEV_2
    out[i].Nmeasure      = in[i].Nm;     
    out[i].Nmissing      = in[i].Nn;     
    out[i].measureOffset = in[i].offset; 
    out[i].missingOffset = in[i].missing;
    out[i].Nextend       = 0;
    out[i].extendOffset  = 0;

    // changed or added for PS1_V1
    out[i].flags    	 = in[i].code;   
    out[i].ChiSqAve    	 = NAN;
    out[i].ChiSqPM    	 = NAN;
    out[i].ChiSqPar    	 = NAN;
    out[i].Tmean    	 = 0;
    out[i].Trange    	 = 0;
    out[i].Npos       	 = 0.0;
    out[i].extID 	 = 0;
  }
  return (out);
}

/* convert internal averages to panstarrs-format averages */
// 'primary is needed to conform with the API for Loneos and Elixir, but is not used
Average_Panstarrs_DEV_1 *AverageInternalTo_Panstarrs_DEV_1 (Average *in, off_t Nvalues, SecFilt *primary) {

  off_t i;
  Average_Panstarrs_DEV_1 *out;

  ALLOCATE (out, Average_Panstarrs_DEV_1, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].R        = in[i].R;      
    out[i].D        = in[i].D;      
    out[i].Xp       = in[i].Xp;     
    out[i].dR       = in[i].dR;
    out[i].dD       = in[i].dD;
    out[i].uR       = in[i].uR;
    out[i].uD       = in[i].uD;
    out[i].duR      = in[i].duR;
    out[i].duD      = in[i].duD;
    out[i].P        = in[i].P;
    out[i].dP       = in[i].dP;
    out[i].objID    = in[i].objID;
    out[i].catID    = in[i].catID;

    // changed or added for PS1_DEV_2
    out[i].Nm       = in[i].Nmeasure;     
    out[i].Nn       = in[i].Nmissing;     
    out[i].offset   = in[i].measureOffset; 
    out[i].missing  = in[i].missingOffset;

    // changed or added for PS1_V1
    out[i].code          = in[i].flags;   
  }
  return (out);
}

/* convert panstarrs-format secfilts to internal secfilts */
SecFilt *SecFilt_Panstarrs_DEV_1_ToInternal (SecFilt_Panstarrs_DEV_1 *in, off_t Nvalues) {

  off_t i;
  SecFilt *out;

  ALLOCATE (out, SecFilt, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].M     = in[i].M;      
    out[i].dM    = in[i].dM;      
    out[i].Xm    = in[i].Xm;     
    out[i].Ncode = in[i].Ncode;
    out[i].Nused = in[i].Nused;

    // changed or added for PS1_V1
    out[i].M_20  = 0;      
    out[i].M_80  = 0;      

    // changed or added for PS1_V2
    out[i].flags = 0;
 }
  return (out);
}

/* convert internal secfilts to panstarrs-format secfilts */
SecFilt_Panstarrs_DEV_1 *SecFiltInternalTo_Panstarrs_DEV_1 (SecFilt *in, off_t Nvalues) {

  off_t i;
  SecFilt_Panstarrs_DEV_1 *out;

  ALLOCATE (out, SecFilt_Panstarrs_DEV_1, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].M     = in[i].M;      
    out[i].dM    = in[i].dM;      
    out[i].Xm    = in[i].Xm;     
    out[i].Ncode = in[i].Ncode;
    out[i].Nused = in[i].Nused;
  }
  return (out);
}

/* convert panstarrs-format images to internal images */
Image *Image_Panstarrs_DEV_1_ToInternal (Image_Panstarrs_DEV_1 *in, off_t Nvalues) {

  off_t i;
  Image *out;

  ALLOCATE (out, Image, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));

    strncpy (out[i].name, in[i].name, 63); // in[64], out[128]
    out[i].name[63] = 0; // force termination

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].secz	    	    = in[i].secz;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit  	    = in[i].apmifit;
    out[i].dapmifit 	    = in[i].dapmifit;
    out[i].Mcal	    	    = in[i].Mcal;
    out[i].dMcal    	    = in[i].dMcal;
    out[i].Xm	    	    = in[i].Xm;
    out[i].photcode   	    = in[i].photcode;
    out[i].exptime  	    = in[i].exptime;
    out[i].sidtime  	    = in[i].sidtime;
    out[i].latitude  	    = in[i].latitude;
    out[i].detection_limit  = in[i].detection_limit;
    out[i].saturation_limit = in[i].saturation_limit;
    out[i].cerror	    = in[i].cerror;
    out[i].fwhm_x	    = in[i].fwhm_x;
    out[i].fwhm_y	    = in[i].fwhm_y;
    out[i].trate	    = in[i].trate;
    out[i].ccdnum	    = in[i].ccdnum;

    out[i].order	    = in[i].order;
    out[i].Mx		    = in[i].Mx;
    out[i].My		    = in[i].My;
    out[i].Mxx		    = in[i].Mxx;
    out[i].Mxy		    = in[i].Mxy;
    out[i].Myy		    = in[i].Myy;
    out[i].Mxxx		    = in[i].Mxxx;
    out[i].Mxxy		    = in[i].Mxxy;
    out[i].Mxyy		    = in[i].Mxyy;
    out[i].Myyy		    = in[i].Myyy;
    out[i].Mxxxx	    = in[i].Mxxxx;
    out[i].Mxxxy	    = in[i].Mxxxy;
    out[i].Mxxyy	    = in[i].Mxxyy;
    out[i].Mxyyy	    = in[i].Mxyyy;
    out[i].Myyyy	    = in[i].Myyyy;

    // changed or added for PS1_DEV_1
    out[i].imageID	    = in[i].imageID_lo;

    // changed or added for PS1_DEV_2
    out[i].externID	    = 0;
    out[i].sourceID	    = 0;

    // changed or added for PS1_V1
    out[i].flags	    = in[i].code;
    out[i].parentID	    = 0;

    // changed or added for PS1_V2
    out[i].RAo  	    = NAN;
    out[i].DECo  	    = NAN;
    out[i].Radius  	    = NAN;
  }
  return (out);
}

/* convert internal images to panstarrs-format images */
Image_Panstarrs_DEV_1 *ImageInternalTo_Panstarrs_DEV_1 (Image *in, off_t Nvalues) {

  off_t i;
  Image_Panstarrs_DEV_1 *out;

  ALLOCATE (out, Image_Panstarrs_DEV_1, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));

    strncpy (out[i].name, in[i].name, 63); // in[128], out[64]
    out[i].name[63] = 0; // force termination

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].secz	    	    = in[i].secz;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit  	    = in[i].apmifit;
    out[i].dapmifit 	    = in[i].dapmifit;
    out[i].Mcal	    	    = in[i].Mcal;
    out[i].dMcal    	    = in[i].dMcal;
    out[i].Xm	    	    = in[i].Xm;
    out[i].photcode   	    = in[i].photcode;
    out[i].exptime  	    = in[i].exptime;
    out[i].sidtime  	    = in[i].sidtime;
    out[i].latitude  	    = in[i].latitude;
    out[i].detection_limit  = in[i].detection_limit;
    out[i].saturation_limit = in[i].saturation_limit;
    out[i].cerror	    = in[i].cerror;
    out[i].fwhm_x	    = in[i].fwhm_x;
    out[i].fwhm_y	    = in[i].fwhm_y;
    out[i].trate	    = in[i].trate;
    out[i].ccdnum	    = in[i].ccdnum;

    out[i].order	    = in[i].order;
    out[i].Mx		    = in[i].Mx;
    out[i].My		    = in[i].My;
    out[i].Mxx		    = in[i].Mxx;
    out[i].Mxy		    = in[i].Mxy;
    out[i].Myy		    = in[i].Myy;
    out[i].Mxxx		    = in[i].Mxxx;
    out[i].Mxxy		    = in[i].Mxxy;
    out[i].Mxyy		    = in[i].Mxyy;
    out[i].Myyy		    = in[i].Myyy;
    out[i].Mxxxx	    = in[i].Mxxxx;
    out[i].Mxxxy	    = in[i].Mxxxy;
    out[i].Mxxyy	    = in[i].Mxxyy;
    out[i].Mxyyy	    = in[i].Mxyyy;
    out[i].Myyyy	    = in[i].Myyyy;

    // changed or added for PS1_DEV_1
    out[i].imageID_hi	    = 0;
    out[i].imageID_lo	    = in[i].imageID;

    // changed or added for PS1_V1
    out[i].code		    = in[i].flags;
  }
  return (out);
}

// XXX no photcode conversions?  this may be from before we had a photcode FITS table...

