<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: reading derived types from a file in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372422#M160781</link>
    <description>&lt;P&gt;Another solution is to read the 14 bytes into a character string of 14 long and transfer the various pieces explicitly. Then you have all control and you put the values into a structure that is convenient for computing. Reverse the process to write the header to file.&lt;/P&gt;</description>
    <pubDate>Mon, 28 Mar 2022 13:56:30 GMT</pubDate>
    <dc:creator>Arjen_Markus</dc:creator>
    <dc:date>2022-03-28T13:56:30Z</dc:date>
    <item>
      <title>reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372290#M160774</link>
      <description>&lt;P&gt;I want to create a neat way to read in stadrad file types,&amp;nbsp; like .bmp in particular right now.&lt;/P&gt;
&lt;P&gt;Such files are binary and havbe a defined format including headers etc.&lt;/P&gt;
&lt;P&gt;So I the idea to cerate a derived&amp;nbsp; type that exactly mirrored the file structure, or at least its fixed size parts like the header.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, the header of a .bmp file is as mirrored by this derived&amp;nbsp; type:&lt;/P&gt;
&lt;P&gt;TYPE BMPFILEHEADER&lt;BR /&gt;SEQUENCE&lt;BR /&gt;CHARACTER*2 SIGNATURE&lt;BR /&gt;INTEGER*4 FILESIZE&lt;BR /&gt;BYTE RESERVED(4)&lt;BR /&gt;INTEGER*4 DATAOFFSET&lt;BR /&gt;END TYPE BMPFILEHEADER&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I first tried to compile it I got the error " structure contains one or more misaligned data types.&amp;nbsp; Well, yes, I can see that: The first 2-buye entry shoves the 4-bute entries following to straddle two 32-bit entities.&lt;/P&gt;
&lt;P&gt;So I used the compile option "allow padding to align structures". Now it comples fine.&lt;/P&gt;
&lt;P&gt;I don't know what exactly padding does, but I assume it merely locates the first 4-byte integer "filessize" on a 4-byte boundary by inserting two dummy byts after the 2-byte filed "SIGNATURE"&lt;/P&gt;
&lt;P&gt;Now if I define&lt;/P&gt;
&lt;P&gt;TYPE (BMPFILEHEADER)::HEADER&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;OPEN(9,FILE="SHAPE.BMP",ACCESS="SEQUENTIAL",FORM="BINARY",STATUS="OLD")&lt;BR /&gt;&amp;nbsp;READ(9)HEADER&lt;BR /&gt;WRITE(*,*)HEADER%SIGNATURE&lt;BR /&gt;WRITE(*,*)HEADER%FILESIZE&lt;/P&gt;
&lt;P&gt;I expect to see&lt;/P&gt;
&lt;P&gt;BM&lt;/P&gt;
&lt;P&gt;the file szie&lt;/P&gt;
&lt;P&gt;In fact I get&lt;/P&gt;
&lt;P&gt;BM&lt;/P&gt;
&lt;P&gt;15&lt;/P&gt;
&lt;P&gt;Now if instead I do:&lt;/P&gt;
&lt;P&gt;READ(9)HEADER%SIGNATURE&lt;BR /&gt;READ(9)HEADER%FILESIZE&lt;/P&gt;
&lt;P&gt;WRITE(*,*)HEADER%SIGNATURE&lt;BR /&gt;WRITE(*,*)HEADER%FILESIZE&lt;/P&gt;
&lt;P&gt;I get&lt;/P&gt;
&lt;P&gt;BM&lt;BR /&gt;1045030&lt;/P&gt;
&lt;P&gt;which is correct. The number is 000FF226 in hex&lt;/P&gt;
&lt;P&gt;What this shows me is that read operation is not reading the variables as defined, but is also reading the invisible padding that was added for aligment and so getting 000F instead of the correct number.&lt;/P&gt;
&lt;P&gt;I think this is an implemenation error, is it not? The read statement should only be reading as many bytes as declared variables have, and ignoring any padding added for processor hardware convenience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul Dent&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 01:48:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372290#M160774</guid>
      <dc:creator>Paul_Dent</dc:creator>
      <dc:date>2022-03-28T01:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372307#M160775</link>
      <description>&lt;P&gt;I get a warning and not an error for the first type. The results when run match what the signature and file size should be. There are several discussions about this like&amp;nbsp;&lt;A href="https://community.intel.com/t5/Intel-Fortran-Compiler/warning-6379-The-structure-contains-one-or-more-misaligned/m-p/1187974" target="_blank" rel="noopener"&gt;Solved: warning #6379: The structure contains one or more misaligned fields. - Intel Communities&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;My take is that the type for io is a type that reflects the bytes on disk, and if one uses the type in computation intensively, then converting the disk type to a computationally aligned type may help in speed, but if saved back to disk, then a conversion back to disk type would have to be done.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 04:03:41 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372307#M160775</guid>
      <dc:creator>MWind2</dc:creator>
      <dc:date>2022-03-28T04:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372415#M160779</link>
      <description>&lt;P&gt;1&amp;gt;&amp;gt;&lt;EM&gt;Such files are binary and havbe a defined format including headers etc.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;2&amp;gt;&amp;gt;&lt;SPAN&gt;&lt;EM&gt;I got the error " structure contains one or more misaligned data types.&lt;/EM&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1 == you are required to use the defined format&lt;/P&gt;
&lt;P&gt;2 == This should have been a warning and not an error.&lt;/P&gt;
&lt;P&gt;Other than for performance reasons, misaligned data should not be in error. Running with a warning is ok.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 13:23:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372415#M160779</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2022-03-28T13:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372420#M160780</link>
      <description>&lt;P&gt;The problem here is that the BMP header is 14 bytes - allowing padding means that your type won't match the file format. And, yes, the misalignment warning is just a warning (unless you've enabled turning warnings into errors). A more annoying problem is that the standard does not guarantee a lack of padding in SEQUENCE types - only that the order of components won't change. Some compilers will pack by default, others won't.&lt;/P&gt;
&lt;P&gt;One might then try an interoperable type, but here the compiler will insert padding after the two-character header if the "companion C processor" would do so - which it will (in the absence of #pragma pack). There's no standard Fortran way to get the packing unless you did something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="fortran"&gt;use, intrinsic :: iso_c_binding
type, bind(C) :: BMPFILEHEADER
character :: SIGNATURE(2)
integer(C_INT8_T) :: FILESIZE8(4)
integer(C_INT8_T) :: RESERVED(4)
integer(C_INT8_T) :: DATAOFFSET8(4)
end type BMPFILEHEADER&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then used TRANSFER to convert between the arrays of 8-bit integers and a 32-bit integer. A bit clumsy but you'd have to do this only once.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 13:46:24 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372420#M160780</guid>
      <dc:creator>Steve_Lionel</dc:creator>
      <dc:date>2022-03-28T13:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372422#M160781</link>
      <description>&lt;P&gt;Another solution is to read the 14 bytes into a character string of 14 long and transfer the various pieces explicitly. Then you have all control and you put the values into a structure that is convenient for computing. Reverse the process to write the header to file.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 13:56:30 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372422#M160781</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2022-03-28T13:56:30Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372423#M160782</link>
      <description>&lt;P&gt;FWIW, I've added to a list of proposals for Fortran 202Y the ability to declare "packed" interoperable types.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 14:03:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372423#M160782</guid>
      <dc:creator>Steve_Lionel</dc:creator>
      <dc:date>2022-03-28T14:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372434#M160785</link>
      <description>&lt;P&gt;Thank you for all the replies.&amp;nbsp; Yes it was only warning, not an error, before I allowed padding. I never think of trying to run a program with a warning. In this case, you all seem to be indicating it would have worked fine.&lt;/P&gt;
&lt;P&gt;In my particular example, it was easy to workaround: I just read the odd 2 bytes of file ID in first. Then I checked that it was "BM" before continuing. Maybe that's what you're supposed to do, because if wasn't BM, the .BMP format wouldnlt apply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in general, wouldn't it be logical to think that if a file had a weird structure, like 3 bytes of this and 5 bytes of that etc, you should be able to create a derived type that mirrored the structure and read it in or write it out in one shot?&lt;/P&gt;
&lt;P&gt;For a particular machine the compiler may well have to insert padding for alignment reasons, but the READ and WRITE statements should ignore the padding and read or write the variable lengths as declared.&lt;/P&gt;
&lt;P&gt;If this is what you've proposed for FORTRAN 202x Steve, sounds good to me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 15:01:43 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372434#M160785</guid>
      <dc:creator>Paul_Dent</dc:creator>
      <dc:date>2022-03-28T15:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372475#M160788</link>
      <description>&lt;P&gt;&lt;A href="https://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header" target="_blank"&gt;BMP file format - Wikipedia&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Yes, it is what I proposed - for Fortran 202y - 202x is in its final stages. (I wrote about that today -&amp;nbsp;&lt;A href="https://stevelionel.com/drfortran/2022/03/28/doctor-fortran-in-its-getting-drafty-in-here/" target="_blank"&gt;Doctor Fortran in "It's Getting Drafty in Here" - Doctor Fortran (stevelionel.com)&lt;/A&gt;)&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 17:14:41 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372475#M160788</guid>
      <dc:creator>Steve_Lionel</dc:creator>
      <dc:date>2022-03-28T17:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372703#M160800</link>
      <description>&lt;P&gt;When I did some BMP stuff some years back that was capturing windows and was full of windows sdk calls anyway I did the bmp file io using the SDK "WriteFile" which takes the address and the number of bytes to write. That avoids any extra padding bytes causing an issue.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 13:23:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1372703#M160800</guid>
      <dc:creator>andrew_4619</dc:creator>
      <dc:date>2022-03-29T13:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: reading derived types from a file</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1373132#M160830</link>
      <description>&lt;P&gt;&lt;a href="https://community.intel.com/t5/user/viewprofilepage/user-id/155059"&gt;@Paul_Dent&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;You write, "&lt;SPAN&gt;I want to create a neat way to read in stadrad file types,&amp;nbsp; like .bmp" and what appears a "neat way" to you is the READ statement. "READ(9)HEADER", and you set your language scope as some dialect of Fortran 90 plus some nonstandard extensions in Intel Fortran.&amp;nbsp; This limits you greatly and you have encountered the first issue already, the one with padding of type components.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Instead you may want to consider current standard Fortran with is fully supported by the free Intel oneAPI IFORT compiler and explore your options.&amp;nbsp; Here's a starting point:&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="fortran"&gt;module bmp_m
   use, intrinsic :: iso_c_binding, only : c_char, c_int16_t, c_int32_t
   integer, parameter :: HEADER_BYTES = 14
   type :: bmp_t
      private
      character(kind=c_char, len=HEADER_BYTES) :: m_header_string
      character(kind=c_char, len=:), allocatable :: m_data
      character(kind=c_char, len=2), public :: header_field = ""
      integer(kind=c_int32_t), public :: file_size = 0
      integer(kind=c_int32_t), public :: offset = 0
      integer(kind=c_int16_t), public :: reserved1 = 0
      integer(kind=c_int16_t), public :: reserved2 = 0
   contains
      private
      procedure, pass(dtv) :: read_bmp_t
      generic, public :: read(unformatted) =&amp;gt; read_bmp_t
   end type
contains
   subroutine read_bmp_t( dtv, lun, istat, imsg )
      ! Argument list
      class(bmp_t), intent(inout)     :: dtv
      integer, intent(in)             :: lun
      integer, intent(out)            :: istat
      character(len=*), intent(inout) :: imsg
      istat = 0
      blk_read: block
         read( lun, iostat=istat, iomsg=imsg ) dtv%m_header_string
         if (istat /= 0 ) exit blk_read
         dtv%header_field = dtv%m_header_string(1:2)
         dtv%file_size = transfer( source=dtv%m_header_string(3:6), mold=dtv%file_size )
         dtv%reserved1 = transfer( source=dtv%m_header_string(7:8), mold=dtv%reserved1 )
         dtv%reserved2 = transfer( source=dtv%m_header_string(9:10), mold=dtv%reserved2 )
         dtv%offset = transfer( source=dtv%m_header_string(11:14), mold=dtv%offset )
         if (dtv%file_size &amp;gt; HEADER_BYTES) allocate( character(kind=c_char,len=(dtv%file_size - HEADER_BYTES)) :: dtv%m_data, stat=istat )
         if ( istat /= 0 ) exit blk_read
         read( lun, iostat=istat, iomsg=imsg ) dtv%m_data
      end block blk_read
   end subroutine 
end module
&lt;/LI-CODE&gt;
&lt;P&gt;The above you can consume using the following main program that includes your "neat way" to read the file:&lt;/P&gt;
&lt;LI-CODE lang="fortran"&gt;   use bmp_m, only : bmp_t
   type(bmp_t) :: bmp
   integer :: lun, istat
   character(len=256) :: imsg
   open( newunit=lun, file="anime_mf.bmp", form="unformatted", access="stream", iostat=istat, iomsg=imsg )
   if ( istat /= 0 ) then
      print *, imsg
      stop
   end if
   read( lun, iostat=istat, iomsg=imsg ) bmp
   if ( istat /= 0 ) then
      print *, imsg
      stop
   end if
   print *, "Header field: ", bmp%header_field 
   print *, "File size: ", bmp%file_size 
   print *, "Data offset: ", bmp%offset 
end    
&lt;/LI-CODE&gt;
&lt;P&gt;You can then run the program using the attached BMP file:&lt;/P&gt;
&lt;LI-CODE lang="none"&gt;C:\temp&amp;gt;ifort /standard-semantics p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.5.0 Build 20211109_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.30.30706.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\temp&amp;gt;p.exe
 Header field: BM
 File size:  2672578
 Data offset:  1078
&lt;/LI-CODE&gt;
&lt;P&gt;Attached is the file tried out with the above program that indeed has a file size on &amp;gt;WinNT 5.0 format of 2,672,578 bytes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Citation: the original PNG version of the BMP file is here:&amp;nbsp;&lt;A href="https://github.com/cat-milk/Anime-Girls-Holding-Programming-Books/blob/master/Fortran" target="_blank"&gt;https://github.com/cat-milk/Anime-Girls-Holding-Programming-Books/blob/master/Fortran&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Makise_Kurisu_Modern_Fortran_Explained.png" style="width: 841px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/28119i716F3A4D90563648/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="Makise_Kurisu_Modern_Fortran_Explained.png" alt="Makise_Kurisu_Modern_Fortran_Explained.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 04:49:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/reading-derived-types-from-a-file/m-p/1373132#M160830</guid>
      <dc:creator>FortranFan</dc:creator>
      <dc:date>2022-03-31T04:49:36Z</dc:date>
    </item>
  </channel>
</rss>

