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

Reju Sam John rejusamjohn at gmail.com
Mon Oct 21 03:31:19 PDT 2013


Dear Kacper,
 This is my backtrace....

Traceback (most recent call last):
  File "cosmic_ray_fra.py", line 50, in <module>
    p = pc.add_slice("MyField", 2)
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/visualization/plot_collection.py",
line 430, in add_slice
    center=center, **field_parameters)
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py",
line 1058, in __init__
    self._refresh_data()
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py",
line 320, in _refresh_data
    self.get_data()
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py",
line 859, in get_data
    if self._generate_field(field):
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py",
line 362, in _generate_field
    self[field] = self.pf.field_info[field](self)
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/field_info_container.py",
line 395, in __call__
    dd = self._function(self, data)
  File "cosmic_ray_fra.py", line 32, in my_new_field
    if data.pf["MachNumber"] < 2:
  File
"/data1/pdf/csurajit/yt-x86_64/src/yt-hg/yt/data_objects/static_output.py",
line 147, in __getitem__
    raise KeyError(key)
KeyError: 'MachNumber'


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20131021/ed854540/attachment.htm>


More information about the yt-users mailing list