- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Many thanks for the sample solution, Anthony.
The approach was extremely relevant.
Best,
Margaret
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page