Easy question on for loops and if statements [message #91451] |
Thu, 16 July 2015 10:15  |
larosaant94
Messages: 3 Registered: July 2015
|
Junior Member |
|
|
I am plotting the magnetic field longitude and using the ATAN function to do so. Though the ATAN function gives a result between the range of -180 to +180. I wish to use a range of
0 to 360.
A look at the code:
Bx = data(10,*)
By = data(11,*)
longitude = 180/!Pi* atan(By, Bx)
FOR j=0,N_Elements(longitude)-1 DO BEGIN
IF longitude[j] lt 0 THEN BEGIN
longitude = 360 + longitude
ELSE
longitude = longitude
ENDFOR
;Does the actual plotting-----------
plot, time, longitude, yrange=[0, 360], yticks=4, yminor=6, $
ytitle='Lambda', position=[0.1, 0.24, 0.95, 0.31], xtickname=blnklb
plots, [min(time), max(time)], 180.0*[1,1], linestyle=2
I want any number less than 0 to get 360 added to it and then once this is completed plot all the data points including the ones above 0!
Thanks!
|
|
|
Re: Easy question on for loops and if statements [message #91453 is a reply to message #91451] |
Thu, 16 July 2015 11:24   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/16/15 11:15 AM, larosaant94@gmail.com wrote:
> I am plotting the magnetic field longitude and using the ATAN
> function to do so. Though the ATAN function gives a result
> between the range of -180 to +180. I wish to use a range of
> 0 to 360.
>
> A look at the code:
>
> Bx = data(10,*)
> By = data(11,*)
>
> longitude = 180/!Pi* atan(By, Bx)
>
> FOR j=0,N_Elements(longitude)-1 DO BEGIN
> IF longitude[j] lt 0 THEN BEGIN
> longitude = 360 + longitude
> ELSE
> longitude = longitude
> ENDFOR
>
> ;Does the actual plotting-----------
>
> plot, time, longitude, yrange=[0, 360], yticks=4, yminor=6, $
> ytitle='Lambda', position=[0.1, 0.24, 0.95, 0.31], xtickname=blnklb
> plots, [min(time), max(time)], 180.0*[1,1], linestyle=2
>
>
>
> I want any number less than 0 to get 360 added to it and then once
> this is completed plot all the data points including the ones above 0!
I don't see a specific question here. I do notice you had some oddities
in your FOR loop, so I corrected:
for j = 0L, n_elements(longitude) - 1L do begin
if (longitude[j] lt 0) then begin
longitude[j] = 360 + longitude[j]
endif
endfor
But really, I would just do the following:
longitude += (longitude lt 0) * 360
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
|
|
|
|
|
|