Home > Archive > Fortran > September 2006 > How to set the FORMAT for reading this data file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to set the FORMAT for reading this data file?
|
|
|
|
I have this kind of file,
0.1 11.750840650304518 0.1374480266382216
0.2 3.4133367257849176 7.108705996120129
0.30000000000000004 20.80921881438798 0.4251595082899424
0.4 5.231982481945481 0.7182384931413827
0.5 26.925221604818102 26.706603701525214
I want only the 2nd and 3rd rows of numbers read into a 2d array, so how
can I set the FORMAT for the READ command?
Thanks
--
Don't forget your dreamS
| |
| David Frank 2006-09-22, 8:00 am |
|
"Nye" <zuyingl@gmail.com> wrote in message
news:87eju4t42u.fsf@Anyer.shao.ac.cn...
>
> I have this kind of file,
>
> 0.1 11.750840650304518
> 0.1374480266382216
> 0.2 3.4133367257849176
> 7.108705996120129
> 0.30000000000000004 20.80921881438798 0.4251595082899424
> 0.4 5.231982481945481
> 0.7182384931413827
> 0.5 26.925221604818102
> 26.706603701525214
>
> I want only the 2nd and 3rd rows of numbers read into a 2d array, so how
> can I set the FORMAT for the READ command?
>
> Thanks
>
You dont need a format,
read (1,*) ! skip record 1
read (1,*) array ! records 2,3 -> array(3,2)
| |
| Dr Ivan D. Reid 2006-09-22, 10:00 pm |
| On Fri, 22 Sep 2006 21:37:29 +0800, Nye <zuyingl@gmail.com>
wrote in <87eju4t42u.fsf@Anyer.shao.ac.cn>:
> I have this kind of file,
> 0.1 11.750840650304518 0.1374480266382216
> 0.2 3.4133367257849176 7.108705996120129
> 0.30000000000000004 20.80921881438798 0.4251595082899424
> 0.4 5.231982481945481 0.7182384931413827
> 0.5 26.925221604818102 26.706603701525214
> I want only the 2nd and 3rd rows of numbers read into a 2d array, so how
> can I set the FORMAT for the READ command?
Rows, or columns? It's unfortunate that whatever created the
file didn't use fixed-width fields. However, you can use free format.
David has already explained how to do rows; if you want to skip columns
I don't see how to do it in one array read, but implied loops can be
used with a scratch variable to take the unwanted data:
C Warning, untested code!
integer i, j, nmax
real a, array(2,...)
open (unit=42, file='blah'...)
nmax=....
read(42,*)(a,(array(i,j),i=1,2)),j=1,nma
x)
--
Ivan Reid, Electronic & Computer Engineering, ___ CMS Collaboration,
Brunel University. Ivan.Reid@[brunel.ac.uk|cern.ch] Room 40-1-B12, CERN
| |
| Richard Maine 2006-09-22, 10:00 pm |
| Dr Ivan D. Reid <Ivan.Reid@brunel.ac.uk> wrote:
> 0.1 11.750840650304518 .1374480266382216
> 0.2 3.4133367257849176 7.108705996120129
> 0.30000000000000004 20.80921881438798 0.4251595082899424
> 0.4 5.231982481945481 0.7182384931413827
> 0.5 26.925221604818102 26.706603701525214
....
> It's unfortunate that whatever created the
> file didn't use fixed-width fields.
Eh? Must be font differences in our newsreaders or something. Those sure
look exactly like fixed-width fields to me. Some oddly different numbers
of digits, but all infixed-width fields. The free format will probably
work fine, though... at least assuming that there are no cases where the
fields are filled enough to leave no blanks between them.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| glen herrmannsfeldt 2006-09-22, 10:00 pm |
| Richard Maine <nospam@see.signature> wrote:
> Dr Ivan D. Reid <Ivan.Reid@brunel.ac.uk> wrote:
> ...
[color=darkred]
> Eh? Must be font differences in our newsreaders or something. Those sure
> look exactly like fixed-width fields to me.
They don't look very fixed to me. I believe they now have spaces,
but it might be that the original used tabs or something else.
Above is how it came through my news reader, with spaces converted to
underline so that (hopefully) they will not be processed by other
news readers.
-- glen
| |
| Richard Maine 2006-09-22, 10:00 pm |
| glen herrmannsfeldt <gah@seniti.ugcs.caltech.edu> wrote:
> Richard Maine <nospam@see.signature> wrote:
> [elided]
>
>
> They don't look very fixed to me. I believe they now have spaces,
> but it might be that the original used tabs or something else.
>
> Above is how it came through my news reader, with spaces converted to
> underline so that (hopefully) they will not be processed by other
> news readers.
Hmm. You are right. And when I actually count (a skill I obviously
underuse :-() the blanks in the original, which do look like blanks
instead of tabs as far as I can tell, the count agrees with that of the
underscores you showed. But my newsreader shows them all "nicely" lined
up. Oh well; not worth figuring out why.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Brooks Moses 2006-09-22, 10:00 pm |
| Richard Maine wrote:
> glen herrmannsfeldt <gah@seniti.ugcs.caltech.edu> wrote:
>
> Hmm. You are right. And when I actually count (a skill I obviously
> underuse :-() the blanks in the original, which do look like blanks
> instead of tabs as far as I can tell, the count agrees with that of the
> underscores you showed. But my newsreader shows them all "nicely" lined
> up. Oh well; not worth figuring out why.
Are you using a proportional font in your newsreader? If so, spaces are
displayed narrower than digits....
It's entirely possible that the original poster was using a proportional
font as well when he or she was writing the post, and added extra spaces
so it would "look right". If true, that changes the set of useful
answers dramatically.
- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
| |
|
| "David Frank" <dave_frank@hotmail.com> writes:
> "Nye" <zuyingl@gmail.com> wrote in message
> news:87eju4t42u.fsf@Anyer.shao.ac.cn...
>
> You dont need a format,
> read (1,*) ! skip record 1
> read (1,*) array ! records 2,3 -> array(3,2)
>
>
Thanks, thus I should skip n rows before I start recording?
Then file contains a huge number of data, and I will have to read the n
th row later (n maybe 100 or 1000, maybe even larger), is there a simple
way to do this?
--
Don't forget your dreamS
| |
|
| Brooks Moses <bmoses-nospam@cits1.stanford.edu> writes:
> Richard Maine wrote:
>
> Are you using a proportional font in your newsreader? If so, spaces are displayed narrower than
> digits....
>
> It's entirely possible that the original poster was using a proportional font as well when he or
> she was writing the post, and added extra spaces so it would "look right". If true, that changes
> the set of useful answers dramatically.
>
> - Brooks
You are definitely right, I've used a proportional font in emacs gnus.
:)
--
Don't forget your dreamS
| |
| David Frank 2006-09-23, 4:00 am |
|
"Nye" <zuyingl@gmail.com> wrote in message
news:87wt7v2fxu.fsf@Anyer.shao.ac.cn...
> "David Frank" <dave_frank@hotmail.com> writes:
>
> Thanks, thus I should skip n rows before I start recording?
> Then file contains a huge number of data, and I will have to read the n
> th row later (n maybe 100 or 1000, maybe even larger), is there a simple
> way to do this?
>
Whether you use a format or use * processing the file pointer is at the same
place after above read, and you proceed from there to the n'th row
via whatever technique thats appropriate.
Me, I would declare array(3,maxrec) read the whole file into the array
getting the last n index as a by-product.
Then if absolutely necessary to populate/use an array declared arr2d(3,2)
instead of directly processing the values in array, copy the sub-array
arr2d = array(:,n:n+1)
|
|
|
|
|