subroutine b1_to_si1 ( b1, i1 ) !*****************************************************************************80 ! !! b1_to_si1() converts one byte to a signed integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character B1, the byte to be converted. ! ! Output: ! ! integer I1, the signed integer. ! implicit none character b1 integer i1 i1 = ichar ( b1 ) - 128 return end subroutine b1_to_ui1 ( b1, i1 ) !*****************************************************************************80 ! !! b1_to_ui1() converts one byte to an unsigned integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character B1, the byte to be converted. ! ! Output, integer I1, the unsigned integer. ! implicit none character b1 integer i1 i1 = ichar ( b1 ) return end subroutine b2_to_si2 ( b2, i2 ) !*****************************************************************************80 ! !! b2_to_si2() converts two bytes to a signed integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character B2(2), the bytes to be converted. ! ! Output, integer I2, the signed integer. ! implicit none character b2(2) integer i2 i2 = ichar ( b2(1) ) * 256 + ichar ( b2(2) ) - 32768 return end subroutine b2_to_ui2 ( b2, i2 ) !*****************************************************************************80 ! !! b2_to_ui2() converts two bytes to an unsigned integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character B2(2), the bytes to be converted. ! ! Output, integer I2, the unsigned integer. ! implicit none character b2(2) integer i integer i2 i2 = 0 do i = 1, 2 i2 = i2 * 256 + ichar ( b2(i) ) end do return end subroutine b4_ieee_to_r4 ( word, r ) !*****************************************************************************80 ! !! b4_ieee_to_r4() converts a 4 byte IEEE word into an R4. ! ! Discussion: ! ! An R4 is a 4 byte real value. ! ! This routine does not seem to working reliably for unnormalized data. ! ! The word containing the real value may be interpreted as: ! ! /SEEEEEEE/EFFFFFFF/FFFFFFFF/FFFFFFFF/ ! ! /33222222/22222222/22222100/00000000/ ! /10987654/32109876/54321098/76543210/ <-- Bit numbering ! ! where ! ! S is the sign bit, ! E are the exponent bits, ! F are the mantissa bits. ! ! The mantissa is usually "normalized"; that is, there is an implicit ! leading 1 which is not stored. However, if the exponent is set to ! its minimum value, this is no longer true. ! ! The exponent is "biased". That is, you must subtract a bias value ! from the exponent to get the true value. ! ! If we read the three fields as integers S, E and F, then the ! value of the resulting real number R can be determined by: ! ! * if E = 255 ! if F is nonzero, then R = NaN; ! if F is zero and S is 1, R = -Inf; ! if F is zero and S is 0, R = +Inf; ! * else if E > 0 then R = (-1)^(S) * 2^(E-127) * (1 + (F/2^24)) ! * else if E = 0 ! if F is nonzero, R = (-1)^(S) * 2^(E-126) * (F/2^24) ! if F is zero and S is 1, R = -0; ! if F is zero and S is 0, R = +0; ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 November 2001 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! ANSI/IEEE Standard 754-1985, ! Standard for Binary Floating Point Arithmetic. ! ! Parameters: ! ! Input, integer WORD, the word to be decoded. ! ! Output, real R, the value of the real number. ! implicit none integer e integer f real r integer s integer word ! ! Read the fields. ! s = 0 call mvbits ( word, 31, 1, s, 0 ) e = 0 call mvbits ( word, 23, 8, e, 0 ) f = 0 call mvbits ( word, 0, 23, f, 0 ) ! ! Don't bother trying to return NaN or Inf just yet. ! if ( e == 255 ) then r = 0.0E+00 else if ( 0 < e ) then r = ( -1.0E+00 )**s * 2.0E+00**(e-127-23) * real ( 8388608 + f ) else if ( e == 0 ) then r = ( -1.0E+00 )**s * 2.0E+00**(-126-23) * real ( f ) end if return end subroutine b4_to_si4 ( b4, i4 ) !*****************************************************************************80 ! !! b4_to_si4() converts four bytes to a signed integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character B4(4), the bytes to be converted. ! ! Output, integer I4, the signed integer. ! implicit none character b4(4) integer i integer i4 i4 = 0 do i = 1, 4 i4 = i4 * 256 + ichar ( b4(i) ) end do i4 = ( i4 - 1073741824 ) - 1073741824 return end subroutine b4_to_ui4 ( b4, i4 ) !*****************************************************************************80 ! !! b4_to_ui4() converts four bytes to an unsigned integer. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character B4(4), the bytes to be converted. ! ! Output, integer I4, the unsigned integer. ! implicit none character b4(4) integer i integer i4 i4 = 0 do i = 1, 4 i4 = i4 * 256 + ichar ( b4(i) ) end do return end subroutine byte_swap_get ( byte_swap, nbytes, bytes ) !*****************************************************************************80 ! !! byte_swap_get() gets the current byte swap information. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Output: ! ! logical BYTE_SWAP, is TRUE if there is byte swapping. ! ! integer NBYTES, the number of bytes ! in a swapping unit. ! ! integer BYTES(NBYTES), is the byte swapping pattern. ! implicit none logical byte_swap integer bytes(*) integer nbytes call byte_swap_info ( 'GET', nbytes, byte_swap, bytes ) return end subroutine byte_swap_info ( action, nbytes, byte_swap, bytes ) !*****************************************************************************80 ! !! byte_swap_info() sets or gets byte swapping information. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) ACTION, defines what is to be done. ! 'SET' means that the user is supplying values. ! 'GET' means that the user is requesting values. ! ! Input/output, integer NBYTES, the number of bytes ! in a swapping unit. ! ! Input/output, logical BYTE_SWAP, is TRUE if there is byte swapping. ! ! Input/output, integer BYTES(NBYTES), is the ! byte swapping pattern. ! implicit none character ( len = * ) action logical byte_swap logical, save :: byte_swap_saved = .false. integer bytes(8) integer, save, dimension ( 8 ) :: bytes_saved = & (/ 1, 2, 3, 4, 5, 6, 7, 8 /) integer nbytes integer, save :: nbytes_saved = 4 if ( action == 'SET' ) then byte_swap_saved = byte_swap bytes_saved(1:nbytes) = bytes(1:nbytes) nbytes_saved = nbytes else if ( action == 'GET' ) then byte_swap = byte_swap_saved bytes(1:nbytes_saved) = bytes_saved(1:nbytes_saved) nbytes = nbytes_saved else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'BYTE_SWAP_INFO - Fatal error!' write ( *, '(a)' ) ' Unrecognized action request: ' write ( *, '(a)' ) trim ( action ) stop end if return end subroutine byte_swap_set ( nbytes, byte_swap, bytes ) !*****************************************************************************80 ! !! byte_swap_set() sets the current byte swap information. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, logical BYTE_SWAP, is TRUE if there is byte swapping. ! ! Input, integer NBYTES, the number of bytes in a swapping unit. ! ! Input, integer BYTES(NBYTES), is the byte swapping pattern. ! implicit none integer nbytes logical byte_swap integer bytes(nbytes) call byte_swap_info ( 'SET', nbytes, byte_swap, bytes ) return end function ch_is_printable ( ch ) !*****************************************************************************80 ! !! ch_is_printable() determines if a character is printable. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 October 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character CH, a character to check. ! ! Output, logical CH_IS_PRINTABLE is TRUE if C is a printable character. ! implicit none character ch logical ch_is_printable integer i i = ichar ( ch ) if ( 32 <= i .and. i <= 127 ) then ch_is_printable = .true. else ch_is_printable = .false. end if return end subroutine chvec_print ( n, a, title ) !*****************************************************************************80 ! !! chvec_print() prints a character vector. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 20 July 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, character A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none integer n character a(n) logical ch_is_printable integer i integer ihi integer ilo integer j character ( len = 80 ) string character ( len = * ) title if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if write ( *, '(a)' ) ' ' do ilo = 1, n, 80 ihi = min ( ilo + 79, n ) string = ' ' do i = ilo, ihi j = i + 1 - ilo if ( ch_is_printable ( a(i) ) ) then string(j:j) = a(i) end if end do write ( *, '(a)' ) trim ( string ) end do return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 March 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer IUNIT. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5 and 6). ! ! Otherwise, IUNIT is an integer between 1 and 99, representing a ! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine r4_to_b4_ieee ( r, word ) !*****************************************************************************80 ! !! r4_to_b4_ieee() converts a real value to a 4 byte IEEE word. ! ! Discussion: ! ! This routine does not seem to working reliably for unnormalized data. ! ! Examples: ! ! 0 00000000 00000000000000000000000 = 0 ! 1 00000000 00000000000000000000000 = -0 ! ! 0 11111111 00000000000000000000000 = Infinity ! 1 11111111 00000000000000000000000 = -Infinity ! ! 0 11111111 00000100000000000000000 = NaN ! 1 11111111 00100010001001010101010 = NaN ! ! 0 01111110 00000000000000000000000 = +1 * 2^(126-127) * 1.0 = 0.5 ! 0 01111111 00000000000000000000000 = +1 * 2^(127-127) * 1.0 = 1 ! 0 10000000 00000000000000000000000 = +1 * 2^(128-127) * 1.0 = 2 ! 0 10000001 00000000000000000000000 = +1 * 2^(129-127) * 1.0 = 4 ! ! 0 10000001 10100000000000000000000 = +1 * 2^(129-127) * 1.101 = 6.5 ! 1 10000001 10100000000000000000000 = -1 * 2^(129-127) * 1.101 = -6.5 ! ! 0 00000001 00000000000000000000000 = +1 * 2^( 1-127) * 1.0 = 2^(-126) ! 0 00000000 10000000000000000000000 = +1 * 2^( 0-126) * 0.1 = 2^(-127) ! 0 00000000 00000000000000000000001 = +1 * 2^( 0-126) * ! 0.00000000000000000000001 = ! 2^(-149) (Smallest positive value) ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 November 2001 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! ANSI/IEEE Standard 754-1985, ! Standard for Binary Floating Point Arithmetic. ! ! Parameters: ! ! Input, real R, the real number to be converted. ! ! Output, integer WORD, the IEEE representation of the number. ! implicit none integer e integer f real r real r_copy integer s integer word r_copy = r ! ! Determine S, the sign bit. ! if ( 0.0E+00 <= r_copy ) then s = 0 else s = 1 r_copy = - r_copy end if ! ! Determine E, the exponent. ! (FOR NOW, IGNORE UNNORMALIZED NUMBERS) ! e = 0 if ( r == 0.0E+00 ) then else do while ( 2.0E+00 <= r_copy ) e = e + 1 r_copy = r_copy / 2.0E+00 end do do while ( r_copy < 1.0E+00 .and. -127 < e ) e = e - 1 r_copy = r_copy * 2.0E+00 end do e = e + 127 end if ! ! Determine F, the fraction. ! if ( r == 0.0E+00 ) then f = 0 else if ( 0 < e ) then r_copy = r_copy - 1.0E+00 f = int ( r_copy * 2.0E+00**23 ) else if ( e == 0 ) then f = int ( r_copy * 2.0E+00**23 ) end if ! ! Set the bits corresponding to S, E, F. ! call mvbits ( s, 0, 1, word, 31 ) call mvbits ( e, 0, 8, word, 23 ) call mvbits ( f, 0, 23, word, 0 ) return end subroutine raw_ch_count ( file_name, nchar ) !*****************************************************************************80 ! !! raw_ch_count() counts the number of characters in a "raw" file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NCHAR, the number of characters in the file. ! implicit none character c character ( len = * ) file_name integer file_rec integer file_unit integer nchar call raw_open ( file_name, file_unit ) nchar = 0 file_rec = 1 do call raw_ch_read ( file_unit, file_rec, c ) if ( file_rec < 0 ) then exit end if nchar = nchar + 1 end do close ( unit = file_unit ) return end subroutine raw_ch_read ( file_unit, file_rec, c ) !*****************************************************************************80 ! !! raw_ch_read() reads one character from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and C is set to ' '. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, character C, the value that was read. ! implicit none character c integer file_rec integer file_unit integer ios character, parameter :: NULL = char ( 0 ) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_CH_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if read ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c if ( ios /= 0 ) then c = NULL file_rec = -1 return end if file_rec = file_rec + 1 return end subroutine raw_chvec_read ( file_unit, file_rec, c, n ) !*****************************************************************************80 ! !! raw_chvec_read() reads N characters from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and C is set to ' '. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, character C(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n character c(n) integer file_rec integer file_unit integer i integer ios character, parameter :: NULL = char ( 0 ) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_CHVEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n read ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(i) if ( ios /= 0 ) then c(i:n) = NULL file_rec = -1 return end if file_rec = file_rec + 1 end do return end subroutine raw_ch_write ( file_unit, file_rec, c ) !*****************************************************************************80 ! !! raw_ch_write() writes one character to a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, character C, the value to be written. ! implicit none character c integer file_rec integer file_unit integer ios if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_CH_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 return end subroutine raw_chvec_write ( file_unit, file_rec, c, n ) !*****************************************************************************80 ! !! raw_chvec_write() writes N characters to a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, character C(N), the values to be written. ! ! Input, integer N, the number of characters to write. ! implicit none integer n character c(n) integer file_rec integer file_unit integer i integer ios if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_CHVEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(i) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do return end subroutine raw_open ( file_name, file_unit ) !*****************************************************************************80 ! !! raw_open() opens a "raw" file. ! ! Discussion: ! ! A "raw" file is a file opened as a binary direct access file with ! record length of a single character. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer FILE_UNIT, the unit number associated ! with the file. ! implicit none character ( len = * ) file_name integer file_unit integer ios call get_unit ( file_unit ) if ( file_unit <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_OPEN - Fatal error!' write ( *, '(a)' ) ' Unable to get a unit number.' stop end if open ( unit = file_unit, file = file_name, form = 'formatted', & access = 'direct', recl = 1, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_OPEN - Fatal error!' write ( *, '(a,i6)' ) ' IO error on open, IOSTAT = ', ios stop end if return end subroutine raw_r4_read ( file_unit, file_rec, r4 ) !*****************************************************************************80 ! !! raw_r4vec_read() reads the "next" 4 byte real from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 September 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, real R4, the value that was read. ! implicit none integer bytes(8) logical byte_swap character c(4) character ( len = 4 ) c4 integer file_rec integer file_unit integer nbytes real r4 if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_R4_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! Read 4 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 4 ) ! ! Concatenate the characters into a string. ! c4(1:4) = c(1) // c(2) // c(3) // c(4) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c4 ) end if ! ! Read the 4 characters as a real value. ! read ( c4, '(a4)' ) r4 return end subroutine raw_r4vec_read ( file_unit, file_rec, r4, n ) !*****************************************************************************80 ! !! raw_r4vec_read() reads the "next" N 4 byte reals from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, real R4(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n integer bytes(8) logical byte_swap character c(4) character ( len = 4 ) c4 integer file_rec integer file_unit integer i integer nbytes real r4(n) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_R4VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 4 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 4 ) ! ! Concatenate the characters into a string. ! c4(1:4) = c(1) // c(2) // c(3) // c(4) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c4 ) end if ! ! Read the 4 characters as a real value. ! read ( c4, '(a4)' ) r4(i) end do return end subroutine raw_r8_read ( file_unit, file_rec, r8 ) !*****************************************************************************80 ! !! raw_r8vec_read() reads the "next" 8 byte real from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 September 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, real ( kind = rk8 ) R8, the values that were read. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer bytes(8) logical byte_swap character c(8) character ( len = 8 ) c8 integer file_rec integer file_unit integer nbytes real ( kind = rk8 ) r8 if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_R8VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! Read 8 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 8 ) ! ! Concatenate the characters into a string. ! c8(1:8) = c(1) // c(2) // c(3) // c(4) // c(5) // c(6) // c(7) // c(8) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c8 ) end if ! ! Read the 8 characters as a real value. ! read ( c8, '(a8)' ) r8 return end subroutine raw_r8vec_read ( file_unit, file_rec, r8, n ) !*****************************************************************************80 ! !! raw_r8vec_read() reads the "next" N 8 byte real from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, real ( kind = rk8 ) R8(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n integer bytes(8) logical byte_swap character c(8) character ( len = 8 ) c8 integer file_rec integer file_unit integer i integer nbytes real ( kind = rk8 ) r8(n) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_R8VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 8 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 8 ) ! ! Concatenate the characters into a string. ! c8(1:8) = c(1) // c(2) // c(3) // c(4) // c(5) // c(6) // c(7) // c(8) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c8 ) end if ! ! Read the 8 characters as a real value. ! read ( c8, '(a8)' ) r8(i) end do return end subroutine raw_s_read ( file_unit, file_rec, s ) !*****************************************************************************80 ! !! raw_s_read() reads the "next" string from a raw file. ! ! Discussion: ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and S is set to ' '. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, character ( len = * ) S, the string that was read. ! implicit none integer file_rec integer file_unit integer i integer ios character ( len = * ) s integer s_len if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_S_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if s_len = len ( s ) do i = 1, s_len read ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) s(i:i) if ( ios == 0 ) then file_rec = file_rec + 1 else file_rec = -1 s(i:) = ' ' return end if end do return end subroutine raw_si1vec_read ( file_unit, file_rec, i1, n ) !*****************************************************************************80 ! !! raw_si1vec_read() reads N signed 1 byte integers from a raw file. ! ! Discussion: ! ! A signed 1 byte integer is a value between -128 and +127. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been incremented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I1(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n character c1(1) integer file_rec integer file_unit integer i integer i1(n) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI1VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 1 character from the file. ! call raw_chvec_read ( file_unit, file_rec, c1, 1 ) ! ! Interpret the character as an integer. ! call b1_to_si1 ( c1(1), i1(i) ) end do return end subroutine raw_si1vec_write ( file_unit, file_rec, i1, n ) !*****************************************************************************80 ! !! raw_si1vec_write() writes N 1 byte signed integers to a raw file. ! ! Discussion: ! ! A signed 1 byte integer is a value between -128 and +127. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I1(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character c1 integer file_rec integer file_unit integer i integer i1(n) integer ios if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI1VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n call si1_to_b1 ( i1(i), c1 ) write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c1 if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do return end subroutine raw_si2vec_read ( file_unit, file_rec, i2, n ) !*****************************************************************************80 ! !! raw_si2vec_read() reads N 2 byte signed integers from a raw file. ! ! Discussion: ! ! A signed 2 byte integer is a value between -32768 and +32767. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I2(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n integer bytes(8) logical byte_swap character ( len = 2 ) c integer file_rec integer file_unit integer i integer i2(n) integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI2VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 2 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 2 ) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI2VEC_READ - Do byte swapping!' write ( *, '(a,2i2)' ) ' BYTES=', bytes(1), bytes(2) call s_byte_swap ( nbytes, bytes, c ) end if ! ! Read the characters as an integer. ! call b2_to_si2 ( c, i2(i) ) end do return end subroutine raw_si2vec_write ( file_unit, file_rec, i2, n ) !*****************************************************************************80 ! !! raw_si2vec_write() writes N 2 byte signed integers to a raw file. ! ! Discussion: ! ! A signed 2 byte integer is a value between -32768 and +32767. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I2(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character c(2) integer file_rec integer file_unit integer i integer i2(n) integer ios integer j if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI2VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! What about BYTE_SWAPPING? ! do i = 1, n call si2_to_b2 ( i2(i), c ) do j = 1, 2 write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(j) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do end do return end subroutine raw_si4vec_read ( file_unit, file_rec, i4, n ) !*****************************************************************************80 ! !! raw_si4vec_read() reads N 4 byte signed integers from a raw file. ! ! Discussion: ! ! A signed 4 byte integer is a value between -2147483648 and +2147483647. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I4(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n integer bytes(8) logical byte_swap character ( len = 4 ) c integer file_rec integer file_unit integer i integer i4(n) integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI4VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 4 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 4 ) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c ) end if ! ! Read the characters as an integer. ! call b4_to_si4 ( c, i4(i) ) end do return end subroutine raw_si4vec_write ( file_unit, file_rec, i4, n ) !*****************************************************************************80 ! !! raw_si4vec_write() writes N 4 byte signed integers to a raw file. ! ! Discussion: ! ! A signed 4 byte integer is a value between -2147483648 and +2147483647. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I4(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character c(4) integer file_rec integer file_unit integer i integer i4(n) integer ios integer j if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_SI4VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! What about BYTE_SWAPPING? ! do i = 1, n call si4_to_b4 ( i4(i), c ) do j = 1, 4 write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(j) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do end do return end subroutine raw_ui1vec_read ( file_unit, file_rec, i1, n ) !*****************************************************************************80 ! !! raw_ui1vec_read() reads N unsigned 1 byte integers from a raw file. ! ! Discussion: ! ! An unsigned 1 byte integer is a value between 0 and +255. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been incremented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I1(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n character c1(1) integer file_rec integer file_unit integer i integer i1(n) if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI1VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 1 character from the file. ! call raw_chvec_read ( file_unit, file_rec, c1, 1 ) ! ! Read the character as an unsigned integer. ! call b1_to_ui1 ( c1(1), i1(i) ) end do return end subroutine raw_ui1vec_write ( file_unit, file_rec, i1, n ) !*****************************************************************************80 ! !! raw_ui1vec_write() writes N 1 byte unsigned integers to a raw file. ! ! Discussion: ! ! An unsigned 1 byte integer is a value between 0 and +255. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I1(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character c1 integer file_rec integer file_unit integer i integer i1(n) integer ios if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI1VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n call ui1_to_b1 ( i1(i), c1 ) write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c1 if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do return end subroutine raw_ui2_read ( file_unit, file_rec, i2 ) !*****************************************************************************80 ! !! raw_ui2_read() reads one 2 byte unsigned integer from a raw file. ! ! Discussion: ! ! An unsigned 2 byte integer is a value between 0 and +65535. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I2, the value that was read. ! implicit none integer bytes(8) logical byte_swap character ( len = 2 ) c integer file_rec integer file_unit integer i2 integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI2_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! Read 2 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 2 ) ! ! Byte swap. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c ) end if ! ! Interpret the characters as an integer. ! call b2_to_ui2 ( c, i2 ) ! write ( *, * ) 'DEBUG: RAW_UI2_READ has I2 = ', i2 return end subroutine raw_ui2_write ( file_unit, file_rec, i2 ) !*****************************************************************************80 ! !! raw_ui2_write() writes a 2 byte unsigned integer to a raw file. ! ! Discussion: ! ! An unsigned 2 byte integer is a value between 0 and +65535. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 September 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I2, the value to be written. ! implicit none integer bytes(8) logical byte_swap character ( len = 2 ) c2 integer file_rec integer file_unit integer i2 integer ios integer j integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI2VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if call ui2_to_b2 ( i2, c2 ) ! ! Byte swap. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c2 ) end if do j = 1, 2 write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c2(j:j) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do return end subroutine raw_ui2vec_read ( file_unit, file_rec, i2, n ) !*****************************************************************************80 ! !! raw_ui2vec_read() reads N 2 byte unsigned integers from a raw file. ! ! Discussion: ! ! An unsigned 2 byte integer is a value between 0 and +65535. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I2(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n integer bytes(8) logical byte_swap character ( len = 2 ) c integer file_rec integer file_unit integer i integer i2(n) integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI2VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 2 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 2 ) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI2VEC_READ - Byte swap!' write ( *, '(a,2i2)' ) ' BYTES=', bytes(1), bytes(2) ! call s_byte_swap ( nbytes, bytes, c ) end if ! ! Interpret the characters as an integer. ! call b2_to_ui2 ( c, i2(i) ) end do return end subroutine raw_ui2vec_write ( file_unit, file_rec, i2, n ) !*****************************************************************************80 ! !! raw_ui2vec_write() writes N 2 byte unsigned integers to a raw file. ! ! Discussion: ! ! An unsigned 2 byte integer is a value between 0 and +65535. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I2(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character ( len = 2 ) c integer file_rec integer file_unit integer i integer i2(n) integer ios integer j if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI2VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! What about BYTE_SWAPPING? ! do i = 1, n call ui2_to_b2 ( i2(i), c ) do j = 1, 2 write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(j:j) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do end do return end subroutine raw_ui4vec_read ( file_unit, file_rec, i4, n ) !*****************************************************************************80 ! !! raw_ui4vec_read() reads N 4 byte unsigned integers from a raw file. ! ! Discussion: ! ! An unsigned 4 byte integer is a value between 0 and +4294967295. ! ! On input, FILE_REC should point to the record to be read. If the first ! character is to be read, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was read. ! ! However, if there was no record corresponding to the input value of ! FILE_REC, then FILE_REC is set to -1, and I is set to 0. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Output, integer I4(N), the values that were read. ! ! Input, integer N, the number of values to read. ! implicit none integer n integer bytes(8) logical byte_swap character ( len = 4 ) c integer file_rec integer file_unit integer i integer i4(n) integer nbytes if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI4VEC_READ - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if do i = 1, n ! ! Read 4 characters from the file. ! call raw_chvec_read ( file_unit, file_rec, c, 4 ) ! ! Byte swap if necessary. ! call byte_swap_get ( byte_swap, nbytes, bytes ) if ( byte_swap ) then call s_byte_swap ( nbytes, bytes, c ) end if ! ! Read the characters as an integer. ! call b4_to_ui4 ( c, i4(i) ) end do return end subroutine raw_ui4vec_write ( file_unit, file_rec, i4, n ) !*****************************************************************************80 ! !! raw_ui4_write() writes N 4 byte unsigned integers to a raw file. ! ! Discussion: ! ! An unsigned 4 byte integer is a value between 0 and +4294967295. ! ! On input, FILE_REC should point to the record to be written. If the first ! character is to be written, then FILE_REC should be 1. ! ! On output, FILE_REC will have been implemented to point to the next ! record after the data that was written. ! ! However, if an error occurred during the write, FILE_REC is set to -1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_UNIT, the name of the file. ! ! Input/output, integer FILE_REC, the record pointer. ! ! Input, integer I4(N), the values to be written. ! ! Input, integer N, the number of values to write. ! implicit none integer n character c(4) integer file_rec integer file_unit integer i integer i4(n) integer ios integer j if ( file_rec <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RAW_UI4VEC_WRITE - Fatal error!' write ( *, '(a,i6)' ) ' Illegal record number = ', file_rec stop end if ! ! What about BYTE_SWAPPING? ! do i = 1, n call ui4_to_b4 ( i4(i), c ) do j = 1, 4 write ( file_unit, rec = file_rec, fmt = '(a)', iostat = ios ) c(j) if ( ios /= 0 ) then file_rec = -1 return end if file_rec = file_rec + 1 end do end do return end subroutine s_byte_swap ( nbytes, bytes, s ) !*****************************************************************************80 ! !! s_byte_swap() swaps the bytes in a string. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 May 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer NBYTES, the number of bytes in ! the swapping pattern. ! ! Input, integer BYTES(NBYTES), the byte swap pattern. ! ! Input/output, character ( len = * ) S, a string whose bytes are to ! be swapped. ! implicit none integer nbytes integer bytes(nbytes) integer i integer i2 integer j integer n character ( len = * ) s character ( len = 256 ) t n = len ( s ) do i = 1, n i2 = mod ( i - 1, nbytes ) + 1 j = bytes(i2) t(i:i) = s(j:j) end do s(1:n) = t(1:n) return end subroutine si1_to_b1 ( i1, b1 ) !*****************************************************************************80 ! !! si1_to_b1() converts a signed integer to one byte. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I1, the signed integer. ! ! Output, character B1, the corresponding byte. ! implicit none character b1 integer i1 b1 = char ( i1 + 128 ) return end subroutine si2_to_b2 ( i2, b2 ) !*****************************************************************************80 ! !! si2_to_b2() converts a signed integer to two bytes. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I2, the signed integer. ! ! Output, character B2(2), the corresponding bytes. ! implicit none character b2(2) integer i2 integer i2_digit integer i2_temp integer j ! ! Convert to unsigned form. ! i2_temp = i2 + 32768 do j = 2, 1, -1 i2_digit = mod ( i2_temp, 256 ) b2(j) = char ( i2_digit ) i2_temp = i2_temp / 256 end do return end subroutine si4_to_b4 ( i4, b4 ) !*****************************************************************************80 ! !! si4_to_b4() converts a signed integer to four bytes. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I4, the signed integer. ! ! Output, character B4(4), the corresponding bytes. ! implicit none character b4(4) integer i4 integer i4_digit integer i4_temp integer j ! ! Convert to unsigned form. ! i4_temp = ( i4 + 1073741824 ) + 1073741824 do j = 4, 1, -1 i4_digit = mod ( i4_temp, 256 ) b4(j) = char ( i4_digit ) i4_temp = i4_temp / 256 end do return end subroutine ui1_to_b1 ( i1, b1 ) !*****************************************************************************80 ! !! ui1_to_b1() converts an unsigned integer to one byte. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I1, the unsigned integer. ! ! Output, character B1, the corresponding byte. ! implicit none character b1 integer i1 b1 = char ( i1 ) return end subroutine ui2_to_b2 ( i2, b2 ) !*****************************************************************************80 ! !! ui2_to_b2() converts an unsigned integer to two bytes. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer I2, the unsigned integer. ! ! Output: ! ! character ( len = 2 ) B2, the corresponding bytes. ! implicit none character ( len = 2 ) b2 integer i2 integer i2_digit integer i2_temp integer j i2_temp = i2 do j = 2, 1, -1 i2_digit = mod ( i2_temp, 256 ) b2(j:j) = char ( i2_digit ) i2_temp = i2_temp / 256 end do return end subroutine ui4_to_b4 ( i4, b4 ) !*****************************************************************************80 ! !! ui4_to_b4() converts an unsigned integer to four bytes. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 November 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I4, the unsigned integer. ! ! Output, character B4(4), the corresponding bytes. ! implicit none character b4(4) integer i4 integer i4_digit integer i4_temp integer j i4_temp = i4 do j = 4, 1, -1 i4_digit = mod ( i4_temp, 256 ) b4(j) = char ( i4_digit ) i4_temp = i4_temp / 256 end do return end