Silverfrost Forums

Welcome to our forums

Error Argument of ichar at (1) must be of length one

12 May 2018 4:06 #22113

Hi all, I have an f90 code which when compile gives the following error: Error Argument of ichar at (1) must be of length one The lines are: [!Flag for NaN values E_det=-1D6 DO j=M_det,1,-1 DO i=1,N_det read(7,*) E_det_strg(i,j) n_strg=ICHAR(E_det_strg(i,j))] and !Skips NaN (FA no-data points) n_strg=ICHAR(FA_strg(i,j)) IF ((n_strg<=47.OR.n_strg>57).AND.n_strg/=45) CYCLE How can I resolve this? Thank you.

12 May 2018 6:58 #22114

The intrinsic function ICHAR takes as argument a character of length 1. Your code seems to be passing instead a REAL or double precision argument.

How to fix? That depends on what is intended to be done with the code. The fragment that you showed does not convey enough information to permit one to give a recommendation.

13 May 2018 12:40 #22115

Thanks mecej4. The code is from a published article (Fullea et al., 2008. FA2BOUG—A FORTRAN 90 code to compute Bouguer gravity anomalies from gridded free-air anomalies). Lines 181 and 260 contains ICHAR. If you don't mind, I can email it. I truncated some part due to the length of the text.

C************************************
163      DO j=M,1,-1
164       DO i=1,N
165        read(2,*) a11,a11,E(i,j)
166
167        read(5,*) a11,a11,FA_strg(i,j)
168
169       ENDDO
170      ENDDO
171
172       REWIND (2)
173!Read the detailed topography file (if present) 
174      IF (det==1) THEN
175      call cpu_time(time1)
176!Flag for NaN values
177       E_det=-1D6
178       DO j=M_det,1,-1
179        DO i=1,N_det
180         read(7,*) E_det_strg(i,j)
181         n_strg=ICHAR(E_det_strg(i,j))
182!Skips NaN and store only the numerical values (i.e. points
  
!Elevation of the calculation point and parameters
248!for the Intermediate zone
249        IF (det==1.AND.det_on) THEN
250         h=E_det(i_det,j_det)
251        ELSE
252         h=E(i,j)
253         IWIN_i=nint(R_i/del_xi)
254         rt_i=1
255         rad=sqrt(2.)*del_xi*1D3/2
256        ENDIF
257!Skips land calculation if requiered
258        IF (h>0.AND.land=='off') CYCLE
259!Skips NaN (FA no-data points)
260        n_strg=ICHAR(FA_strg(i,j))
261        IF ((n_strg<=47.OR.n_strg>57).AND.n_strg/=45) CYCLE
262!Convert string to real
263        READ(FA_strg(i,j),*)FA(i,j)
264
265        IF (h<=0) THEN 
266        rho=rho_c-rho_w
267        nu=-h/(R_t*1D3+h)
268        zm=0D0
269        ELSE
270        rho=rho_c
271        nu=h/(R_t*1D3+h)
272        zm=h
273        ENDIF
13 May 2018 5:41 #22116

At first sight E_det_strg appears to be an array of CHARACTER variables and the line should possibly be...

n_strg=ICHAR(E_det_strg(i,j)(1:1)

But it might be a logical error in the program. As it is, n_strg is the ASCII value of the first character of element (i,j) of the array. Maybe it is intended to be something else. You will need to trace the value of n_strg through the program to see what it does.

ICHAR('0') is 48 whilst ICHAR('9') is 57. So the above seems about right.

13 May 2018 8:54 #22117

Geomarine, your code extract still leaves out the declarations of the variables that occur in the line flagged by the compiler as erroneous.

However, if Paul's guess about E_det_strg is correct, the code is in error since the argument of the ICHAR intrinsic must be a character variable of length 1. The exclusion of unsuitable strings can be done with the following code, as well, which is a bit more readable.

program xbug
   implicit none
   character(4), dimension(2,3) :: E_det_strg
   real, dimension(2,3) :: FA
   integer :: i,j
   character :: c1
do i=1,2
   do j=1,2
      c1=E_det_strg(i,j)(1:1)   ! first character of string
      if((llt(c1,'0').and.c1.ne.'-').or.lgt(c1,'9'))cycle
      read(E_strg(i,j),*)FA(i,j)
   end do
end do
end program xbug

You are welcome to upload a zip file of the source code to a cloud server such as Dropbox, Google Drive, etc., and post a public link to the zip file here.

13 May 2018 9:04 (Edited: 13 May 2018 10:36) #22118

Geomarine, your code extract still leaves out the declarations of the variables that occur in the line flagged by the compiler as erroneous.

However, if Paul's guess about E_det_strg is correct, the code is in error since the argument of the ICHAR intrinsic must be a character variable of length 1. The exclusion of unsuitable strings (for formatted READ from a string into a real variable) can be done with the following code, as well, which is a bit more readable.

program xbug
   implicit none
   character(4), dimension(2,3) :: E_det_strg
   real, dimension(2,3) :: FA
   integer :: i,j
   character :: c1
do i=1,2
   do j=1,2
      c1=E_det_strg(i,j)(1:1)   ! first character of string
      if((llt(c1,'0').and.c1.ne.'-').or.lgt(c1,'9'))cycle
      read(E_strg(i,j),*)FA(i,j)
   end do
end do
end program xbug

Instead of the ICHAR intrinsic, the LLT and LGT intrinsics have been used.

You are welcome to upload a zip file of the source code to a cloud server such as Dropbox, Google Drive, etc., and post a public link to the zip file here.

13 May 2018 9:48 #22119

Thanks PaulLaidler and mecej4. The link to the file on google drive is here: https://drive.google.com/drive/folders/1L_CgDnx6KsIkOIpfPMQmKoKMAyO5Fna2?usp=sharing

13 May 2018 10:35 (Edited: 14 May 2018 3:30) #22120

There are two places where ICHAR is used with an argument of length other than 1: lines 166 and 243. The program may have been developed and used with compilers that allowed such sloppy usage. Either of the fixes suggested earlier can be used.

Gfortran says: $gfortran -ffixed-form *.f95 -o main main.f95:166:22:

          n_strg=ICHAR(E_det_strg(i,j))
                      1
Error: Argument of ichar at (1) must be of length one
main.f95:243:21:

         n_strg=ICHAR(FA_strg(i,j))
                     1
Error: Argument of ichar at (1) must be of length one

There are numerous implicitly typed real variables and they are used together with double precision variables. Unless further examination of the code shows otherwise, it should be assumed that the final results are only single-precision quality, even for variables declared as double-precision.

13 May 2018 11:45 #22121

The use of ICHAR appears to test if the first character of the string read starts with a number. An alternative could be to read the string as a number and use iostat=iostat or test if the character field is a known non numeric, say 'NaN'

The following read may be a better test for a valid number. READ(E_det_strg(i,j),*, iostat=iostat) E_det(i,j) if (iostat /= 0 ) cycle

There are other problems with this code, especially the use of file unit numbers 1,2 and 5 and some of the if constructs. 'REAL(8), PARAMETER :: pi=3.14159' is also questionable ( I prefer pi = 4*atan(1.0d0) )

I would check with the authors to finds out if there is a revised version of this program

Please login to reply.