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

Kacper Kowalik xarthisius.kk at gmail.com
Mon Oct 21 02:28:48 PDT 2013


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 901 bytes
Desc: OpenPGP digital signature
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20131021/aa5cec50/attachment.sig>


More information about the yt-users mailing list