Index: trunk/psLib/src/astronomy/psAstrometry.c
===================================================================
--- trunk/psLib/src/astronomy/psAstrometry.c	(revision 1333)
+++ trunk/psLib/src/astronomy/psAstrometry.c	(revision 1382)
@@ -1,14 +1,55 @@
-// This file contains
-
-#include "psType.h"
-#include "psImage.h"
-#include "psArray.h"
-#include "psList.h"
+/** @file  psAstrometry.c
+*
+*  @brief This file defines the basic types for astronomical coordinate 
+*  transformation
+*
+*  @ingroup AstroImage
+*
+*  @author George Gusciora, MHPCC
+*
+*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-08-04 22:11:09 $
+*
+*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
+*/
+
+#include<math.h>
+
 #include "psFunctions.h"
-
-psGrommit *psGrommitAlloc(const psExposure *exp)
-{
-    double *slaGrommit = (double *) psAlloc(14 * sizeof(double));
-    psGrommit *grommit = (psGrommit *) psAlloc(sizeof(psGrommit));
+#include "psAstrometry.h"
+#include "psMemory.h"
+
+void grommitFree( psGrommit *grommit );
+int checkValidChipCoords( double x, double y, psChip *tmpChip );
+int checkValidImageCoords( double x, double y, psImage *tmpImage );
+
+psExposure* psExposureAlloc( double ra, double dec, double hourAngle,
+                             double zenith, double azimuth, double localTime, float date,
+                             float rotAngle, float temperature, float pressure, float humidity,
+                             float exposureTime )
+{
+    psExposure * exp = psAlloc( sizeof( psExposure ) );
+    
+    *( double* ) & exp->ra = ra;
+    *( double* ) & exp->dec = dec;
+    *( double* ) & exp->hourAngle = hourAngle;
+    *( double* ) & exp->zenith = zenith;
+    *( double* ) & exp->azimuth = azimuth;
+    *( double* ) & exp->localTime = localTime;
+    *( float* ) & exp->date = date;
+    *( float* ) & exp->rotAngle = rotAngle;
+    *( float* ) & exp->temperature = temperature;
+    *( float* ) & exp->pressure = pressure;
+    *( float* ) & exp->humidity = humidity;
+    *( float* ) & exp->exposureTime = exposureTime;
+    
+    return exp;
+    
+}
+
+psGrommit* psGrommitAlloc( const psExposure *exp )
+{
+    double * slaGrommit = ( double * ) psAlloc( 14 * sizeof( double ) );
+    psGrommit *grommit = ( psGrommit * ) psAlloc( sizeof( psGrommit ) );
     /*
         extern void sla_aoppa(double date,
@@ -30,74 +71,78 @@
                   tlr);
     */
-    grommit->latitude = exp->dec;              // XXX Is this correct?
-    grommit->sinLat = sin(grommit->latitude);
-    grommit->cosLat = cos(grommit->latitude);
-    grommit->abberationMag = 0.0;
-    grommit->height = 0.0;
-    grommit->temperature = exp->temperature;
-    grommit->pressure = exp->pressure;
-    grommit->humidity = exp->humidity;
-    grommit->wavelength = 0.0;
-    grommit->lapseRate = 0.0;
-    grommit->refractA = 0.0;
-    grommit->refractB = 0.0;
-    grommit->longitudeOffset = ra;             // XXX Is this correct?
-    grommit->siderealTime = 0.0;
-
-    psFree(slaGrommit);
-    return(grommit);
-}
-
-void p_psGrommitFree(psGrommit *grommit)
-{
-    psFree(grommit);
-}
-
-psCell *psCellinFPA(psCell *out,
-                    const psPlane *coord,
-                    const psFPA *FPA)
-{
-    psChip *tmpChip = NULL;
+    *( double* ) & grommit->latitude = exp->dec;              // XXX Is this correct?
+    *( double* ) & grommit->sinLat = sin( grommit->latitude );
+    *( double* ) & grommit->cosLat = cos( grommit->latitude );
+    *( double* ) & grommit->abberationMag = 0.0;
+    *( double* ) & grommit->height = 0.0;
+    *( double* ) & grommit->temperature = exp->temperature;
+    *( double* ) & grommit->pressure = exp->pressure;
+    *( double* ) & grommit->humidity = exp->humidity;
+    *( double* ) & grommit->wavelength = 0.0;
+    *( double* ) & grommit->lapseRate = 0.0;
+    *( double* ) & grommit->refractA = 0.0;
+    *( double* ) & grommit->refractB = 0.0;
+    *( double* ) & grommit->longitudeOffset = exp->ra;             // XXX Is this correct?
+    *( double* ) & grommit->siderealTime = 0.0;
+    
+    psFree( slaGrommit );
+    return ( grommit );
+}
+
+void p_psGrommitFree( psGrommit *grommit )
+{
+    psFree( grommit );
+}
+
+psCell *psCellinFPA( psCell *out,
+                     const psPlane *coord,
+                     const psFPA *FPA )
+{
+    psChip * tmpChip = NULL;
     psCell *tmpCell = NULL;
-
-    tmpChip = psChipinFPA(tmpChip, coord, FPA);
-    tmpCell = psCellinChip(tmpCell, coord, tmpChip);
-    return(tmpCell);
-}
-
-
-p_psCheckValidChipCoords(double x, double y, psChil *tmpChip)
-{
-    return(0);
-}
-
-psChip *psChipinFPA(psChip *out,
-                    const psPlane *coord,
-                    const psFPA *FPA)
-{
-    psFPA *tmpChip = NULL;
-
-    for (tmpChip = FPA->chips; tmpChip != NULL; tmpChip++) {
-        tmpCoord = psPlaneTransformApply(out, tmpChip->fromFPA, coord);
-        if (p_psCheckValidChipCoords(tmpCoord->x, tmpCoord->y,
-                                     tmpChip)) {
-            psFree(tmpCoord);
-            return(tmpChip);
-        }
-        psFree(tmpCoord);
-    }
-    psFree(tmpCoord);
-    return(NULL);
-}
-
-p_psCheckValidImageCoords(double x, double y, psImage *tmpImage)
-{
-    if ((x < 0.0) ||
-            (x > (double) tmpImage->numCols) ||
-            (y < 0.0) ||
-            (y > (double) tmpImage->numRows)) {
-        return(0);
-    }
-    return(1);
+    
+    tmpChip = psChipinFPA( tmpChip, coord, FPA );
+    tmpCell = psCellinChip( tmpCell, coord, tmpChip );
+    return ( tmpCell );
+}
+
+
+int checkValidChipCoords( double x, double y, psChip *tmpChip )
+{
+    return ( 0 );
+}
+
+psChip *psChipinFPA( psChip *out,
+                     const psPlane *coord,
+                     const psFPA *FPA )
+{
+    psArray * chips = FPA->chips;
+    int nChips = chips->n;
+    psPlane* tmpCoord = NULL;
+    
+    for ( int i = 0; i < nChips; i++ ) {
+            psChip* tmpChip = chips->data[ i ];
+            tmpCoord = psPlaneTransformApply( tmpCoord, tmpChip->fromFPA, coord );
+            if ( checkValidChipCoords( tmpCoord->x, tmpCoord->y,
+                                       tmpChip ) ) {
+                    psFree( tmpCoord );
+                    // XXX: George, you didn't use the out parameter!
+                    return ( tmpChip );
+                }
+            psFree( tmpCoord );
+        }
+    psFree( tmpCoord );
+    return ( NULL );
+}
+
+int checkValidImageCoords( double x, double y, psImage *tmpImage )
+{
+    if ( ( x < 0.0 ) ||
+            ( x > ( double ) tmpImage->numCols ) ||
+            ( y < 0.0 ) ||
+            ( y > ( double ) tmpImage->numRows ) ) {
+            return ( 0 );
+        }
+    return ( 1 );
 }
 
@@ -115,50 +160,65 @@
 XXX: must deallocate memory.
  *****************************************************************************/
-psCell *psCellinChip(psCell *out,
-                     const psPlane *coord,
-                     const psChip *chip)
-{
-    psCell *tmpCell = NULL;
-    psReadout *tmpReadout = NULL;
-    psPlane *tmpCoord = NULL;
-
-    for (tmpCell = chip->cells; tmpCell != NULL; tmpCell++) {
-        if (tmpCell->readouts != NULL) {
-            tmpReadout = tmpCell->readouts;
-            tmpCoord = psPlaneTransformApply(out, tmpCell->fromChip, coord);
-            if (p_psCheckValidImageCoords(tmpCoord->x, tmpCoord->y,
-                                          tmpReadout->image)) {
-                return(tmpCell);
-            }
-        }
-    }
-    return(NULL);
-}
-
-psPlane *psCoordCelltoChip(psPlane *out,
+psCell *psCellinChip( psCell *out,
+                      const psPlane *coord,
+                      const psChip *chip )
+{
+    psPlane * tmpCoord = NULL;
+    psArray* cells;
+    
+    if ( chip == NULL ) {
+            return NULL;
+        }
+        
+    cells = chip->cells;
+    
+    if ( cells == NULL ) {
+            return NULL;
+        }
+        
+    for ( int i = 0; i < cells->n; i++ ) {
+            psCell* tmpCell = ( psCell* ) cells->data[ i ];
+            psArray* readouts = tmpCell->readouts;
+            if ( readouts != NULL ) {
+                    for ( int j = 0; j < readouts->n; j++ ) {
+                            psReadout* tmpReadout = readouts->data[ j ];
+                            tmpCoord = psPlaneTransformApply( tmpCoord, tmpCell->fromChip, coord );
+                            if ( checkValidImageCoords( tmpCoord->x, tmpCoord->y,
+                                                        tmpReadout->image ) ) {
+                                    return ( tmpCell );
+                                }
+                        }
+                }
+        }
+    return ( NULL );
+}
+
+psPlane *psCoordCelltoChip( psPlane *out,
+                            const psPlane *in,
+                            const psCell *cell )
+{
+    return ( psPlaneTransformApply( out, cell->toChip, in ) );
+}
+
+psPlane *psCoordChipToFPA( psPlane *out,
                            const psPlane *in,
-                           const psCell *cell)
-{
-    return(psPlaneTransformApply(out, cell->toChip, in));
-}
-
-psPlane *psCoordChipToFPA(psPlane *out,
+                           const psChip *chip )
+{
+    return ( psPlaneTransformApply( out, chip->toFPA, in ) );
+}
+
+psPlane *psCoordFPAtoTP( psPlane *out,
+                         const psPlane *in,
+                         const psFPA *fpa )
+{
+    // XXX: This code doesn't work; fpa->toTangentPlane is of the wrong type.
+    // return(psPlaneTransformApply(out, fpa->toTangentPlane, in));
+    return ( NULL );
+}
+
+// XXX: must wrap SLA_QAPQK here.
+psSphere *psCoordTPtoSky( psSphere *out,
                           const psPlane *in,
-                          const psChip *chip)
-{
-    return(psPlaneTransformApply(out, chip->toFPA, in));
-}
-
-psPlane *psCoordFPAtoTP(psPlane *out,
-                        const psPlane *in,
-                        const psFPA *fpa)
-{
-    return(psPlaneTransformApply(out, fpa->toTangentPlane, in));
-}
-
-// XXX: must wrap SLA_QAPQK here.
-psSphere *psCoordTPtoSky(psSphere *out,
-                         const psPlane *in,
-                         const psGrommit *grommit)
+                          const psGrommit *grommit )
 {
     /*
@@ -169,14 +229,14 @@
         sla_OAPQK(TYPE, OB1, OB2, *grommit, &RAP, &DAP);
     */
-
-    return(out);
-}
-
-
-psPlane *psCoordCellToFPA(psPlane *out,
-                          const psPlane *in,
-                          const psCell *cell)
-{
-    return(psPlaneTransformApply(out, cell->toFPA, in));
+    
+    return ( out );
+}
+
+
+psPlane *psCoordCellToFPA( psPlane *out,
+                           const psPlane *in,
+                           const psCell *cell )
+{
+    return ( psPlaneTransformApply( out, cell->toFPA, in ) );
 }
 
@@ -185,107 +245,110 @@
 // transformation, as well as a few psPlane structs.  Can this be implemented
 // better?
-psSphere *psCoordCelltoSky(psSphere *out,
+psSphere *psCoordCelltoSky( psSphere *out,
+                            const psPlane *in,
+                            const psCell *cell )
+{
+    psPlane * tmp1 = NULL;
+    psPlane *tmp2 = NULL;
+    psFPA *parFPA = ( cell->parent ) ->parent;
+    psGrommit *tmpGrommit = NULL;
+    
+    tmp1 = psPlaneTransformApply( tmp1, cell->toFPA, in );
+    tmp2 = psPlaneTransformApply( tmp2, parFPA->toTangentPlane, tmp1 );
+    tmpGrommit = psGrommitAlloc( parFPA->exposure );
+    tmp3 = psCoordTPtoSky( out, tmp2, psGrommit );
+    
+    psFree( tmp1 );
+    psFree( tmp2 );
+    psFree( tmpGrommit );
+    
+    return ( psCoordTPtoSky( out, tmp2, psGrommit ) );
+    
+}
+
+psSphere *psCoordCelltoSkyQuick( psSphere *out,
+                                 const psPlane *in,
+                                 const psCell *cell )
+{
+    psPlane * tmp1 = NULL;
+    
+    tmp1 = psPlaneTransformApply( tmp1, cell->toSky, in );
+    
+    //XXX: Do something to convert the linear coords in tmp1 to spherical
+    // coords in out.
+    
+    psFree( tmp1 );
+    
+    return ( out );
+}
+
+// XXX: must wrap SLA_AOPQK here.
+psPlane *psCoordSkytoTP( psPlane *out,
+                         const psSphere *in,
+                         const psGrommit *grommit )
+{
+    extern void sla_AOPQK ( RAP, DAP, AOPRMS, AOB, ZOB, HOB, DOB, ROB );
+    double AOB;
+    double ZOB;
+    double HOB;
+    double DOB;
+    double ROB;
+    
+    if ( out == NULL ) {
+            out = ( psPlane * ) psAlloc( sizeof( psPlane ) );
+        }
+        
+    sla_AOPQK( psSphere->r, psSphere->d, *grommit, &AOB, &ZOB, &HOB, &DOB, &ROB );
+    out->x = XXX;
+    out->y = XXX;
+    return ( out );
+}
+
+psPlane *psCoordTPtoFPA( psPlane *out,
+                         const psPlane *in,
+                         const psFPA *fpa )
+{
+    return ( psPlaneTransformApply( out, fpa->fromTangentPlane, in ) );
+}
+
+psPlane *psCoordFPAtoChip( psPlane *out,
                            const psPlane *in,
-                           const psCell *cell)
-{
-    psPlane *tmp1 = NULL;
-    psPlane *tmp2 = NULL;
-    psFPA *parFPA = (cell->parent)->parent;
-    psGrommit *tmpGrommit = NULL;
-
-    tmp1 = psPlaneTransformApply(tmp1, cell->toFPA, in)
-           tmp2 = psPlaneTransformApply(tmp2, parFPA->toTangentPlane, tmp1)
-                  tmpGrommit = psGrommitAlloc(parFPA->exposure)
-                               tmp3 = psCoordTPtoSky(out, tmp2, psGrommit);
-
-    psFree(tmp1);
-    psFree(tmp2);
-    psFree(tmpGrommit);
-
-    return(psCoordTPtoSky(out, tmp2, psGrommit);
-       }
-
-       psSphere *psCoordCelltoSkyQuick(psSphere *out,
-                                       const psPlane *in,
-                                       const psCell *cell)
-       {
-           psPlane *tmp1 = NULL;
-
-           tmp1 = psPlaneTransformApply(tmp1, cell->toSky, in);
-
-           //XXX: Do something to convert the linear coords in tmp1 to spherical
-           // coords in out.
-
-           free(tmp1)
-
-           return(out);
-       }
-
-       // XXX: must wrap SLA_AOPQK here.
-       psPlane *psCoordSkytoTP(psPlane *out,
-                               const psSphere *in,
-                               const psGrommit *grommit)
-       {
-           extern void sla_AOPQK (RAP, DAP, AOPRMS, AOB, ZOB, HOB, DOB, ROB);
-           double AOB;
-           double ZOB;
-           double HOB;
-           double DOB;
-           double ROB;
-
-           if (out == NULL) {
-               out = (psPlane *) psAlloc(sizeof(psPlane));
-           }
-
-           sla_AOPQK(psSphere->r, psSphere->d, *grommit, &AOB, &ZOB, &HOB, &DOB, &ROB);
-           out->x = XXX;
-           out->y = XXX;
-           return(out);
-       }
-
-       psPlane *psCoordTPtoFPA(psPlane *out,
-                               const psPlane *in,
-                               const psFPA *fpa)
-       {
-           return(psPlaneTransformApply(out, fpa->fromTangentPlane, in));
-       }
-
-       psPlane *psCoordFPAtoChip(psPlane *out,
-                                 const psPlane *in,
-                                 const psChip *chip)
-       {
-           return(psPlaneTransformApply(out, chip->fromFPA, in));
-       }
-
-
-       psPlane *psCoordChiptoCell(psPlane *out,
-                                  const psPlane *in,
-                                  const psCell *cell)
-       {
-           return(psPlaneTransformApply(out, cell->fromChip, in));
-       }
-
-       psPlane *psCoordSkytoCell(psPlane *out,
-                                 const psSphere *in,
-                                 const psCell *cell)
-       {
-           out = psCoordSkytoTP(out, in, tmpGrommit);
-           out = psCoordTPtoFPA(out, out, whichFPA);
-           out = psCoordFPAtoChip(out, out, whichChip);
-           out = psCoordChiptoCell(out, out, whichCell);
-
-           return(out);
-       }
-
-       psPlane *psCoordSkytoCellQuick(psPlane *out,
-                                      const psSphere *in,
-                                      const psCell *cell)
-       {
-           if (out == NULL) {
-               out = (psPlane *) psAlloc(sizeof(psPlane));
-           }
-
-           return(out);
-       }
-
-
+                           const psChip *chip )
+{
+    return ( psPlaneTransformApply( out, chip->fromFPA, in ) );
+}
+
+
+psPlane *psCoordChiptoCell( psPlane *out,
+                            const psPlane *in,
+                            const psCell *cell )
+{
+    return ( psPlaneTransformApply( out, cell->fromChip, in ) );
+}
+
+psPlane *psCoordSkytoCell( psPlane *out,
+                           const psSphere *in,
+                           const psCell *cell )
+{
+    out = psCoordSkytoTP( out, in, tmpGrommit );
+    out = psCoordTPtoFPA( out, out, whichFPA );
+    out = psCoordFPAtoChip( out, out, whichChip );
+    out = psCoordChiptoCell( out, out, whichCell );
+    
+    return ( out );
+}
+
+psPlane *psCoordSkytoCellQuick( psPlane *out,
+                                const psSphere *in,
+                                const psCell *cell )
+{
+    if ( out == NULL ) {
+            out = ( psPlane * ) psAlloc( sizeof( psPlane ) );
+        }
+        
+    return ( out );
+}
+
+
+
+
