What is strlen actually doing? - fortran

This is subroutine strlen
subroutine strlen(string,i1,i2)
character string*(*)
do 10 i=len(string),1,-1
if(string(i:i).ne.' ')then
i2=i
goto 11
endif
10 continue
11 do 20 i=1,len(string)
if(string(i:i).ne.' ')then
i1=i
return
endif
20 continue
return
end
In the main
if(nfil.lt.0)then
call strlen(cpar,i1,i2)
open(unit=10,file=cpar(i1:i2),status='old')
If cpar is declared
character cpar*(*)
Does it exist any rule or limitation regarding the name of the cpar to be openned properly?
My hx263on.sens(cpar) can not be read.Why?

The function just looks for the first and the last character which is not a space. These are returned as i1 and i2. cpar(i1:i2) should be the same as adjustl(trim(cpar)) from Fortran 90.
This should not be necessary. Fortran trims file names passed to open anyway. File-names ending with spaces are not supported.
If the file cannot be opened, it probably does not exist or contains some special characters which the operating system does not like. Or it actually contains something else than you think. Maybe the variable is too short or something like that. Verify the value of cpar and cpar(i1:i2) by printing it.

Related

How do I skip lines when some conditions are met with Fortran? [duplicate]

It is my understanding that Fortran, when reading data from file, will skip lines starting with and asterisk (*) assuming that they are a comment. Well, I seem to be having a problem with achieving this behavior with a very simple program I created. This is my simple Fortran program:
1 program test
2
3 integer dat1
4
5 open(unit=1,file="file.inp")
6
7 read(1,*) dat1
8
9
10 end program test
This is "file.inp":
1 *Hello
2 1
I built my simple program with
gfortran -g -o test test.f90
When I run, I get the error:
At line 7 of file test.f90 (unit = 1, file = 'file.inp')
Fortran runtime error: Bad integer for item 1 in list input
When I run the input file with the comment line deleted, i.e.:
1 1
The code runs fine. So it seems to be a problem with Fortran correctly interpreting that comment line. It must be something exceedingly simple I'm missing here, but I can't turn up anything on google.
Fortran doesn't automatically skip comments lines in input files. You can do this easily enough by first reading the line into a string, checking the first character for your comment symbol or search the string for that symbol, then if the line is not a comment, doing an "internal read" of the string to obtain the numeric value.
Something like:
use, intrinsic :: iso_fortran_env
character (len=200) :: line
integer :: dat1, RetCode
read_loop: do
read (1, '(A)', isostat=RetCode) line
if ( RetCode == iostat_end) exit ReadLoop
if ( RetCode /= 0 ) then
... read error
exit read_loop
end if
if ( index (line, "*") /= 0 ) cycle read_loop
read (line, *) dat1
end do read_loop
Fortran does not ignore anything by default, unless you are using namelists and in that case comments start with an exclamation mark.
I found the use of the backspace statement to be a lot more intuitive than the proposed solutions. The following subroutine skips the line when a comment character, "#" is encountered at the beginning of the line.
subroutine skip_comments(fileUnit)
integer, intent(in) :: fileUnit
character(len=1) :: firstChar
firstChar = '#'
do while (firstChar .eq. '#')
read(fileUnit, '(A)') firstChar
enddo
backspace(fileUnit)
end subroutine skip_comments
This subroutine may be used in programs before the read statement like so:
open(unit=10, file=filename)
call skip_comments(10)
read(10, *) a, b, c
call skip_comments(10)
read(10, *) d, e
close(10)
Limitations for the above implementation:
It will not work if the comment is placed between the values of a variable spanning multiple lines, say an array.
It is very inefficient for large input files since the entire file is re-read from the beginning till the previous character when the backspace statement is encountered.
Can only be used for sequential access files, i.e. typical ASCII text files. Files opened with the direct or append access types will not work.
However, I find it a perfect fit for short files used for providing user-parameters.

Fortran "Cannot assign to a named constant at (1)" in if statement

Here's what I'm trying to run:
if (z.le.zstart) then
if (y.ge.((6.95*wg_y2)/5)).and.(y.le.((12.55*wg_y2)/5)) then
indexmedia=nd
end if
end if
For context,
zstart is an arbitrary line of constant y.
wg_y2 = 5e-6
And for some reason it doesn't like the (y.ge.((6.95*wg_y2)/5)) bit, as the (1) was placed at the end of that bit.
I had a suspicion that it doesn't like anything that isn't an integer in the if statement but I changed 6.95 to 7 and 12.55 to 13 and it still didn't like it. Perhaps it needs to end up resolving to an integer?
I need these parameters to end up to
6.95 < y < 12.55 though.
Is there a workaround for this?
When compiling the code (not nice as I didn't declare the variables!, but that isn't the problem here so I abstain from it to keep it a bit small):
program tst
if (z.le.zstart) then
if (y.ge.((6.95*wg_y2)/5)).and.(y.le.((12.55*wg_y2)/5)) then
indexmedia=nd
end if
end if
end
one gets the error:
aa.f90:3:29:
if (y.ge.((6.95*wg_y2)/5)).and.(y.le.((12.55*wg_y2)/5)) then
1
Error: Cannot assign to a named constant at (1)
aa.f90:6:3:
end if
1
Error: Expecting END PROGRAM statement at (1)
This is due to a missing pair of round brackets in the line:
if (y.ge.((6.95*wg_y2)/5)).and.(y.le.((12.55*wg_y2)/5)) then
which should read
if ((y.ge.((6.95*wg_y2)/5)).and.(y.le.((12.55*wg_y2)/5))) then

