Example #1
0
File: vector.c Project: 198767/cyy2
int vect_strcmp(const vector vector1,const vector vector2)
{
	int ret = 0;
	vector vector_temp1;
	vector vector_temp2;
	if(!vector1 || !vector2) //空指针 出错
		return -1;
	if(vect_getelmtype(vector1) !=STRING || vect_getelmtype(vector2) !=STRING) //保存的不是字符串 出错
		return -1;
	if(vect_getlength(vector1) !=vect_getlength(vector2))	//保存的字符串个数不一致 肯定不相等
		return 1;	
	//拷贝副本
	vector_temp1=vect_copy(vector1);
	if(!vector_temp1)
		return -1;
	vector_temp2=vect_copy(vector2);
	if(!vector_temp2)
		return -1;
	vect_sortstr(vector_temp1);
	vect_sortstr(vector_temp2);

	ret = vect_strcmp_order(vector_temp1, vector_temp2);
	vect_free(vector_temp1);
	vect_free(vector_temp2);
	return (ret);
}
Example #2
0
int determine_padvals(char *maskfilenm, mask * obsmask, float *padvals[])
/* Determine reasonable padding values from the rfifind produced  */
/* *.stats file if it is available.  Return the allocated vector  */
/* (of length numchan) in padvals.  Return a '1' if the routine   */
/* used the stats file, return 0 if the padding was set to aeros. */
{
   FILE *statsfile;
   int ii, numchan, numint, ptsperint, lobin, numbetween;
   float **dataavg, tmp1, tmp2;
   char *statsfilenm, *root, *suffix;

   if (split_root_suffix(maskfilenm, &root, &suffix) == 0) {
      printf("\nThe mask filename (%s) must have a suffix!\n\n", maskfilenm);
      exit(1);
   } else {
      /* Determine the stats file name */
      statsfilenm = calloc(strlen(maskfilenm) + 2, sizeof(char));
      sprintf(statsfilenm, "%s.stats", root);
      free(root);
      free(suffix);
      *padvals = gen_fvect(obsmask->numchan);
      /* Check to see if the file exists */
      printf("Attempting to read the data statistics from '%s'...\n", statsfilenm);
      statsfile = chkfopen(statsfilenm, "rb");
      free(statsfilenm);
      if (statsfile) {          /* Read the stats */
         chkfread(&numchan, sizeof(int), 1, statsfile);
         chkfread(&numint, sizeof(int), 1, statsfile);
         chkfread(&ptsperint, sizeof(int), 1, statsfile);
         chkfread(&lobin, sizeof(int), 1, statsfile);
         chkfread(&numbetween, sizeof(int), 1, statsfile);
         dataavg = gen_fmatrix(numint, numchan);
         /* These are the powers */
         chkfread(dataavg[0], sizeof(float), numchan * numint, statsfile);
         /* These are the averages */
         chkfread(dataavg[0], sizeof(float), numchan * numint, statsfile);
         /* Set the padding values equal to the mid-80% channel averages */
         for (ii = 0; ii < numchan; ii++)
            calc_avgmedstd(dataavg[0] + ii, numint, 0.8, numchan,
                           *padvals + ii, &tmp1, &tmp2);
         printf
             ("...succeded.  Set the padding values equal to the mid-80%% channel averages.\n");
         vect_free(dataavg[0]);
         vect_free(dataavg);
         fclose(statsfile);
         return 1;
      } else {
         /* This is a temporary solution */
         for (ii = 0; ii < obsmask->numchan; ii++)
            (*padvals)[ii] = 0.0;
         printf("...failed.\n  Set the padding values to 0.\n");
         return 0;
      }
   }
}
Example #3
0
static void free_fftpart(fftpart * fp)
{
   vect_free(fp->normvals);
   vect_free(fp->medians);
   vect_free(fp->rawpowers);
#ifdef USEMMAP
   munmap(fp->amps, sizeof(fcomplex) * fp->numamps);
#else
   vect_free(fp->amps);
#endif
   free(fp);
}
Example #4
0
void delete_prepfoldinfo(prepfoldinfo * in)
/* Free all dynamic arrays in the prepfold array */
{
   vect_free(in->rawfolds);
   if (in->nsub > 1)
      vect_free(in->dms);
   vect_free(in->periods);
   vect_free(in->pdots);
   free(in->stats);
   free(in->filenm);
   free(in->candnm);
   free(in->telescope);
   free(in->pgdev);
}
Example #5
0
float estimate_offpulse_redchi2(double *inprofs, foldstats *stats,
                                int numparts, int numsubbands, 
                                int proflen, int numtrials, double dofeff)
// Randomly offset each pulse profile in a .pfd data square or cube
// and combine them to estimate a "true" off-pulse level.  Do this
// numtrials times in order to improve the statistics.  Return the
// inverse of the average of the off-pulse reduced-chi^2 (i.e. the
// correction factor).  dofeff is the effective number of DOF as
// returned by DOF_corr().
{
    int ii, jj, kk, offset, trialnum, phsindex, statindex;
    float *chis;
    double chi_avg, chi_var, redchi;
    double prof_avg, prof_var, *prof_ptr, *sumprof;

    sumprof = gen_dvect(proflen);
    chis = gen_fvect(numtrials);

    for (trialnum = 0; trialnum < numtrials; trialnum++) {
        // Initialize the summed profile
        for (ii = 0; ii < proflen; ii++)
            sumprof[ii] = 0.0;
        prof_avg = 0.0;
        prof_var = 0.0;
        prof_ptr = inprofs;
        for (ii = 0; ii < numparts; ii++) {  // parts
            for (jj = 0; jj < numsubbands; jj++) {  // subbands
                statindex = ii * numsubbands + jj;
                offset = random() % proflen;
                phsindex = 0;
                for (kk = offset; kk < proflen; kk++, phsindex++) // phases
                    sumprof[phsindex] += prof_ptr[kk];
                for (kk = 0; kk < offset; kk++, phsindex++) // phases
                    sumprof[phsindex] += prof_ptr[kk];
                prof_ptr += proflen;
                prof_avg += stats[statindex].prof_avg;
                prof_var += stats[statindex].prof_var;
            }
        }
        /* Calculate the current chi-squared */
        redchi = chisqr(sumprof, proflen, prof_avg, prof_var) / dofeff;
        chis[trialnum] = (float) redchi;
    }
    avg_var(chis, numtrials, &chi_avg, &chi_var);
    vect_free(chis);
    vect_free(sumprof);
    return 1.0/chi_avg;
}
void write_padding(FILE * outfiles[], int numfiles, float value, int numtowrite)
{
    int ii;

    if (numtowrite <= 0) {
        return;
    } else if (numtowrite == 1) {
        for (ii = 0; ii < numfiles; ii++)
            chkfwrite(&value, sizeof(float), 1, outfiles[ii]);
    } else {
        int maxatonce = 8192, veclen, jj;
        float *buffer;
        veclen = (numtowrite > maxatonce) ? maxatonce : numtowrite;
        buffer = gen_fvect(veclen);
        for (ii = 0; ii < veclen; ii++)
            buffer[ii] = value;
        if (veclen == numtowrite) {
            for (ii = 0; ii < numfiles; ii++)
                chkfwrite(buffer, sizeof(float), veclen, outfiles[ii]);
        } else {
            for (ii = 0; ii < numtowrite / veclen; ii++) {
                for (jj = 0; jj < numfiles; jj++)
                    chkfwrite(buffer, sizeof(float), veclen, outfiles[jj]);
            }
            for (jj = 0; jj < numfiles; jj++)
                chkfwrite(buffer, sizeof(float), numtowrite % veclen, outfiles[jj]);
        }
        vect_free(buffer);
    }
}
Example #7
0
int prune_powers(float *arr, int n, int numsumpows)
/* Sets powers that are more than approx PRUNELEV standard */
/* devs above the median value to NEWLEV times the median  */
/* value.  This helps 'clean' the spectrum of high power   */
/* signals that probably have nothing to do with a phase   */
/* modulation spectrum (i.e. they are RF noise or strong   */
/* solitary pulsars.                                       */
{
    int ii, ct = 0;
    float med, cutoff, *tmparr;

    /* Determine the median power */

    tmparr = gen_fvect(n);
    memcpy(tmparr, arr, sizeof(float) * n);
    med = median(tmparr, n);
    vect_free(tmparr);

    /* Throw away powers that are bigger that PRUNELEV * median */

    cutoff = med * PRUNELEV / sqrt((float) numsumpows);
    for (ii = 0; ii < n; ii++) {
        if (arr[ii] > cutoff) {
            arr[ii] = NEWLEV * med;
            ct++;
        }
    }
    return ct;
}
Example #8
0
File: vector.c Project: tomtix/osux
struct vector *cst_vect_from_list(GHashTable *ht, const char *key)
{
    GList *l = cst_list(ht, key);
    if (l == NULL)
        return NULL;
    struct vector * v = vect_new(g_list_length(l), CST_VECT_DIM);

