#!/usr/bin/perl

#
# CGI Script to produce the GPS target table on the fly,
# with sorting option
#
# Aliased to index.html in the ~/.htaccess file
# via script rewriting
#
# Initial Version: 2004-11-23, Joern Wilms
#

use strict;
use warnings;
use CGI;
use CGI::Carp  qw(fatalsToBrowser);

sub alignstring {
    my %args=@_;
    my $value=$args{value};
    my $pre=3;  # number of digits before the decimal point
    my $post=0; # number of digits after the decimal point

    $pre=$args{pre} if defined($args{pre});
    $post=$args{post} if defined($args{post});

    my $tot=$pre+$post;

    my $fmt="%${tot}.${post}f";

    my $str=sprintf($fmt,abs($value));
    my ( $before, $after ) = split(/\./,$str);

    $before =~ s/ //g;

    my $preadd=$pre-length($before);
    if ( $value < 0. ) {
	$before="-${before}";
    } else {
	if ( defined($args{showsign}) ) {
	    $before="+${before}";
	}
    }
    while ( $preadd > 0 ) {
	$before="&nbsp;${before}";
	$preadd--;
    }

    # remove trailing zeros
    $after =~ s/0+$//;

    my $postadd=$post-length($after);
    while ( $postadd > 0 ) {
	$after.="&nbsp;";
	$postadd--;
    }


    $str=$before;
    if ( $post > 0 ) {
	$str.=".${after}";
    }

    return $str;

}

# avoid denial of service attacks;
# (this is the max. size of a posting)
$CGI::POST_MAX=1000;


my $q=new CGI;

my $newStyle=<<END;
<!--
A {text-decoration: none;}
body, div, th, td, tr, a, li, font, p, b, h1 {font-family: Helvetica, Verdana, Arial, sans-serif;
font-size: 95%; text-align: justify}
a:hover { Text-Decoration: underline; Color:red;}
-->
END

