# include "relphot.h"

// array of mosaic definition structures
static off_t  Nmosaic;
static Mosaic *mosaic;

// list of all images associated with a mosaic
static off_t   *Nimlist;
static off_t   **imlist; /* mosaic -> image[] */
static off_t   **bin;    /* catalog, measure -> mosaic */

// list of mosaic associated with each image  
static int     **clist;  /* mosaic -> catalog[] */
static off_t   **mlist;  /* mosiac -> measure[] */
static off_t    *Nlist;
static off_t    *NLIST;

/* find mosaic frames (unique time periods & photcode name matches mosaic) */
void initMosaics (Image *image, off_t Nimage) {

  off_t i, j, status, found, NMOSAIC, *NIMLIST;
  unsigned int start, stop;
  char *pname;

  if (!MOSAIC_ZEROPT) return;

  Nmosaic = 0;
  NMOSAIC = 10;
  ALLOCATE (mosaic, Mosaic, NMOSAIC);

  ALLOCATE (imlist,  off_t *, NMOSAIC);
  ALLOCATE (Nimlist, off_t,   NMOSAIC);
  ALLOCATE (NIMLIST, off_t,   NMOSAIC);

  /* a 'mosaic' in relphot is (unlike relastro) a virtual concept: there is no 
   * entry in the image table that represents this mosaic.  Instead, it is an
   * internal construct that defines a group of related images 
   */

  /* generate list of unique mosaics */
  for (i = 0; i < Nimage; i++) {

    /* select valid mosaic images by photcode */
    pname = GetPhotcodeNamebyCode (image[i].photcode);
    status = strncmp (pname, MOSAICNAME, strlen (MOSAICNAME));
    if (status) continue;

    /* set image time range */
    start = image[i].tzero - MAX(0.01*image[i].trate*image[i].NY, 1);
    stop  = image[i].tzero + MAX(1.01*image[i].trate*image[i].NY, 1);

    /* find existing mosaic with this time range */
    found = FALSE;
    for (j = 0; !found && (j < Nmosaic); j++) { 
      if (stop  < mosaic[j].start) continue;
      if (start > mosaic[j].stop)  continue;
      found = TRUE;

      /* add image to mosaic image list */
      imlist[j][Nimlist[j]] = i;
      Nimlist[j] ++;
      if (Nimlist[j] == NIMLIST[j]) {
	NIMLIST[j] += 10;
	REALLOCATE (imlist[j], off_t, NIMLIST[j]);
      }

    }
    if (found) continue;
    
    /* a new mosaic, define ranges */
    mosaic[Nmosaic].start = start;
    mosaic[Nmosaic].stop  = stop;
    mosaic[Nmosaic].Mcal  = 0.0;
    mosaic[Nmosaic].dMcal = 0.0;
    mosaic[Nmosaic].Xm    = 0.0;
    mosaic[Nmosaic].flags  = image[i].flags;
    mosaic[Nmosaic].secz  = image[i].secz;

    /* add image to mosaic image list */
    NIMLIST[Nmosaic] = 10;
    Nimlist[Nmosaic] = 1;
    ALLOCATE (imlist[Nmosaic], off_t, NIMLIST[Nmosaic]);
    imlist[Nmosaic][0] = i;

    Nmosaic ++;
    if (Nmosaic == NMOSAIC) {
      NMOSAIC += 10;
      REALLOCATE (mosaic,  Mosaic,  NMOSAIC);
      REALLOCATE (imlist,  off_t *, NMOSAIC);
      REALLOCATE (Nimlist, off_t,   NMOSAIC);
      REALLOCATE (NIMLIST, off_t,   NMOSAIC);
    }
  }

  initMosaicGrid (image, Nimage);
  return;
}

