00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <jsoc_main.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027
00028
00029
00030 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00031 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s (status=%d)\n", msg, status); return(status);}
00032
00033 #define FREE_ARR(arr) {if (arr) {free(arr);}}
00034 #define DRMS_FREE_ARR(arr) {if (arr) {drms_free_array(arr);}}
00035
00036
00037
00038
00039
00040 char *module_name = "cp_polarfield";
00041
00042 ModuleArgs_t module_args[] =
00043 {
00044 {ARG_STRING, "in", "", "Input query"},
00045 {ARG_STRING, "out", "hmi.meanpf_720s", "Output query"},
00046 {ARG_END}
00047 };
00048
00049 int DoIt(void)
00050 {
00051
00052 int status = DRMS_SUCCESS;
00053
00054
00055
00056
00057
00058 char *inQuery = (char *)params_get_str(&cmdparams, "in");
00059 char *outQuery = (char *)params_get_str(&cmdparams, "out");
00060
00061
00062
00063
00064
00065 DRMS_RecordSet_t *inRS = NULL;
00066 inRS = drms_open_records(drms_env, inQuery, &status);
00067 if (status || inRS->n == 0) DIE("Input records error.");
00068
00069 int nrecs = inRS->n;
00070
00071
00072
00073
00074
00075 DRMS_RecordSet_t *outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00076 if (status) DIE("Output records not created.");
00077
00078
00079
00080
00081
00082 printf("Start, %d records in total\n", nrecs);
00083
00084 for (int irec = 0; irec < nrecs; irec++) {
00085
00086
00087
00088 DRMS_Record_t *inRec = inRS->records[irec];
00089 TIME t_rec = drms_getkey_time(inRec, "T_REC", &status);
00090
00091 char t_rec_str[100];
00092 sprint_time(t_rec_str, t_rec, "TAI", 0);
00093 printf("Record #%d, [%s]\n", irec, t_rec_str);
00094
00095 DRMS_Segment_t *inSeg = drms_segment_lookupnum(inRec, 0);
00096 DRMS_Array_t *inArray = NULL;
00097 inArray = drms_segment_read(inSeg, DRMS_TYPE_DOUBLE, &status);
00098 if (status) {
00099 SHOW("Input array read error, record skipped.\n");
00100 if (inArray) drms_free_array(inArray);
00101 continue;
00102 }
00103
00104 int nx = inArray->axis[0], ny = inArray->axis[1];
00105 int outDims[1] = {nx};
00106
00107 double *inData = (double *) (inArray->data);
00108 double *mf_br = (double *) (malloc(nx * sizeof(double)));
00109 double *mf_bl = (double *) (malloc(nx * sizeof(double)));
00110 double *w = (double *) (malloc(nx * sizeof(double)));
00111 double *num = (double *) (malloc(nx * sizeof(double)));
00112 for (int i = 0; i < nx; i++) {
00113 mf_br[i] = inData[i];
00114 mf_bl[i] = inData[nx + i];
00115 w[i] = inData[nx * 2 + i];
00116 num[i] = inData[nx * 3 + i];
00117 }
00118
00119
00120
00121 DRMS_Record_t *outRec = outRS->records[irec];
00122
00123 status = drms_copykeys(outRec, inRec, 0, 0);
00124
00125 DRMS_Segment_t *outSeg_br = drms_segment_lookup(outRec, "mf_br");
00126 DRMS_Array_t *outArray_br = drms_array_create(DRMS_TYPE_DOUBLE, 1, outDims, mf_br, &status);
00127 status = drms_segment_write(outSeg_br, outArray_br, 0);
00128
00129 DRMS_Segment_t *outSeg_bl = drms_segment_lookup(outRec, "mf_bl");
00130 DRMS_Array_t *outArray_bl = drms_array_create(DRMS_TYPE_DOUBLE, 1, outDims, mf_bl, &status);
00131 status = drms_segment_write(outSeg_bl, outArray_bl, 0);
00132
00133 DRMS_Segment_t *outSeg_w = drms_segment_lookup(outRec, "w");
00134 DRMS_Array_t *outArray_w = drms_array_create(DRMS_TYPE_DOUBLE, 1, outDims, w, &status);
00135 status = drms_segment_write(outSeg_w, outArray_w, 0);
00136
00137 DRMS_Segment_t *outSeg_num = drms_segment_lookup(outRec, "num");
00138 DRMS_Array_t *outArray_num = drms_array_create(DRMS_TYPE_DOUBLE, 1, outDims, num, &status);
00139 status = drms_segment_write(outSeg_num, outArray_num, 0);
00140
00141
00142 DRMS_FREE_ARR(inArray);
00143 DRMS_FREE_ARR(outArray_br); DRMS_FREE_ARR(outArray_bl);
00144 DRMS_FREE_ARR(outArray_w); DRMS_FREE_ARR(outArray_num);
00145
00146 }
00147
00148
00149
00150 drms_close_records(inRS, DRMS_FREE_RECORD);
00151 drms_close_records(outRS, DRMS_INSERT_RECORD);
00152
00153 return DRMS_SUCCESS;
00154
00155 }