⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hxmt_file.cpp

📁 本源码是一个用于卫星数据处理的程序
💻 CPP
字号:
 
#include <algorithm>
#include <functional>
#include <string.h>
#include "stdafx.h"
#include "hxmt_file.h"
#include "fits_util.h"
#include "ccsds_pro.h"
#include "fits_eph.h"
#include "fits_tcl.h"
#include "fits_cmdh.h"
#include "fits_tmln.h"
#include "fits_ttc.h"
#include "fits_hk.h"

static struct sci_intern	sci_data;
extern std::string product_dest_dir;

int get_move_dir( std::string & dest_dir );
/*****************************************************
** 函数名: hxmt_filetype
** 输入: hxmtfile
**        hxmtfile    --- 文件名结构 
** 输出: x, type
** x --- 通过x返回一个文件结构
** type   --- 文件的类型
** 调用说明:	
**				hxmtname2time() .......文件名中的时间码转换成对应的秒 				
** 功能描述:输入一个字符串路径名,判断出文件的类型
****************************************************************************************/
int hxmt_filetype( const char * filename, hxmt_file_t *x )
{
	if( filename == NULL )
		return UNKNOWN;
	const char *p1, *p2;

	if( (p1 = strrchr(filename, '\\')) == NULL || *p1 != '\\' )//从路径名的末端开始查找第一个反斜杠,反斜杠之后就是文件名
		return UNKNOWN;
	/*******根据文件名的不同,不断的通过指针p1,p2来识别出不同的文件类型*******/
	if( (p1 = strchr(++p1, '_')) == NULL || *p1 != '_' )		return UNKNOWN;
	if( (p2 = strchr(p1+1, '_')) == NULL || *p2 != '_' )		return UNKNOWN;
	p1++;
	p2++;

	int type = UNKNOWN;
	if( *p2 == 'S' ) {
		if( *p1 == 'H' )		type = SCI_HXTA + (*(p1+5) - 'A');//高能X射线
		else if( *p1 == 'S' )	type = SCI_SXT1 + (*(p1+5) - '1');//低能
		else if( *p1 == 'F' )	type = FZCS;//工程遥测数据
		else					return -5;

	} else if( *p2 == 'H' )	{ 
		if( *p1 == 'H' && * (p1+2) == 'F' ) type = HXFREQ;
		else if( *p1 == 'H' && * (p1+2) == 'P' ) type = HXPUL;
		else if( *p1 == 'H' && * (p1+2) == 'H' ) type = HXHV;
		else if( *p1 == 'P' && * (p1+5) == 'E' ) type = PTCFREQ;
	} else if( *p2 == 'X' ) {
		switch(*p1) {
		case 'E':	type = EPH;		break;//星历数据文件
		case 'Z':	type = ZT;		break;//姿态数据文件 
		case 'J':	type = JS;		break;//校时文件
		case 'T':	type = TIMELINE;	break;//TIMELINE文件 
		case 'M':	type = ML;		break;// 命令历史文件
		case 'S':	type = SCMD;	break;//飞行模式文件
		case 'C':	type = CIR;		break;//圈数文件 
		};
	}
	if( !x )		return type;

	if( (p1 = strchr(p2, '_')) == NULL || *p1 != '_' )	return UNKNOWN;
	if( (p2 = strchr(p1+1, '_')) == NULL || *p2 != '_' )	return UNKNOWN;

	if( 0 != hxmtname2time(++p1, &x->start.time) )	return UNKNOWN;
	if( 0 != hxmtname2time(++p2, &x->end.time) )		return UNKNOWN;

	x->start.usec = 0;//通过x返回此文件结构
	x->end.usec = 0;
	x->type = type;	
	x->filename = filename;

	return type;
	
}
/*****************************************************
** 函数名: load_hxmt_files
** 输入: dir
**        dir    --- 路径名 
** 输出: 
**        返回0则说明函数执行成功
** 调用说明:	
**				hxmt_filetype() .......文件名中的时间码转换成对应的秒 
**              get_move_dir()  .......得到搬移路径
**              CreateFilePath() ......创建新的文件路径
**              LoadSCMD()  ...........把模式文件中的数据载入相应的容器中
**              move_hxmtfile() .......搬移处理过的文件
**              LoadCIR() .............把圈次文件中的数据载入相应的容器中
**              LoadEPH() .............载入星历文件中的数据
**				process_eph() .........处理星历数据
**              LoadZT() ..............载入姿态文件中的数据
**              process_zt() ..........处理姿态数据
**              LoadTMLN() ............载入TIMELINE文件中的数据并处理
**              LoadTCL() .............载入 校时文件中的数据并处理
**              LoadCMDH() ............载入命令历史文件中的数据并处理
**              LoadTTC() .............载入工程遥测数据文件中的数据并处理
**              process_hx_sci() ......载入科学高能X射线文件中的数据并处理
**              process_sx_sci() ......载入科学低能X射线文件中的数据并处理
**              LoadHK() ..............载入载荷housekeeping文件中的数据并处理				
** 功能描述:输入一个字符串路径,处理路径下所有的文件,生成一级产品fits文件,并搬移处理过的文件到相应的文件夹下
****************************************************************************************/
int load_hxmt_files( const char *dir )
{
	WIN32_FIND_DATA fdata;
	HANDLE h = INVALID_HANDLE_VALUE;
	std::string d(dir);
	std::string strFile(d+"*");
	hxmt_file_t hxmtfile;

	h = FindFirstFile( strFile.c_str(), &fdata );//找到路径下的第1个文件



	std::vector<hxmt_file_t>	hx_sci_files;    //硬X科学
	std::vector<hxmt_file_t>	sx_sci_files;    //软X科学
	std::vector<hxmt_file_t>	scmd_files;      //飞行模式
	std::vector<hxmt_file_t>	cir_files;       //圈次
	std::vector<hxmt_file_t>	eph_files;       //星历
	std::vector<hxmt_file_t>	fzcs_files;      //工程遥测
	std::vector<hxmt_file_t>	js_files;        //校时
	std::vector<hxmt_file_t>	ml_files;        //命令历史
	std::vector<hxmt_file_t>	timeline_files;  //TIMELINE文件
	std::vector<hxmt_file_t>    hk_files;        //工程数据文件
	std::vector<hxmt_file_t>    zt_files;        //姿态数据文件


	while( h != INVALID_HANDLE_VALUE )
	{
		int type = hxmt_filetype( (d+fdata.cFileName).c_str(), &hxmtfile );//判断文件类型
		
		switch(type) {
		case SCI_HXTA:
		case SCI_HXTB:
		case SCI_HXTC:
			hx_sci_files.push_back( hxmtfile );
			break;
//		case SCI_SXT1:
//		case SCI_SXT2:
//			sx_sci_files.push_back( hxmtfile );
//			break;
 		case SCMD:
			scmd_files.push_back( hxmtfile );
			break;
		case CIR:
			cir_files.push_back( hxmtfile );
			break;
//		case EPH:                                      //星历文件
//			eph_files.push_back( hxmtfile );
// 		    break;
//		case FZCS:                                     //工程遥测数据文件
//			fzcs_files.push_back( hxmtfile );
//			break;
//  		case JS:                                       //校时文件
//			js_files.push_back( hxmtfile );
//			break;
//		case ML:									   //命令历史文件
//			ml_files.push_back( hxmtfile );
//			break;
//		case TIMELINE:                                 //TIMELINE文件
//			timeline_files.push_back( hxmtfile );
//			break;
		case ZT:
			zt_files.push_back( hxmtfile);		       //姿态数据文件
			break;
//		case HXFREQ:
//		case HXPUL:
//		case HXHV:
//		case PTCFREQ:
//			hk_files.push_back( hxmtfile );
// 			break;
		default:
			break;
		};

		if( !FindNextFile( h, &fdata) )                 //循环读取文件夹下的各个文件
			break;

	}
	FindClose( h );
	
	if( scmd_files.empty() || cir_files.empty() )//如果文件夹下没有模式和圈次文件,则没法生成一级产品
		return -1;

	std::sort( hx_sci_files.begin(), hx_sci_files.end() );
	std::sort( scmd_files.begin(), scmd_files.end() );
	std::sort( cir_files.begin(), cir_files.end() );
	std::sort( eph_files.begin(), eph_files.end() );
	std::sort( timeline_files.begin(), timeline_files.end() );
	std::sort( js_files.begin(), js_files.end() );
	std::sort( fzcs_files.begin(), fzcs_files.end() );
	std::sort( ml_files.begin(), ml_files.end() );
	std::sort( hk_files.begin(), hk_files.end() );
	std::sort( zt_files.begin(), zt_files.end());

	if( get_move_dir(d) == 0 )//以当前系统时间为文件名创建搬移路径
		if( !CreateFilePath( d.c_str() ) )
			AfxMessageBox( "创建搬移目录错误!" );

	std::vector<hxmt_file_t>::iterator b;
	for( b = scmd_files.begin(); b != scmd_files.end(); b++ )
	{
		LoadSCMD( b->filename.c_str(), &sci_data );//把全部飞行模式文件中的记录载入定义好的飞行模式容器中
		move_hxmtfile( b, d.c_str() );
	}
	for( b = cir_files.begin(); b != cir_files.end(); b++ ) {
		LoadCIR( b->filename.c_str(), &sci_data );//把全部圈次文件中的记录载入定义好的圈次容器中
		move_hxmtfile( b, d.c_str() );
	}

	scmd_files.clear();
	cir_files.clear();
	for( b = eph_files.begin(); b != eph_files.end(); b++ )	{
		LoadEPH( b, &sci_data );
		process_eph( b );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = zt_files.begin(); b != zt_files.end(); b++ )	{
		LoadZT( b, &sci_data );
//		process_zt( b );
//		move_hxmtfile( b, d.c_str() );
	}
	zt_files.clear();


	for( b = timeline_files.begin(); b != timeline_files.end(); b++ ) {
		LoadTMLN( b, &sci_data );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = js_files.begin(); b != js_files.end(); b++ ) {
		LoadTCL( b, &sci_data );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = ml_files.begin(); b != ml_files.end(); b++ ) {
		LoadCMDH( b, &sci_data );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = fzcs_files.begin(); b != fzcs_files.end(); b++ ) {
		LoadTTC( b, &sci_data );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = hx_sci_files.begin(); b != hx_sci_files.end(); b++ ) {
		process_hx_sci( b );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = sx_sci_files.begin(); b != sx_sci_files.end(); b++ ) {
		process_sx_sci( b );
		move_hxmtfile( b, d.c_str() );
	}
	for( b = hk_files.begin(); b != hk_files.end(); b++ ) {
		LoadHK( b, &sci_data  );
		move_hxmtfile( b, d.c_str() );
	}

	sci_data.js_table.clear();
	sci_data.cir_table.clear();
	sci_data.scmd_table.clear();
	sci_data.eph_table.clear();
	sci_data.zt_table.clear();

	return 0;
}

