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

[MacPerl] Converting RA and DEC to cartesians in Perl



This requires that you have a copy of the "Preliminary Version of the 
Third Catalogue of Nearby Stars," which I haven't included as it's around 
800K.  If you want to TRY this script, you can get the catalog at:

ftp://adc.gsfc.nasa.gov/pub/adc/archives/catalogs/5/5070A/catalog.dat.gz

Be sure to get the ReadMe as well

ftp://adc.gsfc.nasa.gov/pub/adc/archives/catalogs/5/5070A/ReadMe

Some caveats:

I'm redirecting STDERR to a second window.  Why?  Because this code still 
generates plenty of warnings, and they get in the way of the output 
(which itself is really only there for debugging purposes).  The problem 
seems to be in parsing the input string, which isn't a fixed length.  I'm 
working on it.  Suggestions are welcome.

Don't trust the output!  Just because it runs doesn't mean it works! 
Also, I need a way to truncate the output coords to three or four 
signifigant digits.  Not round off, TRUNCATE.  Reason: the data are 
simply not as precise as the numbers make it look.  Three or four 
signifigant digits are about all that really make sense.  Everything 
after that is pure fiction anyway.

I may pull Math::Trig.  All I use from it is deg2rad, which is pretty 
easy to set up by hand. Not calling the module may speed startup of the 
script a bit, and Math::Trig overides the built-in sin() and cos() 
functions (That's a feature according to the authors).

Let me know what you think.

#!usr/bin/perl -w

# USAGE: catalog.plx File_Name
#          where File_Name is a star catalog.
# Mac USAGE: save as droplet.  Drag & Drop target input file
# or just double-click on the droplet and let StandardFile do
# it for you.
#
# Revision History:
# ver 0.2a  Brian McNett <webmaster@mycoinfo.com>
# Redirects STDERR (for now)
# wrote calcs for cartesian coords, luminosity and distance
# prints to STDOUT (for now)
# (syntax check ok)
# IT RUNS! (but doesn't produce correct output :-( )
#
# ver 0.1a  Brian McNett <webmaster@mycoinfo.com>
# use strict and -w
# added Math::Trig (for deg2rad())
# added vars
# detect bad start on Mac
# (syntax check ok) this DOESN'T mean it RUNS!
#
# ver 0.0a Richard L. Grubb, <richard.l.grubb@boeing.com>
# slurps input file (well, it's a start)

use strict;
use Math::Trig;
use vars qw(
	$file $input_file

	$ident $Comp $DistRel
	$RAh $RAm $RAs
	$DE_sign $DEd $DEm $DEs
	$pm $u_pm $pmPA
	$RV $n_RV $Sp $r_Sp
	$Vmag $r_Vmag $n_Vmag
	$B_V $r_B_V $n_B_V
	$U_B $r_U_B $n_U_B
	$R_I $r_R_I $n_R_I 
	$trplx $e_trplx
	$plx $e_plx $n_plx 
	$Mv $n_Mv $q_Mv
	$U $V  $W 
	$HD $DM $Giclas $LHS $Other
	$Remarks

	$alpha $delta
	$phi $theta $rho
	$rVect $Xe $Ye $Ze
	$Xg1950 $Yg1950 $Zg1950
	$Xg2000 $Yg2000 $Zg2000
	
	$luminosity
);

open(STDERR, '>Dev:Console:Messages');

# for Mac users who forget to drag&drop the input file
if ($^O eq 'MacOS') {
	if ($#ARGV < 0) {
		use Mac::StandardFile;
		$file = StandardGetFile('', 'TEXT');
		if ($file->sfGood()) {
			push(@ARGV, $file->sfFile());
		} else {
		  exit(1);
		}
	}
}

print "Opening input data...\n";

$input_file = $ARGV[0];
open( IN_FILE, $input_file) || die "Can't open file $input_file $!\n" ;

while ( <IN_FILE> ) {
	$ident    = substr($_,  0, 8); #A8   #Identifier ; see remarks.
	$Comp     = substr($_,  8, 2); #A2   #Components (A,B,C,... )
	$DistRel  = substr($_, 10, 1); #A1   #[pqsx] Reliability of the distance
	                               #12 is unused
	$RAh      = substr($_, 12, 2); #I2   #? Right Ascension B1950 (hours)
	                               #15 is unused
	$RAm      = substr($_, 15, 2); #I2   #? Right Ascension B1950 (minutes)
	                               #18 is unused
	$RAs      = substr($_, 18, 2); #I2   #? Right Ascension B1950 (seconds)
	                               #21 is unused
	$DE_sign  = substr($_, 21, 1); #A1   #Declination B1950 (sign)
	$DEd      = substr($_, 22, 2); #I2   #? Declination B1950 (degrees)
	                               #25 is unused
	$DEm      = substr($_, 25, 4); #F4.1 #? Declination B1950 (minutes)
	                               #30 is unused
	$pm       = substr($_, 30, 6); #F6.3 #? Total proper motion
	$u_pm     = substr($_, 36, 1); #A1   #Uncertainty flag (:) on pm
	$pmPA     = substr($_, 37, 5); #F5.1 #? Direction angle of proper motion
	                               #43 is unused
	$RV       = substr($_, 43, 6); #F6.1 #? Radial velocity
	                               #50 is unused
	$n_RV     = substr($_, 50, 3); #A3   #Remark on RV
	                               #54 is unused
	$Sp       = substr($_, 54,12); #A12  #Spectral type or color class
	$r_Sp     = substr($_, 66, 1); #A1   #Selected sources
	$Vmag     = substr($_, 67, 6); #F6.2 #Apparent magnitude
	$r_Vmag   = substr($_, 73, 1); #A1   #Note on origin of magnitude
	$n_Vmag   = substr($_, 74, 1); #A1   #[J] joint magnitude
	$B_V      = substr($_, 75, 5); #F5.2 #? color
	$r_B_V    = substr($_, 80, 1); #A1   #Note on origin of magnitude
	$n_B_V    = substr($_, 81, 1); #A1   #Joint color
	$U_B      = substr($_, 82, 5); #F5.2 #? color
	$r_U_B    = substr($_, 87, 1); #A1   #Note on origin of magnitude
	$n_U_B    = substr($_, 88, 1); #A1   #Joint color
	$R_I      = substr($_, 89, 5); #F5.2 #? color
	$r_R_I    = substr($_, 94, 1); #A1   #Note on origin of magnitude
	$n_R_I    = substr($_, 95, 1); #A1   #Joint color
	$trplx    = substr($_, 96, 6); #F6.1 #? Trigonometric parallax
	$e_trplx  = substr($_,102, 5); #F5.1 #? Standard error of trig. parallax
	                               #108 is unused
	$plx      = substr($_,108, 6); #F6.1 #? Resulting parallax
	$e_plx    = substr($_,114, 5); #F5.1 #? Standard error of res.  parallax
	$n_plx    = substr($_,119, 1); #A1   #[rwsop] Code on plx
	                               #121 is unused  
	$Mv       = substr($_,121, 5); #F5.2 #Absolute visual magnitude
	$n_Mv     = substr($_,126, 2); #A2   #Note on Mv, copied from cols 74-75
	$q_Mv     = substr($_,128, 1); #A1   #[a-f] Quality of absolute magnitude
	                               #130 and 131 are unused
	$U        = substr($_,131, 4); #I4   #? U space velocity component in 
the galactic plane and directed to the galactic center
	                               #136 is unused
	$V        = substr($_,136, 4); #I4   #? V space velocity component in 
the galactic plane and in the direction of galactic rotation
	                               #141 is unused
	$W        = substr($_,141, 4); #I4   #? W space velocity component in 
the galactic plane and in the direction of the North Galactic Pole
	                               #146 is unused
	$HD       = substr($_,146, 6); #I6   #[15/352860]? designation
	                               #153 is unused
	$DM       = substr($_,153,12); #A12  #Durchmusterung number BD / CD / CP

	# We can't be certain the line doesn't just end at this point, so...

	if (defined (substr($_,165, 1))) {
	$Giclas   = substr($_,166, 9); #A9   #number        <...>
	}
	if (defined (substr($_,175, 1))) {
	$LHS      = substr($_,176, 5); #A5   #number        <...>
	}
	if (defined (substr($_,181, 1))) {
	$Other    = substr($_,182, 5); #A5   #Other designations
	}
		if (defined (substr($_,187, 1))) {
	$Remarks  = substr($_,188,69); #A69  #Additional identifications (LTT, 
LFT, Wolf, Ross, etc.) and remarks
	}

# Convert Location to Cartesian System

print "parsing record... \n";

&calc_cartesian;

print "$ident $Comp \n ==========\n" ;
print "Coordinates (Earth)    [ $Xe , $Ye , $Ze ] \n" ;
print "Coordinates (Galactic) [ $Xg1950 , $Yg1950 , $Zg1950 ] \n" ;
print "==============================================\n";

# Calculate Neighbors

# Determine Habitable Systems

$luminosity = 1/(2.5**($Mv - 4.85));
print "Luminosity: $luminosity \n";
print "Spectral Class: $Sp ($r_Sp -- see notes)\n";

# Insert Planets, Moons
# Write out resource compiler sources
}


