Exemplo n.º 1
0
int main() {
    FILE *in = fopen("task.in", "r");
    FILE *out = fopen("task.out", "w");
    int length = intScan(in);
    int array[length];
    
    arrayScan(in, array, length);
    arrayAbs(array, length);
    
    fclose(in);
    
    arrayPrint(out, array, length);
    fclose(out);
    return 0;
}
Exemplo n.º 2
0
int resistmean (float *in, int npix, float sigrej, float *mean, 
		float *sigmean, float *min, float *max) {

/* Arguments:
**	in	i: pointer to array of input values 
**	npix	i: size of the input array
**	sigrej	i: the sigma level for pixel rejection
**	mean	o: mean value of unrejected pixels
**	sigmean	o: standard deviation of unrejected pixels
**		   (NOT the uncertainty or sigma of the mean)
**      min	o: minimum of input values
**      max	o: maximum of input values
*/

	/* Local variables */
	int i, j;	  /* loop indexes */
	float median;     /* median value of input array */
	float *absdev; 	  /* absolute deviation array */
	float medabsdev;  /* median of the absdev array */
	float *tempdata;  /* array of good pixel values */
	float *tempdata2; /* array of good pixel values */
	double sum;       /* sum of pixel values */
	float cutoff;	  /* value limitation */
	int   npix1, npix2;  /* number of pixels in temp arrays */
    
	/* Function definitions */
	float findRMedian (float *, int );        /* compute median of array */
   	float findMean(float *, int );              /* compute mean of array */
	float findSigma(float *, int , float *);  /* compute stddev of array */
	void arrayDiff(float *, float *, float , int );   /* subtract median */
	void arrayAbs(float *,int);       /* compute absolute value of array */
	void wheregood(float *, float , int * );   /* flag outliers in array */
    
	/* Initialize the counters and results */
	npix1 = npix;
	*mean = 0.;
	*sigmean = 0.;
	*min = 0;
	*max = 0;
	median = 0.;
	medabsdev = 0.;
	cutoff = 0.;

	/* allocate temp arrays to store and manipulate the input array */
	if (npix != 0) {
        
	    /* an array to store a copy of the data*/
	    tempdata = (float *) calloc(npix, sizeof(float));
	    if (tempdata == NULL) {
		sprintf (MsgText, "Memory allocation failure in resistmean");
        	trlmessage (MsgText);
		return (1);
	    }

	    /* and an array to store absolute deviation */
	    absdev = (float *) calloc(npix, sizeof(float));
	    if (absdev == NULL) {
		sprintf (MsgText, "Memory allocation failure in resistmean");
        	trlmessage (MsgText);
		return (1);
	    }

	} else {
	    sprintf (MsgText, "Zero size array passed to resistmean");
	    trlerror (MsgText);
	    return (1);
	}
              
	/* Copy the input array, computing the initial sum, min, max */
	sum = 0.;
	*min = tempdata[0];
	*max = tempdata[0];
	for (i=0; i<npix1; i++) {
	     tempdata[i] = in[i];
	     sum  += tempdata[i];
	     if (tempdata[i] < *min) *min = tempdata[i];
	     if (tempdata[i] > *max) *max = tempdata[i];
	}

	/* Compute the mean and median of the unrejected values */
	*mean = (float) (sum/(double)npix1);
	median = findRMedian (tempdata, npix1);

	/* Subtract the median from every element in the array */
	arrayDiff (tempdata, absdev, median, npix1);

	/* Get the absolute value for each element in the resulting array */
	arrayAbs (absdev, npix1);

	/* Get the median of the absolute deviation array and divide by a 
	   constant with some logic attached */
	medabsdev = findRMedian (absdev, npix1) / 0.6745; 
	if (medabsdev < 1.0E-24)
	    medabsdev = findMean(absdev,npix1) / 0.8;

	/* Compute the cutoff value in terms of the median absolute deviation */
	cutoff = (sigrej) * medabsdev;

	/* Flag values in the array that are beyond the cutoff */
	wheregood (absdev, cutoff, &npix1);

	/* Start a new temp array to populate with the good values;
	   npix1 should be the count of unrejected pixels here */
	tempdata2 = (float *) calloc(npix1, sizeof(float));
	if (tempdata2 == NULL) {
	    sprintf (MsgText, "Memory allocation failure in resistmean");
	    trlmessage (MsgText);
	    return (1);
	}

	/* Copy only the good values into the new temp array.
	   npix is still the number of original input values, while npix1
	   is now the number of remaining good values. */
	j=0;
	for (i=0; i<npix; i++) {
	     if (absdev[i] != PFLAG) {	
		 tempdata2[j]=tempdata[i];
		 j++;
	     }
	}

	/************ ROUND 2 ***********/

	/* Compute the mean and stddev of the values in the new array */
	*mean = findMean(tempdata2, npix1);    
	*sigmean = findSigma(tempdata2, npix1, mean); 

	/* Compensate sigma for truncation and compute new cutoff */
 	if (sigrej <= 4.5) {
	    *sigmean = *sigmean / (-0.15405+0.90723*sigrej - 0.23584*sigrej*sigrej+0.020142*sigrej*sigrej*sigrej);
	}
 	cutoff = sigrej * (*sigmean); 

	/* Reinitialize the absdev array to hold new set of values */
	free(absdev); /* clear the old array */
	absdev = (float *) calloc(npix1, sizeof(float));
	if (absdev == NULL) {
	    sprintf (MsgText, "Memory allocation failure in resistmean");
	    return (1);
	}
    
	/* Find the median of the good values. */
	median = findRMedian(tempdata2, npix1);

	/* Subtract the median from every element in the array */
	arrayDiff (tempdata2, absdev, median, npix1);

	/* Get the absolute value for each element in the resulting array */
	arrayAbs (absdev, npix1);

	/* Get the median of the absolute deviation array */
	medabsdev = findRMedian(absdev, npix1) / 0.6745; 
	if (medabsdev < 1.0E-24)
	    medabsdev = findMean(absdev, npix1) / 0.8;

	/* Flag values in the array that are beyond the new cutoff */
	npix2 = npix1;
	wheregood(absdev, cutoff, &npix2);

	/* Start a new temp array to populate with just the final good values */
	free (tempdata); /* free the previous copy */
	tempdata = (float *) calloc(npix2, sizeof(float));
	if (tempdata == NULL) {
	    sprintf (MsgText, "Memory allocation failure in resistmean");
	    return (1);
	}
    
	/* Copy only the good values to the new array */
	j=0;
	for (i=0; i<npix1; i++) {
	     if (absdev[i] != PFLAG) {	
		 tempdata[j] = tempdata2[i];
		 j++;
	     }
	}

	/* Compute the mean and stddev of the latest array of good values */
	*mean    = findMean (tempdata, npix2);
	*sigmean = findSigma(tempdata, npix2, mean); 

	/* Compensate sigma for truncation :*/
 	if (sigrej <= 4.5) {
	    *sigmean = *sigmean / (-0.15405+0.90723*sigrej-0.23584*sigrej*sigrej+0.020142*sigrej*sigrej*sigrej);
	}

	/* Free all local arrays */
	free(absdev);
	free(tempdata);
	free(tempdata2);

	return (0);    	/* Successful return */
}