📄 four2one.c
字号:
/**************************************************************//* *//* Program Name : four2one *//* *//* Version: 0.00 2003-01-08 *//* *//* Copyright (c) 2003 T.Kondo/CRL All Right Reserved *//* *//**************************************************************//* 俬俹亅倁俴俛俬儃乕僪偐傜係們倛儌乕僪偱庢摼偟偨僨乕僞僼傽僀儖偐傜 摿掕偺們倛偺僨乕僞傪拪弌偟偨侾們倛儌乕僪偺僨乕僞僼傽僀儖傪嶌惉偡傞 T.Kondo 2003.1.8 V0.0 2003.1.8 憱傜偣曽丂four2one filename [ch [outfile]] 偙偙偱 filename -- 僨乕僞僼傽僀儖柤 ch -- 拪弌偡傞們倛丂乮侾偐傜係乯 僨僼僅儖僩抣偼侾 outfile -- 僨乕僞弌椡僼傽僀儖柤 僨僼僅儖僩偼filename+'.拪弌ch' secoffset -- 僗僞乕僩偐傜偺僆僼僙僢僩昩 丂丂丂丂丂丂僨僼僅儖僩偼侽 span ---- 僨乕僞曄姺偺嬫娫乮昩乯 丂丂丂丂丂丂僨僼僅儖僩偼偡傋偰丂椺丂four2one R2320002.dat 3 3ch僨乕僞傪拪弌偟偰丂R2320002.dat.3 偵弌椡*/#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 ipboard { int adbit; // A/D價僢僩悢 (1,2,4,8) int numch; // 擖椡們倛悢丂(1,4) int sfreq; // 僒儞僾儕儞僌廃攇悢(kHz)(40,100,200,500,1000,2000,4000,8000,16000) long int seconds; // 侽倛偐傜偺宱夁昩悢 unsigned char hd[8]; // 俬俹儃乕僪僿僢僟乕 long int nbsec; // 侾昩僨乕僞拞偺僶僀僩悢乮僿僢僟乕傪娷傓乯 long int bpos; // 彂偒崬傒奐巒僶僀僩埵抲乮侽偐傜僗僞乕僩乯} IPBOARD_T;typedef struct ip_vlbi_file{ unsigned char bit32[4]; unsigned char bit64[8]; unsigned char *ibuf; int *idata; int sfreq; int icnt; // current unit number in a second data int numb; // # of bytes in unit # of samples int usampl; // unit sample number int kpos; int imax; // # of units in a second data char* fname; //僆乕僾儞偟偰偄傞僼傽僀儖柤 double fsample; //僒儞僾儕儞僌廃攇悢(Hz) double tsample; // sampling period double unitp; // usampl*tsampl int adbit; // AD價僢僩挿 long int seconds; // 00h00m00s偐傜偺宱夁昩悢 int hh, mm, ss; int spatn; // sync pattern int syncok; // =1 for 32bit all 1 detection // not used 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; // time at begin of unit double dtimest; // time at begin of file data unsigned int bytesread; int vflag, vflag_old; // byte slip flag added on 2003-02-14 // if header sync detection is suceed // set to 1. else 0 // 1: sync detection success // 0: sync detection error (may be byte slip) // vflag_old is the previous status long int dtime_vflag, dtime_vflag_old; // time at vflag decision made} ip_vlbi_file_t;void ip_hd_make_ini(IPBOARD_T *ipb){ /* IP-VLBI儃乕僪弌椡僨乕僞僿僢僟乕晹偡傋偰嶌惉 */ int n,fid,aid; long int w1; unsigned char b,c; double fsampl; /* sync pattern 1 set */ for (n=0; n<4; n++){ ipb->hd[n]=0xFF; } /* seconds set */ w1=ipb->seconds; b=(unsigned char)((w1 & 0x10000) >> 16); ipb->hd[4]=(unsigned char)(w1 & 0xFF); ipb->hd[5]=(unsigned char)((w1 & 0xFF00) >> 8); b=(unsigned char)((w1 & 0x10000) >> 16); /* ch flag set */ if( ipb->numch == 4){ b=b | 0x2; } /* sampling freq index set */ fid=0; //if( ipb->sfreq == 40) fid=0; if( ipb->sfreq == 100) fid=1; if( ipb->sfreq == 200) fid=2; if( ipb->sfreq == 500) fid=3; if( ipb->sfreq == 1000) fid=4; if( ipb->sfreq == 2000) fid=5; if( ipb->sfreq == 4000) fid=6; if( ipb->sfreq == 8000) fid=7; if( ipb->sfreq == 16000) fid=8; c=(unsigned char)(fid << 2); b=b | c; /* A/D bit length set */ aid=0; //if( ipb->adbit == 1 ) aid=0; if( ipb->adbit == 2 ) aid=1; if( ipb->adbit == 4 ) aid=2; if( ipb->adbit == 8 ) aid=3; c=(unsigned char)(aid << 6); b=b | c; ipb->hd[6]=b; /* sync pattern 2 set */ ipb->hd[7]=0x8B; /* internal data initialize */ fsampl=1000.0*(double)ipb->sfreq; // sampling frequency (Hz) ipb->nbsec=(((long)fsampl)*ipb->adbit*ipb->numch)/8; // # of bytes in 1 sec data except header ipb->nbsec+=8; ipb->bpos=0;}void ip_hd_make(IPBOARD_T *ipb){ /* IP-VLBI儃乕僪弌椡僨乕僞僿僢僟乕晹帪崗晹偺傒峏怴 */ long int w1; unsigned char b,c; /* seconds set */ w1=ipb->seconds; b=(unsigned char)((w1 & 0x10000) >> 16); ipb->hd[4]=(unsigned char)(w1 & 0xFF); ipb->hd[5]=(unsigned char)((w1 & 0xFF00) >> 8); b=ipb->hd[6]; c=(unsigned char)((w1 & 0x10000) >> 16); b=(b & 0xFE) | (c & 0x1); ipb->hd[6]=b;}int checkheader(ip_vlbi_file_t *S){ /* Read 64bit Data header check to get sampling parameters */ /* if first 64bit data is not header block, found header block automatically return value > 0 means above condition occurred */ int iok, i, n, i32; //fpos_t save; i32=0; S->vflag_old=S->vflag; // keep current valid flag S->dtime_vflag_old=S->dtime_vflag; //keep time when vflag decesion made S->vflag=0; S->bytesread = fread( S->bit32, sizeof( char ), 4, S->stream ); if( ferror( S->stream ) || S->bytesread==0 ) { printf("checkheader: File EOF or Read Error! (%s)\n",S->fname); i=fclose( S->stream ) ; i32=-1; goto exit_loop; } for (n=0; n<4; n++){ S->bit64[n]=S->bit32[n]; }loop1: S->bytesread = fread( S->bit32, sizeof( char ), 4, S->stream ); if( ferror( S->stream ) || S->bytesread==0 ) { printf("checkheader: File EOF or Read Error! (%s)\n",S->fname); i=fclose( S->stream ) ; i32=-1; goto exit_loop; } for (n=0; n<4; n++){ S->bit64[n+4]=S->bit32[n]; } 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 { if(i32 !=0) printf(" found after %d bits read!\n",i32*32); printf("checkheader: Header (64bits) Sync Detected!!\n"); printf("checkheader: File : %s\n",S->fname); printf("checkheader: A/D(bits) %d CHs %d SFreq(kHz) %d", S->adbit,S->numch, S->sfreq); printf(" Time %02d:%02d:%02d sec %d\n", S->hh,S->mm,S->ss,S->seconds); S->format=1; goto exit_loop; } else { if(i32 ==0) { printf("checkheader: First 64bit is not header block!\n"); printf("checkheader: Data file name (%s)\n",S->fname); printf("checkheader: searching header....."); } /* search sync pattern by shifting read position by 32 bits forward */ for (n=0; n<4; n++){ S->bit64[n]=S->bit64[n+4]; } i32++; } goto loop1;exit_loop: if(i32 == 0) S->vflag=1; S->dtime_vflag=S->seconds; //printf("checkheader: vflag dtime_vflag %d %d\n",S->vflag,S->dtime_vflag); //printf("checkheader: vflag_old dtime_vflag_old %d %d\n",S->vflag_old,S->dtime_vflag_old); return i32;}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 conv4byte2int(unsigned char *b4){ int i, k, iwork, iw2, ipick, flag, pflag; unsigned char iw; iwork=0; flag=0; // flag to reverse bit order 1: reverse pflag=0; // flag to change parity position not used yet for(i=0; i<4; i++){ iw=b4[i]; iw2=(int)iw; for(k=0; k<8; k++){ ipick=iw2 & 0x1; if(flag==0){ iwork=iwork >> 1; ipick =ipick << 31; iwork= ipick | (iwork & 0x7FFFFFFF); } else { iwork=iwork << 1; ipick =ipick & 0x1; iwork= ipick | (iwork & 0xFFFFFFFE); } iw2=iw2 >> 1; } } return iwork;}int conv32bit2int(int *idat32){ //int idat32[32]; // 32 bit data //int i4; return int i, iwork, ipick, flag; iwork=0; flag=0; // flag to reverse bit order 1: reverse for(i=0; i<32; i++){ ipick=idat32[i]; if(flag==0){ iwork=iwork >> 1; ipick =ipick << 31; iwork= ipick | (iwork & 0x7FFFFFFF); } else { iwork=iwork << 1; ipick =ipick & 0x1; iwork= ipick | (iwork & 0xFFFFFFFE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -