En fait, la meilleure facon de verifier l'endianness d'un fichier est de tester une valeur connue dans le fichier a tester. Ce n'est rien de tres general, mais comme les fichiers UNIX et linux ne transportent pas d'information quant a leur structure, c'est une des seules solution que j'ai trouve.
Bref, le type de fichier que je voulais tester est suffisament specifique pour etre teste de cette maniere, et j'ai donc pu ecrire un petit programme en fortran 90/95 qui me verifie tout ca. Voici le resultat pour ceux qui seraient interesses par l'adapter a leurs besoins.
!! This program will test SAC files for their endianness.
!! As there are no specific endianness information in SAC file headers, we will simply try to read the NPTS of each file and analyse it.
!! If NPTS > 1000000, then we are probably looking at a big endian file (opened as little endian).
!! Otherwise, we can assume we actually have got a little endian file.
!! The program works in two modes:
!! 1) Fully automatic: you can provide a list of files through the command line, each will be tested, and the program will exit.
!! 2) By prompting: without any argument at runtime, you will be prompted for new files until 'q' is sent.
!! This is the module were the test function is located.
!! If the function returns the '1' value, then the file is little_endian.
!! If the returned value is '0' then the file is big endian.
module test
implicit none
!! This module is intended to test the files by checking out their NPTS.
contains
function testing(name)
INTEGER :: testing
CHARACTER(LEN=100) :: name
INTEGER :: loc, I_res, reclength
INTEGER :: ios
loc = 80 !! We will test the NPTS. If it's over 1000000, then we are probably looking at a big endian file.
!write(*,*) "Trying to open file ", name
inquire(FILE=trim(name), RECL=reclength)
write(*,*) "RECL= ",reclength
OPEN(unit=99, file=trim(name), ACCESS='DIRECT', FORM='UNFORMATTED', CONVERT='LITTLE_ENDIAN', RECL=4, ACTION='READ', IOSTAT=ios)
if(ios /= 0) then
Write(*,*) 'OPEN: File opening problem: ',name
STOP
endif
!write(*,*) "File opened, reading its NPTS."
read(UNIT=99, REC=loc) I_res
if(abs(I_res) > 1000000) then
testing = 0
else
testing = 1
endif
close(UNIT=99)
endfunction testing
endmodule test
!! Main program
program lecture
use test
implicit none
!! Variable definition section:
INTEGER :: test_res !! test_res is used to call the corresponding function.
INTEGER :: argnb !! This is the variable containing the number of argument given through the command line.
CHARACTER(LEN=100) :: filename
CHARACTER(LEN=100),DIMENSION(:),ALLOCATABLE :: files !! The command line arguments are stored in that array.
CHARACTER(LEN=3) :: sac
INTEGER :: filelen
INTEGER :: i !! Some classic internal variables
!! Recovering the number of arguments given in command line:
argnb = iargc()
if(argnb /= 0) then !! If any arguments were given, we enter the automatic processing part
ALLOCATE(files(argnb))
do i= 1, argnb
call getarg(i, files(i))
write(*,*) files(i)
enddo
do i=1, argnb
test_res = testing(files(i))
write(*,*) files(i), test_res
enddo
else !! If no arguments were sent, then we enter the prompting mode.
do while(1 == 1)
write(*,*) "Enter the filename or 'q' to quit the program "
read(*,*) filename
do i=1, 98
sac = filename(i:i+2)
if(sac .eq. 'sac') then
filelen = i+2
exit
endif
enddo
if(filename .eq. 'q') then
stop
endif
test_res = testing(filename(1:filelen))
write(*,*) filename(1:filelen), test_res
end do
endif
Endprogram lecture
Merci a Refuznik et phillippe_PMA pour les idees.