File: readpha.c

package info (click to toggle)
ftools-fv 5.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,072 kB
  • sloc: tcl: 50,774; ansic: 17,858; exp: 2,004; makefile: 172; cpp: 169; sh: 109; csh: 10
file content (164 lines) | stat: -rw-r--r-- 4,928 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fitsio.h>
#include "pow.h"
int readimage(char *,void **,float *,float *, long *,double *,double *,
	      double *,double *);
int printerror( int status);

int readpha(ClientData, Tcl_Interp *, int, char **); 



int readpha(ClientData clientData,Tcl_Interp *interp, int argc,
	      char *argv[]) 
{
  char filename[160] = "gis2img.fits";
  char array_name[32];
  FILE *rawout;
  float datamin, datamax;
  void *imagebuff;
  char *i, *j;
  int k;
  float straight_lineX[50],straight_lineY[50];
  int  status, array_type, datasize, height, width;
  int zero = 0, one = 1, fifty = 50;
  double d_zero = 0, d_one = 1;
  long naxes[2];
  double xorigin,xinc,yorigin,yinc;
  int xdisp = 300, ydisp = 300;
  int **databuff; 
  char longStr[1024];

  strcpy(filename,argv[1]);
  
  if((status = readimage(filename,&imagebuff,&datamin,&datamax,naxes, &xorigin,
			 &xinc, &yorigin, &yinc))) {
    printerror ( status);
    return TCL_ERROR;
  } 
  i = strstr(filename, ".");
  if(i == NULL) i = filename + strlen(filename);
  for (j = i; ((memcmp(j,"/",1)) && !(j == filename)) ; j--) {
    /*    printf("%x %x\n",filename,j);*/
  }
  if (!(memcmp(j,"/",1)) ) j++;
  for (k=0;k<32;k++) { array_name[k] = (char) NULL;}
  strncpy(array_name,j,i-j);
  
  array_type = REAL_DATA;
  datasize = pixelSizes[array_type];
  width = (int) naxes[0];
  height = (int) naxes[1];


  sprintf(longStr,PTRFORMAT " %i %i", imagebuff, array_type, width*height);
  Tcl_SetResult(interp, longStr, TCL_OK);
  return TCL_OK;
}


/*--------------------------------------------------------------------------*/
int readimage(char *filename,void **imagebuff,float *datamin,float *datamax,
	      long naxes[2],double *xorigin, double *xinc, double *yorigin,
	      double *yinc)

    /************************************************************************/
    /* Read a FITS image and determine the minimum and maximum pixel values */
    /************************************************************************/
{
    fitsfile *fptr;                             /* pointer to the FITS file */
    int status,  nfound, anynull;
    long  group, fpixel, nbuffer, npixels, ii;
    float *buffptr;
    char comm[80];

    float nullval;

    status = 0;

    Tcl_DumpActiveMemory("TCLDUMP");
    if ( fits_open_file(&fptr, filename, READONLY, &status) )     /* open the image */
         printerror( status );

    /* read the NAXIS1 and NAXIS2 keyword to get image size */
    if ( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status) )
         printerror( status );

    npixels  = naxes[0] * naxes[1];         /* number of pixels in the image */

    *imagebuff =  ckalloc(sizeof(float)*npixels);
    
    group    = 1;
    fpixel   = 1;
    nullval  = 0;                /* don't check for null values in the image */
    *datamin  = 1.0E30;
    *datamax  = -1.0E30;

    nbuffer = npixels;
    
    if ( fits_read_img(fptr,TFLOAT, fpixel, nbuffer,(void *) &nullval,
		       (void *) *imagebuff, &anynull, &status) ) {
      printerror( status );
      return TCL_ERROR;
    }
    
    buffptr = (float *) *imagebuff;
    for (ii = 0; ii < nbuffer; ii++)  {
      if (*buffptr < *datamin) 
	*datamin = *buffptr;

      if (*buffptr > *datamax) 
	*datamax = *buffptr;
      
      buffptr++;
    }
    fits_read_key_dbl(fptr,"CRVAL1",xorigin,comm,&status);
    fits_read_key_dbl(fptr,"CRVAL2",yorigin,comm,&status);
    fits_read_key_dbl(fptr,"WMREBIN",xinc,comm,&status);
    *yinc = *xinc;
    *xorigin *= *xinc;
    *yorigin *= *yinc;

    printf("\nMin and max image pixels =  %.0f, %.0f\n", *datamin, *datamax);

    if ( ffclos(fptr, &status) )                    /* close the FITS file */
         printerror( status );

    return status;
}





/*--------------------------------------------------------------------------*/
int printerror( int status)
{   char tmp_result[1000] = " ";
    /*****************************************************/
    /* Print out cfitsio error messages and exit program */
    /*****************************************************/

    char status_str[FLEN_STATUS], errmsg[FLEN_ERRMSG];
  
    if (status) {
        Tcl_AppendResult(interp,"\n*** Error occurred during program execution ***\n",(char *) NULL);

    ffgerr(status, status_str);        /* get the error status description */
    sprintf(tmp_result,"\nstatus = %d: %s\n", status, status_str);
    Tcl_AppendResult(interp,tmp_result,(char *) NULL);
    }

    if ( ffgmsg(errmsg) )  /* get first message; null if stack is empty */
    {
         sprintf(tmp_result,"\nError message stack:\n  %s\n", errmsg);
	 Tcl_AppendResult(interp,tmp_result,(char *) NULL);

         while ( ffgmsg(errmsg) )  /* get remaining messages */
	 Tcl_AppendResult(interp,errmsg,"\n",(char *) NULL);
    }

    return TCL_ERROR;       /* terminate the program, returning error status */
}