<div dir="ltr">I would be a fan of making a "force_units" kwarg to enable one to create a new field with different units than those specified.  This could resolve some of the problems with native frontend fields being stuck in the wrong unit, like I was running into last week.  As long as force_unit is off by default, it would just enable us to give full control to the user.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 24, 2015 at 4:25 AM, Nathan Goldbaum <span dir="ltr"><<a href="mailto:nathan12343@gmail.com" target="_blank">nathan12343@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Elizabeth,<div><br></div><div>This is discussed in the docs here:</div><div><br></div><div><a href="http://yt-project.org/doc/developing/creating_derived_fields.html#defining-a-new-field" target="_blank">http://yt-project.org/doc/developing/creating_derived_fields.html#defining-a-new-field</a><br><div><br></div><div>You can do one of two things. First, the way I'd handle this is to ensure that your field definition is returning data that have units of Kelvin.</div><div><br></div><div>For example, something like:</div><div><br></div><div>    from yt.utilities.physical_constants import kb, mh</div><div><br></div><div>    mu = 0.6</div><span class=""><div><br></div><div><span style="font-size:12.8px">    @derived_field(name=‘Temp’, units=“K”)</span><br></div></span><div><span style="font-size:12.8px">    def _Temp(field, data):</span></div><div><span style="font-size:12.8px">        return data['thermal_energy'] / kb * mu * mh</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">You could also make mu a field parameter rather than a constant, or if you have a simulation where mu varies with position, make a mean molecular weight derived field.</span></div><div><span style="font-size:12.8px"><br></span></div><div>You can also ensure that your field definition returning a dimensionless value by stripping the units:</div></div><span class=""><div><br></div><div>    <span style="font-size:12.8px">@derived_field(name=‘Temp’, units=“K”)</span></div></span><div><span style="font-size:12.8px">    def _Temp(field, data):</span></div><div><span style="font-size:12.8px">        ret = data['thermal_energy'])</span></div><div><span style="font-size:12.8px">        return ret.ndarray_view()</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">We don't currently have a way to *force* the units to be whatever you specify in add_field. That said, I don't think it would be terribly hard to implement. </span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">We already have units='auto', which forces the units to be the same as the return value of your field function. We might add a keyword 'force_units=True' to add_field which does what you're looking for. I'd be happy to review a pull request adding this, or feel free to open an issue about it to add to our project backlog.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Also, worth pointing out that this was designed this way not to anger users, but to protect them from making mistakes in their field units, which we found to be a common cause of bugs in yt-2. It does mean you need to "buy into" the unit system in your field definition, but the bonus is that you can be much more sure that you're not making a mistake.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Hope that helps,</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Nathan</span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 24, 2015 at 12:41 AM, Elizabeth Tasker <span dir="ltr"><<a href="mailto:tasker@astro1.sci.hokudai.ac.jp" target="_blank">tasker@astro1.sci.hokudai.ac.jp</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi everyone,<br>
<br>
While using yt-3, I created a derived field:<br>
<br>
@derived_field(name=‘Temp’, units=“K”)<br>
def _Temp …..<br>
<br>
It crashes with a YTFieldUnitError because it believes the units should be cm**2/s**2. I understand why yt thinks this — I’m using thermal_energy in the definition for Temp. However, the units are truly Kelvin and I think I should be able to specify whatever I like in “units”!<br>
<br>
Is there a way to stop yt crashing when it disagrees with your life choices?<br>
<br>
Elizabeth<br>
_______________________________________________<br>
yt-users mailing list<br>
<a href="mailto:yt-users@lists.spacepope.org" target="_blank">yt-users@lists.spacepope.org</a><br>
<a href="http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org" rel="noreferrer" target="_blank">http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org</a><br>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
yt-users mailing list<br>
<a href="mailto:yt-users@lists.spacepope.org">yt-users@lists.spacepope.org</a><br>
<a href="http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org" rel="noreferrer" target="_blank">http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Cameron Hummels<div><span style="font-size:12.8000001907349px">NSF Postdoctoral Fellow</span></div><div><span style="font-size:12.8000001907349px">Department of Astronomy</span></div><div><span style="font-size:12.8000001907349px">California Institute of Technology</span><br></div><div><a href="http://chummels.org" style="font-size:12.8000001907349px" target="_blank">http://chummels.org</a><br></div></div></div></div>
</div>