def addfield(mrecord, newfield, newfieldname=None):
_data = mrecord._data<EOL>_mask = mrecord._mask<EOL>if newfieldname is None or newfieldname in reserved_fields:<EOL><INDENT>newfieldname = '<STR_LIT>' % len(_data.dtype)<EOL><DEDENT>newfield = ma.array(newfield)<EOL>newdtype = np.dtype(_data.dtype.descr + [(newfieldname, newfield.dtype)])<EOL>newdata = recarray(_data.shape, newdtype)<EOL>[newdata.setfield(_data.getfield(*f), *f)<EOL>for f in _data.dtype.fields.values()]<EOL>newdata.setfield(newfield._data, *newdata.dtype.fields[newfieldname])<EOL>newdata = newdata.view(MaskedRecords)<EOL>newmdtype = np.dtype([(n, bool_) for n in newdtype.names])<EOL>newmask = recarray(_data.shape, newmdtype)<EOL>[newmask.setfield(_mask.getfield(*f), *f)<EOL>for f in _mask.dtype.fields.values()]<EOL>newmask.setfield(getmaskarray(newfield),<EOL>*newmask.dtype.fields[newfieldname])<EOL>newdata._mask = newmask<EOL>return newdata<EOL>
Adds a new field to the masked record array, using `newfield` as data
and `newfieldname` as name. If `newfieldname` is None, the new field name is
set to 'fi', where `i` is the number of existing fields.