把这个读wis文件的程序转换为C#版本的,能出正确的结果

黯然若水 2012-03-14 11:03:05
原网址http://blog.csdn.net/syunqiang/article/details/6
大侠们,救命啦!
/** ***************************************************************** */
/** This Program is for converting wis format file to txt format file */
/** Command: wis2txt_win.exe args1 args2 */
/** Description: */
/** wis2txt_win.exe: the executable file as command */
/** args1: the first argument that is the wis file name */
/** args2: the second argument that is the txt file name */
/** ***************************************************************** */

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

#define ROW 65536
#define COL 512


/* **************************************** */
/* Below blocks are the structs of wis file */
/* **************************************** */
typedef struct tagWIS_HEAD
{
WORD MachineType; // 0-PC 1-SUN 2-IBM 3-HP
WORD MaxObjectNumber;
WORD ObjectNumber;
WORD BlockLen;
DWORD EntryOffset;
DWORD DataOffset;
DWORD FileSize;
time_t TimeCreate;
char Reserved[32];
}WIS_HEAD;

typedef struct tagWIS_OBJECT_ENTRY
{
char Name[16];
long Status;
short Attribute; // 1-通道对象 2-表对象 3-流对象
short SubAttribute; // 1-曲线对象 2-波形对象 3-地层测试对象 4-时深时对象
DWORD Position; // 对象数据体从文件开始处的偏移量
DWORD BlockNum;
time_t TimeCreate;
time_t TimeWrite;
char Reserved[32];
}WIS_OBJECT_ENTRY;

typedef struct tagWIS_CHANNEL_DIMENSION
{
char Name[8];
char Unit[8];
char AliasName[16];
float StartVal;
float Delta;
DWORD Samples;
DWORD MaxSamples;
DWORD Size;
WORD RepCode;
WORD Reserved;
}WIS_CHANNEL_DIMENSION;

typedef struct tagWIS_CHANNEL
{
char Unit[8];
char AliasName[16];
char AliasUnit[16];
WORD RepCode;
WORD CodeLen;
float MinVal;
float MaxVal;
WORD Reserved;
WORD NumOfDimension;
WIS_CHANNEL_DIMENSION DimInfo[4];
}WIS_CHANNEL;

typedef struct tagWIS_STREAM
{
DWORD Length;
DWORD Offset;
}WIS_STREAM;

typedef struct tagWIS_TABLE_FIELD
{
char Name[32];
WORD RepCode;
WORD Length;
DWORD Reserved;
} WIS_TABLE_FIELD;

typedef struct tagWIS_TABLE
{
DWORD RecordCount;
DWORD FieldCount;
WIS_TABLE_FIELD *pField;
}WIS_TABLE;


typedef struct tagWIS_TABLE_DEFAULT_FILED
{
char Name[16];
char Alias[16];
char Unit[8];
char Type[8];
WORD Length;
WORD Count;
char DefVal[64][12];
} WIS_TABLE_DEFAULT_FIELD;


typedef struct tagWIS_DEFAULT_TABLE
{
char Name[16];
char Alias[16];
char Attrb[8];
DWORD FieldCount;
WIS_TABLE_DEFAULT_FIELD *pField;
} WIS_DEFAULT_TABLE;



/* 曲线的信息结构体*/
typedef struct tagCURVE_HEAD
{
char Name[16]; // 曲线的名字
char Unit[8]; // 曲线的单位
float startMD; // 曲线的起始深度
float stopMD; // 曲线的结束深度
int pointNum; // 数据样点数
int position; // 数据位置
struct tagCURVE_HEAD *next;
} CURVE_HEAD;

/* 曲线的数据结构体 */
typedef struct tagCURVE_DATA
{
char Name[16]; // 曲线的名字
char Unit[8]; // 曲线的单位
float Depth[ROW]; // 曲线的深度值
float Value[ROW]; // 曲线的值
struct tagCURVE_DATA *next;
} CURVE_DATA;


...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
忠向 2013-11-22
  • 打赏
  • 举报
回复
原始文件就有些问题
黯然若水 2012-03-14
  • 打赏
  • 举报
回复
/* *********************************************************************** */
/* Below block is for deciding the min and max Depth, and the max line num */
/* *********************************************************************** */

// 从这里开始判断所有曲线中最小深度和最大深度值
float minDepth = 0.0;
float maxDepth = 0.0;
minDepth = first_head->startMD;
maxDepth = first_head->stopMD;
for(data_head = first_head; data_head!= NULL; data_head=data_head->next)
{
if(minDepth>data_head->startMD)
{
minDepth = data_head->startMD;
}
if(maxDepth < data_head->stopMD)
{
maxDepth = data_head->stopMD;
}
}
// printf("最小深度值:%f/n", minDepth);
// printf("最大深度值:%f/n", maxDepth);

// 利用最小最大深度值和间隔点值,确定输出的采样点数
float depth[ROW];
int lineNum = 0;
lineNum = (maxDepth - minDepth) / delta;
// printf("采样点个数:%d/n", lineNum);