void initMosaicGrid (Image *image, off_t Nimage) {

  /* find max dR, dD range for all mosaics */
  /* define mosaic.coords to cover dR, dD */
  /* send results to initGridBins */

  off_t i, j, m, NX, NY;
  int dXmax, dYmax;
  double dS, dX, dY;
  double R, D, Rmin, Rmax, Dmin, Dmax;
  double Mcal, dMcal, Xm;

  dXmax = dYmax = 0.0;
  for (i = 0; i < Nmosaic; i++) {
    Dmin = Rmin =  360.0;
    Dmax = Rmax = -360.0;
    dS = 0.0;
    Mcal = dMcal = Xm = 0;
    for (j = 0; j < Nimlist[i]; j++) {
      m = imlist[i][j];
      NX = image[m].NX;
      NY = image[m].NY;
      dS += hypot (image[m].coords.cdelt1*image[m].coords.pc1_1, image[m].coords.cdelt1*image[m].coords.pc2_1);
      XY_to_RD (&R, &D, 0.0, 0.0, &image[m].coords);
      Rmin = MIN (Rmin, R);
      Rmax = MAX (Rmax, R);
      Dmin = MIN (Dmin, D);
      Dmax = MAX (Dmax, D);
      XY_to_RD (&R, &D, (double) NX, 0.0, &image[m].coords);
      Rmin = MIN (Rmin, R);
      Rmax = MAX (Rmax, R);
      Dmin = MIN (Dmin, D);
      Dmax = MAX (Dmax, D);
      XY_to_RD (&R, &D, (double) NX, (double) NY, &image[m].coords);
      Rmin = MIN (Rmin, R);
      Rmax = MAX (Rmax, R);
      Dmin = MIN (Dmin, D);
      Dmax = MAX (Dmax, D);
      XY_to_RD (&R, &D, 0.0, (double) NY, &image[m].coords);
      Rmin = MIN (Rmin, R);
      Rmax = MAX (Rmax, R);
      Dmin = MIN (Dmin, D);
      Dmax = MAX (Dmax, D);
      Mcal  += image[m].Mcal;
      dMcal += image[m].dMcal;
      Xm    += image[m].Xm;
      /* we are using mosaic.Mcal, not image.Mcal. reset image.Mcal */
      image[m].Mcal  = 0.0;
      image[m].dMcal = NAN;
      image[m].Xm    = NAN_S_SHORT;
    }
    dS /= Nimlist[i];
    strcpy (mosaic[i].coords.ctype, "RA---TAN");
    mosaic[i].coords.crval1 = Rmin;
    mosaic[i].coords.crval2 = Dmin;
    mosaic[i].coords.crpix1 = 0.0;
    mosaic[i].coords.crpix2 = 0.0;
    mosaic[i].coords.cdelt1 = dS;
    mosaic[i].coords.cdelt2 = dS;
    mosaic[i].coords.pc1_1  = 1.0;
    mosaic[i].coords.pc2_2  = 1.0;
    mosaic[i].coords.pc1_2  = 0.0;
    mosaic[i].coords.pc2_1  = 0.0;
    RD_to_XY (&dX, &dY, Rmax, Dmax, &mosaic[i].coords);

    mosaic[i].Mcal  = Mcal / Nimlist[i];
    mosaic[i].dMcal = dMcal / Nimlist[i];
    mosaic[i].Xm    = Xm / Nimlist[i];
  }
  if (!USE_GRID) return;

  dXmax = MAX (dXmax, dX);
  dYmax = MAX (dYmax, dY);
  initGrid (dXmax, dYmax);
  return;
}

void setMcalFinal () {

  off_t i, j, im, Nimage;
  Image *image;

  if (!MOSAIC_ZEROPT) return;

  image = getimages (&Nimage);

  for (i = 0; i < Nmosaic; i++) {
    for (j = 0; j < Nimlist[i]; j++) {
      im = imlist[i][j];
      image[im].Mcal = mosaic[i].Mcal;
      image[im].dMcal = mosaic[i].dMcal;
      image[im].Xm = mosaic[i].Xm;
      image[im].flags |= (mosaic[i].flags & ID_IMAGE_FEW);
      image[im].flags |= (mosaic[i].flags & ID_IMAGE_POOR);
    }
  }      
}

