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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* File addpix.c
* June 21, 2006
* By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
* Send bug reports to jmink@cfa.harvard.edu
Copyright (C) 2006
Smithsonian Astrophysical Observatory, Cambridge, MA USA
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include "libwcs/fitsfile.h"
#include "libwcs/wcs.h"
static void usage();
static int newimage = 0;
static int verbose = 0; /* verbose flag */
static char *RevMsg = "ADDPIX WCSTools 3.9.5, 30 March 2017, Jessica Mink (jmink@cfa.harvard.edu)";
static void AddPix();
static int version = 0; /* If 1, print only program name and version */
int
main (ac, av)
int ac;
char **av;
{
char *str;
char *fn;
char *value[100];
int i, x[100], y[100];
/* Check for help or version command first */
str = *(av+1);
if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
usage();
if (!strcmp (str, "version") || !strcmp (str, "-version")) {
version = 1;
usage();
}
/* crack arguments */
for (av++; --ac > 0 && *(str = *av) == '-'; av++) {
char c;
while ((c = *++str))
switch (c) {
case 'v': /* more verbosity */
verbose++;
break;
case 'n': /* ouput new file */
newimage++;
break;
default:
usage ();
break;
}
}
/* now there are ac remaining arguments starting at av[0] */
if (ac == 0)
usage ();
fn = *av++;
i = 0;
while (--ac > 2) {
x[i] = atoi (*av++);
ac--;
y[i] = atoi (*av++);
ac--;
value[i] = *av++;
i++;
}
if (i > 0)
AddPix (fn,i,x,y,value);
return (0);
}
static void
usage ()
{
fprintf (stderr,"%s\n",RevMsg);
if (version)
exit (-1);
fprintf (stderr,"Add to pixel of FITS or IRAF image file\n");
fprintf(stderr,"Usage: addpix [-vn] file.fts x y value...\n");
fprintf(stderr," -n: write new file, else overwrite \n");
fprintf(stderr," -v: verbose\n");
exit (1);
}
static void
AddPix (filename, n, x, y, value)
char *filename; /* FITS or IRAF file filename */
int n; /* number of pixels to change */
int *x, *y; /* Horizontal and vertical coordinates of pixel */
/* (1-based) */
char **value; /* value to insert into pixel */
{
char *image; /* FITS image */
char *header; /* FITS header */
int lhead; /* Maximum number of bytes in FITS header */
int nbhead; /* Actual number of bytes in FITS header */
int iraffile; /* 1 if IRAF image */
char *irafheader = NULL; /* IRAF image header */
int i, lext, lroot;
char *imext, *imext1;
double bzero; /* Zero point for pixel scaling */
double bscale; /* Scale factor for pixel scaling */
char newname[256];
char pixname[256];
char tempname[256];
char history[64];
char *ext, *fname;
char echar;
char newline[1];
double dpix, dpix0;
int bitpix,xdim,ydim;
newline[0] = 10;
strcpy (tempname, "fitshead.temp");
/* Open IRAF image and header if .imh extension is present */
if (isiraf (filename)) {
iraffile = 1;
if ((irafheader = irafrhead (filename, &lhead)) != NULL) {
if ((header = iraf2fits (filename, irafheader, lhead, &nbhead)) == NULL) {
free (irafheader);
fprintf (stderr, "Cannot translate IRAF header %s/n", filename);
return;
}
if ((image = irafrimage (header)) == NULL) {
hgetm (header,"PIXFIL", 255, pixname);
fprintf (stderr, "Cannot read IRAF pixel file %s\n", pixname);
free (irafheader);
free (header);
return;
}
}
else {
fprintf (stderr, "Cannot read IRAF header file %s\n", filename);
return;
}
}
/* Read FITS image and header if .imh extension is not present */
else {
iraffile = 0;
if ((header = fitsrhead (filename, &lhead, &nbhead)) != NULL) {
if ((image = fitsrimage (filename, nbhead, header)) == NULL) {
fprintf (stderr, "Cannot read FITS image %s\n", filename);
free (header);
return;
}
}
else {
fprintf (stderr, "Cannot read FITS file %s\n", filename);
return;
}
}
if (verbose)
fprintf (stderr,"%s\n",RevMsg);
/* Add specified value to specified pixel */
hgeti4 (header,"BITPIX",&bitpix);
hgeti4 (header,"NAXIS1",&xdim);
hgeti4 (header,"NAXIS2",&ydim);
bzero = 0.0;
hgetr8 (header,"BZERO",&bzero);
bscale = 1.0;
hgetr8 (header,"BSCALE",&bscale);
for (i = 0; i < n; i++) {
if (strchr (value[i],(int)'.'))
dpix = (double) atoi (value[i]);
else
dpix = atof (value[i]);
dpix0 = getpix (image, bitpix, xdim, ydim, bzero, bscale, x[i]-1, y[i]-1);
dpix = dpix + dpix0;
putpix (image, bitpix, xdim, ydim, bzero, bscale, x[i]-1, y[i]-1, dpix);
/* Note addition as history line in header */
if (bitpix > 0) {
int ipix = (int)dpix;
sprintf (history, "ADDPIX: %d added to pixel at row %d, column %d",
ipix,x[i],y[i]);
}
else if (dpix < 1.0 && dpix > -1.0)
sprintf (history, "ADDPIX: %f added to pixel at row %d, column %d",
dpix,x[i],y[i]);
else
sprintf (history,"ADDPIX: %.2f added to pixel at row %d, column %d",
dpix,x[i],y[i]);
hputc (header,"HISTORY",history);
if (verbose)
printf ("%s\n", history);
}
/* Make up name for new FITS or IRAF output file */
if (newimage) {
/* Remove directory path and extension from file name */
fname = strrchr (filename, '/');
if (fname)
fname = fname + 1;
else
fname = filename;
ext = strrchr (fname, '.');
if (ext != NULL) {
lext = (fname + strlen (fname)) - ext;
lroot = ext - fname;
strncpy (newname, fname, lroot);
*(newname + lroot) = 0;
}
else {
lext = 0;
lroot = strlen (fname);
strcpy (newname, fname);
}
imext = strchr (fname, ',');
imext1 = NULL;
if (imext == NULL) {
imext = strchr (fname, '[');
if (imext != NULL) {
imext1 = strchr (fname, ']');
*imext1 = (char) 0;
}
}
if (imext != NULL) {
strcat (newname, "_");
strcat (newname, imext+1);
}
if (fname)
fname = fname + 1;
else
fname = filename;
strcat (newname, "e");
if (lext > 0) {
if (imext != NULL) {
echar = *imext;
*imext = (char) 0;
strcat (newname, ext);
*imext = echar;
if (imext1 != NULL)
*imext1 = ']';
}
else
strcat (newname, ext);
}
}
else
strcpy (newname, filename);
/* Write fixed header to output file */
if (iraffile) {
if (irafwimage (newname,lhead,irafheader,header,image) > 0 && verbose)
printf ("%s rewritten successfully.\n", newname);
else if (verbose)
printf ("%s could not be written.\n", newname);
free (irafheader);
}
else {
if (fitswimage (newname, header, image) > 0 && verbose)
printf ("%s: rewritten successfully.\n", newname);
else if (verbose)
printf ("%s could not be written.\n", newname);
}
free (image);
free (header);
return;
}
/* Dec 6 1996 New program
*
* Feb 21 1997 Check pointers against NULL explicitly for Linux
* Dec 15 1997 Add capability of reading and writing IRAF 2.11 images
*
* May 27 1998 Include fitsio.h
* Jul 24 1998 Make irafheader char instead of int
* Aug 6 1998 Change fitsio.h to fitsfile.h
* Aug 14 1998 Preserve extension when creating new file name
* Oct 13 1998 Use isiraf() to determine file type
* Nov 30 1998 Add version and help commands for consistency
*
* Apr 29 1999 Add BZERO and BSCALE
* Jun 29 1999 Fix typo in BSCALE setting
* Oct 21 1999 Drop unused variables after lint
*
* Mar 23 2000 Use hgetm() to get the IRAF pixel file name, not hgets()
*
* Apr 9 2002 Do not free unallocated header
*
* Jun 21 2006 Clean up code
*/
|