Fortran read function does not read the last value - fortran

I have a input file (filename: L0analysis) like below
...... !headers
3 2.1 2.4 2.5 !ntfmr rcut(1) rcut(2) rcut(3)
...... !other data
The index i in rcut(i) is determined by ntfmr
in my program,
......
ncrit=55
open(ncrit,file='Lanalysis')
......!(read the headers. skipped here)
read(ncrit,*) ntfmr, (rcut(i),i=1,ntfmr)
do i=1,ntfmr ! # of network formers and their cutoffs
rcut2(i)=rcut(i)*rcut(i)
write(*,*)'ntfmr',ntfmr,'i=',i,rcut(i)
enddo
.......
.......
My output is
ntfmr 3 i= 1 2.10000000000000
ntfmr 3 i= 2 2.40000000000000
ntfmr 3 i= 3 4.41000000000000
I tried to initialize rcut(i)=0. It does not work. I cannot find any place to change this value.
I am unable to figure it out what is going wrong. Any suggestion or comment will be highly appreciated.

Related

Reading records from a file in FORTRAN66 using stdin adding extra unwanted junk

I'm trying to read a file in the format specified below using FORTRAN 66.
1000
MS 1 - Join Grps Group Project 5 5
Four Programs Programming 15 9
Quiz 1 Quizzes 10 7
FORTRAN Programming 25 18
Quiz 2 Quizzes 10 9
HW 1 - Looplang Homework 20 15
I execute and read the file like so:
program < grades.txt
The first line is the total number of points that can be earned in a class
The rest of the lines are assignments in a class
Each line is formatted as such: Assignment name(20 chars) category (20 chars) possible points(14 chars) earned points(14 chars)
For some reason, when the code runs and reads the file, starting at the first assignment record, I get error 5006, and cannot find an explanation of the error code. The output of the program while debugging looks like this:
$ file < grades.txt
MS 1 - Join Grps Group Project 5 6417876
NOT EOF
EOF 5006
NAME CATEGORY POSSIBLE EARNED
My goal is to be able to read each line and put each column into it's appropriate array, then reference those arrays later on to print a report for each category, with each assignment, points possible, earned, and total percentage for the category, then loop, etc.
I do not understand where the "6417876" in the output is coming from, it is definitely not part of the file that's being piped into stdin while the program reads.
The code for the program is as follows:
CHARACTER*20 ASSIGNMENTT(100)
CHARACTER*20 CATEGORY(100)
INTEGER POSSIBLE(100)
INTEGER EARNED(100)
INTEGER TOTALPTS
INTEGER REASON
INTEGER I, N
READ(5,50)TOTALPTS
50 FORMAT(I4)
c Read the arrays in
I=1
100 READ(5,110,IOSTAT=REASON)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
110 FORMAT(2A20x,2I14x)
WRITE(*,110)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
I=I+1
IF (REASON < 0) GOTO 120
WRITE(*,*)"NOT EOF"
IF (I<100 .AND. REASON == 0) GOTO 100
WRITE(*,*)"EOF", REASON
c Get the number of items (For some reason stdin adds an extra item that's not in the file, so I subtract 2 instead of 1
120 N=I-2
c Display the Names and Ages
WRITE(*,200)
200 FORMAT("NAME",T20,"CATEGORY",T40,"POSSIBLE",T54,"EARNED",T68)
DO 300 I=1,N
210 FORMAT(A20,A20,I14,I14)
300 WRITE(*,210)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
END
What could be causing the read issues I'm facing?
The line to read the file contents was too long, so I shortened the names of the variables to save some space and the problem was solved.

Writing a Matrix Array of 2 Rows by 3 Columns, To an Output Text File in Fortran 95

I'm currently learning how to write Matrix Arrays to output Text Files in Fortran 95. The problem I'm facing is that, the Matrix Array of 2 Rows by 3 columns I'm working on, is not formatting to what I desire in the Output Text File. I believe, I'm missing One or Two Lines of Codes or failing to add a few codes to the current Lines of Codes I have. Below are My Lines of Codes, Current Output Data and Desired Output Data. The Goal is to get the "Desired Output Data". Kindly show me My mistake(s), what codes/line(s) of codes I'm missing and where I should add the codes/line(s) of codes. Every answer is welcomed and appreciated. Thank you Stackovites.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,3,5,2,4,6/
Do k= 1,2
B(2,3)= k
!Write to File at Machine 8 and show the formatting in Label 11
Write(8,11) B(2,3)
11 format(3i3)
End Do
Do j= 3,6
B(2,3)= j
!Write to File at Machine 8 and show the formatting in Label 12
Write(8,12) B(2,3)
12 format(3i3)
End Do
End Program Format2
Current Output Data
1
2
3
4
5
6
Desired Output Data
1 3 5
2 4 6
B(2,3) refers only to one particular element of the array. Namely the element with index 2 in the first dimension and index 3 in the other dimension. To refer to a different element use B(i,j) where i and j are integers with the desired index. To refer to the whole array use just B or alternatively B(:,:) for an array section that encompasses the whole array.
So to set the values
do j = 1, 3
do i = 1, 2
B(i,j) = i + (j-1) * 2
end do
end do
and to print them use one of the methods showed in countless duplicates (Print a Fortran 2D array as a matrix Write matrix with Fortran How to write the formatted matrix in a lines with fortran77? Output as a matrix in fortran -- search for more, there will be better ones...) on this site
do i = 1, 2
write(8,'(999i3)') B(i,:)
end do
I've seen My mistakes. The instructions I gave the Fortran Compiler, was the result I got in My Output Text File. I was declaring two-2 Rows of (1,2) and (3,4,5,6); instead of declaring Three-3 Columns of (1,2); (3,4) and (5,6). Below is the correct Lines of Codes to get the Desired Output Data.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,2,3,4,5,6/
!(a)Declare The 1st Two-2 Values You want for k Two-2 Rows, that is (1 and 2)
!(b)Note, doing (a), automatically declares the values in Column 1, that is (1 and 2)
Do k= 1,2
B(2,3)= k
End Do
!(c)Next, Declare, the Four Values You want in Column 2 and Column 3. That is [3,4,5,6]
!(d) Based on (c), We want 3 and 4 in "Column 2"; while "Column 3", gets 5 and 6.
Do j= 3,6
B(2,3)= j
End Do
!Write to File at Machine 8 and show the formatting in Label 13
Write(8,13) ((B(k,j),j= 1,3),k= 1,2)
13 format(3i3)
End Program Format2
The Above Lines of Codes, gives the Desired Output below:
1 3 5
2 4 6

Strange output with Python Lists

I have a dataframe: Outlet_results
it goes something like this
index Calendar year/Week Material Sellthru Qty
0 37.2013 ABC 2
1 38.2913 ABC 7
2 37.2913 BCG 22
3 39.2013 XYZ 5
Now, I wanted a separate list for the Materials and week for further coding.
I used this code for the material list
mat_outlet = list(set(outlet_result['Material']))
It works perfectly and gives me 3 values (ABC, BCG, XYZ)
However, the week list shows a faulty output even though the code is same.
week_outlet_list = list(set(outlet_result['Calendar Year/Week']))
I am getting a list with 4 values
['38.2013', '37.2013', 'Calendar Year/Week', '39.2013']
Why is the string (header) included in the list? Please help me understand this concept.
I am using Python 2.7.... has it got something to do with it?

Fortran read data with different length

I have a Fortran problem. I want to read in data with different length.
they begin with:
<TITLE>University of Wyoming - Radiosonde Data</TITLE>
<LINK REL="StyleSheet" HREF="/resources/select.css" TYPE="text/css">
<BODY BGCOLOR="white">
<H2>08190 Barcelona Observations at 12Z 11 Feb 2015</H2>
<PRE>
-----------------------------------------------------------------------------
PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV
hPa m C C % g/kg deg knot K K K
-----------------------------------------------------------------------------
1012.0 98 14.0 -1.0 36 3.53 0 0 286.2 296.5 286.8
from this point the file is for example 77 lines long. but others have only 55 , and the end is reached when this part comes
</PRE><H3>Station information and sounding indices</H3><PRE>
Station number: 8190
So I think I need a condition which runs out of the do loop?
I get my data with :
wget 'http://weather.uwyo.edu/cgi-bin/sounding?region=europe& TYPE=TEXT%3ALIST&YEAR=2015&MONTH=02&FROM=1112&TO=1112&STNM=08190' -0 data.dat
open(33, file=infilename, form='formatted',&
access='sequential',action='read')
open(34, file=outfilename, form='formatted',&
access='sequential',action='write')
read(33,'(11/)')
do i=1,77
read(33, '(f7.1,2x,i5,2x,a5,2x,a5,4x,a3,3x,f4.2,4x,a3,4x,a3)') pres,height,tmp,tmp_dew,rel_hum,mixing,wind_dir,wind_speed
write(34,'(f7.1,2x,i5,2x,a5,2x,a5,4x,a3,3x,f4.2,4x,a3,4x,a3)') pres,height,tmp,tmp_dew,rel_hum,mixing,wind_dir,wind_speed
end do
close(33)
close(34)
I hope you can help me.
one approach is to read each line as a string, check for the end and process accordingly:
character*1000 wholeline
do while( .true. )
read(33,'(a)')wholeline
if ( index(wholeline,'</PRE>' ).ne.0 )exit
read(wholeline,*)pres,height,tmp,tmp,dew ...
enddo
You can also perhaps more simply just read and exit on an error..
do while( .true. )
read(33,*,err=100)pres,height,tmp,tmp,dew ...
enddo
100 continue
I'm not a fan of deliberately throwing errors if it can be avoided though.
As an aside you do not need that messy format, list directed will work fine for this example. In fact if all you are doing is transferring the data to another file, simply rewrite the string as: write(34,'(a)')trim(wholeline)

Fortran95 -- Reading from a formatted text file

I need to read some values from a table. These are the first five rows, to give you some idea of what it should look like:
1 + 3 98 96 1
2 + 337 2799 2463 1
3 + 2801 3733 933 1
4 + 3734 5020 1287 1
5 + 5234 5530 297 1
My interest is in the first four columns of each row. I need to read these into arrays. I used the following code:
program ----
implicit none
integer, parameter :: totbases = 4639675, totgenes = 4395
integer :: codtot, ks
integer, dimension(totgenes) :: ngene, lend, rend
character :: genome*4639675, sign*4
open(1,file='e_coli_g_info')
open(2,file='e_coli_g_str')
do ks = 1, totgenes
read(1,100) ngene(ks),sign(ks:ks),lend(ks), rend(ks)
end do
100 format(1x,i4,8x,a1, 2(5x,i7), 22x)
do ks = 1, 100
write(*,*) ngene(ks), sign(ks:ks),lend(ks), rend(ks)
end do
end program
The loop at the end of the program is to print the first hundred entries to test that they are being read correctly. The problem is that I am getting this garbage (the fourth row is the problem):
1 + 3 757934891
2 + 337 724249387
3 + 2801 757803819
4 + 3734 757803819
5 + 5234 757935405
Clearly, the fourth column is way off. In fact, I cannot find these values anywhere in the file that I am reading from. I am using the gfortran compiler for Ubuntu 12.04. I would greatly appreciate if somebody would point me in the right direction. I'm sure it's likely that I'm missing something very obvious because I'm new at Fortran.
Fortran formats are (traditionally, there's some newer stuff that I won't go into here) fixed format, that is, they are best suited for file formats with fixed columns. I.e. column N always starts at character position M, no ifs or buts. If your file format is more "free format"-like, that is, columns are separated by whitespace, it's often easier and more robust to read data using list formatting. That is, try to do your read loop as
do ks = 1, totgenes
read(1, *) ngene(ks), sign(ks:ks), lend(ks), rend(ks)
end do
Also, as a general advice, when opening your own files, start from unit 10 and go upwards from there. Fortran implementations typically use some of the low-numbered units for standard input, output, and error (a common choice is units 1, 5, and 6). You probably don't want to redirect those.
PS 2: I haven't tried your code, but it seems that you have a bounds overflow in the sign variable. It's declared of length 4, but then you assign to index ks which goes all the way up to totgenes. As you're using gfortran on Ubuntu 12.04 (that is, gfortran 4.6), when developing compile with options "-O1 -Wall -g -fcheck=all"