I have a short bit of code that is reading in a CSV file. The CSV file is very simple, just an array of floating point numbers. There is no header and each column has the same type of information, so I don’t need to do any sort of FORMAT statement for the read. I have pasted the entire code at the end of this message.
I use Visual Studio Code (vscode) as an editor/IDE. This includes using the Modern Fortran extension (version 3.2.0) provided by https://fortran-lang.org.
Here’s the problem, of sorts. With the Modern Fortran extension running, the extension gives me a warning for two of my variables (max_rows and max_cols, see the code below). Here’s what the warning looks like:
Variable “max_rows” masks variable in parent scope
Variable “max_cols” masks variable in parent scope
Note: it is true that for this code example, I don’t need to pass these variables into the subroutine. However, my plans to add to this code are such that it is better to first introduce these variables a tthe program level (e.g., I will use these same variables in other subroutines).
Why am I getting this warning?
After searching the web, I haven’t found a clear explanation (particularly for a non-CS person like myself). I use an intent(in)
when declaring the variables in the subroutine, which I thought would help avoid issure like this.
(I should note that the code/program works. I am just trying to understand the warning.)
If anyone has any insight or an explanation, I would appreciate it. Or just point me to a good explanation.
Thanks!
Here’s the code:
program main
! The unkillable implicit statement.
implicit none
! The naming scheme for variables in this program:
! tp = titanpool
! vap = vapor
! cp = heat capacity
! liq = liquid
! den = density
character(len=100) :: &
! File with N2 saturation vaporization values from TitanPOOL.
tp_n2_sat_vap_file, &
! File with the N2 heat capacity (cp) values from TitanPOOL.
tp_n2_sat_cp_file, &
! File with the N2 saturation liquidization values from TitanPOOL.
tp_n2_sat_liq_file, &
! File with the values for the density of liquid(?) N2 at saturation.
tp_n2_sat_den_file
! Define the maximum size of the matrix
integer :: max_rows
integer :: max_cols
! Define the matrices to store the data.
real, dimension(1000,1000) :: tp_n2_sat_comp_vap
real, dimension(1000,1000) :: tp_n2_sat_comp_cp
real, dimension(1000,1000) :: tp_n2_sat_comp_liq
real, dimension(1000,1000) :: tp_n2_sat_comp_den
! Assign the file names
tp_n2_sat_vap_file = "Titan_N2_saturation_comp_vap.csv"
tp_n2_sat_cp_file = "Titan_N2_saturation_comp_Cp.csv"
tp_n2_sat_liq_file = "Titan_N2_saturation_comp_liq.csv"
tp_n2_sat_den_file = "Titan_N2_saturation_comp_den.csv"
! Set the value of the maximum size of the matrix.
max_rows = 42
max_cols = 64
! Read the input files
call read_input_file(tp_n2_sat_vap_file, tp_n2_sat_comp_vap, &
max_rows, max_cols)
call read_inputfile(tp_n2_sat_cp_file, tp_n2_sat_comp_cp, &
max_rows, max_cols)
call read_inputfile(tp_n2_sat_liq_file, tp_n2_sat_comp_liq, &
max_rows, max_cols)
call read_inputfile(tp_n2_sat_den_file, tp_n2_sat_comp_den, &
max_rows, max_cols)
contains
subroutine read_input_file(file_path, data_matrix, max_rows, max_cols)
character(len=*), intent(in) :: file_path
integer :: i
integer, intent(in) :: max_rows
integer, intent(in) :: max_cols
real, dimension(max_rows,max_cols), intent(inout) :: data_matrix
! Open the input file
open(unit=10, file=file_path, status='old', action='read')
! Read the data into the matrix
do i = 1, max_rows
read(10, *) data_matrix(i,:)
!write(*,*) data_matrix(i,:)
end do
! Close the input file
close(10)
end subroutine read_input_file
end program main