# include <dvo.h>

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

  int i;
  Measure *out;

  ALLOCATE (out, Measure, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR_PS   = in[i].dR * 0.01;
    out[i].dD_PS   = in[i].dD * 0.01;
    out[i].M_PS    = in[i].M  * 0.001;
    out[i].dM_PS   = in[i].dM * 0.001;
    out[i].Mcal_PS = in[i].Mcal * 0.001;
    out[i].dophot  = in[i].dophot;
    out[i].source  = in[i].source;
    out[i].t       = in[i].t;

    /* flags and averef are now split */
    out[i].averef  =  in[i].averef & 0x00ffffff;
    out[i].flags   = (in[i].averef & 0xff000000) >> 24;

    /* these values don't exist in the Loneos format */
    out[i].Mgal_PS    = in[i].M;
    out[i].dt_PS      = 0;
    out[i].airmass_PS = 0;
    out[i].FWx 	      = 0;
    out[i].FWy 	      = 0;
    out[i].theta      = 0;
  }
  return (out);
}

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

  int i;
  MeasureLoneos *out;

  ALLOCATE (out, MeasureLoneos, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].dR 	  = in[i].dR_PS * 100.0;
    out[i].dD 	  = in[i].dD_PS * 100.0;
    out[i].M  	  = in[i].M_PS  * 1000.0;
    out[i].dM 	  = in[i].dM_PS * 1000.0;
    out[i].Mcal   = in[i].Mcal_PS * 1000.0;
    out[i].dophot = in[i].dophot;
    out[i].source = in[i].source;
    out[i].t      = in[i].t;

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

/* convert loneos-format averages to internal averages */
Average *AverageLoneosToInternal (AverageLoneos *in, int Nvalues) {

  int 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].M       = in[i].M * 0.001;      
    out[i].Xp      = in[i].Xp;     
    out[i].Xm      = in[i].Xm;     
    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 Loneos */
    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].dM      = 0xffff;
    out[i].Xg      = 0xffff;
  }
  return (out);
}

/* convert internal averages to loneos-format averages */
AverageLoneos *AverageInternalToLoneos (Average *in, int Nvalues) {

  int i;
  AverageLoneos *out;

  ALLOCATE (out, AverageLoneos, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].R       = in[i].R;      
    out[i].D       = in[i].D;      
    out[i].M       = in[i].M * 1000.0;      
    out[i].Xp      = in[i].Xp;     
    out[i].Xm      = in[i].Xm;     
    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;
  }
  return (out);
}

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

  int i;
  SecFilt *out;

  ALLOCATE (out, SecFilt, Nvalues);

  for (i = 0; i < Nvalues; i++) {
    out[i].M_PS  = in[i].M*0.001;      
    out[i].dM_PS = 0xffff;
    out[i].Xm    = in[i].Xm;      
  }
  return (out);
}

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

  int i;
  SecFiltLoneos *out;

  ALLOCATE (out, SecFiltLoneos, Nvalues);

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

/* convert loneos-format images to internal images */
Image *ImageLoneosToInternal (ImageLoneos *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_PS    	    = in[i].secz * 0.001;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit_PS  	    = in[i].apmifit * 0.001;
    out[i].dapmifit_PS 	    = in[i].dapmifit * 0.001;
    out[i].Mcal_PS	    = in[i].Mcal * 0.001;
    out[i].dMcal_PS    	    = in[i].dMcal * 0.001;
    out[i].Xm	    	    = in[i].Xm;
    out[i].source   	    = 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;
  }
  return (out);
}

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

  int i;
  ImageLoneos *out;

  ALLOCATE (out, ImageLoneos, 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_PS * 1000.0;
    out[i].NX	    	    = in[i].NX;
    out[i].NY	    	    = in[i].NY;
    out[i].apmifit  	    = in[i].apmifit_PS * 1000.0;
    out[i].dapmifit 	    = in[i].dapmifit_PS * 1000.0;
    out[i].Mcal	    	    = in[i].Mcal_PS * 1000.0;
    out[i].dMcal    	    = in[i].dMcal_PS * 1000.0;
    out[i].Xm	    	    = in[i].Xm;
    out[i].source   	    = 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;
  }
  return (out);
}
