1

I did the following script to integrate (average) data by intervals in python:

# N = points to mean in the array
# data = original data
# data_mean = average data each N points

data_mean = np.array([np.mean(i) for i in np.array_split(data, len(data)/N)])

How could do that in IDL? There are a "mean" function, but a "array_split-like"?

paisanco
  • 4,098
  • 6
  • 27
  • 33
nandhos
  • 681
  • 2
  • 16
  • 31

2 Answers2

2

The array_split functionality is usually done via REFORM to create a two (or higher) dimensional array from a 1-dimensional array using the same values. So for example:

n = 20
data = randomu(seed, 100)
data = reform(data, 100 / n, n)
print, mean(data, dimension=2)
mgalloy
  • 2,356
  • 1
  • 12
  • 10
  • I got the fllowing message after printing mean(data, dimension=2): % Keyword DIMENSION not allowed in call to: MEAN % Execution halted at: $MAIN$ – nandhos Sep 13 '15 at 20:01
  • It work but REFORM is simlar to RESHAPE. When I was using RESHAPE, i didnt be able to reshape in a non-multiple of the number of elements – nandhos Sep 14 '15 at 00:24
  • The `DIMENSION` keyword to `MEAN` was added in IDL 8.0. Pre-8.0, use `TOTAL` and divide by the number of elements. There has been a `dimension` second parameter in `TOTAL` for a long time. – mgalloy Sep 14 '15 at 04:02
  • `REFORM` does require the number of elements in each dimension to be a multiple of the total number of elements. What do you want to happen in that case? You could pad the array with `NaN`s or some other value. – mgalloy Sep 14 '15 at 04:04
0

The IDL mean function is equivalent to the numpy mean function, and the IDL reform can be used similarly to the numpy array_split:

data_mean = mean(reform(data, n_elements(data) / N), dimension=2)

If you don't mind data ending up with different dimensions, you can greatly speed this up using the /overwrite keyword:

data_mean = mean(reform(data, n_elements(data) / N, /overwrite), dimension=2)

Finally, if you have a version of IDL before IDL 8.0, then you won't have the dimension keyword for the mean function. Use this (less elegant) pattern instead:

data_mean = total(reform(data, n_elements(data) / N), 2) / N

Note that this version with total also accepts the /nan keyword, so that it works even when some data are missing.

Pi Marillion
  • 4,465
  • 1
  • 19
  • 20