×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Fortran code to read unformatted binary files

Fortran code to read unformatted binary files

Fortran code to read unformatted binary files

(OP)
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

RE: Fortran code to read unformatted binary files

Could you post the errors you are getting.

RE: Fortran code to read unformatted binary files

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 

RE: Fortran code to read unformatted binary files

It seems that something is wrong with the data file fort.1.
The data are indeed binary:

CODE

$ head fort.1
@�1�@�#�@��@��@���@���@��y@�� ...
... 

RE: Fortran code to read unformatted binary files

I found here how to convert binary files from Big Endian into little endian
http://llllloooooo.blogspot.com/2017/12/convert-bi...

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 

RE: Fortran code to read unformatted binary files

(OP)
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.

RE: Fortran code to read unformatted binary files

Quote (Space_Fede)


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.


RE: Fortran code to read unformatted binary files

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 

RE: Fortran code to read unformatted binary files

(OP)
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!

RE: Fortran code to read unformatted binary files

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.

RE: Fortran code to read unformatted binary files

(OP)
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

RE: Fortran code to read unformatted binary files

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.

RE: Fortran code to read unformatted binary files

(OP)
You are a savior my man.
Thanks so much

RE: Fortran code to read unformatted binary files

Quote (Space_Fede )

You are a savior my man
roll1

RE: Fortran code to read unformatted binary files

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

RE: Fortran code to read unformatted binary files

(OP)
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 :)

RE: Fortran code to read unformatted binary files

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.

RE: Fortran code to read unformatted binary files

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 ?

RE: Fortran code to read unformatted binary files

@mikrom yes I did but no way to find these files anymore

RE: Fortran code to read unformatted binary files

@JamesONeil,
I still have the Fortran Source, but I have already deleted the 3 huge files.

RE: Fortran code to read unformatted binary files

@mikrom I still have the 3 huge binary data files so if you have the source one I will be glad ! Right now I am trying to compile it after removing the shell part but I have a lot of "unclassifiable statement error" and it seems it is because of the fixed format in Fortran. So if you still have your source I am interested, thanks !

RE: Fortran code to read unformatted binary files

