56 '$Id: Binary_File_Utility.f90 60152 2015-08-13 19:19:13Z paul.vandelst@noaa.gov $' 60 INTEGER(Long),
PARAMETER ::
false = 0_long
61 INTEGER(Long),
PARAMETER ::
true = 1_long
63 INTEGER,
PARAMETER ::
ml = 256
64 INTEGER,
PARAMETER ::
gl = 5000
159 For_Output, & ! Optional input
163 CHARACTER(*),
INTENT(IN) :: filename
164 INTEGER,
INTENT(OUT) :: fileid
165 LOGICAL,
OPTIONAL,
INTENT(IN) :: for_output
166 LOGICAL,
OPTIONAL,
INTENT(IN) :: no_check
170 CHARACTER(*),
PARAMETER :: routine_name =
'Open_Binary_File' 173 CHARACTER(ML) :: io_msg
174 LOGICAL :: file_check
175 LOGICAL :: file_input
177 INTEGER(Long) :: magic_number_read
178 CHARACTER(7) :: file_status
179 CHARACTER(5) :: file_action
185 IF (
PRESENT(for_output) ) file_input = .NOT. for_output
187 file_check = file_input
188 IF (
PRESENT(no_check) ) file_check = (.NOT. no_check) .AND. file_input
192 IF ( file_input )
THEN 198 file_status =
'REPLACE' 199 file_action =
'WRITE' 204 IF ( file_check )
THEN 206 IF ( err_stat /=
success )
THEN 207 msg =
'Error checking '//trim(filename)//
' file byte order' 215 IF ( fileid < 0 )
THEN 216 msg =
'Error obtaining file unit number for '//trim(filename)
222 OPEN( fileid, file = filename , &
223 status = file_status , &
224 action = file_action , &
225 access =
'SEQUENTIAL' , &
226 form =
'UNFORMATTED', &
229 IF ( io_stat /= 0 )
THEN 230 msg =
'Error opening '//trim(filename)//
' - '//trim(io_msg)
236 IF ( file_input )
THEN 237 READ( fileid, iostat=io_stat, iomsg=io_msg ) magic_number_read
238 IF ( io_stat /= 0 )
THEN 239 msg =
'Error reading magic number from '//trim(filename)//
' - '//trim(io_msg)
243 WRITE( fileid, iostat=io_stat, iomsg=io_msg )
magic_number 244 IF ( io_stat /= 0 )
THEN 245 msg =
'Error writing magic number to '//trim(filename)//
' - '//trim(io_msg)
254 CLOSE( fileid, iostat=io_stat, iomsg=io_msg )
255 IF ( io_stat /= 0 ) &
256 msg = trim(msg)//
'; Error closing file during error cleanup - '//trim(io_msg)
272 Write_Module, & ! Optional input
273 Created_On , & ! Optional input
274 Title , & ! Optional input
275 History , & ! Optional input
279 INTEGER ,
INTENT(IN) :: fid
280 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: write_module
281 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: created_on
282 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: title
283 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: history
284 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: comment
288 CHARACTER(*),
PARAMETER :: routine_name =
'WriteGAtts_Binary_File' 291 CHARACTER(ML) :: io_msg
292 CHARACTER(8) :: cdate
293 CHARACTER(10) :: ctime
294 CHARACTER(5) :: czone
303 IF ( err_stat /=
success )
RETURN 306 CALL date_and_time( cdate, ctime, czone )
307 IF (
PRESENT(created_on) )
THEN 312 cdate(1:4)//
'/'//cdate(5:6)//
'/'//cdate(7:8)//
', '// &
313 ctime(1:2)//
':'//ctime(3:4)//
':'//ctime(5:6)//
' '// &
316 IF ( err_stat /=
success )
RETURN 321 IF ( err_stat /=
success )
RETURN 326 IF ( err_stat /=
success )
RETURN 331 IF ( err_stat /=
success )
RETURN 336 CHARACTER(*),
INTENT(IN) :: gattname
337 CHARACTER(*),
OPTIONAL,
INTENT(IN) :: gattvalue
339 CHARACTER(GL) :: l_gattvalue
342 IF (
PRESENT(gattvalue) )
THEN 343 IF ( len_trim(gattvalue) /= 0 ) l_gattvalue = trim(gattname)//
': '//trim(gattvalue)
345 gattlen = len_trim(l_gattvalue)
347 WRITE( fid, iostat=io_stat, iomsg=io_msg ) gattlen
348 IF ( io_stat /= 0 )
THEN 349 msg =
'Error writing '//trim(gattname)//
' attribute length - '//trim(io_msg)
352 IF ( gattlen == 0 )
RETURN 354 WRITE( fid, iostat=io_stat, iomsg=io_msg ) trim(l_gattvalue)
355 IF ( io_stat /= 0 )
THEN 356 msg =
'Error writing '//trim(gattname)//
' attribute - '//trim(io_msg)
363 CLOSE( fid, iostat=io_stat, iomsg=io_msg )
364 IF ( io_stat /= 0 ) &
365 msg = trim(msg)//
'; Error closing output file during error cleanup - '//trim(io_msg)
379 Write_Module, & ! Optional output
380 Created_On , & ! Optional output
381 Title , & ! Optional output
382 History , & ! Optional output
386 INTEGER ,
INTENT(IN) :: fid
387 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: write_module
388 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: created_on
389 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: title
390 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: history
391 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: comment
395 CHARACTER(*),
PARAMETER :: routine_name =
'ReadGAtts_Binary_File' 398 CHARACTER(ML) :: io_msg
408 IF ( err_stat /=
success )
RETURN 412 IF ( err_stat /=
success )
RETURN 417 IF ( err_stat /=
success )
RETURN 422 IF ( err_stat /=
success )
RETURN 427 IF ( err_stat /=
success )
RETURN 432 CHARACTER(*),
INTENT(IN) :: gattname
433 CHARACTER(*),
OPTIONAL,
INTENT(OUT) :: gattvalue
434 INTEGER :: i, gattlen
435 CHARACTER(GL) :: l_gattvalue
437 IF (
PRESENT(gattvalue) ) gattvalue =
'' 440 READ( fid, iostat=io_stat, iomsg=io_msg ) gattlen
441 IF ( io_stat /= 0 )
THEN 442 msg =
'Error reading '//trim(gattname)//
' attribute length - '//trim(io_msg)
445 IF ( gattlen == 0 )
RETURN 447 READ( fid, iostat=io_stat, iomsg=io_msg ) l_gattvalue(1:gattlen)
448 IF ( io_stat /= 0 )
THEN 449 msg =
'Error reading '//trim(gattname)//
' attribute - '//trim(io_msg)
453 IF (
PRESENT(gattvalue) )
THEN 454 i = index(l_gattvalue,
': ')
455 gattvalue = l_gattvalue(i+2:gattlen)
461 CLOSE( fid, iostat=io_stat, iomsg=io_msg )
462 IF ( io_stat /= 0 ) &
463 msg = trim(msg)//
'; Error closing input file during error cleanup - '//trim(io_msg)
487 INTEGER,
INTENT(IN) :: fid
488 LOGICAL,
INTENT(OUT) :: logical_value
492 CHARACTER(*),
PARAMETER :: routine_name =
'ReadLogical_Binary_File(Scalar)' 495 CHARACTER(ML) :: io_msg
497 INTEGER(Long) :: logical_integer
503 READ( fid,iostat=io_stat,iomsg=io_msg ) logical_integer
504 IF ( io_stat /= 0 )
THEN 506 msg =
'Error reading logical integer value - '//trim(io_msg)
512 logical_value = (logical_integer ==
true)
521 INTEGER,
INTENT(IN) :: fid
522 LOGICAL,
INTENT(OUT) :: logical_value(:)
526 CHARACTER(*),
PARAMETER :: routine_name =
'ReadLogical_Binary_File(Rank-1)' 529 CHARACTER(ML) :: io_msg
531 INTEGER(Long) :: logical_integer(
size(logical_value))
537 READ( fid,iostat=io_stat,iomsg=io_msg ) logical_integer
538 IF ( io_stat /= 0 )
THEN 540 msg =
'Error reading logical integer rank-1 array - '//trim(io_msg)
546 logical_value = (logical_integer ==
true)
564 INTEGER,
INTENT(IN) :: fid
565 LOGICAL,
INTENT(IN) :: logical_value
569 CHARACTER(*),
PARAMETER :: routine_name =
'WriteLogical_Binary_File(Scalar)' 572 CHARACTER(ML) :: io_msg
574 INTEGER(Long) :: logical_integer
581 IF ( logical_value )
THEN 582 logical_integer =
true 584 logical_integer =
false 589 WRITE( fid,iostat=io_stat,iomsg=io_msg ) logical_integer
590 IF ( io_stat /= 0 )
THEN 592 msg =
'Error writing logical integer - '//trim(io_msg)
604 INTEGER,
INTENT(IN) :: fid
605 LOGICAL,
INTENT(IN) :: logical_value(:)
609 CHARACTER(*),
PARAMETER :: routine_name =
'WriteLogical_Binary_File(Rank-1)' 612 CHARACTER(ML) :: io_msg
614 INTEGER(Long) :: logical_integer(
size(logical_value))
621 WHERE ( logical_value )
622 logical_integer =
true 624 logical_integer =
false 629 WRITE( fid,iostat=io_stat,iomsg=io_msg ) logical_integer
630 IF ( io_stat /= 0 )
THEN 632 msg =
'Error writing logical integer rank-1 array - '//trim(io_msg)
694 CHARACTER(*),
INTENT(IN) :: filename
698 CHARACTER(*),
PARAMETER :: routine_name =
'Check_Binary_File' 701 CHARACTER(ML) :: io_msg
704 INTEGER(Long) :: magic_number_read
705 INTEGER(Long) :: magic_number_swapped
712 IF ( bit_size( 1_long ) /= 32 )
THEN 713 msg =
'32-bit integers not supported. Unable to determine endian-ness' 721 msg =
'Error obtaining file unit number for '//trim(filename)
727 OPEN( fid, file = filename , &
730 access =
'DIRECT' , &
731 form =
'UNFORMATTED', &
735 IF ( io_stat /= 0 )
THEN 736 msg =
'Error opening '//trim(filename)//
' - '//trim(io_msg)
742 READ( fid, rec=2, iostat=io_stat, iomsg=io_msg ) magic_number_read
743 IF ( io_stat /= 0 )
THEN 744 msg =
'Error reading file magic number - '//trim(io_msg)
757 magic_number_swapped =
swap_endian( magic_number_read )
759 msg =
'Unrecognised file format. Invalid magic number.' 764 msg =
'Data file needs to be byte-swapped.' 773 CLOSE( fid, iostat=io_stat, iomsg=io_msg )
774 IF ( io_stat /= 0 ) &
775 msg = trim(msg)//
'; Error closing file during error cleanup - '//trim(io_msg)
subroutine readgatts_cleanup()
subroutine readsinglegatt(gattname, gattvalue)
integer, parameter, public failure
integer, parameter, public warning
integer, parameter, public long
character(*), parameter history_gattname
character(*), parameter title_gattname
integer function writelogical_scalar(fid, logical_value)
integer function, public readgatts_binary_file(fid, Write_Module, Created_On, Title, History, Comment)
integer(long), parameter magic_number
integer(long), parameter true
integer, parameter, public n_bytes_long
integer function, public get_lun()
integer(long), parameter false
integer function check_binary_file(Filename)
integer function, public open_binary_file(Filename, FileID, For_Output, No_Check)
recursive subroutine, public display_message(Routine_Name, Message, Error_State, Message_Log)
subroutine writesinglegatt(gattname, gattvalue)
character(*), parameter write_module_gattname
character(*), parameter comment_gattname
character(*), parameter module_version_id
************************************************************************GNU Lesser General Public License **This file is part of the GFDL Flexible Modeling System(FMS). ! *! *FMS is free software without even the implied warranty of MERCHANTABILITY or *FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License *for more details **You should have received a copy of the GNU Lesser General Public *License along with FMS If see< http:! ***********************************************************************subroutine READ_RECORD_CORE_(unit, field, nwords, data, start, axsiz) integer, intent(in) ::unit type(fieldtype), intent(in) ::field integer, intent(in) ::nwords MPP_TYPE_, intent(inout) ::data(nwords) integer, intent(in) ::start(:), axsiz(:) integer(SHORT_KIND) ::i2vals(nwords)!rab used in conjunction with transfer intrinsic to determine size of a variable integer(KIND=1) ::one_byte(8) integer ::word_sz!#ifdef __sgi integer(INT_KIND) ::ivals(nwords) real(FLOAT_KIND) ::rvals(nwords)!#else! integer ::ivals(nwords)! real ::rvals(nwords)!#endif real(DOUBLE_KIND) ::r8vals(nwords) pointer(ptr1, i2vals) pointer(ptr2, ivals) pointer(ptr3, rvals) pointer(ptr4, r8vals) if(mpp_io_stack_size< nwords) call mpp_io_set_stack_size(nwords) call mpp_error(FATAL, 'MPP_READ currently requires use_netCDF option') end subroutine READ_RECORD_CORE_ subroutine READ_RECORD_(unit, field, nwords, data, time_level, domain, position, tile_count, start_in, axsiz_in)!routine that is finally called by all mpp_read routines to perform the read!a non-netCDF record contains:! field ID! a set of 4 coordinates(is:ie, js:je) giving the data subdomain! a timelevel and a timestamp(=NULLTIME if field is static)! 3D real data(stored as 1D)!if you are using direct access I/O, the RECL argument to OPEN must be large enough for the above!in a global direct access file, record position on PE is given by %record.!Treatment of timestamp:! We assume that static fields have been passed without a timestamp.! Here that is converted into a timestamp of NULLTIME.! For non-netCDF fields, field is treated no differently, but is written! with a timestamp of NULLTIME. There is no check in the code to prevent! the user from repeatedly writing a static field. integer, intent(in) ::unit, nwords type(fieldtype), intent(in) ::field MPP_TYPE_, intent(inout) ::data(nwords) integer, intent(in), optional ::time_level type(domain2D), intent(in), optional ::domain integer, intent(in), optional ::position, tile_count integer, intent(in), optional ::start_in(:), axsiz_in(:) integer, dimension(size(field%axes(:))) ::start, axsiz integer ::tlevel !, subdomain(4) integer ::i, error, is, ie, js, je, isg, ieg, jsg, jeg type(domain2d), pointer ::io_domain=> tlevel if(PRESENT(start_in) .AND. PRESENT(axsiz_in)) then if(size(start(! the data domain and compute domain must refer to the subdomain being passed ! In this ! since that attempts to gather all data on PE size(field%axes(:)) axsiz(i)
integer function, public writegatts_binary_file(fid, Write_Module, Created_On, Title, History, Comment)
character(*), parameter created_on_gattname
integer function writelogical_rank1(fid, logical_value)
subroutine writegatts_cleanup()
integer, parameter, public success
integer function readlogical_scalar(fid, logical_value)
integer function readlogical_rank1(fid, logical_value)