help-octave
[Top][All Lists]
Advanced

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

Re: Astro: Coordinate conversion


From: Isak Delberth Davids
Subject: Re: Astro: Coordinate conversion
Date: Fri, 10 Dec 2010 10:47:25 +0200

On 10 December 2010 00:24, Isak Delberth Davids <address@hidden> wrote:
On 9 December 2010 17:49, James Sherman Jr. <address@hidden> wrote:
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.

Thanks James,

First of all, lets forget about the tic-toc in this case --- I do not see much sense in my goal there. Otherwise, I will now happily use the function for RA succesfully as follows:

function ra2degree(hour, minute, second);
format long;
%% Converting Galactic coordinate RA (Right Ascension) to decimal degrees
%% ID Davids, December 9,2010

%% The following is the convention:
%% 1 h = 15 deg (from 360 deg = 24 hour)

%% 60 min = 1 h = 15 deg
%% 60 sec = 1 min = (15/60) deg, therefore 1 sec = (15/3600) deg
RA_degree = hour*15+minute*15/60+second*15/3600;
fprintf(stderr, '\n Your right ascension is %f degrees. \n\n', RA_degree);
endfunction
 
However, I notice that the unit conversions for DEC is different. In fact, the input format there is DEGREE:ARCMIN:ARCSEC, and therefor my function in that case is

function dec2degree(degree, arcminute, arcsecond);
format long;
%% Converting Galactic coordinate DEC (declination) to decimal degrees
%% ID Davids, December 9,2010

%% The following is the convention:
%% DEC angles are most often measured in degrees (deg), arc minutes (arc min, ')
%% and arc seconds (arc sec, "), where 1 deg = 60' = 3,600"
DEC_degree = degree + arcminute/60 + arcsecond/3600;
fprintf(stderr, '\n Your declination is %f degrees. \n\n', DEC_degree);
endfunction

Now, I have a problem with the latter for cases where the degree<0. Yes, the angle can be negative for points below the galactic plane (click the drop-down arrow at https://www.swift.psu.edu/secure/toop/convert.htm). For example,
 dec2degree(-48,45,4) results into -47.248889 (which is wrong), instead of
-48.7511

Now, actually the correct value (for negative degrees ) is obtained by doing:
DEC_degree = -1*(-degree + arcminute/60 + arcsecond/3600)

In that light now, I changed the function dec2degree to

function dec2degree(degree, arcminute, arcsecond);
format long;

%% Converting Galactic coordinate DEC (declination) to decimal degrees
%% ID Davids, December 9,2010

%% The following is the convention:
%% DEC angles are most often measured in degrees (deg), arc minutes (arc min, ')
%% and arc seconds (arc sec, "), where 1 deg = 60' = 3,600"
if (degree <= 0)
    DEC_degree = (-1)*(-degree + arcminute/60 + arcsecond/3600)
        else
            DEC_degree = degree + arcminute/60 + arcsecond/3600;
        end
end
fprintf(stderr, '\n Your declination is %f degrees. \n\n', DEC_degree);
endfunction

Which does not run! Now where am I guilty of the offense "error: subscript indices must be either positive integers or logicals."?

Regards,

Isak

* * * * * * * * * * * * * * *
*  Isak Delbert Davids *
*  +264-81-33 049 33  *
*  +264-61-206 3789   *
*    Namibia, Afrika     *
* * * * * * * * * * * * * * *

Hi all,

I should say I got this matter resolved --- see end of this message. But for interest sake, I also learned that one can use the function 'datevec'

RA = '15:27:05';
fmt = 'HH:MM:SS';
d = datevec(RA,fmt); % where in general, [Yr, Mo, Day, Hr, Min, Sec] = datevec(...)
t = d(4) + d(5)/60 + d(6)/3600; % time in hours
RA_deg = 360*t/24;

Thanks for all contributions,

Isak Davids
___________________________________________________________

function dec2degree(degree, arcminute, arcsecond);
format long;
%% Converting Galactic coordinate DEC (declination) to decimal degrees
%% ID Davids, December 9,2010
%% The following is the convention:
%% DEC angles are most often measured in degrees (deg), arc minutes (arc min, ')
%% and arc seconds (arc sec, "), where 1 deg = 60' = 3,600"
if (degree < 0)
    DEC_degree = (-1)*(abs(degree) + arcminute/60 + arcsecond/3600);
    elseif (degree >= 0)
    DEC_degree = degree + arcminute/60 + arcsecond/3600;
end
fprintf(stderr, '\n Your declination is %f degrees. \n\n', DEC_degree);
endfunction
___________________________________________________________


reply via email to

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