fitsbody.c 38.8 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1001
1002
1003
            else
              {
               unsigned char	*bufdata = (unsigned char *)cbufdata0;
1004
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1005
              for (i=spoonful; i--;)
1006
                *(bufdata++) = (unsigned char)((*(ptr++)-bz)/bs+0.49999);;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1007
1008
1009
1010
1011
1012
1013
              }
            break;

          case BP_SHORT:
            if (tab->bitsgn)
              {
               short	*bufdata = (short *)cbufdata0;
1014
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1015
1016
1017
1018
1019
1020
              for (i=spoonful; i--;)
                *(bufdata++) = (short)((*(ptr++)-bz)/bs+0.49999);
              }
            else
              {
               unsigned short	*bufdata = (unsigned short *)cbufdata0;
1021
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
              for (i=spoonful; i--;)
                *(bufdata++) = (unsigned short)((*(ptr++)-bz)/bs+0.49999);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 2, spoonful);
            break;

          case BP_LONG:
           if (tab->bitsgn)
              {
               int	*bufdata = (int *)cbufdata0;
1033
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1034
1035
1036
1037
1038
1039
              for (i=spoonful; i--;)
                *(bufdata++) = (int)((*(ptr++)-bz)/bs+0.49999);
              }
            else
              {
               unsigned int	*bufdata = (unsigned int *)cbufdata0;
1040
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1041
1042
1043
1044
1045
1046
1047
              for (i=spoonful; i--;)
                *(bufdata++) = (unsigned int)((*(ptr++)-bz)/bs+0.49999);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 4, spoonful);
            break;

1048
1049
1050
1051
#ifdef HAVE_LONG_LONG_INT
          case BP_LONGLONG:
           if (tab->bitsgn)
              {
1052
               SLONGLONG	*bufdata = (SLONGLONG *)cbufdata0;
1053
#pragma ivdep
1054
              for (i=spoonful; i--;)
1055
                *(bufdata++) = (SLONGLONG)((*(ptr++)-bz)/bs+0.49999);
1056
1057
1058
1059
              }
            else
              {
               ULONGLONG	*bufdata = (ULONGLONG *)cbufdata0;
1060
#pragma ivdep
1061
1062
1063
1064
1065
1066
1067
              for (i=spoonful; i--;)
                *(bufdata++) = (ULONGLONG)((*(ptr++)-bz)/bs+0.49999);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 8, spoonful);
            break;
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1068
1069
1070
          case BP_FLOAT:
            {
             float	*bufdata = (float *)cbufdata0;
1071
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1072
1073
            for (i=spoonful; i--;)
              *(bufdata++) = (*(ptr++)-bz)/bs;
1074

1075
1076
1077
#ifdef	HAVE_CFITSIO
            if (!tab->infptr && bswapflag)
#else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1078
            if (bswapflag)
1079
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1080
1081
1082
1083
1084
1085
1086
              swapbytes(cbufdata0, 4, spoonful);
            }
            break;

          case BP_DOUBLE:
            {
             double	*bufdata = (double *)cbufdata0;
1087
#pragma ivdep
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1088
1089
1090
1091
1092
1093
1094
1095
            for (i=spoonful; i--;)
              *(bufdata++) = (double)(*(ptr++)-bz)/bs;
            if (bswapflag)
              swapbytes(cbufdata0, 8, spoonful);
            }
            break;

          default:
1096
1097
1098
1099
            error(EXIT_FAILURE,"*FATAL ERROR*: unknown BITPIX type in ",
                                "read_body()");
            break;
          }
1100

