ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_io::getCountRecord Interface Reference

Generate and return the number of records in the entire record-oriented input file. More...

Detailed Description

Generate and return the number of records in the entire record-oriented input file.

If any error occurs during the IO and the output iostat argument is missing, the program will halt by calling error stop.
Within the procedures,

  1. If the input argument file is present, the file will be opened on input and closed before returning the control.
  2. If the input argument unit is present, the file is assumed to opened already, will be rewound to the file beginning, and will be positioned after the last record in file without being closed on return.

See getCountRecordLeft for counting the number of records left in an already-opened file.

Parameters
[in]file: The input scalar character of default kind SK containing the file path, whose number of records will be returned.
The input file must not be connected prior to calling this procedure.
If the file is already connected, specify the file unit as input unit argument.
(optional. It must be present if and only if the input argument unit is missing.)
[in]unit: The input scalar integer of default kind IK containing the connected file unit, whose number of records will be returned.
The input unit must be connected prior to calling this procedure.
If the unit is not already connected, specify the file path as the input file argument.
(optional. It must be present if and only if the input argument unit is missing.)
isCountable: The external user-specified function that takes one input scalar character of default kind SK of arbitrary length type parameter containing the most recent record that has been read from the input file.
It returns a scalar logical of default kind LK that is .true. if and only if the input record should be included in counting.
The following illustrates the generic interface of isCountable,
function isCountable(record) result(countable)
use pm_kind, only: LK, SK
character(*, SK), intent(in) :: record
logical(LK) :: countable
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This user-defined countability check is extremely useful where certain records in the file ought to be excluded from counting or only records with specific patterns should be included (for example, excluding or including only records that are empty or begin with or end with or have specific patterns).
(optional. If missing, all records are included in counting.)
[in]del: The input scalar logical of default kind LK.
  1. If .true., the file will be deleted upon return.
  2. If .false., the file will only be closed (without deletion) upon return.
(optional, default = .false.)
[out]iostat: The optional output scalar integer of default kind IK.
  1. If present and no error occurs, it is set to 0 on output.
  2. If present and an error occurs (e.g., if the input argument values are wrong or inconsistent), it is set to a positive non-zero value.
  3. If missing and an error occurs, then the program halts by calling error stop followed by the relevant error message.
(optional.)
[in,out]iomsg: The input/output scalar character of default kind SK containing the error message, if any error occurs.
A length type parameter value of LEN_IOMSG is generally sufficient for iomsg to contain the output error messages.
(optional. Its presence is relevant only if the iostat output argument is also present.)
Returns
nrecord : The output scalar integer of default kind IK, representing the number of records in the input file.


Possible calling interfaces

nrecord = getCountRecord(file, isCountable = isCountable, del = del, iostat = iostat, iomsg = iomsg) ! file must not be connected.
nrecord = getCountRecord(unit, isCountable = isCountable, del = del, iostat = iostat, iomsg = iomsg) ! unit must be already connected. `rewind(unit)` will occur.
Generate and return the number of records in the entire record-oriented input file.
Definition: pm_io.F90:1502
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
Warning
The input file must not be connected prior to calling this procedure.
The input unit must be already connected prior to calling this procedure.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
Remarks
The procedures under discussion are impure.
See also
getCountRecordLeft
isPreconnected
getFileUnit


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_io, only: display_type
5 use pm_io, only: getCountRecord
6
7 implicit none
8
9 integer(IK) :: iostat
10 character(255, SK) :: iomsg
11
12 type(display_type) :: disp
13
14 disp = display_type(file = "main.out.F90")
15
16 call disp%skip
17 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
18 call disp%show("!Count the number of records in the record-oriented (sequential access) `main.F90` file.")
19 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
20 call disp%skip
21
22 iomsg = repeat(" ", len(iomsg))
23
24 call disp%skip
25 call disp%show("getCountRecord(file = SK_'main.F90')")
26 call disp%show( getCountRecord(file = SK_'main.F90') )
27 call disp%skip
28
29 call disp%skip
30 call disp%show("getCountRecord(file = SK_'main.F90', iostat = iostat, iomsg = iomsg)")
31 call disp%show( getCountRecord(file = SK_'main.F90', iostat = iostat, iomsg = iomsg) )
32 call disp%show("iostat")
33 call disp%show( iostat )
34 call disp%show("trim(adjustl(iomsg))")
35 call disp%show( trim(adjustl(iomsg)) , deliml = SK_"""" )
36 call disp%skip
37
38 call disp%skip
39 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
40 call disp%show("!Exclude certain records that match the user-specified behavior via isCountable.")
41 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
42 call disp%skip
43
44 call disp%skip
45 call disp%show("getCountRecord(file = SK_'main.F90', isCountable = isCountable)")
46 call disp%show( getCountRecord(file = SK_'main.F90', isCountable = isCountable) )
47 call disp%skip
48
49contains
50
51 function isCountable(record) result(countable)
52 character(*, SK), intent(in) :: record
53 logical(LK) :: countable
54 countable = index(record, SK_"call disp%skip", kind = IK) == 0_IK
55 end function
56
57end program example
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Count the number of records in the record-oriented (sequential access) `main.F90` file.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7getCountRecord(file = SK_'main.F90')
8+56
9
10
11getCountRecord(file = SK_'main.F90', iostat = iostat, iomsg = iomsg)
12+56
13iostat
14+0
15trim(adjustl(iomsg))
16" "
17
18
19!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20!Exclude certain records that match the user-specified behavior via isCountable.
21!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22
23
24getCountRecord(file = SK_'main.F90', isCountable = isCountable)
25+46
26
27
Test:
test_pm_io
Todo:
This procedure can be converted to a generic interface to add the optional iseq() external comparison procedures for custom exclusion or inclusion of lines in the count.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Tuesday March 7, 2017, 3:50 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 1502 of file pm_io.F90.


The documentation for this interface was generated from the following file: