示例#1
0
main(int argc, char *argv[]) 
{
	FILE *fileptr, *outfile;
	char filename[1024],*telescope,*backend,*datatype,message[80],unit[16];
	int i,j,year,month,day,check,rah,ram,ded,dem;
	double ras,des,frac,tobs;
	char sra[6],sde[6],decsign;
	int raw,uth,utm,uts;
	long long numsamps,datasize,headersize;
	double readsec,skipsec;

	readsec=1;
	skipsec=0;


	fileptr=stdin;
	outfile=stdout;
	strcpy(filename,"stdin");
	strcpy(rawdatafile,"stdin");
	pulsarcentric=barycentric=0;


	if (argc>1) {
		print_version(argv[0],argv[1]);
		if (help_required(argv[1])) {
			chop_fil_help();
			exit(0);
		} else if (file_exists(argv[1])) {
			strcpy(filename,argv[1]);
			fileptr=open_file(filename,"rb");
		} else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) {
			sprintf(message,"Data file: %s not found...\n",argv[1]);
			error_message(message);
			exit(1);
		}
	}


	if (!(headersize=read_header(fileptr))) {
		rewind(fileptr);
		if ((raw=typeof_inputdata(fileptr,filename))) {
			data_type=0;
			switch (raw) {
				case 1:
					headersize=32768;
					break;
				case 5:
					headersize=32768;
					break;
				case 3:
					headersize=wapp_header_size+wapp_incfile_length;
					break;
				default:
					break;
			}
		} else {
			error_message("could not read header parameters!");
			exit(1);
		}
	}

	/* attempt to find number of bytes of data and number of samples */
	if (!strings_equal(filename,"stdin")) {
		datasize=sizeof_file(filename)-headersize;
		numsamps=nsamples(filename,headersize,nbits,nifs,nchans);
	} else if (!strings_equal(rawdatafile,"stdin")) {
		datasize=sizeof_file(rawdatafile)-headersize;
		numsamps=nsamples(rawdatafile,headersize,nbits,nifs,nchans);
	} else {
		datasize=numsamps=0;
	}

	telescope=telescope_name(telescope_id);
	backend=backend_name(machine_id);
	datatype=data_category(data_type);

	if (argc>2) {
		check=1;
		i=2;
	} else if ((argc>1) && strings_equal(filename,"stdin")) {
		check=1;
		i=1;
	} else {
		check=0;
	}
	char force_read=0;

	for (i = 0; i < argc ; i++){
		if (strcmp(argv[i],"-s")==0){
			skipsec=atof(argv[++i]);
		}
		if (strcmp(argv[i],"-r")==0){
			readsec=atof(argv[++i]);
		}
		if (strcmp(argv[i],"-f")==0){
		   force_read=1;
		}

	}

	rewind(fileptr);

	char* block_array;
	unsigned long long int count;
	unsigned long long int update_count;
	unsigned long long int bytes_per_sample=(unsigned long long int)(nchans*nbits)/8;
	unsigned long long int bytes_to_read=bytes_per_sample * (unsigned long long int)(readsec / tsamp +0.5);
	unsigned long long int bytes_to_skip=bytes_per_sample * (unsigned long long int)(skipsec / tsamp + 0.5);
	unsigned long long int blocksize = bytes_per_sample;
	unsigned long long int numblocks = bytes_to_read / blocksize;
	unsigned long long int update_size = (unsigned long long int) (10.0 * (bytes_per_sample/tsamp));
	fprintf(stderr,"Bytes per sample = %lld\n",bytes_per_sample);
	fprintf(stderr,"Bytes to read    = %lld\n",bytes_to_read);
	fprintf(stderr,"Bytes to skip    = %lld\n",bytes_to_skip);


	fprintf(stderr,"\n\n==============\n");
	fprintf(stderr,"Copying header (%d bytes)\n",headersize);

	block_array = (char*)malloc(headersize);
	count = fread(block_array,1,headersize,fileptr);
	if ( count != headersize ){
		fprintf(stderr,"Error! Could not read header %d/%d\n",count,headersize);
		exit(1);
	}
	count = fwrite(block_array,1,headersize,outfile);
	if ( count != headersize ){
		fprintf(stderr,"Error! Could not write header\n");
		exit(1);
	}
	free(block_array);

	fprintf(stderr,"Skipping data...\n");
	count = 0;
	update_count = update_size;
	while ( count < bytes_to_skip ) { 
		fseek(fileptr,blocksize,SEEK_CUR);
		count += blocksize;
		update_count += blocksize;
		if ( update_count >= update_size ){
			update_count=0;
			fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
			fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);
		}
	}
	fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);


	fprintf(stderr,"\n\n");
	block_array = (char*) malloc(blocksize);
	
	fprintf(stderr,"Copying data...\n");
	count = 0;
	update_count = update_size;
	while ( count < bytes_to_read ) { 
		int read = fread(block_array,1,blocksize,fileptr);
		if ( read < 1 ) {
		   if (force_read){
			  fclose(fileptr);
			  fileptr = fopen("/dev/urandom","r");
			  fprintf(stderr,"\rPast end of file, reading from /dev/urandom\n");
			  continue;
		   } else {
			fprintf(stderr,"Error! Could not read enough data\n");
			exit(2);
		   }
		}
		read = fwrite(block_array,1,read,outfile);
		if ( read < 1 ) {
			fprintf(stderr,"Error! Could not write enough data\n");
			exit(2);
		}
		count += read;
		update_count += read;
		if ( update_count >= update_size ){
			update_count=0;
			fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
			fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);
		}
	}
	fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);

	fprintf(stderr,"\nDone\n");

	free(block_array);

	exit(0);
}
示例#2
0
文件: header.c 项目: samb8s/sigproc
main(int argc, char *argv[]) 
{
	FILE *fileptr;
	char filename[80],*telescope,*backend,*datatype,message[80],unit[16];
	int i,j,year,month,day,check,rah,ram,ded,dem;
	double ras,des,frac,tobs;
	char sra[6],sde[6],decsign;
	int raw,uth,utm,uts;
	long long numsamps,datasize,headersize;

	int writeobsdbline;

	fileptr=stdin;
	strcpy(filename,"stdin");
	strcpy(rawdatafile,"stdin");
	pulsarcentric=barycentric=0;

	writeobsdbline=0;

	if (argc>1) {
		print_version(argv[0],argv[1]);
		if (help_required(argv[1])) {
			header_help();
			exit(0);
		} else if (file_exists(argv[1])) {
			strcpy(filename,argv[1]);
			fileptr=open_file(filename,"rb");
		} else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) {
			sprintf(message,"Data file: %s not found...\n",argv[1]);
			error_message(message);
			exit(1);
		}
	}


	if (!(headersize=read_header(fileptr))) {
		rewind(fileptr);
		if ((raw=typeof_inputdata(fileptr,filename))) {
			data_type=0;
			switch (raw) {
				case 1:
					headersize=32768;
					break;
				case 5:
					headersize=32768;
					break;
				case 3:
					headersize=wapp_header_size+wapp_incfile_length;
					break;
				default:
					break;
			}
		} else {
			error_message("could not read header parameters!");
			exit(1);
		}
	}

	/* attempt to find number of bytes of data and number of samples */
	if (!strings_equal(filename,"stdin")) {
		datasize=sizeof_file(filename)-headersize;
		numsamps=nsamples(filename,headersize,nbits,nifs,nchans);
	} else if (!strings_equal(rawdatafile,"stdin")) {
		datasize=sizeof_file(rawdatafile)-headersize;
		numsamps=nsamples(rawdatafile,headersize,nbits,nifs,nchans);
	} else {
		datasize=numsamps=0;
	}

	telescope=telescope_name(telescope_id);
	backend=backend_name(machine_id);
	datatype=data_category(data_type);

	if (argc>2) {
		check=1;
		i=2;
	} else if ((argc>1) && strings_equal(filename,"stdin")) {
		check=1;
		i=1;
	} else {
		check=0;
	}

	angle_split(src_raj,&rah,&ram,&ras);
	if (ras<10.0) {
		sprintf(sra,"0%.1f",ras);
	} else {
		sprintf(sra,"%.1f",ras);
	}

	angle_split(src_dej,&ded,&dem,&des);
	if (src_dej > 0.0) 
		decsign = '+';
	else 
		decsign = '-';
	if (des<10.0) {
		sprintf(sde,"0%.1f",des);
	} else {
		sprintf(sde,"%.1f",des);
	}

	cal(tstart,&year,&month,&day);

	if (check) {
		/* check command-line parameters */ 
		while (i<argc) {
			if (strings_equal(argv[i],"-telescope")) {
				puts(telescope);
			} else if (strings_equal(argv[i],"-obsdb")) {
				writeobsdbline=1;
			} else if (strings_equal(argv[i],"-machine")) {
				puts(backend);
			} else if (strings_equal(argv[i],"-source_name")) {
				puts(source_name);
			} else if (strings_equal(argv[i],"-scan_number")) {
				puti(scan_number);
			} else if (strings_equal(argv[i],"-datatype")) {
				puts(datatype);
			} else if (strings_equal(argv[i],"-frame")) {
				if (pulsarcentric) 
					puts("pulsarcentric");
				else if (barycentric) 
					puts("barycentric");
				else 
					puts("topocentric");
			} else if (strings_equal(argv[i],"-barycentric")) {
				puti(barycentric);
			} else if (strings_equal(argv[i],"-pulsarcentric")) {
				puti(pulsarcentric);
			} else if (strings_equal(argv[i],"-data_type")) {
				puti(data_type);
			} else if (strings_equal(argv[i],"-headersize")) {
				printf("%d\n",headersize);
			} else if (strings_equal(argv[i],"-datasize")) {
				printf("%lld\n",datasize);
			} else if (strings_equal(argv[i],"-nsamples")) {
				printf("%lld\n",numsamps);
			} else if (strings_equal(argv[i],"-tobs")) {
				printf("%f\n",(double)numsamps*tsamp);
			} else if (strings_equal(argv[i],"-az_start")) {
				printf("%f\n",az_start);
			} else if (strings_equal(argv[i],"-za_start")) {
				printf("%f\n",za_start);
			} else if (strings_equal(argv[i],"-fch1")) {
				printf("%.3f\n",fch1);
			} else if (strings_equal(argv[i],"-bandwidth")) {
				printf("%.3f\n",fabs(foff)*(double)nchans);
			} else if (strings_equal(argv[i],"-fmid")) {
				printf("%.3f\n",fch1+foff*nchans/2);
			} else if (strings_equal(argv[i],"-foff")) {
				printf("%f\n",foff);
			} else if (strings_equal(argv[i],"-refdm")||strings_equal(argv[i],"-dm")) {
				printf("%f\n",refdm);
			} else if (strings_equal(argv[i],"-nchans")) {
				printf("%d\n",nchans);
			} else if (strings_equal(argv[i],"-tstart")) {
				printf("%.12f\n",tstart);
			} else if (strings_equal(argv[i],"-frequencies")) {
				for (j=0; j<nchans; j++) printf("%f\n",frequency_table[j]);
			} else if (strings_equal(argv[i],"-mjd")) {
				printf("%d\n",(int)floor(tstart));
			} else if (strings_equal(argv[i],"-date")) {
				printf("%4d/%02d/%02d\n",year,month,day);
			} else if (strings_equal(argv[i],"-utstart")) {
				frac=tstart-floor(tstart);
				uth=(int) floor(24.0*frac);
				frac-=(double)uth/24.0;
				utm=(int) floor(1440.0*frac);
				frac-=(double)utm/1440.0;
				uts=(int) floor(86400.0*frac);
				printf("%02d:%02d:%02d\n",uth,utm,uts);
			} else if (strings_equal(argv[i],"-tsamp")) {
				printf("%.5f\n",tsamp*1.0e6);
			} else if (strings_equal(argv[i],"-nbits")) {
				printf("%d\n",nbits);
			} else if (strings_equal(argv[i],"-nifs")) {
				printf("%d\n",nifs);
			} else if (strings_equal(argv[i],"-src_raj")) {
				printf("%02d:%02d:%s\n",rah,ram,sra);
			} else if (strings_equal(argv[i],"-src_dej")) {
				printf("%c%02d:%02d:%s\n",decsign,abs(ded),dem,sde);
			}else if (strings_equal(argv[i],"-ra_deg")) {
				printf("%f\n",rah*15+ram/4.0+ras/240.0);
			} else if (strings_equal(argv[i],"-dec_deg")) {
				printf("%c%f\n",decsign,abs(ded)+dem/60.0+des/3600.0);
			} else {
				header_help();
				sprintf(message,"unknown argument (%s) passed to header",argv[i]);
				error_message(message);
			}
			i++;
		}
		/* if we are doing a obs line do this... otherwise continue normaly
		 * MK 2006, for the MM survey bookkeeping.
		 */


		if(writeobsdbline){
			printf("%s ",source_name);
                        printf("%3.3lf %3.3lf ",gal_l,gal_b);
			printf("%s ",filename);
                        printf("%5.3lf %6.6lf ",header_tobs,tstart);
			printf("%6.3lf %6.3lf ",src_raj,src_dej);
                        printf("%5.3lf %3.3lf ",raw_fch1,raw_foff);
			printf("%d %d %5.3lf",nbeams,nchans,tsamp*1000);
			printf("\n");

		}

		exit(0);
	}


	/* no command-line flags were specified - display full output */

	printf("Data file                        : %s\n",filename);
	printf("Header size (bytes)              : %d\n",headersize);
	if (datasize) 
		printf("Data size (bytes)                : %lld\n",datasize);
	if (pulsarcentric) 
		printf("Data type                        : %s (pulsarcentric)\n",datatype);
	else if (barycentric) 
		printf("Data type                        : %s (barycentric)\n",datatype);
	else
		printf("Data type                        : %s (topocentric)\n",datatype);

	printf("Telescope                        : %s\n",telescope);
	printf("Datataking Machine               : %s\n",backend);


	if (!strings_equal(source_name,"")) 
		printf("Source Name                      : %s\n",source_name);
	if (src_raj != 0.0) 
		printf("Source RA (J2000)                : %02d:%02d:%s\n",rah,ram,sra);
	if (src_dej != 0.0)
		printf("Source DEC (J2000)               : %c%02d:%02d:%s\n",
				decsign,abs(ded),dem,sde);
	if ((az_start != 0.0) && (az_start != -1.0))
		printf("Start AZ (deg)                   : %f\n",az_start);
	if ((za_start != 0.0) && (za_start != -1.0))
		printf("Start ZA (deg)                   : %f\n",za_start);

	switch (data_type) {
		case 0:
		case 1:
			if ((fch1==0.0) && (foff==0.0)) {
				printf("Highest frequency channel (MHz)  : %f\n",
						frequency_table[0]);
				printf("Lowest frequency channel  (MHz)  : %f\n",
						frequency_table[nchans-1]);
			} else {
				printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
				printf("Channel bandwidth      (MHz)     : %f\n",foff); 
				printf("Number of channels               : %d\n",nchans);
				printf("Number of beams                  : %d\n",nbeams);
				printf("Beam number                      : %d\n",ibeam); 
			}
			break;
		case 2:
			nchans=1;
			printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Reference frequency    (MHz)     : %f\n",fch1);
			break;
		case 3:
			if (refdm > 0.0)
				printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
			printf("Channel bandwidth      (MHz)     : %f\n",foff); 
			printf("Number of channels               : %d\n",nchans);
			printf("Number of phase bins             : %d\n",nbins);
			printf("Folding period  (s)              : %.12f\n",period);
			break;
		case 6:
			printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
			printf("Channel bandwidth      (MHz)     : %f\n",foff); 
			printf("Number of channels               : %d\n",nchans);
			break;
	}

	printf("Time stamp of first sample (MJD) : %.12f\n",tstart);
	printf("Gregorian date (YYYY/MM/DD)      : %4d/%02d/%02d\n",year,month,day);

	if (data_type != 3) 
		printf("Sample time (us)                 : %.5f\n",tsamp*1.0e6); 

	if (datasize && data_type != 3) {
		printf("Number of samples                : %lld\n",numsamps);
		tobs=(double)numsamps*tsamp;
		strcpy(unit,"(seconds)   ");
		if (tobs>60.0) {
			tobs/=60.0;
			strcpy(unit,"(minutes)   ");
			if (tobs>60.0) {
				tobs/=60.0;
				strcpy(unit,"(hours)     ");
				if (tobs>24.0) {
					tobs/=24.0;
					strcpy(unit,"(days)      ");
				}
			}
		}
		printf("Observation length %s  : %.1f\n",unit,tobs);
	}
	printf("Number of bits per sample        : %d\n",nbits);
	printf("Number of IFs                    : %d\n",nifs);
	exit(0);
}
示例#3
0
/*
char *telescope_name(int telescope_id);
char *data_category(int data_type);
char *backend_name(int machine_id);
void angle_split(double angle,int *dd,int *mm,double *ss);
void cal(double djm,int *year,int *month,int *day);
long numsamples(FILE *filename,int headersize,int nbits,int nifs,int nchans);
*/
int main(int argc,char *argv[]){

  FILE *fil;
  int i;
  char key_name[N]="";
  int key_length;
  int size_int=sizeof(int),size_double=sizeof(double);
  int nsamples=0;
  long numsamps;
  int size_header=0;
  long size_data;
  int dd,mm;
  double ss;
  int year,month,day;
  double total_obs;
  double low_channel,high_channel,total_bandwidth;
  char unit[N];
  int index_chan=0;

  if(help_required(argv[1])){
	  help_header();
	  exit(-1);
  }

//Default value of key parameters

//  rawdatafile="";
//  source_name="uknown";
  src_raj=0;
  src_dej=0;
  telescope_id=11; //Telescope="uknown"
  machine_id=11; //Datataking Machine="uknown"
  data_type=11; //Data file="uknown"
  barycentric=0; //Barycentric="No"
  pulsarcentric=0; //pulsarcentric="Yes"
  az_start=0;
  za_start=0;
  tstart=0;
  tsamp=0;
  nbits=0;
  nsamples=0;
  total_obs=0;
  ibeam=0;
  nbeams=0;
  nifs=0;
  low_channel=0;
  high_channel=0;
  foff=0;
  nchans=0;
  total_bandwidth=0;
  refdm=0;
  size_data=0;
  size_header=0; 

//END

  if((fil=fopen(argv[1],"rb")) == NULL){
    fprintf(stderr,"Error:Cannot open file!\n");
    exit(-1);
  }
//read HEADER_START of *.fil
  if(fread(&key_length,size_int,1,fil) < 1){
    fprintf(stderr,"Error read key_length\n");
    exit(-1);
  }
  
  size_header=size_header+size_int*1;

  if((fgets(key_name,key_length+1,fil)) == NULL){
    fprintf(stderr,"Error:Failed to read key_name\n");
    exit(-1);
  }

  size_header=size_header+key_length*1;
  
  if(strcmp(key_name,"HEADER_START")){
    fprintf(stderr,"Error:Failed to read header\n");
    exit(-1);
  }

//read HEADER of *.fil 
  while(1){

    if(fread(&key_length,size_int,1,fil) < 1){  
      fprintf(stderr,"Error read key_length\n");
      exit(-1);
    }

    size_header=size_header+size_int*1;

    if((fgets(key_name,key_length+1,fil)) == NULL){
      fprintf(stderr,"Error:Failed to read key_name\n");
      exit(-1);
    }

    size_header=size_header+key_length*1;
//find key_parameters
    if(strcmp(key_name,"telescope_id") == 0){
      if(fread(&telescope_id,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read telescope_id\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"FREQUENCY_START") == 0){
      index_chan=0;
    }
    else if(strcmp(key_name,"FREQUENCY_END") == 0){
      fch1=0;
      foff=0;
    }
    else if(strcmp(key_name,"fchannel") == 0){
      if(fread(&frequency_table[index_chan++],size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read fchannel\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"machine_id") == 0){
      if(fread(&machine_id,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read telescope_id\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"data_type") == 0){
      if(fread(&data_type,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read data_type\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nchans") == 0){
      if(fread(&nchans,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nchans\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nbits") == 0){
      if(fread(&nbits,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nbits\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nifs") == 0){
      if(fread(&nifs,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nifs\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"scan_number") == 0){
      if(fread(&scan_number,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read scan_number\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"barycentric") == 0){
      if(fread(&barycentric,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read barycentric\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"pulsarcentric") == 0){
      if(fread(&pulsarcentric,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read pulsarcentric\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"tstart") == 0){
      if(fread(&tstart,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read tstart\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"mjdobs") == 0){
      if(fread(&mjdobs,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read mjdobs\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"tsamp") == 0){
      if(fread(&tsamp,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read tsamp\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"fch1") == 0){
      if(fread(&fch1,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read fch1\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"foff") == 0){
      if(fread(&foff,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read foff\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"refdm") == 0){
      if(fread(&refdm,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read refdm\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"az_start") == 0){
      if(fread(&az_start,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read az_start\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"za_start") == 0){
      if(fread(&za_start,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read za_start\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"src_raj") == 0){
      if(fread(&src_raj,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read src_raj\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"src_dej") == 0){
      if(fread(&src_dej,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read src_dej\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"ibeam") == 0){
      if(fread(&ibeam,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read ibeam\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nbeams") == 0){
      if(fread(&nbeams,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nbeams\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nsamples") == 0){
      if(fread(&nsamples,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nsamples\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"rawdatafile") == 0){
      if(fread(&key_length,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read rawdatafile_length\n");
        exit(-1);
      }  
      size_header=size_header+size_int*1;    
      if(fgets(rawdatafile,key_length+1,fil) ==NULL){
        fprintf(stderr,"Error:Failed to read rawdatafile\n");
      }
      size_header=size_header+key_length*1;
    }
    else if(strcmp(key_name,"source_name") == 0){
      if(fread(&key_length,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read source_name_length\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
      if(fgets(source_name,key_length+1,fil) ==NULL){
        fprintf(stderr,"Error:Failed to read source_name\n");
      }
      size_header=size_header+key_length*1;
    }
    else if(strcmp(key_name,"HEADER_END") == 0) break;
    else{
      fprintf(stderr,"Error:Failed to recognize key parameter\n");
    }
  }
//Data file
  if(strcmp(rawdatafile,"") != 0)
    printf("%-38s : %s\n","Data file",rawdatafile);
  else
    printf("%-38s : %s\n","Data file",argv[1]);
//Source name
  printf("%-38s : %s\n","Source Name",source_name);
//Source RA (J2000)
  angle_split(src_raj,&dd,&mm,&ss);
  printf("%-38s : %d:%d:%.1lf\n","Source RA (J2000)",dd,mm,ss);
//Source DEC (J2000)
  angle_split(src_dej,&dd,&mm,&ss);
  printf("%-38s : %d:%d:%.1lf\n","Source DEC (J2000)",dd,mm,ss);
//Telescope
  printf("%-38s : %s\n","Telescope",telescope_name(telescope_id));
//Datataking Machine
  printf("%-38s : %s\n","Datataking Machine",backend_name(machine_id));
//Data type
  printf("%-38s : %s\n","Data type",data_category(data_type));
//Barycentric
  if(barycentric==1)
    printf("%-38s : %s\n","Barycentric","Yes");
  else
    printf("%-38s : %s\n","Barycentric","No");
//Pulsarcentric
  if(pulsarcentric==1)
    printf("%-38s : %s\n","Pulsarcentric","Yes");
  else
    printf("%-38s : %s\n","Pulsarcentric","No");
//Telescope azimuth angle (degrees)
    printf("%-38s : %lf\n","Telescope azimuth angle (degrees)",az_start);
//Telescope zenith angle (degrees)
    printf("%-38s : %lf\n","Telescope zenith angle (degrees)",za_start);
//Time stamp of first sample (MJD)
    printf("%-38s : %.12lf\n","Time stamp of first sample (MJD)",tstart);
//Gregorian date
  cal(tstart,&year,&month,&day);
  printf("%-38s : %d/%.2d/%.2d\n","Gregorian date",year,month,day);
//Sample time (us)
    printf("%-38s : %lf\n","Sample time (us)",tsamp*1.0e6);
//Number of bits per sample
    printf("%-38s : %d\n","Number of bits per sample",nbits);
//Number of samples //Warning:nifs*nchans*nsamples should be even;
  if(nsamples == 0){
    numsamps=numsamples(fil,size_header,nbits,nifs,nchans);
    size_data=(long)nifs*nchans*numsamps*nbits/8;
    total_obs=numsamps*tsamp;
    printf("%-38s : %ld\n","Number of samples",numsamps);
  }
  else{
    printf("%-38s : %d\n","Number of samples",nsamples);
    size_data=nifs*nchans*nsamples*nbits/8;
    total_obs=nsamples*tsamp;
  }
//Observation length
  strcpy(unit,"(s)");
  if(total_obs>60.0){
    total_obs/=60.0;
    strcpy(unit,"(m)");
    if(total_obs>60.0){
      total_obs/=60.0;
      strcpy(unit,"(h)");
      if(total_obs>24.0){
        total_obs/=24.0;
        strcpy(unit,"(d)");
      }
    }
  }
  printf("%s %-19s : %lf\n","Observation length",unit,total_obs);
//Beam number
  printf("%-38s : %d\n","Beam number",ibeam);
//Number of beams
  printf("%-38s : %d\n","Number of beams",nbeams);
//Number of IFs
  printf("%-38s : %d\n","Number of IFs",nifs);
//frequency table
  if(index_chan > 0){
    printf("%-38s : ","Frequency");
    for(i=0;i<index_chan;i++){
      printf("%lf ",frequency_table[i]);
    }
  }
  else{
//Low channel (MHz)
    if(foff < 0)
      low_channel=fch1+(nchans-1)*foff;
    else if(foff > 0)
      low_channel=fch1;
    printf("%-38s : %lf\n","Low channel (MHz)",low_channel);
//High channel (MHz)
    if(foff > 0)
      high_channel=fch1+(nchans-1)*foff;
    else if(foff < 0)
      high_channel=fch1;
    printf("%-38s : %lf\n","High channel (MHz)",high_channel);
//Central freq (MHz)
    printf("%-38s : %lf\n","Central freq (MHz)",(high_channel+low_channel)/2.);
//Channel bandwidth (MHz)
    printf("%-38s : %lf\n","Channel bandwidth (MHz)",foff);  
//Number of channels
    printf("%-38s : %d\n","Number of channels",nchans);
//Total Bandwidth (MHz)
    total_bandwidth=fabs(foff)*nchans;
    printf("%-38s : %lf\n","Total Bandwidth (MHz)",total_bandwidth);
  }
//Reference dispersion measure (cm-3 pc)
  printf("%-38s : %lf\n","Reference dispersion measure (cm-3 pc)",refdm);
//Data size (bytes)
  printf("%-38s : %ld\n","Data size (bytes)",size_data);
//Header size (bytes)
  printf("%-38s : %d\n","Header size (bytes)",size_header);


  fclose(fil);

return 0;
}