1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
#ifdef	HAVE_CFITSIO
          // if cfitsio output file has been set up, then proceed to write
          if (tab->infptr) {
            // now read section of image
             int	datatype;
            switch(tab->bitpix) {
              case BYTE_IMG:
                datatype = TBYTE;
                break;
              case SHORT_IMG:
                datatype = TSHORT;
                  break;
              case LONG_IMG:
                datatype = TLONG;
                break;
              case FLOAT_IMG:
                datatype = TFLOAT;
                break;
              case DOUBLE_IMG:
                datatype = TDOUBLE;
                break;
              default:
                datatype = TFLOAT;
                break;
            }

            // turn off any scaling so that we copy the raw pixel values
             double	*array,
			bscale = 1.0,
			bzero = 0.0,
			nulval = 0.0;

             int	status = 0;

            fits_set_bscale(tab->infptr, bscale, bzero, &status);
            status = 0;
            fits_write_img(tab->infptr, datatype, tab->currentElement, spoonful,
			cbufdata0, &status);

            if (status) {

              printf("CFITSIO ERROR writing start=%d end=%d absolute end=%d\n",
			tab->currentElement, (tab->currentElement + spoonful),
			(tab->naxisn[0]*tab->naxisn[1]));
              fits_report_error(stderr, status);
            }

            tab->currentElement  = tab->currentElement  + spoonful;
        } else
          // otherwise, continue with usual AstrOmatic fits writing routine
          QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
#else
        QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
#endif // HAVE_CFITSIO
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
        }
      break;

/*-- Compressed image */
    case COMPRESS_BASEBYTE:
      break;

    case COMPRESS_PREVPIX:
      break;

    default:
      error(EXIT_FAILURE,"*Internal Error*: unknown compression mode in ",
                                "read_body()");
    }

  return;
  }


/******* write_ibody ***********************************************************
PROTO	write_ibody(tabstruct *tab, FLAGTYPE *ptr, long size)
PURPOSE	Write integer values to a FITS body.
INPUT	A pointer to the tab structure,
	a pointer to the array in memory,
	the number of elements to be written.
OUTPUT	-.
NOTES	.
AUTHOR	E. Bertin (IAP)
1183
VERSION	11/02/2020
1184
1185
1186
1187
1188
1189
1190
 ***/
void	write_ibody(tabstruct *tab, FLAGTYPE *ptr, size_t size)
  {
   static FLAGTYPE	bufdata0[DATA_BUFSIZE/sizeof(FLAGTYPE)];
   catstruct		*cat;
   char			*cbufdata0;
   size_t		i, bowl, spoonful;
1191
   unsigned short	ashort = 1;
1192
   double		bs,bz;
1193
1194
1195
   int			bswapflag;

  bswapflag = *((char *)&ashort);	// Byte-swapping flag
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221

  bs = tab->bscale;
  bz = tab->bzero;
  cat = tab->cat;
  if (!cat)
    error(EXIT_FAILURE, "*Internal Error*: no parent cat structure for table ",
		tab->extname);

  cbufdata0 = (char *)bufdata0;	/* A trick to remove gcc aliasing warnings */
 
  switch(tab->compress_type)
    {
/*-- Uncompressed image */
    case COMPRESS_NONE:
      bowl = DATA_BUFSIZE/tab->bytepix;
      spoonful = size<bowl?size:bowl;
      for(; size>0; size -= spoonful)
        {
        if (spoonful>size)
          spoonful = size;
        switch(tab->bitpix)
          {
          case BP_BYTE:
            if (tab->bitsgn)
              {
               char	*bufdata = (char *)cbufdata0;
1222
#pragma ivdep
1223
1224
1225
1226
1227
1228
              for (i=spoonful; i--;)
                *(bufdata++) = (char)*(ptr++);
              }
            else
              {
               unsigned char	*bufdata = (unsigned char *)cbufdata0;
1229
#pragma ivdep
1230
1231
1232
1233
1234
1235
1236
1237
1238
              for (i=spoonful; i--;)
                *(bufdata++) = (unsigned char)*(ptr++);
              }
            break;

          case BP_SHORT:
            if (tab->bitsgn)
              {
               short	*bufdata = (short *)cbufdata0;
1239
#pragma ivdep
1240
1241
1242
1243
1244
1245
              for (i=spoonful; i--;)
                *(bufdata++) = (short)*(ptr++);
              }
            else
              {
               unsigned short	*bufdata = (unsigned short *)cbufdata0;
1246
#pragma ivdep
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
              for (i=spoonful; i--;)
                *(bufdata++) = (unsigned short)*(ptr++);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 2, spoonful);
            break;

          case BP_LONG:
           if (tab->bitsgn)
              {
               int	*bufdata = (int *)cbufdata0;
1258
#pragma ivdep
1259
1260
1261
1262
1263
1264
              for (i=spoonful; i--;)
                *(bufdata++) = (int)*(ptr++);
              }
            else
              {
               unsigned int	*bufdata = (unsigned int *)cbufdata0;
1265
#pragma ivdep
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
              for (i=spoonful; i--;)
                *(bufdata++) = (unsigned int)*(ptr++);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 4, spoonful);
            break;

#ifdef HAVE_LONG_LONG_INT
          case BP_LONGLONG:
           if (tab->bitsgn)
              {
1277
               SLONGLONG	*bufdata = (SLONGLONG *)cbufdata0;
1278
#pragma ivdep
1279
              for (i=spoonful; i--;)
1280
                *(bufdata++) = (SLONGLONG)*(ptr++);
1281
1282
1283
1284
              }
            else
              {
               ULONGLONG	*bufdata = (ULONGLONG *)cbufdata0;
1285
#pragma ivdep
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
              for (i=spoonful; i--;)
                *(bufdata++) = (ULONGLONG)*(ptr++);
              }
            if (bswapflag)
              swapbytes(cbufdata0, 8, spoonful);
            break;
#endif
          case BP_FLOAT:
            {
             float	*bufdata = (float *)cbufdata0;
1296
#pragma ivdep
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
            for (i=spoonful; i--;)
              *(bufdata++) = (float)((double)*(ptr++)-bz)/bs;
            if (bswapflag)
              swapbytes(cbufdata0, 4, spoonful);
            }
            break;

          case BP_DOUBLE:
            {
             double	*bufdata = (double *)cbufdata0;
1307
#pragma ivdep
1308
1309
1310
1311
1312
1313
1314
1315
            for (i=spoonful; i--;)
              *(bufdata++) = ((double)*(ptr++)-bz)/bs;
            if (bswapflag)
              swapbytes(cbufdata0, 8, spoonful);
            }
            break;

          default:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
            error(EXIT_FAILURE,"*FATAL ERROR*: unknown BITPIX type in ",
                                "read_body()");
            break;
          }
        QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
        }
      break;

/*-- Compressed image */
    case COMPRESS_BASEBYTE:
      break;

    case COMPRESS_PREVPIX:
      break;

    default:
      error(EXIT_FAILURE,"*Internal Error*: unknown compression mode in ",
                                "read_body()");
    }

  return;
  }


