00001 #include "mex.h"
00002 #include <math.h>
00003 #include <stdint.h>
00004 #include <fitsio.h>
00005 #include "mexhead.h"
00006 #include "Doc/loadfitsa_docstring.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define NARGIN_MIN 1
00061 #define NARGIN_MAX 3
00062 #define NARGOUT_MIN 0
00063 #define NARGOUT_MAX 3
00064
00065 #define ARG_FILENAME 0
00066 #define ARG_BLOCK 1
00067 #define ARG_TYPE 2
00068
00069 #define ARG_IMAGE 0
00070 #define ARG_ERROR 1
00071 #define ARG_PARAMS 2
00072
00073 #define PROGNAME loadfitsa
00074 static const char *progname = "loadfitsa";
00075 static const char *in_specs[NARGIN_MAX ] = { "SV", "IS", "SV" };
00076 static const char *in_names[NARGIN_MAX ] = { "filename", "block" };
00077 static const char *out_names[NARGOUT_MAX] = { "image", "error", "params" };
00078
00079
00080
00081
00082
00083 static
00084 void
00085 fits_printerror(int status)
00086 {
00087 char errmsg[81];
00088
00089 mexPrintf("%s: Runtime error (%s). Exiting.\n",
00090 progname,
00091 (status == 0) ? "not a FITSIO error" : "FITSIO trace follows");
00092 if (status != 0) {
00093 fits_get_errstatus(status, errmsg);
00094 mexPrintf("status = %d: %s\n", status, errmsg);
00095
00096 mexPrintf("Error message stack:\n");
00097 while (fits_read_errmsg(errmsg))
00098 mexPrintf("\t%s\n", errmsg);
00099 }
00100 }
00101
00102
00103 static
00104 void
00105 fio_get_type(char *typeS, int *typeF, mxClassID *typeM, void *nullval)
00106 {
00107 if (strcmp(typeS, "double") == 0) {
00108 *(double *)nullval = mxt_getnand();
00109 *typeM = mxDOUBLE_CLASS;
00110 *typeF = TDOUBLE;
00111 } else if (strcmp(typeS, "single") == 0) {
00112 *(float *)nullval = mxt_getnanf();
00113 *typeM = mxSINGLE_CLASS;
00114 *typeF = TFLOAT;
00115 } else if (strcmp(typeS, "int8") == 0) {
00116 *(int8_t *)nullval = INT8_MIN;
00117 *typeM = mxINT8_CLASS;
00118 *typeF = TSBYTE;
00119 } else if (strcmp(typeS, "uint8") == 0) {
00120 *(uint8_t *)nullval = UINT8_MAX;
00121 *typeM = mxUINT8_CLASS;
00122 *typeF = TBYTE;
00123 } else if (strcmp(typeS, "int16") == 0) {
00124 *(int16_t *)nullval = INT16_MIN;
00125 *typeM = mxINT16_CLASS;
00126 *typeF = TSHORT;
00127 } else if (strcmp(typeS, "uint16") == 0) {
00128 *(uint16_t *)nullval = UINT16_MAX;
00129 *typeM = mxUINT16_CLASS;
00130 *typeF = TUSHORT;
00131 } else if (strcmp(typeS, "int32") == 0) {
00132 *(int32_t *)nullval = INT32_MIN;
00133 *typeM = mxINT32_CLASS;
00134 *typeF = TINT;
00135 } else if (strcmp(typeS, "uint32") == 0) {
00136 *(uint32_t *)nullval = UINT32_MAX;
00137 *typeM = mxUINT32_CLASS;
00138 *typeF = TUINT;
00139 } else if (strcmp(typeS, "int64") == 0) {
00140 *(int64_t *)nullval = INT64_MIN;
00141 *typeM = mxINT64_CLASS;
00142 *typeF = TLONGLONG;
00143 } else if (strcmp(typeS, "uint64") == 0) {
00144 *(uint64_t *)nullval = UINT64_MAX;
00145 *typeM = mxUINT64_CLASS;
00146 *typeF = TULONG;
00147 } else {
00148
00149 *typeM = mxUNKNOWN_CLASS;
00150 *typeF = 0;
00151 }
00152 }
00153
00154
00155
00156
00157
00158 static
00159 mxArray *
00160 fio_GetFITS(char *filename, int block, char *typeS,
00161 int *bitpix_p, double *bscale, double *bzero, double *blank)
00162 {
00163 mxArray *pm;
00164 fitsfile *fptr;
00165 int status = 0;
00166 int naxisM;
00167 int naxisF;
00168 mwSize *naxesM;
00169 long *naxesF;
00170 int nfound;
00171 long nelements;
00172 int i;
00173 int bitpix;
00174 char nulbuf[8];
00175 void *nullval = nulbuf;
00176 mxClassID typeM;
00177 int typeF;
00178 int has_extension;
00179
00180 fits_open_file(&fptr, filename, READONLY, &status);
00181 if (status) {
00182 fits_printerror(status);
00183 return NULL;
00184 }
00185 fits_get_img_dim(fptr, &naxisF, &status);
00186 fits_read_key(fptr, TLOGICAL, "EXTEND", &has_extension, NULL, &status);
00187 if (status) {status = 0; has_extension = 0;}
00188 while (status == 0 && has_extension && naxisF == 0) {
00189
00190 fits_movrel_hdu(fptr, 1, NULL, &status);
00191 fits_get_img_dim(fptr, &naxisF, &status);
00192 }
00193 fits_get_img_type(fptr, &bitpix, &status);
00194
00195 if (status) {
00196 fits_printerror(status);
00197 return NULL;
00198 }
00199
00200
00201 if (bitpix_p) {
00202 *bitpix_p = bitpix;
00203 }
00204 if (bscale) {
00205 fits_read_key(fptr, TDOUBLE, "BSCALE", bscale, NULL, &status);
00206 if (status) {
00207 status = 0; *bscale = 1.0;
00208 }
00209 }
00210 if (bzero) {
00211 fits_read_key(fptr, TDOUBLE, "BZERO", bzero, NULL, &status);
00212 if (status) {
00213 status = 0; *bzero = 0.0;
00214 }
00215 }
00216 if (blank) {
00217 fits_read_key(fptr, TDOUBLE, "BLANK", blank, NULL, &status);
00218 if (status) {
00219 status = 0; *blank = 0.0;
00220 }
00221 }
00222
00223
00224
00225
00226 fio_get_type(typeS, &typeF, &typeM, nullval);
00227 if (typeM == mxUNKNOWN_CLASS) {
00228 mexPrintf("Did not understand desired output class\n");
00229 fits_printerror(0);
00230 return NULL;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 if (bitpix < 0)
00241 nullval = NULL;
00242
00243 naxisM = (naxisF < 2) ? 2 : naxisF;
00244 naxesF = calloc(naxisF, sizeof(*naxesF));
00245 naxesM = calloc(naxisM, sizeof(*naxesM));
00246 if (naxesM == NULL || naxesF == NULL) {
00247 mexPrintf("Failed to input-convert matrix (failed calloc)\n");
00248 fits_printerror(0);
00249 return NULL;
00250 }
00251
00252 fits_get_img_size(fptr, naxisF, naxesF, &status);
00253 if (status) {
00254 free(naxesM);
00255 free(naxesF);
00256 fits_printerror(status);
00257 return NULL;
00258 }
00259
00260 for (i = 0; i < naxisM; i++)
00261 if (i < naxisF)
00262 naxesM[i] = 1 + (naxesF[i] - 1) / block;
00263 else {
00264
00265 if (i == 0)
00266 naxesM[i] = 0;
00267 else
00268 naxesM[i] = (naxesM[i-1] == 0) ? 0 : 1;
00269 }
00270
00271
00272 pm = mxCreateNumericArray(naxisM, naxesM, typeM, mxREAL);
00273 nelements = (long) mxGetNumberOfElements(pm);
00274
00275 if (block == 1) {
00276
00277 fits_read_img(fptr, typeF, 1L, nelements, nullval,
00278 mxGetPr(pm), &nfound, &status);
00279 if (status) {
00280
00281 mxDestroyArray(pm);
00282 free(naxesM);
00283 free(naxesF);
00284 fits_printerror(status);
00285 return NULL;
00286 }
00287 } else {
00288
00289 long *fpixel = calloc(naxisF, sizeof(long));
00290 long *blocks = calloc(naxisF, sizeof(long));
00291 if (fpixel == NULL || blocks == NULL) {
00292 mexPrintf("Failed to block input matrix (failed calloc)\n");
00293 mxDestroyArray(pm);
00294 free(naxesM);
00295 free(naxesF);
00296 fits_printerror(0);
00297 return NULL;
00298 }
00299 for (i = 0; i < naxisF; i++) {
00300 fpixel[i] = 1L;
00301 blocks[i] = (long) block;
00302 }
00303 fits_read_subset(fptr, typeF, fpixel, naxesF, blocks, nullval,
00304 mxGetPr(pm), &nfound, &status);
00305 free(fpixel);
00306 free(blocks);
00307 if (status) {
00308
00309 mxDestroyArray(pm);
00310 free(naxesM);
00311 free(naxesF);
00312 fits_printerror(status);
00313 return NULL;
00314 }
00315 }
00316
00317 free(naxesM);
00318 free(naxesF);
00319
00320 if (fits_close_file(fptr, &status)) {
00321 mxDestroyArray(pm);
00322 fits_printerror(status);
00323 return NULL;
00324 }
00325 #ifdef FITS_TRANSPOSE_MODE
00326
00327
00328
00329 if (!transpose_double_mxArray(pm)) {
00330 mexPrintf("Failed to input-convert matrix (failed malloc)\n");
00331 fits_printerror(0);
00332 return NULL;
00333 }
00334 #endif
00335 return pm;
00336 }
00337
00338
00339 #ifdef StaticP
00340 StaticP
00341 #endif
00342 void
00343 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
00344 {
00345 char errstr[200];
00346 char *filename;
00347 int block;
00348 char *dest_type;
00349 mxArray *result;
00350 int bitpix;
00351 double bscale, bzero, blank;
00352
00353
00354 if (nrhs < 0) {
00355 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00356 NARGIN_MIN, NARGIN_MAX,
00357 NARGOUT_MIN, NARGOUT_MAX,
00358 in_names, in_specs, out_names, docstring);
00359 return;
00360 }
00361
00362 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00363 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00364 "%s: Expect %d <= input args <= %d",
00365 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00366 if ((nlhs < NARGOUT_MIN) || (nlhs > NARGOUT_MAX))
00367 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00368 "%s: Expect %d <= output args <= %d",
00369 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00370 mexargparse(nrhs, prhs, in_names, in_specs, NULL, progname);
00371 start_sizechecking();
00372 sizeinit(prhs[ARG_FILENAME]);
00373 sizeisM(1);
00374 sizecheck_msg(progname, in_names, ARG_FILENAME);
00375 if (nrhs > ARG_TYPE) {
00376 sizeinit(prhs[ARG_TYPE]);
00377 sizeisM(1);
00378 sizecheck_msg(progname, in_names, ARG_TYPE);
00379 }
00380
00381
00382 filename = mxArrayToString(prhs[ARG_FILENAME]);
00383 if (!filename)
00384 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00385 "%s: Trouble converting filename string",
00386 progname), errstr));
00387
00388
00389 if (nrhs > ARG_BLOCK)
00390 block = (int) mxGetScalar(prhs[ARG_BLOCK]);
00391 else
00392 block = 1;
00393 if (block <= 0)
00394 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00395 "%s: blocking factor must be positive",
00396 progname), errstr));
00397
00398
00399 if (nrhs > ARG_TYPE) {
00400 dest_type = mxArrayToString(prhs[ARG_TYPE]);
00401 } else {
00402 dest_type = "double";
00403 }
00404 if (!dest_type)
00405 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00406 "%s: Trouble converting type string",
00407 progname), errstr));
00408
00409
00410 result = fio_GetFITS(filename, block, dest_type, &bitpix, &bscale, &bzero, &blank);
00411 mxFree(filename);
00412 if (nrhs > ARG_TYPE)
00413 mxFree(dest_type);
00414
00415
00416 plhs[ARG_IMAGE] = result ?
00417 result : mxCreateDoubleMatrix((mwSize) 0, (mwSize) 0, mxREAL);
00418
00419
00420 if (nlhs > ARG_PARAMS) {
00421 plhs[ARG_PARAMS] = mxCreateDoubleMatrix(1, 4, mxREAL);
00422 mxGetPr(plhs[ARG_PARAMS])[0] = (double) bitpix;
00423 mxGetPr(plhs[ARG_PARAMS])[1] = bscale;
00424 mxGetPr(plhs[ARG_PARAMS])[2] = bzero;
00425 mxGetPr(plhs[ARG_PARAMS])[3] = blank;
00426 }
00427
00428
00429 if (nlhs > ARG_ERROR)
00430 plhs[ARG_ERROR] = mxCreateDoubleScalar((double) (result == NULL));
00431
00432
00433 if ((result == NULL) && (nlhs <= ARG_ERROR))
00434 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00435 "%s: Runtime error. Exiting.",
00436 progname), errstr));
00437 }
00438
00439
00440
00441
00442 #ifdef MEX2C_TAIL_HOOK
00443 #include "mex2c_tail.h"
00444 #endif
00445