File indexing completed on 2024-09-08 03:28:59
0001 #!/usr/bin/perl 0002 # 0003 # parse-sac-to-mysql.pl Put DSO data from Saguaro Astronomy Club's database in the Fence format into a database 0004 # 0005 # CAUTION: Will truncate the table supplied! 0006 # 0007 # SPDX-FileCopyrightText: 2009 James Bowlin <bowlin@mindspring.com> and Akarsh Simha <akarsh.simha@kdemail.net> 0008 # SPDX-License-Identifier: GPL-2.0-or-later 0009 0010 use strict; 0011 use DBI; 0012 0013 my $ERROR; 0014 0015 my $VERBOSE = 1; 0016 0017 # For database handling 0018 my $db_db = shift; 0019 my $db_user = shift; 0020 !($db_db eq "") and !($db_user eq "") or print "USAGE: " . $0 . " <database name> <MySQL Username> [[MySQL Password [Table]]\n" and exit; 0021 my $db_pass = shift; 0022 my $db_tbl = shift or "SAC_DeepSky"; 0023 0024 my $db_query = qq/CREATE DATABASE IF NOT EXISTS `$db_db`;/; 0025 my $db_select_query = qq/USE `$db_db`/; 0026 0027 my $tbl_query = qq/ 0028 CREATE TABLE IF NOT EXISTS `$db_tbl` ( 0029 `primary` varchar(30) NOT NULL COMMENT 'Primary Designation', 0030 `secondary` varchar(30) NOT NULL COMMENT 'Alternate Designation', 0031 `type` smallint NOT NULL COMMENT 'Object Type', 0032 `const` varchar(3) NOT NULL COMMENT 'Constellation', 0033 `RA` double NOT NULL COMMENT 'Right Ascension', 0034 `Dec` double NOT NULL COMMENT 'Declination', 0035 `mag` float NOT NULL COMMENT 'Magnitude', 0036 `subr` float NOT NULL COMMENT 'Surface Brightness', 0037 `U2K` mediumint(9) NULL COMMENT 'Uranometria 2000 chart number', 0038 `SA2K` smallint(6) NULL COMMENT'SkyAtlas 2000.0 chart number', 0039 `a` float NOT NULL COMMENT 'Major Axis', 0040 `b` float NOT NULL COMMENT 'Minor Axis', 0041 `pa` int NOT NULL COMMENT 'Position Angle', 0042 `class` varchar(15) NOT NULL COMMENT 'Classification detail', 0043 `ngc_desc` varchar(30) NOT NULL COMMENT 'NGC Description', 0044 `notes` varchar(50) NOT NULL COMMENT 'SAC notes', 0045 PRIMARY KEY (`primary`), 0046 KEY `other` (`secondary`) 0047 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;/; 0048 0049 my $tbl_trunc_query = qq/TRUNCATE TABLE `$db_tbl`/; 0050 0051 # Get the database handle 0052 my $dbh = DBI->connect("DBI:mysql:", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 0 }); 0053 0054 my @fields = qw/primary secondary type const RA Dec mag subr U2K SA2K a b pa class ngc_desc notes/; 0055 my %objtypes = qw/1STAR 1 2STAR 17 3STAR 17 4STAR 17 8STAR 17 ASTER 13 OPNCL 3 GLOCL 4 CL+NB 5 G+C+N 5 GX+DN 5 GX+GC 4 BRTNB 5 DRKNB 15 PLNNB 6 SNREM 7 GALXY 8 GALCL 14 LMCCN 5 LMCDN 5 LMCGC 4 LMCOC 3 NONEX 18 SMCCN 5 SMCDN 5 SMCGC 4 SMCOC 3 QUASR 16/; 0056 0057 $dbh->do($db_query); 0058 $dbh->do($db_select_query); 0059 $dbh->do($tbl_query); 0060 $dbh->do($tbl_trunc_query); 0061 $dbh->commit(); 0062 0063 while(<>) { 0064 m/^\s*#/ and do { print if $VERBOSE; next}; 0065 m/\S/ or do { print if $VERBOSE; next}; 0066 chomp; 0067 my $dso = kstars_unpack($_) or do { 0068 warn sprintf("%4d: FORMAT ERROR ($ERROR):\n$_\n", $.); 0069 next; 0070 }; 0071 $dso->{line} = $.; 0072 0073 my $query ||= qq/INSERT INTO `$db_tbl` (/ . 0074 join(", ", map {"`$_`"} @fields) . 0075 qq/) VALUES (/ . 0076 join(", ", map {"?"} @fields) . 0077 qq/)/; 0078 0079 my $sth ||= $dbh->prepare($query); 0080 0081 $sth->execute(@$dso{@fields}); 0082 0083 } 0084 0085 $dbh->commit(); 0086 0087 $dbh->disconnect(); 0088 0089 exit; 0090 0091 #---------------------------------------------------------------------------- 0092 #--- Subroutines ------------------------------------------------------------ 0093 #---------------------------------------------------------------------------- 0094 0095 sub kstars_unpack { 0096 my $line = shift; 0097 chomp $line; 0098 $VERBOSE and print $line; 0099 $line =~ m{^\|\s*([^\s\|]+)\s # Catalog 0100 \s*([\d+-\.\sA-Za-z]*?)\s*\| # Catalog Number / Designation 0101 (.*?)\s*\| # Secondary Designation 0102 ([0-9A-Z+]{5})\| # Type 0103 ([A-Z]{3})\| # Constellation 0104 (\d\d\s\d\d\.\d)\| # RA [String: HH MM.M] 0105 ([+-]\d{2}\s\d{2})\| # Dec [String: +/-DD MM] 0106 ([\s\d]\d\.\d)\| # Magnitude 0107 (\d+\.?\d*)\s*\| # Surface Brightness 0108 (\d+)\s*\| # Uranometria 2000.0 Chart Number 0109 (\d+)\s*\| # Tirion Sky Atlas 2000.0 Chart Number 0110 \s*(\d+\.?\d*\s*[ms])?\s*\| # Major Axis 0111 \s*(\d+\.?\d*\s*[ms])?\s*\| # Minor Axis 0112 (\d*)\s*\| # Position Angle 0113 ([^|]*?)\s*\| # Class 0114 ## NOTE: Ignoring fields NSTS, BRSTR and BCHM; TODO: Include them later 0115 [^\|]*\|[^\|]*\|[^\|]*\| # Ignore NSTS, BRSTR, BCHM 0116 ([^|]*?)\s*\| # NGC Description 0117 ([^|]*?)\s*\| # Notes 0118 }x or do 0119 { 0120 $ERROR = "Positional Error (0-59)"; 0121 return; 0122 }; 0123 my $object = { 0124 cat => $1, 0125 catno => $2, 0126 secondary => $3, 0127 type_str => $4, 0128 const => $5, 0129 ra_str => $6, 0130 dec_str => $7, 0131 mag => $8, 0132 subr => $9, 0133 U2K => $10, 0134 SA2K => $11, 0135 a_str => $12, 0136 b_str => $13, 0137 pa => $14, 0138 class => $15, 0139 ngc_desc => $16, 0140 notes => $17 0141 }; 0142 0143 $object->{primary} = $object->{cat} . " " . $object->{catno}; 0144 $object->{secondary} =~ s/\s+/ /g; 0145 $object->{type} = $objtypes{$object->{type_str}}; 0146 $object->{ra_str} =~ m/\s*(\d\d) (\d\d.\d)/;# or do { $ERROR="RA String format error: " . $object->{ra_str}; return; }; 0147 $object->{RA} = $1 + $2 / 60.0; 0148 $object->{dec_str} =~ m/\s*([+-]?\d\d) (\d\d)/;# or do { $ERROR="Dec String format error: " . $object->{dec_str}; return; }; 0149 $object->{Dec} = $1 + $2 / 60.0; 0150 $object->{a_str} =~ m/\s*(\d*\.?\d*)\s*([ms]?)\s*/;# or do { $ERROR="Major axis format error: " . $object->{a_str}; return; }; 0151 $object->{a} = $1 / (($2 eq 's')? 60.0 : 1.0); 0152 $object->{b_str} =~ m/\s*(\d*\.?\d*)\s*([ms]?)\s*/;# or do { $ERROR="Minor axis format error: " . $object->{b_str}; return; }; 0153 $object->{b} = $1 / (($2 eq 's')? 60.0 : 1.0); 0154 0155 return $object; 0156 }