📄 ccsds_pro.cpp
字号:
if( !hxmtfile || (in = fopen(hxmtfile->filename.c_str(), "r+b")) == NULL )
return -1;
hxmt = *hxmtfile;
if( (buffer = (unsigned char *)malloc(FREAD_BUFFER_SIZE)) == NULL ) {
fclose(in);
return -ERROR_NOT_ENOUGH_MEMORY;
}
memset( buffer, 0, FREAD_BUFFER_SIZE );
ZT_t::reset_find_pointer();
while( !feof(in) && !ferror(in) )
{
int nStart = 0;
offset += fread(buffer+offset, sizeof(unsigned char), FREAD_BUFFER_SIZE-offset, in);
if( offset < SCI_PACKET_LENGTH )
continue;
while( offset - nStart >= SCI_PACKET_LENGTH )
{
S_SCI_header *sci = (S_SCI_header *)(buffer+nStart);
unsigned char *data = buffer+nStart+SXM_SCI_PACKET_HDR_LENGTH;
Tm2Time( sci->time_code, &hxmt.start.time, &hxmt.start.usec );
for( int i = 0; i < 121; i++ )
{
SXM_t *sxm = (SXM_t *)data;
timespec_t time;
sx_t s_time;
unsigned char sxm_data[2];
memset( sxm_data, 0, sizeof(sxm_data) );
memset( &s_time, 0, sizeof(s_time) );
if( 0 == chaNo(sxm) ) {
hxmt.start.time += 1;
} else {
hxmt.start.usec = ptime(sxm)<<3;
{
std::string prev_fitsname;
if( fptr )
prev_fitsname = fptr->Fptr->filename;
std::string new_fitsname;
std::string pathname;
if( get_fits_filename(&hxmt, new_fitsname, pathname, modetype, fits_name) ) {
status = -4; goto PROCESS_OUT;
}
if( new_fitsname != prev_fitsname ) {
if( fptr != NULL ) {
update_fits_keys( fptr, &fits_start, &fits_end, mode_for_update );
close_fits_sci( fptr );
}
fptr = NULL;
if( 0 != open_fits_sx_sci( &hxmt, &fptr ) ) {
status = -3; goto PROCESS_OUT;
}
mode_for_update = modetype;
bnewfitsfile = true;
}
}
sxm_data[0] = (char)energy( sxm );
sxm_data[1] = (char)chaNo( sxm ) ;
if( sxm_data[1] == 0 )
continue;
time.time = hxmt.start.time;
time.usec = ptime(sxm)<<3;
struct tm *tm = NULL;
if( (tm = localtime(&time.time)) != NULL )
set_sxtime( &s_time, tm, time.usec );
if( bnewfitsfile ) {
fits_start = fits_end = hxmt.start;
bnewfitsfile = false;
}
if( 0 != write_fits_sx_sci( fptr, (const char *)sxm_data, s_time, 1, &hxmt.start) ) {
status = -2; goto PROCESS_OUT;
}
fits_end = hxmt.start;
}
data += SXM_LENGTH;
}
nStart += SCI_PACKET_LENGTH;
}
offset -= nStart;
memcpy( buffer, buffer+nStart, offset );
}
PROCESS_OUT:
free( buffer );
if( fptr ) {
update_fits_keys( fptr, &fits_start, &fits_end, mode_for_update );
close_fits_sci( fptr );
}
fclose( in );
return status;
}
int LoadJS( const char * filename, struct sci_intern *s )
{
return 0;
}
/*****************************************************
** 函数名: LoadCIR
** 输入: filename, s
** filename --- 文件名
** s --- 容器数据结构
** 输出:
** 0 --- 函数执行成功
** 其他 --- 函数没有正常执行完,中途退出
**调用说明:
** strUTC2time()
** 功能描述: 把圈次文件中的每行数据存入定义好的容器中
****************************************************************************************/
int LoadCIR( const char *filename, struct sci_intern *s )
{
FILE *fp = NULL;
char *buffer = NULL;
if( !s || (fp = fopen(filename, "r")) == NULL )//打开圈次文件
return -1;
//开辟4096字节的存储区
if( (buffer = (char *)malloc(FREAD_BUFFER_SIZE)) == NULL ) {
fclose(fp);
return -ERROR_NOT_ENOUGH_MEMORY;
}
while( fgets(buffer, FREAD_BUFFER_SIZE, fp) )//循环读入每行圈次文件中的数据
{
CIR_t cir;
char *p = strchr(buffer, '\t');//找到第一个空格
*p = '\0';
cir.id = buffer;//把第一个空格前的变量(卫星号)赋给id
p += 1;
sscanf( p, "%d", &cir.cir );//读入圈数给变量cir
p = strchr( p, '\t' ) + 1;
strUTC2time( p, &cir.utc );//读入此圈的开始时间给utc
p = strchr( p, '\t' ) + 1;
strUTC2time( p, &cir.time2 );//读入此圈的结束时间给time2
s->cir_table.push_back( cir );//把此条记录存入容器
}
free( buffer );
fclose( fp );
return 0;
}
/*****************************************************
** 函数名: LoadSCMD
** 输入: filename, s
** filename --- 文件名
** s --- 容器数据结构
** 输出:
** 0 --- 函数执行成功
** 其他 --- 函数没有正常执行完,中途退出
**调用说明:
** strUTC2time()
** 功能描述: 把模式文件中的每行数据存入定义好的容器中
****************************************************************************************/
int LoadSCMD( const char *filename, struct sci_intern *s )
{
FILE *fp = NULL;
char *buffer = NULL;
if( !s || (fp = fopen(filename, "r")) == NULL )//打开模式文件
return -1;
//开辟4096字节的存储区
if( (buffer = (char *)malloc(FREAD_BUFFER_SIZE)) == NULL ) {
fclose(fp);
return -ERROR_NOT_ENOUGH_MEMORY;
}
while( fgets(buffer, FREAD_BUFFER_SIZE, fp) )//循环读入每一行,
{
SCMD_t scmd;
char *p = NULL;
if( buffer[0] == 'P' )//定点模式
{
scmd.mode = 'P';
if( (p = strchr(buffer+1,'\t')) == NULL ) continue;//找到第一个空格
*p = '\0';
scmd.id = (buffer+1); //标识号赋给id
} else if( buffer[0] == 'X' ) {
scmd.mode = 'X';//巡天模式
if( (p = strchr(buffer, '\t')) == NULL ) continue;//找到第一个空格
} else
continue;
++p;
strUTC2time( p, &scmd.start );//此模式开始时间
p = strchr( p, '\t' ) + 1;
strUTC2time( p, &scmd.end );//此模式结束时间
s->scmd_table.push_back( scmd );//把此条记录存入容器
}
free( buffer );
fclose( fp );
return 0;
}
/*****************************************************
** 函数名: FindCIR
** 输入: time, s, id
** id --- 卫星号
** s --- 容器数据结构
** time --- 某个事例发生的时间
** 输出:b
** b --- 此事例发生时间所对应的圈数
** 0 --- 没有找到对应的圈数
** 功能描述: 输入一个事例的时间,在圈次容器中找出它所对应的圈次结构
****************************************************************************************/
CIR_t * FindCIR( struct sci_intern *s, const timespec_t *time, const std::string& id )
{
if( s == NULL || time == NULL )
return NULL;
std::vector<CIR_t>::iterator b(s->cir_table.begin());
std::vector<CIR_t>::iterator e(s->cir_table.end());
for( b; b != e; ++b )//循环查找圈次,得到给定的时间在圈次文件中对应的圈次结构
{
if( b->utc.time>time->time || (b->utc.time==time->time && b->utc.usec>time->usec) )
break;
}
if( b == s->cir_table.begin() )
return NULL;
return --b;
}
/*****************************************************
** 函数名: FindSCMD
** 输入: time, s,
** s --- 容器数据结构
** time --- 某个事例发生的时间
** 输出:b
** b --- 此事例发生时间所对应的模式
** 0 --- 没有找到对应的模式
** 功能描述: 输入一个事例的时间,在模式容器中找出它所对应的模式
****************************************************************************************/
SCMD_t * FindSCMD( struct sci_intern *s, const timespec_t *time )
{
if( s == NULL || time == NULL )
return NULL;
std::vector<SCMD_t>::iterator b(s->scmd_table.begin());
std::vector<SCMD_t>::iterator e(s->scmd_table.end());
for( b; b != e; ++b )//循环查找模式,得到给定时间在模式文件中的对应模式
{
if( (b->start.time < time->time || (b->start.time == time->time && b->start.usec<=time->usec))
&& (b->end.time > time->time || (b->end.time == time->time && b->end.usec > time->usec)) )
break;
}
if( b == e )
return NULL;
return b;
}
/*****************************************************
** 函数名: FindZT
** 输入: time, s,
** s --- 容器数据结构
** time --- 某个事例发生的时间
** 输出:b
** b --- 此事例发生时间所对应的姿态
** 0 --- 没有找到对应的姿态
**调用说明:
** get_find_pointer()
** set_find_pointer()
** 功能描述: 输入一个事例的时间,在姿态容器中找出它所对应的姿态
****************************************************************************************/
const ZT_t * FindZT( struct sci_intern *s, const timespec_t *time)
{
if( s == NULL || time == NULL)
return NULL;
time_t t_time = time->time;
long usec = time->usec;
std::vector<ZT_t>::const_iterator b(s->zt_table.begin());
std::vector<ZT_t>::const_iterator e(s->zt_table.end());
ZT_t::find_pointer s_ite = ZT_t::get_find_pointer();
if( s_ite == NULL ) {
if( t_time < b->utc.time )
return b;
for( s_ite = b; s_ite != e ; ++s_ite ) {
if( t_time - s_ite->utc.time < 1000000 )
break;
}
if( s_ite == e )
return --s_ite;
}
ZT_t::set_find_pointer( s_ite );
b = s_ite + 1;
long t1 = (t_time - s_ite->utc.time) * 1000;
t1 += (usec - s_ite->utc.usec) / 1000;
t1 = abs(t1);
for( ; b != e; ++b)
{
ASSERT( (t_time - b->utc.time < 1000000) );
long t2 = (t_time - b->utc.time) * 1000;
t2 += (usec - b->utc.usec) / 1000;
t2 = abs(t2);
if( t1 < t2 )
break;
else {
ZT_t::set_find_pointer( b );
t1 = t2;
}
}
return ZT_t::get_find_pointer();
}
int is_x_mode( timespec_t t )//此函数判断某一时间是否是巡天模式
{
SCMD_t *scmd = FindSCMD( get_sci_data(), &t );
if( scmd == NULL )
return -1;
return scmd->mode == 'X';//相等则返回1(true),不相等则返回0(false)
}
int get_cir_num( timespec_t t )//此函数得到对应时间的圈数
{
std::string str;
CIR_t *cir = FindCIR( get_sci_data(), &t, str );
if( cir == NULL )
return -1;
return cir->cir;
}
std::string get_x_id( timespec_t t )//此函数得到定点模式下任务的标识号
{
SCMD_t *scmd = FindSCMD( get_sci_data(), &t );
if( scmd && scmd->mode == 'P' )
return scmd->id;
else
return "";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -