[yt-users] How to use if, elif, else statements in creating a derived field

Matthew Turk matthewturk at gmail.com
Sat Oct 26 07:29:46 PDT 2013


Hi Reju,

On Sat, Oct 26, 2013 at 2:50 AM, Reju Sam John <rejusamjohn at gmail.com> wrote:
> Dear Kacper,
>
> My logic was wrong, and yours is right. But with the following code
>
> @derived_field(name = "MyField")
> def my_new_field(field, data):
>     temp = data["MachNumber"].copy().fill(5.446361E-01)
>     #print temp
>
>     ind = np.where(data["MachNumber"] < 2)
>     temp[ind] = 1.9564E-3*((data["MachNumber"][ind]**2) - 1)
>     ind = np.where((data["MachNumber"] >= 2) and (data["MachNumber"]) < 100)
>     temp[ind] =
> (5.699327E-1*((data["MachNumber"][ind]-1)**4))/(data["MachNumber"][ind]**4)
> - (3.337557E-1*
> ((data["MachNumber"][ind]-1)**3))/((data["MachNumber"][ind])**4) +
> (4.173271E+0*
> ((data["MachNumber"][ind]-1)**2))/((data["MachNumber"][ind])**4) -
> (9.775620E+0* ((data["MachNumber"][ind]-1)))/((data["MachNumber"][ind])**4)+
> 5.455605E+0/(data["MachNumber"][ind])**4
>     return temp
>
> I am getting this  Traceback
>
>  line 41, in my_new_field
>     temp[ind] = 1.9564E-3*((data["MachNumber"][ind]**2) - 1)
> TypeError: 'NoneType' object does not support item assignment
>
> Would you suggest any solution?

I believe the problem is in your .fill() statement.  That doesn't
return an array, so you need to do it after the assignment.  For
instance:

temp = data["MachNumber"].copy()
temp.fill(5.446361E-01)

-Matt

>
>
>
> On Mon, Oct 21, 2013 at 2:58 PM, Kacper Kowalik <xarthisius.kk at gmail.com>
> wrote:
>>
>> On 10/21/2013 11:10 AM, Reju Sam John wrote:
>> > Dear all,
>> >
>> > I would like to create a derived field which should return a value
>> > according to specified conditions. My definition of new field is shown
>> > below.. But it is giving error.
>> >
>> >
>> > @derived_field(name = "MyField")
>> > def my_new_field(field, data):
>> >     if data.pf["MachNumber"] < 2 :
>> >         return (.5* (data["MachNumber"])**2) - 1
>> >     elif data.pf["MachNumber"] < 100 :
>> >         return (.5* (data["MachNumber"])**4) + (.5*
>> > (data["MachNumber"])**3)
>> >     else:
>> >        return 5.446361E-01
>> >
>> >
>> >
>> > Please suggest me how to implement if, elif, else statements in creating
>> > a
>> > derived field.
>>
>> Hi,
>> your derived fields looks fine (except for missing space in the
>> indentation of the last statement). Could you attach the backtrace?
>>
>> One thing that's confusing me is 'data.pf["MachNumber"]'. Do you have a
>> global parameter with such name defined or you'd rather create a MyField
>> based on local value of "MachNumber" field?
>> If the latter you could try something like this:
>>
>> @derived_field(name = "MyField")
>> def my_new_field(field, data):
>>     temp = data["MachNumber"].copy().fill(5.446361E-01)
>>     ind = np.where(data["MachNumber"] < 2)
>>     temp[ind] = 0.5 * data["MachNumber"][ind] ** 2 - 1.0
>>     ind = np.where((data["MachNumber"] >= 2) &
>>                    (data["MachNumber"]) < 100))
>>     temp[ind] = 0.5 * data["MachNumber"][ind] ** 4 + \
>>                 0.5 * data["MachNumber"][ind] ** 3
>>     return temp
>>
>>
>> Cheers,
>> Kacper
>>
>>
>>
>> _______________________________________________
>> yt-users mailing list
>> yt-users at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>
>
>
>
> --
> Reju Sam John
>
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>



More information about the yt-users mailing list