    GList *it;
    int i = 0, j = 0;
    for (it = l; it != NULL; it = it->next) {
        GList *l2 = yw_extract_list((osux_yaml*) it->data);
        if (l2 == NULL) {
            vect_free(v);
            return NULL;
        }

        g_assert(g_list_length(l2) == CST_VECT_DIM);
        GList *it2;
        for (it2 = l2; it2 != NULL; it2 = it2->next) {
            v->t[i][j] = atof(yw_extract_scalar((osux_yaml*) it2->data));
            j++;
        }
        i++;
    }
    return v;
}
Example #9
0
void calc_avgmedstd(float *arr, int numarr, float fraction,
                    int step, float *avg, float *med, float *std)
/* Calculates the median and middle-'fraction' std deviation  */
/* and average of the array 'arr'.  Values are returned in    */
/* 'avg', 'med' and 'std'.  The array is not modified.        */
{
   int ii, jj, len, start;
   float *tmparr;
   double davg, dstd;

   len = (int) (numarr * fraction + 0.5);
   if (len > numarr || len < 0) {
      printf("fraction (%g) out-of-bounds in calc_avgmedstd()\n", fraction);
      exit(1);
   }
   start = (numarr - len) / 2;
   tmparr = gen_fvect(numarr);
   for (ii = 0, jj = 0; ii < numarr; ii++, jj += step)
      tmparr[ii] = arr[jj];
   qsort(tmparr, numarr, sizeof(float), compare_floats);
   avg_var(tmparr + start, len, &davg, &dstd);
   *avg = (float) davg;
   *med = tmparr[numarr / 2];
   *std = sqrt(dstd);
   vect_free(tmparr);
}
Example #10
0
void free_mask(mask obsmask)
/* Free the contents of an mask structure */
{
   int ii;

   for (ii = 0; ii < obsmask.numint; ii++) {
      if (obsmask.num_chans_per_int[ii] > 0 &&
          obsmask.num_chans_per_int[ii] <= obsmask.numchan)
         vect_free(obsmask.chans[ii]);
   }
   free(obsmask.chans);
   vect_free(obsmask.num_chans_per_int);
   if (obsmask.num_zap_chans)
      vect_free(obsmask.zap_chans);
   if (obsmask.num_zap_ints)
      vect_free(obsmask.zap_ints);
}
Example #11
0
File: add.c Project: 198767/cyy3
/*
 * get the addtion result with the <decimal base> form 
 * but if the base is too large (>16) and hard to find  letters to present the digits(maybe >16), then use the <digit1*base1^power1+digit2*base2^power2.....> form
 */
char* get_decimalstr(const vector digits,const ln base)
{
	int i;
	char *s1,*s2;
	ln digit;
	vector decimal;
	//produce the decimal string
	decimal=vect_create_str("");
	if(!decimal)
		return NULL;

	s2=ln2str(base);
	for(i=0;i<vect_getlength(digits);i++)
	{
		digit=*((ln *)vect_getelmat(digits,i));
		if(ln_cmp_int(base,16)>0)
		{
			if(ln_cmp_int(digit,0)>0)
			{
				s1=ln2str(digit);
				if(i>0)
					vect_strcat(decimal,"+");
				vect_strcat(decimal,"%s*%s^(%d)",s1,s2,vect_getlength(digits)-i-2);
				free(s1);
			}
		}
		else
		{
			s1=ln2str(digit);
			if(i==vect_getlength(digits)-1) //add the point
			{
				if(strcmp(s1,"0")==0)
				{
					free(s1);
					break;
				}
				if(vect_getlength(decimal)==0)
					vect_strcat(decimal,"0");
				vect_strcat(decimal,".");
			}
			if(strlen(s1)==1) //0-9
				vect_strcat(decimal,"%s",s1);
			else
				vect_strcat(decimal,"%c",'a'+s1[1]-'0'); //10-15 converted to a-f
			free(s1);
		}
	}
	
	//append the base
	if(ln_cmp_int(base,16)<=0)
		vect_strcat(decimal," %s",s2);
	free(s2);

	s1=vect2str(decimal);
	vect_free(decimal);
	return s1;
}
Example #12
0
static void free_datapart(datapart * dp)
{
#ifdef USEMMAP
   munmap(dp->data, sizeof(float) * dp->nn);
#else
   vect_free(dp->data);
#endif
   free(dp);
}
Example #13
0
int bary2topo(double *topotimes, double *barytimes, int numtimes,
              double fb, double fbd, double fbdd,
              double *ft, double *ftd, double *ftdd)
