00001
00002
00003 #include "jsoc_main.h"
00004 #include "drms_types.h"
00005
00006 char *module_name = "opendsrecs";
00007
00008 #define kRecSetIn "recsin"
00009 #define kOutSeries "su_arta.TestDSDS"
00010
00011 ModuleArgs_t module_args[] =
00012 {
00013 {ARG_STRING, kRecSetIn, "", "Input data series."},
00014 {ARG_END}
00015 };
00016
00017 int DoIt(void)
00018 {
00019 int status = DRMS_SUCCESS;
00020 int error = 0;
00021 char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00022 DRMS_RecordSet_t *inRecSet = drms_open_records(drms_env, inRecQuery, &status);
00023
00024 if (status == DRMS_SUCCESS)
00025 {
00026 int nRecs = inRecSet->n;
00027 int iRec;
00028
00029
00030 DRMS_Record_t *sourceRec = NULL;
00031 DRMS_Record_t *recout = NULL;
00032 DRMS_Record_t *prototype = NULL;
00033 DRMS_Segment_t *segout = NULL;
00034 DRMS_Segment_t *segin = NULL;
00035 DRMS_Segment_t *segproto = NULL;
00036 HContainer_t *matchSegNames = NULL;
00037
00038 for (iRec = 0; !error && iRec < nRecs; iRec++)
00039 {
00040 DRMS_Record_t *rec = inRecSet->records[iRec];
00041 if (rec)
00042 {
00043 if (iRec == 0)
00044 {
00045 if (drms_series_exists(drms_env, kOutSeries, &status))
00046 {
00047 prototype = drms_create_recproto(rec, &status);
00048 segproto = drms_segment_lookup(prototype, kDSDS_Segment);
00049
00050 if (segproto && segproto->info->protocol == DRMS_DSDS)
00051 {
00052 segproto->info->protocol = DRMS_FITS;
00053 }
00054
00055 matchSegNames = (HContainer_t *)malloc(sizeof(HContainer_t));
00056 XASSERT(matchSegNames != NULL);
00057 int compat = drms_series_checkrecordcompat(drms_env,
00058 kOutSeries,
00059 prototype,
00060 matchSegNames,
00061 &status);
00062 hcon_destroy(&matchSegNames);
00063 drms_destroy_recproto(&prototype);
00064 prototype = NULL;
00065
00066 if (!compat)
00067 {
00068 fprintf(stderr,
00069 "Output series %s is not compatible with output data.\n",
00070 kOutSeries);
00071 status = DRMS_ERROR_INVALIDDATA;
00072 }
00073 }
00074 else
00075 {
00076
00077 sourceRec = drms_template_record(drms_env, rec->seriesinfo->seriesname, &status);
00078 if (sourceRec)
00079 {
00080 prototype = drms_create_recproto(sourceRec, &status);
00081 segproto = drms_segment_lookup(prototype, kDSDS_Segment);
00082
00083 if (segproto && segproto->info->protocol == DRMS_DSDS)
00084 {
00085 segproto->info->protocol = DRMS_FITS;
00086 status = drms_create_series_fromprototype(&prototype, kOutSeries, 0);
00087 error = (status != DRMS_SUCCESS);
00088 }
00089 else
00090 {
00091 drms_destroy_recproto(&prototype);
00092 error = 1;
00093 }
00094 }
00095 }
00096 }
00097 else
00098 {
00099
00100 prototype = drms_create_recproto(rec, &status);
00101 segproto = drms_segment_lookup(prototype, kDSDS_Segment);
00102
00103 if (segproto && segproto->info->protocol == DRMS_DSDS)
00104 {
00105 segproto->info->protocol = DRMS_FITS;
00106 }
00107
00108 matchSegNames = (HContainer_t *)malloc(sizeof(HContainer_t));
00109 XASSERT(matchSegNames != NULL);
00110 int compat = drms_series_checkrecordcompat(drms_env,
00111 kOutSeries,
00112 prototype,
00113 matchSegNames,
00114 &status);
00115 hcon_destroy(&matchSegNames);
00116 drms_destroy_recproto(&prototype);
00117 prototype = NULL;
00118
00119 if (!compat)
00120 {
00121 fprintf(stderr,
00122 "Output series %s is not compatible with output data.\n",
00123 kOutSeries);
00124 status = DRMS_ERROR_INVALIDDATA;
00125 }
00126
00127 error = (status != DRMS_SUCCESS);
00128 }
00129
00130 if (!error)
00131 {
00132 segin = drms_segment_lookup(rec, kDSDS_Segment);
00133 if (segin)
00134 {
00135 DRMS_Array_t *arr = drms_segment_read(segin, segin->info->type, &status);
00136 if (arr)
00137 {
00138 int iData;
00139 void *data = arr->data;
00140
00141
00142 DRMS_Type_Value_t intval;
00143 DRMS_Value_t val;
00144 DRMS_Value_t opval;
00145 DRMS_Value_t resval;
00146 long long nElem = drms_array_count(arr);
00147
00148
00149 intval.int_val = 100;
00150 drms_convert(arr->type, &(opval.value), DRMS_TYPE_INT, &intval);
00151 opval.type = arr->type;
00152
00153
00154
00155
00156 for (iData = 0; iData < nElem; iData++)
00157 {
00158 DRMS_ARRAY_GETVAL(val, arr, iData);
00159 resval = drms_val_add(&val, &opval);
00160 DRMS_ARRAY_SETVAL(resval, arr, iData);
00161 }
00162
00163 DRMS_RecordSet_t *rs = drms_create_records(drms_env,
00164 1,
00165 kOutSeries,
00166 DRMS_PERMANENT,
00167 &status);
00168
00169 error = (status != DRMS_SUCCESS);
00170
00171 if (!error)
00172 {
00173 recout = rs->records[0];
00174 }
00175
00176 if (recout)
00177 {
00178 DRMS_Keyword_t *key = NULL;
00179 segout = drms_segment_lookup(recout, kDSDS_Segment);
00180 if (segout)
00181 {
00182 HIterator_t *hit = hiter_create(&(rec->keywords));
00183 if (hit)
00184 {
00185 while ((key = hiter_getnext(hit)) != NULL)
00186 {
00187 status = drms_setkey(recout,
00188 key->info->name,
00189 key->info->type,
00190 &(key->value));
00191 }
00192
00193 hiter_destroy(&hit);
00194 }
00195
00196 drms_segment_write(segout, arr, 0);
00197 }
00198
00199 drms_close_records(rs, DRMS_INSERT_RECORD);
00200 }
00201
00202 drms_free_array(arr);
00203 }
00204 }
00205 else
00206 {
00207 error = 1;
00208 }
00209 }
00210 }
00211 }
00212
00213 if (inRecSet)
00214 {
00215 drms_close_records(inRecSet, DRMS_FREE_RECORD);
00216 }
00217 }
00218
00219 return error;
00220 }