void initMosaicBins (Catalog *catalog, int Ncatalog) {

  off_t i, j;

  /* measure -> mosaic */
  if (!MOSAIC_ZEROPT) return;

  ALLOCATE (bin, off_t *, Ncatalog);
  for (i = 0; i < Ncatalog; i++) {
    ALLOCATE (bin[i], off_t, MAX (catalog[i].Nmeasure, 1));
    for (j = 0; j < catalog[i].Nmeasure; j++) bin[i][j] = -1;
  }

  /* mosaic -> measure */
  ALLOCATE (Nlist, off_t,   Nmosaic);
  ALLOCATE (NLIST, off_t,   Nmosaic);
  ALLOCATE (clist, int *,   Nmosaic);
  ALLOCATE (mlist, off_t *, Nmosaic);

  for (i = 0; i < Nmosaic; i++) {
    Nlist[i] = 0;
    NLIST[i] = 100;
    ALLOCATE (clist[i], int,   NLIST[i]);
    ALLOCATE (mlist[i], off_t, NLIST[i]);
  }
}

void freeMosaicBins (int Ncatalog) {

  off_t i;

  /* measure -> mosaic */
  if (!MOSAIC_ZEROPT) return;

  for (i = 0; i < Ncatalog; i++) {
    free (bin[i]);
  }
  free (bin);

  /* mosaic -> measure */
  for (i = 0; i < Nmosaic; i++) {
    free (clist[i]);
    free (mlist[i]);
  }
  free (Nlist);
  free (NLIST);
  free (clist);
  free (mlist);
}

int findMosaics (Catalog *catalog, int Ncatalog) {
  
  int i, ecode;
  off_t j;

  if (!MOSAIC_ZEROPT) return (FALSE);

  for (i = 0; i < Ncatalog; i++) {
    for (j = 0; j < catalog[i].Nmeasure; j++) {
      if (TimeSelect) {
	if (catalog[i].measure[j].t < TSTART) continue;
	if (catalog[i].measure[j].t > TSTOP) continue;
      }
      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
      if (photcode[0].code != ecode) continue;
      matchMosaics (catalog, j, i);
    }
  }
  return (TRUE);
}

void matchMosaics (Catalog *catalog, off_t meas, int cat) {

  int i;

  for (i = 0; i < Nmosaic; i++) {
    if (catalog[cat].measure[meas].t < mosaic[i].start) continue;
    if (catalog[cat].measure[meas].t > mosaic[i].stop) continue;
    
# ifdef GRID_V1
    if (USE_GRID) {
      ave = catalog[cat].measure[meas].averef;
      ra  = catalog[cat].average[ave].R - catalog[cat].measure[meas].dR / 3600.0;
      dec = catalog[cat].average[ave].D - catalog[cat].measure[meas].dD / 3600.0;

      /* X,Y always positive-definite in range 0,0 - dX, dY */
      RD_to_XY (&X, &Y, ra, dec, &mosaic[i].coords);
      setGridMeasure (meas, cat, X, Y);
    }
# endif

    bin[cat][meas] = i;

    clist[i][Nlist[i]] = cat;
    mlist[i][Nlist[i]] = meas;
    Nlist[i] ++;
    
    if (Nlist[i] == NLIST[i]) {
      NLIST[i] += 100;
      REALLOCATE (clist[i], int,   NLIST[i]);
      REALLOCATE (mlist[i], off_t, NLIST[i]);
    }	
    return;
  }
  fprintf (stderr, "missed measurement\n");
  return;
}

float getMmos (off_t meas, int cat) {

  off_t i;
  float value;

  if (!MOSAIC_ZEROPT) return (0);

  i = bin[cat][meas];
  if (i == -1) return (NAN);

  if (mosaic[i].flags & IMAGE_BAD) return (NAN);  
  value = mosaic[i].Mcal;
  return (value);
}