/* Convert a set of barycentric pulsar spin parameters (fb, fbd, fbdd) */
/* into topocentric spin parameters (ft, ftd, ftdd) by performing      */
/* a linear least-squares fit (using LAPACK routine DGELS).  The       */
/* routine equates the pulse phase using topcentric parameters and     */
/* times to the pulse phase using barycentric parameters and times.    */
{
   double *work, *aa, *bb, dtmp;
   int ii, mm = 3, nn, nrhs = 1, lwork, info, index;
   char trans = 'T';

   if (numtimes < 4) {
      printf("\n'numtimes' < 4 in bary2topo():  Cannot solve.\n\n");
      exit(0);
   }
   nn = numtimes;
   lwork = mm + nn * 9;
   aa = gen_dvect(mm * nn);
   bb = gen_dvect(nn);
   work = gen_dvect(lwork);
   for (ii = 0; ii < nn; ii++) {
      index = ii * 3;
      dtmp = (topotimes[ii] - topotimes[0]) * SECPERDAY;
      aa[index] = dtmp;
      aa[index + 1] = 0.5 * dtmp * dtmp;
      aa[index + 2] = dtmp * dtmp * dtmp / 6.0;
      dtmp = (barytimes[ii] - barytimes[0]) * SECPERDAY;
      bb[ii] = dtmp * (fb + dtmp * (0.5 * fbd + fbdd * dtmp / 6.0));
   }
   // dgels_(&trans, &mm, &nn, &nrhs, aa, &mm, bb, &nn, work, &lwork, &info);
   call_dgels(&trans, mm, nn, nrhs, aa, mm, bb, nn, work, lwork, &info);
   *ft = bb[0];
   *ftd = bb[1];
   *ftdd = bb[2];
   vect_free(aa);
   vect_free(bb);
   vect_free(work);
   return info;
}
Example #14
0
static char * 
test_resize() 
{
	vect_int *vi = vect_init_int(1);
	for (int i=0; i<257; i++) {
		vect_push_int(vi, i);
	}
		
	mu_assert("error resize: vi->capacity != 512", vi->capacity == 512);
	vect_free(vi);
	return 0;
}
Example #15
0
File: add.c Project: 198767/cyy3
void free_digit(vector digits)
{
	int i;
	ln p;
	for(i=0;i<vect_getlength(digits);i++)
	{
		p=*((ln *)vect_getelmat(digits,i));
		ln_free(*p);
	}
	vect_free(digits);
	return;
}
Example #16
0
void plot_profile(int proflen, float *profile, const char *title,
                  const char *probtxt, const char *foldtxt,
                  int showerr, float *errors, int showid)
{
    int ii;
    float *x, overy, ymin, ymax;
    float errmin = 0.0, errmax = 0.0, offset, avg = 0.0, av[2];

    find_min_max_arr(proflen, profile, &ymin, &ymax);
    if (showerr)
        find_min_max_arr(proflen, errors, &errmin, &errmax);
    overy = 0.1 * (ymax + errmax - ymin - errmin);
    ymax = ymax + overy + errmax;
    ymin = ymin - overy - errmin;
    x = gen_fvect(proflen);
    for (ii = 0; ii < proflen; ii++)
        x[ii] = (float) ii / (float) proflen;
    cpgenv(0.0, 1.00001, ymin, ymax, 0, 0);
    cpgscf(2);
    cpglab("Pulse Phase", "Counts", "");
    if (showid)
        cpgiden();
    cpgslw(5);
    if (showerr) {
        cpgbin(proflen, x, profile, 0);
    } else {
        cpgline(proflen, x, profile);
    }
    cpgslw(1);
    if (showerr) {
        offset = 0.5 / (float) proflen;
        for (ii = 0; ii < proflen; ii++)
            x[ii] += offset;
        cpgerrb(6, proflen, x, profile, errors, 2);
        cpgpt(proflen, x, profile, 5);
    }
    for (ii = 0; ii < proflen; ii++)
        avg += profile[ii];
    avg /= proflen;
    cpgsls(4);
    x[0] = 0.0;
    x[1] = 1.0;
    av[0] = avg;
    av[1] = avg;
    cpgline(2, x, av);
    cpgsls(1);
    cpgsch(1.3);
    cpgmtxt("T", +2.0, 0.5, 0.5, title);
    cpgsch(1.0);
    cpgmtxt("T", +0.8, 0.5, 0.5, foldtxt);
    cpgmtxt("T", -1.5, 0.5, 0.5, probtxt);
    vect_free(x);
}
Example #17
0
static void plot_rfi(rfi * plotrfi, float top, int numint, int numchan,
                     float T, float lof, float hif)
{
   int ii;
   float period, perioderr, dy = 0.035, *temparr;
   float tr[6] = { -0.5, 1.0, 0.0, -0.5, 0.0, 1.0 };
   char temp[40];

   if (plotrfi->freq_avg == 0.0)
      period = 0.0;
   else
      period = 1000.0 / plotrfi->freq_avg;
   if (plotrfi->freq_var == 0.0)        /* Why are these zero? */
      perioderr = 0.0;
   else
      perioderr = 1000.0 * sqrt(plotrfi->freq_var) /
          (plotrfi->freq_avg * plotrfi->freq_avg);
   cpgsvp(0.0, 1.0, 0.0, 1.0);
   cpgswin(0.0, 1.0, 0.0, 1.0);
   cpgnice_output_2(temp, plotrfi->freq_avg, sqrt(plotrfi->freq_var), 0);
   cpgptxt(0.03, top - 0.6 * dy, 0.0, 0.0, temp);
   cpgnice_output_2(temp, period, perioderr, 0);
   cpgptxt(0.12, top - 0.6 * dy, 0.0, 0.0, temp);
   sprintf(temp, "%-5.2f", plotrfi->sigma_avg);
   cpgptxt(0.21, top - 0.6 * dy, 0.0, 0.0, temp);
   sprintf(temp, "%d", plotrfi->numobs);
   cpgptxt(0.27, top - 0.6 * dy, 0.0, 0.0, temp);
   ii = (numint > numchan) ? numint : numchan;
   temparr = gen_fvect(ii);
   for (ii = 0; ii < numchan; ii++)
      temparr[ii] = GET_BIT(plotrfi->chans, ii);
   cpgsvp(0.33, 0.64, top - dy, top);
   cpgswin(0.0, numchan, 0.0, 1.0);
   cpgimag(temparr, numchan, 1, 1, numchan, 1, 1, 0.0, 1.0, tr);
   cpgswin(0.0, numchan, 0.0, 1.0);
   cpgbox("BST", 0.0, 0, "BC", 0.0, 0);
   cpgswin(lof, hif, 0.0, 1.0);
   cpgbox("CST", 0.0, 0, "", 0.0, 0);
   for (ii = 0; ii < numint; ii++)
      temparr[ii] = GET_BIT(plotrfi->times, ii);
   cpgsvp(0.65, 0.96, top - dy, top);
   cpgswin(0.0, numint, 0.0, 1.0);
   cpgimag(temparr, numint, 1, 1, numint, 1, 1, 0.0, 1.0, tr);
   cpgswin(0.0, numint, 0.0, 1.0);
   cpgbox("BST", 0.0, 0, "BC", 0.0, 0);
   cpgswin(0.0, T, 0.0, 1.0);
   cpgbox("CST", 0.0, 0, "", 0.0, 0);
   vect_free(temparr);
}
Example #18
0
static char *
test_pushpop() 
{
	int result = 0;
	vect_int *vi = vect_init_int(1);
	for (int i=0; i<100; i++) 
		vect_push_int(vi, i);	
	while(vi->size)
		result += vect_pop_int(vi);
		
	mu_assert("error pushpop: result != 4950", result == 4950);
	mu_assert("error pushpop: vi->size != 0", vi->size == 0);
	vect_free(vi);
	return 0;
}
Example #19
0
void correct_subbands_for_DM(double dm, prepfoldinfo * search,
                             double *ddprofs, foldstats * ddstats)