int row=0;
for(row=0; row<lineNum; row++)
{
depth[row] = minDepth + delta * row;
}



/* ******************************************************** */
/* Below block put every point value into a link as a curve */
/* ******************************************************** */

// 从这里开始将数据值放入曲线各自对应的链表里

CURVE_DATA *first_node = NULL;
CURVE_DATA *data_node = NULL;

data_head = first_head;

for(num = 0; num < curveNum ; num++)
{
data_node = (CURVE_DATA *)malloc(sizeof(CURVE_DATA));
int i = 0;
for(i=0; i<16; i++)
{
data_node->Name[i] = data_head->Name[i];
}
for(i=0; i<8; i++)
{
data_node->Unit[i] = data_head->Unit[i];
}
// printf("曲线名称:%s/n", data_node->Name);
// printf("曲线单位:%s/n", data_node->Unit);

int startLine = 0;
for(row=0; row<lineNum; row++)
{
if( abs(depth[row]-data_head->startMD)<= delta
&& depth[row] >= data_head->startMD )
{
startLine = row;
break;
}
}
// printf("起始点:%d/n", startLine);

// fseek(wisfile, data_head->position, SEEK_SET);
fseek(wisfile, data_head->position+2*sizeof(WIS_CHANNEL)+464, SEEK_SET);

// printf("测试样点数%d/n", data_head->pointNum);
for(row=0; row<lineNum; row++)
{
float data[1];
data[0] = -9999.000;
if(row>=startLine && (row-startLine) < data_head->pointNum)
{
fread(data, sizeof(float), 1, wisfile);
data_node->Depth[row] = data_head->startMD + delta * (row-startLine);
data_node->Value[row] = data[0];
}
else
{
data_node->Depth[row] = minDepth + delta * row;
data_node->Value[row] = -9999.000;
}

// printf("读取值:%f/n", data_node->Value[row]);
}


data_node->next = first_node;
first_node = data_node;
if(data_head->next == NULL)
{
break;
}

data_head = data_head->next;
// printf("下一条曲线名:%s/n", data_head->Name);
}


/* **************************************************** */
/* Below block print all point value for every curve */
/* The important thing is how to decide the right depth */
/* **************************************************** */

// 从这里开始写入文本

// Print Curve Name
fprintf(txtfile, "DEPTH ");
for(data_node=first_node; data_node!=NULL; data_node=data_node->next)
{
fprintf(txtfile, "%9s ", data_node->Name);
}
fprintf(txtfile, "\n");
// printf("/n");

// Print Curve Unit
fprintf(txtfile, "M ");
for(data_node=first_node; data_node!=NULL; data_node=data_node->next)
{
fprintf(txtfile, "%9s ", data_node->Unit);
}
fprintf(txtfile, "\n");
// printf("/n");

// Print curve value
for(row=0; row<lineNum; row++)
{
if(depth[row] > maxDepth)
{
break;
}
fprintf(txtfile, "%-9.4f ", depth[row]);
for(data_node=first_node; data_node!=NULL; data_node=data_node->next)
{
// fprintf(txtfile, "%-9.4f ", data_node->Depth[row]);
fprintf(txtfile, "%9.3f ", data_node->Value[row]);
}
fprintf(txtfile, "\n");
}
// */


// Process Over
fclose(wisfile);
fclose(txtfile);
printf("\n%s Complete!\n", infilename);
// system("PAUSE");
// Program Over
return 0;
}
黯然若水 2012-03-14
  • 打赏
  • 举报
回复
/* ************************************************************* */
/* Below block is the Main program of processing wis format file */
/* ************************************************************* */

