Beispiel #1
0
int read_and_process(char* path)
{
	fitsfile *fptr;    
    int status = 0;
    unsigned int bitpix, naxis;
    long naxes[3];
    long nelements;
    int anynul;
    unsigned char* image;
    
	if (!fits_open_file(&fptr, path, READONLY, &status))
    {
		if (!fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status) )
        {
			if (naxis != 3){
				printf("Not a 3D fits\n");
				return -1;
			}
			
			if (bitpix != 8){
				printf("Process only 8 bit fits\n");
				return -1;
			}
			
			printf("bitpix=%d naxes=%d width=%ld height=%ld\n",bitpix,naxis,naxes[0],naxes[1]);
			nelements = naxes[0]*naxes[1];
			if (width==0 && height==0){
				width = naxes[0];
				height = naxes[1];
			} else {
				if (width != naxes[0] || height != naxes[1]){
					printf("naxes error: %ldx%ld instead of %ldx%ld\n",naxes[0],naxes[1],width,height);
					return -1;
				}
			}
			
			image = malloc(sizeof(unsigned char)*nelements);
			
			fits_read_img(fptr,TBYTE,1,nelements,NULL,image, &anynul, &status);
			write_fits("red.fits",image);
			fits_read_img(fptr,TBYTE,nelements +1,nelements,NULL,image, &anynul, &status);
			write_fits("green.fits",image);
			fits_read_img(fptr,TBYTE,2* nelements +1,nelements,NULL,image, &anynul, &status);
			write_fits("blue.fits",image);
			fits_close_file(fptr, &status);
			
			free(image);
						
			return status;
	
		  }
     }
     return -1;		
}
Beispiel #2
0
size_t write_memory(void** mem, size_t width, size_t height, size_t noutput, cl_float* output[], const char* names[])
{
    int status = 0;
    
    // memory block
    *mem = NULL;
    size_t siz = 0;
    
    // the FITS file
    fitsfile* fptr;
    
    // create in-memory FITS
    fits_create_memfile(&fptr, mem, &siz, 0, realloc, &status);
    
    // write in-memory FITS
    write_fits(fptr, TFLOAT, width, height, noutput, (void**)output, names, &status);
    
    // close in-memory FITS
    fits_close_file(fptr, &status);
    
    // report FITS errors
    if(status)
        fits_error(NULL, status);
    
    // return the in-memory FITS
    return siz;
}
Beispiel #3
0
void nemo_main()
{
  int i;

  setparams();                               /* set cmdln par's */
  instr = stropen (getparam("in"), "r");     /* open image file */
  for (i=0; i<isel; i++)
    if (read_image (instr,&iptr)==0)
      error("Cannot process image select=%d",i);
  headline = ask_headline();                 /* possible headline */
  strclose(instr);                           /* close image file */
  write_fits(getparam("out"),iptr);          /* write fits file */
  free_image(iptr);
}
Beispiel #4
0
int main(int argc, char* argv[]) {
	unsigned char* a;
	unsigned char* b;
	unsigned char* c;
	long nelements;
	int i;
	int errors = 0;
	unsigned char maxi = 0;
	unsigned char delta;
	
	if (argc != 4){
		printf("Usage: %s <raw> <offset or dark> <result>\n",argv[0]);
		return -1;
	}
	
	if (read_fits(argv[1],&a) == 0){
		if (read_fits(argv[2],&b) == 0){	
			nelements = width*height;
			c = malloc(sizeof(unsigned char)*nelements);
			for(i=0;i<nelements;i++){
				if (a[i] >= b[i])
					c[i] = a[i] - b[i];
				else {
					c[i] = 0;
					delta = b[i] - a[i];
					if (delta > maxi) maxi = delta;
					errors++;
				}
			}
			printf("%d errors : maxi =%d\n",errors,maxi);
			
			remove(argv[3]);
			if (write_fits(argv[3],c) == 0){
				free(a); free(b); free(c);
				return 0;
			}
		}
	}
	free(a); free(b); free(c);
	return -1;
	
}
Beispiel #5
0
void write_output(const char* filename, size_t width, size_t height, size_t noutput, cl_float* output[], const char* names[])
{
    int status = 0;
    
    // the FITS file
    fitsfile* fptr;
    
    // create FITS file
    fits_create_file(&fptr, filename, &status);
    
    // write FITS file
    write_fits(fptr, TFLOAT, width, height, noutput, (void**)output, names, &status);
               
    // close FITS file
    fits_close_file(fptr, &status);
    
    // report FITS errors
    if(status)
        fits_error(filename, status);
}
int main(int argc, char *argv[]) {

    int arg=1;
    int sbig_type = NO_CAMERA;
    int info_mode = 0;
    int verbose = 0;
    char name[MAX_STRING] = "";
    char ra[MAX_STRING] = "";
    char dec[MAX_STRING] = "";
    char alt[MAX_STRING] = "";
    char az[MAX_STRING] = "";
    char imtype[8];
    int phase;
    float exptime;
    int err;

    /* Set an interrupt handler to trap Ctr-C nicely */
    if(signal(SIGINT, SIG_IGN) != SIG_IGN)
		signal(SIGINT, InterruptHandler);

    /*  set lockfile from the outset */
    get_lock();
    store_pid_in_lockfile();


    /* parse args */
    if (argc < 3) {
        error_exit(usage);
    };
    while (arg < argc - 2) 
    {
        switch (argv[arg++][1]) {
            case 'v':
                verbose = 1;
                SetVerbosity(verbose);
                break;
            case 'n':
                sscanf(argv[arg++], "%s", name);
                break;
             case 'r':
                sscanf(argv[arg++], "%s", ra);
                break;
             case 'd':
                sscanf(argv[arg++], "%s", dec);
                break;
             case 'a':
                sscanf(argv[arg++], "%s", alt);
                break;
             case 'z':
                sscanf(argv[arg++], "%s", az);
                break;
             default:
                error_exit(usage);
                break;
        }
    }
    sscanf(argv[arg++],"%s",imtype);
    sscanf(argv[arg++],"%f",&exptime);

    // Determine what kind of image to take 
    switch(value_from_imagetype_key(imtype)) {
        case DARK:   ccd_type=DARK;  if (verbose) printf("Taking dark frame.\n");  break;
        case LIGHT:  ccd_type=LIGHT; if (verbose) printf("Taking light frame.\n"); break;
        case BIAS:   ccd_type=BIAS;  if (verbose) printf("Taking bias frame.\n");  break;
        case FLAT:   ccd_type=FLAT;  if (verbose) printf("Taking flat frame.\n");  break;
        case BADKEY: fprintf(stderr,"Unknown image type %s\n",imtype); return(1); break;
    }
    fflush(stdout);

    CountCameras();
    if (ccd_ncam < 1){
	fprintf(stderr,"Found 0 cameras\n");
        return(1);
    }

    InitializeAllCameras();
    store_timestamped_note_in_lockfile("Started");
    store_directory_in_lockfile();
    char myline[128];
    sprintf(myline,"Exptime: %5.1f\n",exptime);
    store_note_in_lockfile(myline);


    // Start integrations going on each of the cameras one by one.
    for (int cam_num = 0; cam_num <ccd_ncam; cam_num++)
    {
        err = SetActiveCamera(cam_num);
        ccd_image_data[cam_num] = 
            (unsigned short *) malloc(ccd_image_width*ccd_image_height*sizeof(unsigned short));
        phase = 0;
        err = CaptureImage(&phase,ccd_image_data[cam_num],ccd_type,exptime,FALSE,0,0,0,0);
    }

    // Wait until the data is ready. I'm intentionally playing this safe by
    // making sure I wait at least 1s before trying to read out the data.
    // This is something I will have to look into if I ever use this for
    // fast focusing.
    int nsec = (int) exptime + 1;
    int count = 0;
    for (int i=0; i<=nsec;i++)
    {
        if(nsec > 3 && nsec < 10){ 
            load_bar(count++,nsec,3,30);
        }
        else if (nsec >  10 && nsec < 100){
            load_bar(count++,nsec,(int)(nsec/2),30);
        }
        else if (nsec >  100){
            load_bar(count++,nsec,(int)(nsec/3),30);
        }
        sleep(1);
    }

    // The cameras are ready to be read out. We once again cycle over each camera and 
    // save the data.
    
    for (int cam_num = 0; cam_num <ccd_ncam; cam_num++)
    {
        char infoline[128];
        phase = 1;
        err = SetActiveCamera(cam_num); 
        fflush(stderr);
        err = CaptureImage(&phase,ccd_image_data[cam_num],ccd_type,exptime,FALSE,0,0,0,0);

        // Print out some pixel values to let the user check data integrity
        if (verbose)
            printf("Some pixel values: %u %u %d\n",
                    *(ccd_image_data[cam_num] + 10000), 
                    *(ccd_image_data[cam_num] + 15000), 
                    *(ccd_image_data[cam_num]+20000)); 

        // Figure out what to call the new file
        char *newname;
        newname = (char *)malloc(MAX_STRING*sizeof(char));
        new_filename(ccd_serial_number,imtype,newname);    

        // Save as a FITS file
        GetCameraTemperature();
        double temperature = ccd_camera_info[ActiveCamera()].temperature;
        int filterNumber = 0;
        if (IsCameraAnST402ME())
            filterNumber = FilterWheelPosition();
        write_fits(newname, ccd_image_width, ccd_image_height, ccd_image_data[cam_num], 
                   exptime,imtype,temperature,filterNumber,ccd_serial_number, name,
                   ra,dec,alt,az);
        fprintf(stderr,"Saved %s \n",newname);
        sprintf(infoline,"Camera %d wrote: %s\n",cam_num,newname);
        store_note_in_lockfile(infoline);
        free(ccd_image_data[cam_num]);
        free(newname);

    }

    DisconnectAllCameras();
    release_lock();

    store_timestamped_note_in_lockfile("Completed");

    // Release the lock file
    release_lock();

    // Ring bell to wake up the astronomer
    if (verbose)
    	printf("Camera(s) opened and closed successfully.\n");
    putchar('\a'); putchar('\a'); putchar('\a');

    return(0);

}
Beispiel #7
0
int main(int argc, char* argv[]) {

	unsigned char* c;
	long nelements;

	unsigned char factor;
	unsigned char thresholda;
	unsigned char thresholdb;
	unsigned long  ul;
	
	unsigned char mini;
	unsigned char maxi;
	int i,j,k,l;
	int n;
	int na, nb;
	unsigned char us;
	int va,vb,vc,size,index;
	
	Tpivot pivota[4];
	Tpivot pivotb[4];
	
	Px* pxa;
	Px* pxb; 
	Px* pxl;
	
	int* a2;
	int* b2;
	
	int iter;
	
	int dx,dy,distance,distmini;
	
	int x,y;
	int x0, y0;
	int x1, y1;
	
	int upper;
	
	int lista[10];
	int listb[10];
	
	int pointa[20];
	int pointb[20];
	
	int point[20];
	
	int la,lb;
	
	int go;
	
	int nsample;
	
	int ii;
	
	
	nsample = 20;
	

	x=0;
	y=0;
	x0=0;
	y0=0;
	
	xmini = 0;
	ymini = 0;
	
	unsigned int* score;
	
	
	if (argc != 5){
		printf("Usage: %s <image1> <image2> <result> <factor>\n",argv[0]);
		return -1;
	}
	
	factor = atoi(argv[4]);
	
	if (read_fits(argv[1],&a) == 0){
		if (read_fits(argv[2],&b) == 0){
			nelements = height * width;
			
			// detect maxi/mini
			mini = UCHAR_MAX;
			maxi = 0;
			for (i=0;i<nelements;i++){
				us = a[i];
				if ( us < mini ) mini = us;
				if ( us > maxi ) maxi = us;
			}
			ul = (unsigned long)(maxi-mini)*(unsigned long)factor;
			thresholda = (unsigned char)(ul/100L)+mini;
			printf("mini=%d - maxi=%d - threshold=%d\n",mini,maxi,thresholda);
			// 
			// detect stars
			aa = malloc(sizeof(unsigned char)*nelements);
			for (i=0;i<nelements;i++){
				aa[i] = 0;
			}
			n = 0;
			for (y=1;y<height-1;y++)
				for(x=1;x<width-1;x++){
					i = y*width+x;
					us = a[i];
					aa[i] = us;
					for(y0=y-1;y0<=y+1;y0++){
						for(x0=x-1;x0<=x+1;x0++){
							if (a[y0*width+x0] > us)
							{
								aa[i] = 0;
								break;
							}
						}
					}
					if (aa[i]!=0){
						//printf("(%d,%d)\n",x,y);
						n++;
					}	
				}
								
			printf("detect %d/%ld\n",n,nelements);
			
			na = n;
			pxa = malloc(sizeof(Px)*na);
			
			// filter 
			upper = INT_MAX;
			j = 0;
			for (i=0;i<nsample;i++){
				//printf(". %d\n",upper);
				ii = get_max_uchar(aa,nelements,&upper);
				//printf(".. %d\n",upper);
				if (upper == 0) break;
				pxa[j].x = ii % width;
				pxa[j].y = ii / width;
				j++;
			}
				
			na = j;
						
			a2 = malloc(sizeof(int)*na*na);
			for(i=0;i<na;i++)
				for(j=0;j<na;j++){
					dx=pxa[i].x-pxa[j].x;
					dy=pxa[i].y-pxa[j].y;
					distance=dx*dx+dy*dy;
					distance = (int)sqrt((double)distance);
					//printf("%d:%d -> %d\n",i,j,distance);
					a2[j*na+i]=distance;	
				}
			
			// detect maxi/mini
			mini = UCHAR_MAX;
			maxi = 0;
			for (i=0;i<nelements;i++){
				us = b[i];
				if ( us < mini ) mini = us;
				if ( us > maxi ) maxi = us;
			}
			ul = (unsigned long)(maxi-mini)*(unsigned long)factor;
			thresholdb = (unsigned char)(ul/100L)+mini;
			printf("mini=%d - maxi=%d - threshold=%d\n",mini,maxi,thresholdb);	
			// detect stars
			bb = malloc(sizeof(unsigned char)*nelements);
			for (i=0;i<nelements;i++){
				bb[i] = 0;
			}
			n = 0;
			for (y=1;y<height-1;y++)
				for(x=1;x<width-1;x++){
					i = y*width+x;
					us = b[i];
					bb[i] = us;
					for(y0=y-1;y0<=y+1;y0++){
						for(x0=x-1;x0<=x+1;x0++){
							if (b[y0*width+x0] > us)
							{
								bb[i] = 0;
								break;
							}
						}
					}
					if (bb[i]!=0){
						//printf("(%d,%d)\n",x,y);
						n++;
					}		
				}
			printf("detect %d/%ld\n",n,nelements);	
			
			nb = n;
			pxb = malloc(sizeof(Px)*nb);
			
			// filter 
			upper = INT_MAX;
			j = 0;
			for (i=0;i<nsample;i++){
				ii = get_max_uchar(bb,nelements,&upper);
				//printf(".. %d\n",upper);
				if (upper == 0) break;
				pxb[j].x = ii % width;
				pxb[j].y = ii / width;
				j++;
			}
			
			nb = j;
					
			b2 = malloc(sizeof(int)*nb*nb);
			for(i=0;i<nb;i++)
				for(j=0;j<nb;j++){
					dx=pxb[i].x-pxb[j].x;
					dy=pxb[i].y-pxb[j].y;
					distance=dx*dx+dy*dy;
					distance = (int)sqrt((double)distance);
					//printf("%d:%d -> %d\n",i,j,distance);
					b2[j*nb+i]=distance;	
				}	
				
				
			// segment correspondance
			upper = INT_MAX;
			go = 1;
			i = 0;
			while ( go ) {
				la = get_max(a2,na*na,&upper);
				if (la>=0) {
					//printf("upper=%d a=%d\n",upper,la);
					lb = get_index(b2,nb*nb,upper);
					if (lb>=0) {
						//printf("found b=%d\n",lb);
						lista[i] = la;
						listb[i++] = lb;
						if (i==10) go=0;
					}
				} else {
					go = 0;
				}
			}
			
			size = i;
			printf("distance match=%d\n",size);
			
			if (size == 0) {
				printf ("No matching found!\n");
				exit(0);
			}
			
			for(j=0;j<size;j++){
				la = lista[j];
				pointa[2*j  ]=la%na;
				pointa[2*j+1]=la/na;
				lb = listb[j];
				pointb[2*j  ]=lb%nb;
				pointb[2*j+1]=lb/nb;
			}
			
			pxl = malloc(sizeof(Px)*size);
			
			// middle of segments
			
			for(j=0;j<size;j++){
				x0 = pointa[2*j];
				x1 = pointa[2*j+1];
				y0 = pointb[2*j];
				y1 = pointb[2*j+1];
				//printf("a (%d,%d) - (%d,%d)\n",pxa[x0].x,pxa[x0].y,pxa[x1].x,pxa[x1].y);
				x = (pxa[x0].x+pxa[x1].x) - (pxb[y0].x+pxb[y1].x);
				x = x/2;
				//printf("b (%d,%d) - (%d,%d)\n",pxb[y0].x,pxb[y0].y,pxb[y1].x,pxb[y1].y);
				y = (pxa[x0].y+pxa[x1].y) - (pxb[y0].y+pxb[y1].y);
				y = y/2;
				pxl[j].x = x;
				pxl[j].y = y;
				//printf("dx=%d , dy=%d\n",x,y);
			}
			
			score = malloc(sizeof(unsigned int)*size);
			
			for (i=0;i<size;i++){
				score[i] = 0;
				for(j=i;j<size;j++)
					if ((pxl[i].x == pxl[j].x) && (pxl[i].y == pxl[j].y))
						score[i]++;
			}
			
			maxi = 0;
			index = 0;
			for (i=0;i<size;i++){
				//printf("%d:%d (%d,%d)\n",i,score[i],pxl[i].x,pxl[i].y);
				if (score[i] > maxi){
					maxi = score[i];
					index = i;
				}
			}
				
			printf("max score= %d index=%d\n",maxi,index);
			
			if (size == 1) {
				index = 0;
			} else {
				if (maxi < 2) {
					printf("No enough matching\n");
					free(pxl);
					free(score);
					exit(0);
				}
			}
			
			x = (pxl[index].x);
			y = (pxl[index].y);

			
			free(pxl);
			free(score);
			
			xmini = -x;
			ymini = -y;
			
			
			printf("translate: x:%d y:%d\n",xmini,ymini);
	   
		   // output fits is the image2 translated ( relative to image1)
		   c = malloc(sizeof(unsigned char)*nelements);
		   for(y=0;y< height;y++)
			for(x=0;x< width; x++){
				x0=x+xmini;
				y0=y+ymini;
				if ( ( x0 >= width ) || ( x0 < 0) ||  ( y0 >= height ) || (y0 < 0) ){
						c[x+y*width] = 0;
				} else {
						c[x+y*width] = b[x0+y0*width];
				}
			}

		   remove(argv[3]);
		   if (write_fits(argv[3],c) == 0){
				free(a); free(b); free(c);
				return 0;
			}
		   
	   }
		   
	}
					
	free(a); free(b); free(c);
	return -1;
	
}