/******* set_maxram ***********************************************************
PROTO	int set_maxram(size_t maxram)
PURPOSE	Set the maximum amount of silicon memory that can be allocated for
	storing FITS body data.
INPUT	The maximum amount of RAM (in bytes).
OUTPUT	RETURN_OK if within limits, RETURN_ERROR otherwise.
NOTES	.
AUTHOR	E. Bertin (IAP)
VERSION	19/12/99
 ***/
int set_maxram(size_t maxram)
  {

  if (maxram<1)
    return RETURN_ERROR;

  body_maxram = maxram;

  return RETURN_OK;
  }


/******* set_maxvram **********************************************************
PROTO	int set_maxvram(size_t maxram)
PURPOSE	Set the maximum amount of total virtual memory that can be used for
	storing FITS body data.
INPUT	The maximum amount of VRAM (in bytes).
OUTPUT	RETURN_OK if within limits, RETURN_ERROR otherwise.
NOTES	.
AUTHOR	E. Bertin (IAP)
VERSION	08/02/2000
 ***/
int set_maxvram(size_t maxvram)
  {

  if (maxvram<1)
    return RETURN_ERROR;

  body_maxvram = maxvram;

  return RETURN_OK;
  }


/******* set_swapdir **********************************************************
PROTO	int set_swapdir(char *dirname)
PURPOSE	Set the path name of the directory that will be used for storing
	memory swap files.
INPUT	The pointer to the path string.
OUTPUT	RETURN_OK if path appropriate, RETURN_ERROR otherwise.
NOTES	.
AUTHOR	E. Bertin (IAP)
VERSION	19/12/99
 ***/
int set_swapdir(char *dirname)
  {

  if (!dirname)
    return RETURN_ERROR;

  strcpy(body_swapdirname, dirname);

  return RETURN_OK;
  }

For faster browsing, not all history is shown. View entire blame