www.pudn.com > MFC_OGLTest1.rar > ReadAndWriteFun.cpp, change:2007-09-18,size:7457b


// ReadAndWriteFun.cpp: implementation of the ReadAndWriteFun class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "MFC_OGLTest.h" 
#include "ReadAndWriteFun.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
ReadAndWriteFun::ReadAndWriteFun() 
{ 
 
} 
 
ReadAndWriteFun::~ReadAndWriteFun() 
{ 
 
} 
 
//**************************************************************// 
//* 名称:ReadBaseData(char filename[80]) 
//* 类型:布耳型,若读文件操作失败(体扫不完整等),返回FALSE。 
//* 功能:从基数据文件中读出仰角,反射率,速度,谱宽数据,保存到 
//*       数组中。 
//* 参数:filename,基数据文件名 
//**************************************************************// 
bool ReadAndWriteFun::ReadBaseData(CString str) 
{ 
	FILE *fp=0; 
 
	int FstBin,LstBin,BinNum; 
	float CurAz,CurEl; 
	int ElIndex,AzIndex,BnIndex; 
	int ptrPos; 
 
	size_t readSize; 
	int fileEndFlag; 
 
	bool VolBeg=false; 
	bool VolEnd=false; 
 
	bool RFlag,VFlag,WFlag; 
 
	fp = fopen(str,"rb"); 
	if(fp==0) return false; 
	 
	pOneRadial = 0; 
	pOneRadial = (RADIALDATA*)malloc(sizeof(RADIALDATA)); 
	if(pOneRadial==0) return false; 
 
	//Initialize array 
	for(ElIndex=0; ElIndex<MaxCuts; ElIndex++) 
	{ 
		for(AzIndex=0; AzIndex<MaxRads; AzIndex++) 
		{ 
			for(BnIndex=0; BnIndex<RGates; BnIndex++) 
				RData[ElIndex][AzIndex][BnIndex] = VALUE_INVALID; 
			for(BnIndex=0; BnIndex<VGates; BnIndex++) 
				VData[ElIndex][AzIndex][BnIndex] = VALUE_INVALID; 
			for(BnIndex=0; BnIndex<WGates; BnIndex++) 
				WData[ElIndex][AzIndex][BnIndex] = VALUE_INVALID; 
		}//end az 
 
		Elevation[ElIndex]=VALUE_INVALID; 
	}//end el 
 
	do 
	{ 
		//Initialize flags 
		RFlag=VFlag=WFlag=false; 
 
		readSize = fread(pOneRadial,sizeof(RADIALDATA),1,fp); 
		fileEndFlag = feof(fp); 
 
		//Start a volume scan 
		if(pOneRadial->RadialStatus == VOL_BEG)  
		{ 
			ElIndex=0; 
			CurEl = float((pOneRadial->El/8.)*(180./4096.)); 
			Elevation[ElIndex] = CurEl; 
 
			VolBeg = true; 
			//output text information 
//			TRACE("VCP number is %3d\n",pOneRadial->VcpNumber); 
			printf("VCP number is %3d\n",pOneRadial->VcpNumber); 
 
//			TRACE("Elevation %3d  (%5.2f Degree) start...\n",ElIndex+1,CurEl); 
			printf("Elevation %3d  (%5.2f Degree) start...\n",ElIndex+1,CurEl); 
		} 
		//Find the beginning of the volume scan 
		if(!VolBeg) continue;		 
 
		//Start an elevation 
		if(pOneRadial->RadialStatus == ELV_BEG) 
		{ 
			CurEl = float((pOneRadial->El/8.)*(180./4096.)); 
			if(CurEl-Elevation[ElIndex] > 0.4) 
			{//different elevation angle 
				ElIndex++; 
				Elevation[ElIndex] = CurEl; 
			} 
//			TRACE("Elevation %3d  (%5.2f Degree) start...\n",ElIndex+1,CurEl); 
			printf("Elevation %3d  (%5.2f Degree) start...\n",ElIndex+1,CurEl); 
		} 
 
		//Start an elevation 
		if(pOneRadial->RadialStatus == VOL_END) 
			VolEnd=true; 
 
//		if(pOneRadial->RadialStatus == ELV_BEG || pOneRadial->RadialStatus == VOL_BEG) 
 
 
		//Calculate azimuth angle and Azimuth Index 
		CurAz = float((pOneRadial->Az/8.)*(180./4096.)); 
		if(CurAz >= 360.) CurAz = CurAz-360.; 
		AzIndex = int(CurAz+0.5); 
 
		//what kind of data in this cut 
		if(pOneRadial->PtrOfReflectivity !=0)	RFlag=true; 
		if(pOneRadial->PtrOfVelocity !=0)		VFlag=true; 
		if(pOneRadial->PtrOfSpectrumWidth !=0)	WFlag=true; 
 
		//Save reflectivity data into the array 
		if(RFlag) 
		{ 
			//Get first bin, last bin, and number of bins 
			FstBin = int(pOneRadial->RangeToFirstGateOfRef/pOneRadial->GateSizeOfReflectivity+0.5); 
			BinNum = pOneRadial->GatesNumberOfReflectivity; 
			if(FstBin<0) 
			{ 
				BinNum = FstBin+BinNum; 
				FstBin = -1*FstBin; 
			} 
			LstBin = FstBin + BinNum; 
			ptrPos = pOneRadial->PtrOfReflectivity; 
 
			//Save data 
			for(BnIndex=FstBin; BnIndex<LstBin; BnIndex++) 
				RData[ElIndex][AzIndex][BnIndex] = this->DecodeRef(pOneRadial->Echodata[ptrPos+BnIndex]); 
		} 
 
		//Save velocity data into the array 
		if(VFlag) 
		{ 
			//Get first bin, last bin, and number of bins 
			FstBin = int(pOneRadial->RangeToFirstGateOfDop/pOneRadial->GateSizeOfDoppler+0.5); 
			BinNum = pOneRadial->GatesNumberOfDoppler; 
			if(FstBin<0) 
			{ 
				BinNum = FstBin+BinNum; 
				FstBin = -1*FstBin; 
			} 
			LstBin = FstBin + BinNum; 
			ptrPos = pOneRadial->PtrOfVelocity; 
			//Save data 
			for(BnIndex=FstBin; BnIndex<LstBin; BnIndex++) 
				VData[ElIndex][AzIndex][BnIndex] =  
				this->DecodeVel(pOneRadial->Echodata[ptrPos+BnIndex],pOneRadial->ResolutionOfVelocity); 
		} 
 
		//Save spectrum width data into the array 
		if(WFlag) 
		{ 
			//Get first bin, last bin, and number of bins 
			FstBin = int(pOneRadial->RangeToFirstGateOfDop/pOneRadial->GateSizeOfDoppler+0.5); 
			BinNum = pOneRadial->GatesNumberOfDoppler; 
			if(FstBin<0) 
			{ 
				BinNum = FstBin+BinNum; 
				FstBin = -1*FstBin; 
			} 
			LstBin = FstBin + BinNum; 
			ptrPos = pOneRadial->PtrOfSpectrumWidth; 
			//Save data 
			for(BnIndex=FstBin; BnIndex<LstBin; BnIndex++) 
				WData[ElIndex][AzIndex][BnIndex] = this->DecodeSpw(pOneRadial->Echodata[ptrPos+BnIndex]); 
		} 
	}while(fileEndFlag==0 && !VolEnd && !(readSize<1)); 
	 
	if(!VolEnd && VolBeg) 
	{ 
		fclose(fp); 
		free(pOneRadial); 
		printf("Error! Incomplete Volume Scan\n"); 
//		TRACE("Error! Incomplete Volume Scan\n"); 
		return false; 
	} 
 
	NumValidCuts = ElIndex+1; 
	fclose(fp); 
	free(pOneRadial); 
	return true; 
} 
 
