00001 #include "drms.h"
00002 #include "drms_priv.h"
00003 #include "cfitsio.h"
00004 #include "tasrw.h"
00005
00006 int drms_fitstas_create(DRMS_Env_t *env,
00007 const char *filename,
00008 const char *comp,
00009 DRMS_Type_t type,
00010 int naxis,
00011 int *axis,
00012 double bzero,
00013 double bscale)
00014 {
00015 int status = DRMS_SUCCESS;
00016 CFITSIO_IMAGE_INFO info;
00017 DRMS_Array_t arr;
00018 memset(&arr, 0, sizeof(DRMS_Array_t));
00019
00020 if (type != DRMS_TYPE_RAW)
00021 {
00022 arr.type = type;
00023 arr.naxis = naxis;
00024 memcpy(arr.axis, axis, sizeof(int) * naxis);
00025 arr.bzero = bzero;
00026 arr.bscale = bscale;
00027
00028
00029
00030 if (bzero == 0.0 && bscale == 1.0)
00031 {
00032 arr.israw = 0;
00033 }
00034 else
00035 {
00036 arr.israw = 1;
00037 }
00038
00039 if (!drms_fitsrw_SetImageInfo(&arr, &info))
00040 {
00041 if (fitsrw_writeintfile(env->verbose, filename, &info, NULL, comp, NULL) != CFITSIO_SUCCESS)
00042 {
00043 fprintf(stderr, "couldn't create FITS TAS file '%s'\n", filename);
00044 status = DRMS_ERROR_CANTCREATETASFILE;
00045 }
00046 else
00047 {
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 int fitsrwErr = CFITSIO_SUCCESS;
00065
00066 if (fitsrw_initializeTAS(env->verbose, filename) != CFITSIO_SUCCESS)
00067 {
00068 fprintf(stderr, "could not initialize FITS TAS file '%s'\n", filename);
00069 status = DRMS_ERROR_CANTCREATETASFILE;
00070 }
00071 }
00072 }
00073 else
00074 {
00075 fprintf(stderr, "couldn't set FITS TAS file image info\n");
00076 status = DRMS_ERROR_CANTCREATETASFILE;
00077 }
00078 }
00079 else
00080 {
00081 status = DRMS_ERROR_INVALIDTYPE;
00082 }
00083
00084 return status;
00085 }
00086
00087 int drms_fitstas_readslice(DRMS_Env_t *env,
00088 const char *filename,
00089 int naxis,
00090 int *axis,
00091 int *lower,
00092 int *upper,
00093 int slotnum,
00094 DRMS_Array_t **arr)
00095 {
00096 int status = DRMS_SUCCESS;
00097 int start[DRMS_MAXRANK+1];
00098 int end[DRMS_MAXRANK+1];
00099 int i;
00100
00101 if (lower && upper)
00102 {
00103 memcpy(start, lower, naxis * sizeof(int));
00104 memcpy(end, upper, naxis * sizeof(int));
00105 }
00106 else
00107 {
00108 for (i = 0; i < naxis; i++)
00109 {
00110 start[i] = 0;
00111 end[i] = axis[i] - 1;
00112 }
00113 }
00114
00115 start[naxis] = slotnum;
00116 end[naxis] = slotnum;
00117
00118 status = drms_fitsrw_readslice(env, filename, naxis + 1, start, end, arr);
00119
00120 if (status == DRMS_SUCCESS)
00121 {
00122
00123 ((*arr)->axis)[naxis] = 0;
00124 (*arr)->naxis--;
00125 }
00126
00127 return status;
00128 }
00129
00130
00131 int drms_fitstas_writeslice(DRMS_Env_t *env,
00132 DRMS_Segment_t *seg,
00133 const char *filename,
00134 int naxis,
00135 int *axis,
00136 int *lower,
00137 int *upper,
00138 int slotnum,
00139 DRMS_Array_t *arrayout)
00140 {
00141 int status = DRMS_SUCCESS;
00142 int start[DRMS_MAXRANK] = {0};
00143 int end[DRMS_MAXRANK] = {0};
00144 int iaxis;
00145
00146 if (lower && upper)
00147 {
00148 memcpy(start, lower, naxis * sizeof(int));
00149 memcpy(end, upper, naxis * sizeof(int));
00150 }
00151 else
00152 {
00153 for (iaxis = 0; iaxis < naxis; iaxis++)
00154 {
00155 end[iaxis] = start[iaxis] + axis[iaxis] - 1;
00156 }
00157 }
00158
00159 start[naxis] = slotnum;
00160 end[naxis] = slotnum;
00161
00162
00163
00164 status = drms_fitsrw_writeslice(env, seg, filename, naxis, start, end, arrayout);
00165
00166 if (status == DRMS_SUCCESS)
00167 {
00168
00169
00170
00171 char kw[DRMS_MAXKEYNAMELEN];
00172 snprintf(kw, sizeof(kw), "%s_bzero", seg->info->name);
00173 status = drms_setkey_double(seg->record, kw, arrayout->bzero);
00174
00175 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00176 {
00177 fprintf(stderr, "%s keyword not defined, cannot save bzero.\n", kw);
00178 }
00179 else
00180 {
00181 snprintf(kw, sizeof(kw), "%s_bscale", seg->info->name);
00182 status = drms_setkey_double(seg->record, kw, arrayout->bscale);
00183
00184 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00185 {
00186 fprintf(stderr, "%s keyword not defined, cannot save bscale.\n", kw);
00187 }
00188 }
00189 }
00190
00191 return status;
00192 }