[Numpy-discussion] more recfunctions, structured array help

John [H2O] washakie@gmail....
Tue Dec 8 18:11:18 CST 2009



Pierre GM-2 wrote:
> 
> 
> Did you check scikits.timeseries ? Might be a solution if you have data
> indexed in time
> 
> 
>> np.rec.fromrecords(codata_masked,names='datetime,lon,lat,elv,co')
>>    return codata, codata_masked
> 
> OK, I gonna have to guess again:
> codata is a regular ndarray, not structured ? Then you don't have to
> transform it into a masked array
> codata=...
> codata_masked = np.ma.masked_values(codata,-9999.)
> 
> Then you transform codata into a np.recarray... But why not transforming
> codata_masked ?
> 
> It is hard to help you, because I don't know the actual structure you use.
> Once again, please give a self contained example. The first two entries of
> codata would be enough.
> 
> 
> 
> 
> 
> Er... You can check whether codata_masked is masked by checking if some
> entries of its mask are True (codata_masked.mask.any()). 
> Given your graph, I'd say yes, codata_masked is actually masked: see how
> you have a gap in your green curve when the blue one plummets into
> negative? That's likely where your elevation was -9999., I'd say.  
> _______________________________________________
> 
> 

My apologies for adding confusing. In answer to your first question. Yes at
one point I tried playing with scikits.timeseries... there were some issues
at the time that prevented me from working with it, maybe I should revisit.
But on to this problem...

First off, let me say, my biggest curve with numpy has been dealing with
different types of data structures, and I find that I always am starting
with something new. For instance, I am now somewhat comfortable with arrays,
but not yet masked arrays, and then it seems record arrays are the most
'advanced' and quite practical, but it seems the handling of them is quite
different from standard arrays. So I;m learning!

As best I can I'll provide a full example.

Here's what I have:

def mk_COarray(rD,dtvector,mask=None):
    codata =
np.column_stack((np.array(dtvector),rD.lon,rD.lat,rD.elv,rD.co))
    print type(codata)
    
    if mask:
        codata_masked = np.ma.masked_where(codata==mask,codata,copy=False)
    # Create record array from codata_masked
    else:
        codata_masked = codata
    codata =
np.rec.fromrecords(codata_masked,names='datetime,lon,lat,elv,co')
    #Note the above is just for debugging, and below I return masked and
unmasked arrays
    return codata, codata_masked


In [162]: codata,codata_masked =mk_COarray(rD,dtvec,mask=-9999.)
In [163]: type(codata); type(codata_masked)
Out[163]: <class 'numpy.core.records.recarray'>
Out[163]: <class 'numpy.ma.core.MaskedArray'>
In [164]: codata[0]
Out[164]: (datetime.datetime(2008, 4, 6, 11, 38, 37, 760000),
20.327100000000002, 67.8215, 442.60000000000002, -9999.0)

In [165]: codata_masked[0]
Out[165]: 
masked_array(data = [2008-04-06 11:38:37.760000 20.3271 67.8215 442.6 --],
             mask = [False False False False  True],
       fill_value = ?)


So then, the plot above will be the same. codata is the blue line
(codata_masked converted into a rec array),  whereas for debugging, I also
return codata_masked (and it is plotted green).

In my prior post I used the variables cd and cdm which refer to codata and
codata_masked.

I know this isn't terribly clear, but hopefully enough so to let me know how
to create a masked record array ;)

-john









-- 
View this message in context: http://old.nabble.com/more-recfunctions%2C-structured-array-help-tp26700380p26703152.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.



More information about the NumPy-Discussion mailing list