There seem to be some vulnerabilities during ctypes calls.[[BR]] Consider the [/wiki/KnownIssues#WritingDataFromNumpyArraysAfterIndexingOperations example] below where a non-contiguous array gets passed on to the MiniSEED write methods resulting in the wrong data getting written to file.[[BR]] When data is passed on via ctypes calls (MSEED, GSE, ..) we should '''always''' check for contiguous arrays and fix non-contiguous ones before passing on the data.[[BR]]
[[BR]][[BR]][[BR]] {{{
!python
import numpy as np from obspy.core import read, Trace
x = np.arange(10) y = x[:5] z = x[::2] tr1 = Trace(data=y) tr2 = Trace(data=z) print tr1.data print tr2.data }}} ...which shows that in Python the data of Traces 1 and 2 is: {{{
!python
[0 1 2 3 4] [0 2 4 6 8] }}} But after writing and reading the data again... {{{
!python
tr1.write("/tmp/tr1.tmp", "MSEED") tr2.write("/tmp/tr2.tmp", "MSEED") tr1 = read("/tmp/tr1.tmp")[0] tr2 = read("/tmp/tr2.tmp")[0] print tr1.data print tr2.data }}} ...it is obvious that there was a problem with using the reference to the original data array. During the write operation not the correct data got written to file: {{{
!python
[0 1 2 3 4] [0 1 2 3 4] }}} It seems that this can only be avoided by creating a fresh array from the array that was created as a new view on the original data: {{{
!python
z_safe = np.array(z) tr1 = Trace(data=y) tr2 = Trace(data=z_safe) tr1.write("/tmp/tr1.tmp", "MSEED") tr2.write("/tmp/tr2.tmp", "MSEED") tr1 = read("/tmp/tr1.tmp")[0] tr2 = read("/tmp/tr2.tmp")[0] print tr1.data print tr2.data }}} which gives the expected result: {{{
!python
[0 1 2 3 4] [0 2 4 6 8] }}}
该提问来源于开源项目:obspy/obspy