00001
00061 #include "jsoc.h"
00062 #include "jsoc_main.h"
00063 #include "fstats.h"
00064
00065 char *module_name = "rebin2";
00066
00067 #define DIE(msg) {fflush(stdout);fprintf(stderr,"%s, status=%d\n",msg,status); return(status);}
00068
00069 ModuleArgs_t module_args[] =
00070 {
00071 {ARG_STRING, "in", "NOT SPECIFIED", "Input data series."},
00072 {ARG_STRING, "out", "NOT SPECIFIED", "Output data series."},
00073 {ARG_FLOAT, "scale", "NOTSPECIFIED", "Scale factor. negative for shrink factor"},
00074 {ARG_STRING, "mode", "simple", "conversion type."},
00075 {ARG_END}
00076 };
00077
00078 int DoIt(void)
00079 {
00080 int status = DRMS_SUCCESS;
00081 DRMS_RecordSet_t *inRS, *outRS;
00082 int irec, nrecs;
00083 const char *inQuery = params_get_str(&cmdparams, "in");
00084 const char *outSeries = params_get_str(&cmdparams, "out");
00085 const char *mode = params_get_str(&cmdparams, "mode");
00086 int scale = params_get_int(&cmdparams, "scale");
00087 int iscale, factor;
00088 float fscale;
00089 if (scale < 0.0)
00090 {
00091 iscale = -scale;
00092 fscale = 1.0/iscale;
00093 }
00094 else
00095 {
00096 iscale = (int) scale;
00097 fscale = scale;
00098 }
00099
00100 inRS = drms_open_records(drms_env, inQuery, &status);
00101 if (status || inRS->n == 0)
00102 DIE("No input data found");
00103 nrecs = inRS->n;
00104 if (nrecs == 0)
00105 DIE("No records found");
00106 outRS = drms_create_records(drms_env, nrecs, (char *)outSeries, DRMS_PERMANENT, &status);
00107 if (status)
00108 DIE("Output recordset not created");
00109
00110 for (irec=0; irec<nrecs; irec++)
00111 {
00112 DRMS_Record_t *outRec, *inRec;
00113 DRMS_Segment_t *outSeg, *inSeg;
00114 DRMS_Array_t *inArray, *outArray;
00115 float *inData, *outData;
00116 int inx, iny, outx, outy, i, j;
00117 float val;
00118 int quality=0;
00119
00120 inRec = inRS->records[irec];
00121 quality = drms_getkey_int(inRec, "QUALITY", &status);
00122 if (status || (!status && quality >= 0))
00123 {
00124 inSeg = drms_segment_lookupnum(inRec, 0);
00125 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00126 if (status)
00127 {
00128 printf(" No data file found but QUALITY not bad, status=%d\n", status);
00129 drms_free_array(inArray);
00130 continue;
00131 }
00132
00133 int naxis = inArray->naxis;
00134 int in_nx = inArray->axis[0];
00135 int in_ny = inArray->axis[1];
00136 int in_inc = (scale < 0 ? 1 : iscale);
00137 int out_inc = (scale < 0 ? iscale : 1);
00138 int out_nx = in_nx * fscale;
00139 int out_ny = in_ny * fscale;
00140
00141 int outDims[2] = {out_nx, out_ny};
00142 inData = (float *)inArray->data;
00143 outArray = drms_array_create(DRMS_TYPE_FLOAT, naxis, outDims, NULL, &status);
00144 outData = (float *)outArray->data;
00145
00146 if (strcmp(mode, "simple")==0)
00147 {
00148 if (scale < 0)
00149 {
00150 for (outy = 0; outy < out_ny; outy += 1)
00151 for (outx = 0; outx < out_nx; outx += 1)
00152 {
00153 double total = 0.0;
00154 int nn = 0;
00155 for (j = 0; j < in_inc; j += 1)
00156 for (i = 0; i < in_inc; i += 1)
00157 {
00158 val = inData[in_nx*(outy*iscale + j) + outx*iscale + i];
00159 if (!drms_ismissing_float(val))
00160 {
00161 total = total + val;
00162 nn++;
00163 }
00164 }
00165 outData[out_nx*outy + outx] = (nn > 0 ? total/nn : DRMS_MISSING_FLOAT);
00166 }
00167 }
00168 else
00169 {
00170 for (iny = 0; iny < in_ny; iny += 1)
00171 for (inx = 0; inx < in_nx; inx += 1)
00172 {
00173 val = inData[in_nx*iny + inx];
00174 for (j = 0; j < out_inc; j += 1)
00175 for (i = 0; i < out_inc; i += 1)
00176 outData[out_nx*(iny*iscale + j) + inx*iscale + i] = val;
00177 }
00178 }
00179 }
00180 else
00181 DIE("invalid conversion mode");
00182
00183 drms_free_array(inArray);
00184
00185
00186 outRec = outRS->records[irec];
00187 outSeg = drms_segment_lookupnum(outRec, 0);
00188
00189
00190 drms_copykeys(outRec, inRec, 0, kDRMS_KeyClass_Explicit);
00191 set_statistics(outSeg, outArray, 1);
00192
00193
00194
00195 drms_setkey_double(outRec, "CDELT1", drms_getkey_double(inRec, "CDELT1", NULL)/fscale);
00196 drms_setkey_double(outRec, "CDELT2", drms_getkey_double(inRec, "CDELT2", NULL)/fscale);
00197 drms_setkey_double(outRec, "CRPIX1", (drms_getkey_double(inRec, "CRPIX1", NULL)-0.5) * fscale + 0.5);
00198 drms_setkey_double(outRec, "CRPIX2", (drms_getkey_double(inRec, "CRPIX2", NULL)-0.5) * fscale + 0.5);
00199 drms_setkey_int(outRec, "SCALE", scale);
00200
00201
00202
00203 outArray->bzero = outSeg->bzero;
00204 outArray->bscale = outSeg->bscale;
00205 outArray->parent_segment = outSeg;
00206
00207 status = drms_segment_write(outSeg, outArray, 0);
00208 if (status)
00209 DIE("problem writing file");
00210 drms_free_array(outArray);
00211 }
00212 }
00213
00214 drms_close_records(inRS, DRMS_FREE_RECORD);
00215 drms_close_records(outRS, DRMS_INSERT_RECORD);
00216 return (DRMS_SUCCESS);
00217 }