| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
#include <sys/types.h> |
| 6 |
|
| 7 |
#include <mShrinkCube.h> |
| 8 |
#include <montage.h> |
| 9 |
|
| 10 |
#define MAXSTR 256 |
| 11 |
|
| 12 |
extern char *optarg; |
| 13 |
extern int optind, opterr; |
| 14 |
|
| 15 |
extern int getopt(int argc, char *const *argv, const char *options); |
| 16 |
|
| 17 |
|
| 18 |
int main(int argc, char **argv) |
| 19 |
{ |
| 20 |
int c, debug, hdu, fixedSize, mfactor; |
| 21 |
double shrinkFactor; |
| 22 |
|
| 23 |
char input_file [MAXSTR]; |
| 24 |
char output_file[MAXSTR]; |
| 25 |
|
| 26 |
char *end; |
| 27 |
|
| 28 |
struct mShrinkCubeReturn *returnStruct; |
| 29 |
|
| 30 |
FILE *montage_status; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
debug = 0; |
| 38 |
fixedSize = 0; |
| 39 |
hdu = 0; |
| 40 |
mfactor = 1; |
| 41 |
|
| 42 |
opterr = 0; |
| 43 |
|
| 44 |
montage_status = stdout; |
| 45 |
|
| 46 |
while ((c = getopt(argc, argv, "d:h:m:s:f")) != EOF) |
| 47 |
{ |
| 48 |
switch (c) |
| 49 |
{ |
| 50 |
case 'd': |
| 51 |
debug = montage_debugCheck(optarg); |
| 52 |
|
| 53 |
if(debug < 0) |
| 54 |
{ |
| 55 |
fprintf(montage_status, "[struct stat=\"ERROR\", msg=\"Invalid debug level.\"]\n"); |
| 56 |
exit(1); |
| 57 |
} |
| 58 |
break; |
| 59 |
|
| 60 |
case 'h': |
| 61 |
hdu = strtol(optarg, &end, 10); |
| 62 |
|
| 63 |
if(end < optarg + strlen(optarg) || hdu < 0) |
| 64 |
{ |
| 65 |
printf("[struct stat=\"ERROR\", msg=\"HDU value (%s) must be a non-negative integer\"]\n", |
| 66 |
optarg); |
| 67 |
exit(1); |
| 68 |
} |
| 69 |
break; |
| 70 |
|
| 71 |
case 'm': |
| 72 |
mfactor = strtol(optarg, &end, 10); |
| 73 |
|
| 74 |
if(end < optarg + strlen(optarg) || mfactor <= 0) |
| 75 |
{ |
| 76 |
printf("[struct stat=\"ERROR\", msg=\"Third axis compression factor (%s) must be a positive integer\"]\n", |
| 77 |
optarg); |
| 78 |
exit(1); |
| 79 |
} |
| 80 |
break; |
| 81 |
|
| 82 |
case 's': |
| 83 |
if((montage_status = fopen(optarg, "w+")) == (FILE *)NULL) |
| 84 |
{ |
| 85 |
printf("[struct stat=\"ERROR\", msg=\"Cannot open status file: %s\"]\n", |
| 86 |
optarg); |
| 87 |
exit(1); |
| 88 |
} |
| 89 |
break; |
| 90 |
|
| 91 |
case 'f': |
| 92 |
fixedSize = 1; |
| 93 |
break; |
| 94 |
|
| 95 |
default: |
| 96 |
printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-f(ixed-size)] [-d level] [-h hdu] [-m factor] [-s statusfile] in.fits out.fits factor\"]\n", argv[0]); |
| 97 |
exit(1); |
| 98 |
break; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (argc - optind < 3) |
| 103 |
{ |
| 104 |
fprintf(montage_status, "[struct stat=\"ERROR\", msg=\"Usage: %s [-f(ixed-size)] [-d level] [-h hdu] [-m factor] [-s statusfile] in.fits out.fits factor\"]\n", argv[0]); |
| 105 |
exit(1); |
| 106 |
} |
| 107 |
|
| 108 |
strcpy(input_file, argv[optind]); |
| 109 |
strcpy(output_file, argv[optind + 1]); |
| 110 |
|
| 111 |
shrinkFactor = strtod(argv[optind + 2], &end); |
| 112 |
|
| 113 |
if(end < argv[optind + 2] + strlen(argv[optind + 2])) |
| 114 |
{ |
| 115 |
printf("[struct stat=\"ERROR\", msg=\"Shrink factor (%s) cannot be interpreted as an real number\"]\n", |
| 116 |
argv[optind + 2]); |
| 117 |
exit(1); |
| 118 |
} |
| 119 |
|
| 120 |
returnStruct = mShrinkCube(input_file, output_file, shrinkFactor, mfactor, hdu, fixedSize, debug); |
| 121 |
|
| 122 |
if(returnStruct->status == 1) |
| 123 |
{ |
| 124 |
fprintf(montage_status, "[struct stat=\"ERROR\", msg=\"%s\"]\n", returnStruct->msg); |
| 125 |
exit(1); |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
fprintf(montage_status, "[struct stat=\"OK\", module=\"mShrinkCube\", %s]\n", returnStruct->msg); |
| 130 |
exit(0); |
| 131 |
} |
| 132 |
} |