示例#1
0
VALUE rb_hps_buf(VALUE self)
{
  int len;
  hashpipe_status_t *s;

  Data_Get_HPStruct_Ensure_Attached(self, s);
  len = gethlength(s->buf);
  return rb_str_new(s->buf, len);
}
示例#2
0
int main(int argc, char *argv[]) {
	struct guppi_params gf;
    struct psrfits pf;
    char buf[32768];
    char partfilename[250]; //file name for first part of file
    
	int filepos=0;
	size_t rv=0;
	long unsigned int by=0;
    
    FILE *fil = NULL;   //input file
    FILE *partfil = NULL;  //partial file
    
	int x,y,z;
	int a,b,c;
	
    int vflag=0; //verbose




    
	   if(argc < 2) {
		   print_usage(argv);
		   exit(1);
	   }


       opterr = 0;
     
       while ((c = getopt (argc, argv, "Vvi:o:")) != -1)
         switch (c)
           {
           case 'v':
             vflag = 1;
             break;
           case 'V':
             vflag = 2;
             break; 
           case 'i':
			 sprintf(pf.basefilename, optarg);
			 fil = fopen(pf.basefilename, "rb");
             break;
           case 'o':
			 sprintf(partfilename, optarg);
			 if(strcmp(partfilename, "stdout")==0) {
				 partfil = stdout;
			 } else {
				 partfil = fopen(partfilename, "wb");			
			 }
             break;
           case '?':
             if (optopt == 'i' || optopt == 'o')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (isprint (optopt))
               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             else
               fprintf (stderr,
                        "Unknown option character `\\x%x'.\n",
                        optopt);
             return 1;
           default:
             abort ();
           }


   
    pf.filenum=1;
    pf.sub.dat_freqs = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    pf.sub.dat_weights = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    pf.sub.dat_offsets = (float *)malloc(sizeof(float) 
           * pf.hdr.nchan * pf.hdr.npol);
    pf.sub.dat_scales  = (float *)malloc(sizeof(float) 
            * pf.hdr.nchan * pf.hdr.npol);
    pf.sub.data  = (unsigned char *)malloc(pf.sub.bytes_per_subint);




	


	if(!fil || !partfil) {
		fprintf(stderr, "must specify input/output files\n");
		print_usage(argv);
		exit(1);
	}
	
	filepos=0;
	
	while(fread(buf, sizeof(char), 32768, fil)==32768) {		

		 fseek(fil, -32768, SEEK_CUR);
		 //printf("lhead: %d", lhead0);
		 if(vflag>=1) fprintf(stderr, "length: %d\n", gethlength(buf));

		 guppi_read_obs_params(buf, &gf, &pf);
	 
		 //printf("%d\n", pf.hdr.nchan);    
		 if(vflag>=1) fprintf(stderr, "size %d\n",pf.sub.bytes_per_subint + gethlength(buf));
		 by = by + pf.sub.bytes_per_subint + gethlength(buf);
		 if(vflag>=1) fprintf(stderr, "mjd %Lf\n", pf.hdr.MJD_epoch);
		 if(vflag>=1) fprintf(stderr, "zen: %f\n\n", pf.sub.tel_zen);
		 
		 fprintf(stderr, "%s packetindex %Ld\n", pf.basefilename, gf.packetindex);
		 fprintf(stderr, "packetsize: %d\n", gf.packetsize);
		 fprintf(stderr, "n_packets %d\n", gf.n_packets);
		 fprintf(stderr, "n_dropped: %d\n", gf.n_dropped);
		 
		 fprintf(stderr, "bytecnt: %d\n", pf.sub.bytes_per_subint);
		 fprintf(stderr, "drop_frac_tot: %f\n\n", gf.drop_frac_tot);
		 
		 if (pf.sub.data) free(pf.sub.data);
         pf.sub.data  = (unsigned char *)malloc(pf.sub.bytes_per_subint);
		 
		 fseek(fil, gethlength(buf), SEEK_CUR);
		 rv=fread(pf.sub.data, sizeof(char), pf.sub.bytes_per_subint, fil);		 
		 


		
		 if((long int)rv == pf.sub.bytes_per_subint){
			 if(vflag>=1) fprintf(stderr, "%i\n", filepos);
			 if(vflag>=1) fprintf(stderr, "pos: %ld %d\n", ftell(fil),feof(fil));


			 if(filepos == 0) {
					 					 
					 //fwrite(buf, sizeof(char), gethlength(buf), partfil);  //write header
					 //fwrite(pf.sub.data, sizeof(char), pf.sub.bytes_per_subint, partfil);  //write data					 
					 //fclose(partfil);
					 //fclose(fil);
  					 //exit(0);

 			 }
									
			 filepos++;

		} else {
				if(vflag>=1) fprintf(stderr, "only read %ld bytes...\n", (long int) rv);
		}

	}
		if(vflag>=1) fprintf(stderr, "bytes: %ld\n",by);
		if(vflag>=1) fprintf(stderr, "pos: %ld %d\n", ftell(fil),feof(fil));
	
	fclose(partfil);
	fclose(fil);
    exit(1);
}
示例#3
0
int main(int argc, char *argv[]) {
	struct guppi_params gf;
    struct psrfits pf;
    char buf[32768];
    char quantfilename[250]; //file name for quantized file
    
	int filepos=0;
	size_t rv=0;
	int by=0;
    
    FILE *fil = NULL;   //input file
    FILE *quantfil = NULL;  //quantized file
    
	int x,y,z;
	int a,b,c;
	int sample;
	
	double running_sum;
	double running_sum_sq;
	double mean[32][2];   //shouldn't be more than 32 dual pol channels in a file for our obs
	double std[32][2];
	
    int vflag=0; //verbose

    

    
	   if(argc < 2) {
		   print_usage(argv);
		   exit(1);
	   }


       opterr = 0;
     
       while ((c = getopt (argc, argv, "Vvi:o:")) != -1)
         switch (c)
           {
           case 'v':
             vflag = 1;
             break;
           case 'V':
             vflag = 2;
             break; 
           case 'i':
			 sprintf(pf.basefilename, optarg);
			 fil = fopen(pf.basefilename, "rb");
             break;
           case 'o':
			 sprintf(quantfilename, optarg);
			 if(strcmp(quantfilename, "stdout")==0) {
				 quantfil = stdout;
			 } else {
				 quantfil = fopen(quantfilename, "wb");			
			 }
             break;
           case '?':
             if (optopt == 'i' || optopt == 'o')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (isprint (optopt))
               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             else
               fprintf (stderr,
                        "Unknown option character `\\x%x'.\n",
                        optopt);
             return 1;
           default:
             abort ();
           }


   

    pf.filenum=1;
    pf.sub.dat_freqs = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    pf.sub.dat_weights = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    pf.sub.dat_offsets = (float *)malloc(sizeof(float) 
           * pf.hdr.nchan * pf.hdr.npol);
    pf.sub.dat_scales  = (float *)malloc(sizeof(float) 
            * pf.hdr.nchan * pf.hdr.npol);
    pf.sub.data  = (unsigned char *)malloc(pf.sub.bytes_per_subint);




	

	if(!fil || !quantfil) {
		fprintf(stderr, "must specify input/output files\n");
		print_usage(argv);
		exit(1);
	}
	
	filepos=0;
	
	while(fread(buf, sizeof(char), 32768, fil)==32768) {		

		 fseek(fil, -32768, SEEK_CUR);
		 //printf("lhead: %d", lhead0);
		 if(vflag>=1) fprintf(stderr, "length: %d\n", gethlength(buf));

		 guppi_read_obs_params(buf, &gf, &pf);
	 
		 printf("%d\n", pf.hdr.nbits);    
		 if(vflag>=1) fprintf(stderr, "size %d\n",pf.sub.bytes_per_subint + gethlength(buf));
		 by = by + pf.sub.bytes_per_subint + gethlength(buf);
		 if(vflag>=1) fprintf(stderr, "mjd %Lf\n", pf.hdr.MJD_epoch);
		 if(vflag>=1) fprintf(stderr, "zen: %f\n\n", pf.sub.tel_zen);
		 if (pf.sub.data) free(pf.sub.data);
         pf.sub.data  = (unsigned char *)malloc(pf.sub.bytes_per_subint);
		 
		 fseek(fil, gethlength(buf), SEEK_CUR);
		 rv=fread(pf.sub.data, sizeof(char), pf.sub.bytes_per_subint, fil);		 
		 


		
		 if((long int)rv == pf.sub.bytes_per_subint){
			 if(vflag>=1) fprintf(stderr, "%i\n", filepos);
			 if(vflag>=1) fprintf(stderr, "pos: %ld %d\n", ftell(fil),feof(fil));


			 if(filepos == 0) {
					 
					 /* calulcate mean and rms for each channel-polarization */
					 /* we'll treat the real and imaginary parts identically - considering them as 2 samples/period) */

					
/*
					 for(x=0;x < pf.hdr.nchan; x = x + 1)   {
							for(y=0;y<pf.hdr.rcvr_polns;y=y+1) {
								 running_sum = 0;
								 running_sum_sq = 0;
								 
								 for(z=0;z < pf.sub.bytes_per_subint/pf.hdr.nchan; z = z + (pf.hdr.rcvr_polns * 2)){
									 //pol 0, real imag

									 //real
									 sample = (int) ((signed char) pf.sub.data[(x * pf.sub.bytes_per_subint/pf.hdr.nchan) + z + (y * 2)]);
									 running_sum = running_sum + (double) sample;

									 //imag
									 sample = (int) ((signed char) pf.sub.data[(x * pf.sub.bytes_per_subint/pf.hdr.nchan) + z + (y * 2) + 1]);
									 running_sum = running_sum + (double) sample;

								 }
			 
								 mean[x][y] =  running_sum / (double) (pf.sub.bytes_per_subint/(pf.hdr.nchan * pf.hdr.rcvr_polns) );
			 
								 for(z=0;z < pf.sub.bytes_per_subint/pf.hdr.nchan; z = z + (pf.hdr.rcvr_polns * 2)){
										 //sample = (int) ((signed char) pf.sub.data[(x * pf.sub.bytes_per_subint/pf.hdr.nchan) + z]);

										 //real
										 sample = (int) ((signed char) pf.sub.data[(x * pf.sub.bytes_per_subint/pf.hdr.nchan) + z + (y * 2)]);
										 running_sum_sq = running_sum_sq + pow( ((double) sample - mean[x][y]) , 2);
	
										 //imag
										 sample = (int) ((signed char) pf.sub.data[(x * pf.sub.bytes_per_subint/pf.hdr.nchan) + z + (y * 2) + 1]);
										 running_sum_sq = running_sum_sq + pow( ((double) sample - mean[x][y]) , 2);

								 }

								 std[x][y] = pow(running_sum_sq / ((double) (pf.sub.bytes_per_subint/(pf.hdr.nchan*pf.hdr.rcvr_polns)) - 1), 0.5);
														 
								 
								 if(vflag>=1) fprintf(stderr, "chan  %d pol %d mean %f\n", x,y,mean[x][y]);
								 if(vflag>=1) fprintf(stderr, "chan  %d pol %d std %f\n", x,y,std[x][y]);
							}			
					 }
 			 		*/


 					 for(x=0;x<32;x++){
 			 		 	std[x][0] = 19;
 			 		 	mean[x][0] = 0;		
 			 		 	std[x][1] = 19;
 			 		 	mean[x][1] = 0;		
 			 		 }
 			 }


			quantize_2bit_o(&pf, mean, std);

			hputi4 (buf, "BLOCSIZE", pf.sub.bytes_per_subint);
			hputi4 (buf,"NBITS",pf.hdr.nbits);

			fwrite(buf, sizeof(char), gethlength(buf), quantfil);  //write header
			
			/* bytes_per_subint now updated to be the proper length */
			fwrite(pf.sub.data, sizeof(char), pf.sub.bytes_per_subint, quantfil);  //write data

			filepos++;

			 //pol, time, frequency
			 
//			 for(x=0; x < pf.sub.bytes_per_subint; x = x + 1) {
//			 	fprintf(stderr, "old: %d", pf.sub.data[x]);
//			 	if(pf.sub.data[x] >  127) { pf.sub.data[x] = pf.sub.data[x] - 256; 
//			 	fprintf(stderr, "new: %d", pf.sub.data[x]); }
//			 }
			 
			 /*
			 for(x=0; x < pf.sub.bytes_per_subint/pf.hdr.nchan; x = x + (4 * sampsper)) {
				power=0;
				for(z=0;z<sampsper;z=z+4){						
			 		for(y=0;y<4;y++) {
						sample = (int) pf.sub.data[x+y+(z*4)];
						if(sample > 127) sample = sample - 256;
			 			power = power + pow((double) sample, 2);
					}
				}
			 	//printf("%d, %d\n", pf.sub.data[x], pf.sub.data[x+1]); 			 
				printf("%f\n",power);			  

			  }
			  */
		} else {
				if(vflag>=1) fprintf(stderr, "only read %ld bytes...\n", (long int) rv);
		}

	}
		if(vflag>=1) fprintf(stderr, "bytes: %d\n",by);
		if(vflag>=1) fprintf(stderr, "pos: %ld %d\n", ftell(fil),feof(fil));
	
	
	//fread(buf, sizeof(char), 32768, fil);

    //guppi_read_obs_params(buf, &gf, &pf);
	//printf("mjd %llf\n", pf.hdr.MJD_epoch);
    //printf("zen: %f", pf.sub.tel_zen);


    //while ((rv=psrfits_read_subint(&pf))==0) { 
    //    printf("Read subint (file %d, row %d/%d)\n", 
    //            pf.filenum, pf.rownum-1, pf.rows_per_file);
    //}
    //if (rv) { fits_report_error(stderr, rv); }
	fclose(quantfil);
	fclose(fil);
    exit(0);
}
示例#4
0
VALUE rb_hps_length(VALUE self)
{
  hashpipe_status_t *s;
  Data_Get_HPStruct_Ensure_Attached(self, s);
  return UINT2NUM((unsigned int)gethlength(s->buf));
}