Re: BYTES to LONG [message #22325] |
Wed, 08 November 2000 00:00 |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Kelly Dean wrote:
>
> Thanks Paul,
>
> Your suggestion solved my problem.
>
> long_x = TOTAL( ISHFT( LONG(x), [24,16,8,0] ) )
>
> It converted the 4 Bytes into a LONG UTM number.
Wow. I posted a useful solution...excuse me while I get up from the floor! :o)
I have to admit, I treated your question as a little IDL exercise rather than think a bit
harder and ask a few more questions to help you solve your problem. If, as Med Bennett
suggested, you can read the data in as a LONG, isn't that a better method? Or didn't that
work for you? I have no idea what the format of a USGS SDTS file is so it may be a
braindead question.
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.207, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: BYTES to LONG [message #22326 is a reply to message #22325] |
Wed, 08 November 2000 00:00  |
Kelly Dean
Messages: 92 Registered: March 1997
|
Member |
|
|
Thanks Paul,
Your suggestion solved my problem.
long_x = TOTAL( ISHFT( LONG(x), [24,16,8,0] ) )
It converted the 4 Bytes into a LONG UTM number.
Sorry Dave, I couldn't get your suggestion to work.
I still have some problems with some values, but these USGS SDTS files have
the Spatial Addresses scattered around my positioning maybe off on a few
locations. I suspect there maybe a record delimiter in the file that I need
to remove or skip over.
I'll post my routine once I solve my problem.
Kelly
|
|
|
Re: BYTES to LONG [message #22334 is a reply to message #22325] |
Tue, 07 November 2000 00:00  |
Kelly Dean
Messages: 92 Registered: March 1997
|
Member |
|
|
Doing a POINT_LUN, then ReadU them in as LONG works.
I am testing a method were I reading in the whole at once, then loop though it
later.
Kelly
Med Bennett wrote:
> If your variables for holding X and Y are of the correct type, you should be
> able to read up to or point_lun to the proper place in the file, and then
> read them in directly using the READU command. It helps to have a hex
> editor/viewer to figure out the offsets into the file that you are looking
> for.
>
> Kelly Dean wrote:
>
>> I am reading in a combination ASCII/BINARY file with USGS DLG
>> information as a binary file.
>>
>> I am able to convert the bytes into ASCII with STRING([72B, 101B, 108B,
>> 108B, 111B]).
>>
>> However, I cannot figure out how to convert the 4 bytes into LONG, whihc
>> is the UTM X and Y numbers. Any suggestions?
>>
>> Kelly
|
|
|
Re: BYTES to LONG [message #22335 is a reply to message #22334] |
Tue, 07 November 2000 00:00  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
"J.D. Smith" wrote:
>
> Paul van Delst wrote:
>>
>> For some reason the TOTAL returned a floating point number? Weird. Never noticed that
>> before.
>>
>
> The total() function always converts to floating point before the sum,
> to avoid overflow issues. Consider:
>
> a=total(bindgen(256))
>
> if it did the total as bytes, that would overflow. Of course, it could
> have done them as regular ints, but how should it decide, a priori? I
> for one use total a lot in testing various conditions, and it always
> bothered me that I had to test the floating result, but there's really
> no other clean solution.
You're absolutely right, of course. I've spent the last month writing a crapload of f90
code so when I saw a type that isn't what I thought I had declared it, I momentarily
freaked.
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.207, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: BYTES to LONG [message #22337 is a reply to message #22334] |
Tue, 07 November 2000 00:00  |
John-David T. Smith
Messages: 384 Registered: January 2000
|
Senior Member |
|
|
Paul van Delst wrote:
>
> Kelly Dean wrote:
>>
>> I am reading in a combination ASCII/BINARY file with USGS DLG
>> information as a binary file.
>>
>> I am able to convert the bytes into ASCII with STRING([72B, 101B, 108B,
>> 108B, 111B]).
>>
>> However, I cannot figure out how to convert the 4 bytes into LONG, whihc
>> is the UTM X and Y numbers. Any suggestions?
>>
>> Kelly
>
> If I understand your question correctly,
>
> if
>
> x = [ 0B, 1B, 1B, 1B ]
>
> then long_x = TOTAL( ISHFT( LONG(x), [24,16,8,0] ) )
>
> ??
>
> This gave me long_x = 65793.0 = 65536 + 256 + 1 which seems correct, no?
>
> For some reason the TOTAL returned a floating point number? Weird. Never noticed that
> before.
>
The total() function always converts to floating point before the sum,
to avoid overflow issues. Consider:
a=total(bindgen(256))
if it did the total as bytes, that would overflow. Of course, it could
have done them as regular ints, but how should it decide, a priori? I
for one use total a lot in testing various conditions, and it always
bothered me that I had to test the floating result, but there's really
no other clean solution.
JD
--
J.D. Smith | WORK: (607) 255-6263
Cornell Dept. of Astronomy | (607) 255-5842
304 Space Sciences Bldg. | FAX: (607) 255-5875
Ithaca, NY 14853 |
|
|
|
Re: BYTES to LONG [message #22338 is a reply to message #22334] |
Tue, 07 November 2000 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Kelly Dean (krdean@lamar.colostate.edu) writes:
> I am reading in a combination ASCII/BINARY file with USGS DLG
> information as a binary file.
>
> I am able to convert the bytes into ASCII with STRING([72B, 101B, 108B,
> 108B, 111B]).
>
> However, I cannot figure out how to convert the 4 bytes into LONG, whihc
> is the UTM X and Y numbers. Any suggestions?
This is the purpose of the Offset parameter in the LONG
function. If you use an offset into a byte array, it will
then extract the next 4 bytes as a long integer.
Here is a little example program to show you how it works.
The important part is where the byteValues are converted
to LONGs:
PRO TEST
data = [1L, 4L, 8L, 12L]
OpenW, lun, 'test.dat', /Get_Lun
WriteU, lun, data
Free_Lun, lun
byteValues = BytArr(16)
OpenR, lun, 'test.dat', /Get_Lun
ReadU, lun, byteValues
Free_Lun, lun
longValues = LonArr(4)
For j=0,3 DO longValues[j] = Long(byteValues, j*4)
Print, longValues
END
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: BYTES to LONG [message #22339 is a reply to message #22334] |
Tue, 07 November 2000 00:00  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Kelly Dean wrote:
>
> I am reading in a combination ASCII/BINARY file with USGS DLG
> information as a binary file.
>
> I am able to convert the bytes into ASCII with STRING([72B, 101B, 108B,
> 108B, 111B]).
>
> However, I cannot figure out how to convert the 4 bytes into LONG, whihc
> is the UTM X and Y numbers. Any suggestions?
>
> Kelly
If I understand your question correctly,
if
x = [ 0B, 1B, 1B, 1B ]
then long_x = TOTAL( ISHFT( LONG(x), [24,16,8,0] ) )
??
This gave me long_x = 65793.0 = 65536 + 256 + 1 which seems correct, no?
For some reason the TOTAL returned a floating point number? Weird. Never noticed that
before.
Hmm.
You could also just add the requisite power of two to each individual element before
totaling also. Dunno if either works with a sign bit.
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.207, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: BYTES to LONG [message #22340 is a reply to message #22334] |
Tue, 07 November 2000 00:00  |
Med Bennett
Messages: 109 Registered: April 1997
|
Senior Member |
|
|
If your variables for holding X and Y are of the correct type, you should be
able to read up to or point_lun to the proper place in the file, and then
read them in directly using the READU command. It helps to have a hex
editor/viewer to figure out the offsets into the file that you are looking
for.
Kelly Dean wrote:
> I am reading in a combination ASCII/BINARY file with USGS DLG
> information as a binary file.
>
> I am able to convert the bytes into ASCII with STRING([72B, 101B, 108B,
> 108B, 111B]).
>
> However, I cannot figure out how to convert the 4 bytes into LONG, whihc
> is the UTM X and Y numbers. Any suggestions?
>
> Kelly
|
|
|