Search code examples
idl-programming-language

idl/gdl ERROR: function not found or scalar subscript out of range


i try to solve this problem with my code. When i compile i have the follow error message:

% POINCARE: Ambiguous: POINCARE: Function not found: XT  or: POINCARE: Scalar subscript out of range [>].e
% Execution halted at: POINCARE            38 poincare.pro
%                      $MAIN$          

It's very simple:

1) i OPEN THE FILE AND COUNT THE NUMBER OF ROWS AND COLUMNS, 2) save the fle in a matrix of ROWSxCOLUMNS, 3) take the rows that i want and save them as vectors,

Now i want to modify the columns as follow:

A) translate each element of first and second column (x and y) by a costant factor (xc, yc ....) B) apply some manipulation of each new element of this two new columns (xn ,yn ...) C) if the value pyn is greater than 0. then save the rows with the four value of xn ,pxn.

Here the code:

pro poincare

file = "orbitm.txt"
rows =File_Lines(file) ; per le righe
openr,lun,file,/Get_lun ; per le colonne
line=""
readf,lun,line
cols = n_elements(StrSplit(line, /RegEx, /extract))

openr,1,"orbitm.txt" 
 data = dblarr(cols,rows)
 readf,1,data
close,1

x = data(0,*) ; colonne e righe
y = data(1,*)
px = data(2,*)
py = data(3,*)

mu =0.001

xc = 0.5-mu
yc = 0.5*sqrt(3.)

openw,3,"section.txt"

 for i=1, rows-2 do begin


   xt = x(i)-xc
   yt = y(i)-yc
   pxt = px(i)-yc
   pyt = py(i)+xc

  tau = y(i)/(y(i)-y(i+1))


  xn = xt(i) + (xt(i+1)-xt(i))*tau

  yn = yt(i) + (yt(i+1)-yt(i))*tau

  pxn = pxt(i) + (pxt(i+1)-pxt(i))*tau

  pyn = pyt(i) + (pyt(i+1)-pyt(i))*tau

   if (pyt(i) GT 0.) then begin
    printf,3, xt(i), pxt(i)
   endif
 endfor 
close,3


end

I attach also the first rows of my input orbitm.txt:

 0.73634     0.66957     0.66062    -0.73503    
 0.86769     0.54316     0.51413    -0.82823    
 0.82106     0.66553     0.60353    -0.74436    
 0.59526     0.88356     0.79569    -0.52813    
 0.28631      1.0193     0.92796    -0.24641    
-0.29229E-02  1.0458     0.96862     0.21874E-01
-0.21583      1.0090     0.95142     0.22650    
-0.33994     0.96091     0.92099     0.35144    
-0.38121     0.93413     0.90831     0.39745    
-0.34462     0.93959     0.92534     0.36561    
-0.22744     0.96833     0.96431     0.25054    
-0.24560E-01 0.99010     0.99480     0.45173E-01
 0.25324     0.95506     0.96459    -0.24000    
 0.55393     0.81943     0.82584    -0.54830    
 0.78756     0.61644     0.61023    -0.77367    
 0.88695     0.53076     0.50350    -0.82814    

Solution

  • I can see a few issues that are immediately obvious. The first is that you define the variables XT, YT, PXT, and PYT inside your FOR loop as scalars. Shortly after, you try to index them as if they are arrays with multiple elements. Either your definition for these variables needs to change, or you need to change your definition of XN, YN, PXN, and PYN. Otherwise, this will not work as written. I have attached a modified version of your code with some suggestions and comments included.

    pro poincare
    
    file = "orbitm.txt"
    rows =File_Lines(file) ; per le righe
    openr,lun,file,/Get_lun ; per le colonne
    line=""
    readf,lun,line
    cols = n_elements(StrSplit(line, /RegEx, /extract))
    free_lun,lun  ;; need to close this LUN
    
    ;; define data array
    data = dblarr(cols,rows)
    ;;openr,1,"orbitm.txt" 
    ;;readf,1,data
    ;; data = dblarr(cols,rows)
    ;;close,1
    openr,lun,"orbitm.txt",/get_lun
     readf,lun,data
    free_lun,lun  ;; close this LUN
    
    ;;x = data(0,*) ; colonne e righe
    ;;y = data(1,*)
    ;;px = data(2,*)
    ;;py = data(3,*)
    x  = data[0,*]  ;;  use []'s instead of ()'s in IDL
    y  = data[1,*]
    px = data[2,*]
    py = data[3,*]
    mu = 0.001
    xc = 0.5 - mu       ;;  currently a scalar
    yc = 0.5*sqrt(3.)   ;;  currently a scalar
    ;;  Perhaps you want to define XT, YT, PXT, and PYT as:
    ;;    xt  =  x - xc[0]
    ;;    yt  =  y - yc[0]
    ;;    pxt = px - yc[0]
    ;;    pyt = py + xc[0]
    ;;  Then you could index these inside the FOR loop and
    ;;  remove their definitions therein.
    
    ;;openw,3,"section.txt"
    openw,lun,"section.txt",/get_lun
      for i=1L, rows[0] - 2L do begin
        xt  =   x[i] - xc   ;;  currently a scalar
        yt  =   y[i] - yc   ;;  currently a scalar
        pxt =  px[i] - yc   ;;  currently a scalar
        pyt =  py[i] + xc   ;;  currently a scalar
        tau =   y[i]/(y[i] - y[i+1])   ;;  currently a scalar
        ;;  In the following you are trying to index XT, YT, PXT, and PYT but
        ;;    each are scalars, not arrays!
        xn  =  xt[i] + (xt[i+1] - xt[i])*tau
        yn  =  yt[i] + (yt[i+1] - yt[i])*tau
        pxn = pxt[i] + (pxt[i+1] - pxt[i])*tau
        pyn = pyt[i] + (pyt[i+1] - pyt[i])*tau
        if (pyt[i] GT 0.) then begin
          printf,lun, xt[i], pxt[i]
        endif
      endfor 
    free_lun,lun  ;; close this LUN
    ;;close,3
    
    ;;  Return
    return
    end
    

    General IDL Notes: You should use []'s instead of ()'s to index arrays to avoid confusion with functions. It is generally better to let IDL define a logical unit number (LUN) and then free the LUN than use CLOSE.