### SUBROUTINES ###

sub calc_cartesian {
if ($RAh eq '  ') {

$Xe     = 0.000;
$Ye     = 0.000;
$Ze     = 0.000;

$Xg1950 = 0.000;
$Yg1950 = 0.000;
$Zg1950 = 0.000;

# $Xg2000 = 0.000;
# $Yg2000 = 0.000;
# $Zg2000 = 0.000;

} else {

if ($plx =~ '       ') {
	$rho = 0;
	} else {
	$rho = 1000/$plx;
	}

	$alpha = (($RAh * 15)+($RAm * 0.25)+($RAs * 0.0041666));
	$delta = (($DEd + $DEm/60) * join('' , $DE_sign,$DEd));

	# calculate Phi, Theta (in radians)
	# ---------------------------------
	$phi     = deg2rad($alpha);
	$theta   = deg2rad($delta);

	# Earth-equatorial cartesian coords
	# ---------------------------------
	$rVect = $rho   * cos($theta);
	$Xe    = $rVect * cos($phi);
	$Ye    = $rVect * sin($phi);
	$Ze    = $rho   * sin($theta);

	# Galactic-equatorial cartesian coords (B1950)
	# --------------------------------------------
	$Xg1950 = -(0.0672 * $Xe) - (0.8727 * $Ye) - (0.4835 * $Ze);
	$Yg1950 =  (0.4927 * $Xe) - (0.4504 * $Ye) + (0.7445 * $Ze);
	$Zg1950 = -(0.8676 * $Xe) - (0.1884 * $Ye) + (0.4602 * $Ze);

	# Galactic-equatorial cartesian coords (J2000)
	# commented out until implementation
	# --------------------------------------------
	# $Xg2000 = -(0.0550 * $Xe) - (0.8734 * $Ye) - (0.4839 * $Ze);
	# $Yg2000 =  (0.4940 * $Xe) - (0.4449 * $Ye) + (0.7470 * $Ze);
	# $Zg2000 = -(0.8677 * $Xe) - (0.1979 * $Ye) + (0.4560 * $Ze);
	}
}


# Fungal Parataxonomy                   Mycology Information (Mycoinfo)
# Webmaster, Staff Writer      **The World's First Mycology E-Journal**   
# <mailto:webmaster@mycoinfo.com>            <http://www.mycoinfo.com/> 
#
# First they ignore you. Then they laugh at you. Then they fight you.
# Then you win.                                     --Mohandas Gandhi


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org