00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026 #include <sys/time.h>
00027 #include <math.h>
00028 #include <string.h>
00029 #include "jsoc_main.h"
00030 #include "astro.h"
00031
00032 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00033 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00034
00035
00036 char *module_name = "update_disambig_b720s";
00037 char *version_id = "2014 Feb 14";
00038
00039 ModuleArgs_t module_args[] =
00040 {
00041 {ARG_STRING, "b", NULL, "Input/output B series"},
00042 {ARG_END}
00043 };
00044
00045 int DoIt(void)
00046 {
00047
00048 char *cvsinfo = strdup("$Id: update_disambig_b720s.c,v 1.1 2014/02/21 20:55:02 xudong Exp $");
00049
00050 int status = DRMS_SUCCESS;
00051
00052 char *bQuery = (char *) params_get_str(&cmdparams, "b");
00053
00054
00055
00056 DRMS_RecordSet_t *inRS = drms_open_records(drms_env, bQuery, &status);
00057 int nrecs = inRS->n;
00058 if (status || (nrecs == 0)) DIE("Error open data series");
00059
00060
00061
00062 DRMS_RecordSet_t *outRS = drms_clone_records(inRS, DRMS_PERMANENT, DRMS_COPY_SEGMENTS, &status);
00063 if (status) DIE("Error cloning data series");
00064
00065
00066
00067 drms_close_records(inRS, DRMS_FREE_RECORD);
00068
00069
00070
00071 for (int irec = 0; irec < nrecs; irec++) {
00072
00073
00074
00075
00076 DRMS_Record_t *outRec = outRS->records[irec];
00077
00078 TIME t_rec = drms_getkey_time(outRec, "T_REC", &status);
00079 char t_rec_str[100];
00080 sprint_time(t_rec_str, t_rec, "TAI", 0);
00081
00082 printf("Record #%d of %d, T_REC=%s...\n", irec + 1, nrecs, t_rec_str);
00083
00084
00085
00086 DRMS_Segment_t *confSeg = drms_segment_lookup(outRec, "conf_disambig");
00087 DRMS_Array_t *confArray = drms_segment_read(confSeg, DRMS_TYPE_CHAR, &status);
00088 if (status) {
00089 printf("Conf_disambig read error, skipping record #%d\n", irec + 1);
00090 continue;
00091 }
00092 char *conf = (char *) confArray->data;
00093
00094 DRMS_Segment_t *disambSeg = drms_segment_lookup(outRec, "disambig");
00095 DRMS_Array_t *disambArray = drms_segment_read(disambSeg, DRMS_TYPE_CHAR, &status);
00096 if (status) {
00097 printf("Disambig read error, skipping record #%d\n", irec + 1);
00098 drms_free_array(confArray);
00099 continue;
00100 }
00101 char *disamb = (char *) disambArray->data;
00102
00103
00104
00105 int nx = disambArray->axis[0], ny = disambArray->axis[1], nxny = nx * ny;
00106 char *disamb_new = (char *) (malloc(nxny * sizeof(char)));
00107
00108 for (int i = 0; i < nxny; i++) {
00109 disamb_new[i] = disamb[i];
00110 if (conf[i] > 85 || conf[i] < 55) continue;
00111 disamb_new[i] = (disamb[i] % 2) ? 7 : 0;
00112 }
00113
00114
00115
00116 drms_free_array(confArray);
00117 drms_free_array(disambArray);
00118
00119
00120
00121 int outDims[2] = {nx, ny};
00122
00123 DRMS_Segment_t *outSeg = disambSeg;
00124 DRMS_Array_t *outArray = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, disamb_new, &status);
00125 if (status) {
00126 printf("Error while creating disambig, skip record #%d\n", irec + 1);
00127 continue;
00128 }
00129 outArray->israw = 0;
00130 outArray->bzero = outSeg->bzero;
00131 outArray->bscale = outSeg->bscale;
00132
00133 status = drms_segment_write(outSeg, outArray, 0);
00134 if (status) {
00135 printf("Error while writing disambig, skip record #%d\n", irec + 1);
00136 drms_free_array(outArray);
00137 continue;
00138 }
00139
00140
00141
00142 char timebuf[512], histbuf[2014];
00143 double val;
00144 sprint_time(timebuf, (double)time(NULL) + UNIX_EPOCH, "ISO", 0);
00145 drms_setkey_string(outRec, "DATE", timebuf);
00146
00147 sprintf(histbuf, "disambig fixed with %s", cvsinfo);
00148 drms_setkey_string(outRec, "HISTORY", histbuf);
00149
00150
00151
00152 drms_free_array(outArray);
00153
00154 }
00155
00156
00157
00158 drms_close_records(outRS, DRMS_INSERT_RECORD);
00159
00160 return DRMS_SUCCESS;
00161
00162 }