Silverfrost Forums

Welcome to our forums

Read binary file with Fortran

16 Jan 2015 5:09 #15312

Hello, I have a question about reading binary files with Fortran.

I have a file with logged GPS observations. File and format for first several data fields are attached:

https://yadi.sk/i/R5vTA80Je2CTg https://yadi.sk/d/cM9NiQaCe2CTN

Could anybody write several words about how to correctly open and start read this file? Unfortunately I have never dealt with binary. I'm not a programmer.

Thanks in advance!

[/img]

23 Jan 2015 11:18 #15421

The links you have included do not appear to work.

The effort required to read the data is relatively small once the file formats are fully understood. Some knowledge of the machine used to create the data is extremely helpful, as well as the floating point system used (if not one of the standards).

Also, it is not uncommon for some GPS devices to store the data in an arcane system (like only seconds of arc, not DD:MM:SS.SSSS or DDMMSS.ssss or decimal degrees, etc.).

Without the internal format, it is impossible to write the code!

24 Jan 2015 10:33 #15425

Are you sure the GPS output is binary ? I would expect it is text although the records may not be terminated as DOS format <cr><lf>.

I would open the file using access='TRANSPARENT' and review all characters in the file. The following gives some idea of an approach:

      open (unit   = 11,               &
            file   = file_name,        &
            status = 'OLD',            &
            form   = 'UNFORMATTED',    &
            access = 'TRANSPARENT',    &
            iostat = iostat)
      write (*,2000) 'Opening file ',trim(file_name),' iostat = ',iostat
2000  format (a,a,a,i0)
!
!  Read all characters and lines
!
      do
!
!      Get next character from file
         lastc = ic
         read (unit=11, iostat=iostat) c
          if (iostat /= 0) exit
!
         n  = n + one      ! number of characters read
         ic = ichar(c)
         count_ic(ic) = count_ic(ic) + one
!
!      respond to this character
         select case (ic)
!
            case (10)      ! <lf>  appears in both Unix and DOS format files
               num_lf = num_lf + one             ! line control characters ; 10:LF indicates end of line
!
!            report last line
               max_len  = max (max_len, line_len)
               call Write_line (line_content, line_index, num_line, line_len, next_char, max_lines, options)
!
               if (lastc /= 13) then
                  write (*,*) '<LF>', lastc, ic
                  probably_unix = probably_unix + 1
               else
                  probably_dos  = probably_dos  + 1
               end if
!
            case (13)       ! <CR>
               num_cr = num_cr + one             ! line control characters ; 13:CR indicates new line
!
            case (9)
               num_control = num_control + one   ! control characters
               line_content(next_char) = ' '
               if (next_char < max_file_size) next_char = next_char + 1
               line_len    = line_len  + 1
!
            case (0:8,11,12,14:31)
               num_control = num_control + one   ! control characters
               line_len    = line_len  + 1
!
            case (32:126)
               num_alfa = num_alfa + one         ! normal character
               line_content(next_char) = c
               if (next_char < max_file_size) next_char = next_char + 1
               line_len = line_len  + 1
!
            case (127:256)
               num_other = num_other + one       ! other character
!
            case default
               write (*,*) 'Unrecognised character : ichar =',ic
!
         end select
      end do
Please login to reply.