@JamesONeil,
here is the extracted fortan code I mentioned above (I don't have the original with the shell part)

read_3files.f

CODE

c-----------------------------------------------------------------------
      program readmin1
      implicit real*8(a-h,o-z)
      parameter(exclud=9999.d0,
     $   dlatg=2.5d0/60.d0,dlong=2.5d0/60.d0,nrowsg=04320,ncolsg=08640)
         real*4 data(ncolsg)
      dimension sdat(ncolsg,2),stati(22)
c-----------------------------------------------------------------------
c
c   Read-in Gravity Anomaly 2.5x2.5 minute point value file.
c
      write(6,6001)
 6001 format(///15x,'Statistics of Gravity Anomaly Values  (mGal)',/)
      do i = 1, nrowsg
        read(1) (  data(j),j=1,ncolsg)
        rlat = 90.d0 - (i-1.d0)*dlatg - dlatg*0.5d0
        do j = 1, ncolsg
          clon = (j-1.d0)*dlong + dlong*0.5d0
c
c   Print a few values.
c
          if(i.eq.   1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
 6101       format(5x,2f15.10,f15.5)
          endif  !  top row
          sdat(j,1) = data(j)
          sdat(j,2) = 1.d0
        enddo  !  j
        call stats(90.d0,0.d0,i,dlatg,dlong,nrowsg,ncolsg,sdat,exclud,0,
     $             stati)
      enddo  !  i
c-----------------------------------------------------------------------
c
c   Read-in xi Deflection of Vertical 2.5x2.5 minute point value file.
c
      write(6,6002)
 6002 format(///15x,'Statistics of xi DOV Values  (arc-second)',/)
      do i = 1, nrowsg
        read(2) (  data(j),j=1,ncolsg)
        rlat = 90.d0 - (i-1.d0)*dlatg - dlatg*0.5d0
        do j = 1, ncolsg
          clon = (j-1.d0)*dlong + dlong*0.5d0
c
c   Print a few values.
c
          if(i.eq.   1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
          endif  !  top row
          sdat(j,1) = data(j)
          sdat(j,2) = 1.d0
        enddo  !  j
        call stats(90.d0,0.d0,i,dlatg,dlong,nrowsg,ncolsg,sdat,exclud,0,
     $             stati)
      enddo  !  i
c-----------------------------------------------------------------------
c
c   Read-in eta Deflection of Vertical 2.5x2.5 minute point value file.
c
      write(6,6003)
 6003 format(///15x,'Statistics of eta DOV Values  (arc-second)',/)
      do i = 1, nrowsg
        read(3) (  data(j),j=1,ncolsg)
        rlat = 90.d0 - (i-1.d0)*dlatg - dlatg*0.5d0
        do j = 1, ncolsg
          clon = (j-1.d0)*dlong + dlong*0.5d0
c
c   Print a few values.
c
          if(i.eq.   1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
          endif  !  top row
          sdat(j,1) = data(j)
          sdat(j,2) = 1.d0
        enddo  !  j
        call stats(90.d0,0.d0,i,dlatg,dlong,nrowsg,ncolsg,sdat,exclud,0,
     $             stati)
      enddo  !  i
c-----------------------------------------------------------------------
      stop
      end
C
C
C
      SUBROUTINE STATS(TOPLAT,WSTLON,I,GRDN,GRDE,NROWS,NCOLS,DATA,
     $                 EXCLUD,ISIG,STAT)
C-----------------------------------------------------------------------
      IMPLICIT REAL*8(A-H,O-Z)
      CHARACTER*20 DLABEL(14)
      CHARACTER*20 SLABEL( 8)
      DIMENSION DATA(NCOLS,2)
      DOUBLE PRECISION DEXCLUD,DG,SD,STAT(22)
      SAVE
      DATA PI/3.14159265358979323846D+00/
      DATA DLABEL/'    Number of Values','  Percentage of Area',
     $            '       Minimum Value',' Latitude of Minimum',
     $            'Longitude of Minimum','       Maximum Value',
     $            ' Latitude of Maximum','Longitude of Maximum',
     $            '     Arithmetic Mean','  Area-Weighted Mean',
     $            '      Arithmetic RMS','   Area-Weighted RMS',
     $            '   Arithmetic S.Dev.','Area-Weighted S.Dev.'/
      DATA SLABEL/'       Minimum Sigma',' Latitude of Minimum',
     $            'Longitude of Minimum','       Maximum Sigma',
     $            ' Latitude of Maximum','Longitude of Maximum',
     $            'Arithmetic RMS Sigma','Area-wghtd RMS Sigma'/
C-----------------------------------------------------------------------
      IF(I.EQ.1) THEN
      DEXCLUD=EXCLUD
      DTR=PI/180.D0
      FOURPI=4.D0*PI
      DPR=GRDN*DTR
      DLR=GRDE*DTR
      CAREA=2.D0*DLR*SIN(DPR/2.D0)
      DO 10 K=1,22
      STAT(K)=0.D0
   10 CONTINUE
      STAT( 3)= DEXCLUD
      STAT( 6)=-DEXCLUD
      STAT(15)= DEXCLUD
      STAT(18)= 0.D0
      ENDIF
C-----------------------------------------------------------------------
      DLAT=TOPLAT-(I-1.D0)*GRDN-GRDN/2.D0
      COLATC=(90.D0-DLAT)*DTR
      AREA=CAREA*SIN(COLATC)
C-----------------------------------------------------------------------
      DO 110 J=1,NCOLS
      DLON=WSTLON+(J-1.D0)*GRDE+GRDE/2.D0
      DG=DATA(J,1)
      SD=DATA(J,2)
      IF(DG.LT.DEXCLUD) THEN
C-----------------------------------------------------------------------
      STAT( 1)=STAT( 1)+1.D0
      STAT( 2)=STAT( 2)+AREA
      IF(DG.LE.STAT( 3)) THEN
      STAT( 3)=DG
      STAT( 4)=DLAT
      STAT( 5)=DLON
      ENDIF
      IF(DG.GE.STAT( 6)) THEN
      STAT( 6)=DG
      STAT( 7)=DLAT
      STAT( 8)=DLON
      ENDIF
      STAT( 9)=STAT( 9)+DG
      STAT(10)=STAT(10)+DG*AREA
      STAT(11)=STAT(11)+DG**2
      STAT(12)=STAT(12)+DG**2*AREA
      IF(SD.LE.STAT(15)) THEN
      STAT(15)=SD
      STAT(16)=DLAT
      STAT(17)=DLON
      ENDIF
      IF(SD.GE.STAT(18)) THEN
      STAT(18)=SD
      STAT(19)=DLAT
      STAT(20)=DLON
      ENDIF
      STAT(21)=STAT(21)+SD**2
      STAT(22)=STAT(22)+SD**2*AREA
C-----------------------------------------------------------------------
      ENDIF
  110 CONTINUE
C-----------------------------------------------------------------------
      IF(I.NE.NROWS) RETURN
      IF(STAT(1).GT.0.D0) THEN
      STAT( 9)=STAT( 9)/STAT( 1)
      STAT(10)=STAT(10)/STAT( 2)
      STAT(11)=SQRT(STAT(11)/STAT( 1))
      STAT(12)=SQRT(STAT(12)/STAT( 2))
      STAT(13)=SQRT(STAT(11)**2-STAT( 9)**2)
      STAT(14)=SQRT(STAT(12)**2-STAT(10)**2)
      STAT(21)=SQRT(STAT(21)/STAT( 1))
      STAT(22)=SQRT(STAT(22)/STAT( 2))
      STAT( 2)=STAT( 2)/FOURPI*100.D0
      ELSE
      DO 120 J=3,22
      STAT(J)=DEXCLUD
  120 CONTINUE
      ENDIF
C=======================================================================
      NUM=INT(STAT(1))
      WRITE(6,6001) DLABEL(1),NUM
 6001 FORMAT(/5X,A20,3X,I11)
      DO 210 K=2,14
      WRITE(6,6002) DLABEL(K),STAT(K)
 6002 FORMAT(5X,A20,3X,F15.3)
  210 CONTINUE
      WRITE(6,6003)
 6003 FORMAT(' ')
      IF(ISIG.EQ.1) THEN
      DO 220 K=1,8
      WRITE(6,6002) SLABEL(K),STAT(K+14)
  220 CONTINUE
      ENDIF
C=======================================================================
      RETURN
      END 

RE: Fortran code to read unformatted binary files

Hello, so I created a file containing your source and renamed it .f90 because I had some problems when compiling it that may come from a "fixed format". Anyway I tried to correct what I could but I don't understand these errors :

CODE -->

gfortran read_3files.f90 -o readmin1
read_3files.f90:6:0:

    6 | c-----------------------------------------------------------------------
      |
Error: Unclassifiable statement at (1)
read_3files.f90:88:64:

   88 |       DATA DLABEL/'    Number of Values','  Percentage of Area',
      |                                                                1
Error: Syntax error in DATA statement at (1)
read_3files.f90:89:6:

   89 |      $            '       Minimum Value',' Latitude of Minimum',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:90:6:

   90 |      $            'Longitude of Minimum','       Maximum Value',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:91:6:

   91 |      $            ' Latitude of Maximum','Longitude of Maximum',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:92:6:

   92 |      $            '     Arithmetic Mean','  Area-Weighted Mean',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:93:6:

   93 |      $            '      Arithmetic RMS','   Area-Weighted RMS',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:94:6:

   94 |      $            '   Arithmetic S.Dev.','Area-Weighted S.Dev.'/
      |      1
Error: Invalid character in name at (1)
read_3files.f90:95:64:

   95 |       DATA SLABEL/'       Minimum Sigma',' Latitude of Minimum',
      |                                                                1
Error: Syntax error in DATA statement at (1)
read_3files.f90:96:6:

   96 |      $            'Longitude of Minimum','       Maximum Sigma',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:97:6:

   97 |      $            ' Latitude of Maximum','Longitude of Maximum',
      |      1
Error: Invalid character in name at (1)
read_3files.f90:98:6:

   98 |      $            'Arithmetic RMS Sigma','Area-wghtd RMS Sigma'/
      |      1
Error: Invalid character in name at (1)
read_3files.f90:174:3:

  174 | C=======================================================================
      |   1
Error: Invalid character in name at (1)
read_3files.f90:189:3:

  189 | C=======================================================================
      |   1
Error: Invalid character in name at (1) 
Thank you for your help

RE: Fortran code to read unformatted binary files

It's not fortran 90 so you should not use the extension .f90
It's fortran 77, so if you are using gfortran compiler then use the extension .f like me - I used the file name read_3files.f
I compiled it with the command

CODE

$ gfortran read_3files.f -o readmin1 

RE: Fortran code to read unformatted binary files

Ok so I created a new file .f and did exactly this command :

CODE -->

gfortran read_3files.f -o readmin1
read_3files.f:1:1:

    1 | program readmin1
      | 1
Error: Non-numeric character in statement label at (1) 
I am using notepad++ to visualize the file so I don't know if there could be a problem with indentation or line but it doesn't seem that it is the problem...

RE: Fortran code to read unformatted binary files

As I posted the source the formatter indented the keyword program on the first column. But it must be in the 7-th column like the keyword implicit. Look again at the source I posted above, now I inserted a comment line on the first line and it should be now ok.

RE: Fortran code to read unformatted binary files

Ok great thanks a lot @mikrom now it compiles !
@Space_Fede do you still have the 3 files :
Dg01_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh
xi_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh
eta_cnt2.5x2.5_EGM08_to2190_WGS84_ell_nh
because I thought I had them but turns out not and because the link doesn't work anymore and it is not available on NGA anymore it is kind of a pity ^^' But I really need them for my research though...

RE: Fortran code to read unformatted binary files

(OP)
Hi James!
I have them! They are already converted in Little Endian. Unfortunately the files are too large to be attached in the post.
Here it is a we transfer link:
https://we.tl/t-dQTFHbHW6x

RE: Fortran code to read unformatted binary files

Great ! Thanks @Space_Fede ! So now I can compile and get the results :) My LAST question and after I will stop bothering both of you ^^ So this program is compiling only a few values of the files we have and of course it is our choice to add more. So I guess that the values nrowsg and ncolsg have to be changed since they are used in the loops. In fact maybe only nrowsg because ncolsg is not supposed to be more than it is. My question is to what number format corresponds nrowsg =04320 ?
I just want to know how to modify the code in order for example to get values from a window of latitudes and longitudes. From the code given you get only 19 values with latitude 89.979... and different longitudes starting at 0.0208...

CODE --> fortran

program readmin1
      implicit real*8(a-h,o-z)
      parameter(exclud=9999.d0,
     $   dlatg=2.5d0/60.d0,dlong=2.5d0/60.d0,nrowsg=04320,ncolsg=08640)
         real*4 data(ncolsg)
      dimension sdat(ncolsg,2),stati(22)
c-----------------------------------------------------------------------
c
c   Read-in Gravity Anomaly 2.5x2.5 minute point value file.
c
      write(6,6001)
 6001 format(///15x,'Statistics of Gravity Anomaly Values  (mGal)',/)
      do i = 1, nrowsg
        read(1) (  data(j),j=1,ncolsg)
        rlat = 90.d0 - (i-1.d0)*dlatg - dlatg*0.5d0
        do j = 1, ncolsg
          clon = (j-1.d0)*dlong + dlong*0.5d0
c
c   Print a few values.
c
          if(i.eq.   1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
 6101       format(5x,2f15.10,f15.5)
          endif  !  top row
          sdat(j,1) = data(j)
          sdat(j,2) = 1.d0
        enddo  !  j
        call stats(90.d0,0.d0,i,dlatg,dlong,nrowsg,ncolsg,sdat,exclud,0,
     $             stati)
      enddo  !  i 
Thanks a lot for your time

RE: Fortran code to read unformatted binary files

Quote (JamesONeil)


From the code given you get only 19 values with latitude 89.979... and different longitudes starting at 0.0208...
Yes, it's because, they want to print only few values that's when i=1 and j is less than 20, i.e.
1 <= j < 20. i is row index and j is column index, so they print only first 19 columns from first row.

CODE

c
c   Print a few values.
c
          if(i.eq.1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
 6101       format(5x,2f15.10,f15.5)
          endif  !  top row 
If you comment out or delete the IF with the ENDIF, then you will get all values.

RE: Fortran code to read unformatted binary files

Here is the minimal program to read all values from the first file
read_data.f

CODE

c-----------------------------------------------------------------------
      program read_data
      implicit real*8(a-h,o-z)
      parameter(exclud=9999.d0,
     $   dlatg=2.5d0/60.d0,dlong=2.5d0/60.d0,nrowsg=04320,ncolsg=08640)
         real*4 data(ncolsg)
      dimension sdat(ncolsg,2),stati(22)
c-----------------------------------------------------------------------
c
c   Read-in Gravity Anomaly 2.5x2.5 minute point value file.
c
      write(6,6001)
 6001 format(15x,'Statistics of Gravity Anomaly Values  (mGal)')
      do i = 1, nrowsg
        read(1) (  data(j),j=1,ncolsg)
        rlat = 90.d0 - (i-1.d0)*dlatg - dlatg*0.5d0
        do j = 1, ncolsg
          clon = (j-1.d0)*dlong + dlong*0.5d0
c
c   Print a few values.
c
c         if(i.eq.1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
 6101       format(5x,2f15.10,f15.5)
c         endif  !  top row
          sdat(j,1) = data(j)
          sdat(j,2) = 1.d0
        enddo  !  j
c       call stats(90.d0,0.d0,i,dlatg,dlong,nrowsg,ncolsg,sdat,exclud,0,
c    $             stati)
      enddo  !  i
C=======================================================================
      RETURN
      END 
Compile it with

CODE

gfortran read_data.f -o read_data 
and run it redirecting the output into a text file

CODE

./read_data > data.txt 
Now you can observe all data. But the text file is very huge, it has 1.9 GB and 4320*8640 = 37324800 lines.
I'm looking only at begin and end of the file:

CODE

$ head data.txt
               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

$ tail data.txt
      -89.9791666667 359.6041666667      -32.69642
      -89.9791666667 359.6458333333      -32.69638
      -89.9791666667 359.6875000000      -32.69634
      -89.9791666667 359.7291666667      -32.69630
      -89.9791666667 359.7708333333      -32.69625
      -89.9791666667 359.8125000000      -32.69622
      -89.9791666667 359.8541666667      -32.69617
      -89.9791666667 359.8958333333      -32.69613
      -89.9791666667 359.9375000000      -32.69609
      -89.9791666667 359.9791666667      -32.69605 

RE: Fortran code to read unformatted binary files

Quote (JamesONeil)


I just want to know how to modify the code in order for example to get values from a window of latitudes and longitudes.
Then modify this condition, from which rows and columns you want to get the data:

CODE

c
c   Print a few values.
c
          if(i.eq.1.and.j.lt.20) then  !  top row
            write(6,6101) rlat,clon,data(j)
 6101       format(5x,2f15.10,f15.5)
          endif  !  top row 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close