[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[python #MVS-634658]: Metpy LCL question

Hello! Thanks for reaching out to us.

Please be sure to include the errors you receive so we can diagnose more 
quickly and easily! In executing your provided code I ran into two main issues:

1.) Units. For now and for some of MetPy, you may have to be careful that you 
are creating an array that contains individual unit quantities as opposed to a 
quantity with an array of values within it. In the case of masked_arrays you 
can quickly achieve this by multiplying your unit on the "left", e.g. 
`units.degC * temp_masked` as opposed to `temp_masked * units.degC`, or 
construct the `units.Quantity` yourself for complete control.

Importantly, from here any "multiplication" of a new unit is considered a real 
calculation! So if you take a quantity of temperature with the units kelvin and 
multiply it by `units.kelvin` again you will receive a new Quantity with units 
of Kelvin*Kelvin. In your code you have multiplied by units more than once in 
trying to overcome the initial issue of not having the correctly-structured 
quantity. Also for your own info, note that our unit support here allows you to 
convert units without having to do so by hand.

2.) Masked_array handling. This is part of your issue with the units. However, 
once you have created this "masked array with units attached" instead of "array 
containing masked arrays each with units" you will run into our LCL calculation 
can not handle masked_arrays. So, you will have to dig out `temp.data` (if 
there are no masked/missing values) or `temp.filled(temp.fill_value)` (if there 
may be masked/missing values) to pull out the un-masked data arrays before 
calculating LCL. Importantly if you do this step first, you shouldn't have to 
worry about "correctly" assigning units as I described above. It's still a good 
thing to keep an eye on!

So the quickest way to fix what I ran into would be something like this,

  temp_masked = dataset.variables['temp'][:].squeeze()
  sfct = units.kelvin * np.mean(temp_masked.filled(temp_masked.fill_value), 0)
  sfct.ito(units.degC)  # the same as `sfct = sfct.to(units.degC)`, unit 

and then get rid of all of your extra unit multiplication later in your script 
(lines 41-43 and line 46). Finally there is a small issue in defining your 
levels for your plot. In your `np.arange` you can specify `lclh.m` instead of 
`lclh` to pull out the max and min values only (no units) so that you can 
define your levels in this way. I hope all of this helps. If there are any 
other issues or if you could use any further clarification, please don't 
hesitate to follow up!

All the best,


> Hi there,
> I am trying to run some code to calculate the lcl for each point in a 
> (428,614) grid from the NAM. However, when I use two for loops it takes about 
> an hour just to calculate every single one then it gives me an error when I 
> plot.  I have attached the code in question so you can see what the problem 
> might be.

Ticket Details
Ticket ID: MVS-634658
Department: Support Python
Priority: Low
Status: Closed
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.