/* Calculate the DM delays and apply them to the subbands */
/* to create de-dispersed profiles.                      */
{
   int ii, *dmdelays;
   double *subbanddelays, hif, dopplerhif, hifdelay, rdphase;

   rdphase = search->fold.p1 * search->proflen;
   hif = search->lofreq + (search->numchan - 1.0) * search->chan_wid;
   dopplerhif = doppler(hif, search->avgvoverc);
   hifdelay = delay_from_dm(dm, dopplerhif);
   subbanddelays = subband_delays(search->numchan, search->nsub, dm,
                                  search->lofreq, search->chan_wid,
                                  search->avgvoverc);
   dmdelays = gen_ivect(search->nsub);
   for (ii = 0; ii < search->nsub; ii++)
      dmdelays[ii] =
          NEAREST_INT((subbanddelays[ii] - hifdelay) * rdphase) % search->proflen;
   vect_free(subbanddelays);
   combine_subbands(search->rawfolds, search->stats, search->npart,
                    search->nsub, search->proflen, dmdelays, ddprofs, ddstats);
   vect_free(dmdelays);
}
Example #20
0
void drotate_1d(double *data, long numbins, long bins_to_left)
/* Rotates a vector by bins_to_left places to the left.    */
/* numbins is the number of DOUBLE points to move.         */
/* drotate is better.  Use it.                       */
{
   double *tmp;

   if (bins_to_left < 0 || bins_to_left >= numbins) {
      printf("\nNumber of bins to rotate array in rotate_1d is\n");
      printf("\nout of bounds.  Tried to rotate %ld bins.  Exiting.\n",
             bins_to_left);
      exit(1);
   }
   tmp = gen_dvect(bins_to_left);
   memcpy(tmp, data, sizeof(double) * bins_to_left);
   memmove(data, data + bins_to_left, sizeof(double) * (numbins - bins_to_left));
   memcpy(data + bins_to_left, tmp, sizeof(double) * bins_to_left);
   vect_free(tmp);
}
Example #21
0
void drotate(double *data, long numbins, double bins_to_left)
/* Rotates a vector by bins_to_left places to the left.    */
/* numbins is the number of DOUBLE points to move.         */
{
   double *tmp, lopart, hipart, intpart;
   long i, index;

   bins_to_left = fmod(bins_to_left, (double) numbins);
   if (bins_to_left < 0.0)
      bins_to_left += numbins;
   tmp = gen_dvect(numbins);
   lopart = modf(bins_to_left, &intpart);
   hipart = 1.0 - lopart;
   index = (long) floor(intpart + 1.0E-20);
   for (i = 0; i < numbins; i++)
      tmp[i] = hipart * data[(index + i) % numbins] +
          lopart * data[(index + i + 1) % numbins];
   memcpy(data, tmp, sizeof(double) * numbins);
   vect_free(tmp);
}
Example #22
0
void max_rz_file_harmonics(FILE * fftfile, int num_harmonics,
                             int lobin,
                             double rin, double zin,
                             double *rout, double *zout, rderivs derivs[],
                             double maxpow[])
/* Return the Fourier frequency and Fourier f-dot that      */
/* maximizes the power of the candidate in 'fftfile'.       */
/* WARNING: not tested */
{
   int i;
   double maxz, rin_int, rin_frac;
   int kern_half_width, filedatalen, extra = 10;
   int* r_offset;
   fcomplex** filedata;

   r_offset = (int*)malloc(sizeof(int)*num_harmonics);
   filedata = (fcomplex**)malloc(sizeof(fcomplex*)*num_harmonics);
   maxz = fabs(zin*num_harmonics) + 4.0;
   kern_half_width = z_resp_halfwidth(maxz, HIGHACC);
   filedatalen = 2 * kern_half_width + extra;

   for (i=1;i<=num_harmonics;i++) {
       rin_frac = modf(rin*i, &rin_int);
       r_offset[i-1] = (int) rin_int - filedatalen / 2 + lobin;
       filedata[i-1] = read_fcomplex_file(fftfile, r_offset[i-1], filedatalen);
   }
   rin_frac = modf(rin, &rin_int);
   max_rz_arr_harmonics(filedata, num_harmonics,
                        r_offset,
                        filedatalen, rin_frac + filedatalen / 2,
                        zin, rout, zout, derivs,
                        maxpow);

   *rout += r_offset[0];
   for (i=1;i<=num_harmonics;i++) {
       vect_free(filedata[i-1]);
   }
   free(r_offset);
   free(filedata);
}
Example #23
0
static char * 
test_fibs_build() 
{
	/* build fibs sequence */
	uint64_t result;
	vect_ui64 *vl = vect_init_ui64(8);
	uint64_t a = 0;
	uint64_t b = 1;	
	vect_push_ui64(vl, 0);
	for(int i=1; i<94; i++) {
		uint64_t tmp = a;
		a = a + b;
		b = tmp;
		vect_push_ui64(vl, a);
	}		
	result = vect_at_ui64(vl, vl->size-1);
	
	mu_assert("error fibs: result != 12200160415121876738", 
			       result == 12200160415121876738ULL);
	vect_free(vl);
	return 0;
}
Example #24
0
double *subband_search_delays(int numchan, int numsubbands, double dm,
                              double lofreq, double chanwidth, double voverc)
/* Return an array of delays (sec) for a subband DM search.  The      */
/* delays are calculated normally for each of the 'numchan' channels  */
/* using the appropriate frequencies at the 'dm'.  Then the delay     */
/* from the highest frequency channel of each of the 'numsubbands'    */
/* subbands is subtracted from each subband.  This gives the subbands */
/* the correct delays for each freq in the subband, but the subbands  */
/* themselves are offset as if they had not been de-dispersed.  This  */
/* way, we can call float_dedisp() on the subbands if needed.         */
/* 'lofreq' is the center frequency in MHz of the lowest frequency    */
/* channel.  'chanwidth' is the width in MHz of each channel.  The    */
/* returned array is allocated by this routine.  'voverc' is used to  */
/* correct the input frequencies for doppler effects.  See the        */
/* comments in dedisp_delays() for more info.                         */
/* Note:  When performing a subband search, the delays for each       */
/*   subband must be calculated with the frequency of the highest     */
/*   channel in each subband, _not_ the center subband frequency.     */
{
    int ii, jj, chan_per_subband;
    double *delays, *subbanddelays;

    chan_per_subband = numchan / numsubbands;

    /* Calculate the appropriate delays to subtract from each subband */

    subbanddelays = subband_delays(numchan, numsubbands, dm,
                                   lofreq, chanwidth, voverc);

    /* Calculate the appropriate delays for each channel */

    delays = dedisp_delays(numchan, dm, lofreq, chanwidth, voverc);
    for (ii = 0; ii < numsubbands; ii++)
        for (jj = 0; jj < chan_per_subband; jj++)
            delays[ii * chan_per_subband + jj] -= subbanddelays[ii];
    vect_free(subbanddelays);

    return delays;
}
Example #25
0
double max_rz_file(FILE * fftfile, double rin, double zin,
                   double *rout, double *zout, rderivs * derivs)
