[obspy-users] obspy.core.trace.Trace.trim / padding / fill_value & masked arrays -- clarification request

Peter Danecek Peter.Danecek at bo.ingv.it
Tue Apr 2 16:16:51 CEST 2013


Thanks Tobias,

I'll make this become an issue (not immediately though), I just was  
not completely sure if my expectation is wrong ...
~petr


On 29 Mar 2013, at 13:59, Tobias Megies wrote:

> Hi Peter,
>
> I did not have time to try it myself yet but from reading your post it
> seems to me that case 3) indeed probably should return a regular
> ndarray. I think this might best be discussed in form of an issue at  
> github.
>
> best,
> Tobias
>
> On 03/28/2013 04:13 PM, Peter Danecek wrote:
>>
>> Hi ObsPy community,
>>
>> sorry for the longish title (I'd like to keep it descriptive) ...
>>
>> I am somewhat puzzled with the behaviour of method Trace.trim() if
>> padding AND fill_value is used.
>>
>> From who I interpret the documentation on Trace.trim() (see
>> http://docs.obspy.org/packages/autogen/obspy.core.trace.Trace.trim.html) 
>>  I
>> would expect that setting a fill_value would avoid creating a NumPy
>> masked array (<class 'numpy.ma.core.MaskedArray'>), which otherwise  
>> is
>> the case when gaps or pads occur. Instead a normal ndarray (<type
>> 'numpy.ndarray'>) as in the case without gaps/pads is created as a  
>> result.
>>
>> However, I observe a somewhat different behaviour, which is  
>> "something
>> in between".
>>
>>
>> === details (snip) ===
>>
>> I am always starting with a trace as follows:
>>
>> In : print tr
>> IV.MMN..HHZ | 2012-01-01T00:00:00.000000Z -  
>> 2012-01-01T20:50:58.990000Z
>> | 100.0 Hz, 7505900 samples
>>
>>
>> (1) If I use simple trim() method (no padding) I obtain the following
>> result, which is actually what I would expect:
>>
>> In [16]:
>> tr.trim(starttime=obspy.UTCDateTime("2012-01-01T20:00:00.000000Z"),
>> endtime=obspy.UTCDateTime("2012-01-01T21:00:00.000000Z"))
>>
>> In [17]: print trIV.MMN..HHZ | 2012-01-01T20:00:00.000000Z -
>> 2012-01-01T20:50:58.990000Z | 100.0 Hz, 305900 samples
>>
>> In [18]: tr.data
>> Out[18]: array([ -34.,  -41., -107., ...,   86.,  119.,  151.],
>> dtype=float32)
>>
>> In [19]: print type(tr.data)
>> <type 'numpy.ndarray'>
>>
>>
>> (2) In case instead I request for padding, I get a masked array due  
>> to
>> the missing samples at the and which is fine:
>>
>> In [20]: tr = tr_copy.copy()
>>
>> In [21]:
>> tr.trim(starttime=obspy.UTCDateTime("2012-01-01T20:00:00.000000Z"),
>> endtime=obspy.UTCDateTime("2012-01-01T21:00:00.000000Z"), pad=True)
>>
>> In [22]: print tr
>> IV.MMN..HHZ | 2012-01-01T20:00:00.000000Z -  
>> 2012-01-01T21:00:00.000000Z
>> | 100.0 Hz, 360001 samples (masked)
>>
>> In [23]: tr.data
>> Out[23]:
>> masked_array(data = [-34.0 -41.0 -107.0 ..., -- -- --],
>>             mask = [False False False ...,  True  True  True],
>>       fill_value = 1e+20)
>>
>>
>> (3) However, if I try to avoid a masked array by setting a  
>> "fill_value",
>> I expected to get a simple ndarray as in (1) just longer and filled  
>> with
>> the fill_value, the trace is reported NOT to be masked, but I get  
>> again
>> a class 'numpy.ma.core.MaskedArray' object, with a somewhat different
>> setting.
>>
>> In [32]:
>> tr.trim(starttime=obspy.UTCDateTime("2012-01-01T20:00:00.000000Z"),
>> endtime=obspy.UTCDateTime("2012-01-01T21:00:00.000000Z"), pad=True,
>> fill_value=-999 )
>>
>> In [33]: print tr
>> IV.MMN..HHZ | 2012-01-01T20:00:00.000000Z -  
>> 2012-01-01T21:00:00.000000Z
>> | 100.0 Hz, 360001 samples
>>
>> In [34]: print tr.data
>> [ -34.  -41. -107. ..., -999. -999. -999.]
>>
>> In [35]: print type(tr.data)
>> <class 'numpy.ma.core.MaskedArray'>
>>
>> In [36]: tr.data
>> Out[36]:
>> masked_array(data = [ -34.  -41. -107. ..., -999. -999. -999.],
>>             mask = False,
>>       fill_value = 1e+20)
>>
>>
>> === END details (snap) ===
>>
>>
>> So what is the issue here?
>>
>> - Is my expectation wrong?
>> - Am I missing something or is there some error in my reasoning?
>> - Is this eventually a bug/
>>
>>
>> Thanks for any hint and comments!
>> ~petr
>>
>>
>
>
> -- 
> Dipl.-Geophys. Tobias Megies
>
> Geophysikalisches Observatorium
> Ludwigshöhe 8
> 82256 Fürstenfeldbruck
>
> Ludwig-Maximilians-Universität
> Department für Geo- und Umweltwissenschaften
> Sektion Geophysik
> Theresienstrasse 41/IV
> 80333 München
>
> Tel: +49 (0) 89 2180-73981
>     +49 (0) 89 2180-4326
> Mail: tobias.megies at geophysik.uni-muenchen.de
>
> _______________________________________________
> obspy-users mailing list
> obspy-users at lists.sevor.de
> http://lists.obspy.org/cgi-bin/mailman/listinfo/obspy-users





More information about the obspy-users mailing list