?? fits_cmdh.cpp
字號(hào):
#include <stdio.h>
#include "stdafx.h"
#include "ccsds_pro.h"
#include "fits_cmdh.h"
/********此文件中函數(shù)前都有詳細(xì)說(shuō)明,函數(shù)中具體實(shí)現(xiàn)參照f(shuō)its_asf.cpp中的注釋******/
/*****************************************************
** 函數(shù)名: LoadCMDH
** 輸入: hxmtfile, s
** hxmtfile --- 文件名結(jié)構(gòu)
** s --- 存放多個(gè)容器的結(jié)構(gòu)體
** 輸出:
** 函數(shù)執(zhí)行成功返回0
** 調(diào)用說(shuō)明:
** strUTC2time() .......把年月日是時(shí)分秒格式的時(shí)間字符串轉(zhuǎn)換成對(duì)應(yīng)的秒
** close_fits_cmdh()....關(guān)閉fits文件
** get_fits_filename()..取得fits文件名和保存路徑
** open_fits_cmdh() ....生成fits文件并在fits肉中寫(xiě)入關(guān)鍵字
** write_fits_cmdh() ...寫(xiě)fits文件
** 功能描述:輸入一個(gè)命令歷史文件,把文件中的每行數(shù)據(jù)的各個(gè)字段讀入結(jié)構(gòu)體中,并生成fits文件,然后寫(xiě)入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 )//打開(kāi)文件
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) )//從文件中逐行讀取數(shù)據(jù)
{
CMDH_t cmdh;//定義一個(gè)命令歷史數(shù)據(jù)結(jié)構(gòu),用于存放文件中一行數(shù)據(jù)的各個(gè)字段
char *p = NULL;
if( (p = strchr(buffer, '\t')) == NULL )//找到第1個(gè)空格
continue;
*p = '\0';
sscanf( buffer, "%d", &cmdh.cir );//第一個(gè)字段賦值給cmdh.cir
char* temp = ++p;
if( (p = strchr( p, '\t')) == NULL ) continue;
strUTC2time( temp, &cmdh.e_time );//第2個(gè)字段賦值給cmdh.e_time
temp = ++p;
if( (p = strchr( p, '\t')) == NULL ) continue;
*p = '\0';
cmdh.event = temp;//第3個(gè)字段賦值給cmdh.event
sscanf( ++p, "%d", &cmdh.param );//第4個(gè)字段賦值給cmdh.param
if( (p = strchr( p, '\t')) == NULL ) continue;
temp = ++p;
if( (p = strchr( p, '\n')) == NULL ) continue;
*p = '\0';
cmdh.cmd = temp;//第5個(gè)字段賦值給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 );//寫(xiě)fits文件
}
free( buffer );
close_fits_cmdh( fptr );
fclose( fp );
return 0;
}
/*****************************************************
** 函數(shù)名: open_fits_cmdh
** 輸入: hxmtfile,
** hxmtfile --- 文件名結(jié)構(gòu)
** 輸出: *fptr
** *fptr ---
** 調(diào)用說(shuō)明:
** get_fits_filename() .......得到文件名和保存路徑以及模式
** DirExist1() .......判斷路徑是否存在
** CreateFilePath() .......創(chuàng)建一個(gè)新路徑
** time2strUTC_fitshead() ....秒時(shí)間轉(zhuǎn)換成對(duì)應(yīng)的年月日時(shí)分秒格式的字符串
** 功能描述: 輸入一個(gè)命令歷史文件,生成fits文件,并寫(xiě)入fits頭中的關(guān)鍵字,通過(guò)fptr返回生成fits文件的結(jié)構(gòu)
****************************************************************************************/
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;
}
/*****************************************************
** 函數(shù)名: write_fits_cmdh
** 輸入: fptr, zt,n
** hxmtfile --- 文件名結(jié)構(gòu)
** cmdh --- 命令歷史數(shù)據(jù)結(jié)構(gòu)
** n ---
** 輸出:
** 函數(shù)執(zhí)行成功返回0
** 調(diào)用說(shuō)明:
** time2strUTC_fitshead() ....秒時(shí)間轉(zhuǎn)換成對(duì)應(yīng)的年月日時(shí)分秒格式的字符串
** 功能描述: 輸入先前生成的fits文件結(jié)構(gòu)和命令歷史數(shù)據(jù),把文件中數(shù)據(jù)寫(xiě)入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 )//此函數(shù)用于關(guān)閉
{
int status = 0;
if( fits_close_file( fptr, &status) )
return status;
return 0;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -