C语言读取MDSplus树数据

#include <stdio.h>
#include <string.h>
#include <mdslib.h>

#define EXIT_FAILURE -1
#define status_ok(status) (((status) & 1) == 1)
/定义get_signal_length函数/
int get_signal_length(const char signal)
{
/
local vars */
int dtype_long = DTYPE_LONG;
char buf[1024];
int size;
int null = 0;
int idesc = descr(&dtype_long, &size, &null);
int status;

/* init buffer */
memset(buf,0,sizeof(buf));

/* put SIZE() TDI function around signal name */
snprintf(buf,sizeof(buf)-1,“SIZE(%s)”,signal);

/* use MdsValue to get the signal length */
status = MdsValue(buf, &idesc, &null, 0);
if ( !status_ok(status) )
{
fprintf(stderr,“Unable to get length of %s.\n”,signal);
return EXIT_FAILURE;
}

/* return signal length */
return size;

}

int main(int argc, char argv[])
{
/
local vars */
int dtype_float = DTYPE_FLOAT;
int null = 0;
int shot = 107000;
int status;
int socket;
float data; / array of floats used for signal */
float timebase; / array of floats used for timebase /
int sigdesc; /
signal descriptor /
int timedesc; /
descriptor for timebase /
int size; /
length of signal */
int i, len;

/* 连接MDSplus服务器 */
socket = MdsConnect("202.127.204.3");
if ( socket == -1 )
{
  fprintf(stderr,"Error connect.\n");
  return EXIT_FAILURE;
}

/*打开shot树 */
status = MdsOpen("EFIT01", &shot);
if ( !status_ok(status) )
{
  fprintf(stderr,"Error opening EFIT01 tree for shot %l.\n",shot);
  return EXIT_FAILURE;
}


size = get_signal_length("\\AMINOR");
if ( size < 1 )
{
  /* error */
  fprintf(stderr,"Error retrieving length of signal\n");
  return EXIT_FAILURE;
}

/* 用malloc() 给信号分配内存 */
data = (float *)malloc(size * sizeof(float));
if ( !data )
{
  fprintf(stderr,"Error allocating memory for data\n");
  return EXIT_FAILURE;
}

/* 创建信号描述符 */
sigdesc = descr(&dtype_float, data, &size, &null);

/*获取信号 */
status = MdsValue("\\AMINOR",&sigdesc, &null, &len );
if ( !status_ok(status) )
{
  /* error */
  fprintf(stderr,"Error retrieving signal\n");
  free( (void *)data );
  return EXIT_FAILURE;
}

/* 使用malloc()为timebase分配内存 */
timebase = (float *)malloc(size * sizeof(float));
if ( !timebase )
{
  fprintf(stderr,"Error allocating memory for timebase");
  free( (void *)data );
  return EXIT_FAILURE;
}

/* 创建timebase描述符 */
timedesc = descr(&dtype_float, timebase, &size, &null);

/* 获取信号的 timebase  */
status = MdsValue("DIM_OF(\\AMINOR)", &timedesc, &null, 0);
if ( !status_ok(status) )
{
  /* error */
  fprintf(stderr,"Error retrieving timebase\n");
  free( (void *)data );
  free( (void *)timebase );
  return EXIT_FAILURE;
}



/* 例如打印出信号中的十个数 */
for ( i = 0 ; i < size && i < 10 ; i++ )
  printf("%i  X:%f  Y:%f\n", i, timebase[i], data[i]);

/* 释放内存*/
free( (void *)data );
free( (void *)timebase );

return 0;

}
此段代码是利用C语言读取MDSplus数据库中某一棵树某个节点的信息,里面有详细的注释,具体参见MDSplus官网http://www.mdsplus.org/index.php?title=Introduction&open=50764574999721279519&page=Introduction

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值