# include "addstar.h"

Image *fakeimage (char *rootname, int *Nimage, int photcode) {

  int i, j, Nx, Ny, Nchips;
  double pltscale, pixscale;
  double Rmin, Rmax, Dmin, Dmax;
  // double Xmin, Xmax, Ymin, Ymax;
  double dX, dY, r, d;
  char chipname[80], chipdata[256], name[80];
  char *config;
  Image *image;
  e_time MosaicTime;

  /* this is a somewhat bogus method to set a time for the exposure */
  struct timeval now;
  long int seedval;

  gettimeofday (&now, NULL);
  seedval = now.tv_sec + now.tv_usec;
  srand48(seedval);

  MosaicTime = 0xffffffff * drand48();
  fprintf (stderr, "time: %x\n", MosaicTime);

  /* load in the camera layout file */
  config = LoadConfigFile (CameraLayout);
  if (config == (char *) NULL) {
    fprintf (stderr, "ERROR: can't find camera layout file %s\n", CameraLayout);
    exit (1);
  }
  if (VERBOSE) fprintf (stderr, "loaded camera layout file: %s\n", CameraLayout);

  /* create a mosaic distortion structure */
  ALLOCATE (MOSAIC, Coords, 1);
  strcpy (MOSAIC[0].ctype, "RA---DIS");
  MOSAIC[0].crval1 = FAKE_RA;
  MOSAIC[0].crval2 = FAKE_DEC;
  
  MOSAIC[0].crpix1 = MOSAIC[0].crpix2 = 0.0;
  
  /* mosaic 'pixels' are millimeters */
  ScanConfig (config, "PLATE_SCALE",   "%lf", 0, &pltscale);
  MOSAIC[0].cdelt1 = MOSAIC[0].cdelt2 = pltscale / 3600.0;

  MOSAIC[0].pc1_1 =  cos(FAKE_THETA*RAD_DEG);
  MOSAIC[0].pc1_2 = -sin(FAKE_THETA*RAD_DEG);
  MOSAIC[0].pc2_1 =  sin(FAKE_THETA*RAD_DEG);
  MOSAIC[0].pc2_2 =  cos(FAKE_THETA*RAD_DEG);

  MOSAIC[0].Npolyterms = 3;
  for (i = 0; i < 7; i++) {
    MOSAIC[0].polyterms[i][0] = 0;
    MOSAIC[0].polyterms[i][1] = 0;
  }
  ScanConfig (config, "DPLATE_X",   "%lf", 0, &pltscale);
  MOSAIC[0].polyterms[3][0] = pltscale;  // L : X^3 Y^0
  MOSAIC[0].polyterms[5][0] = pltscale;  // L : X^1 Y^2
  ScanConfig (config, "DPLATE_Y",   "%lf", 0, &pltscale);
  MOSAIC[0].polyterms[4][1] = pltscale;  // M : X^0 Y^0
  MOSAIC[0].polyterms[6][1] = pltscale;  // M : X^2 Y^0

  /* some basic data about the chisp */
  ScanConfig (config, "NCHIPS", "%d", 0, &Nchips);
  ScanConfig (config, "NAXIS1", "%d", 0, &Nx);
  ScanConfig (config, "NAXIS2", "%d", 0, &Ny);
  ScanConfig (config, "PIXEL_SCALE", "%lf", 0, &pixscale);

  ALLOCATE (image, Image, Nchips + 1);
  
  Rmin = Rmax = Dmin = Dmax = 0;

  /* define the chip images (1 - Nchips) */
  for (i = 0; i < Nchips; i++) {
    /* this is the addstar name for the chip in the camera */
    sprintf (chipname, "CHIP.%03d", i);
    ScanConfig (config, chipname, "%s", 0, chipdata);

    sscanf (chipdata, "%s %lf %lf", chipname, &dX, &dY);
    // if (VERBOSE) fprintf (stderr, "chip %s (%f,%f)\n", chipname, dX, dY);

    /* this is the camera-specific name of a chip */
    sprintf (name, "%s.%s", rootname, chipname);
    strcpy (image[i+1].name, name);

    strcpy (image[i+1].coords.ctype, "RA---WRP");
    
    image[i+1].coords.crval1 = dX*pixscale;
    image[i+1].coords.crval2 = dY*pixscale;
    
    image[i+1].coords.crpix1 = image[i+1].coords.crpix2 = 0.0;
    image[i+1].coords.cdelt1 = image[i+1].coords.cdelt2 = pixscale;

    image[i+1].coords.pc1_1 = 1.0;
    image[i+1].coords.pc1_2 = 0.0;
    image[i+1].coords.pc2_1 = 0.0;
    image[i+1].coords.pc2_2 = 1.0;

    image[i+1].coords.Npolyterms = 0;
    for (j = 0; j < 7; j++) {
      image[i+1].coords.polyterms[j][0] = 0;
      image[i+1].coords.polyterms[j][1] = 0;
    }

    image[i+1].sidtime  = 0.0;
    image[i+1].latitude = 0.0;

    image[i+1].cerror = 0.0;
    
    image[i+1].NX = Nx;
    image[i+1].NY = Ny;

    image[i+1].photcode = photcode;

    image[i+1].exptime = 0.0;
  
    image[i+1].apmifit = 0.0;
    image[i+1].dapmifit = 0.0;

    image[i+1].detection_limit = 0.0;
    image[i+1].saturation_limit = 0.0;
    image[i+1].fwhm_x = 0.0;
    image[i+1].fwhm_y = 0.0;
    image[i+1].tzero = MosaicTime;
    image[i+1].trate = 0;
    image[i+1].secz = 1.0;
    image[i+1].ccdnum = 0xff;

    image[i+1].Mcal = 0.0;
    image[i+1].Xm   = NAN_S_SHORT;
    image[i+1].code = 0;
    memset (image[i+1].dummy, 0, sizeof(image[i+1].dummy));

    image[i+1].nstar = 0;
    image[i+1].Myyyy = 0;

    /* check if chip hits outer bounds of mosaic */
    XY_to_RD (&r, &d, 0, 0, &image[i+1].coords);
    Rmin = MIN (Rmin, r);
    Rmax = MAX (Rmax, r);
    Dmin = MIN (Dmin, d);
    Dmax = MAX (Dmax, d);
    XY_to_RD (&r, &d, Nx, 0, &image[i+1].coords);
    Rmin = MIN (Rmin, r);
    Rmax = MAX (Rmax, r);
    Dmin = MIN (Dmin, d);
    Dmax = MAX (Dmax, d);
    XY_to_RD (&r, &d, 0, Ny, &image[i+1].coords);
    Rmin = MIN (Rmin, r);
    Rmax = MAX (Rmax, r);
    Dmin = MIN (Dmin, d);
    Dmax = MAX (Dmax, d);
    XY_to_RD (&r, &d, Nx, Ny, &image[i+1].coords);
    Rmin = MIN (Rmin, r);
    Rmax = MAX (Rmax, r);
    Dmin = MIN (Dmin, d);
    Dmax = MAX (Dmax, d);
  }

  /* define the mosaic image */
  strcpy (image[0].name, rootname);

  image[0].coords = MOSAIC[0];
  strcpy (image[0].coords.ctype, MOSAIC[0].ctype);

  image[0].sidtime  = 0.0;
  image[0].latitude = 0.0;
  image[0].cerror = 0.0;
    
  // RD_to_XY (&Xmax, &Ymax, Rmax, Dmax, MOSAIC);
  // RD_to_XY (&Xmin, &Ymin, Rmin, Dmin, MOSAIC);
  image[0].NX = Rmax - Rmin;
  image[0].NY = Dmax - Dmin;

  image[0].photcode = photcode;

  image[0].exptime = 0.0;
  image[0].apmifit = 0.0;
  image[0].dapmifit = 0.0;
  image[0].detection_limit = 0.0;
  image[0].saturation_limit = 0.0;
  image[0].fwhm_x = 0.0;
  image[0].fwhm_y = 0.0;
  image[0].tzero = MosaicTime;
  image[0].trate = 0;
  image[0].secz = 1.0;
  image[0].ccdnum = 0xff;
  image[0].Mcal = 0.0;
  image[0].Xm   = NAN_S_SHORT;
  image[0].code = 0;
  memset (image[0].dummy, 0, sizeof(image[0].dummy));
  image[0].nstar = 0;
  image[0].Myyyy = 0;

  *Nimage = Nchips + 1;
  return (image);
}
