jpk1292000 has asked for the wisdom of the Perl Monks concerning the following question:
The data is meteorological data (temperature in degrees K) on a 614 x 428 grid. I tried coding up a reader for this, but am getting nonsensical results. Here is the code:*** First record (4 byte integer) - byte size of record (4*N) (f77 header) (4 byte float) .. value 1 (4 byte float) .. value 2 ... (4 byte float) .. value N N = number of grid points in the field (4 byte integer) .. byte size of record (4*N) (f77 trailer) **** Second record (4 byte integer) - byte size of record (4*N) (f77 header) (4 byte float) .. value 1 (4 byte float) .. value 2 ... (4 byte float) .. value N N = number of grid points in the field (4 byte integer) .. byte size of record (4*N) (f77 trailer)
The results I get when I print out the first field are non-sensical (negative numbers, etc). I think the issue is that I'm not properly setting up my template and record length. Also, how do I find out what is "the native format of the machine"?my $out_file = "/dicast2-papp/DICAST/smg_data/" . $gfn . ".bin"; #path + to binary file my $template = "if262792i"; #binary layout (integer 262792 floats in +teger) as described in the format documentation above (not sure if th +is is correct) my $record_length = 4; #not sure what record_length is supposed to rep +resent (number of values in 1st record, or should it be length of var +iable [4 bytes]) my (@fields,$record); open (FH, $out_files ) || die "couldn't open $out_files\n"; until (eof(FH)) { my $val_of_read = read (FH, $record, $record_length) == $record_ +length or die "short read\n"; @fields = unpack ($template, $record); print "field = $fields[0]\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reading binary files with Perl
by davorg (Chancellor) on Nov 16, 2006 at 15:53 UTC | |
|
Re: reading binary files with Perl
by ikegami (Patriarch) on Nov 16, 2006 at 16:04 UTC | |
by jpk1292000 (Initiate) on Nov 16, 2006 at 19:09 UTC | |
|
Re: reading binary files with Perl
by BrowserUk (Patriarch) on Nov 16, 2006 at 16:13 UTC | |
by ikegami (Patriarch) on Nov 16, 2006 at 16:29 UTC | |
by BrowserUk (Patriarch) on Nov 16, 2006 at 19:17 UTC | |
by ikegami (Patriarch) on Nov 16, 2006 at 21:12 UTC | |
|
Re: reading binary files with Perl
by jmcnamara (Monsignor) on Nov 16, 2006 at 16:33 UTC |