# include <dvo.h>

/* convert loneos-format measures to internal measures */
Measure *Measure_Loneos_ToInternal (Measure_Loneos *in, int Nvalues) {

  int i;
  Measure *out;

  ALLOCATE (out, Measure, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].t        = in[i].t;

    // added or changed for ELIXIR
    out[i].averef    =  in[i].averef & 0x00ffffff;
    out[i].dbFlags   = (in[i].averef & 0xff000000) >> 24;
    out[i].t         = in[i].t;
    out[i].dt        = 0;
    out[i].airmass   = 0;
    out[i].FWy 	     = 0;
    out[i].FWx       = 0;
    out[i].theta     = 0;

    // changed for PANSTARRS_DEV_0
    out[i].dR       = (in[i].dR      == NAN_S_SHORT) ? NAN : in[i].dR     * 0.01;
    out[i].dD       = (in[i].dD      == NAN_S_SHORT) ? NAN : in[i].dD     * 0.01;
    out[i].M        = (in[i].M       == NAN_S_SHORT) ? NAN : in[i].M      * 0.001;
    out[i].dM       = (in[i].dM      == NAN_U_CHAR)  ? NAN : in[i].dM     * 0.001;
    out[i].Mcal     = (in[i].Mcal    == NAN_S_SHORT) ? NAN : in[i].Mcal   * 0.001;
    out[i].Map      = (in[i].M       == NAN_S_SHORT) ? NAN : in[i].M      * 0.001;
    out[i].photcode = in[i].source;
    
    // added for PANSTARRS_DEV_0
    out[i].Xccd      = 0;  // determine on-the-fly
    out[i].Yccd      = 0;  // determine on-the-fly
    out[i].dXccd     = 0;
    out[i].dYccd     = 0;
    out[i].az        = 0;
    out[i].Sky       = 0;
    out[i].dSky      = 0;
    out[i].psfQual   = 0;
    out[i].psfChisq  = 0;
    out[i].crNsigma  = 0;
    out[i].extNsigma = 0;
    out[i].detID     = 0;  // determine on-the-fly
    out[i].imageID   = 0;  // determine on-the-fly

    // changed or added for PS1_DEV_1 (2008.02.26)
    out[i].detID      = 0;
    out[i].imageID    = 0;

    // changed or added for PS1_DEV_2
    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 loneos-format measures */
Measure_Loneos *MeasureInternalTo_Loneos (Measure *in, int Nvalues) {

  int i;
  Measure_Loneos *out;

  ALLOCATE (out, Measure_Loneos, Nvalues);

  for (i = 0; i < Nvalues; i++) {

    // changed for PANSTARRS_DEV_0
    out[i].dR     = isnan(in[i].dR     ) ? NAN_S_SHORT : in[i].dR      *  100.0;
    out[i].dD     = isnan(in[i].dD     ) ? NAN_S_SHORT : in[i].dD      *  100.0;
    out[i].M      = isnan(in[i].M      ) ? NAN_S_SHORT : in[i].M       * 1000.0;
    out[i].dM     = isnan(in[i].dM     ) ? NAN_U_CHAR  : in[i].dM      * 1000.0;
    out[i].Mcal   = isnan(in[i].Mcal   ) ? NAN_S_SHORT : in[i].Mcal    * 1000.0;
    out[i].source = in[i].photcode;
    out[i].t      = in[i].t;

    /* flags and averef are merged in Loneos */
    out[i].averef =  (in[i].averef & 0x00ffffff) | (in[i].dbFlags << 24);

    // changed or added for PS1_V1
    out[i].dophot  = in[i].photFlags >> 16;
  }
  return (out);
}

/* convert loneos-format averages to internal averages */
Average *Average_Loneos_ToInternal (Average_Loneos *in, int Nvalues, SecFilt **primary) {

  int i;
  Average *out;

  ALLOCATE (out, Average, Nvalues);
  ALLOCATE (*primary, SecFilt, 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;     

    // added for ELIXIR
    primary[0][i].dM    = NAN;

    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
    primary[0][i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M  * 0.001;      
    primary[0][i].Xm    = in[i].Xm;     
    primary[0][i].Ncode = 0;     
    primary[0][i].Nused = 0;

    // added for PANSTARRS_DEV_0
    out[i].dR      = 0;
    out[i].dD      = 0;
    out[i].uR      = 0;
    out[i].uD      = 0;
    out[i].duR     = 0;
    out[i].duD     = 0;
    out[i].P       = 0;
    out[i].dP      = 0;
    out[i].objID   = 0; // determine on-the-fly
    out[i].catID   = 0; // determine on-the-fly

    // 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].ChiSq       	 = 0.0;
    out[i].Npos       	 = 0.0;
    out[i].extID 	 = 0;
  }
  return (out);
}

/* convert internal averages to loneos-format averages */
Average_Loneos *AverageInternalTo_Loneos (Average *in, int Nvalues, SecFilt *primary) {

  int i;
  Average_Loneos *out;

  ALLOCATE (out, Average_Loneos, 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;     

    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
    out[i].M       = isnan(primary[i].M)  ? NAN_S_SHORT : primary[i].M   * 1000.0;
    out[i].Xm      = primary[i].Xm;     

    // 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 loneos-format secfilts to internal secfilts */
SecFilt *SecFilt_Loneos_ToInternal (SecFilt_Loneos *in, int Nvalues) {

  int i;
  SecFilt *out;

  ALLOCATE (out, SecFilt, Nvalues);

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

    // added for ELIXIR
    out[i].dM    = NAN;

    // added or changed for PANSTARRS_DEV_0
    out[i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M * 0.001;
    out[i].Ncode = 0;
    out[i].Nused = 0;

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

/* convert internal secfilts to loneos-format secfilts */
SecFilt_Loneos *SecFiltInternalTo_Loneos (SecFilt *in, int Nvalues) {

  int i;
  SecFilt_Loneos *out;

  ALLOCATE (out, SecFilt_Loneos, Nvalues);

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

    // added or changed for PANSTARRS_DEV_0
    out[i].M    = isnan(in[i].M)  ? NAN_S_SHORT : in[i].M * 1000.0;
  }
  return (out);
}

/* convert loneos-format images to internal images */
Image *Image_Loneos_ToInternal (Image_Loneos *in, int Nvalues) {

  int 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, 31); // in[32], out[128]
    out[i].name[31] = 0; // force termination

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;

    out[i].Xm	    	    = in[i].Xm;
    out[i].photcode   	    = in[i].source;
    out[i].exptime  	    = in[i].exptime;
    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;

    // added or changed for PANSTARRS_DEV_0
    out[i].secz    	    = (in[i].secz     == NAN_S_SHORT) ? NAN : in[i].secz     * 0.001;
    out[i].apmifit  	    = (in[i].apmifit  == NAN_S_SHORT) ? NAN : in[i].apmifit  * 0.001;
    out[i].dapmifit  	    = (in[i].dapmifit == NAN_S_SHORT) ? NAN : in[i].dapmifit * 0.001;
    out[i].Mcal   	    = (in[i].Mcal     == NAN_S_SHORT) ? NAN : in[i].Mcal     * 0.001;
    out[i].dMcal   	    = (in[i].dMcal    == NAN_S_SHORT) ? NAN : in[i].dMcal    * 0.001;
    out[i].sidtime  	    = NAN;
    out[i].latitude  	    = NAN;
    out[i].imageID          = 0; // determine on-the-fly

    // 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;
  }
  return (out);
}

/* convert internal images to loneos-format images */
Image_Loneos *ImageInternalTo_Loneos (Image *in, int Nvalues) {

  int i;
  Image_Loneos *out;

  ALLOCATE (out, Image_Loneos, Nvalues);

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

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

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;

    out[i].Xm	    	    = in[i].Xm;
    out[i].source   	    = in[i].photcode;
    out[i].exptime  	    = in[i].exptime;
    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;

    // added or changed for PANSTARRS_DEV_0
    out[i].secz    	    = isnan(in[i].secz    ) ? NAN_S_SHORT : in[i].secz     * 1000.0;
    out[i].apmifit    	    = isnan(in[i].apmifit ) ? NAN_S_SHORT : in[i].apmifit  * 1000.0;
    out[i].dapmifit    	    = isnan(in[i].dapmifit) ? NAN_S_SHORT : in[i].dapmifit * 1000.0;
    out[i].Mcal    	    = isnan(in[i].Mcal    ) ? NAN_S_SHORT : in[i].Mcal     * 1000.0;
    out[i].dMcal    	    = isnan(in[i].dMcal   ) ? NAN_S_SHORT : in[i].dMcal    * 1000.0;

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