📄 extdata.c
字号:
/**************************************************************//* *//* Program Name : extdata *//* *//* Version: 0.00 2002-08-27 *//* *//* Copyright (c) 2002 T.Kondo/CRL All Right Reserved *//* *//**************************************************************//* 俬俹亅倁俴俛俬儃乕僪庢摼僨乕僞僼傽僀儖偐傜摿掕偺 們倛偺僨乕僞傪拪弌偟丄僥僉僗僩僨乕僞偲偟偰弌椡偡傞 T.Kondo 2002.8.27 V0.0 2002.8.27 憱傜偣曽丂extdata filename [ch [soffset [period [omode [outfile]]]]] 偙偙偱 filename -- 僨乕僞僼傽僀儖柤 0 偲偡傞偲僼傽僀儖偼 丂丂丂tds.data傪巊偆 ch -- 拪弌偡傞們倛丂乮侾偐傜係乯 僨僼僅儖僩抣偼侾 soffset -- 僗僞乕僩帪崗僆僼僙僢僩乮昩乯 僨僼僅儖僩抣偼0.0 period -- 拪弌偡傞僨乕僞偺帪娫挿乮昩乯 僨僼僅儖僩抣偼1.0 omode -- 僨乕僞弌椡儌乕僪 丂丂丂丂丂丂0:僿僢僟乕忣曬晅偒 丂丂丂丂丂丂1:僿僢僟乕柍偟 僨僼僅儖僩抣偼0 outfile -- 僨乕僞弌椡僼傽僀儖柤 僨僼僅儖僩偼extdata.txt弌椡僥僉僗僩僼傽僀儖偺椺乮僒儞僾儕儞僌僨乕僞偼侾峴偵俀侽揰乯乮僿僢僟乕忣曬偼丂** 偱巒傑傞嵟弶偺俈峴乯** Data File : D:\IPVLBI\data\R2320002.dat** Sampling Freq : 4MHz** A/D bits : 4** Number of CHs : 4** Extracted CH# : 1** Start Time of Data : 12:15:38.00000000** Period (sec) : 0.010000 10 6 8 8 9 7 8 6 7 8 8 7 9 8 8 8 8 10 7 8 7 8 7 9 8 9 11 8 8 8 5 7 7 7 7 7 6 7 8 9 8 9 7 6 10 8 7 7 10 7 7 6 8 7 8 9 6 11 7 6 6 7 6 6 7 9 8 7 7 7 8 8 8 9 9 8 7 8 8 8 8 6 6 9 9 6 7 8 6 8 9 7 8 5 7 8 5 7 9 9*/#ifndef WIN32#include <unistd.h>#endif#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <sys/stat.h>#ifdef linux#include <sys/time.h>#else#include <time.h>#endif//#include <math.h>#include "libipvlbi.h" // IP-VLBI梡娭悢孮(by T.Kondo)#ifndef PIRA#define PIRA 1.745329252E-2 // PI/180 degree to radian#endif#ifndef PIDE#define PIDE 57.29577951 // 180/PI radian to degree#endif#ifndef TWOPI#define TWOPI 6.283185307 // 2PI#endif#ifndef PI#define PI 3.141592653#endif/* Offset from dB to dBm conversion from A/D multibit sampling data */ #define OFFSET1 16.0 //1 bit sampling full power convert to 0 dB (theoretical) /* following constants are provisional (measured on 2002/5/18) */#define OFFSET2 9.0 //2 bit sampling data offset measured at 200kHz for PC(vlbi11)#define OFFSET4 -1.0 //4 bit sampling data offset measured at 200kHz for PC(vlbi11)#define OFFSET8 -26.0 //8 bit sampling data offset measured at 200kHz for PC(vlbi11)typedef struct ip_vlbi_file{ unsigned char bit32[4]; unsigned char bit64[8]; //char buf[65536]; int sfreq; int icnt; // current position number char* fname; //僆乕僾儞偟偰偄傞僼傽僀儖柤 float fsample; //僒儞僾儕儞僌廃攇悢(Hz) int adbit; // AD價僢僩挿 long int seconds; // 00h00m00s偐傜偺宱夁昩悢 int hh, mm, ss; int spatn; // sync pattern int syncok; // =1 for 32bit all 1 detection int format; // =0 for old format , =1 for new format FILE *stream; fpos_t pos; int fh; int ch; int numch; int ierr; double dtime; unsigned int bytesread;} ip_vlbi_file_t;int checkheader(ip_vlbi_file_t *S){ /* Read 64bit Data header check to get sampling parameters */ int iok, i; S->bytesread = fread( S->bit64, sizeof( char ), 8, S->stream ); //printf("bytes read 1 %d\n",bytesread); //debug if( ferror( S->stream ) || S->bytesread==0 ) { printf("checkheader: File EOF or Read Error!\n"); i=fclose( S->stream ) ; return -1; } headerchkn(S->bit64,&iok,&S->spatn,&S->adbit,&S->sfreq,&S->numch, &S->seconds,&S->hh,&S->mm,&S->ss); if (iok == 0) // sync detection { printf("New Format (header 64bits) Sync Detected!!\n"); printf("File : %s\n",S->fname); printf(" A/D(bits) = %d # of ch = %d Sampling Freq(kHz) = %d\n", S->adbit,S->numch, S->sfreq); printf(" Time %02d:%02d:%02d Sec in Day = %d\n", S->hh,S->mm,S->ss,S->seconds); S->icnt=0; return 0; } else { printf("First 64bit is not header block!\n"); i=fclose( S->stream ) ; return -1; }}int fwdNsec(unsigned char* ibuf, int nsec, int numb, int imax,ip_vlbi_file_t *dat){ int n, i; if(nsec <= 0) return 0; //printf("fwdNsec: nsec, numb, imax %d %d %d\n",nsec,numb,imax); //debug for(n=0; n<nsec; n++) { for(i=0; i< imax; i++) { dat->bytesread = fread( ibuf, sizeof( char ), numb, dat->stream ); //printf("i, bytesread %d %d %d\n",i,dat->bytesread,ferror( dat->stream )); if( ferror( dat->stream ) || dat->bytesread==0 ){ return -1; } } if(checkheader(&(*dat)) < 0){ return -1; } } return 0;}int main(int argc, char *argv[]){ int sfreq, adbit, numch, kpos; long int startoffset; // start time offset in sec long int smpl_count; // sample number counts to extract long int counts; //sample counter long int counts2; //extract sample counter long int sofst_pos; // start sample position from sec tic int nloop; int i,imax, m, k; int smode,usampl, omode; unsigned char *ibuf; int nbyte=5000; fpos_t pos; int bytesread; int bits, bytes, numb; int nch; double fsampl,tsampl,fact; int *idata; double soffset, fsec, period; char fname[80],oname[80]; char lab2[60], ltime1st[60]; char labfsampl[10]; char* dflt_fname="tds.data"; char* dflt_oname="extdata.txt"; ip_vlbi_file_t X; FILE *fo; if(argc == 1){ printf("Usage: extdata filename [ch [soffset [period [omode [outfile]]]]\n"); printf("\n"); printf(" where filename ---- Data file name\n"); printf(" if 0 is given, tds.data will be used\n"); printf(" ch -------- Channel # to extract\n"); printf(" default is 1\n"); printf(" soffset --- Start time offset in sec (float seconds)\n"); printf(" default is 0.0\n"); printf(" period --- Extracting period in sec (float seconds)\n"); printf(" default is 1.0\n"); printf(" omode ---- out file mode 0: with header 1: without header\n"); printf(" default is 0\n"); printf(" outfile ---- Extract data outfile name\n"); printf(" default is extdata.txt\n"); exit(0); } /* data file name get */ strcpy(fname,*(argv+1)); if (fname[0]=='0'){ strcpy(fname,dflt_fname); } X.fname=fname; /* Ch# to extract get */ if (argc>=3) { nch = atoi(*(argv + 2 )); } else { nch=1; } /* Start time offset get */ if (argc>=4) { soffset = atof(*(argv + 3 )); } else { soffset=0.0; } /* Period to extract get */ if (argc>=5) { period = atof(*(argv + 4 )); } else { period=1.0; } /* Output mode get */ if (argc>=6) { omode = atoi(*(argv + 5 )); } else { omode=0; } /* Out file name get */ if (argc>=7) { strcpy(oname,*(argv+6)); } else { strcpy(oname,dflt_oname); } /* Run condition monitor for debug*/ //printf("Data File is %s\n",fname); //printf("nch, soffset, period %d %f %f\n",nch,soffset,period); /* Program Name Display */ printf("****************************************************\n"); printf("* IP-VLBI DATA EXTRACTION PROGRAM (extdata) *\n"); printf("* Ver 0.0 2002-08-27 by T.KONDO/CRL *\n"); printf("****************************************************\n"); printf("\n"); /* Data file open */ if( (X.stream = fopen( X.fname, "rb" )) == NULL ) { printf("Data File %s Open Error! \n",fname); goto exitend2; } /* output file create */ if( (fo = fopen( oname, "w" )) == NULL ) { printf("File %s Open Error! \n",oname); goto exitend3; } /* Data header check to get sampling parameters */ if(checkheader(&X) < 0){ goto exitend3; } /* parameter set */ sfreq=X.sfreq; adbit=X.adbit; numch=X.numch; fsampl=1000.0*(float)sfreq; // sampling frequency (Hz) tsampl=1.0/fsampl; // sampling period (sec) bits=((long)fsampl)*adbit*numch; // # of bits in 1 sec data except header bytes=bits/8; // # of bytes in 1 sec data except header X.fsample=(float)fsampl; /* Set extract Ch# to 1 in case of 1 ch board*/ if(numch == 1){nch=1; } /* File read buffer size set */ smode=1; // low resolution mode if(smode ==1){ usampl=10000; } else { usampl=50000; if(sfreq == 40){ usampl=40000; } if(sfreq == 100){ usampl=100000; } if(sfreq == 200){ usampl=200000; } } imax=(sfreq*1000)/usampl; numb=usampl*adbit*numch/8; // # of bytes in unit # of samples //printf("imax numb %d %d\n",imax,numb); //debug ibuf = (unsigned char *)malloc(numb); idata = (int *)malloc(4*usampl*sizeof(int)); fact=((float)usampl)*((float)usampl); /* sampling frequency in text for print */ if(sfreq == 40){strcpy(labfsampl,"40kHz"); } if(sfreq == 100){strcpy(labfsampl,"100kHz"); } if(sfreq == 200){strcpy(labfsampl,"200kHz"); } if(sfreq == 500){strcpy(labfsampl,"500kHz"); } if(sfreq == 1000){strcpy(labfsampl,"1MHz"); } if(sfreq == 2000){strcpy(labfsampl,"2MHz"); } if(sfreq == 4000){strcpy(labfsampl,"4MHz"); } if(sfreq == 8000){strcpy(labfsampl,"8MHz"); } if(sfreq == 18000){strcpy(labfsampl,"16MHz"); } /* Data extract start point set */ startoffset=(long int)soffset; fsec=soffset-(double)startoffset; sofst_pos=(long int)(fsec/tsampl + 0.01); /* Data extraction period set */ smpl_count=(long int)(period/tsampl + 0.01); /* Run condition display */ //printf("startoffset, sofst_pos, smpl_count %d %d %d\n",startoffset,sofst_pos,smpl_count); printf("\n"); printf(" Data File : %s\n",fname); printf(" Sampling Freq : %s\n",labfsampl); printf(" A/D bits : %d\n",adbit); printf(" Number of CHs : %d\n",numch); printf(" Extracted CH# : %d\n",nch); /* foward startffset seconds */ if(fwdNsec(ibuf, startoffset, numb, imax, &X) <0){ goto exitend; } printf(" Time %02d:%02d:%02d Sec in Day = %d\n",X.hh,X.mm,X.ss,X.seconds); sprintf(ltime1st,"Time(UT) %02d:%02d:%02d\0",X.hh,X.mm,X.ss); strcpy(lab2,ltime1st); /* continue run info display */ printf(" Start Time of Extraction : %02d:%02d:%02d.%08d\n",X.hh,X.mm,X.ss,(long int)(fsec*1.0e8)); printf(" Period (sec) : %f\n",period); printf(" Out File : %s\n",oname); /* Data info out to out_file */ if(omode == 0) { fprintf(fo,"** Data File : %s\n",fname); fprintf(fo,"** Sampling Freq : %s\n",labfsampl); fprintf(fo,"** A/D bits : %d\n",adbit); fprintf(fo,"** Number of CHs : %d\n",numch); fprintf(fo,"** Extracted CH# : %d\n",nch); fprintf(fo,"** Start Time of Data : %02d:%02d:%02d.%08d\n",X.hh,X.mm,X.ss,(long int)(fsec*1.0e8)); fprintf(fo,"** Period (sec) : %f\n",period); } /* Data extraction start */ counts=0; counts2=0; m=nch-1; nloop=0; /* Data read loop start here */loop1: if(nloop >0) { if(checkheader(&X) < 0){ goto exitend; } printf(" Time %02d:%02d:%02d Sec in Day = %d\n",X.hh,X.mm,X.ss,X.seconds); } for(i=0; i< imax; i++) { bytesread = fread( ibuf, sizeof( char ), numb, X.stream ); //printf("bytes read 2 %d\n",bytesread); //debug parseADdata_2(ibuf,numb,adbit,idata, &kpos); for(k=0; k<kpos/numch; k++) { counts++; if(counts > sofst_pos) { counts2++; if(counts2 > smpl_count){ goto exithere; } fprintf(fo," %d",idata[k*numch+m]); if ((counts2 % 20) == 0) fprintf(fo,"\n"); } } } nloop++; goto loop1;exithere: fprintf(fo,"\n"); printf("\n"); printf("Data extraction finished.\n"); fgetpos(X.stream,&pos); //debug //printf("\n %d bytes read\n",pos); //counts2--; //printf("counts2 = %d\n",counts2); i=fclose( fo ) ;exitend: free(ibuf); free(idata);exitend3: i=fclose( X.stream ) ; exitend2: return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -