Esempio n. 1
0
/*
 * Juho Gävert & Santeri Hiltunen
 Starting point of calculation. Searches for temperature balance in Array for
 maximum iterations of max_iters.
 */
double calculate_heatconduct(Array* arr, unsigned int max_iters)
{

  if (max_iters == 0 || arr->width < 3 || arr->height < 3)
    return -1;

  Array* temp_arr = new_array(arr->width, arr->height);
  copy_array(arr, temp_arr);

  double prev_mean = -1;
  for (unsigned int i = 0; i < max_iters; ++i) {
    double new_mean = calculate_iteration(arr, temp_arr);

    swap_ptrs((void**) &arr, (void**) &temp_arr);

    if (conf.verbose) {
      printf("Iter: %d Mean: %.15f\n", i + 1, new_mean);
      if (conf.verbose > 1) {
        print_arr(arr);
      }
    }

    if (fabs(new_mean - prev_mean) < 0.0000000000001) {
      printf("Found balance after %d iterations.\n", i);
      del_array(temp_arr);
      return new_mean;
    }

    prev_mean = new_mean;
  }
  del_array(temp_arr);
  printf("Didn't find balance after %d iterations.\n", max_iters);
  return prev_mean;
}
Esempio n. 2
0
int find_max_value(int *a, int len)
{
    int i,j;    
    int *ass; 
    int range = len;
    int ass_len = len/2;
    int max_value;
    if(len == 1)
        return a[0];
    if(range % 2 != 0)
    {
        range -= 1;
        ass_len += 1;
    }

    ass = malloc(ass_len * sizeof(int));
    if(ass == NULL)
        return ERROR;
    memset(ass, 0, ass_len * sizeof(int));
    for(i = 0,j = 0; i < range; i+=2)
        ass[j++] = max(a[i], a[i+1]);
    if(range ==  len - 1)
        ass[j++] = a[range];
    max_value = find_max_value(ass, ass_len);
    del_array(&ass);
    return max_value;
}
Esempio n. 3
0
int plot_fit_curve(float *a, int ncoeff, float tmin, float tmax)
{
  int i,j;            /* Looping variables */
  int nstep=500;      /* Number of steps used to compute curve */
  float xtmp,ytmp;    /* Values of x,y used in constructing the curve */
  float *polyx=NULL;  /* Polynomial in x */

  cpgsci(2);
  cpgslw(5);

  /*
   * Allocate memory for container for polynomial values
   */

  if(!(polyx = new_array(ncoeff,1))) {
    fprintf(stderr,"ERROR: plot_fit_curve\n");
    return 1;
  }

  /*
   * Loop through range of x values in step sizes determined by nstep.
   * At each value of x, compute the value of y by first calling poly
   *  to compute the values of the various powers of x at that step, and
   *  then multiplying by the coefficients contained in a.
   */

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

    /*
     * Compute the values of x and y at this step
     */

    xtmp = tmin + i*(tmax - tmin)/(1.0 * nstep);
    sinpoly(xtmp,polyx-1,ncoeff);
    ytmp = 0;
    for(j=0; j<ncoeff; j++)
      ytmp += a[j] * polyx[j];

    /*
     * Now connect this point to the previous one with a cpgdraw call
     */

    if(i == 0)
      cpgmove(xtmp,ytmp);
    else
      cpgdraw(xtmp,ytmp);
  }

  /*
   * Clean up and exit
   */

  cpgslw(1);
  cpgsci(1);
  polyx = del_array(polyx);

  return 0;
}
Esempio n. 4
0
void	try_exec_cmd(t_all *all)
{
	char	**argv_bin;

	all->cmd = ft_epur_str(all->cmd);
	argv_bin = ft_strsplit(all->cmd, ' ');
	exec_right_binary(all, argv_bin);
	del_array(&argv_bin);
}
Esempio n. 5
0
int main(void)
{
    int len = 10;
    int *a;

    int ret = init_array(&a, len);
    printf("print the initial array:\n");
    print_array(a, len);
    printf("max value is %d\n", find_max_value(a, len));
    del_array(&a);

    return 0;
}
Esempio n. 6
0
void test_array_array()
{
  printf("test array array\n");
  Array* array0 = new_array(10);
  Array* array1 = new_array(10);
  Array* array2 = new_array(10);
  Array* array3 = new_array(10);
  Array* array  = new_array(10);
  array_push(array, array0, del_array);
  array_push(array, array1, del_array);
  array_push(array, array2, del_array);
  array_push(array, array3, del_array);
  array_print(array);
  del_array(array);
}
Esempio n. 7
0
void	exec_command(t_all *all)
{
	int		ct;

	ct = 0;
	while (all->cmd2exec[ct])
	{
		ft_strdel(&all->cmd);
		all->cmd = ft_epur_str(all->cmd2exec[ct]);
		if (check_redirection(all->cmd))
			try_redirection_cmd(all);
		else
			if (!try_builtins_cmd(all))
				try_exec_cmd(all);
		ct++;
	}
	del_array(&all->cmd2exec);
}
Esempio n. 8
0
void test_array()
{
  printf("test array\n");
  Array* array = new_array(10);
  Blob* data0  = new_blob(1,2,3,4);
  Blob* data1  = new_blob(2,3,4,5);
  Blob* data2  = new_blob(3,4,5,6);
  Blob* data3  = new_blob(1,2,3,4);
  Blob* data4  = new_blob(2,3,4,5);
  Blob* data5  = new_blob(3,4,5,6);
  Blob* data6  = new_blob(1,2,3,4);
  Blob* data7  = new_blob(2,3,4,5);
  Blob* data8  = new_blob(3,4,5,6);
  Blob* data9  = new_blob(1,2,3,4);
  Blob* data10 = new_blob(2,3,4,5);
  Blob* data11 = new_blob(3,4,5,6);
  array_push(array, data0 , del_blob);
  array_push(array, data1 , del_blob);
  array_push(array, data2 , del_blob);
  array_push(array, data3 , del_blob);
  array_push(array, data4 , del_blob);
  array_push(array, data5 , del_blob);
  array_push(array, data6 , del_blob);
  array_push(array, data7 , del_blob);
  array_push(array, data8 , del_blob);
  array_push(array, data9 , del_blob);
  array_push(array, data10, del_blob);
  array_push(array, data11, del_blob);
  array_print(array);
  int32_t  length  = array->length, i;
  Element* element = array->elements;
  for (i = 0; i < length; ++i)
  {
    blob_print(element->data, false);
    element ++;
    printf("--------------------\n");
  }
  array_clear(array);
  array_print(array);
  del_array(array);
}
Esempio n. 9
0
int plot_resid(Fluxrec *flux, int npoints, float *a, int ncoeff,
	       float *meanrms)
{
  int i,j;              /* Looping variables */
  int no_error=1;       /* Flag set to 0 on error */
  float ytmp;           /* Value of fitted function at a given day */
  float mean;           /* Mean of residual curve */
  float *polyx=NULL;    /* Polynomial in x */
  Fluxrec *resid=NULL;  /* Residual curve */
  Fluxrec *rptr;        /* Pointer to navigate resid */
  Fluxrec *fptr;        /* Pointer to navigate flux */

  /*
   * Allocate memory
   */

  if(!(polyx = new_array(ncoeff,1))) {
    fprintf(stderr,"ERROR: plot_resid\n");
    return 1;
  }

  if(!(resid = new_fluxrec(npoints)))
    no_error = 0;

  /*
   * Loop through flux, at each step compute the value of the function
   *  and then subtract it from the flux.
   */

  if(no_error) {
    for(i=0,fptr=flux,rptr=resid; i<npoints; i++,fptr++,rptr++) {

    /*
     * Compute the function value at this step
     */

    sinpoly(fptr->day,polyx-1,ncoeff);
    ytmp = 0;
    for(j=0; j<ncoeff; j++)
      ytmp += a[j] * polyx[j];

    /*
     * Now set resid->flux to be the difference between flux->flux and the
     *  fitted function
     */

    *rptr = *fptr;
    rptr->flux -= ytmp;
    }
  }

  /*
   * Now plot the points
   */

  if(no_error)
    if(plot_lcurve(resid,npoints,"Days","Residual Flux Density",""))
      no_error = 0;

  /*
   * Draw a line at 0.0 residual
   */

  if(no_error) {
    cpgsci(2);
    cpgslw(5);
    cpgmove(-999,0.0);
    cpgdraw(999,0.0);
    cpgslw(1);
    cpgsci(1);
  }

  /*
   * Calculate mean and rms
   */

  if(no_error) {
    if(calc_mean(resid,npoints,&mean,meanrms))
      no_error = 0;
    else {
      *meanrms /= sqrt(npoints);
      printf("\nplot_resid: Mean of residuals = %8.4f. ",mean);
      printf("RMS uncertainty in mean = %6.4f\n",*meanrms);
    }
  }

  /*
   * Clean up and exit
   */

  cpgslw(1);
  cpgsci(1);
  polyx = del_array(polyx);
  resid = del_fluxrec(resid);

  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: plot_resid\n");
    return 1;
  }
}
Esempio n. 10
0
int main(int argc, char *argv[])
{
  int i;                      /* Looping variable */
  int no_error=1;             /* Flag set to 0 on error */
  int nlines=0;               /* Number of data lines in lens input file */
  int ncompos;                /* Number of points in composite curve */
  int flagbad=1;              /* Flag set to 1 for bad day flagging */
  int errtype=0;              /* 1 ==> include fracrms in errors */
  int ndeg;                   /* Degree of fitting function */
  int nbad[N08];              /* Number of bad days in bad day file */
  int shiftind;               /* Curve excluded from composite */
  float fracrms;              /* Fractional rms scatter in 1634/1635 ratio */
  float tmin,tmax;            /* Min and max times used for fit */
  float meanrms;              /* RMS about mean for residual curve */
  float bestlag;              /* Lag giving lowest reduced chisq */
  float mean[N08];            /* Means of the 4 light curves */
  float *flat=NULL;           /* "Flat field" light curve */
  float *a=NULL;              /* Coefficients of fitting function */
  char line[MAXC];            /* General string for reading input */
  char lensfile[MAXC];        /* Input file name for 1608 data */
  char calfile[MAXC];         /* Input file name for 1634 and 1635 data */
  Fluxrec *fl08[N08]={NULL};  /* Raw 1608 light curves */
  Fluxrec *ff08[N08]={NULL};  /* Flat-fielded 1608 light curves */
  Fluxrec *fl34=NULL;         /* 1634 light curve */
  Fluxrec *fl35=NULL;         /* 1635 light curve */
  Fluxrec *compos=NULL;       /* Composite curve created from 3 light curves */
  FILE *lfp=NULL;             /* 1608 data file pointer */
  FILE *cfp=NULL;             /* 1634/1635 data file pointer */

  /*
   * Check input line
   */
  if(argc < 3) {
    fprintf(stderr,"\nUsage: fit_1_to_3 [1608 filename] [1634/1635 filename]");
    fprintf(stderr,"\n\n");
    return 1;
  }

  /*
   * Open 1608 and secondary calibrator files.
   */

  strcpy(lensfile,argv[1]);
  strcpy(calfile,argv[2]);

  if(!(lfp = open_readfile(lensfile)))
    no_error = 0;
  if(!(cfp = open_readfile(calfile)))
    no_error = 0;

  /*
   * First count the number of data input lines in order to set
   *  sizes of arrays.
   */

  if(no_error)
    nlines = n_lines(lfp,'#');

  if(nlines == 0)
    no_error = 0;
  else if(nlines != n_lines(cfp,'#')) {
    fprintf(stderr,
	    "ERROR. Number of lines in lens and cal files don't match.\n");
    no_error = 0;
  }
  else {
    printf("\n%d data lines in lens and cal input files.\n\n",nlines);
    rewind(lfp);
    rewind(cfp);
  }

  /*
   * Allocate arrays
   */

  if(no_error)
    for(i=0; i<N08; i++) {
      if(!(fl08[i] = new_fluxrec(nlines)))
	no_error = 0;
    }

  if(no_error)
    if(!(fl34 = new_fluxrec(nlines)))
      no_error = 0;

  if(no_error)
    if(!(fl35 = new_fluxrec(nlines)))
      no_error = 0;

  /*
   * Read in data
   */

  if(no_error)
    if(read_data(fl34,fl35,fl08,nlines,lfp,cfp))
      no_error = 0;

  /*
   * See if bad-day flagging is requested
   */

  if(no_error) {
    printf("Flag bad days? (1/0) [%d] ",flagbad);
    fgets(line,MAXC,stdin);
    if(line[0] != '\n') {
      while(sscanf(line,"%d",&flagbad) != 1 || flagbad < 0) {
	fprintf(stderr,"ERROR: bad input.  Enter value again:  ");
	fgets(line,MAXC,stdin);
      }
    }
  }

  /*
   * Flag bad days, if requested
   */

  if(flagbad && no_error) {
    if(flag_bad(fl34,fl35,fl08,nlines,&flagbad,"badday.list",nbad)) {
      no_error = 0;
    }
    else {
      if(flagbad == 2)
	printf("Bad days flagged: %d %d %d %d\n",nbad[0],nbad[1],nbad[2],
	       nbad[3]);
      else
	printf("Bad days flagged: %d\n",nbad[0]);
    }
  }

  /*
   * Make the "flat field" curve
   */

  if(no_error)
    if(!(flat = make_flat(fl34,fl35,nlines)))
      no_error = 0;

  /*
   * Set error determination
   */

  if(no_error) {
    printf("\nEnter choice of error determination\n");
    printf("  0. RMS noise in map only\n");
    printf("  1. Include fractional RMS from 1634/1635 ratio\n");
    printf("Choice? [%d] ",errtype);
    fgets(line,MAXC,stdin);
    if(line[0] != '\n') {
      while(sscanf(line,"%d",&errtype) != 1 || errtype < 0) {
	fprintf(stderr,"ERROR: Bad input.  Enter new value:  ");
	fgets(line,MAXC,stdin);
      }
    }
  }

  /*
   * Calculate fractional error in 1634/1635 flux ratio
   */

  if(no_error) {
    if(errtype == 0)
      fracrms = 0.0;
    else
      if(ratio_err(fl34,fl35,nlines,&fracrms))
	no_error = 0;
  }

  /*
   * Flat field the 1608 light curves
   */

  if(no_error) {
    for(i=0; i<N08; i++) {
      if(!(ff08[i] = flat_field(fl08[i],flat,nlines,fracrms,NULL,0)))
	no_error = 0;
    }
  }

  /*
   * Set the curve means to their default values
   */

  mean[0] = AMEAN;
  mean[1] = BMEAN;
  mean[2] = CMEAN;
  mean[3] = DMEAN;

  /*
   * Create the three-curve composite
   */

  if(no_error)
    if(!(compos = compos_3(ff08,nlines,nbad,&ncompos,&shiftind)))
      no_error = 0;

  /*
   * Get tmin and tmax from compos, which is easy since it is sorted
   */

  if(no_error) {
    tmin = compos[0].day;
    tmax = compos[ncompos-1].day;
    printf("tmin = %6.2f, tmax = %6.2f\n",tmin,tmax);
  }

  /*
   * Open the plot
   */

  if(no_error)
    plot_open(1.5);

  /*
   * Fit a function to the composite curve
   */

  if(no_error)
    if(!(a = fit_compos(compos,ncompos,&ndeg)))
      no_error = 0;

  /*
   * Summarize results
   */

  printf("\nUsing a fit of degree %d\n",ndeg);

  /*
   * Find and plot residuals
   */

  if(no_error)
    if(plot_resid(compos,ncompos,a,ndeg+1,&meanrms))
      no_error = 0;

  /*
   * Now shift the free curve in steps along the composite curve and
   *  create a new composite curve from the free + old composite.
   * Calculate a reduced chisq at each step by comparing the new composite
   *  with the fitted function.
   */

  if(no_error)
    if(find_best_shift(ff08[shiftind],nlines,a,ndeg+1,meanrms,1.0,tmin,tmax,
		       &bestlag))
      no_error = 1;

  /*
   * Plot out best fit
   */

  if(no_error)
    if(plot_best_shift(ff08[shiftind],nlines,mean[shiftind],a,ndeg+1,
		       tmin,tmax,bestlag))
      no_error = 0;

  /*
   * Clean up
   */

  if(no_error)
    printf("\nCleaning up\n\n");

  plot_close();
  a = del_array(a);
  fl34 = del_fluxrec(fl34);
  fl35 = del_fluxrec(fl35);
  compos = del_fluxrec(compos);
  for(i=0; i<4; i++) {
    fl08[i] = del_fluxrec(fl08[i]);
    ff08[i] = del_fluxrec(ff08[i]);
  }
  flat = del_array(flat);

  if(lfp)
    fclose(lfp);
  if(cfp)
    fclose(cfp);

  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: Exiting program\n");
    return 1;
  }
}
Esempio n. 11
0
float *fit_compos(Fluxrec *compos, int ncompos, int *ndeg)
{
  int no_error=1;   /* Flag set to 0 on error */
  int contin=1;     /* Flag set to 0 to break out of loop */
  float chisq;      /* Reduced chisq calculated in curve fitting */
  float *a=NULL;    /* Container for coefficients of fit */
  char line[MAXC];  /* General string for reading input */

  while(contin && no_error){

    /*
     * Plot points
     */

    if(no_error)
      if(plot_lcurve(compos,ncompos,"Days","Flux Density",""))
	no_error = 0;

    /*
     * Get the "degree" of the fitting function
     */

    if(no_error) {
      printf("\nEnter the desired degree of the fitting function: ");
      fgets(line,MAXC,stdin);
      while(sscanf(line,"%d",ndeg) != 1 || *ndeg < 1) {
	fprintf(stderr,"ERROR: Bad input.  Enter value again: ");
	fgets(line,MAXC,stdin);
      }
    }

    /*
     * Call the fitting function
     */

    if(no_error)
      if(!(a = fit_poly(compos,ncompos,*ndeg+1,1)))
	no_error = 0;

    /*
     * Plot fitted polynomial
     */

    if(no_error)
      if(plot_fit_curve(a,*ndeg+1,compos[0].day,compos[ncompos-1].day))
	no_error = 0;

    /*
     * Report reduced chisq
     */

    if(no_error)
      if((chisq = chisq_fit(compos,ncompos,a,*ndeg+1,0,1)) < 0.0)
	no_error = 0;

    /*
     * Continue?
     */

    printf("Another fit? (y/n) [y] ");
    fgets(line,MAX,stdin);
    if(strcmp(line,"") != 0) {
      if(line[0] == 'N' || line[0] == 'n')
	contin = 0;
    }
    else
      a = del_array(a);
  }

  /*
   * Return
   */

  if(no_error)
    return a;
  else {
    fprintf(stderr,"ERROR: fit_compos\n");
    return del_array(a);
  }
}
Esempio n. 12
0
int plot_shifts(Secat *shiftcat, int nshift)
{
  int i;                     /* Looping variable */
  int no_error=1;            /* Flag set to 0 on error */
  float x1,x2,y1,y2;         /* Limits on plot */
  float *fdx=NULL;           /* float version of x offsets */
  float *fdy=NULL;           /* float version of y offsets */
  float *fxptr,*fyptr;       /* Navigation pointers */
  double *dx=NULL;           /* x offsets */
  double *dy=NULL;           /* y offsets */
  double *xptr,*yptr;        /* Navigation pointers */
  double xmean, xsig, xmed;  /* Statistics on dx */
  double ymean, ysig, ymed;  /* Statistics on dx */
  Secat *sptr;               /* Pointer to navigate shiftcat */
  FILE *ofp=NULL;            /* Output file pointer */

  /*
   * Allocate memory for dx and dy arrays
   */

  if(!(dx = new_doubarray(nshift))) {
    fprintf(stderr,"ERROR: calc_shift_stats\n");
    return 1;
  }
  if(!(dy = new_doubarray(nshift)))
    no_error = 0;
  if(!(fdx = new_array(nshift,1)))
    no_error = 0;
  if(!(fdy = new_array(nshift,1)))
    no_error = 0;

  if(no_error) {

    /*
     * Transfer info to new arrays
     */

    for(i=0,sptr=shiftcat,xptr=dx,yptr=dy,fxptr=fdx,fyptr=fdy; 
	i<nshift; i++,sptr++,xptr++,yptr++,fxptr++,fyptr++) {
      *xptr = sptr->dx;
      *yptr = sptr->dy;
      *fxptr = (float) sptr->dx;
      *fyptr = (float) sptr->dy;
    }

    /*
     * Calculate statistics on dx and dy
     */

    doubstats(dx,nshift,&xmean,&xsig,&xmed);
    doubstats(dy,nshift,&ymean,&ysig,&ymed);

    /*
     * Give output values
     */

    printf("\nStatistics on x shift:\n");
    printf("  mean = %f\n",xmean);
    printf("  rms = %f\n",xsig);
    printf("  median = %f\n",xmed);
    printf("Statistics on y shift:\n");
    printf("  mean = %f\n",ymean);
    printf("  rms = %f\n",ysig);
    printf("  median = %f\n",ymed);

    /*
     * Set the limits and median
     */


    x1 = xmed - 5.0 * xsig;
    x2 = xmed + 5.0 * xsig;
    y1 = ymed - 5.0 * ysig;
    y2 = ymed + 5.0 * ysig;

    /*
     * Plot distribution
     */

    cpgslct(2);
    cpgenv(x1,x2,y1,y2,0,1);
    cpglab("x shift","y shift","Calculated Shifts");
    cpgpt(nshift,fdx,fdy,9);

    /*
     * Plot median
     */

    cpgsci(2);
    cpgslw(5);
    fdx[0] = fdx[1] = xmed;
    fdy[0] = y1;
    fdy[1] = y2;
    cpgline(2,fdx,fdy);
    fdy[0] = fdy[1] = ymed;
    fdx[0] = x1;
    fdx[1] = x2;
    cpgline(2,fdx,fdy);
    cpgsci(1);
    cpgslw(1);
  }

  /*
   * Write median shifts to output file -- NB: for these to be the
   *  proper shifts for an iraf imcombine offsets file, the value
   *  need to be the negative of what the above calculation gives.
   */

  if(!(ofp = open_writefile("tmp.offsets")))
    no_error = 0;
  else
    fprintf(ofp,"%8.2f %8.2f\n",-xmed,-ymed);

  /*
   * Clean up and exit
   */

  dx = del_doubarray(dx);
  dy = del_doubarray(dy);
  fdx = del_array(fdx);
  fdy = del_array(fdy);
  if(ofp)
    fclose(ofp);
  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: calc_shift_stats\n");
    return 1;
  }
}
Esempio n. 13
0
Fluxrec *cross_corr_nr(float *flux1, float *flux2, int npoints, float dx)
{
  int i;                    /* Looping variable */
  int no_error=1;           /* Flag set to 0 on error */
  float *y1=NULL;           /* Light curve 1 fluxes */
  float *y2=NULL;           /* Light curve 2 fluxes */
  float *corrout=NULL;      /* Float output from correlation */
  float *yptr1,*yptr2;      /* Pointers to navigate y1, y2, and corrout */
  float *fptr1,*fptr2;      /* Pointers to navigate flux1 and flux2 */
  Fluxrec *xcorr=NULL;      /* Cross-correlation curve */
  Fluxrec *xptr;            /* Pointer to navigate xcorr */

  /*
   * Allocate memory for the float versions of the light curves
   */

  if(!(y1 = new_array(npoints,1))) {
    fprintf(stderr,"ERROR: cross_corr_nr\n");
    return NULL;
  }

  if(!(y2 = new_array(npoints,1)))
    no_error = 0;

  /*
   * Transfer data into y1 and y2.  We need to do this because the
   *  correl routine modifies the arrays passed to i.
   */

  if(no_error) {
    for(i=0,fptr1=flux1,fptr2=flux2,yptr1=y1,yptr2=y2; i<npoints;
	i++,fptr1++,fptr2++,yptr1++,yptr2++) {
      *yptr1 = *fptr1;
      *yptr2 = *fptr2;
    }
  }

  /*
   * Allocate memory for the output from the cross-correlation
   */

  if(no_error)
    if(!(corrout = new_array(npoints,2)))
      no_error = 0;

  /*
   * Call the correlation function.
   * NB: The -1 after y1, y2, and corrout is necessary because of the
   *  Numerical Recipes fortran convention.
   */

  if(no_error)
    correl(y1-1,y2-1,(unsigned long) npoints,corrout-1);

  /*
   * Allocate memory for output Fluxrec array
   */

  if(no_error)
    if(!(xcorr = new_fluxrec(npoints)))
      no_error = 0;

  /*
   * Fill array.  
   * *** NB: The output from correl is the cross-correlation curve with
   *         the positive values going from 0 to (npoints/2-1) and
   *         the negative values going from npoints to npoints/2.
   *         We want the array to be in the "correct" order with the most
   *         negative value at position 0 and monotonically increasing from
   *         there.  Therefore fill the array accordingly.
   */

  if(no_error) {
    for(i=0,yptr1=corrout,xptr=xcorr; i<npoints/2; i++,yptr1++,xptr++) {
      yptr2 = yptr1 + npoints/2;
      xptr->day = (i-npoints/2)*dx;
      xptr->flux = *yptr2;
      (xptr+npoints/2)->day = i*dx;
      (xptr+npoints/2)->flux = *yptr1;
    }
  }

  /*
   * Clean up and exit
   */

  y1 = del_array(y1);
  y2 = del_array(y2);
  corrout = del_array(corrout);

  if(no_error)
    return xcorr;
  else {
    fprintf(stderr,"ERROR: cross_corr_nr.\n");
    return NULL;
  }
}
Esempio n. 14
0
Fluxrec *cross_corr_fft(float *flux1, float *flux2, int size, float dx)
{
  int i;                  /* Looping variable */
  int no_error=1;         /* Flag set to 0 on error */
  float re1,im1;          /* Real and imag. parts of FFT of flux1 */
  float re2,im2;          /* Real and imag. parts of FFT of flux2 */
  float nfact;            /* Normalization factor for FFTs */
  float *y1=NULL;         /* Fluxes from first array -- complex format */
  float *y2=NULL;         /* Fluxes from second array -- complex format */
  float *yptr1,*yptr2;    /* Pointers for y arrays */
  float *fptr1,*fptr2;    /* Pointers to navigate flux1 and flux2 */
  Fluxrec *correl=NULL;   /* Cross-correlation of flux1 and flux2 */
  Fluxrec *cptr;          /* Pointer to navigate correl */

  /*
   * Allocate memory for float arrays
   */

  if(!(y1 = new_array(2*size,1))) {
    return NULL;
  }

  if(!(y2 = new_array(2*size,1)))
    no_error = 0;

  /*
   * Allocate memory for the cross-correlation array
   */

  if(no_error) {
    if(!(correl = new_fluxrec(size))) {
      no_error = 0;
    }
  }

  /*
   * Put information from Fluxrec arrays into float arrays, which
   *  are in the Numerical Recipes "complex" format which consists.
   *  of alternating real and complex values.  Since our arrays
   *  are real, set all the imaginary members to zero.  Note that
   *  new_array automatically sets all of its members to zero, so
   *  all we have to do is fill in the real members of the arrays with
   *  the members of flux1 and flux2.
   */

  yptr1 = y1;
  yptr2 = y2;

  if(no_error) {
#if 0
    for(i=0,fptr1=flux1,fptr2=flux2; i<size; i++,fptr1++,fptr2++) {
      *yptr1 = *fptr1;
      *yptr2 = *fptr2;
      yptr1 += 2;
      yptr2 += 2;
    }
#endif
    for(i=0; i<size; i++) {
      y1[2*i] = flux1[i];
      y2[2*i] = flux2[i];
    }
  }

  /*
   * Do the cross-correlation
   */

  if(no_error) {

    /*
     * Fourier transform the two arrays
     */

    four1(y1-1,size,1);
    four1(y2-1,size,1);

    /*
     * Now we have the transform -> compute the correlation.
     */

    nfact = 1.0;

    for(i=0; i<size; i++) {
      re1 = y1[2*i];
      im1 = y1[2*i+1];
      re2 = y2[2*i];
      im2 = y2[2*i+1];

      /*
       * Store the correlation in the first complex array.
       */

      y1[2*i] = (re1*re2 + im1*im2)/nfact;
      y1[2*i+1] = (im1*re2 - im2*re1)/nfact;
    }

    /*
     * Transform back.
     */

    four1(y1-1,size,-1);
  }

  /*
   * Put the results into the cross correlation array
   */

  if(no_error) {
#if 0
    yptr1 = y1;
    for(i=0,cptr=correl; i<size/2; i++,cptr++) {
      yptr2 = yptr1 + size;
      cptr->day = (i-size/2)*dx;
      (cptr+size/2)->day = i*dx;
      cptr->flux = *yptr2;
      (cptr+size/2)->flux = *yptr1;
      yptr1 += 2;
    }
#endif
    for(i=0; i<size/2; i++) {
      correl[i].day = (i-size/2)*dx;
      correl[i+size/2].day = i*dx;
      correl[i].flux = y1[size+2*i];
      correl[i+size/2].flux = y1[2*i];
    }
  }

  /*
   * Clean up
   */

  y1 = del_array(y1);
  y2 = del_array(y2);

  if(no_error)
    return correl;
  else {
    fprintf(stderr,"ERROR: cross_corr_fft\n");
    return NULL;
  }
}
Esempio n. 15
0
Fluxrec *do_corr(Fluxrec *flux1, Fluxrec *flux2, int size, int *corsize,
		 int nbad)
{
  int no_error=1;         /* Flag set to 0 on error */
  int nx;                 /* Size of gridded arrays */
  float xmin,xmax;        /* Min and max day numbers */
  float dx;               /* Grid step-size between days */
  float test;             /* Tests size to see if it's a power of 2 */
  float *zpad1=NULL;      /* Zero padded light curve */
  float *zpad2=NULL;      /* Zero padded light curve */
  Fluxrec *grflux1=NULL;  /* Gridded version of flux1 */
  Fluxrec *grflux2=NULL;  /* Gridded version of flux2 */
  Fluxrec *correl=NULL;   /* Cross-correlation of flux1 and flux2 */

  /*
   * Note that the day part of the flux array will be sorted by 
   *  construction, so the min and max values will be easy to find.
   */

  xmin = flux1->day;
  xmax = (flux1+size-1)->day;

  /*
   * Check if size is a power of 2 because the number of points in the
   *  curve MUST be a power of two if the cross-correlation function
   *  is cross_corr_fft or cross_corr_nr.
   */

  test = log((float)size)/log(2.0);

  /*
   * If it is or if CORRFUNC == 3 , then set the values for nx and dx, 
   *  as simple functions of size, xmin, and xmax, and then zero
   *  pad the input data
   */

  if(test-(int)test == 0) {
    nx = size;
    dx = (xmax-xmin)/(nx-1);

    if(!(zpad1 = zero_pad(flux1,nx)))
      no_error = 0;
    if(!(zpad2 = zero_pad(flux2,nx)))
      no_error = 0;
  }    


  /*
   * If size is not a power of 2 and CORRFUNC != 3, interpolate the data onto 
   *  an appropriately spaced grid with a size that is a power of 2.
   */

  else {

    /*
     * Compute new values for nx and dx
     */

    nx = pow(2.0f,floor((log((double)size)/log(2.0)+0.5)));
    dx = (xmax-xmin)/(nx-1);
    printf("do_corr: Using grid of %d points for cross-correlation.\n",nx);
    if(!(grflux1 = lin_interp(flux1,size,nx,dx,nbad)))
      no_error = 0;
      
    if(no_error)
      if(!(grflux2 = lin_interp(flux2,size,nx,dx,nbad)))
	no_error = 0;
      
    /*
     * Zero-pad the gridded data
     */
      
    if(no_error)
      if(!(zpad1 = zero_pad(grflux1,nx)))
	no_error = 0;
      
    if(no_error)
      if(!(zpad2 = zero_pad(grflux2,nx)))
	no_error = 0;
  }

  /*
   * Do the cross-correlation, via one of three styles:
   *  1. FFT method (Numerical Recipes FFTs, then mulitply and transform back
   *  2. Numerical Recipes correl function
   *  3. Time domain method with no FFTs
   */

  if(no_error) {
    nx *= ZEROFAC;
    switch(CORRFUNC) {
    case 1:
      if(!(correl = cross_corr_fft(zpad1,zpad2,nx,dx)))
	no_error = 0;
      break;
    case 2:
      if(!(correl = cross_corr_nr(zpad1,zpad2,nx,dx)))
	no_error = 0;
      break;
    default:
      fprintf(stderr,"ERROR: do_corr.  Bad choice of correlation function\n");
      no_error = 0;
    }
  }

  /*
   * Clean up
   */

  zpad1 = del_array(zpad1);
  zpad2 = del_array(zpad2);
  grflux1 = del_fluxrec(grflux1);
  grflux2 = del_fluxrec(grflux2);

  if(no_error) {
    *corsize = nx;
    return correl;
  }
  else {
    *corsize = 0;
    return NULL;
    fprintf(stderr,"ERROR: do_corr\n");
  }
}
Esempio n. 16
0
int main(int argc, char *argv[])
{
  int i;                  /* Looping variable */
  int no_error=1;         /* Flag set to 0 on error */
  int ncomp=1;            /* Number of components */
  int srctype=CAL;        /* Type of source (lens or cal) */
  float rmsp=0.0;         /* RMS noise from phasecal map */
  float rmsg=0.0;         /* RMS noise from gscale map */
  float chisqp=0.0;       /* Chisq value from phasecal modelfit */
  float chisqg=0.0;       /* Chisq value from gscale modelfit */
  float diff;             /* Difference between gscale and phasecal fluxes */
  double obsdate;         /* Date of observation */
  char root[MAXC];        /* Source root name */
  char datename[MAXC];    /* Observation date as used in input filenames */
  char flag[MAXC];        /* Either 'lens' or 'cal' */
  char baddayname[MAXC];  /* Name for bad day file */
  Secat *fluxp=NULL;      /* Component fluxes from phasecal model */
  Secat *fluxg=NULL;      /* Component fluxes from gscale model */
  Secat *pptr,*gptr;      /* Pointers to navigate flux arrays */
  FILE *dayfp=NULL;       /* Output file pointer for temporary day file */
  FILE *badfp=NULL;       /* Output file pointer for bad day file */

  /*
   * Check command line format
   */

  if(argc != 4) {
    fprintf(stderr,"\n*** Usage: getflux [date] [source_name] [flag] ");
    fprintf(stderr,"***\n\n");
    fprintf(stderr,"The date is the observing date in the form yyyymmdd\n");
    fprintf(stderr,"The source name is how the source is identified in the\n");
    fprintf(stderr,"    input files.  It is often the first part of the IAU");
    fprintf(stderr," name.\n");
    fprintf(stderr,"    e.g., 1608 for 1608+656");
    fprintf(stderr,"The flag should be either lens or cal\n");
    fprintf(stderr,"For example:\n\n");
    fprintf(stderr,"             getflux 20000214 1608 lens\n\n");
    return 1;
  }

  /*
   * Set datename, root, and flag variables from command line
   */

  strcpy(datename,argv[1]);
  strcpy(root,argv[2]);
  strcpy(flag,argv[3]);

  /*
   * Check that the flag variable is correct
   */

  if(strcmp(flag,"lens") == 0)
    srctype = LENS;
  else if(strcmp(flag,"cal") == 0)
    srctype = CAL;
  else {
    fprintf(stderr,"\n\nERROR: Flag must be either 'lens' or 'cal'\n");
    fprintf(stderr,"  You entered %s\n",flag);
    return 1;
  }

  /*
   * Process data, once for phasecal model and once for gscale model
   */

  printf("\n");
  if(no_error)
    if(!(fluxp = source_flux(datename,root,"p",&ncomp,&rmsp,&chisqp,
		    &obsdate)) == 1)
      no_error = 0;

  if(no_error)
    if(!(fluxg = source_flux(datename,root,"g",&ncomp,&rmsg,&chisqg,
		    &obsdate)) == 1)
      no_error = 0;

  /*
   * Convert JD to MJD 
   */

  if(no_error)
    set_log_obsdate(&obsdate);

  /*
   * Print out results to STDOUT
   */

  if(no_error) {
    printf("\nBest-fit flux densities for %s (mJy):\n",root);
    printf("---------------------------------------\n\n");
    printf("   Component  S(phasecal)  S(gscale)   dS(mJy)   dS/S(\%)\n");
    printf("   ---------  -----------  ---------  ---------  -------\n");
    for(i=0,pptr=fluxp,gptr=fluxg; i<ncomp; i++,pptr++,gptr++) {
      diff = gptr->fauto - pptr->fauto;
      printf("   %4d         %7.3f     %7.3f     %6.3f  %7.3f\n",
	     i+1,pptr->fauto,gptr->fauto,diff,100.0*diff / gptr->fauto);
    }
    printf("\n  Phasecal fit parameters: rms = %8.5f mJy/beam, ",rmsp);
    printf("chisq = %8.4f\n",chisqp);
    printf("  Gscale fit parameters:   rms = %8.5f mJy/beam, chisq = %8.4f\n",
	   rmsg,chisqg);
    printf("\n");
  }

  /*
   * Open tmpday and badday output files
   */

  if(srctype == LENS) {
    if(!(dayfp = open_writefile("tmpday")))
      no_error = 0;
    sprintf(baddayname,"%s%s_badday.edt",REDDIR,root);
    if(!(badfp = open_appendfile(baddayname,1)))
      no_error = 0;
  }

  /*
   * Print data to output files
   */

  if(no_error) {
    if(srctype == LENS) {
      if(print_lensflux(root,fluxp,fluxg,ncomp,rmsp,rmsg,chisqp,chisqg,
			obsdate))
	no_error = 0;
      fprintf(dayfp,"DAY %7.2f\n",obsdate);
      fprintf(badfp,"%7.2f 0 %s\n",obsdate,datename);
    }
    else {
      if(print_calflux(root,fluxp,fluxg,ncomp,rmsp,rmsg,chisqp,chisqg,
		       obsdate))
	no_error = 0;
    }
  }

  /*
   * Clean up
   */

  fluxp = del_array(fluxp);
  fluxg = del_array(fluxg);
  if(dayfp)
    fclose(dayfp);
  if(badfp)
    fclose(badfp);

#if 0
  /***********************************************************************
   *
   * Old 1634 and 1635 stuff -- keep just in case we go back to AF310 
   * analysis.
   */

  float flux34;           /* Flux for 1634 best-fit model */
  float flux35;           /* Flux for 1635 best-fit model */
  float rms34,rms35;      /* RMS errors on best-fit models */
  float chisq34,chisq35;  /* chisq values for best-fit models */
  char out1634[MAXC];     /* Output filename for 1634  */
  char out1635[MAXC];     /* Output filename for 1635 */
  FILE *ofp1634=NULL;     /* Output file pointer for 1634  */
  FILE *ofp1635=NULL;     /* Output file pointer for 1635 */

  /*
   * Process 1634 data
   */

  if((get_best_cal("1634",ext,&flux34,&rms34,&chisq34)) == 1)
    no_error = 0;

  /*
   * Process 1635 data
   */

  if(no_error)
    if((get_best_cal("1635",ext,&flux35,&rms35,&chisq35)) == 1)
      no_error = 0;

  /*
   * Print out results to STDOUT
   */

  if(no_error) {
    printf("\nBest fit 1634 flux = %9.5f, rms = %10.3e, chisq = %8.3f\n",
	   flux34,rms34,chisq34);
    printf("Best fit 1635 flux = %9.5f, rms = %10.3e, chisq = %8.3f\n",
	   flux35,rms35,chisq35);
  }

  /*
   * Set the output filenames
   */

  sprintf(out1634,"%s1634.edt",REDDIR);
  sprintf(out1635,"%s1635.edt",REDDIR);

  /*
   * Open the output files
   */

  if((ofp1634 = fopen(out1634,"a")) == NULL) {
    fprintf(stderr,"ERROR.  Cannot open output file %s\n",out1634);
    no_error = 0;
  }
  if((ofp1635 = fopen(out1635,"a")) == NULL) {
    fprintf(stderr,"ERROR.  Cannot open output file %s\n",out1635);
    no_error = 0;
  }

  /*
   * Print data to output files
   */

  if(no_error) {
    fprintf(ofp1634,"%6.1f %8.3f %8.5f %8.3f\n",
	    obsdate,flux34,rms34,chisq34);
    fprintf(ofp1635,"%6.1f %8.3f %8.5f %8.3f\n",
	    obsdate,flux35,rms35,chisq35);
  }

  if(ofp1634)
    fclose(ofp1634);
  if(ofp1635)
    fclose(ofp1635);

  /*
   ***********************************************************************
   */
#endif

  if(no_error) {
    printf("\nFinished with program getflux.c.\n\n");
    return 0;
  }
  else {
    fprintf(stderr,"ERROR: Exiting program getflux.c\n\n");
    return 1;
  }
}