unclassifiable statement at (1) Fortran Error

at line 64 confirmed as the errors began
I was asked to modify a legacy code. I have found these errors when compiling error compiling picture . Does anybody know how to solve these errors? I use gfortran as my compiler.
The source code:
* Spectral analysis of measured data *
PARAMETER (ND=86400,NSP=43200,NND=86400)
COMMON /WDATA/ WD(NND),WD2(NND)
COMMON /SPEC/ WSP(NSP)
COMMON /TSDATA/ TS(ND*2),CTTS(ND*2)
COMMON /SPDATA/ P(NSP),DF
REAL MEAN
DATA DT/1.0/
DATA COTL/14400.0/
DATA COTS/600.0/
PI=3.141593
OPEN(32,FILE="Pw.txt",STATUS="OLD")
OPEN(12,FILE="output1",STATUS="UNKNOWN")
OPEN(13,FILE="output2",STATUS="UNKNOWN")
DO J=1,NND
READ(32,*)WD(J)
END DO
TOTAL=0.0
MEAN=0.0
DO J=1,NND
TOTAL=TOTAL+WD(J)
END DO
MEAN=TOTAL/FLOAT(NND)
DO J=1,NND
WD(J)=WD(J)-MEAN
END DO
Numerical filtering and Spectral analysis
M=ND/2
KF=1
TD=DT*FLOAT(ND)
DF=1./TD
DO J=1,ND
TS(J)=WD(J)
TS(J+ND)=0.
END DO
COFL=1./COTL
COFH=1./COTS
NCUTL=IFIX((COFL+DF/2.)/DF)+1
NCUTH=IFIX((COFH-DF/2.)/DF)+1
=========================
CALL CUTOFF(M,NCUTL,NCUTH)
=========================
DO J=1,ND
WD2(J)=CTTS(J)
END DO
=================================
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
=================================
PARAMETER(ND=86400,NSP=43200)
COMMON /TSDATA/ TS(ND*2),CTTS(ND*2)
COMMON /FFTDATA/ W1(ND*2)
MM=M+M
M4=MM+MM
DO J=1,MM
W1(2*J-1)=TS(J)
W1(2*J)=TS(J+MM)
END DO
===============
CALL FOUR1(MM,1)
===============
DO J=1,M
IF(J.EQ.1.AND.NCUTL.GT.0)THEN
W1(1)=0.
W1(2)=0.
ELSE IF(J.LT.NCUTL)THEN
W1(2*J-1)=0.
W1(2*J)=0.
W1(M4-2*J+3)=0.
W1(M4-2*J+4)=0.
END IF
IF(J.GT.NCUTH)THEN
W1(2*J-1)=0.
W1(2*J)=0.
W1(M4-2*J+3)=0.
W1(M4-2*J+4)=0.
END IF
IF(NCUTH.GT.M) THEN
W1(MM+1)=0.
W1(MM+2)=0.
END IF
END DO
-----------------
CALL FOUR1(MM,-1)
-----------------
DO I=1,MM
CTTS(I)=W1(2*I-1)/FLOAT(MM)
CTTS(I+MM)=W1(2*I)/FLOAT(MM)
END DO
RETURN
END
==========================
SUBROUTINE FOUR1(NN,ISIGN)
==========================
PARAMETER(ND=86400)
REAL*8 WR,WI,WPR,WPI,WTEMP,THETA
COMMON /FFTDATA/ DATA(ND*2)C
N=2*NN
J=1
DO 11 I=1,N,2
IF(J.GT.I) THEN
TEMPR=DATA(J)
TEMPI=DATA(J+1)
DATA(J)=DATA(I)
DATA(J+1)=DATA(I+1)
DATA(I)=TEMPR
DATA(I+1)=TEMPI
ENDIF
M=N/2
1 IF ((M.GE.2).AND.(J.GT.M)) THEN
J=J-M
M=M/2
GO TO 1
ENDIF
J=J+M
11 CONTINUE
MMAX=2
2 IF (N.GT.MMAX) THEN
ISTEP=2*MMAX
THETA=6.28318530717959D0/(ISIGN*MMAX)
WPR=-2.D0*DSIN(0.5D0*THETA)**2
WPI=DSIN(THETA)
WR=1.D0
WI=0.D0
DO 13 M=1,MMAX,2
DO 12 I=M,N,ISTEP
J=I+MMAX
TEMPR=SNGL(WR)*DATA(J)-SNGL(WI)*DATA(J+1)
TEMPI=SNGL(WR)*DATA(J+1)+SNGL(WI)*DATA(J)
DATA(J)=DATA(I)-TEMPR
DATA(J+1)=DATA(I+1)-TEMPI
DATA(I)=DATA(I)+TEMPR
DATA(I+1)=DATA(I+1)+TEMPI
12 CONTINUE
WTEMP=WR
WR=WR*WPR-WI*WPI+WR
WI=WI*WPR+WTEMP*WPI+WI
13 CONTINUE
MMAX=ISTEP
GO TO 2
ENDIF
RETURN
END
You haven't closed the main program with an end statement before the subroutine cutoff statement
DO J=1,ND
WD2(J)=CTTS(J)
END DO
=================================
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
=================================
This should read something like
DO J=1,ND
WD2(J)=CTTS(J)
END DO
END
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
however that doesn't really make sense. I'm sure there are more missing lines. There are also many illegal statements in the code presented mostly due to bad formatting as Vladimir F has noted.
Muhajjir,
If gfortran is anything like fortran used to be, this code will generate a
plethora of errors. One thing is for sure, there does HAVE to be and END statment at the end of the main. Otherwise, the compiler gets VERY confused. In
addition, some of your statments appear to have code to the left of column 7.
Remember, fortran dates from the days of IBM punch cards, which were HIGHLY
column oriented. A capital 'c' was typically used, in column 1, to indicate a
comment. Column 6 was reserved for continuation character, which eventually
became any character you wanted, as long as column 6 was not empty. Numbers used as labels HAD to start in column 1 and could not go past column 5 into 6, or beyond. This code looks like many of these basic rules have been violated. Check everything, straighten it all out, and everythng should be fine. If not, we can go from there.
Blake Mitchell
Retired free lance programmer
P.S. To whom it may concern,
I have read through the how to answer link and this answer appears to fit the
requirements perfectly. Why do you think it doesn't?

