psrcat2json.pl   psrcat2json.pl 
#!/usr/bin/perl #!/usr/bin/perl -w
# #
# 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;
$PSRCAT = "./psrcat.db"; $PSRCAT = "./psrcat.db";
$JSON = "../resources/catalog.json"; $JSON = "./pulsars.json";
$FORMAT = 2;
$CATVER = 1.44;
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\": \"0.2.0\",\n"; print JSON "\t\"version\": \"".$FORMAT."\",\n";
print JSON "\t\"shortName\": \"A catalogue of pulsars, based on ATNF Pulsar print JSON "\t\"shortName\": \"A catalogue of pulsars, based on ATNF Pulsar
Catalogue v. 1.41\",\n"; Catalogue v. ".$CATVER."\",\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;
$dmeasure = 0; $dmeasure = 0;
$frequency = 0; $frequency = 0;
$pfrequency = 0; $pfrequency = 0;
$eccentricity = 0; $eccentricity = 0;
$w50 = 0; $w50 = 0;
$s400 = 0; $s400 = 0;
$s600 = 0; $s600 = 0;
$s1400 = 0; $s1400 = 0;
$distance = 0; $distance = 0;
$outRA = ""; $outRA = "";
$outDE = ""; $outDE = "";
$elat = "";
$elong = "";
$flag = 0; $flag = 0;
$notes = ""; $notes = "";
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\.]+)/) {
$elong = $2;
}
if ($lines[$j] =~ /^ELAT(\s+)([\d\-\+\.]+)/) {
$elat = $2;
}
if ($lines[$j] =~ /^RAJ(\s+)([\d\-\+\:\.]+)/) { if ($lines[$j] =~ /^RAJ(\s+)([\d\-\+\:\.]+)/) {
($hour,$min,$sec) = split(":",$2); ($hour,$min,$sec) = split(":",$2);
$sec += 0; $sec += 0;
$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\-\+\:\.]+)/) {
($deg,$min,$sec) = split(":",$2); ($deg,$min,$sec) = split(":",$2);
$sec += 0; $sec += 0;
$outDE = $deg."d".$min."m".$sec."s"; $outDE = $deg."d".$min."m".$sec."s";
skipping to change at line 167 skipping to change at line 182
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 ($notes ne '') if ($notes ne '')
{ {
$out .= "\t\t\t\"notes\": \"".$notes."\",\n"; $out .= "\t\t\t\"notes\": \"".$notes."\",\n";
} }
$out .= "\t\t\t\"RA\": \"".$outRA."\",\n"; if (($outRA ne '') && ($outDE ne ''))
$out .= "\t\t\t\"DE\": \"".$outDE."\"\n"; {
$out .= "\t\t\t\"RA\": \"".$outRA."\",\n";
$out .= "\t\t\t\"DE\": \"".$outDE."\"\n";
}
if (($elat ne '') && ($elong ne ''))
{
$coseps = 0.91748213149438;
$sineps = 0.39777699580108;
$deg2rad = pi / 180.0; # deg2rad
$sdec = sin($elat * $deg2rad) * $coseps + cos($elat * $deg
2rad) * $sineps * sin($elong * $deg2rad);
$dec = asin($sdec); # in radians
$cos_ra = ( cos($elong * $deg2rad) * cos($elat * $deg2rad) /
cos($dec) );
$sin_ra = ( sin($dec) * $coseps - sin($elat * $deg2rad) ) /
(cos($dec) * $sineps);
$ra = atan2($sin_ra, $cos_ra); # in radians
if ($ra < 0.0) {
$ra = 2 * pi + $ra;
} elsif ($ra > 2 * pi) {
$ra = $ra - 2 * pi;
}
$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
$rahh = $RA * 24.0;
$ramm = ($RA * 24.0 - int($rahh)) * 60.0;
$rasec = (($RA * 24.0 - int($rahh)) * 60.0 - int($ramm)) * 6
0.0;
$raisec = ($rasec * 10000.0 + 0.5) / 10000;
if ($raisec == 60) {
$rasec = 0.0;
$ramm = $ramm + 1;
if ($ramm == 60) {
$ramm = 0;
$rahh = $rahh + 1;
if ($rahh == 24) {
$rahh = 0;
}
}
}
$outRA = sprintf("%02dh%02dm%05.3fs",$rahh, $ramm, $rasec);
if ($Dec < 0.0) {
$trn = $Dec * -1;
} else {
$trn = $Dec;
}
$dd = $Dec * 360.;
$mm = ($trn * 360.0 - int($dd)) * 60.;
$sec = (($trn * 360.0 - int($dd)) * 60.0 - int($mm)) * 60.0;
$isec = ($sec * 1000.0 + 0.5) / 1000;
if ($isec == 60){
$sec = 0.;
$mm = $mm + 1;
if ($mm == 60) {
$mm = 0;
$dd = $dd + 1;
}
}
$outDE = sprintf("%02dd%02dm%05.3fs",$dd,$mm,$sec);
$out .= "\t\t\t\"RA\": \"".$outRA."\",\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 .= ",";
} }
if (($outRA ne '') && ($outDE ne '') || (($elat ne '') && ($elong ne
if (($outRA ne '') && ($outDE ne '') && ($flag == 1)) { '')) && ($flag == 1)) {
print JSON $out."\n"; print JSON $out."\n";
} }
} }
print JSON "\t}\n}\n"; print JSON "\t}\n}\n";
close JSON; close JSON;
 End of changes. 10 change blocks. 
11 lines changed or deleted 88 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/