Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Problem with strings and scientific notation

Help
2012-08-16
2013-11-23
  • Hi,

    I have been using IDL to read some ASCII output from a Fortran legacy code. It is a rather complicated format (i.e., not a simple multi-column file), so I had to write specific routines to do the job.

    I am planning to use GDL now instead, but I've run into an annoying problem with my reading routines. The Fortran output contains numbers in scientific format, e.g., "1.500D-01" for 0.15. When read as strings, it was never a problem for IDL to directly translate that into a float or double number. However, now with GDL this does not work anymore, as GDL only recognizes scientific notation that uses "E" instead of "D" as in "1.500E-01".

    Self-evidently, I could simply read the Fortran output and check for the occurrence of "D" and change it to "E". However, I'm reading very large chunks of data and this would simply take too long. Also, I'm afraid rewriting the Fortran code is not an option. Is there any trick to this that I might have overlooked?

    I had a look at the GDL documentation and did some web searching, but couldn't find any remarks about this limitation anywhere. Therefore, this problem might potentially affect more people who relied on IDL's type conversion abilities.

    Thanks,
    Michael G.

     
  • Alain C.
    Alain C.
    2012-08-17

    could you please provide a (as small as possible) sub set of the data
    (can be "fake") with the related sub set code to read it ?

    We will gain time and being focused on the concrete problem.

    Alain

    PS: we cannot promise we can quickly solve this issue
    but I have the feeling it is an important issue today.

    PS: you can write me directly

    PPS: a temporary work around would be to read back in IDL, generating
    XDR output via SAVE in IDL, RESTORE in GDL (you must add your self the CMSV lib,
    we can not put it in the GDL CVS tree du to licence issue) then test you code
    and report any others problems (in the bug tracker !)

     
  • Hi Alain,

    actually, it's not really necessary to give a snipped of the code or the data.
    I'm sorry that I was not very specific when I said it didn't work with GDL.
    The problem can be easily reproduced.
    Simply try the following:

    a = '1.500D-01'
    b = 1.0*a
    print,b

    IDL will print:  0.15
    GDL will print: 1.5

    Try the same thing with '1.500E-1' will produce the right answer with both interpreters.

    Hope that helps & thanks for your suggestions :)

    Ciao,
    Michael

     
  • By the way, I just discovered a nice work-around for the efficient conversion of multiple occurrences of the "D" format character to the "E" format character in a string. In the astrolib (the IDL Astronomy User's Library - http://idlastro.gsfc.nasa.gov/) there is a function called REPCHR which first converts the input string into a byte array and then performs a "where" search for the byte-representation of one character (e.g., "D"), changes all which were found to the byte value of another character (e.g., "E") and then converts the byte array back into a string.

     
  • Hi,
    Thanks for reporting it!
    I'll work on it this weekend.
    Sylwester

     
  • Fixed in the CVS:

    GDL> a = '1.500D-01'
    GDL> b = 1.0*a
    GDL> print, b
          0.15000

    Best,
    Sylwester

     
  • jan schween
    jan schween
    2012-10-02

    hello,

    i realized that the print procedure is missing date time formatting:

    GDL> print, systime(/julian), format='(C())'
    Format parser: Mismatched Token
    % Format parser: Mismatched Token
    % Execution halted at: $MAIN$         

    The same in IDL:
    IDL> print, systime(/julian), format='(C())'  
    Tue Oct 02 10:07:33 2012

    for a description of the data time format of idl see e.g.:
    http://www.physics.nyu.edu/grierlab/idl_html_help/files24.html#wp2823814

     
  • ghassel
    ghassel
    2013-11-23

    Hi -

    I'm encountering a similar problem. The a = '1.500D-01'; b = 0.15 issue is working properly if I define "a" within the body of the code.

    However, if I try to read a simple 2 column file of:

    1.500D-01 1.600D-01

    with the commands:

    a=0.d
    b=0.d
    infil='testread.dat'
    openr,lun,infil,/Get_Lun
    readf,lun,a,b
    close,lun
    Free_lun,lun
    print,a
    print,b

    The result is 1.5000000 and 1.6000000. Are there any work-arounds or alternatives to ReadF that would maintain the scientific notation?

    Many thanks!
    -George