help-octave
[Top][All Lists]
Advanced

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

Re: Astro: Coordinate conversion


From: James Sherman Jr.
Subject: Re: Astro: Coordinate conversion
Date: Thu, 9 Dec 2010 10:49:36 -0500

On Thu, Dec 9, 2010 at 10:17 AM, Isak Delberth Davids <address@hidden> wrote:
Hi all,

The code below converts a set of three time values (h,min,secs), entered by a user, into a single decimal degree value. This essentially being the coordinate conversion in astronomy.

My code does the job, but I am sure someone can help me do it shorter, faster and therefore smarter:

%% Octave script to convert the right ascension (RA) and the declination (DEC) of a
%% celestial position from hours:minutes:seconds format into decimal degrees.
%% Compare result with: https://www.swift.psu.edu/secure/toop/convert.htm

%% The following is the convention:
%% 1 h = 15 deg
%% 60 min = 1 h = 15 deg
%% 60 sec = 1 min = (15/60) deg, therefore 1 sec = (15/3600) deg

RA = zeros(3,1)';
RA(1) = input('Please enter the hours of the RA(1) ');
RA(2) = input('Please enter the minutes of the RA(2) ');
RA(3) = input('and finally please enter the seconds of the RA(3) ');

RA_deg = RA(1)*15 + RA(2)*(15/60) + RA(3)*(15/3600);

computerTime = toc; % turn of the timer
fprintf(stderr, '\n Your right ascension is %f degrees. This code has a terrible structure, in fact it took %f seconds of your time --- improve it! \n',RA_deg, computerTime);

       Cheers,
                   Isak


I think what you want to do is make your procedure into a function.  Put the following (without the ='s) into a file called radec2degree.m
=====
function degree = radec2degree(hour, minute, second)

degree = hour*15+minute*15/60+second*15/3600;

endfunction
=====
Then, when you're in the same directory as this file (or you put the file into your path using "addpath"), you call this function simply doing:
deg = radec2degree(1,2,3);
should give you the degree value for something at 1 hour, 2 minutes, and 3 seconds.

Though I think I should note, that (I'm assuming you called tic right before calling the script you have) most of the time taken in your script would be the actual physical time it would take a person to enter in the numbers when prompted.  If you still want the user to be prompted, then I don't think you're going to get any improvement.

Hope this helps.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]