print $q->header(),
      $q->start_html(-title=>'Accreting Pulsating Neutron Stars',
		     -style=>{-code=>$newStyle}
		    ),
      $q->h2('Accreting Pulsating Neutron
      Stars'),
      'As part of INTEGRAL\'s guaranteed time program, from 2003 Jan to
      2006 Aug INTEGRAL scanned
      parts of the Galactic plane every fortnight (the INTEGRAL
      "Galactic Plane Survey", GPS). These WWW-pages
      contain up to date information of basic observational data from
      all pulsating accreting neutron stars from the GPS scans, which
      is made available within days of the consolidated INTEGRAL data
      becoming available at the ',
      $q->a({href=>'http://isdc.unige.ch'},"INTEGRAL Science Data
      Centre"),
      '. At the moment source fluxes on a INTEGRAL Science Window
      basis are shown for the JEM-X and IBIS instruments and selected
      energy bands for all Science Windows where a source has been
      detected. All data reduction was performed with INTEGRAL OSA 5.1.
      We plan to add pulse profiles and pulse period
      information in the next few months.
      <p>The pages also contain basic scientific information about the
      sources monitored, including literature references and links to data
      from the RXTE All Sky Monitor and the BATSE experiment on the Compton
      Gamma Ray observatory, where available.
      <p>The following table contains our current source sample,
      click on the source name to access the information. Click on the
      column title to sort the table.';

# Table of all sources

my @sources;

push @sources,
  {dir=>'cenx3',name=>'Cen X-3'         ,lon=>292.09,lat=>0.34  ,pspin=>  4.82,porb=>2.09,alpha=>'11:21:15.2',delta=>'-60:37:24'},
  {dir=>'velax1',name=>'Vela X-1'       ,lon=>263.06,lat=>3.90  ,pspin=>283.2 ,porb=>8.96,alpha=>'09:02:06.86',delta=>'-40:33:16.9'},
  {dir=>'gx14',name=>'GX 1+4'           ,lon=>1.94  ,lat=>4.79
  ,pspin=>120   ,porb=>-1,alpha=>'17:32:2.1',delta=>'-24:44:44'},
  {dir=>'oao1657',name=>'OAO 1657-415'  ,lon=>344.35,lat=>0.31  ,pspin=>37.7  ,porb=>10.4,alpha=>'17:00:48.9',delta=>'-41:39:21.6'},
  {dir=>'j1808',name=>'SAX J1808.4-3658',lon=>355.38,lat=>-8.15 ,pspin=>0.0025,porb=>0.837,alpha=>'18:08:27.54',delta=>'-36:58:44.3'},
  {dir=>'j0635',name=>'SAX J0635.2+0533',lon=>206.15,lat=>-1.04 ,pspin=>0.0338,porb=>-1,alpha=>'06:35:17.4',delta=>'+05:33:21'},
  {dir=>'j1744',name=>'GRO J1744-28'    ,lon=>0.05  ,lat=>0.30  ,pspin=>0.467 ,porb=>11.76,alpha=>'17:44:33.09',delta=>'-28:44:27.0' },
  {dir=>'4u1822',name=>'3A 1822-371'    ,lon=>356.85,lat=>-11.29,pspin=>0.593 ,porb=>0.23,alpha=>'18:25:46.8',delta=>'-37:06:19'},
  {dir=>'j1906',name=>'XTE J1906+090'   ,lon=>42.59 ,lat=>0.89  ,pspin=> 89.17,porb=>-1,alpha=>'',delta=>''},
  {dir=>'j1820',name=>'AX J1820.5-1434' ,lon=>16.47 ,lat=>0.07  ,pspin=>152.26,porb=>-1},
  {dir=>'j1324',name=>'SAX J1324.4-6200',lon=>306.79,lat=>0.61  ,pspin=>170.84,porb=>22},
  {dir=>'1h1238',name=>'3A 1239-599'    ,lon=>301.8 ,lat=>2.60  ,pspin=>191   ,porb=>-1},
  {dir=>'j0440',name=>'RX J0440.9+4431' ,lon=>159.85,lat=>-1.27 ,pspin=>202.5 ,porb=>-1},
  {dir=>'1e1145',name=>'1E1145.1-6141'  ,lon=>295.49,lat=>-0.01 ,pspin=>297.29,porb=>14.365},
  {dir=>'2s1145',name=>'H 1145-619'     ,lon=>295.61,lat=>-0.24 ,pspin=>292.2 ,porb=>187.5},
  {dir=>'j2103',name=>'SAX J2103.5+4545',lon=>87.12 ,lat=>0.68  ,pspin=>358.61,porb=>12.68},
  {dir=>'j1855',name=>'XTE J1855-026'   ,lon=>31.09 ,lat=>-2.14 ,pspin=>361   ,porb=>6.1},
  {dir=>'j1452',name=>'SAX J1452.8-5949',lon=>317.64,lat=>-0.46 ,pspin=>437.4 ,porb=>-1},
  {dir=>'4u1907',name=>'4U 1907+097'    ,lon=>43.74 ,lat=>0.48  ,pspin=>440.4 ,porb=>8.38},
  {dir=>'4u1538',name=>'H 1538-522'     ,lon=>327.42,lat=>2.16  ,pspin=>528.8 ,porb=>3.73},
  {dir=>'gx301',name=>'GX 301-2'        ,lon=>300.10,lat=>-0.04 ,pspin=>681   ,porb=> 41.5 },
  {dir=>'j1700',name=>'AX J1700.1-4157' ,lon=>344.0 ,lat=>-0.60 ,pspin=>714.5 ,porb=>-1},
  {dir=>'j1037',name=>'4U 1036-56'      ,lon=>285.36,lat=>1.43  ,pspin=>862   ,porb=>-1},
  {dir=>'a0114',name=>'3A 0114+650'     ,lon=>125.71,lat=>2.56  ,pspin=>9828  ,porb=>11.6},
  {dir=>'4u0115',name=>'4U 0115+634'    ,lon=>125.92,lat=>1.03  ,pspin=>  3.61,porb=>24.3},
  {dir=>'a0535',name=>'A0535+262'     ,lon=>181.5 ,lat=>-2.64 ,pspin=>103.5   ,porb=>110.3};


my $sorton='lon';

if ( defined($q->param('sort')) ) {
    my $sortpar=$q->param('sort');
    $sorton='lat' if ($sortpar eq 'lat');
    $sorton='lon' if ($sortpar eq 'lon');
    $sorton='pspin' if ( $sortpar eq 'pspin');
    $sorton='porb' if ( $sortpar eq 'porb');
    $sorton='name' if ( $sortpar eq 'name');
}

@sources = sort({$a->{name} cmp $b->{name}} @sources) if ( $sorton eq 'name');
@sources = sort({$a->{lon} <=> $b->{lon}}  @sources) if ( $sorton eq 'lon');
@sources = sort({$a->{lat} <=> $b->{lat}}  @sources) if ( $sorton eq 'lat');
@sources = sort({$a->{pspin} <=> $b->{pspin}}  @sources) if ( $sorton eq 'pspin');
@sources = sort({$a->{porb} <=> $b->{porb}}  @sources) if ( $sorton eq 'porb');


print '<table border=3 cellpadding=3 cellspacing=2 bgcolor=#D3D3D3
        summary="List of the pulsating neutron stars in the field of
        visibility of the INTEGRAL galactic plane survey">';
print '<tr bgcolor=#A1C4EE>';
my $newurl=new CGI($q);
$newurl->param(sort=>"name");
print $q->th($q->a({href=>$newurl->url(-query=>1)},'Source Name'));
$newurl->param(sort=>"lon");
print $q->th($q->a({href=>$newurl->url(-query=>1)},'l (deg)'));
$newurl->param(sort=>"lat");
print $q->th($q->a({href=>$newurl->url(-query=>1)},'b (deg)'));
$newurl->param(sort=>"pspin");
print $q->th($q->a({href=>$newurl->url(-query=>1)},'P<sub>spin</sub> (s)'));
$newurl->param(sort=>"porb");
print $q->th($q->a({href=>$newurl->url(-query=>1)},'P<sub>orb</sub> (d)'));
print '</tr>';
foreach my $src ( @sources ) {
    print "<tr>";
    print $q->td(
		 {bgcolor=>"#FFFFE8"},
		 $q->a({href=>"./" . $src->{dir}},
		       $src->{name})
		);
    print $q->td({bgcolor=>"#FFFFE8",align=>"char",char=>"."},
		 alignstring(value=>$src->{lon},pre=>3,post=>2) );
    print $q->td({bgcolor=>"#FFFFE8",align=>"char",char=>"."},
		 alignstring(value=>$src->{lat},pre=>3,post=>2,showsign=>1) );
    print $q->td({bgcolor=>"#FFFFE8",align=>"char",char=>"."},
		 alignstring(value=>$src->{pspin},pre=>6,post=>4));
    if ( $src->{porb} > 0 ) {
	print $q->td({bgcolor=>"#FFFFE8",align=>"char",char=>"."},
		     alignstring(value=>$src->{porb},pre=>4,post=>3));
    } else {
	print $q->td({bgcolor=>"#FFFFE8"},"&nbsp;");
    }
    print "</tr>";
}

print "</table>";

print <<END;
<p>When using the data provided here, please include a reference to these
web pages as follows: <I>INTEGRAL GPS Team for accreting neutron stars
(Wilms, Santangelo, Staubert et al., 2004-2007)</I> and include the
URL of these WWW pages. Please email J&ouml;rn Wilms</a>
(<tt>wilms at astro.uni-tuebingen.de</tt>)
with questions concerning these pages.

<p>The team responsible for the analysis of accreting
pulsating neutron stars within the INTEGRAL GPS consists of the
following people:
J&uuml;rgen Barnstedt (IAA T&uuml;bingen),
Masha Chernyakova (ISDC/University of Geneva),
Thierry Courvoisier (ISDC/University of Geneva),
Miroslaw Denis (CBK Warsaw)
Guido DiCocco (INAF/CNR Bologna),
Carlo Ferrigno (INAF Palermo),
Sonja Fritz (IAA T&uuml;bingen),
Peter Kretschmar (ESA-ESAC),
Ingo Kreykenbohm (ISDC/IAA T&uuml;bingen),
Nikolai von Krusenstiern (IAA T&uuml;bingen),
Stefan Larsson (University of Stockholm),
Jenny Maar (IAA T&uuml;bingen),
Sandro Mereghetti (INAF Milano),
Ada Paizis (ISDC/INAF Milano),
Katja Pottschmidt (ISDC/UC San Diego),
Andrea Santangelo (IAA T&uuml;bingen),
Alberto Segreto (INAF Palermo),
Lara Sidoli (INAF Milano),
R&uuml;diger Staubert (IAA T&uuml;bingen),
Nils-Jorgen Westergaard (DSRI Copenhagen), and
J&ouml;rn Wilms (University of Erlangen-N&uuml;rnberg).

<p>Further information and corrections for the pages were provided by
the MAGNET collaboration, especially
Isabel Caballero (IAA T&uuml;bingen)
Dima Klochkov (IAA T&uuml;bingen),
Vanessa McBride (University of Southampton), and
Rick Rothschild (UCSD).
END


print $q->end_html;

exit;

