0

I went onto one line of code, which I want to convert from numpy syntax to regular python 2.7 syntax:

SunAz=SunAz + (SunAz < 0) * 360

source: https://github.com/Sandia-Labs/PVLIB_Python/blob/master/pvlib/pvl_ephemeris.py#L147

If we pretend that the numpy array is one dimensional, can it be translated to regular python 2.7 syntax like so:

newSunAz = []
for item in SunAz:
    if item < 0:
        newItem = item + item*360
        newSunAz.append(newItem)
    else:
        newSunAz.append(item)

??

Thank you for the help.

3
  • 1
    There is nothing wrong with your code. It should work fine. Commented Nov 10, 2014 at 18:30
  • Are you just asking these questions to understand the effect of the numpy syntax? I see you've asked three such questions now. If your goal is simply to understand what numpy operations do, it might be better to ask one question with a few different examples, because as far as I can see, the three questions you've asked all have basically the same answer, which is "numpy operations work componentwise". Commented Nov 10, 2014 at 18:32
  • Thank you for the reply hagubear. @BrenBarn - I am just afraid that I may not get an answer to all my questions if I ask all of them in one topic. I thought that separating them into a couple of individual ones, would disenable skipping replies to some of them. Commented Nov 10, 2014 at 18:36

2 Answers 2

2

I'm not sure that this would be the translation. The line

SunAz=SunAz + (SunAz < 0) * 360

(SunAz < 0) creates a boolean array, True where the angle is negative, and false otherwise. Multiplying False by a constant gives 0, and True is interpreted to be a 1. This line actually says, "shift the angle by 360 degrees if negative, otherwise leave it be".

So a more literal translation would be the following:

SunAz = [angle + 360 if angle < 0 else angle for angle in SunAz]
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

new_sun_az = [i+360 if i > 0 else i for i in sun_az]

The main difference is that most operators are applied to the list object in plain Python lists and return a single result, while they return a numpy array where each item is the result of the operation applied to the corresponding item on the original array for numpy arrays.

>>> import numpy as np
>>> plainlist = range(5)
>>> plainlist
[0, 1, 2, 3, 4]
>>> plainlist > 5   # single result
True
>>> nparray = np.array(plainlist)
>>> nparray
array([0, 1, 2, 3, 4])
>>> nparray > 5    # array of results
array([False, False, False, False, False], dtype=bool)
>>> 

[update]

Mike's answer is right. My original answer was:

new_sun_az = [i+i*360 if i > 0 else i for i in sun_az]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.