Reading file, FORTRAN 77 UNFORMATTED
Hello:
I have a "Fortran77 unformatted" data file which I have been trying to read this way:
PrintWriter _out = response.getWriter();
String file = application.getRealPath("/") +"p1/db/A/d2007081_bin/cim2045410501.bin";
FileInputStream fileinputstream =new FileInputStream(file);
int numberBytes = fileinputstream.available();
byte bytearray[] =newbyte[numberBytes];
fileinputstream.read(bytearray);
for(int i = 0; i < numberBytes; i++)
{
out.println(bytearray[i]);
}
fileinputstream.close();
The data, when formatted, has text lines like this:
1.59505e-0232.00412
2.73028e+023-3.16259e-031
-4.19709e-008-5.42768e+015
2.42313e-041 8.02358e-016
1.07617e+008 1.42820e+031
-1.59505e-023-2.00412
-2.73028e+023 3.16259e-031
4.19710e-008 5.42768e+015
(decimal numbers)
The output I am having is:
4
0
0
0
-40
3
0
0
4
0
0
0
96
15
0
0
52
-13
-117
67
-102
25
-116
67
0
64
...and so on...
The owner of these files told me that the contents inside are like this:
A B C D
...Where each letter represents a number and each character indicates the starting and closing of a line.
If any of you have faced this same problem, I will appreciate any help.
>for(int i = 0; i < numberBytes; i++)
>{
>out.println(bytearray);
/////////////////////////////look here. you have written println
> }
>
println prints every character on new line so instead of that use print.
might solve your problem.
Hmm, I'm not complete clear on your question but I'm going to take a stab in the dark... if I'm way off base just disregard.
Did you want you output to be 3 numbers wide like the following:
A B C
D E F
G H I
if so you could change your code to
for(int i = 0; i < numberBytes; i++)
{
out.print(bytearray[i] + " ");
i++;
out.print(bytearray[i] + " ");
i++;
out.print(bytearray[i] + " ");
}
I have a feeling though that you are not so much worried about the WAY it's outputting but the data that it's out putting. If so, could you clarify?
> I have a feeling though that you are not so much
> worried about the WAY it's outputting but the data
> that it's out putting. If so, could you clarify?
Oh yes, my problem is that the output of my Java program is not matching the output of the original program written in IDL (Interactive Data Language). IDL has a function to read files in FORTRAN77_UNFORMATTED, so in the code, the IDL program uses that function to read and then print the numbers...
So the output in IDL gives:
1.59505e-0232.00412
2.73028e+023-3.16259e-031
-4.19709e-008-5.42768e+015
2.42313e-041 8.02358e-016
1.07617e+008 1.42820e+031
-1.59505e-023-2.00412
-2.73028e+023 3.16259e-031
4.19710e-008 5.42768e+015
-
Ok, in this Web site I found a package that has an Object that deals with reading/writing in the format I need:
http://sourceforge.net/projects/fudaa/
> FortranBinaryInputStream
I'll check if that helps me..
..If it does not help me... I'll be posting again after the third cup of coffee..
> > for(int i = 0; i < numberBytes; i++)
>{
>out.print(bytearray[i] + " ");
>i++;
>out.print(bytearray[i] + " ");
>i++;
>out.print(bytearray[i] + " ");
>
>
>
That gives me:
4 0 0 0 -40 3 0 0 4 0 0 0 96 15 0 0 52 -13 -117 67 -102 25 -116 67 0 64 -116 67 103 102 -116 67 -51 -116 -116 67 52 -77 -116 67 -102 -39 -116
...and so on..
My goal is not to output the data, but in this phase I need to output the data to see if it is reading correctly.
--ty--
Oh ok, i understand now.. i meant to include the the last println a \n to break up the grouping. But that doesn't matter because your not getting the output that you want. Let me do some research on your question and I'll get back to you.
Do you think that it's because your storing the information in a bytearray? have you explored other options? I will look into some options for you. Sorry i can't give you a definitive answer but you got me interested :)
> Do you think that it's because your storing the
> information in a bytearray? have you explored other
> options? I will look into some options for you. Sorry
> i can't give you a definitive answer but you got me
> interested :)
I'll try now with this:
http://www.cetmef.equipement.gouv.fr/projets/transversaux/fudaa/devel/dodico/documents/lectureFichierFortran.html
Although it is in French they are using an example using a package that has class/object/methods to read unformatted files written in Fortran:
http://sourceforge.net/projects/fudaa
http://fudaa.sourceforge.net/
...I'll play a while with that package to see what output gives, after this cup of coffee.
.....any result I'll reply later... after another cup of coffee..
Does the file contain the floating point numbers as binary or text? Use a hex editor to find out. If they are text you can probably use java.text.NumberFormat to parse the values. If it is binary then you have two options:
1) If the numbers were written using IEEE 754 complient formats then java may be able to read them in natively (see java.nio.* for handling byte order issues).
2) They are written in a proprietry format. In this case you need to write code that parses that format manually. (or use a third party lib).
The first thing you should do is ask the person who created the file "what format are the numbers written in?". If (s)he cannot answer this then look at the specs for fortran 77 and see if there is a standard format used to store float numbers.
matfud
Message was edited by:
matfud
woohoo! I got the correct output!! ... No more coffee for today!!
In the case of my .BIN file, the code I used for debbugging is:
String file = application.getRealPath("/") + "p1/db/A/d2007081_bin/cim2045410501.bin";
FortranBinaryInputStream in= new FortranBinaryInputStream(new FileInputStream(file),false,"X86");
while(in.isFinFichier() == false && in.positionCourante() < 16)
{_out.println(in.readReal());}
It outputs the first A B C D numbers:
5.6E-45 1.379E-42 5.6E-45 5.516E-42
If I want to see the whole content then:
while(in.isFinFichier() == false)
{_out.println(in.readReal());}
There's documentation of these methods and this package here:http://docjar.com/docs/api/org/fudaa/dodico/fortran/NativeBinaryInputStream.html
http://docjar.com/docs/api/org/fudaa/dodico/fortran/FortranBinaryInputStream.html