Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran code to read unformatted binary files 1

Status
Not open for further replies.

Space_Fede

Technical User
Mar 3, 2021
9
0
0
IT
Hello everyone,
I am trying to study the data of the gravity anomaly provided by the National Geospacial-Intelligence Agency.

In the following link you can find the Fortran code that should read the binary files in which the data I am interested in, are stored.
Link

When I try to compile the code, several errors appear. I am a Fortran newbie so any help would be appreciated.

For the record, I tried to compile it both with gfortran (in wsl) and cgywin.

Thanks in advance
 
Could you post the errors you are getting.
 
I tried it.
I downloaded the 3 big data files and created from them symlinks fort.1, fort.2, fort.3
Then I extracted the fortran source from the shell script, compiled it with gfortran and when I executed it I got this runtime error:
Code:
$ ln -s Dg01_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.1
$ ln -s xi_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.2
$ ln -s eta_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.3
$ gfortran read_3files.f -o readmin1
$ ./readmin1



               Statistics of Gravity Anomaly Values  (mGal)

       89.9791666667   0.0208333333       -0.00000
       89.9791666667   0.0625000000***************
       89.9791666667   0.1041666667***************
       89.9791666667   0.1458333333        0.00000
       89.9791666667   0.1875000000        0.00000
       89.9791666667   0.2291666667***************
       89.9791666667   0.2708333333***************
       89.9791666667   0.3125000000       -0.03656
       89.9791666667   0.3541666667       -0.00000
       89.9791666667   0.3958333333       -0.00000
       89.9791666667   0.4375000000       -0.00000
       89.9791666667   0.4791666667***************
       89.9791666667   0.5208333333***************
       89.9791666667   0.5625000000***************
       89.9791666667   0.6041666667***************
       89.9791666667   0.6458333333***************
       89.9791666667   0.6875000000***************
       89.9791666667   0.7291666667***************
       89.9791666667   0.7708333333***************
At line 14 of file read_3files.f (unit = 1, file = 'fort.1')
Fortran runtime error: End of file
 
It seems that something is wrong with the data file fort.1.
The data are indeed binary:
Code:
$ head fort.1
@�1�@�#�@��@��@���@���@��y@�� ...
...
 
Here is why this is not working:
These data files were computed on a SUN computer, which uses a BIG ENDIAN internal binary representation.
According to this is my system little endian
Code:
$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6
1
 
I found here how to convert binary files from Big Endian into little endian

So I have done it (and replaced symlinks):

Code:
$ xxd -e -g4 Dg01_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh temp.txt
$ xxd -r temp.txt Dg_little_endian
$ ln -s -f Dg_little_endian fort.1

$ xxd -e -g4 xi_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh temp.txt
$ xxd -r temp.txt xi_little_endian
$ ln -s -f xi_little_endian fort.2

$ xxd -e -g4 eta_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh temp.txt
$ xxd -r temp.txt eta_little_endian
$ ln -s -f eta_little_endian fort.3

Now the program runs and computes the statistics:

