Previous: Header Contents, Up: Image Formats


A.4 Sample Code

Here's some sample Fortran code which reads a header (which is similar to, if not exactly the same as, that used by the rectilinear gridding method), reads in the data, and prints it out. This code assumes that image_datum has the value ‘float

Here's the header definition (in C).

     typedef struct
     {
       size_t ni, nj;
       long i_min, j_min;
       double x0, y0;
       double x_sz, y_sz;
     } RectilinearImage;

The Fortran code:

     real*8 x0, y0, x_sz, y_sz
     real data(5)
     integer ni, nj, i_min, j_min, hdr_type
     
     open(unit=8, file='f.img', form='unformatted')
     
     read(8) hdr_type
     if (hdr_type .ne. 0) then
       write(*,*) "header type isn't 0, it's", hdr_type
       stop
     end if
     
     read(8) ni, nj, i_min, j_min, x0, y0, x_sz, y_sz
     write (*,*)  ni, nj, i_min, j_min, x0, y0, x_sz, y_sz
     
     if (nj .gt. 5) then
       write(*,*) "nj greater than built in limits"
       stop
     end if
     
     do i=1,ni
         read (8) (data(j), j=1,nj)
         write (*,*) data
     end do
     
     end