int setMmos (Catalog *catalog, int PoorImages) {

  off_t i, j, m, c, N, Nmax;
  int n, mark, bad, Nfew, Nbad, Ncal, Nrel, Ngrid, Nsys;
  float Msys, Mrel, Mcal, Mgrid;
  double *list, *dlist, *Mlist, *dMlist;
  StatType stats;
  Image *image;

  if (!MOSAIC_ZEROPT) return (FALSE);
  if (FREEZE_MOSAICS) return (FALSE);

  image = getimages (&N);

  if (PoorImages) {
    IMAGE_BAD = 0;
  }

  Nmax = 0;
  for (i = 0; i < Nmosaic; i++) {
    Nmax = MAX (Nmax, Nlist[i]);
  }
  ALLOCATE (list, double, Nmax);
  ALLOCATE (dlist, double, Nmax);
  ALLOCATE (Mlist, double, Nmax);
  ALLOCATE (dMlist, double, Nmax);

  Nfew = Nbad = Ncal = Nrel = Ngrid = Nsys = 0;

  for (i = 0; i < Nmosaic; i++) {
    
    /* on PoorImages run, skip good images */
    if (PoorImages) {
      bad = mosaic[i].flags & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
      if (!bad) continue;
    }      

    N = 0;
    for (j = 0; j < Nlist[i]; j++) {
      
      m = mlist[i][j];
      c = clist[i][j];
      
      if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
	  Nbad ++;
	  continue;
      }
      Mcal  = getMcal  (m, c);
      if (isnan(Mcal)) {
	  Ncal++;
	  continue;
      }
      Mgrid = getMgrid (m, c);
      if (isnan(Mgrid)) {
	  Ngrid ++;
	  continue;
      }
      Mrel  = getMrel  (catalog, m, c);
      if (isnan(Mrel)) {
	  Nrel ++;
	  continue;
      }
      
      n = catalog[c].measure[m].averef;
      Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
      if (isnan(Msys)) {
	Nsys++;
	continue;
      }
      list[N]  = Msys - Mrel - Mcal - Mgrid;
      dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
      Mlist[N] = Msys;
      dMlist[N] = list[N];
      N++;
    }
    /* Nlist[i] is all measurements, N is good measurements */

    /* too few good measurements or too many bad measurements (skip in PoorImages run) */

    if (!PoorImages) {
      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
      if (mark) {
	fprintf (stderr, "marked mosaic %s (%lld), (%lld < %d) || (%lld < %f*%lld)\n", image[imlist[i][0]].name, (long long) i, (long long) N, IMAGE_TOOFEW, (long long) N, IMAGE_GOOD_FRACTION, (long long) Nlist[i]);
	mosaic[i].flags |= ID_IMAGE_FEW;
	Nfew ++;
      } else {
	mosaic[i].flags &= ~ID_IMAGE_FEW;
      }
    }
    liststats (list, dlist, N, &stats);
    if (PoorImages) fprintf (stderr, "Mmos: %f %f %d %lld\n", stats.mean, stats.sigma, stats.Nmeas, (long long) N);
    mosaic[i].Mcal  = stats.mean;
    mosaic[i].dMcal = stats.sigma;
    mosaic[i].Xm    = 100.0*log10(stats.chisq);
  }
  free (list);
  free (dlist);
  free (Mlist);
  free (dMlist);

  fprintf (stderr, "%d mosaics marked having too few measurements (Nbad: %d, Ncal: %d, Ngrid: %d, Nrel: %d, Nsys: %d)\n", Nfew, Nbad, Ncal, Ngrid, Nrel, Nsys);

  if (PoorImages) {
    IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
  }
  return (TRUE);
}
  
StatType statsMosaicM (Catalog *catalog) {

  off_t i;
  int n;
  double *list, *dlist;
  StatType stats;

  bzero (&stats, sizeof (StatType));
  if (!MOSAIC_ZEROPT) return (stats);

  ALLOCATE (list, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);

  n = 0;
  for (i = 0; i < Nmosaic; i++) {
    if (mosaic[i].flags & IMAGE_BAD) continue;
    list[n] = mosaic[i].Mcal;
    dlist[n] = 1;
    n++;
  }

  liststats (list, dlist, n, &stats);
  free (list);
  free (dlist);
  return (stats);
}

