""" -*- mode: python -*-

   Displaying the EGRET All Sky survey

   author: James Chiang <jchiang@slac.stanford.edu>
   author: Paul F. Kunz <Paul_Kunz@slac.stanford.edu>
   
"""
#
# $Id: egret_old.py.in,v 1.4 2005/07/09 14:43:38 pfkeb Exp $
#

import sys, math, copy
import setPath
import pyfits, numarray

egret = pyfits.open('@top_srcdir@/examples/EGRET_AllSky.fits')

prihdr = egret[0].header
nx = prihdr['naxis1']
ny = prihdr['naxis2']

#image = []
#for i in range(0, nx ) :
#    for j in range(0, ny ) :
#        image.append(egret[0].data[j][i])

x = copy.deepcopy(egret[0].data)
x.transpose()
image = list(numarray.array(x).flat)

from hippo import *
from load_hippo import app, canvas

# This ntuple will be built column-wise, that's the hardest.
nt = NTuple()

# Register the ntuple so it is visible to the application
ntc = NTupleController.instance()
ntc.registerNTuple ( nt )

nt.setTitle ( 'EGRET All Sky Survey' )
label = 'Intensity'
nt.addColumn ( label, image )

plot = Display ( "Image", nt, (label,) )
plot.setNumberOfBins ( 'x', nx )
plot.setNumberOfBins ( 'y', ny )

x_step = prihdr['cdelt1']
y_step = prihdr['cdelt2']

plot.setBinWidth ( 'x', x_step )
plot.setBinWidth ( 'y', y_step )

x_orig = - x_step * ( prihdr['crpix1'] -1 ) + prihdr['crval1']
y_orig = - y_step * ( prihdr['crpix2'] -1 ) + prihdr['crval2']
plot.setOffset ( 'x', x_orig )
plot.setOffset ( 'y', y_orig )

if x_step < 0 :
    plot.setRange ( 'x', x_orig + (nx+1) * x_step, x_orig )
else :
    plot.setRange ( 'x', x_orig, x_orig + (nx+1) * x_step )

plot.setRange ( 'y', y_orig, y_orig + (ny+1) * y_step )

plot.setLog ( 'z', 1 )
plot.setAspectRatio ( 2.0 )

canvas.addDisplay ( plot )
