# include <dvo.h>

/* convert elixir-format measures to internal measures */
Measure *Measure_Elixir_ToInternal (Measure_Elixir *in, int Nvalues) {

  int i;
  Measure *out;

  ALLOCATE (out, Measure, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR       = in[i].dR * 0.01;
    out[i].dD       = in[i].dD * 0.01;
    out[i].M        = in[i].M  * 0.001;
    out[i].dM       = in[i].dM * 0.001;
    out[i].dt       = in[i].dt * 0.001;
    out[i].Mcal     = in[i].Mcal * 0.001;
    out[i].Mgal     = in[i].Mgal * 0.001;
    out[i].airmass  = in[i].airmass * 0.001;
    out[i].FWx 	    = in[i].FWx;
    out[i].FWy 	    = in[i].fwy * in[i].FWx * 0.01;
    out[i].theta    = in[i].theta*(0x10000 / 0x100);
    out[i].dophot   = in[i].dophot;
    out[i].photcode = in[i].source;
    out[i].t        = in[i].t;
    out[i].averef   = in[i].averef;
    out[i].dbFlags  = in[i].flags;
    
    /* these can be determined if needed / desired */
    out[i].Xccd     = 0;
    out[i].Yccd     = 0;
    out[i].dXccd    = 0;
    out[i].dYccd    = 0;

    /* these do not have a corresponding value */
    out[i].az        = 0;
    out[i].stargal   = 0;
    out[i].Sky       = 0;
    out[i].dSky      = 0;
    out[i].qPSF      = 0;
    out[i].psfProb   = 0;
    out[i].crNsigma  = 0;
    out[i].extNsigma = 0;
    out[i].photFlags = 0;

    /* XXX add these later */
    out[i].detID = 0;
    out[i].imageID = 0;
  }
  return (out);
}

/* convert internal measures to elixir-format measures */
Measure_Elixir *MeasureInternalTo_Elixir (Measure *in, int Nvalues) {

  int i;
  Measure_Elixir *out;

  ALLOCATE (out, Measure_Elixir, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR      = in[i].dR * 100.0;
    out[i].dD 	   = in[i].dD * 100.0;
    out[i].M       = in[i].M  * 1000.0;
    out[i].dM      = in[i].dM * 1000.0;
    out[i].dt      = in[i].dt * 1000.0;
    out[i].Mcal    = in[i].Mcal * 1000.0;
    out[i].Mgal    = in[i].Mgal * 1000.0;
    out[i].airmass = in[i].airmass * 1000.0;
    out[i].FWx 	   = in[i].FWx;
    out[i].fwy 	   = in[i].FWy * 100.0 / in[i].FWx;
    out[i].theta   = in[i].theta*(0x100/ 0x10000);
    out[i].dophot  = in[i].dophot;
    out[i].source  = in[i].photcode;
    out[i].t       = in[i].t;
    out[i].averef  = in[i].averef;
    out[i].flags   = in[i].dbFlags;
  }
  return (out);
}

/* convert elixir-format averages to internal averages */
Average *Average_Elixir_ToInternal (Average_Elixir *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;     
    out[i].Nm      = in[i].Nm;     
    out[i].Nn      = in[i].Nn;     
    out[i].code    = in[i].code;   
    out[i].offset  = in[i].offset; 
    out[i].missing = in[i].missing;

    /* these don't exist in Elixir */
    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;

    /* XXX add these later */
    out[i].objID_hi = 0;
    out[i].objID_lo = 0;

    primary[0][i].M  = in[i].M  * 0.001;      
    primary[0][i].dM = in[i].dM * 0.001;      
    primary[0][i].Xm = in[i].Xm;     
    primary[0][i].Ncode = 0;     
    primary[0][i].Nused = 0;

    // XXX drop this or keep this?
    // primary[0][i].Xg = in[i].Xg;
  }
  return (out);
}

/* convert internal averages to elixir-format averages */
Average_Elixir *AverageInternalTo_Elixir (Average *in, int Nvalues, SecFilt *primary) {

  int i;
  Average_Elixir *out;

  ALLOCATE (out, Average_Elixir, 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].Nm      = in[i].Nm;     
    out[i].Nn      = in[i].Nn;     
    out[i].code    = in[i].code;   
    out[i].offset  = in[i].offset; 
    out[i].missing = in[i].missing;

    out[i].M       = primary[i].M  * 1000.0;      
    out[i].dM      = primary[i].dM * 1000.0;
    out[i].Xm      = primary[i].Xm;     
    // out[i].Xg      = in[i].Xg;
  }
  return (out);
}

/* convert elixir-format secfilts to internal secfilts */
SecFilt *SecFilt_Elixir_ToInternal (SecFilt_Elixir *in, int Nvalues) {

  int i;
  SecFilt *out;

  ALLOCATE (out, SecFilt, Nvalues);

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

/* convert internal secfilts to elixir-format secfilts */
SecFilt_Elixir *SecFiltInternalTo_Elixir (SecFilt *in, int Nvalues) {

  int i;
  SecFilt_Elixir *out;

  ALLOCATE (out, SecFilt_Elixir, Nvalues);

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

/* convert elixir-format images to internal images */
Image *Image_Elixir_ToInternal (Image_Elixir *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));
    memcpy (out[i].dummy, in[i].dummy, 20);
    strcpy (out[i].name, in[i].name);

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].secz    	    = in[i].secz * 0.001;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit  	    = in[i].apmifit * 0.001;
    out[i].dapmifit 	    = in[i].dapmifit * 0.001;
    out[i].Mcal   	    = in[i].Mcal * 0.001;
    out[i].dMcal   	    = in[i].dMcal * 0.001;
    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].code		    = in[i].code;
    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;

    /* XXX add these later */
    out[i].imageID = 0;
  }
  return (out);
}

/* convert internal images to elixir-format images */
Image_Elixir *ImageInternalTo_Elixir (Image *in, int Nvalues) {

  int i;
  Image_Elixir *out;

  ALLOCATE (out, Image_Elixir, Nvalues);

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

    out[i].tzero    	    = in[i].tzero;
    out[i].nstar    	    = in[i].nstar;
    out[i].secz	    	    = in[i].secz * 1000.0;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit  	    = in[i].apmifit * 1000.0;
    out[i].dapmifit 	    = in[i].dapmifit * 1000.0;
    out[i].Mcal	    	    = in[i].Mcal * 1000.0;
    out[i].dMcal    	    = in[i].dMcal * 1000.0;
    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].code		    = in[i].code;
    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;
  }
  return (out);
}
