psrcat2json.pl   psrcat2json.pl 
skipping to change at line 37 skipping to change at line 37
# #
# read psrcat.db from ATNF Pulsar Catalogue and convert to JSON # read psrcat.db from ATNF Pulsar Catalogue and convert to JSON
# URL: http://www.atnf.csiro.au/research/pulsar/psrcat/download.html # URL: http://www.atnf.csiro.au/research/pulsar/psrcat/download.html
# #
use Math::Trig; use Math::Trig;
$PSRCAT = "./psrcat.db"; $PSRCAT = "./psrcat.db";
$JSON = "./pulsars.json"; $JSON = "./pulsars.json";
$FORMAT = 2; $FORMAT = 2;
$CATVER = 1.46; $CATVER = 1.50;
open (PSRCAT, "<$PSRCAT"); open (PSRCAT, "<$PSRCAT");
@catalog = <PSRCAT>; @catalog = <PSRCAT>;
close PSRCAT; close PSRCAT;
$psrcat = ""; $psrcat = "";
foreach $s (@catalog) { foreach $s (@catalog) {
$psrcat .= $s; $psrcat .= $s;
} }
@cat = split("@",$psrcat); @cat = split("@",$psrcat);
open (JSON, ">$JSON"); open (JSON, ">$JSON");
print JSON "{\n"; print JSON "{\n";
print JSON "\t\"version\": \"".$FORMAT."\",\n"; print JSON "\t\"version\": \"".$FORMAT."\",\n";
print JSON "\t\"shortName\": \"A catalogue of pulsars, based on ATNF Pulsar Catalogue v. ".$CATVER."\",\n"; print JSON "\t\"shortName\": \"A catalogue of pulsars, based on ATNF Pulsar Catalogue v. ".$CATVER."\",\n";
print JSON "\t\"originalCatalogURL\": \"http://www.atnf.csiro.au/research/p ulsar/psrcat/\",\n";
print JSON "\t\"pulsars\":\n"; print JSON "\t\"pulsars\":\n";
print JSON "\t{\n"; print JSON "\t{\n";
for ($i=0;$i<scalar(@cat)-1;$i++) { for ($i=0;$i<scalar(@cat)-1;$i++) {
@lines = split("\n", $cat[$i]); @lines = split("\n", $cat[$i]);
$period = 0; $period = 0;
$pderivative = 0; $pderivative = 0;
$bperiod = 0; $bperiod = 0;
$parallax = 0; $parallax = 0;
$dmeasure = 0; $dmeasure = 0;
skipping to change at line 78 skipping to change at line 79
$s400 = 0; $s400 = 0;
$s600 = 0; $s600 = 0;
$s1400 = 0; $s1400 = 0;
$distance = 0; $distance = 0;
$outRA = ""; $outRA = "";
$outDE = ""; $outDE = "";
$elat = ""; $elat = "";
$elong = ""; $elong = "";
$flag = 0; $flag = 0;
$notes = ""; $notes = "";
$glitch = 0;
for ($j=0;$j<scalar(@lines);$j++) { for ($j=0;$j<scalar(@lines);$j++) {
if ($lines[$j] =~ /^PSRJ(\s+)J([\d]{4})([\+\-]{1})([\d]{2,4} )([\w]{0,1})(\s+)/) { if ($lines[$j] =~ /^PSRJ(\s+)J([\d]{4})([\+\-]{1})([\d]{2,4} )([\w]{0,1})(\s+)/) {
$name = $2.$3.$4.$5; $name = $2.$3.$4.$5;
$flag = 1; $flag = 1;
} }
if ($lines[$j] =~ /^ELONG(\s+)([\d\.]+)/) { if ($lines[$j] =~ /^ELONG(\s+)([\d\.]+)/) {
$elong = $2; $elong = $2;
} }
if ($lines[$j] =~ /^ELAT(\s+)([\d\-\+\.]+)/) { if ($lines[$j] =~ /^ELAT(\s+)([\d\-\+\.]+)/) {
$elat = $2; $elat = $2;
} }
if ($lines[$j] =~ /^RAJ(\s+)([\d\-\+\:\.]+)/) { if ($lines[$j] =~ /^RAJ(\s+)([\d\-\+\:\.]+)/) {
$secf = 0;
($hour,$min,$sec) = split(":",$2); ($hour,$min,$sec) = split(":",$2);
$min += 0; $min += 0;
if ($min!=int($min)) { $secf = $min-int($min); $secf *= 60; $min = int($min); }
if ($min<10) { $min = "0".$min; } if ($min<10) { $min = "0".$min; }
$sec += 0; $sec += $secf;
if ($sec<10) { $sec = "0".$sec; } if ($sec<10) { $sec = "0".$sec; }
$outRA = $hour."h".$min."m".$sec."s"; $outRA = $hour."h".$min."m".$sec."s";
} }
if ($lines[$j] =~ /^DECJ(\s+)([\d\-\+\:\.]+)/) { if ($lines[$j] =~ /^DECJ(\s+)([\d\-\+\:\.]+)/) {
$secf = 0;
($deg,$min,$sec) = split(":",$2); ($deg,$min,$sec) = split(":",$2);
$min += 0; $min += 0;
if ($min!=int($min)) { $secf = $min-int($min); $secf *= 60; $min = int($min); }
if ($min<10) { $min = "0".$min; } if ($min<10) { $min = "0".$min; }
$sec += 0; $sec += $secf;
if ($sec<10) { $sec = "0".$sec; } if ($sec<10) { $sec = "0".$sec; }
$outDE = $deg."d".$min."m".$sec."s"; $outDE = $deg."d".$min."m".$sec."s";
} }
if ($lines[$j] =~ /^P0(\s+)([\d\.]+)/) { if ($lines[$j] =~ /^P0(\s+)([\d\.]+)/) {
$period = $2; $period = $2;
} }
if ($lines[$j] =~ /^P1(\s+)([\d\.\-E]+)/) { if ($lines[$j] =~ /^P1(\s+)([\d\.\-E]+)/) {
$pderivative = $2; $pderivative = $2;
skipping to change at line 166 skipping to change at line 172
} }
if ($lines[$j] =~ /^ECC(\s+)([\d\.\-E]+)/) { if ($lines[$j] =~ /^ECC(\s+)([\d\.\-E]+)/) {
$eccentricity = $2; $eccentricity = $2;
} }
if ($lines[$j] =~ /^TYPE(\s+)([\w\,]+)/) if ($lines[$j] =~ /^TYPE(\s+)([\w\,]+)/)
{ {
$notes = $2; $notes = $2;
} }
if ($lines[$j] =~ /^NGLT(\s+)([\d]+)/)
{
$glitch = $2;
}
} }
$out = "\t\t\"PSR J".$name."\":\n"; $out = "\t\t\"PSR J".$name."\":\n";
$out .= "\t\t{\n"; $out .= "\t\t{\n";
if ($parallax > 0) { if ($parallax > 0) {
$out .= "\t\t\t\"parallax\": ".$parallax.",\n"; $out .= "\t\t\t\"parallax\": ".$parallax.",\n";
} }
if ($distance > 0) { if ($distance > 0) {
$out .= "\t\t\t\"distance\": ".$distance.",\n"; $out .= "\t\t\t\"distance\": ".$distance.",\n";
} }
skipping to change at line 209 skipping to change at line 220
} }
if ($s400 > 0) { if ($s400 > 0) {
$out .= "\t\t\t\"s400\": ".$s400.",\n"; $out .= "\t\t\t\"s400\": ".$s400.",\n";
} }
if ($s600 > 0) { if ($s600 > 0) {
$out .= "\t\t\t\"s600\": ".$s600.",\n"; $out .= "\t\t\t\"s600\": ".$s600.",\n";
} }
if ($s1400 > 0) { if ($s1400 > 0) {
$out .= "\t\t\t\"s1400\": ".$s1400.",\n"; $out .= "\t\t\t\"s1400\": ".$s1400.",\n";
} }
if ($glitch > 0) {
$out .= "\t\t\t\"glitch\": ".$glitch.",\n";
}
if ($notes ne '') if ($notes ne '')
{ {
$out .= "\t\t\t\"notes\": \"".$notes."\",\n"; $out .= "\t\t\t\"notes\": \"".$notes."\",\n";
} }
if (($outRA ne '') && ($outDE ne '')) if (($outRA ne '') && ($outDE ne ''))
{ {
$out .= "\t\t\t\"RA\": \"".$outRA."\",\n"; $out .= "\t\t\t\"RA\": \"".$outRA."\",\n";
$out .= "\t\t\t\"DE\": \"".$outDE."\"\n"; $out .= "\t\t\t\"DE\": \"".$outDE."\"\n";
} }
if (($elat ne '') && ($elong ne '')) if (($elat ne '') && ($elong ne ''))
skipping to change at line 236 skipping to change at line 250
$sin_ra = ( sin($dec) * $coseps - sin($elat * $deg2rad) ) / (cos($dec) * $sineps); $sin_ra = ( sin($dec) * $coseps - sin($elat * $deg2rad) ) / (cos($dec) * $sineps);
$ra = atan2($sin_ra, $cos_ra); # in radians $ra = atan2($sin_ra, $cos_ra); # in radians
if ($ra < 0.0) { if ($ra < 0.0) {
$ra = 2 * pi + $ra; $ra = 2 * pi + $ra;
} elsif ($ra > 2 * pi) { } elsif ($ra > 2 * pi) {
$ra = $ra - 2 * pi; $ra = $ra - 2 * pi;
} }
$RA = $ra / (2.0 * pi); # by dividing by 2PI of radians you obtain number of turns $RA = $ra / (2.0 * pi); # by dividing by 2PI of radians you obtain number of turns
$Dec = $dec / (2.0 * pi); # by dividing by 2PI of radians yo u obtain number of turns $Dec = $dec / (2.0 * pi); # by dividing by 2PI of radians yo u obtain number of turns
$rahh = $RA * 24.0; $rahh = $RA * 24.0;
$ramm = ($RA * 24.0 - int($rahh)) * 60.0; $ramm = ($rahh - int($rahh)) * 60.0;
$rasec = (($RA * 24.0 - int($rahh)) * 60.0 - int($ramm)) * 6 $rasec = ($ramm - int($ramm)) * 60.0;
0.0;
$raisec = ($rasec * 10000.0 + 0.5) / 10000; $raisec = ($rasec * 10000.0 + 0.5) / 10000;
if ($raisec == 60) { if ($raisec == 60) {
$rasec = 0.0; $rasec = 0.0;
$ramm = $ramm + 1; $ramm = $ramm + 1;
if ($ramm == 60) { if ($ramm == 60) {
$ramm = 0; $ramm = 0;
$rahh = $rahh + 1; $rahh = $rahh + 1;
if ($rahh == 24) { if ($rahh == 24) {
$rahh = 0; $rahh = 0;
} }
} }
} }
$outRA = sprintf("%02dh%02dm%05.3fs",$rahh, $ramm, $rasec); $outRA = sprintf("%02dh%02dm%05.3fs",$rahh, $ramm, $rasec);
if ($Dec < 0.0) { if ($Dec < 0.0) {
$trn = $Dec * -1; $sign = -1;
} else { } else {
$trn = $Dec; $sign = 1;
} }
$dd = $Dec * 360.; $dd = $Dec * 360.;
$mm = ($trn * 360.0 - int($dd)) * 60.; $mm = abs($dd - int($dd)) * 60.;
$sec = (($trn * 360.0 - int($dd)) * 60.0 - int($mm)) * 60.0; $sec = ($mm - int($mm)) * 60.0;
$isec = ($sec * 1000.0 + 0.5) / 1000; $isec = ($sec * 1000.0 + 0.5) / 1000;
if ($isec == 60){ if ($isec == 60){
$sec = 0.; $sec = 0.;
$mm = $mm + 1; if ($sign == 1) {
$mm = $mm + 1;
} else {
$mm = $mm - 1;
}
if ($mm == 60) { if ($mm == 60) {
$mm = 0; $mm = 0;
$dd = $dd + 1; if ($sign == 1) {
$dd = $dd + 1;
} else {
$dd = $dd - 1;
}
} }
} }
$outDE = sprintf("%02dd%02dm%05.3fs",$dd,$mm,$sec); $outDE = sprintf("%02dd%02dm%05.3fs",$dd,$mm,$sec);
$out .= "\t\t\t\"RA\": \"".$outRA."\",\n"; $out .= "\t\t\t\"RA\": \"".$outRA."\",\n";
$out .= "\t\t\t\"DE\": \"".$outDE."\"\n"; $out .= "\t\t\t\"DE\": \"".$outDE."\"\n";
} }
$out .= "\t\t}"; $out .= "\t\t}";
if ($i<scalar(@cat)-2) { if ($i<scalar(@cat)-2) {
$out .= ","; $out .= ",";
} }
 End of changes. 17 change blocks. 
12 lines changed or deleted 33 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/