Code:
$ ./readmin1



               Statistics of Gravity Anomaly Values  (mGal)

       89.9791666667   0.0208333333        6.07722
       89.9791666667   0.0625000000        6.07550
       89.9791666667   0.1041666667        6.07377
       89.9791666667   0.1458333333        6.07204
       89.9791666667   0.1875000000        6.07032
       89.9791666667   0.2291666667        6.06860
       89.9791666667   0.2708333333        6.06687
       89.9791666667   0.3125000000        6.06515
       89.9791666667   0.3541666667        6.06343
       89.9791666667   0.3958333333        6.06172
       89.9791666667   0.4375000000        6.06000
       89.9791666667   0.4791666667        6.05829
       89.9791666667   0.5208333333        6.05657
       89.9791666667   0.5625000000        6.05486
       89.9791666667   0.6041666667        6.05315
       89.9791666667   0.6458333333        6.05144
       89.9791666667   0.6875000000        6.04973
       89.9791666667   0.7291666667        6.04802
       89.9791666667   0.7708333333        6.04631

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value          -385.543
      Latitude of Minimum            35.896
     Longitude of Minimum            74.771
            Maximum Value           966.334
      Latitude of Maximum            10.854
     Longitude of Maximum           286.271
          Arithmetic Mean            -0.471
       Area-Weighted Mean            -0.000
           Arithmetic RMS            33.379
        Area-Weighted RMS            35.028
        Arithmetic S.Dev.            33.376
     Area-Weighted S.Dev.            35.028
 



               Statistics of xi DOV Values  (arc-second)

       89.9791666667   0.0208333333        3.09995
       89.9791666667   0.0625000000        3.09938
       89.9791666667   0.1041666667        3.09881
       89.9791666667   0.1458333333        3.09824
       89.9791666667   0.1875000000        3.09766
       89.9791666667   0.2291666667        3.09709
       89.9791666667   0.2708333333        3.09652
       89.9791666667   0.3125000000        3.09594
       89.9791666667   0.3541666667        3.09537
       89.9791666667   0.3958333333        3.09479
       89.9791666667   0.4375000000        3.09422
       89.9791666667   0.4791666667        3.09364
       89.9791666667   0.5208333333        3.09306
       89.9791666667   0.5625000000        3.09248
       89.9791666667   0.6041666667        3.09190
       89.9791666667   0.6458333333        3.09132
       89.9791666667   0.6875000000        3.09074
       89.9791666667   0.7291666667        3.09016
       89.9791666667   0.7708333333        3.08958

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value          -122.315
      Latitude of Minimum            28.479
     Longitude of Minimum            84.062
            Maximum Value           104.391
      Latitude of Maximum            10.938
     Longitude of Maximum           286.271
          Arithmetic Mean            -0.453
       Area-Weighted Mean            -0.175
           Arithmetic RMS             5.417
        Area-Weighted RMS             5.640
        Arithmetic S.Dev.             5.398
     Area-Weighted S.Dev.             5.637
 



               Statistics of eta DOV Values  (arc-second)

       89.9791666667   0.0208333333        0.65138
       89.9791666667   0.0625000000        0.65319
       89.9791666667   0.1041666667        0.65501
       89.9791666667   0.1458333333        0.65682
       89.9791666667   0.1875000000        0.65864
       89.9791666667   0.2291666667        0.66045
       89.9791666667   0.2708333333        0.66227
       89.9791666667   0.3125000000        0.66408
       89.9791666667   0.3541666667        0.66589
       89.9791666667   0.3958333333        0.66770
       89.9791666667   0.4375000000        0.66951
       89.9791666667   0.4791666667        0.67132
       89.9791666667   0.5208333333        0.67313
       89.9791666667   0.5625000000        0.67494
       89.9791666667   0.6041666667        0.67675
       89.9791666667   0.6458333333        0.67855
       89.9791666667   0.6875000000        0.68036
       89.9791666667   0.7291666667        0.68216
       89.9791666667   0.7708333333        0.68397

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value           -88.282
      Latitude of Minimum            28.646
     Longitude of Minimum            83.729
            Maximum Value            91.924
      Latitude of Maximum            28.021
     Longitude of Maximum            92.771
          Arithmetic Mean             0.000
       Area-Weighted Mean             0.000
           Arithmetic RMS             5.503
        Area-Weighted RMS             5.709
        Arithmetic S.Dev.             5.503
     Area-Weighted S.Dev.             5.709
 
Thank you very much @mikrom. Your help was really eye-opening.
In you're opinion, is there a way to manage these files with a C code?
However, thank you again.
 