//**************************************************************// 
//* 名称:DecodeRef(unsigned char code) 
//* 类型:实型,返回解码后的反射率(DBZ)。 
//* 功能:将读出的反射率数据解码。 
//* 参数:code,无符号的BYTE型,读出的反射率编码值。 
//**************************************************************// 
float ReadAndWriteFun::DecodeRef(unsigned char code) 
{ 
	if(code==CODE_INVALID)		return VALUE_INVALID; 
	else if(code==CODE_RANFOLD)	return VALUE_RANFOLD; 
	else						 
		return (float((code-2.)/2.-32.5)); 
} 
 
//**************************************************************// 
//* 名称:DecodeVel(unsigned char code) 
//* 类型:实型,返回解码后的径向速度(M/S)。 
//* 功能:将读出的径向速度数据解码。 
//* 参数:code,无符号的BYTE型,读出的径向速度编码值; 
//*       ResType,2字节整型,速度精度标记,精度不同,解码方式不同 
//*            ResType = 2:精度为0.5 M/S 
//*            ResType = 4:精度为1.0 M/S 
//**************************************************************// 
float ReadAndWriteFun::DecodeVel(unsigned char code, short ResType) 
{ 
	if(code==CODE_INVALID)		return VALUE_INVALID; 
	else if(code==CODE_RANFOLD)	return VALUE_RANFOLD; 
	else						 
	{ 
		if(ResType==RES_POINT_FIVE) //0.5 m/s  
			return (float((code-2.)/2.-63.5)); 
		else 
			return (float((code-2)-127.)); 
	} 
} 
 
//**************************************************************// 
//* 名称:DecodeSpw(unsigned char code) 
//* 类型:实型,返回解码后的谱宽(M/S)。 
//* 功能:将读出的谱宽数据解码。 
//* 参数:code,无符号的BYTE型,读出的谱宽编码值。 
//**************************************************************// 
float ReadAndWriteFun::DecodeSpw(unsigned char code) 
{ 
	if(code==CODE_INVALID)		return VALUE_INVALID; 
	else if(code==CODE_RANFOLD)	return VALUE_RANFOLD; 
	else						 
		return (float((code-2.)/2.-63.5)); 
}