int main(int argc, char *argv[])
{
FILE *wisfile; // 输入文件
FILE *txtfile; // 输出文件
//char *infilename = argv[1];
//char *outfilename = argv[2];
char *infilename = "demo.wis";
char *outfilename = "demo.txt";

/*if(argc != 3 )
{
printf("ERROR: Command parameters is not defined correctly!/n");
exit(1);
}*/


wisfile = fopen(infilename, "rb");
if(wisfile == NULL)
{
printf("ERROR: Read file terminated!/n");
exit(1);
}
txtfile = fopen(outfilename, "w");
if(txtfile == NULL)
{
printf("ERROR: Write file terminated!/n");
exit(1);
}

/* Process wis file, Read it and transfer it to text */

/** wis文件标识符从文件偏移0开始,为10个字节的字符 */
char wisid[10];
fread(wisid, 10, 1, wisfile);
// printf("文件标识符: %s/n", wisid);
fprintf(txtfile, "ASCII WIS %c%c%c\n", wisid[4], wisid[5], wisid[6]);

/*fread
  功 能: 从一个流中读数据
  函数原型: size_t fread(void*buffer,size_tsize,size_tcount,FILE*stream); 
  参 数:
  1.用于接收数据的地址(指针)(buffer)
  2.单个元素的大小(size) :单位是字节而不是位,例如读取一个int型数据就是4个字节
  3.元素个数(count)
  4.提供数据的文件指针(stream)
  返回值:读取的元素的个数*/

/** 头文件紧接文件标识 */
WIS_HEAD *wishead;
wishead = (WIS_HEAD *)malloc(sizeof(WIS_HEAD));
fseek(wisfile, 10, SEEK_SET);
fread(wishead, sizeof(WIS_HEAD), 1, wisfile);
printf("长度: %d\n",sizeof(WIS_HEAD));
printf("对象入口记录从文件开始的偏移量EntryOffset: %d\n", wishead->EntryOffset);//66
printf("对象数据记录从文件开始的偏移量DataOffset: %d\n", wishead->DataOffset);//377888
printf("当前记录的对象总数ObjectNumber: %d\n", wishead->ObjectNumber);


/** 对象入口,位置由头结构中EntryOffset参数指定 最多512条曲线 */
/** curvePosition数组 保存曲线数据开始的位置 WIS_OBJECT_ENTRY->Position的值 */
/** curveName数组 保存每条曲线的名称 */
/** curveNum变量 文件中保存曲线的数目 */
int curvePosition[512];
int curveName[512][16];
int curveNum = 0; // curveNum is the identifer of the curves number
fseek(wisfile, wishead->EntryOffset, SEEK_SET);
// fseek(wisfile, 10+sizeof(WIS_HEAD), SEEK_SET);
while(1)
{
WIS_OBJECT_ENTRY *objectEntry;
objectEntry = (WIS_OBJECT_ENTRY *)malloc(sizeof(WIS_OBJECT_ENTRY));
fread(objectEntry, sizeof(WIS_OBJECT_ENTRY), 1, wisfile);

if(objectEntry->Attribute == 0)
{
break;
}
else if(objectEntry->Attribute == 1)
{
// printf("通道类型: %d/n",objectEntry->Attribute);
// printf("No. %d/n", curveNum);
// printf("曲线对象的名称Name: %s/n", objectEntry->Name);
// fprintf(outfile, "%s ", objectEntry->Name); // 打印曲线名字
// printf("对象数据体绝对偏移量Position: %d/n/n", objectEntry->Position);
curvePosition[curveNum] = objectEntry->Position;
int i=0;
for(i=0; i<16; i++)
{
curveName[curveNum][i] = objectEntry->Name[i];
}

curveNum++;
}
else
{
continue;
}

}
printf("\n%s文件的曲线数目: %d\n\n", infilename, curveNum);



/* ****************************************************************** */
/* Below block read every curve head information and put it in a link */
/* ****************************************************************** */

/** 创建链表 data_node 开始读取数据 */
/** num变量 每条曲线的行数 */
CURVE_HEAD *first_head = NULL;
float delta = 0.0;

CURVE_HEAD *data_head = NULL;
int num = 0;
for(num = 0; num < curveNum ; num++) // num相当于第几条曲线
{

data_head = (CURVE_HEAD *)malloc(sizeof(CURVE_HEAD));
data_head->position = curvePosition[num];

fseek(wisfile, curvePosition[num], SEEK_SET);
WIS_CHANNEL *channel;
channel = (WIS_CHANNEL *)malloc(sizeof(WIS_CHANNEL));
fread(channel, sizeof(WIS_CHANNEL), 1, wisfile);
// printf("对象的单位Unit: %s/n", channel->Unit);
// printf("对象的别名AliasName: %s/n", channel->AliasName);
// printf("单位的别称AliasUnit: %s/n", channel->AliasUnit);
// printf("对象的最小值MinVal: %f/n", channel->MinVal);
// printf("对象的最大值MaxVal: %f/n", channel->MaxVal);
// printf("对象维信息数NumOfDimension: %d/n", channel->NumOfDimension);
// printf("维的开始值StartVal: %f/n", channel->DimInfo[0].StartVal);
// printf("维的增量值Delta: %f/n", channel->DimInfo[0].Delta);

data_head->pointNum = channel->DimInfo[0].MaxSamples; // 深度采样点的个数
data_head->startMD = channel->DimInfo[0].StartVal; // 起始深度
delta = channel->DimInfo[0].Delta; // 间隔点值
data_head->stopMD = channel->DimInfo[0].StartVal + delta * channel->DimInfo[0].MaxSamples; // 结束深度


int i = 0;
for(i=0; i<16; i++)
{
data_head->Name[i] = curveName[num][i];
}
for(i=0; i<8; i++)
{
data_head->Unit[i] = channel->Unit[i];
}

printf("No.%d\t", num+1); // 曲线编号
printf("%s\t", data_head->Name); // 曲线名
printf("%s\t", data_head->Unit); // 曲线单位
printf("%d\t", data_head->pointNum); // 采样点数
printf("%9.3f\t", delta); // 采样点间隔
printf("%9.3f\t", data_head->startMD); // 起始深度
printf("%9.3f\n", data_head->stopMD); // 结束深度



data_head->next = first_head;
first_head = data_head;
}



110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