Hi Nick,

What you're seeing in those print statements is how yt's field detection system works. Before we actually select and return data, we pass your field function fake data to make sure it doesn't raise errors or it doesn't select fields that aren't available for your dataset.

Here's a script I made based on yours that helps explain what's going on:

https://gist.github.com/ngoldbaum/4d27987bda8ecbce5c9bd9b98bbbbf49

And here's the output of that script:

https://gist.github.com/ngoldbaum/475fcd827a53e4d02e76cb25711ff5ea

I made a couple modifications compared to your script. First, I added a line at the end that selects your new field from a data object, which causes real data to be fed into the field function after field detection completes. I also specified "units='auto'" in the call to add_field. The dimensions keyword is only used when "units='auto'" is set.

You can see that your field function is called twice. First with a `FieldDetector` instance and then the second time with a real yt data object. In general the field function might be called several times before real data is supplied to it. One way I commonly deal with this is the following hack to only see output when the field definition is called with real data:

    def NewField(field, data):
        q = data[('STAR', 'age')]
        if 'FieldDetector' in str(type(data)):
            print("BBB",data)
        return q

Although more often I use python's debugger via "import pdb; pdb.set_trace()" instead of print statements.

Hope that helps,

Nathan

On Sun, May 27, 2018 at 10:51 AM, Nick Gnedin <ngnedin@gmail.com> wrote:

Guys,

I am trying to add a new particle field, and apparently I am doing something wrong, but I could find the documentation for this:

import yt
def NewField(field,data):
    q = data[('STAR','age')]
    print("BBB",data)
    return q
d = yt.load("rei10_a0.1001/rei10_a0.1001.art")
print('AAA',d.all_data()[("STAR","age")])
d.add_field(("STAR","new-field"),function=NewField,dimensions="time",sampling_type="particle")

The result is here:

AAA [  1.35689106e+08   1.29578798e+08   1.15241829e+08 ..., 2.97175878e+07
   2.04648908e+07   1.35365980e+07] yr
BBB defaultdict(<function FieldDetector.__init__.<locals>.<lambda> at 0x2b9e698e3598>, {('STAR', 'BIRTH_TIME'): YTArray([ 1.]) (dimensionless), ('STAR', 'creation_time'): YTArray([ 1.]) (dimensionless), ('STAR', 'age'): YTArray([ 1.]) (dimensionless)})

It appears that the data object sent to NewField(field,data) has all its fields set to dimensionless.

I tried omitting sampling_type="particle", but it does not help, just generates an incomprehensible warning.

n
_______________________________________________
yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org