X
4731

Sneaky way to get contour paths from CONTOUR funtion

Topic

With the Direct Graphics CONTOUR procedure, the PATH_XY and PATH_INFO can be used to get the X and Y information of the drawn contours. These keywords do not exist with the IDL 8 CONTOUR function. This article provides an example how you can use the CONTOUR procedure to get the path information for the contour and apply it to the same contour produced using the CONTOUR function.

Discussion

The code shown below, "contour_path_workaround.pro" provides an example how to apply the path information gathered from the CONTOUR procedure to the same contour plot generated using the CONTOUR function:

pro contour_path_workaround
  compile_opt idl2

  ;Generate a contour with the
  ;CONTOUR function explicitly
  ;telling it to use 4 levels
  c = contour(dist(300), n_levels=4)
 
 
  ;change the device to Z-buffer
  ;so the plot doesn't appear
  current_device = !D.name
  set_plot, 'Z'
 
  ;Generate a plot procedure with
  ;same data and number of levels
  ;Use the PATH_XY, PATH_INFO and
  ;PATH_DATA_COORDS to get out
  ;the path in data coordinates
  contour, dist(300), NLEVELS=4, path_xy=xy, $
    PATH_INFO=xy_inf, /PATH_DATA_COORDS
 
  ;Change the device back to the
  ;original value
  set_plot, current_device
 
  ;seperate x and y into seperate arrays
  x = reform(xy[0,*])
  y = reform(xy[1,*])
 
  ;Generate a connectivity array
  path_size = N_ELEMENTS(x) ;get the number of elements in path
  num_con = N_ELEMENTS(xy_inf) ;get the number of contours
  conn_arr = LONARR(path_size + num_con) ;generate an array that will hold
                                         ;connectivity informaiton
   
  ;loop through XY_INFO result and input data
  ;into the connectivity array
  for ind = 0L, num_con-1 do begin
    struct = xy_inf[ind] ;get the XY_INFO structure
    ll=lindgen(struct.n)
    data = ll + struct.offset
    ;Number of points to be connected
    conn_arr[struct.offset+ind]=struct.n
    ;Index values of array to be connected
    conn_arr[struct.offset+ind+1] = data     
  endfor
 
  ;Use the path information and connectivity
  ;array to draw black polygon lines
  p = polyline(x,y,CONNECTIVITY=conn_arr,/data)

end

 Reviewed KK DS 9/5/2014