cavanaug@uars1.acd.ucar.edu (Charles Cavanaugh) writes:
> Please, please, please somebody tell me what I'm doing wrong with
> my attempt to cast a floating point value into e-notation (using
> our old buddy IDL) :
> IDL> a = 3.567
> IDL> print, size(a)
> 0 4 1
> IDL> print, a
> 3.56700
> IDL> b = strtrim (a, 2)
> IDL> print, size(b)
> 0 7 1
> IDL> print, b
> 3.56700
> IDL> print, format = '(e11.4)', a
> 3.5670e+00
> IDL> print, format = '(e11.4)', b
> 3.5670e+00
> IDL> c = 0.0
> IDL> reads, format = '(e11.4)', a, c
> IDL> print, size(c)
> 0 4 1
> IDL> print, c
> 3.56700
> IDL> reads, format = '(e11.4)', b, c
> IDL> print, size(c)
> 0 4 1
> IDL> print, c
> 3.56700
> IDL> c = ''
> IDL> reads, format = '(e11.4)', a, c
> IDL> print, size(c)
> 0 7 1
> IDL> print, c
> 3.5670000
> IDL> reads, format = '(e11.4)', b, c
> IDL> print, size(c)
> 0 7 1
> IDL> print, c
> 3.5670000
> The print command works as expected when given a format, but when reads is
> used, the variable "c", afraid of being labelled as an e-type by his f-type
> friends, refuses, with all his strength, to be cast into e-notation. Why?
>
What I can't figure out is why you're using READS to cast something as a string
variable. I expect what's happening is the following:
1. READS uses the FORMAT specification to determine how to read the
string and convert it into one or more numerical values.
2. If the input parameter isn't a string, then IDL first makes it a
string and then reads it back out as a number again. This is
polite of it, but leads to confusion. It does not use the FORMAT
specification when converting it into the string, because the
FORMAT is for reading, not writing.
3. After reading the parameter according to the format specification,
it recasts the output into the correct data type. If necessary, it
recasts it into a string, but again it doesn't use the format
specification to do that, because that's not what it's for.
To see why it doesn't use the FORMAT expression to format the output, consider
the following statements.
IDL> A = ''
IDL> B = 0
IDL> READS, 'XY=345' , A, B, FORMAT='(A2,1X,I3)'
If it needed to recast the B value back into a string for some reason, it
wouldn't be able to use the FORMAT statement to do that because it wouldn't be
valid.
Bill Thompson
|