struct sci_intern * get_sci_data()//此函数得到一个包含多个容器的结构
{
	return &sci_data;
}
/*****************************************************
** 函数名: get_move_dir
** 输入: 
** 输出: dest_dir
** dest_dir --- 通过dest_dir返回一个搬移路径
** 调用说明:	
**								
** 功能描述:以当前系统时间名创建一个搬移路径名
****************************************************************************************/
int get_move_dir( std::string & dest_dir )
{
	char buffer[40];
	time_t cur_time;
	extern std::string data_move_dest_dir;

	if( data_move_dest_dir.length() < 2 )
		return -1;

	time( &cur_time );//当前系统时间
	struct tm *tm = localtime(&cur_time);

	if( !tm )
		return -1;
	sprintf( buffer, "%.4d-%.2d-%.2dT%.2d-%.2d-%.2d\\", tm->tm_year+1900, tm->tm_mon+1, 
				tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec );

	dest_dir = data_move_dest_dir;
	dest_dir += buffer;//最终的搬移路径
	return 0;
}
/*****************************************************
** 函数名: move_hxmtfile
** 输入: x, dest_dir
**        x           --- 文件名结构
**        dest_dir    --- 最终的搬移路径 
** 输出: 
**        函数执行成功则返回非0
** 调用说明:				
** 功能描述:搬移处理过的文件
****************************************************************************************/
int move_hxmtfile( const hxmt_file_t *x, const char * dest_dir )
{
 	return 0;
	
	int len = strlen( dest_dir );
	if( len < 2 )	return -2;

	if( *(dest_dir+len-1) == '\\' )
		--len;
	std::string new_filename(dest_dir, len);
	new_filename += x->filename.substr( x->filename.rfind('\\') );
	return MoveFile( x->filename.c_str(), new_filename.c_str() );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -