home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / xfitsvew.zip / XFITSview / logger.c < prev    next >
C/C++ Source or Header  |  1997-06-20  |  7KB  |  201 lines

  1. /* position logging  for XFITSview */
  2. /*-----------------------------------------------------------------------
  3. *  Copyright (C) 1996,1997
  4. *  Associated Universities, Inc. Washington DC, USA.
  5. *  This program is free software; you can redistribute it and/or
  6. *  modify it under the terms of the GNU General Public License as
  7. *  published by the Free Software Foundation; either version 2 of
  8. *  the License, or (at your option) any later version.
  9. *
  10. *  This program is distributed in the hope that it will be useful,
  11. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *  GNU General Public License for more details.
  14. *-----------------------------------------------------------------------*/
  15. #include "logger.h"
  16. #include "menu.h"
  17. #include "wpos.h"
  18. #include "imagedisp.h"
  19.  
  20. TextFilePtr LogText = NULL; /* logging TextFile */
  21.  
  22. /* internal prototypes */
  23. /* initialize log file, argument not used */
  24. void LoggerInit (XPointer arg);
  25. /* file selection canceled, arg not used */
  26. void LoggerCancel (XPointer arg);
  27.  
  28. /* public functions */
  29.  
  30. /* toggle position logging on/off */
  31. /* toggle start/stop logging callback clientData = ImageDisplay */
  32. void LoggerCB (Widget w, XtPointer clientData, XtPointer callData)
  33. {
  34.   ImageDisplay  *IDdata= (ImageDisplay *)clientData;
  35. /* turn on or off ?*/
  36.    if (doLog)
  37.      { /* turn off */
  38.        if (!LogText) return; /* bail out if no log TextFile */
  39.        doLog = 0;
  40.        MenuMarkLogger (1); /* reset menu item label */
  41.        TextFileKill(LogText); /* delete structures */
  42.        LogText = NULL;
  43.      }
  44.    else
  45.      {/* turn on */
  46.        if (LogText) TextFileKill (LogText); /* shouldn't need this */
  47.        if (!log_dir) log_dir = MakeString(" ");
  48.        LogText = TextFileMake (IDdata->shell, NULL, log_dir->sp);
  49.        /* ask for file name and initialize file */
  50.        TextFileFind (2, LogText, LoggerInit, LoggerCancel);
  51.        doLog = 1;
  52.        MenuMarkLogger (2); /* reset menu item label */
  53.      }
  54. } /* end LoggerCB */
  55.  
  56. /* log position */
  57. void DoLogger(void)
  58. {
  59.    int          ret;
  60.    Logical      valid, posOK;
  61.    Integer      ipos[7], ndim, error, i;
  62.    char         flux[20], equistr[7], pixel[23], line[100];
  63.    char         axtype[3][9], label[3][30];
  64.    double       pos[3];
  65.    float        pix[3], fblank, val;
  66.    MatrixPos    *impos;
  67.    ImageData    *Image = &image[CurImag];
  68.  
  69.    if (!Image) return; /* Validity check */
  70.    if (!Image->Image) return; /* Validity checks */
  71.    if (!Image->valid) return; 
  72.    if (!LogText) return; 
  73.    
  74. /* encode pixel location (convert to 1 relative) */
  75.    sprintf (pixel, "(%7.2f,%7.2f,%4d)", 
  76.         Image->fXpixel+1.0, Image->fYpixel+1.0, Image->PlaneNo+1);
  77.  
  78. /*  get flux  */
  79.      ndim = Image->ndim;  /* Number of dimensions  */
  80.      fblank = MagicBlank();
  81. /*  fitted or current?  */
  82.     if (Image->iFitted>0) /* fitted values  */
  83.       {valid = 1;
  84.        val = Image->fBpixel;}
  85.     else  /* current position  */
  86.       {val = 0;
  87.        for (i=3; i<7; i++) ipos[i] = 0;
  88. /* Note: Image class pixel numbers are 0 rel; geometry is 1 rel.  */
  89.        ipos[0] = Image->iXPixel; ipos[1] = Image->iYPixel;
  90.        ipos[2] = Image->PlaneNo;
  91.        impos = MakeMatrixPos(ndim, ipos);
  92.        valid = (ndim>1) && IsValid(Image->Image->matx, impos);
  93.        if (valid)
  94.          {
  95.           val = MatrixGetPixel(Image->Image->matx, impos);
  96.           error = Image->Image->matx->fw->error;
  97.           if (error)   /* I/O error  */
  98.             {ErrorMess("DoLogger: Error reading pixel value");
  99.          Image->valid = 0; /* mark as invalid */
  100.              KillMatrixPos(impos);
  101.          return;}
  102.          } /* end of read valid pixel */
  103.         KillMatrixPos(impos); /* destroy what you create */
  104.       } /* end of read current pixel */
  105.     if ((val==fblank) || (!valid)) /* blanked or invalid? - quit*/
  106.       {return;}
  107.     sprintf (flux, "%f",val);
  108.     flux[7] = 0;   /* only 6 char */
  109.  
  110. /* equinox */
  111.     sprintf (equistr, "  ????");
  112.     if ((usr_equinox>0.0) && (Image->Image->descript->equinox>0.0))
  113.       {if (usr_equinox==2000.0) sprintf (equistr, " J2000");
  114.       if(usr_equinox==1950.0) sprintf (equistr, " B1950");}
  115.     if ((usr_equinox<0.0) && (Image->Image->descript->equinox>0.0))
  116.       {if (Image->Image->descript->equinox==2000.0) sprintf (equistr, " J2000");
  117.       if (Image->Image->descript->equinox==1950.0) sprintf (equistr, " B1950");}
  118.  
  119. /* celestial position  */
  120.    pix[0] = Image->fXpixel+1.0; pix[1] = Image->fYpixel+1.0;
  121.    pix[2] = (float)(Image->PlaneNo)+1.0;
  122.    strncpy(axtype[0], Image->cname[0]->sp, 8);
  123.    strncpy(axtype[1], Image->cname[1]->sp, 8);
  124.    axtype[0][8] = 0; axtype[1][8] = 0;
  125.    posOK = !get_wpos(Image->Image->descript, pix, pos);
  126.    AxisLabel(pos[0], axtype[0], label[0]);  /* human readable  */
  127.    label[1][0] = 0;
  128.    if (ndim>=2) AxisLabel(pos[1], axtype[1], label[1]);
  129.    label[0][17]=0; /* only 17 characters */
  130.    label[1][17]=0; /* only 17 characters */
  131.    if (!posOK)   /* valid position? if not quit */
  132.      {return;} /* bail out - logging OK */
  133.       
  134. /* Open Log File */
  135.    if (TextFileOpen (LogText, 2) != 1) {
  136.      ErrorMess ("Error opening logging file");
  137.      TextFileKill(LogText);
  138.      LogText = NULL;
  139.      doLog = 0;
  140.      MenuMarkLogger (1); /* reset menu item label */
  141.      return;
  142.    } /* end of error trap */
  143.    
  144. /* write output line */
  145.    sprintf (line, "%s %17s,%17s,%6s,%6s,%s", 
  146.       pixel, label[0], label[1], equistr,flux, Image->FileName->sp);
  147.    if (!TextFileWrite(LogText, line)) {
  148.      ErrorMess ("Error writing logging file");
  149.      TextFileClose(LogText);
  150.      TextFileKill(LogText);
  151.      LogText = NULL;
  152.      doLog = 0;
  153.      MenuMarkLogger (1); /* reset menu item label */
  154.      return;
  155.    } /* end of error trap */
  156.    TextFileClose(LogText); /* close file */
  157.    return;
  158. } /* end DoLogger */
  159.  
  160. /* initialize log file, argument not used */
  161. /* called after file selected */
  162. void LoggerInit (XPointer arg)
  163. {
  164.    char line[100];
  165.    
  166. /* Open/create Text File */
  167.    if (TextFileOpen (LogText, 2) != 1) {
  168.      ErrorMess ("Error opening logging file");
  169.      TextFileKill(LogText);
  170.      LogText = NULL;
  171.      doLog = 0;
  172.      MenuMarkLogger (1); /* reset menu item label */
  173.      return;
  174.    } /* end of error trap */
  175.    
  176. /* Add first entry */
  177.    sprintf (line, "XFITSview position logging: (pixel) celestial pos., equinox, value, filename");
  178.    if (!TextFileWrite(LogText, line)) {
  179.      ErrorMess ("Error writing logging file");
  180.      TextFileClose(LogText);
  181.      TextFileKill(LogText);
  182.      LogText = NULL;
  183.      doLog = 0;
  184.      MenuMarkLogger (1); /* reset menu item label */
  185.      return;
  186.    } /* end of error trap */
  187.    TextFileClose(LogText); /* close file */
  188.  
  189. } /* end LoggerInit */
  190.  
  191. /* file selection canceled, arg not used */
  192. void LoggerCancel (XPointer arg)
  193. {
  194. /* shut down, bail out */
  195. /*if (LogText&&LogText->State) TextFileClose(LogText);  close file if open*/
  196.   if (LogText) TextFileKill(LogText); /* delete structure */
  197.   LogText = NULL;
  198.   doLog = 0;
  199.   MenuMarkLogger (1); /* reset menu item label */
  200. } /* end LoggerCancel */
  201.