Space_Fede said:
is there a way to manage these files with a C code?
Why not, everything is possible.
The fortran program logic seems not to be complicated. In my opinion, the really bad thing here is just that the data is in binary format. In this format the data file is like a black box for me. I would clearly prefer text file if it was possible. So if you manage how to read the binary data in C, then the rest should not be difficult.
If you do it in C successful, you can then post your solution here - it would be interesting to compare the Fortran with C solution.
If you would have some difficulties in your experiments with C, you are welcome to ask questions in our C forum.


 
Just for curiosity, I tried it today on my Big Endian machine IBM i.
It runs on it like a charm without any additional steps.
Code:
> gfortran read_3files.f -o readmin1
> ln -s Dg01_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.1
> ln -s xi_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.2
> ln -s eta_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh fort.3
> ./readmin1



               Statistics of Gravity Anomaly Values  (mGal)

       89.9791666667   0.0208333333        6.07722
       89.9791666667   0.0625000000        6.07550
       89.9791666667   0.1041666667        6.07377
       89.9791666667   0.1458333333        6.07204
       89.9791666667   0.1875000000        6.07032
       89.9791666667   0.2291666667        6.06860
       89.9791666667   0.2708333333        6.06687
       89.9791666667   0.3125000000        6.06515
       89.9791666667   0.3541666667        6.06343
       89.9791666667   0.3958333333        6.06172
       89.9791666667   0.4375000000        6.06000
       89.9791666667   0.4791666667        6.05829
       89.9791666667   0.5208333333        6.05657
       89.9791666667   0.5625000000        6.05486
       89.9791666667   0.6041666667        6.05315
       89.9791666667   0.6458333333        6.05144
       89.9791666667   0.6875000000        6.04973
       89.9791666667   0.7291666667        6.04802
       89.9791666667   0.7708333333        6.04631

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value          -385.543
      Latitude of Minimum            35.896
     Longitude of Minimum            74.771
            Maximum Value           966.334
      Latitude of Maximum            10.854
     Longitude of Maximum           286.271
          Arithmetic Mean            -0.471
       Area-Weighted Mean            -0.000
           Arithmetic RMS            33.379
        Area-Weighted RMS            35.028
        Arithmetic S.Dev.            33.376
     Area-Weighted S.Dev.            35.028




               Statistics of xi DOV Values  (arc-second)

       89.9791666667   0.0208333333        3.09995
       89.9791666667   0.0625000000        3.09938
       89.9791666667   0.1041666667        3.09881
       89.9791666667   0.1458333333        3.09824
       89.9791666667   0.1875000000        3.09766
       89.9791666667   0.2291666667        3.09709
       89.9791666667   0.2708333333        3.09652
       89.9791666667   0.3125000000        3.09594
       89.9791666667   0.3541666667        3.09537
       89.9791666667   0.3958333333        3.09479
       89.9791666667   0.4375000000        3.09422
       89.9791666667   0.4791666667        3.09364
       89.9791666667   0.5208333333        3.09306
       89.9791666667   0.5625000000        3.09248
       89.9791666667   0.6041666667        3.09190
       89.9791666667   0.6458333333        3.09132
       89.9791666667   0.6875000000        3.09074
       89.9791666667   0.7291666667        3.09016
       89.9791666667   0.7708333333        3.08958

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value          -122.315
      Latitude of Minimum            28.479
     Longitude of Minimum            84.062
            Maximum Value           104.391
      Latitude of Maximum            10.938
     Longitude of Maximum           286.271
          Arithmetic Mean            -0.453
       Area-Weighted Mean            -0.175
           Arithmetic RMS             5.417
        Area-Weighted RMS             5.640
        Arithmetic S.Dev.             5.398
     Area-Weighted S.Dev.             5.637




               Statistics of eta DOV Values  (arc-second)

       89.9791666667   0.0208333333        0.65138
       89.9791666667   0.0625000000        0.65319
       89.9791666667   0.1041666667        0.65501
       89.9791666667   0.1458333333        0.65682
       89.9791666667   0.1875000000        0.65864
       89.9791666667   0.2291666667        0.66045
       89.9791666667   0.2708333333        0.66227
       89.9791666667   0.3125000000        0.66408
       89.9791666667   0.3541666667        0.66589
       89.9791666667   0.3958333333        0.66770
       89.9791666667   0.4375000000        0.66951
       89.9791666667   0.4791666667        0.67132
       89.9791666667   0.5208333333        0.67313
       89.9791666667   0.5625000000        0.67494
       89.9791666667   0.6041666667        0.67675
       89.9791666667   0.6458333333        0.67855
       89.9791666667   0.6875000000        0.68036
       89.9791666667   0.7291666667        0.68216
       89.9791666667   0.7708333333        0.68397

         Number of Values      37324800
       Percentage of Area           100.000
            Minimum Value           -88.282
      Latitude of Minimum            28.646
     Longitude of Minimum            83.729
            Maximum Value            91.924
      Latitude of Maximum            28.021
     Longitude of Maximum            92.771
          Arithmetic Mean             0.000
       Area-Weighted Mean             0.000
           Arithmetic RMS             5.503
        Area-Weighted RMS             5.709
        Arithmetic S.Dev.             5.503
     Area-Weighted S.Dev.             5.709
 
Cool!
My problem actually was a lot "simpler". The compiler didn't like the shell part and since I have never used Fortran in my life I was stucked with the very first line of code! [dazed]
However, I've learnt some basics thank to your help, thank you!

 
I tried it by hand, without the shell script, because I wanted to try it out step by step in case an error occurs anywhere.
Now when you understand how it works, you can modify the shell script too. No need for korn shell it should run with bash too. Just replace f90 with gfortran, the names of the directories and maybe something more.
 
Yes I've done some modifications and now it works.
I am now addressing the .txt writing issue. I think that the best way is to write the .txt directly from the fortran code. The problem is that the the data are REAL*4, so the format of the write function should be the same. However, since the REAL*4 data can be read only by Fortran, the .txt that I write is useless aswell.
Any hints on how I can change the format of the data and then write them on a .txt?
Thanks
 
if you want a text file from the program output, I think that the simplest way is redirecting the output from the program into a text file like:
Code:
./readmin1 > statistics_of_gravity.txt
you can modify your shell script in that way.
 
Hello,
I am also new to Fortran and I wanted to know what did you do mikrom when you said "Then I extracted the fortran source from the shell script, compiled it with gfortran". The fortran script that is given has a shell header and after the program readmin1. Did you delete all the shell description and rename the file in read_3files.f to be able to compile it ?

Thank you for your help
 
Hi James!
From newbie to newbie...I've just commented out the lines of code with #, then I've compiled the fortran code.
Before executing the output you need to "reassign" the fort.1,.2,.3 files as mikrom explained in a previous answer.
I don't know if there is a more sophisticated way to do it, but it definitely worked for me :)
 
I do not know anymore, either I copied only the fortran code from the mixed shell-fortran source file out, or I deleted the shell code. It doesn't matter how you do it.
 
Ok thank you Space_Fede I will try it ! By the way the link provided to get the files from NGA is not working for me so I don't know if they were moved since your last discussion. Do you know where could I download them again ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top