Re: formatting array output? [message #33844] |
Fri, 31 January 2003 08:41 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Murat Maga (maga@mail.utexas.edu) writes:
> I have an integer array (2000x1000) which I would like to write in file
> in the typical matrix convention. I think IDL breaks the line at 80th
> column and everything starts to look quite messy.
> To format an array output, do I use the print/printf, or are there any
> other tricks good to know?
Years ago, before all of us were born (circa 1875), computer
programs weren't written in a text editor. They were written
on punch cards. You hammered away at a big typewriter-like
console with really big keys. You had to slam them pretty good,
because the arms of the keys had to punch a chad out of the
card. Hanging chads were quite a serious problem in those
days (I.e., it wasn't just the future of our country that
depended upon it, it was an extra day or two running back
and forth to the computer center that was at stake!).
Anyway, those cards had 80 columns and the number 80
has been indelibly imprinted on computer operations
ever since. If you want your output "line" to have,
say, 20000 columns (suitable for 2000 values, each
having a width of 10 characters), you would use the
WIDTH keyword when you open the file:
OPENW, 1, 'myfile.txt', Width=20000
Now you can put your data into the file in the way
you like:
FOR j=0,999 DO PRINTF, 1, data[*,j], Format='(I10)'
When you look at it in a text editor, however, you might
find that the editor word-wraps at 80 characters. :-(
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: formatting array output? [message #33848 is a reply to message #33844] |
Fri, 31 January 2003 08:12  |
thompson
Messages: 584 Registered: August 1991
|
Senior Member |
|
|
Murat Maga <maga@mail.utexas.edu> writes:
> Kenneth Bowman wrote:
>>
>> In article <3E394CDD.8F9B0C65@mail.utexas.edu>,
>> Murat Maga <maga@mail.utexas.edu> wrote:
>>
>>> Hi All,
>>> I have an integer array (2000x1000) which I would like to write in file
>>> in the typical matrix convention. I think IDL breaks the line at 80th
>>> column and everything starts to look quite messy.
>>> To format an array output, do I use the print/printf, or are there any
>>> other tricks good to know?
>>> Best,
>>> Murat
>>
>> Use a portable binary format like netcdf or hdf. You aren't really
>> planning to read the file, are you?
>>
>> id = NCDF_OPEN('newfile')
>> d1 = NCDF_DIMDEF(id, 'dim1', 2000)
>> d2 = NCDF_DIMDEF(id, 'dim2', 1000)
>> vid = NCDF_VARDEF(id, 'matrix', [d1, d2], /LONG)
>> NCDF_CONTROL, id, /ENDEF
>> NCDF_VARPUT, id, 'matrix', matrix
>> NCDF_CLOSE, id
>>
>> Ken Bowman
> Thanks for the answers everybody. Yes I do need to use an ASCII format,
> HDF is not an option for me. I also need to parse the file manually -at
> least partially-, that is the other reason.
> printf, 2, '$(2000F)'
> command is actually all i need. But I have one other silly question,
> what if the size of the array is parametric? I tried to put the variable
> name but that didnt work. Just for the record, I come from neither C nor
> fortran school. No programming skills, unfortunately. I measure bones
> :-)
> Best,
> Murat
The simplest way to do that in IDL is to create the format string on the fly,
e.g.
format = '$(' + strtrim(n,2) + 'F)'
printf, 2, format=format, ...
You can use the SIZE function to determine what N should be, e.g.
sz = size(array)
format = '$(' + strtrim(sz[1],2) + 'F)'
printf, 2, format=format, array
William Thompson
|
|
|
Re: formatting array output? [message #33855 is a reply to message #33848] |
Thu, 30 January 2003 12:29  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
Murat Maga wrote:
...
> printf, 2, '$(2000F)'
> command is actually all i need. But I have one other silly question,
> what if the size of the array is parametric? I tried to put the variable
> name but that didnt work. Just for the record, I come from neither C nor
> fortran school. No programming skills, unfortunately. I measure bones
arrayform = '$(' + strtrim(columns,1) + 'F)'
printf, 2, arrayform, array
|
|
|
Re: formatting array output? [message #33856 is a reply to message #33855] |
Thu, 30 January 2003 12:34  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
"Murat Maga" <maga@mail.utexas.edu> wrote in message
news:3E39A27F.3DC7F39D@mail.utexas.edu...
>>> I have an integer array (2000x1000) which I would like to write in
>>> file in the typical matrix convention. I think IDL breaks the line
>>> at 80th column and everything starts to look quite messy.
> Thanks for the answers everybody. Yes I do need to use an ASCII
> format, HDF is not an option for me. I also need to parse the file
> manually -at least partially-, that is the other reason.
How about writing the array dimensions on the first line of the file,
followed by array @ 1 element per line? It's easy to parse
programmatically, easy to parse manually, and you are not tempted to
muck around getting everything to line up.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: formatting array output? [message #33857 is a reply to message #33855] |
Thu, 30 January 2003 14:09  |
Murat Maga
Messages: 6 Registered: December 2002
|
Junior Member |
|
|
Kenneth Bowman wrote:
>
> In article <3E394CDD.8F9B0C65@mail.utexas.edu>,
> Murat Maga <maga@mail.utexas.edu> wrote:
>
>> Hi All,
>> I have an integer array (2000x1000) which I would like to write in file
>> in the typical matrix convention. I think IDL breaks the line at 80th
>> column and everything starts to look quite messy.
>> To format an array output, do I use the print/printf, or are there any
>> other tricks good to know?
>> Best,
>> Murat
>
> Use a portable binary format like netcdf or hdf. You aren't really
> planning to read the file, are you?
>
> id = NCDF_OPEN('newfile')
> d1 = NCDF_DIMDEF(id, 'dim1', 2000)
> d2 = NCDF_DIMDEF(id, 'dim2', 1000)
> vid = NCDF_VARDEF(id, 'matrix', [d1, d2], /LONG)
> NCDF_CONTROL, id, /ENDEF
> NCDF_VARPUT, id, 'matrix', matrix
> NCDF_CLOSE, id
>
> Ken Bowman
Thanks for the answers everybody. Yes I do need to use an ASCII format,
HDF is not an option for me. I also need to parse the file manually -at
least partially-, that is the other reason.
printf, 2, '$(2000F)'
command is actually all i need. But I have one other silly question,
what if the size of the array is parametric? I tried to put the variable
name but that didnt work. Just for the record, I come from neither C nor
fortran school. No programming skills, unfortunately. I measure bones
:-)
Best,
Murat
|
|
|
Re: formatting array output? [message #33860 is a reply to message #33855] |
Thu, 30 January 2003 08:41  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
Kenneth Bowman wrote:
>
> In article <3E394CDD.8F9B0C65@mail.utexas.edu>,
> Murat Maga <maga@mail.utexas.edu> wrote:
>
>> Hi All,
>> I have an integer array (2000x1000) which I would like to write in file
>> in the typical matrix convention. I think IDL breaks the line at 80th
>> column and everything starts to look quite messy.
>> To format an array output, do I use the print/printf, or are there any
>> other tricks good to know?
>> Best,
>> Murat
>
> Use a portable binary format like netcdf or hdf. ...
While netcdf or hdf are both supported by IDL, he might be producing
output which might need to be read using a system which doesn't support
netcdf, or hdf. ASCII text is still the most widely portable format.
|
|
|
Re: formatting array output? [message #33861 is a reply to message #33860] |
Thu, 30 January 2003 08:39  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
James Kuyper wrote:
>
> Murat Maga wrote:
>>
>> Hi All,
>> I have an integer array (2000x1000) which I would like to write in file
>> in the typical matrix convention. I think IDL breaks the line at 80th
>> column and everything starts to look quite messy.
>> To format an array output, do I use the print/printf, or are there any
>> other tricks good to know?
Assuming ASCII output, use the WIDTH keyword in the OPENW statement. Explicit format
strings are handy too.
paulv
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
Ph: (301)763-8000 x7274
Fax:(301)763-8545
|
|
|
Re: formatting array output? [message #33862 is a reply to message #33861] |
Thu, 30 January 2003 08:37  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
Murat Maga wrote:
>
> Hi All,
> I have an integer array (2000x1000) which I would like to write in file
> in the typical matrix convention. I think IDL breaks the line at 80th
> column and everything starts to look quite messy.
> To format an array output, do I use the print/printf, or are there any
> other tricks good to know?
> Best,
> Murat
I presume that this file is being written in ASCII rather than binary
format for portability reasons, and not for human readability. When a
single line of the matrix contains a thousand or more elements, it's a
pretty safe bet that humans won't be reading it. I've never heard of a
display devise big enough to display a single row of that matrix all on
one line.
That being the case, why do you care where it breaks the line? It's easy
enough to write code that reads the file in a way that treats the
newline characters the same as other whitespace characters. That's true
not only in IDL, and also in several other languages that I know.
However, if you do indeed want to do this, print/printf does indeed seem
to be the way to go. The only "tricks" I'm aware of are repeat counts
and format reversion:
array = fltarr(2000,1000)
readf,infile,array
printf,outfile,'$(2000F)', array
These feel like tricks to someone coming from a C background; but for
Fortran programmers it seems quite normal.
|
|
|
Re: formatting array output? [message #33865 is a reply to message #33862] |
Thu, 30 January 2003 06:25  |
K. Bowman
Messages: 330 Registered: May 2000
|
Senior Member |
|
|
In article <3E394CDD.8F9B0C65@mail.utexas.edu>,
Murat Maga <maga@mail.utexas.edu> wrote:
> Hi All,
> I have an integer array (2000x1000) which I would like to write in file
> in the typical matrix convention. I think IDL breaks the line at 80th
> column and everything starts to look quite messy.
> To format an array output, do I use the print/printf, or are there any
> other tricks good to know?
> Best,
> Murat
Use a portable binary format like netcdf or hdf. You aren't really
planning to read the file, are you?
id = NCDF_OPEN('newfile')
d1 = NCDF_DIMDEF(id, 'dim1', 2000)
d2 = NCDF_DIMDEF(id, 'dim2', 1000)
vid = NCDF_VARDEF(id, 'matrix', [d1, d2], /LONG)
NCDF_CONTROL, id, /ENDEF
NCDF_VARPUT, id, 'matrix', matrix
NCDF_CLOSE, id
Ken Bowman
|
|
|