StatType statsMosaicdM (Catalog *catalog) {

  off_t i, n;
  double *list, *dlist;
  StatType stats;

  bzero (&stats, sizeof (StatType));
  if (!MOSAIC_ZEROPT) return (stats);
  if (FREEZE_MOSAICS) return (stats); 

  ALLOCATE (list, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);

  n = 0;
  for (i = 0; i < Nmosaic; i++) {

    if (mosaic[i].flags & IMAGE_BAD) continue;
    list[n] = mosaic[i].dMcal;
    dlist[n] = 1;
    n++;
  }

  liststats (list, dlist, n, &stats);
  free (list);
  free (dlist);
  return (stats);
}

StatType statsMosaicN (Catalog *catalog) {

  off_t i, j, m, c, n, N;
  double *list, *dlist;
  float Mcal, Mgrid, Mrel;
  StatType stats;

  bzero (&stats, sizeof (StatType));
  if (!MOSAIC_ZEROPT) return (stats);
  if (FREEZE_MOSAICS) return (stats); 

  ALLOCATE (list, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);

  n = 0;
  for (i = 0; i < Nmosaic; i++) {
    if (mosaic[i].flags & IMAGE_BAD)  continue;

    N = 0;
    for (j = 0; j < Nlist[i]; j++) {

      m = mlist[i][j];
      c = clist[i][j];

      Mcal = getMcal  (m, c);
      if (isnan(Mcal)) continue;
      Mgrid = getMgrid (m, c);
      if (isnan(Mgrid)) continue;
      Mrel = getMrel  (catalog, m, c);
      if (isnan(Mrel)) continue;
      N++;
    }
    list[n] = N;
    dlist[n] = 1;
    n++;
  }
  fprintf (stderr, "Nmosaic: %lld, n: %lld\n", (long long) Nmosaic, (long long) n);

  liststats (list, dlist, n, &stats);
  free (list);
  free (dlist);
  return (stats);
}

StatType statsMosaicX (Catalog *catalog) {

  off_t i, n;
  double *list, *dlist;
  StatType stats;

  bzero (&stats, sizeof (StatType));
  if (!MOSAIC_ZEROPT) return (stats);
  if (FREEZE_MOSAICS) return (stats); 

  ALLOCATE (list, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);

  n = 0;
  for (i = 0; i < Nmosaic; i++) {

    if (mosaic[i].flags & IMAGE_BAD) continue;
    list[n] = pow(10.0, 0.01*mosaic[i].Xm);
    dlist[n] = 1;
    n++;
  }

  liststats (list, dlist, n, &stats);
  free (list);
  free (dlist);
  return (stats);
}

/* mark mosaic if: abs(Mcal - <Mcal>) too large, dMcal too large */
void clean_mosaics () {

  off_t i, N, mark, Nmark;
  double *mlist, *slist, *dlist;
  double MaxOffset, MedOffset, MaxScatter;
  StatType stats;

  if (!MOSAIC_ZEROPT) return;
  if (FREEZE_MOSAICS) return;

  if (VERBOSE) fprintf (stderr, "marking poor mosaics\n");

  ALLOCATE (mlist, double, Nmosaic);
  ALLOCATE (slist, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);

  for (i = N = 0; i < Nmosaic; i++) {
    if (mosaic[i].flags & IMAGE_BAD) continue;
    mlist[N] = mosaic[i].Mcal;
    slist[N] = mosaic[i].dMcal;
    dlist[N] = 1;
    N++;
  }
  initstats ("MEAN");
  liststats (mlist, dlist, N, &stats);
  MaxOffset = MAX (IMAGE_OFFSET, 2*stats.sigma);
  MedOffset = stats.median;
  liststats (slist, dlist, N, &stats);
  MaxScatter = MAX (IMAGE_SCATTER, 2*stats.median);
  fprintf (stderr, "Mrel: %f, dMrel: %f, Max Scatter: %f, Max Offset: %f\n", MedOffset, stats.median, MaxScatter, MaxOffset);
  
  Nmark = 0;
  for (i = 0; i < Nmosaic; i++) {
    mark = FALSE;
    mark = (mosaic[i].dMcal > MaxScatter) || (fabs(mosaic[i].Mcal - MedOffset) > MaxOffset);
    if (mark) { 
      Nmark ++;
      mosaic[i].flags |= ID_IMAGE_POOR;
    } else {
      mosaic[i].flags &= ~ID_IMAGE_POOR;
    }
  }

  fprintf (stderr, "%lld mosaics marked poor\n", (long long) Nmark);
  initstats (STATMODE);
  free (mlist);
  free (slist);
  free (dlist);
}