/* Return the Fourier frequency and Fourier f-dot that      */
/* maximizes the power of the candidate in 'fftfile'.       */
{
   double maxz, maxpow, rin_int, rin_frac;
   int kern_half_width, filedatalen, startbin, extra = 10;
   fcomplex *filedata;

   maxz = fabs(zin) + 4.0;
   rin_frac = modf(rin, &rin_int);
   kern_half_width = z_resp_halfwidth(maxz, HIGHACC);
   filedatalen = 2 * kern_half_width + extra;
   startbin = (int) rin_int - filedatalen / 2;

   filedata = read_fcomplex_file(fftfile, startbin, filedatalen);
   maxpow = max_rz_arr(filedata, filedatalen, rin_frac + filedatalen / 2,
                       zin, rout, zout, derivs);
   *rout += startbin;
   vect_free(filedata);
   return maxpow;
}
Example #26
0
static basicstats *calc_stats(dataview * dv, datapart * dp)
{
   int ii, jj;
   float *tmpdata;
   basicstats *tmpstats;

   tmpstats = (basicstats *) malloc(sizeof(basicstats));
   tmpstats->max = SMALLNUM;
   tmpstats->min = LARGENUM;
   tmpdata = gen_fvect(dv->numsamps);
   for (ii = 0, jj = dv->lon - dp->nlo; ii < dv->numsamps; ii++, jj++) {
      tmpdata[ii] = dp->data[jj];
      if (tmpdata[ii] > tmpstats->max)
         tmpstats->max = tmpdata[ii];
      if (tmpdata[ii] < tmpstats->min)
         tmpstats->min = tmpdata[ii];
   }
   stats(tmpdata, dv->numsamps, &tmpstats->average, &tmpstats->stdev,
         &tmpstats->skewness, &tmpstats->kurtosis);
   tmpstats->stdev = sqrt(tmpstats->stdev);
   tmpstats->median = median(tmpdata, dv->numsamps);
   vect_free(tmpdata);
   return tmpstats;
}
Example #27
0
fcomplex *corr_rz_interp(fcomplex * data, int numdata, int numbetween,
                         int startbin, double z, int fftlen,
                         presto_interp_acc accuracy, int *nextbin)
  /* This routine uses the correlation method to do a Fourier        */
  /* complex interpolation of a slice of the f-fdot plane.           */
  /* Arguments:                                                      */
  /*   'data' is a complex array of the data to be interpolated.     */
  /*   'numdata' is the number of complex points (bins) in data.     */
  /*   'numbetween' is the number of points to interpolate per bin.  */
  /*   'startbin' is the first bin to use in data for interpolation. */
  /*   'z' is the fdot to use (z=f-dot*T^2).                         */
  /*   'fftlen' is the # of complex pts in kernel and result.        */
  /*   'accuracy' is either HIGHACC or LOWACC.                       */
  /*   'nextbin' will contain the bin number of the first bin not    */
  /*      interpolated in data.                                      */
{
   fcomplex **result, *tempreturn;

   result = corr_rz_plane(data, numdata, numbetween, startbin, z, z, 1,
                          fftlen, accuracy, nextbin);
   tempreturn = result[0];
   vect_free(result);
   return tempreturn;
}
Example #28
0
static dataview *get_dataview(int centern, int zoomlevel, datapart * dp)
{
   int ii, jj, offset;
   double tmpavg, tmpvar;
   float *tmpchunk;
   dataview *dv;

   dv = (dataview *) malloc(sizeof(dataview));
   dv->zoomlevel = zoomlevel;
   dv->numsamps = (1 << (LOGMAXDISPNUM - zoomlevel));
   if (dv->numsamps > dp->nn)
      dv->numsamps = next2_to_n(dp->nn) / 2;
   dv->chunklen = (zoomlevel < -LOGMINCHUNKLEN) ?
       (1 << abs(zoomlevel)) : (1 << LOGMINCHUNKLEN);
   dv->dispnum = (dv->numsamps > MAXDISPNUM) ? MAXDISPNUM : dv->numsamps;
   if (DEBUGOUT)
      printf("zoomlevel = %d  numsamps = %d  chunklen = %d  dispnum %d  nn = %d\n",
             dv->zoomlevel, dv->numsamps, dv->chunklen, dv->dispnum, dp->nn);
   dv->numchunks = dv->numsamps / dv->chunklen;
   dv->centern = centern;
   dv->lon = centern - dv->numsamps / 2;
   dv->vdt = dv->chunklen * idata.dt;
   dv->maxval = SMALLNUM;
   dv->minval = LARGENUM;
   if (dv->lon < 0) {
      dv->lon = 0;
      dv->centern = dv->lon + dv->numsamps / 2;
   }
   if (dv->lon + dv->numsamps >= dp->nn) {
      dv->lon = dp->nn - dv->numsamps;
      dv->centern = dv->lon + dv->numsamps / 2;
   }
   tmpchunk = gen_fvect(dv->chunklen);
   for (ii = 0; ii < dv->numchunks; ii++) {
      float tmpmin = LARGENUM, tmpmax = SMALLNUM, tmpval;
      offset = dv->lon + ii * dv->chunklen;
      memcpy(tmpchunk, dp->data + offset, sizeof(float) * dv->chunklen);
      avg_var(dp->data + offset, dv->chunklen, &tmpavg, &tmpvar);
      if (usemedian)
         dv->avgmeds[ii] = median(tmpchunk, dv->chunklen);
      else
         dv->avgmeds[ii] = tmpavg;
      dv->stds[ii] = sqrt(tmpvar);
      for (jj = 0; jj < dv->chunklen; jj++, offset++) {
         tmpval = dp->data[offset];
         if (tmpval > tmpmax)
            tmpmax = tmpval;
         if (tmpval < tmpmin)
            tmpmin = tmpval;
      }
      dv->maxs[ii] = tmpmax;
      if (tmpmax > dv->maxval)
         dv->maxval = tmpmax;
      dv->mins[ii] = tmpmin;
      if (tmpmin < dv->minval)
         dv->minval = tmpmin;
   }
   vect_free(tmpchunk);
   offset = dv->lon;
   if (zoomlevel > 0) {
      for (ii = 0, offset = dv->lon; ii < dv->numsamps; ii++, offset++)
         *(dv->vals + ii) = *(dp->data + offset);
   }
   return dv;
}
Example #29
0
void twopassfft_scratch(multifile * infile, multifile * scratch,
                        long long nn, int isign)
{
   long long n1, n2, bb, fp, ii, jj, kk, kind, df;
   int move_size;
   unsigned char *move;
   rawtype *data, *dp;
   double tmp1, tmp2, wtemp, wpi, wpr, wi, wr, delta;

   if (nn < 2)
      return;

   /* Treat the input data as a n1 (rows) x n2 (cols) */
   /* matrix.  Make sure that n2 >= n1.               */

   n1 = good_factor(nn);
   if (n1 == 0) {
      printf("\nLength of FFT in twopassfft_scratch() must be factorable\n\n");
      exit(1);
   }
   n2 = nn / n1;
   bb = find_blocksize(n1, n2);
   if (bb == 0) {
      printf("\nCan't factor the FFT length in twopassfft_scratch()\n");
      printf("   into useful sizes.\n\n");
      exit(1);
   }

   data = gen_rawvect(bb * n2);
   move_size = (bb + n2) / 2;
   move = (unsigned char *) malloc(move_size);

   /* First do n2 transforms of length n1 by  */
   /* fetching size bb x n1 blocks in memory. */

   for (ii = 0; ii < n2; ii += bb) {

      /* Read a n1 (rows) x bb (cols) block of data */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n2;
      for (jj = 0; jj < n1; jj++) {
         fseek_multifile(infile, fp, SEEK_SET);
         fread_multifile(dp, sizeof(rawtype), bb, infile);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }

      /* Transpose the n1 (rows) x bb (cols) block of data */

      transpose_fcomplex(data, n1, bb, move, move_size);

      /* Do bb transforms of length n1 */

      for (jj = 0; jj < bb; jj++)
         COMPLEXFFT(data + jj * n1, n1, isign);

      /* Multiply the matrix A(ii,jj) by exp(isign 2 pi i jj ii / nn). */
      /* Use recursion formulas from Numerical Recipes.                */

      for (jj = 0; jj < bb; jj++) {
         delta = isign * TWOPI * (ii + jj) / nn;
         wr = cos(delta);
         wi = sin(delta);
         wtemp = sin(0.5 * delta);
         wpr = -2.0 * wtemp * wtemp;
         wpi = wi;
         kind = jj * n1 + 1;
         for (kk = 1; kk < n1; kk++, kind++) {
            tmp1 = data[kind].r;
            tmp2 = data[kind].i;
            data[kind].r = tmp1 * wr - tmp2 * wi;
            data[kind].i = tmp2 * wr + tmp1 * wi;
            wtemp = wr;
            wr = wtemp * wpr - wi * wpi + wr;
            wi = wi * wpr + wtemp * wpi + wi;
         }
      }
      fwrite_multifile(data, sizeof(rawtype), bb * n1, scratch);
   }

   /* Now do n1 transforms of length n2 by fetching  */
   /* groups of size n2 (rows) x bb (cols) blocks.   */

   for (ii = 0; ii < n1; ii += bb) {

      /* Read two n2 (rows) x bb (cols) blocks from the file */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n1;
      for (jj = 0; jj < n2; jj++) {
         fseek_multifile(scratch, fp, SEEK_SET);
         fread_multifile(dp, sizeof(rawtype), bb, scratch);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }

      /* Transpose the n2 (rows) x bb (cols) block of data */

      transpose_fcomplex(data, n2, bb, move, move_size);

      /* Do bb transforms of length n2 */

      for (jj = 0; jj < bb; jj++)
         COMPLEXFFT(data + jj * n2, n2, isign);

      /* Transpose the bb (rows) x n2 (cols) block of data */

      transpose_fcomplex(data, bb, n2, move, move_size);

      /* Scale the data if needed */

      if (isign == 1) {
         tmp1 = 1.0 / (double) nn;
         for (jj = 0; jj < n2 * bb; jj++) {
            data[jj].r *= tmp1;
            data[jj].i *= tmp1;
         }
      }


      /* Write n2 (rows) x bb (cols) blocks to the file  */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n1;
      for (jj = 0; jj < n2; jj++) {
         fseek_multifile(infile, fp, SEEK_SET);
         fwrite_multifile(dp, sizeof(rawtype), bb, infile);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }
   }
   free(move);
   vect_free(data);
}
Example #30
0
int main(int argc, char *argv[])
{
   FILE *bytemaskfile;
   float **dataavg = NULL, **datastd = NULL, **datapow = NULL;
   float *chandata = NULL, powavg, powstd, powmax;
   float inttime, norm, fracterror = RFI_FRACTERROR;
   float *rawdata = NULL;
   unsigned char **bytemask = NULL;
   short *srawdata = NULL;
   char *outfilenm, *statsfilenm, *maskfilenm;
   char *bytemaskfilenm, *rfifilenm;
   int numchan = 0, numint = 0, newper = 0, oldper = 0, good_padvals = 0;
   int blocksperint, ptsperint = 0, ptsperblock = 0, padding = 0;
   int numcands, candnum, numrfi = 0, numrfivect = NUM_RFI_VECT;
   int ii, jj, kk, slen, numread = 0, insubs = 0;
   int harmsum = RFI_NUMHARMSUM, lobin = RFI_LOBIN, numbetween = RFI_NUMBETWEEN;
   double davg, dvar, freq;
   struct spectra_info s;
   presto_interptype interptype;
   rfi *rfivect = NULL;
   mask oldmask, newmask;
   fftcand *cands;
   infodata idata;
   Cmdline *cmd;

   /* Call usage() if we have no command line arguments */

   if (argc == 1) {
      Program = argv[0];
      printf("\n");
      usage();
      exit(0);
   }

   /* Parse the command line using the excellent program Clig */

   cmd = parseCmdline(argc, argv);
   spectra_info_set_defaults(&s);
   s.filenames = cmd->argv;
   s.num_files = cmd->argc;
   s.clip_sigma = cmd->clip;
   // -1 causes the data to determine if we use weights, scales, & 
   // offsets for PSRFITS or flip the band for any data type where
   // we can figure that out with the data
   s.apply_flipband = (cmd->invertP) ? 1 : -1;
   s.apply_weight = (cmd->noweightsP) ? 0 : -1;
   s.apply_scale  = (cmd->noscalesP) ? 0 : -1;
   s.apply_offset = (cmd->nooffsetsP) ? 0 : -1;
   s.remove_zerodm = (cmd->zerodmP) ? 1 : 0;
   if (cmd->noclipP) {
       cmd->clip = 0.0;
       s.clip_sigma = 0.0;
   }
   if (cmd->ifsP) {
       // 0 = default or summed, 1-4 are possible also
       s.use_poln = cmd->ifs;
   }
   slen = strlen(cmd->outfile) + 20;

#ifdef DEBUG
   showOptionValues();
#endif

   printf("\n\n");
   printf("               Pulsar Data RFI Finder\n");
   printf("                 by Scott M. Ransom\n\n");

   /* The following is the root of all the output files */

   outfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(outfilenm, "%s_rfifind", cmd->outfile);

   /* And here are the output file names */

   maskfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(maskfilenm, "%s.mask", outfilenm);
   bytemaskfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(bytemaskfilenm, "%s.bytemask", outfilenm);
   rfifilenm = (char *) calloc(slen, sizeof(char));
   sprintf(rfifilenm, "%s.rfi", outfilenm);
   statsfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(statsfilenm, "%s.stats", outfilenm);
   sprintf(idata.name, "%s", outfilenm);

   if (RAWDATA) {
       if (cmd->filterbankP) s.datatype = SIGPROCFB;
       else if (cmd->psrfitsP) s.datatype = PSRFITS;
       else if (cmd->pkmbP) s.datatype = SCAMP;
       else if (cmd->bcpmP) s.datatype = BPP;
       else if (cmd->wappP) s.datatype = WAPP;
       else if (cmd->spigotP) s.datatype = SPIGOT;
   } else {  // Attempt to auto-identify the data
       identify_psrdatatype(&s, 1);
       if (s.datatype==SIGPROCFB) cmd->filterbankP = 1;
       else if (s.datatype==PSRFITS) cmd->psrfitsP = 1;
       else if (s.datatype==SCAMP) cmd->pkmbP = 1;
       else if (s.datatype==BPP) cmd->bcpmP = 1;
       else if (s.datatype==WAPP) cmd->wappP = 1;
       else if (s.datatype==SPIGOT) cmd->spigotP = 1;
       else if (s.datatype==SUBBAND) insubs = 1;
       else {
           printf("Error:  Unable to identify input data files.  Please specify type.\n\n");
           exit(1);
       }
   }

   if (!cmd->nocomputeP) {

       if (RAWDATA || insubs) {
           char description[40];
           psrdatatype_description(description, s.datatype);
           if (s.num_files > 1)
               printf("Reading %s data from %d files:\n", description, s.num_files);
           else
               printf("Reading %s data from 1 file:\n", description);
           if (insubs) s.files = (FILE **)malloc(sizeof(FILE *) * s.num_files);
           for (ii = 0; ii < s.num_files; ii++) {
               printf("  '%s'\n", cmd->argv[ii]);
               if (insubs) s.files[ii] = chkfopen(cmd->argv[ii], "rb");
           }
           printf("\n");
       }           

       if (RAWDATA) {
           read_rawdata_files(&s);
           print_spectra_info_summary(&s);
           spectra_info_to_inf(&s, &idata);
           ptsperblock = s.spectra_per_subint;
           numchan = s.num_channels;
           idata.dm = 0.0;
       }
       
       if (insubs) {
           /* Set-up values if we are using subbands */
           char *tmpname, *root, *suffix;
           if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) {
               printf("Error:  The input filename (%s) must have a suffix!\n\n", s.filenames[0]);
               exit(1);
           }
           if (strncmp(suffix, "sub", 3) == 0) {
               tmpname = calloc(strlen(root) + 6, 1);
               sprintf(tmpname, "%s.sub", root);
               readinf(&idata, tmpname);
               free(tmpname);
           } else {
               printf("\nThe input files (%s) must be subbands!  (i.e. *.sub##)\n\n",
                      s.filenames[0]);
               exit(1);
           }
           free(root);
           free(suffix);
           ptsperblock = 1;
           /* Compensate for the fact that we have subbands and not channels */
           idata.freq = idata.freq - 0.5 * idata.chan_wid +
               0.5 * idata.chan_wid * (idata.num_chan / s.num_files);
           idata.chan_wid = idata.num_chan / s.num_files * idata.chan_wid;
           idata.num_chan = numchan = s.num_files;
           idata.dm = 0.0;
           sprintf(idata.name, "%s", outfilenm);
           writeinf(&idata);
           s.padvals = gen_fvect(s.num_files);
           for (ii = 0 ; ii < s.num_files ; ii++)
               s.padvals[ii] = 0.0;
       }

       /* Read an input mask if wanted */
       if (cmd->maskfileP) {
           read_mask(cmd->maskfile, &oldmask);
           printf("Read old mask information from '%s'\n\n", cmd->maskfile);
           good_padvals = determine_padvals(cmd->maskfile, &oldmask, s.padvals);
       } else {
           oldmask.numchan = oldmask.numint = 0;
       }

      /* The number of data points and blocks to work with at a time */

      if (cmd->blocksP) {
         blocksperint = cmd->blocks;
         cmd->time = blocksperint * ptsperblock * idata.dt;
      } else {
         blocksperint = (int) (cmd->time / (ptsperblock * idata.dt) + 0.5);
      }
      ptsperint = blocksperint * ptsperblock;
      numint = (long long) idata.N / ptsperint;
      if ((long long) idata.N % ptsperint)
         numint++;
      inttime = ptsperint * idata.dt;
      printf("Analyzing data sections of length %d points (%.6g sec).\n",
             ptsperint, inttime);
      {
         int *factors, numfactors;

         factors = get_prime_factors(ptsperint, &numfactors);
         printf("  Prime factors are:  ");
         for (ii = 0; ii < numfactors; ii++)
            printf("%d ", factors[ii]);
         printf("\n");
         if (factors[numfactors - 1] > 13) {
            printf("  WARNING:  The largest prime factor is pretty big!  This will\n"
                   "            cause the FFTs to take a long time to compute.  I\n"
                   "            recommend choosing a different -time value.\n");
         }
         printf("\n");
         free(factors);
      }

      /* Allocate our workarrays */

      if (RAWDATA)
          rawdata = gen_fvect(idata.num_chan * ptsperblock * blocksperint);
      else if (insubs)
          srawdata = gen_svect(idata.num_chan * ptsperblock * blocksperint);
      dataavg = gen_fmatrix(numint, numchan);
      datastd = gen_fmatrix(numint, numchan);
      datapow = gen_fmatrix(numint, numchan);
      chandata = gen_fvect(ptsperint);
      bytemask = gen_bmatrix(numint, numchan);
      for (ii = 0; ii < numint; ii++)
         for (jj = 0; jj < numchan; jj++)
            bytemask[ii][jj] = GOODDATA;
      rfivect = rfi_vector(rfivect, numchan, numint, 0, numrfivect);
      if (numbetween == 2)
         interptype = INTERBIN;
      else
         interptype = INTERPOLATE;

      /* Main loop */

      printf("Writing mask data  to '%s'.\n", maskfilenm);
      printf("Writing  RFI data  to '%s'.\n", rfifilenm);
      printf("Writing statistics to '%s'.\n\n", statsfilenm);
      printf("Massaging the data ...\n\n");
      printf("Amount Complete = %3d%%", oldper);
      fflush(stdout);

      for (ii = 0; ii < numint; ii++) { /* Loop over the intervals */
         newper = (int) ((float) ii / numint * 100.0 + 0.5);
         if (newper > oldper) {
            printf("\rAmount Complete = %3d%%", newper);
            fflush(stdout);
            oldper = newper;
         }

         /* Read a chunk of data */

         if (RAWDATA)
             numread = read_rawblocks(rawdata, blocksperint, &s, &padding);
         else if (insubs)
             numread = read_subband_rawblocks(s.files, s.num_files,
                                              srawdata, blocksperint, &padding);

         if (padding)
            for (jj = 0; jj < numchan; jj++)
               bytemask[ii][jj] |= PADDING;

         for (jj = 0; jj < numchan; jj++) {     /* Loop over the channels */

             if (RAWDATA)
                 get_channel(chandata, jj, blocksperint, rawdata, &s);
             else if (insubs)
                 get_subband(jj, chandata, srawdata, blocksperint);

            /* Calculate the averages and standard deviations */
            /* for each point in time.                        */

            if (padding) {
                dataavg[ii][jj] = 0.0;
                datastd[ii][jj] = 0.0;
                datapow[ii][jj] = 1.0;
            } else {
               avg_var(chandata, ptsperint, &davg, &dvar);
               dataavg[ii][jj] = davg;
               datastd[ii][jj] = sqrt(dvar);
               realfft(chandata, ptsperint, -1);
               numcands = 0;
               norm = datastd[ii][jj] * datastd[ii][jj] * ptsperint;
               if (norm == 0.0)
                  norm = (chandata[0] == 0.0) ? 1.0 : chandata[0];
               cands = search_fft((fcomplex *) chandata, ptsperint / 2,
                                  lobin, ptsperint / 2, harmsum,
                                  numbetween, interptype, norm, cmd->freqsigma,
                                  &numcands, &powavg, &powstd, &powmax);
               datapow[ii][jj] = powmax;

               /* Record the birdies */

               if (numcands) {
                  for (kk = 0; kk < numcands; kk++) {
                     freq = cands[kk].r / inttime;
                     candnum = find_rfi(rfivect, numrfi, freq, RFI_FRACTERROR);
                     if (candnum >= 0) {
                        update_rfi(rfivect + candnum, freq, cands[kk].sig, jj, ii);
                     } else {
                        update_rfi(rfivect + numrfi, freq, cands[kk].sig, jj, ii);
                        numrfi++;
                        if (numrfi == numrfivect) {
                           numrfivect *= 2;
                           rfivect = rfi_vector(rfivect, numchan, numint,
                                                numrfivect / 2, numrfivect);
                        }
                     }
                  }
                  free(cands);
               }
            }
         }
      }
      printf("\rAmount Complete = 100%%\n");

      /* Write the data to the output files */

      write_rfifile(rfifilenm, rfivect, numrfi, numchan, numint,
                    ptsperint, lobin, numbetween, harmsum,
                    fracterror, cmd->freqsigma);
      write_statsfile(statsfilenm, datapow[0], dataavg[0], datastd[0],
                      numchan, numint, ptsperint, lobin, numbetween);

   } else {                     /* If "-nocompute" */
      float freqsigma;

      /* Read the data from the output files */

      printf("Reading  RFI data  from '%s'.\n", rfifilenm);
      printf("Reading statistics from '%s'.\n", statsfilenm);
      readinf(&idata, outfilenm);
      read_rfifile(rfifilenm, &rfivect, &numrfi, &numchan, &numint,
                   &ptsperint, &lobin, &numbetween, &harmsum,
                   &fracterror, &freqsigma);
      numrfivect = numrfi;
      read_statsfile(statsfilenm, &datapow, &dataavg, &datastd,
                     &numchan, &numint, &ptsperint, &lobin, &numbetween);
      bytemask = gen_bmatrix(numint, numchan);
      printf("Reading  bytemask  from '%s'.\n\n", bytemaskfilenm);
      bytemaskfile = chkfopen(bytemaskfilenm, "rb");
      chkfread(bytemask[0], numint * numchan, 1, bytemaskfile);
      fclose(bytemaskfile);
      for (ii = 0; ii < numint; ii++)
         for (jj = 0; jj < numchan; jj++)
            bytemask[ii][jj] &= PADDING;        /* Clear all but the PADDING bits */
      inttime = ptsperint * idata.dt;
   }

   /* Make the plots and set the mask */

   {
      int *zapints, *zapchan;
      int numzapints = 0, numzapchan = 0;

      if (cmd->zapintsstrP) {
         zapints = ranges_to_ivect(cmd->zapintsstr, 0, numint - 1, &numzapints);
         zapints = (int *) realloc(zapints, (size_t) (sizeof(int) * numint));
      } else {
         zapints = gen_ivect(numint);
      }
      if (cmd->zapchanstrP) {
         zapchan = ranges_to_ivect(cmd->zapchanstr, 0, numchan - 1, &numzapchan);
         zapchan = (int *) realloc(zapchan, (size_t) (sizeof(int) * numchan));
      } else {
         zapchan = gen_ivect(numchan);
      }
      rfifind_plot(numchan, numint, ptsperint, cmd->timesigma, cmd->freqsigma,
                   cmd->inttrigfrac, cmd->chantrigfrac,
                   dataavg, datastd, datapow, zapchan, numzapchan,
                   zapints, numzapints, &idata, bytemask,
                   &oldmask, &newmask, rfivect, numrfi,
                   cmd->rfixwinP, cmd->rfipsP, cmd->xwinP);

      vect_free(zapints);
      vect_free(zapchan);
   }

   /* Write the new mask and bytemask to the file */

   write_mask(maskfilenm, &newmask);
   bytemaskfile = chkfopen(bytemaskfilenm, "wb");
   chkfwrite(bytemask[0], numint * numchan, 1, bytemaskfile);
   fclose(bytemaskfile);

   /* Determine the percent of good and bad data */

   {
      int numpad = 0, numbad = 0, numgood = 0;

      for (ii = 0; ii < numint; ii++) {
         for (jj = 0; jj < numchan; jj++) {
            if (bytemask[ii][jj] == GOODDATA) {
               numgood++;
            } else {
               if (bytemask[ii][jj] & PADDING)
                  numpad++;
               else
                  numbad++;
            }
         }
      }
      printf("\nTotal number of intervals in the data:  %d\n\n", numint * numchan);
      printf("  Number of padded intervals:  %7d  (%6.3f%%)\n",
             numpad, (float) numpad / (float) (numint * numchan) * 100.0);
      printf("  Number of  good  intervals:  %7d  (%6.3f%%)\n",
             numgood, (float) numgood / (float) (numint * numchan) * 100.0);
      printf("  Number of  bad   intervals:  %7d  (%6.3f%%)\n\n",
             numbad, (float) numbad / (float) (numint * numchan) * 100.0);
      qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_sigma);
      printf("  Ten most significant birdies:\n");
      printf("#  Sigma     Period(ms)      Freq(Hz)       Number \n");
      printf("----------------------------------------------------\n");
      for (ii = 0; ii < 10; ii++) {
         double pperr;
         char temp1[40], temp2[40];

         if (rfivect[ii].freq_var == 0.0) {
            pperr = 0.0;
            sprintf(temp1, " %-14g", rfivect[ii].freq_avg);
            sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg);
         } else {
            pperr = 1000.0 * sqrt(rfivect[ii].freq_var) /
                (rfivect[ii].freq_avg * rfivect[ii].freq_avg);
            nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var),
                          -15);
            nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15);
         }
         printf("%-2d %-8.2f %13s %13s %-8d\n", ii + 1, rfivect[ii].sigma_avg,
                temp2, temp1, rfivect[ii].numobs);
      }
      qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_numobs);
      printf("\n  Ten most numerous birdies:\n");
      printf("#  Number    Period(ms)      Freq(Hz)       Sigma \n");
      printf("----------------------------------------------------\n");
      for (ii = 0; ii < 10; ii++) {
         double pperr;
         char temp1[40], temp2[40];

         if (rfivect[ii].freq_var == 0.0) {
            pperr = 0.0;
            sprintf(temp1, " %-14g", rfivect[ii].freq_avg);
            sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg);
         } else {
            pperr = 1000.0 * sqrt(rfivect[ii].freq_var) /
                (rfivect[ii].freq_avg * rfivect[ii].freq_avg);
            nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var),
                          -15);
            nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15);
         }
         printf("%-2d %-8d %13s %13s %-8.2f\n", ii + 1, rfivect[ii].numobs,
                temp2, temp1, rfivect[ii].sigma_avg);
      }
      printf("\nDone.\n\n");
   }

   /* Close the files and cleanup */

   free_rfi_vector(rfivect, numrfivect);
   free_mask(newmask);
   if (cmd->maskfileP)
      free_mask(oldmask);
   free(outfilenm);
   free(statsfilenm);
   free(bytemaskfilenm);
   free(maskfilenm);
   free(rfifilenm);
   vect_free(dataavg[0]);
   vect_free(dataavg);
   vect_free(datastd[0]);
   vect_free(datastd);
   vect_free(datapow[0]);
   vect_free(datapow);
   vect_free(bytemask[0]);
   vect_free(bytemask);
   if (!cmd->nocomputeP) {
       //  Close all the raw files and free their vectors
       close_rawfiles(&s);
       vect_free(chandata);
       if (insubs)
           vect_free(srawdata);
       else
           vect_free(rawdata);
   }
   return (0);
}