開発メモその2

あらかじめバイナリにしておく? [サポート事例]

Posted Date:2009/08/28(Fri) 11:05rss

 AVS/Expressのフィールドファイルフォーマットは
データとしてアスキーとバイナリどっちも指定できますが
読み込み速度は、バイナリのほうが圧倒的に速くなります。

 ソルバーからの出力をあらかじめバイナリにすることが
できればいいのですが、そうでないばあい変換用のプログラム
を作った方がいいのかどうか?変換するのも時間がかかるし
そのまま読み込んだ方が手間でない場合もあります。

 データが時系列で手法を変えてアニメーションを作るとか
する場合は、もちろん変換してほうがいいですが、これも
やっぱりデータサイズなどによってはそのままでもいいかも
しれません。

 カラムに並んだアスキーデータをバイナリに変換する
サンプルがあったので一応載せておきます。
エラー処理とかほとんどしてないですけど、、、

データファイル ascii.txt。
------------------------------------
value  X    Y    Z
0.02  0.0  0.0  0.0
0.04  5.0  2.0  0.0
0.06  1.0  0.0  0.0
0.08  4.0  4.0  1.0
0.10  3.0  3.0  2.0
0.12  2.0  1.0  2.5
0.14  2.0  4.0  2.5 
0.16  1.0  4.0  3.0 
0.18  3.0  1.0  3.2 
0.20  5.0  4.0  3.5
0.22  3.0  4.0  3.8
0.24  0.0  3.0  4.1
0.26  3.0  0.0  4.3
0.28  1.0  4.0  4.3
0.30  0.0  3.0  4.8

---------------------------------------

変換プログラム ascii_to_binary.c
---------------------------------------
/* ab.c Ascii Binary 座標値 変換*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <string.h>

/* 区切り文字列 */
#define FLDHDRDELIMS " \t=\n\r,"
/* 点の最大個数 */
#define POINTS_MAX   1000

int
main (int argc, char **argv)
{
    FILE *fp_in  = NULL;
    FILE *fp_out = NULL;

    char    out_filename[512];
    char    fld_filename[512];
    char    buffer[1024];

    /* 点の数 */
    int     points_num = 0;
    /* valueと座標値の配列*/
    float   value[POINTS_MAX];
    float   coordx[POINTS_MAX];
    float   coordy[POINTS_MAX];
    float   coordz[POINTS_MAX];

    size_t  size       = 0;
    size_t  write_size = 0;

    char *ptr = NULL;

    /* 引数チェック */
    if( argc < 2 )
    {
        printf("command input_filename\n");
        return(1);
    }
    /* 入力ファイルを開く */
    if ((fp_in = fopen(argv[1], "r")) == NULL)
    {
        printf("Cannot open input file %s\n",argv[1]);
        return( 0 );
    }

    /* 1行目を読み飛ばす */
    fgets( buffer , 1023 , fp_in);

    /* value x y zを読み込む*/
    points_num = 0;
    while( NULL != fgets( buffer , 1023 , fp_in) )
    {
        /* value 読み込み */
        ptr = strtok(buffer, FLDHDRDELIMS);
        if( ptr==NULL ){ break;}
        value[points_num] = atof( ptr );
        /* coord X読み込み */
        ptr = strtok(NULL, FLDHDRDELIMS);
        coordx[points_num] = atof( ptr );
        /* coord Y読み込み */
        ptr = strtok(NULL, FLDHDRDELIMS);
        coordy[points_num] = atof( ptr );
        /* coord Z読み込み */
        ptr = strtok(NULL, FLDHDRDELIMS);
        coordz[points_num] = atof( ptr );
/* デバッグ用 */
/*
printf("[%d]={%f,%f,%f,%f}\n"
       ,points_num
       ,value[points_num]
       ,coordx[points_num]
       ,coordy[points_num]
       ,coordz[points_num]
       );
*/
        points_num++;
    }
    fclose(fp_in);

    /* バイナリデータとfieldヘッダを出力する。 */
    /* バイナリファイルを開く */
    sprintf( out_filename,"%s.bin",argv[1]);
    if ((fp_out = fopen( out_filename, "wb")) == NULL)
    {
        printf("Cannot openoutput file %s\n",out_filename);
        return( 0 );
    }
    size = (size_t)points_num;
    /* Valueを出力 */
    write_size = fwrite( value , sizeof(float), size, fp_out);
    if(write_size != size )
    {
        printf("can't write value array\n");
    }
    /* X座標値を書き込み */
    write_size = fwrite( coordx, sizeof(float), size, fp_out);
    if(write_size != size )
    {
        printf("can't write coordx array\n");
    }
    /* Y座標値を書き込み */
    write_size = fwrite( coordy, sizeof(float), size, fp_out);
    if(write_size != size )
    {
        printf("can't write coordy array\n");
    }
    /* Z座標値を書き込み */
    write_size = fwrite( coordz, sizeof(float), size, fp_out);
    if(write_size != size )
    {
        printf("can't write coordz array\n");
    }
    fclose( fp_out );

    /* フィールドヘッダを出力する */
    sprintf( fld_filename,"%s.fld",argv[1]);
    if ((fp_out = fopen( fld_filename, "w")) == NULL)
    {
        printf("Cannot open output file %s\n",fld_filename);
        return( 0 );
    }
    fprintf(fp_out,"# AVS field file\n");
    fprintf(fp_out,"ndim=1\n");
    fprintf(fp_out,"dim1=%d\n",points_num);
    fprintf(fp_out,"nspace=3\n");
    fprintf(fp_out,"veclen=1\n");
    fprintf(fp_out,"data=float\n");
    fprintf(fp_out,"field=irregular\n");
    fprintf(fp_out,"variable 1  file=./%s filetype=binary offset=0\n",out_filename);
    fprintf(fp_out
           ,"coord 1  file=./%s filetype=binary skip=%d\n",out_filename
           ,points_num*sizeof(float)
           );
    fprintf(fp_out
           ,"coord 2  file=./%s filetype=binary skip=%d\n",out_filename
           ,points_num*sizeof(float)*2
           );
    fprintf(fp_out
           ,"coord 3  file=./%s filetype=binary skip=%d\n",out_filename
           ,points_num*sizeof(float)*3
           );
   
    fclose(fp_out);
    return(1);
}

---------------------------------------------------
ascii_to_binary.cをコンパイルして
axcii_to_binary.exe ascii.txtと実行すると
バイナリファイルとフィールドヘッダができます。






Comments

Name:
Mail Address:
Comments:
Password:

トラックバック一覧

<< 9/2010 >>

MONTUEWEDTHUFRISATSUN
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

このページへのアクセス数

Total:10PV

今日 : 2 graph
1日前 : 1 graph
2日前 : 1 graph
4日前 : 1 graph
7日前 : 2 graph
8日前 : 2 graph