Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Fortran programming problem

marma
Beginner
1,350 Views

Dear Sir/Madam

I would be most grateful if you would offer me some advice on a problem I am currently dealing with which is related to Fortran programming.

Suppose I have some measurement data defined according to x-coordinate and y-co-ordinate as in the attached file and I want to have as output a new file in which the first, second and third column represent x-coordinate, y-coordinate and sum of all measurements for that given combination of x- and y-coordinate, respectively. The number of terms in any given sum will not be uniform across combinations of x- and y-coordinates and in practice my data files and number of terms in an individual sum are much bigger.

I would greatly value your suggestions.

Many thanks

Yours faithfully

Margaret

0 Kudos
12 Replies
anthonyrichards
New Contributor III
1,350 Views

Well, there is a simple solution IF, and only IF, all your X and Y co-ordinate values are +ve integers. You can just use them as array indeces and then sum the third number into each array element.

e.g.Read in latest triple of Xcoord, Ycoord and value, then...

Array(latestXcoord,latest Ycoord) = Array(latestxcoord,latestYcoord) + latestvalue

You have the problem of establishing how big your array should be and then initialising each element to zero. You need to know beforehand the maximum range of both X and Y values and declare an array of adequate size.(On the other hand, you could be sneaky and read through the records first to find the maximum X and Y values, then allocate an array of adequate size, then rewind the file and start r eading in the data again).

If you want to graduate to fractional X and Y values, you can use the same method, but basically define 'bins' and use rounding and truncation based on the bin-size to generate integers that identify a bin. E.G suppose you want a bin size of 1.0, so that values from 0-0.99999 fall in one bin and values from 1.000-1.999999 fall in the next bin, then compute Nearest-integer-below[ (X)/1.0 ]+1 as the X-index etc. This can be generalised to Nearest-integer-below[(X)/Bin-size] + 1.

Message Edited by anthonyrichards on 03-18-2004 03:25 AM

Message Edited by anthonyrichards on 03-18-2004 03:27 AM

Message Edited by anthonyrichards on 03-18-2004 03:28 AM

0 Kudos
marma
Beginner
1,350 Views

Hi Anthony

My x and y values should all be positive integers. However, I do not follow your suggestions. Could you kindly offersome Fortran code which would solve the problem for the particular sample file which I attached. This way, I shall have a clearer picture of what to do in a more complicated case.

Many thanks for your patience and yourinterest in my enquiry.

Best wishes

Margaret

0 Kudos
anthonyrichards
New Contributor III
1,350 Views
Ok, here is a solution as a console project...
0 Kudos
marma
Beginner
1,350 Views

Many thanks for the sample solution, Anthony.

The approach was extremely relevant.

Best,

Margaret

0 Kudos
marma
Beginner
1,350 Views

Dear Anthony

Can you kindly modify your program in order that average (rather than total) measurements are calculated as array elements for each x- y- combination?

Many thanks

Best wishes

Margaret

0 Kudos
marma
Beginner
1,350 Views

No need to reply, Anthony as, having found some spare time, I have derived a simple solution.

Would be happy to share it with anyone if requested to.

Best,

Margaret

0 Kudos
anthonyrichards
New Contributor III
1,350 Views

I willgive some guidance, but otherwise I think I will, as they say, 'leave it as an exercise for the reader'!

You need to define an integer array with the same size as 'xyarray'whichwill keeptrack of the number of readings with the same X-Y coordinates. Call it 'ixytotals'. You also need to define an array to store the averages; call it 'xyave', say.

Initialise 'ixytotals' to unity values in the same loop as you initialise 'xyarray' and similarly, initialise 'xyave' to zeros.

After the line

xyarray(ixcoord,iycoord)=xyarray(ixcoord,iycoord)+measurement

add the following:

ixytotals(ixcoord,iycoord)=ixytotals(ixcoord,iycoord)+1

then you must somewhere have aloop to divide 'xyarray' values by non-zero 'ixytotals' values to give you 'ixyave' values. There is already a loop used to output the values to a table, so just add the required code before the 'write(6,110) i,j,xyarray(i,j) ' statement. Make sure you test 'ixytotals' value is non-zero before doing the divide.

Then all you have to do is (1) modify the Format statement 100 to add an 'average' heading after 'Total measurement' , (2) add ', xyave(i,j) ' to the WRITE statements, and(3) add another 'F10.3' to the Format statement 110.

You could also addthe 'ixytotals' to the output in the same way.

0 Kudos
larsm
Beginner
1,350 Views
I feel I must reply to this one.
This is the kind of problem that I think one should have tools around for to be able to solve fast.
I have been making tools in Fortran for these kinds of problems for many years.
One of the tools is a matrix module that we use here at Volvo Aero/IT.
I thought it might be interesting to show what you can do in modern Fortran when it comes to building tools that takes you away from the tedious details such as reading/writing from known formats, automatic dimensioning of matrices, a number of common functions such as reductions, compressions, aggregations, sets, interpolations, statistics e.t.c. Note that the procedures are mostly functional, i.e. use a matrix as argument and return a matrix as result.

In this case I was lucky, I could read the smaplefile.txt directly, and output the result file, res.txt in the same format.

I include my solution and the result file in a ZIP file.
0 Kudos
anthonyrichards
New Contributor III
1,350 Views
I think that your solution, although modularised, compact and general, is rather more than the original poster was looking for, or indeed more than she could possibly deal with. It seemed to me, from the query, that it was from someone quite new to FORTRAN programming, and so a solution showing the basics would be more useful and a better guide to how to do it. After all, you could notdivulge and include the code for thethe module of routines thatyou use, as I understand it would be proprietory, so the poster remains none the wiser. No offence.
0 Kudos
larsm
Beginner
1,350 Views
Anthony!

I quite agree with you.

I just wanted to show a novice Fortran programmer (and maybe some others) what you CAN do using modern Fortran.

As to the proprietory stuff, the module is my own and I have not decided what do to with it yet. I'm playing with the idea to either have it published or, if there is sufficient interest, put it on the market somehow.

LarsM
0 Kudos
marma
Beginner
1,350 Views

All responses were welcome but as I said, time allowed me to find a simplesolution.This involvedinitializing an array countxyarray as0 andincluding the lines

countxyarray(ixcoord,iycoord)=countxyarray(ixcoord,iycoord)+1
meanxyarray(ixcoord,iycoord)=xyarray(ixcoord,iycoord)/countxyarray(ixcoord,iycoord)

0 Kudos
marma
Beginner
1,350 Views
All replies were appreciated. However, as indicated, I had already found a simple solution. This involved initializing an array countxyarray as 0
and including the code
countxyarray(ixcoord,iycoord)=countxyarray(ixcoord,iycoord)+1
meanxyarray(ixcoord,iycoord)=xyarray(ixcoord,iycoord)/countxyarray(ixcoord,iycoord)
within the main loop of the program.
The suggestion by Lars(?) is most welcome and not complicated.
Many thanks for all expressions of interest. I think that one of the privileges of joining this forum is that of learning new programming styles from the expertise of others who have a wealth of experience to offer.
Best wishes
Margaret
0 Kudos
Reply