File indexing completed on 2024-09-08 03:28:58
0001 #!/usr/bin/perl 0002 # 0003 # hipdatatomysql.pl Put star data from file (in the plain-text data format used by KStars) into a database 0004 # 0005 # CAUTION: Will truncate the table supplied! 0006 # 0007 # NOTE: This script reads Hipparcos text data file on stdin. 0008 # use cat <hipparcos data file> | <this script> 0009 0010 use strict; 0011 use DBI; 0012 use HTMesh; 0013 0014 my $ERROR; 0015 my @STARS; 0016 0017 my $VERBOSE = 0; 0018 0019 my $cnt; 0020 0021 # For database handling 0022 my $db_db = shift; 0023 my $db_user = shift; 0024 !($db_db eq "") and !($db_user eq "") or print "USAGE: " . $0 . " <database name> <MySQL Username> [[MySQL Password [Table]]\n" and exit; 0025 my $db_pass = shift; 0026 my $db_tbl = shift; 0027 0028 my $db_query = qq/CREATE DATABASE IF NOT EXISTS `$db_db`;/; 0029 my $db_select_query = qq/USE `$db_db`/; 0030 0031 my $tbl_query = qq/ 0032 CREATE TABLE IF NOT EXISTS `$db_tbl` ( 0033 `trixel` varchar(14) NOT NULL, 0034 `ra` double NOT NULL COMMENT 'RA Hours', 0035 `dec` double NOT NULL COMMENT 'Declination Degrees', 0036 `dra` double NOT NULL, 0037 `ddec` double NOT NULL, 0038 `pm` double NOT NULL, 0039 `parallax` double NOT NULL, 0040 `mag` float NOT NULL, 0041 `bv_index` float NOT NULL, 0042 `spec_type` char(2) NOT NULL, 0043 `mult` tinyint(1) NOT NULL, 0044 `var_range` float default NULL, 0045 `var_period` float default NULL, 0046 `name` varchar(70) default NULL, 0047 `gname` varchar(15) default NULL, 0048 `UID` int(11) NOT NULL auto_increment, 0049 PRIMARY KEY (`UID`), 0050 UNIQUE KEY `UID` (`UID`), 0051 KEY `trixel` (`trixel`,`pm`,`mag`) 0052 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;/; 0053 0054 my $tbl_trunc_query = qq/TRUNCATE TABLE `allstars`/; 0055 0056 # For the HTMesh 0057 my $level = 3; 0058 0059 # Create a new HTMesh, of level $level 0060 my $mesh = new HTMesh($level); 0061 0062 # Get the database handle 0063 my $dbh = DBI->connect("DBI:mysql:", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 0 }); 0064 0065 my @fields = qw/trixel ra dec dra ddec pm parallax mag bv_index 0066 spec_type mult var_range var_period name gname/; 0067 0068 $dbh->do($db_query); 0069 $dbh->do($db_select_query); 0070 $dbh->do($tbl_query); 0071 $dbh->do($tbl_trunc_query); 0072 $dbh->commit(); 0073 0074 while(<>) { 0075 m/^\s*#/ and do { print if $VERBOSE; next}; 0076 m/\S/ or do { print if $VERBOSE; next}; 0077 chomp; 0078 my $star = kstars_unpack($_) or do { 0079 warn sprintf("%4d: FORMAT ERROR ($ERROR):\n$_\n", $.); 0080 next; 0081 }; 0082 $star->{line} = $.; 0083 0084 $VERBOSE and print_star_line($star); 0085 0086 $star->{trixel} = $mesh->lookup_name($star->{ra}, $star->{dec}); 0087 $star->{var_range} eq '' and $star->{var_range} = '0'; 0088 $star->{var_period} eq '' and $star->{var_period} = '0'; 0089 0090 my $query ||= qq/INSERT INTO `$db_tbl` (/ . 0091 join(", ", map {"`$_`"} @fields) . 0092 qq/) VALUES (/ . 0093 join(", ", map {"?"} @fields) . 0094 qq/)/; 0095 0096 my $sth ||= $dbh->prepare($query); 0097 0098 $sth->execute(@$star{@fields}); 0099 0100 } 0101 0102 $dbh->commit(); 0103 0104 $dbh->disconnect(); 0105 0106 exit; 0107 0108 #---------------------------------------------------------------------------- 0109 #--- Subroutines ------------------------------------------------------------ 0110 #---------------------------------------------------------------------------- 0111 0112 sub print_star_line { 0113 my $star = shift; 0114 #printf "%8.2f ", $star->{pm}; 0115 printf "%s %s %s%s%s %s%s%s %s%s", 0116 @$star{qw/ra_str dec_str dra ddec parallax mag bv_index spec_type mult/}; 0117 my $s2; 0118 $star->{var_range} || $star->{var_period} and do { 0119 $s2 = sprintf " %4.2f %6.2f", @$star{qw/var_range var_period/}; 0120 }; 0121 $star->{name} || $star->{gname} and $s2 ||= " " x 12; 0122 $s2 and print $s2; 0123 $star->{name} and print $star->{name}; 0124 $star->{name} and $star->{gname} and print " "; 0125 $star->{gname} and print ": $star->{gname}"; 0126 print "\n"; 0127 } 0128 0129 sub kstars_unpack { 0130 my $line = shift; 0131 chomp $line; 0132 my $s1 = substr($line, 0, 60, ""); 0133 $s1 =~ m{^(\d{6}\.\d\d)\s # RA HHMMSS.SS 0134 ([+-]\d{6}\.\d)\s # DEC DDMMSS.SS 0135 ([+-]\d{6}\.\d) # dRA/dt 0136 ([+-]\d{6}\.\d) # dDec/dt 0137 (\d{5}\.\d)\s # Parallax 0138 ([\d-]\d\.\d\d) # Magnitude 0139 ([\d-]\d\.\d\d) # B-V index 0140 ([A-Z ].|sd)\s # Spectral Type 0141 (\d) # Multiplicity 0142 }x or do 0143 { 0144 $ERROR = "Positional Error (0-59)"; 0145 return; 0146 }; 0147 my $star = { 0148 ra_str => $1, 0149 dec_str => $2, 0150 dra => $3, 0151 ddec => $4, 0152 parallax => $5, 0153 mag => $6, 0154 bv_index => $7, 0155 spec_type => $8, 0156 mult => $9, 0157 line => $., 0158 pm => sqrt($3 * $3 + $4 * $4), 0159 }; 0160 $star->{ra} = hms_to_hour($star->{ra_str}); 0161 $star->{dec} = hms_to_hour($star->{dec_str}); 0162 $star->{ra_hm} = hms_to_hm($star->{ra_str}); 0163 $star->{dec_hm} = hms_to_hm($star->{dec_str}); 0164 0165 $line or return $star; 0166 0167 my $s2 = substr($line, 0, 12, ""); 0168 #print "$s2\n"; 0169 $s2 =~ m/^\s+(\d\.\d\d)? # var range 0170 \s+(\d+\.\d\d)? # var period 0171 /x or do 0172 { 0173 $ERROR = "Variable params (61 - 71)"; 0174 return; 0175 }; 0176 $star->{var_range} = $1; 0177 $star->{var_period} = $2; 0178 $line or return $star; 0179 $star->{gname} = (($line =~ s/^\s*,\s*([^\s,][^,]*),?\s*//) ? $1 : ""); 0180 $line =~ s/^[\s,]*//; 0181 $star->{name} = $line ? $line : ""; 0182 return $star; 0183 } 0184 0185 sub hms_to_hour { 0186 my $string = shift; 0187 $string =~ /^([+-]?)(\d\d)(\d\d)(\d\d(?:\.\d*)?)/ or return; 0188 my ($h, $m, $s) = ($2, $3, $4); 0189 my $sign = ($1 eq '-') ? -1 : 1; 0190 $m += $s / 60; 0191 return $sign * ($h + $m / 60); 0192 } 0193 0194 sub hms_to_hm { 0195 my $string = shift; 0196 $string =~ s/([+-]?\d\d)(\d\d).*/$1:$2/; 0197 return $string; 0198 }