Guest User

COORDINATES IDL program by James Preiss

a guest
Apr 7th, 2010
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IDL 3.36 KB | None | 0 0
  1. ; +
  2. ;
  3. ; COORDINATES by James Alan Preiss
  4. ;
  5. ; PURPOSE:
  6. ;     generate multidemensional arrays of coordinates
  7. ;     to use in vectorized computation of functions.
  8. ;
  9. ; PARAMETERS:
  10. ;
  11. ;   MINS: scalar or one-dimensional array containing the minimum
  12. ;     desired coordinate value for each dimension.
  13. ;
  14. ;   MAXES: scalar or one-dimensional array containing the maximum
  15. ;     desired coordinate value for each dimension.
  16. ;
  17. ;   MINS and MAXES must have the same number of elements.
  18. ;
  19. ;   TYPE: IDL numerical typecode. If TYPE is not given, Long64
  20. ;     integers will be used. Currently, COORDINATES does not
  21. ;     check if the chosen data type is large enough to hold the
  22. ;     range of values that will be generated.
  23. ;
  24. ; RETURNS:
  25. ;   A pointer array, where element I references the multidimensional
  26. ;   array containing coordinate values for the Ith dimension.
  27. ;
  28. ; EXAMPLES:
  29. ;  
  30. ;   In this example, we generate the vector field for a
  31. ;   Lorenz Attractor, using typical values for the parameters.
  32. ;
  33. ;       coords = coordinates([-20,-20,0],[20,20,40],4)
  34. ;       x = *coords[0]
  35. ;       y = *coords[1]
  36. ;       z = *coords[2]
  37. ;
  38. ;       dxdt = 10.0*(y - x)
  39. ;       dydt = x*(28.0 - z) - y
  40. ;       dzdt = x*y - (8.0/3)*z
  41. ;
  42. ; HISTORY:
  43. ;   Written April 7, 2010
  44. ;+            
  45.  
  46. function coordinates, mins, maxes, type
  47.  
  48.     ;a type argument exists - make sure it is usable.
  49.     ;notice that this DOES NOT check for overflow!
  50.     if (n_params() gt 2) then begin
  51.         ;types labeled 1 are unusable, 2 are unsigned
  52.         typecds = bytarr(15)
  53.         typecds[[0,1,6,7,8,9,10,11]] = 1
  54.         typecds[[1,12,13,15]] = 2
  55.         if (typecds[type] eq 1) then message, $
  56.             'TYPE must be a non-complex numerical typecode.'
  57.         if (typecds[type] eq 2) then begin
  58.             if (total(mins lt 0) ne 0) then message, $
  59.                 'Unsigned data type cannot contain negative values.'
  60.         endif
  61.     endif    
  62.        
  63.     ;check for proper dimensions in mins and maxes
  64.     ;each should be an n-element vector where n is the number
  65.     ;of dimensions for which we are generating coordinates
  66.     mindims = size(mins, /dimensions)
  67.     maxdims = size(maxes, /dimensions)
  68.    
  69.     if (~array_equal(mindims, maxdims) || $
  70.         n_elements(mindims) gt 1 || n_elements(maxdims) gt 1) then $
  71.             message, 'MINS and MAXES must be scalars ' + $
  72.                      'or one-dimensional arrays of the same length.'
  73.    
  74.     ;check for nonsensical ranges -
  75.     ;the min must be less than or equal to the max
  76.     sizes = maxes - mins + 1
  77.     if (total(sizes lt 1) ne 0) then message, $
  78.         'Maxes must be less than or equal to Mins.'
  79.    
  80.     dims = mindims
  81.     if (dims eq 0) then dims = 1
  82.    
  83.        
  84.     out = ptrarr(dims)
  85.    
  86.     for i=0, dims[0]-1 do begin
  87.         ;make a 1D vector containing the indices for this dimension
  88.         out[i] = ptr_new(make_array(sizes[i], /index, type=type))
  89.         ;shift index vector it contains values from min to max, inclusive
  90.         *out[i] +=+ mins[i]
  91.         ;make a vector to feed into reform for dimensional juggling
  92.         rvec = replicate(1,i+1)
  93.         ;place the index vector into the proper dimension
  94.         rvec[i] = sizes[i]
  95.         ;replicate the index vector across all dimensions
  96.         *out[i] = rebin(reform(*out[i], rvec), sizes)
  97.     endfor
  98.        
  99.     return, out
  100. end
Advertisement
Add Comment
Please, Sign In to add comment