Unexpected end of file with the read command

I'm trying to read data from a mesh file in Fortran 2003, but I'm getting an unexpected end of file runtime error. Some lines in the file seem to be skipped by the read command. For example, with this sample.txt file :
1 2 2 0 1 1132 1131 1165
2 2 2 0 2 1099 1061 1060
I want to read the first integer from each line, so my program is :
program read_file
implicit none
integer :: ierr, i, j
open(unit=10,file='sample.txt',status='old',action='read',iostat=ierr)
read(10,*) i
read(10,*) j
write(*,*) i, j
end program read_file
And at runtime, I'm getting
Fortran runtime error: End of file
What is odd is that if I force a carriage return at the end of the file, the program will read the two integers just fine.
If you really need to fix this on the read side (ie. properly terminating the last line of the file is not practical for some reason ) you might try reading each line into a string, then internal reading from the string:
character*80 line
integer i
do ..
read(unit,'(a)')line
read(line,*)i
enddo
Of course this may or may not work depending on the compiler as well..
Obviously fixing the file is the best option ( Whatever program is creating this file should be fixed )
Every record in a sequential file must be properly terminated. The records in text files are the lines. They must be properly terminated. In some editors that means you must add an empty line to the end. Every line containing data must be terminated.
Some compilers are less sensitive to this issue than others and will terminate the last record for you.

Interpolate string length into fortran format specifier

I am trying to compile someone else's FORTRAN code using gfortran 4.4.6. The original coder used something like Compaq FORTRAN.
The code in question is supposed to read a filename like 'foo.txt' and create a new file called 'foo_c.txt':
file_label_end = SCAN(filename, '.') - 1
WRITE (output_filename,5) filename
5 FORMAT (A<file_label_end>, '_c.txt' )
gfortran complains about the opening angle bracket of the character specifier. That is, rather than "A3" he has "A<(a variable with value 3)>". I cannot find any information about interpolating format widths from variables... Is this possible? If not, what's the best way to fix this?
Update:
This seems to be working (compiling):
file_label_end = SCAN(par_filename, '.', .TRUE. ) + 1
output_filename = par_filename(1:file_label_end) // '_c.par'
but I later have a similar case:
12 FORMAT (<n-1>F10.5)
...
READ(1,12) (cat_parm (blk,j), j = 1,n-1)
which I attempted to fix by creating a format string:
write(fmt12,'(I0,A4)') n-1, 'F10.5'
!12 FORMAT (fmt12)
12 FORMAT (fmt=fmt12)
But the "t" in "fmt" gets flagged with an error about "Nonnegative width required in format string"
The use of <> in Fortran edit descriptors is non-standard, though widely implemented. You certainly can build edit descriptors from variables, the standard way is through an internal write, something like this (omitting declarations):
format_string = ''
write(format_string,'(a1,i0,a6)') 'A', file_label_end,'''_c.txt''' ! WARNING I haven't tested this
and then
write(output_filename,fmt=format_string) filename
But in your case this is not entirely necessary. You could try this instead
file_label_end = SCAN(filename, '.') - 1
WRITE (output_filename,'(a)') filename(1:file_label_end)//'_c.txt'
With the 'a' edit descriptor omitting a width means that all the characters in the expression will be written. You may want to declare
character(len=*), allocatable :: output_filename
if you haven't already done so.
The easiest is to use modern fortran
WRITE (output_filename,'(a)') trim(filename) // '_c.txt'
Otherwise I would write the number in the format string using a similar internal write.