📄 fits_cmdh.cpp
字号:
#include <stdio.h>
#include "stdafx.h"
#include "ccsds_pro.h"
#include "fits_cmdh.h"
/********此文件中函数前都有详细说明,函数中具体实现参照fits_asf.cpp中的注释******/
/*****************************************************
** 函数名: LoadCMDH
** 输入: hxmtfile, s
** hxmtfile --- 文件名结构
** s --- 存放多个容器的结构体
** 输出:
** 函数执行成功返回0
** 调用说明:
** strUTC2time() .......把年月日是时分秒格式的时间字符串转换成对应的秒
** close_fits_cmdh()....关闭fits文件
** get_fits_filename()..取得fits文件名和保存路径
** open_fits_cmdh() ....生成fits文件并在fits肉中写入关键字
** write_fits_cmdh() ...写fits文件
** 功能描述:输入一个命令历史文件,把文件中的每行数据的各个字段读入结构体中,并生成fits文件,然后写入fits文件中
****************************************************************************************/
int LoadCMDH( 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_cmdh( fptr );
return -ERROR_NOT_ENOUGH_MEMORY;
}
while( fgets(buffer, FREAD_BUFFER_SIZE, fp) )//从文件中逐行读取数据
{
CMDH_t cmdh;//定义一个命令历史数据结构,用于存放文件中一行数据的各个字段
char *p = NULL;
if( (p = strchr(buffer, '\t')) == NULL )//找到第1个空格
continue;
*p = '\0';
sscanf( buffer, "%d", &cmdh.cir );//第一个字段赋值给cmdh.cir
char* temp = ++p;
if( (p = strchr( p, '\t')) == NULL ) continue;
strUTC2time( temp, &cmdh.e_time );//第2个字段赋值给cmdh.e_time
temp = ++p;
if( (p = strchr( p, '\t')) == NULL ) continue;
*p = '\0';
cmdh.event = temp;//第3个字段赋值给cmdh.event
sscanf( ++p, "%d", &cmdh.param );//第4个字段赋值给cmdh.param
if( (p = strchr( p, '\t')) == NULL ) continue;
temp = ++p;
if( (p = strchr( p, '\n')) == NULL ) continue;
*p = '\0';
cmdh.cmd = temp;//第5个字段赋值给cmdh.cmd
hxmt.start.time = cmdh.e_time.time;
hxmt.start.usec = cmdh.e_time.usec;
{
std::string new_fitsname;
std::string pathname;
get_fits_filename( &hxmt, new_fitsname, pathname, modetype,fits_name );//得到fits文件名和路径
if( new_fitsname != prev_fitsname ) {
prev_fitsname = new_fitsname;
close_fits_cmdh( fptr );
fptr = NULL;
if( 0 != open_fits_cmdh( &hxmt, &fptr) ) {
fclose( fp );
free( buffer );
return -3;
}
}
}
write_fits_cmdh( fptr, &cmdh, 1 );//写fits文件
}
free( buffer );
close_fits_cmdh( fptr );
fclose( fp );
return 0;
}
/*****************************************************
** 函数名: open_fits_cmdh
** 输入: hxmtfile,
** hxmtfile --- 文件名结构
** 输出: *fptr
** *fptr ---
** 调用说明:
** get_fits_filename() .......得到文件名和保存路径以及模式
** DirExist1() .......判断路径是否存在
** CreateFilePath() .......创建一个新路径
** time2strUTC_fitshead() ....秒时间转换成对应的年月日时分秒格式的字符串
** 功能描述: 输入一个命令历史文件,生成fits文件,并写入fits头中的关键字,通过fptr返回生成fits文件的结构
****************************************************************************************/
int open_fits_cmdh( const hxmt_file_t *hxmtfile, 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;
timespec_t t;
time( &t.time );
t.usec = 0;
char buf[40];
time2strUTC( 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 , "TIME_SYS", "UTC", "HDPS level of data processing", &status) )
return status;
if ( fits_update_key(fp, TSTRING , "EPOCH", buf, "program that create this FITS file", &status) )
return status;
status = 0;
int tfields = 5; /* table will have 5 columns */
long nrows = 0;
char extname[] = " CMDH_Binary "; /* extension name */
char *ttype[] = { "REVLUT", "E_TIME", "EVENT", "PARAM", "COMMAND" };
char *tform[] = { "1J", "27A", "10A", "1J","10A" };
char *tunit[] = { "\0", "\0", "\0", "\0", "\0" };
if ( fits_create_tbl( fp, BINARY_TBL, nrows, tfields, ttype, tform, tunit, extname, &status) ) {
fits_close_file( fp, &status );
return status;
}
}
*fptr = fp;
return 0;
}
/*****************************************************
** 函数名: write_fits_cmdh
** 输入: fptr, zt,n
** hxmtfile --- 文件名结构
** cmdh --- 命令历史数据结构
** n ---
** 输出:
** 函数执行成功返回0
** 调用说明:
** time2strUTC_fitshead() ....秒时间转换成对应的年月日时分秒格式的字符串
** 功能描述: 输入先前生成的fits文件结构和命令历史数据,把文件中数据写入fits文件中
****************************************************************************************/
int write_fits_cmdh( fitsfile *fptr, const CMDH_t *cmdh, 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;
void *var[1];
status = 0;
fits_write_col(fptr, TINT, 1, firstrow, firstelem, n, (void *)&cmdh->cir, &status);
char strUTC[40];
time2strUTC_fitshead( strUTC, sizeof(strUTC), &cmdh->e_time);
var[0] = (void *)strUTC;
fits_write_col(fptr, TSTRING, 2, firstrow, firstelem, n, var, &status);
var[0] = (void *)cmdh->event.c_str();
fits_write_col(fptr, TSTRING, 3, firstrow, firstelem, n, var, &status);
fits_write_col(fptr, TINT, 4, firstrow, firstelem, n, (void *)&cmdh->param, &status);
var[0] = (void *)cmdh->cmd.c_str();
fits_write_col(fptr, TSTRING, 5, firstrow, firstelem, n, var, &status);
}
return 0;
}
int close_fits_cmdh( fitsfile *fptr )//此函数用于关闭
{
int status = 0;
if( fits_close_file( fptr, &status) )
return status;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -