static void PutArray(char dtype, int size, int n_dims, hsize_t *dims, void *ptr, struct descriptor_xd *xd) { /*************************************************** Store array of data into MDSplus descriptor. Use simple array descriptor for one dimensional arrays and a coefficient array descriptor for multi-dimensional arrays. ***************************************************/ if (n_dims == 1) { DESCRIPTOR_A(dsc,0,0,0,0); dsc.length = size; dsc.dtype = dtype; dsc.pointer = ptr; dsc.arsize = (int)dims[0] * size; MdsCopyDxXd(&dsc,xd); } else { int i; DESCRIPTOR_A_COEFF(dsc, 0,0,0,8,0); dsc.length = size; dsc.dtype = dtype; dsc.pointer = ptr; dsc.arsize = size; dsc.dimct = n_dims; dsc.a0 = ptr; for (i=0;i<n_dims;i++) { dsc.m[i] = (int)dims[i]; dsc.arsize *= (int)dims[i]; } MdsCopyDxXd(&dsc,xd); } }
/*---------------------------------------------- Internal routine to output a unit */ STATIC_ROUTINE int TdiPutUnit(FILE *unit, struct descriptor_xd *out_ptr) { struct descriptor unit_d = {0,DTYPE_T,CLASS_S,0}; unit_d.length = unit != 0 ? sizeof(unit) : 0; unit_d.pointer = unit != 0 ? (char *)&unit : 0; return MdsCopyDxXd(&unit_d, out_ptr); }
struct descriptor *XmdsExprGetXd(Widget w) { struct descriptor_xd *ans = (struct descriptor_xd *) XtMalloc(sizeof(struct descriptor_xd)); XmdsExprWidget ew = (XmdsExprWidget) w; char *text = GetString(ew->expr.text_widget); struct descriptor_s text_dsc = {0, DTYPE_T, CLASS_S, (char *) 0}; text_dsc.length = strlen(text); text_dsc.pointer = text; ResetErrors(); *ans = empty_xd; if (ew->expr.is_text) MdsCopyDxXd((struct descriptor *) & text_dsc,ans); else { int status; int old_def; int def_nid = GetDefaultNid(ew); if (def_nid != -1) { TreeGetDefaultNid(&old_def); TreeSetDefaultNid(def_nid); } status = (*ew->expr.compile) (&text_dsc,ans MDS_END_ARG); if ((status & 1) == 0) { TdiComplain(w); XtFree((char *)ans); ans = 0; } if (def_nid != -1) TreeSetDefaultNid(old_def); } return (struct descriptor *) ans; }
/*-------------------------------------------------------------- Make text into data type path. BUILD_PATH(string) BUILD_EVENT(string) WARNING: BUILD_PATH and BUILD_EVENT should be called MAKE_xxx. */ int Tdi1BuildPath(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; struct descriptor_xd sig[1], uni[1], dat[1]; struct TdiCatStruct cats[2]; int cmode = 0; status = TdiGetArgs(opcode, narg, list, sig, uni, dat, cats); cats[1].out_dtype = cats[1].in_dtype; cats[1].out_cat = cats[1].in_cat; if (status & 1) status = TdiCvtArgs(narg, dat, cats); /********************************* Change from text to path or event. *********************************/ if (status & 1) { dat[0].pointer->dtype = TdiRefFunction[opcode].o1; status = MdsCopyDxXd((struct descriptor *)&dat[0], out_ptr); } if (status & 1) status = TdiMasterData(narg, sig, uni, &cmode, out_ptr); if (sig[0].pointer) MdsFree1Dx(&sig[0], NULL); if (uni[0].pointer) MdsFree1Dx(&uni[0], NULL); if (dat[0].pointer) MdsFree1Dx(&dat[0], NULL); return status; }
/*-------------------------------------------------------------- Return without evaluation. Passes paths, nids, and functions. any = AS_IS(any) */ int Tdi1AsIs(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; status = MdsCopyDxXd(list[0], out_ptr); return status; }
static void PutScalar(char dtype, int size, void *ptr, struct descriptor_xd *xd) { /********************************************** Store scalar data in MDSplus descriptor **********************************************/ struct descriptor dsc = {0,0,CLASS_S,0}; dsc.length = size; dsc.dtype = dtype; dsc.pointer = ptr; MdsCopyDxXd(&dsc,xd); }
/*-------------------------------------------------------------- Evaluate arguments before building into the given data type. Use in returning calculations using local variables from a FUN. Some arguments may be "unevaluated" by using an AS_IS(arg). This can be used to pass back expressions evaluated in an outer FUN. So BUILD_xxx(x,y,...) is the same as MAKE_xxx(AS_IS(x),AS_IS(y),...). */ int Tdi1Make(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; struct descriptor *ptr; int j, k, modif; struct TdiFunctionStruct *fun_ptr = (struct TdiFunctionStruct *)&TdiRefFunction[opcode]; DESCRIPTOR_FUNCTION(build, 0, 255); struct descriptor_xd xd[255]; unsigned char modif_c; unsigned short modif_s; build.length = 0; build.dtype = fun_ptr->o1; build.ndesc = (unsigned char)narg; if (fun_ptr->o2 != fun_ptr->o1) { status = TdiGetLong(list[0], &modif); switch (fun_ptr->o2) { case DTYPE_BU: build.length = 1; modif_c = (unsigned char)modif; build.pointer = &modif_c; break; case DTYPE_WU: build.length = 2; modif_s = (unsigned short)modif; build.pointer = (unsigned char *)&modif_s; break; default: status = TdiINVDTYDSC; break; } --build.ndesc; j = 1; } else j = 0; for (k = 0; j < narg && status & 1; ++j, ++k) { xd[k] = EMPTY_XD; if (list[j]) status = TdiEvaluate(list[j], &xd[k] MDS_END_ARG); ptr = (struct descriptor *)&xd[k]; while (ptr && ptr->dtype == DTYPE_DSC) ptr = (struct descriptor *)ptr->pointer; if (ptr && ptr->dtype == DTYPE_MISSING) ptr = 0; build.arguments[k] = ptr; } if (status & 1) status = MdsCopyDxXd((struct descriptor *)&build, out_ptr); for (; --k >= 0;) MdsFree1Dx(&xd[k], NULL); return status; }
void XmdsExprSetXd(Widget w,struct descriptor *dsc) { XmdsExprWidget ew = (XmdsExprWidget) w; if (ew->expr.xd) MdsFree1Dx(ew->expr.xd, 0); else { ew->expr.xd = (struct descriptor_xd *) XtMalloc(sizeof(struct descriptor_xd)); *ew->expr.xd = empty_xd; } MdsCopyDxXd(dsc,ew->expr.xd); LoadExpr(ew,(struct descriptor *) ew->expr.xd); Resize((Widget) ew); }
EXPORT struct descriptor_xd *MdsEventGetMemState() { static EMPTYXD(xd); DESCRIPTOR_A(stateD, 1, DTYPE_B, 0, 0); char *state; int size; state = EventGetSerializedMemState(&size); stateD.pointer = state; stateD.arsize = size; MdsCopyDxXd((struct descriptor *)&stateD, &xd); free(state); return &xd; }
/*------------------------------------------------------------------*/ int Tdi1DecompileDependency(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; struct descriptor_d answer = { 0, DTYPE_T, CLASS_D, 0 }; struct descriptor *pdep = list[0]; while (pdep && pdep->dtype == DTYPE_DSC) pdep = (struct descriptor *)pdep->pointer; if (pdep) status = DependencyGet(P_WEAK, (struct descriptor_r *)pdep, &answer); if (status & 1) status = MdsCopyDxXd((struct descriptor *)&answer, out_ptr); StrFree1Dx(&answer); return status; }
int TreeFindTagWildDsc(char *wild, int *nidout, void **ctx_inout, struct descriptor_xd *name) { int status; char *ans = _TreeFindTagWild(DBID, wild, nidout, ctx_inout); if (ans) { static struct descriptor tag = {0, DTYPE_T, CLASS_S, 0}; tag.length = strlen(ans); tag.pointer = ans; MdsCopyDxXd(&tag,name); status = 1; } else status = 0; return status; }
/*-------------------------------------------------------------- Make a class-R record of pointers. Standard generic call: result = BUILD_xxxx(arg1,...) The first argument is evaluated for BUILD_CONDITION (byte modifier) BUILD_DEPENDENCY (byte op_code) BUILD_DISPATCH (byte type) BUILD_FUNCTION (word opcode) BUILD_CALL (byte dtype) and the remaining arguments are shifted by one. Each argument is NOT evaluated to get "real" data or to do further builds: BUILD_ACTION(BUILD_DISPATCH(...),BUILD_TASK(...)) Limitations: 254 arguments in some uses plus 1 for output. No type checking at build time. No arrays of descriptors. */ int Tdi1Build(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; struct descriptor *ptr; struct TdiFunctionStruct *fun_ptr = (struct TdiFunctionStruct *)&TdiRefFunction[opcode]; DESCRIPTOR_FUNCTION(build, 0, 255); int j, k, modif; unsigned short modif_s; unsigned char modif_c; build.length = 0; build.dtype = fun_ptr->o1; build.ndesc = (unsigned char)narg; if (fun_ptr->o2 != fun_ptr->o1) { status = TdiGetLong(list[0], &modif); switch (fun_ptr->o2) { case DTYPE_BU: build.length = 1; modif_c = (unsigned char)modif; build.pointer = &modif_c; break; case DTYPE_WU: build.length = 2; modif_s = (unsigned short)modif; build.pointer = (unsigned char *)&modif_s; break; default: status = TdiINVDTYDSC; break; } --build.ndesc; j = 1; } else j = 0; for (k = 0; j < narg; ++j, ++k) { ptr = list[j]; while (ptr && ptr->dtype == DTYPE_DSC) ptr = (struct descriptor *)ptr->pointer; build.arguments[k] = ptr; } if (status & 1) status = MdsCopyDxXd((struct descriptor *)&build, out_ptr); return status; }
/*-------------------------------------------------------------- Shot number identifier converted to string. */ int Tdi3Shotname( struct descriptor *in_ptr, struct descriptor_xd *out_ptr) { struct descriptor *dummy = in_ptr; int value; int retlen, status; struct dbi_itm lst[] = {{sizeof(value),DbiSHOTID,0,0},{0,DbiEND_OF_LIST,0}}; DESCRIPTOR(dmodel, "MODEL"); char string[15]; lst[0].pointer = (unsigned char *)&value; lst[0].return_length_address = &retlen; status = TreeGetDbi(lst); if (value != -1) { sprintf(string, "%ld", value); dmodel.pointer = string; dmodel.length = (unsigned short)strlen(string); } if (status & 1) status = MdsCopyDxXd((struct descriptor *)&dmodel, out_ptr); return status; }
int Tdi1GetDbi(int opcode, int narg, struct descriptor *list[], struct descriptor_xd *out_ptr) { int status = 1; struct descriptor_d string = { 0, DTYPE_T, CLASS_D, 0 }; struct descriptor_xd tmp = EMPTY_XD; struct item *key_ptr = 0; int index; DBI_ITM lst[] = { {sizeof(index), DbiINDEX, 0, 0} , EOL, EOL }; lst[0].pointer = (unsigned char *)&index; /********************** String of item to find. **********************/ status = TdiData(list[0], &tmp MDS_END_ARG); if (status & 1) status = TdiUpcase(&tmp, &string MDS_END_ARG); if (status & 1) { key_ptr = (struct item *)bsearch(&string, table, numtab, siztab, (int (*)(const void *, const void *)) compare); if (key_ptr == 0) status = TdiBAD_INDEX; } StrFree1Dx(&string); MdsFree1Dx(&tmp, NULL); /********************************** Somebody might want others in pool. **********************************/ if (status & 1 && narg > 1) status = TdiGetLong(list[1], &index); else index = 0; /*********************** Get the item asked for. Fixed length or varying. ***********************/ if (status & 1) { lst[1].code = key_ptr->item_code; if ((lst[1].buffer_length = key_ptr->item_length) != 0) { status = MdsGet1DxS((unsigned short *)&lst[1].buffer_length, &key_ptr->item_dtype, out_ptr); if (status & 1) { lst[1].pointer = (unsigned char *)out_ptr->pointer->pointer; status = TreeGetDbi(lst); } } else { lst[1].buffer_length = 0; lst[1].pointer = NULL; status = TreeGetDbi(lst); if (status & 1) { struct descriptor ans = { 0, DTYPE_T, CLASS_S, 0 }; if (lst[1].pointer) { ans.length = strlen((char *)lst[1].pointer); ans.pointer = (char *)lst[1].pointer; } status = MdsCopyDxXd(&ans, out_ptr); if (ans.pointer) TreeFree(ans.pointer); } } } return status; }
int TdiMasterData( int nsig, struct descriptor_xd sig[1], struct descriptor_xd uni[1], int *cmode_ptr, struct descriptor_xd *out_ptr) { struct descriptor *pdu = uni[0].pointer; int cmode = -1, j, status = 1; /****************** Kill units if none. ******************/ if (pdu && pdu->dtype == DTYPE_T && pdu->length == 1 && *pdu->pointer == ' ') pdu = 0; /************************************************* cmode = unsignaled, +n for signal *************************************************/ for (j = 0; j < nsig; j++) if (sig[j].pointer) { if (cmode >= 0) {cmode = -1; break;} if (*cmode_ptr == j || *cmode_ptr < 0) cmode = j; } /*************************************** If we have a signal, embed units or data. ***************************************/ if (cmode >= 0 && cmode < nsig) { struct descriptor_xd tmp; struct descriptor_signal *sig_ptr = (struct descriptor_signal *)sig[cmode].pointer; struct descriptor *keep_ptr = sig_ptr->data; struct descriptor *raw_ptr = sig_ptr->raw; DESCRIPTOR_WITH_UNITS(wu,0,0); tmp = emptyxd; sig_ptr->raw = 0; /************************************ If we have units, embed data in them. ************************************/ if (pdu) { wu.data = out_ptr->pointer; wu.units = pdu; sig_ptr->data = (struct descriptor *)&wu; } else sig_ptr->data = out_ptr->pointer; status = MdsCopyDxXd((struct descriptor *)sig_ptr, &tmp); sig_ptr->data = keep_ptr; sig_ptr->raw = raw_ptr; MdsFree1Dx(out_ptr, NULL); *out_ptr = tmp; } else if (pdu) { struct descriptor_xd tmp; DESCRIPTOR_WITH_UNITS(wu,0,0); tmp = emptyxd; wu.data = out_ptr->pointer; wu.units = pdu; MdsCopyDxXd((struct descriptor *)&wu, &tmp); MdsFree1Dx(out_ptr, NULL); *out_ptr = tmp; } *cmode_ptr = cmode; return status; }
struct descriptor_xd *MdsFilter(float *in_data, float *in_dim, int *size, float *cut_off, int *num_in_poles) { static struct descriptor_xd out_xd = {0, DTYPE_DSC, CLASS_XD, 0, 0}; DESCRIPTOR_A(data_d, sizeof(float), DTYPE_FLOAT, 0, 0); DESCRIPTOR_SIGNAL(signal_d, 1, 0, 0); DESCRIPTOR_DIMENSION(dimension_d, 0, 0); DESCRIPTOR_WINDOW(window_d, 0, 0, 0); DESCRIPTOR_RANGE(range_d, 0, 0, 0); struct descriptor start_d = {sizeof(float), DTYPE_FLOAT, CLASS_S, 0}, end_d = {sizeof(float), DTYPE_FLOAT, CLASS_S, 0}, delta_d = {sizeof(float), DTYPE_FLOAT, CLASS_S, 0}, start_idx_d = {sizeof(int), DTYPE_L, CLASS_S, 0}, end_idx_d = {sizeof(int), DTYPE_L, CLASS_S, 0}, time_at_0_d = {sizeof(float), DTYPE_FLOAT, CLASS_S, 0}; int num_samples, num_poles, start_idx, end_idx, i; float fc, delta, dummy, *filtered_data, start, end, time_at_0; float phs_steep, delay; float *mod, *phs; static Filter *filter; if(*num_in_poles > 0) num_poles = *num_in_poles; else num_poles = 10; signal_d.data = (struct descriptor *)&data_d; signal_d.dimensions[0] = (struct descriptor *)&dimension_d; dimension_d.window = (struct descriptor *)&window_d; dimension_d.axis = (struct descriptor *)&range_d; window_d.startidx = (struct descriptor *)&start_idx_d; window_d.endingidx = (struct descriptor *)&end_idx_d; window_d.value_at_idx0 = (struct descriptor *)&time_at_0_d; start_idx_d.pointer = (char*)&start_idx; end_idx_d.pointer = (char *)&end_idx; time_at_0_d.pointer = (char *)&time_at_0; range_d.begin = (struct descriptor *)&start_d; range_d.ending = (struct descriptor *)&end_d; range_d.deltaval = (struct descriptor *)&delta_d; start_d.pointer = (char *)&start; end_d.pointer = (char *)&end; delta_d.pointer = (char *)δ num_samples = *size; fc = 1/ (in_dim[1] - in_dim[0]); filter = ButtwInvar(cut_off, &dummy, &dummy, &dummy, &fc, &num_poles); filtered_data = (float *)malloc(num_samples * sizeof(float)); mod = (float *)malloc(sizeof(float) * 1000); phs = (float *)malloc(sizeof(float) * 1000); TestFilter(filter, fc, 1000, mod, phs); for(i = 1; i < 1000 - 1 && !isnan(phs[i]) && !isnan(phs[i+1]) && phs[i] > phs[i+1]; i++); if(i > 1 && i < 1000) { phs_steep = (phs[1] - phs[i])/((i/1000.) * fc/2.); delay = phs_steep/(2*PI); } free((char *)mod); free((char *)phs); DoFilter(filter, in_data, filtered_data, &num_samples); FreeFilter(filter); data_d.pointer = (char *)filtered_data; data_d.arsize = num_samples * sizeof(float); start = in_dim[0]-delay; end = in_dim[num_samples - 1]-delay; delta = in_dim[1] - in_dim[0]; start_idx = 0; end_idx = num_samples - 1; time_at_0 = in_dim[0] - delay; MdsCopyDxXd((struct descriptor *)&signal_d, &out_xd); free((char *)filtered_data); return &out_xd; }
static void StoreSignal(int nid, int num, float *v, TIME *t, struct descriptor_xd *limits, int sort) { DESCRIPTOR_A(vals, sizeof(float), DTYPE_NATIVE_FLOAT, 0, 0); DESCRIPTOR_A(tims, sizeof(TIME), DTYPE_QU, 0, 0); int status; static struct descriptor_xd tmp_xd = {0, DTYPE_DSC, CLASS_XD, 0, 0}; static struct descriptor_xd values = {0, DTYPE_DSC, CLASS_XD, 0, 0}; static struct descriptor_xd times = {0, DTYPE_DSC, CLASS_XD, 0, 0}; static DESCRIPTOR_SIGNAL_1(signal, &values, 0, ×); vals.pointer = (char *)v; vals.arsize = num * sizeof(float); tims.pointer = (char *)t; tims.arsize = num * sizeof(TIME); if (limits->l_length) { static struct descriptor_xd selections = {0,DTYPE_DSC,CLASS_XD,0,0}; static DESCRIPTOR(select, "(($1 > $2[0]) && ($1 < $2[1]))"); static DESCRIPTOR(pack, "PACK($, $)"); TdiExecute(&select, &vals, limits, &selections MDS_END_ARG); TdiExecute(&pack, &vals, &selections, &values MDS_END_ARG); TdiExecute(&pack, &tims, &selections, × MDS_END_ARG); MdsFree1Dx(&selections,0); } else { MdsCopyDxXd((struct descriptor *)&tims, ×); MdsCopyDxXd((struct descriptor *)&vals, &values); } status = TreeGetRecord(nid, &tmp_xd); if (status&1) { static struct descriptor_xd value = {0,DTYPE_DSC,CLASS_XD,0,0}; static struct descriptor_xd time = {0,DTYPE_DSC,CLASS_XD,0,0}; static DESCRIPTOR(sorted_times,"SORT($)"); static DESCRIPTOR(dim_of, "DIM_OF($)"); static DESCRIPTOR(val_of, "DATA($)"); static DESCRIPTOR(set_range, "SET_RANGE(SIZE($1), $1)"); TdiExecute(&dim_of, &tmp_xd, &time MDS_END_ARG); TdiExecute(&val_of, &tmp_xd, &value MDS_END_ARG); TdiVector(&value,&values,&values MDS_END_ARG); TdiVector(&time,×,× MDS_END_ARG); TdiExecute(&set_range, &values, &values MDS_END_ARG); TdiExecute(&set_range, ×, × MDS_END_ARG); if (sort) { static struct descriptor_xd index = {0,DTYPE_DSC,CLASS_XD,0,0}; static struct descriptor_xd sorted_values = {0,DTYPE_DSC,CLASS_XD,0,0}; static struct descriptor_xd sorted_times = {0,DTYPE_DSC,CLASS_XD,0,0}; static struct descriptor_xd sorted_signal = {0,DTYPE_DSC,CLASS_XD,0,0}; static DESCRIPTOR_SIGNAL_1(signal, &sorted_values, 0, &sorted_times); TdiSort(times.pointer,&index MDS_END_ARG); TdiSubscript(times.pointer,index.pointer,&sorted_times MDS_END_ARG); TdiSubscript(values.pointer,index.pointer,&sorted_values MDS_END_ARG); TdiUnion(sorted_times.pointer,&time MDS_END_ARG); TdiSubscript(&signal,time.pointer,&sorted_signal MDS_END_ARG); status = TreePutRecord(nid, (struct descriptor *)&sorted_signal,0); MdsFree1Dx(&index,0); MdsFree1Dx(&sorted_values,0); MdsFree1Dx(&sorted_times,0); } else status = TreePutRecord(nid, (struct descriptor *)&signal,0); MdsFree1Dx(&value,0); MdsFree1Dx(&time,0); } else { signal.data = (struct descriptor *)&vals; signal.dimensions[0] = (struct descriptor *)&tims; status = TreePutRecord(nid, (struct descriptor *)&signal,0); } MdsFree1Dx(&tmp_xd,0); }