
forums.silverfrost.com Welcome to the Silverfrost forums

View previous topic :: View next topic 
Author 
Message 
geomarine
Joined: 07 May 2018 Posts: 6

Posted: Sat May 12, 2018 5:06 am Post subject: Error Argument of ichar at (1) must be of length one 


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 nodata 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. 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Sat May 12, 2018 7:58 am Post subject: 


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. 

Back to top 


geomarine
Joined: 07 May 2018 Posts: 6

Posted: Sun May 13, 2018 1:40 am Post subject: 


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 freeair 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.
Code: 
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 nodata 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_crho_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



Back to top 


PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 6400 Location: Salford, UK

Posted: Sun May 13, 2018 6:41 am Post subject: 


At first sight E_det_strg appears to be an array of CHARACTER variables and the line should possibly be...
Code:  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. 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Sun May 13, 2018 9:54 am Post subject: 


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.
Code: 
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. 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Sun May 13, 2018 10:04 am Post subject: Re: 


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.
Code: 
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.
Last edited by mecej4 on Sun May 13, 2018 11:36 am; edited 1 time in total 

Back to top 


geomarine
Joined: 07 May 2018 Posts: 6


Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Sun May 13, 2018 11:35 am Post subject: 


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: Code:  $gfortran ffixedform *.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 singleprecision quality, even for variables declared as doubleprecision.
Last edited by mecej4 on Mon May 14, 2018 4:30 pm; edited 2 times in total 

Back to top 


JohnCampbell
Joined: 16 Feb 2006 Posts: 2184 Location: Sydney

Posted: Sun May 13, 2018 12:45 pm Post subject: 


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 

Back to top 




You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
