Re: read binary data for integer and float types

From: Li, Yongzuo <yongzuo.li_at_nyahnyahspammersnyahnyah>
Date: Wed Jan 20 2010 - 09:40:27 MST

Dennis,

Thanks for your reply.

I use C language to prepare data for NCL.

The problem is that NCL does not know ii and jj before
 irec = fbinrecread("data.bin",0, ii*jj+2,"integer")

Here is the sample program

(1) write data

float *vr;
int ii,jj,*two;
FILE *out0 = fopen( "data.bin", "wb" );
       two = calloc(2,sizeof(int));
       two[0] = 360;
       two[1] = 900;
       jj = two[0];
       ii = two[1];

       vr = calloc(jj*ii, sizeof(float));

       vr[0] = 7.3;
       vr[899] = 9.5;

       fwrite(two,sizeof(int),2, out0);
       fwrite(vr, sizeof(float),jj*ii, out0);

       fclose(out0);
       free ((float *) vr);

exit(-1);
 
(2) read data
float *vr;
int ii,jj,*two;
FILE *out0 = fopen( "data.bin", "rb" );

       two = calloc(2,sizeof(int));
       fread(two,sizeof(int),2, out0);
       ii = two[0];
       jj = two[1];
       vr = calloc(ii*jj, sizeof(float));
       fread(vr, sizeof(float), ii*jj, out0);

       printf("%d %d\n",two[0],two[1]);
       printf("%f %f\n",vr[0],vr[899]);

       fclose(out0);
       free ((float *) vr);

exit(-1);

________________________________________
From: shea@ucar.edu [shea@ucar.edu]
Sent: Tuesday, January 19, 2010 8:03 PM
To: Li, Yongzuo
Cc: ncl-talk@ucar.edu
Subject: Re: read binary data for integer and float types

This is not clear.

Are you sure the file was created with "direct" writing?

Or, was it created with a fortran sequential write

    integer i1, i2
    real x(ii,jj)

    write(iunit) i1, i2, x

If the latter, then use fbinrecread

    irec = fbinrecread("data.bin",0, ii*jj+2,"integer")
    i1 = irec(0)
    i2 = irec(1)

    rec = fbinrecread("data.bin",0, ii*jj+2,"float")
    data = onedtond( rec(2:), (/jj,ii/) )

> Hi,
>
> I have following script to read binary data in integer/float type.
>
> two = new(2,"integer")
> ;two = fbindirread("data.bin",0,2,"integer")
> jj = 67
> ii = 43
> rf = new((/jj,ii/),"float")
> rf = fbindirread("data.bin",0,(/jj,ii/),"float")
>
> Question (1),
>
> Can fbindirread work without "(/jj,ii/)"? I tried and failed without it.
>
> Question (2),
>
> How can jj and ii be read from data.bin and then used to define jj and ii
> for rf = new(xxx) ?
>
> Either
> two = fbindirread("data.bin",0,2,"integer")
> or
> rf = fbindirread("data.bin",0,(/jj,ii/),"float")
> works well separately.
>
> But they do not work together (change 0 to 1 in rf line when both
> applied).
>
> Thanks
>
> Yongzuo
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Jan 20 09:40:40 2010

This archive was generated by hypermail 2.1.8 : Thu Jan 21 2010 - 13:54:45 MST