?? fits_tmln.cpp
字號:
#include <stdio.h>
#include "stdafx.h"
#include "ccsds_pro.h"
#include "fits_tmln.h"
/********此文件中函數前都有詳細說明,函數中具體實現參照fits_asf.cpp中的注釋******/
/*****************************************************
** 函數名: LoadTMLN
** 輸入: hxmtfile, s
** hxmtfile --- 文件名結構
** s --- 存放多個容器的結構體
** 輸出:
** 函數執行成功返回0
** 調用說明:
** strUTC2time() .......把年月日是時分秒格式的時間字符串轉換成對應的秒
** close_fits_tmln()....關閉fits文件
** get_fits_filename()..取得fits文件名和保存路徑
** open_fits_tmln() ....生成fits文件并在fits肉中寫入關鍵字
** write_fits_tmln() ...寫fits文件
** 功能描述:輸入一個TIMELINE文件,把文件中的每行數據的各個字段讀入結構體中,并生成fits文件,然后寫入fits文件中
****************************************************************************************/
int LoadTMLN( 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_tmln( fptr );
return -ERROR_NOT_ENOUGH_MEMORY;
}
while( fgets(buffer, FREAD_BUFFER_SIZE, fp) )
{
TMLN_t tmln;
char *p = NULL;
if( (p = strchr(buffer, '\t')) == NULL ) continue;
sscanf( buffer, "%d", &tmln.cir );
char* temp = ++p;
strUTC2time( temp, &tmln.time );
if( (p = strchr( p, '\t')) == NULL ) continue;
temp = ++p;
if( (p = strchr( p, '\t')) == NULL ) continue;
*p = '\0';
tmln.event = temp;
sscanf( ++p, "%d", &tmln.param );
hxmt.start.time = tmln.time.time;
hxmt.start.usec = tmln.time.usec;
{
std::string new_fitsname;
std::string pathname;
get_fits_filename( &hxmt, new_fitsname, pathname, modetype, fits_name);
if( new_fitsname != prev_fitsname ) {
prev_fitsname = new_fitsname;
close_fits_tmln( fptr );
fptr = NULL;
if( 0 != open_fits_tmln( &hxmt, &fptr) ) {
fclose( fp );
free( buffer );
return -3;
}
}
}
write_fits_tmln( fptr, &tmln, 1 );
}
free( buffer );
close_fits_tmln( fptr );
fclose( fp );
return 0;
}
/*****************************************************
** 函數名: open_fits_tmln
** 輸入: hxmtfile,
** hxmtfile --- 文件名結構
** 輸出: *fptr
** *fptr ---
** 調用說明:
** get_fits_filename() .......得到文件名和保存路徑以及模式
** DirExist1() .......判斷路徑是否存在
** CreateFilePath() .......創建一個新路徑
** time2strUTC_fitshead() ....秒時間轉換成對應的年月日時分秒格式的字符串
** 功能描述: 輸入一個TIMELINE文件,生成fits文件,并寫入fits頭中的關鍵字,通過fptr返回生成fits文件的結構
****************************************************************************************/
int open_fits_tmln( 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", "", &status) )
return status;
if ( fits_update_key(fp, TSTRING , "EPOCH", buf, "2006-10-10T01:39:00.000000Z", &status) )
return status;
status = 0;
int tfields = 4; /* table will have 4 columns */
long nrows = 0;
char extname[] = "TIMELINE_Binary"; /* extension name */
char *ttype[] = { "REVLUT", "TIME", "EVENT", "PARAM" };
char *tform[] = { "1J", "27A", "24A", "1J" };
char *tunit[] = { "\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_tmln
** 輸入: fptr, tmln,n
** hxmtfile --- 文件名結構
** tmln --- TIMELINE數據結構
** n ---
** 輸出:
** 函數執行成功返回0
** 調用說明:
** time2strUTC_fitshead() ....秒時間轉換成對應的年月日時分秒格式的字符串
** 功能描述: 輸入先前生成的fits文件結構和TIMELINE數據,把文件中數據寫入fits文件中
****************************************************************************************/
int write_fits_tmln( fitsfile *fptr, const TMLN_t *tmln, 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 *)&tmln->cir, &status);
char strUTC[40];
time2strUTC_fitshead( strUTC, sizeof(strUTC), &tmln->time );
var[0] = (void *)strUTC;
fits_write_col(fptr, TSTRING, 2, firstrow, firstelem, n, var, &status);
var[0] = (void *)tmln->event.c_str();
fits_write_col(fptr, TSTRING, 3, firstrow, firstelem, n, var, &status);
fits_write_col(fptr, TINT, 4, firstrow, firstelem, n, (void *)&tmln->param, &status);
}
return 0;
}
int close_fits_tmln( 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 + -