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

📄 fits_hk.cpp

📁 本源码是一个用于卫星数据处理的程序
💻 CPP
字号:
#include <stdio.h>
#include "stdafx.h"
#include "ccsds_pro.h"
#include "fits_hk.h"
#include "fits_util.h"
#include <vector>
#include <string>

std::string xchar_to_string( const char *data );

/********此文件中函数前都有详细说明,函数中具体实现参照fits_asf.cpp中的注释******/

/*****************************************************
** 函数名: LoadHK
** 输入: hxmtfile, s
**        hxmtfile    --- 文件名结构 
**        s           --- 存放多个容器的结构体
** 输出: 
**           函数执行成功返回0
** 调用说明:	
**			strUTC2time() .......把年月日是时分秒格式的时间字符串转换成对应的秒	
**			close_fits_hk()....关闭fits文件
**			get_fits_filename()..取得fits文件名和保存路径
**          open_fits_hk() ....生成fits文件并在fits肉中写入关键字
**			write_fits_hk() ...写fits文件
**          xchar_to_string()..
** 功能描述:输入一个载荷housekeeping文件,把文件中的每行数据的各个字段读入结构体中,并生成fits文件,然后写入fits文件中
****************************************************************************************/
int LoadHK( const hxmt_file_t *hxmtfile , struct sci_intern *s )
{
	FILE *fp = NULL;
	char *buffer = NULL;
	fitsfile *fptr = NULL;
	int modetype;
	std::string fits_name;
	if( !hxmtfile || (fp = fopen(hxmtfile->filename.c_str(), "r")) == NULL  )//打开文件
		return -1;

	hxmt_file_t hxmt = *hxmtfile;
	std::string prev_fitsname;

	if( (buffer = (char *)malloc(FREAD_BUFFER_SIZE)) == NULL )	{
		fclose(fp); close_fits_hk( fptr );
		return -ERROR_NOT_ENOUGH_MEMORY;
	}
    
	if( !fgets(buffer, FREAD_BUFFER_SIZE, fp) )//读第一行,即 Excle表格中的第一行
		return -1;

	hk_type_t hk_type;
	char *p = buffer;
	while( p ) {
		char *p2 = strchr(p, '\t');
		if( p2 ) {	*p2 = '\0'; }//找到空格
		else //没找到空格,此时到了第一行的末尾,为换行符
		{
			if( (p2 = strchr( p, '\n' )) == NULL )
				break;
			*p2 = '\0';
			hk_type.push_back( p );
			break;
		}
		hk_type.push_back( p );

		p = p2 + 1;
	}
	
    int num = hk_type.size();//Excel表格中的字段个数
	
	while(fgets(buffer, FREAD_BUFFER_SIZE, fp))//循环从文件的第2行开始处理数据
	{
		hk_data_t hk_data;
		char *p = buffer;
		for( int i = 0; i < num; i++ ){
			
			char *p2 = NULL;
			if( (p2 = strchr(p, '\t')) == NULL ) {
				if( (p2 = strchr( p, '\n')) == NULL )
					break;
			}
			*p2 = '\0';
			hk_data.push_back( xchar_to_string(p) );
			p = p2 + 1;
		}
		int n = hk_data.size();
		if( hk_data.size() < hk_type.size() )
			continue;

		timespec_t temp_time;
		strUTC2time( hk_data[0].c_str(), &temp_time );

		hxmt.start.time = temp_time.time;
		hxmt.start.usec = temp_time.usec;
		{
			std::string new_fitsname;
			std::string pathname;
			if( get_fits_filename(&hxmt, new_fitsname, pathname,modetype, fits_name) ) {
				if( fptr != NULL )
				close_fits_hk( fptr );
				free( buffer );
				fclose( fp );
				return -4;
			}
			if( new_fitsname != prev_fitsname ) {
				prev_fitsname = new_fitsname;
				close_fits_hk( fptr );
				fptr = NULL;
				if( 0 != open_fits_hk( &hxmt, hk_type, &fptr) ) {
					fclose( fp ); 
					free( buffer );
					return -3;
				}
			}
		}

		write_fits_hk( fptr, hk_data, 1 );
	}
	
	free( buffer );
	close_fits_hk( fptr );
	fclose( fp );

	return 0;		
}
/*****************************************************
** 函数名: open_fits_hk
** 输入: hxmtfile, hk_type
**        hxmtfile    --- 文件名结构
**        hk_type  ------ 
** 输出: *fptr
**        *fptr       ---
** 调用说明:	
**			get_fits_filename() .......得到文件名和保存路径以及模式
**			DirExist1()         .......判断路径是否存在
**			CreateFilePath()    .......创建一个新路径
**			time2strUTC_fitshead() ....秒时间转换成对应的年月日时分秒格式的字符串
** 功能描述: 输入一个载荷housekeeping文件,生成fits文件,并写入fits头中的关键字,通过fptr返回生成fits文件的结构
****************************************************************************************/
int open_fits_hk( const hxmt_file_t *hxmtfile, const hk_type_t & hk_type, fitsfile **fptr )
{
    fitsfile *fp;
	int status = 0;
	std::string filename;
	std::string pathname;
	int modetype;
	std::string fits_name;
	if( get_fits_filename(hxmtfile, filename, pathname, modetype, fits_name) )
		return -1;
	{
		extern std::string product_dest_dir;
		std::string path = product_dest_dir + pathname + "\\";
		if( !DirExist1(path.c_str()) )		{
			extern FILE *arch_filep;
			pathname += "\n";
			fwrite(pathname.c_str(), sizeof(char), pathname.length(), arch_filep);
		}
		
	}
	if( !CreateFilePath(filename.c_str()) )
		return -2;
	if( fits_open_file(&fp, filename.c_str(), READWRITE, &status) )
	{
		status = 0;
		if( fits_create_file(&fp, filename.c_str(), &status) )
			return status;

		if( fits_create_img(fp,  BYTE_IMG, 0, NULL, &status) )
			return status;
//		ffpcom(*fptr, "ALL TIME IN THIS FILE IS UTC", &status); /*增加必要的注释*/
		
		timespec_t t;
		time( &t.time );
		t.usec = 0;
		char buf[40];
		time2strUTC_fitshead( buf, sizeof(buf), &t );
		/* 写入需要的关键字 */
		if ( fits_update_key(fp, TSTRING , "DATE_CRT", buf, "FITS file creation date", &status) )
			 return status; 
		if ( fits_update_key(fp, TSTRING , "TELESCOP", "HXMT", "Telescope or mission name", &status) )
			 return status;
		if ( fits_update_key(fp, TSTRING , "ORIGIN", "HDPS", "Origin of FITS file", &status) )
			 return status;
		if ( fits_update_key(fp, TSTRING , "HDPSLEVL", "RAW", "HDPS level of data processing", &status) )
			 return status;
		if ( fits_update_key(fp, TSTRING , "CREATOR", "POAC", "program that create this FITS file", &status) )                                                                                                                                   
			 return status;

		status = 0;
		long nrows  = 0;
		const char *ttype[100] ;
		char *tform[100] ;
		char *tunit[100] ;

		char extname[] = "HK_Binary";           /* extension name */
		char form0[] = "27A";
		char form_others[] = "8A";
		char unit[] = "N";
		for( int i =0; i < hk_type.size() ; i++)
		{
			ttype[i] = hk_type[i].c_str();
			tform[i] = form_others;
 			tunit[i] = unit;
		}
		tform[0] = form0;
		
		if ( fits_create_tbl( fp, BINARY_TBL, nrows, hk_type.size(), (char**)ttype, tform,	tunit, extname, &status) ) {
			fits_close_file( fp, &status );
		
			return status;
		}
		
	}
	
	*fptr = fp;
	return 0;

}
/********************************************************************8
** 函数名: write_fits_hk
** 输入: fptr, hk_data,n
**        hxmtfile    --- 文件名结构 
**        hk_data          --- 载荷housekeeping数据结构
**		  n           ---
** 输出: 
**        函数执行成功返回0
** 调用说明:	
** 功能描述: 输入先前生成的fits文件结构和载荷housekeeping数据,把文件中数据写入fits文件中
****************************************************************************************/
int write_fits_hk( fitsfile *fptr, const hk_data_t & hk_data, int n )
{
	if( fptr == NULL )
		return -1;

	int status = 0;
	unsigned int nrows = 0;
	int hdutype;
    if ( fits_movabs_hdu(fptr, 2, &hdutype, &status) ) 
		 return status;
	if( fits_read_key(fptr, TINT, "NAXIS2", &nrows, NULL, &status) )
		return status;

//	for( int i = 0; i < n; i++, nrows++ )
	{
		if (fits_insert_rows(fptr, nrows, 1, &status))
			  return status;
    
		int firstrow = nrows + 1;
		int firstelem = 1;
		for( int i = 0; i < hk_data.size(); i++){
			void * var[1];
			var[0] = (void *)hk_data[i].c_str();
			if( fits_write_col(fptr, TSTRING, i+1, firstrow, firstelem, n, var, &status) ) {
				return status;
			}
		}		
	}
	return 0;	
}
int close_fits_hk( fitsfile *fptr )//此函数用于关闭
{
	int status = 0;
	if( fits_close_file( fptr, &status) )
		return status;
	return 0;

}
/***************************************************************************************
** 函数名: xchar_to_string
** 输入: data
**        data    --- 通过指针所指的字符串
** 输出: str, 
**        str --- 通过str返回转换后的字符串
** 调用说明:	
**							
** 功能描述:传入指针所指的一个字符串,转换成一个string类型的字符串
****************************************************************************************/
std::string xchar_to_string( const char *data )
{
	char str[9] = "0x000000";
	int len = strlen(data);
	if( len > 8 )
		return data;
	len = min(len, 8) - 2;
	strncpy( str+8-len, data+2, len );
	return str;
}

⌨️ 快捷键说明

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