void plot_mosaic_fields (Catalog *catalog) {

  off_t i, j, m, c, N, ave, Nimage;
  double *xlist, *ylist;
  double Xmin, Xmax, Ymin, Ymax;
  char string[64];
  Image *image;
  Graphdata graphdata;

  if (!MOSAIC_ZEROPT) return;

  image = getimages (&Nimage);

  N = 0;
  for (i = 0; i < Nmosaic; i++) 
    N = MAX (N, Nlist[i]);

  ALLOCATE (xlist, double, N);
  ALLOCATE (ylist, double, N);

  for (i = 0; i < Nmosaic; i++) {
    N = 0;
    Xmin = Ymin = +360.0;
    Xmax = Ymax = -360.0;
    for (j = 0; j < Nlist[i]; j++) {
      
      m = mlist[i][j];
      c = clist[i][j];
      
      if (catalog[c].measure[m].dbFlags & (ID_MEAS_AREA | ID_MEAS_NOCAL)) continue;

      ave = catalog[c].measure[m].averef;
      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR / 3600.0;
      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD / 3600.0;
      N++;
    }
  
    sprintf (string, "Mosaic %lld", (long long) i);
    plot_defaults (&graphdata);
    plot_list (&graphdata, xlist, ylist, N, string, NULL);
  }

  free (ylist);
  free (xlist);
}

void plot_mosaics () {

  off_t i, bin;
  double *xlist, *Mlist, *dlist;
  Graphdata graphdata;

  if (!MOSAIC_ZEROPT) return;
  if (FREEZE_MOSAICS) return;

  ALLOCATE (xlist, double, Nmosaic);
  ALLOCATE (dlist, double, Nmosaic);
  ALLOCATE (Mlist, double, Nmosaic);

  for (i = 0; i < Nmosaic; i++) {
    Mlist[i] = mosaic[i].Mcal;
    dlist[i] = mosaic[i].dMcal;
    xlist[i] = mosaic[i].secz;
  }

  plot_defaults (&graphdata);
  graphdata.xmin = 0.95;
  graphdata.xmax = 2.50;
  graphdata.ymin = PlotdMmin;
  graphdata.ymax = PlotdMmax;
  plot_list (&graphdata, xlist, Mlist, Nmosaic, "airmass vs Mcal", "%s.airmass.png", OUTROOT);
  plot_defaults (&graphdata);
  graphdata.size = 1.5;
  graphdata.ptype = 7;
  plot_list (&graphdata, Mlist, dlist, Nmosaic, "Mcal vs dMcal", "%s.MdM.png", OUTROOT);

# define NBIN 200
  REALLOCATE (xlist, double, NBIN);
  REALLOCATE (Mlist, double, NBIN);

  /**** dMcal histgram ****/
  for (i = 0; i < NBIN; i++) xlist[i] = 0.00005*i;
  bzero (Mlist, NBIN*sizeof(double));
  for (i = 0; i < Nmosaic; i++) {
    bin = mosaic[i].dMcal / 0.00005;
    bin = MAX (0, MIN (NBIN - 1, bin));
    Mlist[bin] += 1.0;
  }
  plot_defaults (&graphdata);
  graphdata.style = 1;
  plot_list (&graphdata, xlist, Mlist, NBIN, "dMcal hist", "%s.dMcalhist.png", OUTROOT);

  free (dlist);
  free (xlist);
  free (Mlist);
}

off_t *SelectRefMosaic (Mosaic **refmosaic, off_t *Nimage) {

  off_t i, Imax, Nmax;

  Imax = 0;
  Nmax = Nimlist[0];
  for (i = 0; i < Nmosaic; i++) {
    if (Nimlist[i] > Nmax) {
      Imax = i;
      Nmax = Nimlist[i];
    }
  }

  *refmosaic = &mosaic[Imax];
  *Nimage = Nmax;
  return (imlist[Imax]);
}
