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

📄 fits_tcl.cpp

📁 本源码是一个用于卫星数据处理的程序
💻 CPP
字号:

#include <stdio.h>
#include "stdafx.h"
#include "ccsds_pro.h"
#include "fits_tcl.h"
/********此文件中函数前都有详细说明,函数中具体实现参照fits_asf.cpp中的注释******/

/*****************************************************
** 函数名: LoadTCL
** 输入: hxmtfile, s
**        hxmtfile    --- 文件名结构 
**        s           --- 存放多个容器的结构体
** 输出: 
**           函数执行成功返回0
** 调用说明:	
**			strUTC2time() .......把年月日是时分秒格式的时间字符串转换成对应的秒	
**			close_fits_tcl()....关闭fits文件
**			get_fits_filename()..取得fits文件名和保存路径
**          open_fits_tcl() ....生成fits文件并在fits肉中写入关键字
**			write_fits_tcl() ...写fits文件
** 功能描述:输入一个校时文件,把文件中的每行数据的各个字段读入结构体中,并生成fits文件,然后写入fits文件中
****************************************************************************************/

int LoadTCL( 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_tcl( fptr );
		return -ERROR_NOT_ENOUGH_MEMORY;
	}

	while( fgets(buffer, FREAD_BUFFER_SIZE, fp) )
	{
		TCL_t tcl;
		float f;
		char *p = NULL;
		if( (p = strchr(buffer, '\t')) == NULL )
			continue;
		*p = '\0';
		strUTC2time( buffer, &tcl.time );

		sscanf( ++p, "%f", &f);
		tcl.tcal = f;
		
		hxmt.start.time = tcl.time.time;
		hxmt.start.usec = tcl.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_tcl( fptr );
				fptr = NULL;
				if( 0 != open_fits_tcl( &hxmt, &fptr) ) {
					fclose( fp ); 
					free( buffer );
					return -3;
				}
			}
		}
        write_fits_tcl( fptr, &tcl, 1 );
	}
	
	free( buffer );
	close_fits_tcl( fptr );
	fclose( fp );

	return 0;	
}
/*****************************************************
** 函数名: open_fits_tcl
** 输入: hxmtfile, 
**        hxmtfile    --- 文件名结构 
** 输出: *fptr
**        *fptr       ---
** 调用说明:	
**			get_fits_filename() .......得到文件名和保存路径以及模式
**			DirExist1()         .......判断路径是否存在
**			CreateFilePath()    .......创建一个新路径
**			time2strUTC_fitshead() ....秒时间转换成对应的年月日时分秒格式的字符串
** 功能描述: 输入一个校时文件,生成fits文件,并写入fits头中的关键字,通过fptr返回生成fits文件的结构
****************************************************************************************/
int open_fits_tcl( 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   = 2;       /* table will have 2 columns */
		long nrows    = 0;
		char extname[] = "TCAL_Binary";           /* extension name */
		char *ttype[] = { "TIME", "TCAL" };
		char *tform[] = { "27A", "1D" };
		char *tunit[] = { "\0", "s" };
		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_tcl
** 输入: fptr, tcl,n
**        hxmtfile    --- 文件名结构 
**        tcl          --- 校时数据结构
**		  n           ---
** 输出: 
**        函数执行成功返回0
** 调用说明:	
**			time2strUTC_fitshead() ....秒时间转换成对应的年月日时分秒格式的字符串
** 功能描述: 输入先前生成的fits文件结构和校时数据,把文件中数据写入fits文件中
****************************************************************************************/
int write_fits_tcl( fitsfile *fptr, const TCL_t *tcl, 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;
		char strUTC[40];
		time2strUTC_fitshead( strUTC, sizeof(strUTC), &tcl->time );
		var[0] = (void *)strUTC;
		
		fits_write_col(fptr, TSTRING, 1, firstrow, firstelem, n, var, &status);
		fits_write_col(fptr, TDOUBLE, 2, firstrow, firstelem, n, (void *)&tcl->tcal, &status);
	
	}
	return 0;
}

int close_fits_tcl( 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 + -