int kml_document(FILE *fp) { CLASS *oclass, *openclass=NULL; MODULE *mod; char buffer[1024]; time_t now = time(NULL); kml_write("%s"," <Document>\n"); kml_write(" <name>%s</name>\n", global_modelname); kml_write(" <description>GridLAB-D results for %s</description>\n", convert_from_timestamp(global_clock,buffer,sizeof(buffer))?buffer:"unknown date/time"); /* for each module */ for (mod=module_get_first(); mod!=NULL; mod=mod->next) { if (mod->kmldump) mod->kmldump(kml_write,NULL); /* dump styles */ } /* scan each class in the model */ for (oclass=class_get_first_class(); oclass!=NULL; oclass=oclass->next) { OBJECT *obj; /* scan each object in the model */ for (obj=object_get_first(); obj!=NULL; obj=obj->next) { int has_location = !(isnan(obj->latitude) || isnan(obj->longitude)); MODULE *mod; /* class does not match current object */ if (obj->oclass!=oclass) continue; /* first instance of this class needs folder */ else if (openclass==NULL) { kml_write(" <Folder><name>Class %s</name>\n", oclass->name); kml_write(" <description>Module %s",oclass->module->name); if (oclass->module->minor!=0 || oclass->module->major!=0) kml_write(" (V%d.%02d)",oclass->module->major,oclass->module->minor); kml_write("</description>\n",oclass->module->name); openclass=oclass; } /* module overrides KML output */ mod = (MODULE*)(obj->oclass->module); if (mod->kmldump!=NULL) (*(mod->kmldump))(kml_write,obj); else if (has_location) { /* basic KML output of published variables */ PROPERTY *prop; kml_write(" <Placemark>\n"); if (obj->name) kml_write(" <name>%s</name>\n", obj->name); else kml_write(" <name>%s %d</name>\n", obj->oclass->name, obj->id); kml_write(" <description>\n"); kml_write(" <![CDATA[\n"); kml_write(" <TABLE><TR>\n"); for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass; prop=prop->next) { char *value = object_property_to_string(obj,prop->name, buffer, 1023); if (value!=NULL) kml_write("<TR><TH ALIGN=LEFT>%s</TH><TD ALIGN=RIGHT>%s</TD></TR>", prop->name, value); } kml_write(" </TR></TABLE>\n"); kml_write(" ]]>\n"); kml_write(" </description>\n"); kml_write(" <Point>\n"); kml_write(" <coordinates>%f,%f</coordinates>\n",obj->longitude,obj->latitude); kml_write(" </Point>\n"); kml_write(" </Placemark>\n"); } } /* close folder if any */ if (openclass!=NULL) { kml_write(" </Folder>\n"); openclass=NULL; } } kml_write(" </Document>\n"); return 0; }
void CGldEditorView::LoadObject(OBJECT *obj) { CListCtrl &list = GetListCtrl(); int nColumns = list.GetHeaderCtrl()?list.GetHeaderCtrl()->GetItemCount():0; for (int i=0; i<nColumns; i++) list.DeleteColumn(0); CRect wr; list.GetClientRect(&wr); int nCol=0; int nWid=0; #define W(X) (nWid+=X,X) int Name = list.InsertColumn(nCol++,"Name",LVCFMT_LEFT,W(150),nCol); int Type = list.InsertColumn(nCol++,"Type",LVCFMT_LEFT,W(100),nCol); int Class = list.InsertColumn(nCol++,"Class",LVCFMT_LEFT,W(150),nCol); int Access = list.InsertColumn(nCol++,"Access",LVCFMT_LEFT,W(100),nCol); int Data = list.InsertColumn(nCol++,"Data",LVCFMT_RIGHT,W(100),nCol); int Description = list.InsertColumn(nCol++,"Description",LVCFMT_LEFT,wr.Width()-nWid,nCol); #undef W int nItem; char buffer[1024]=""; CLASS *oclass; PROPERTY *prop; nItem = list.InsertItem(list.GetItemCount(),"clock"); list.SetItemText(nItem,Type,"TIMESTAMP"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PROTECTED"); list.SetItemText(nItem,Data,convert_from_timestamp(obj->clock,buffer,sizeof(buffer))?buffer:""); nItem = list.InsertItem(list.GetItemCount(),"name"); list.SetItemText(nItem,Type,"OBJECTNAME"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,object_name(obj)); nItem = list.InsertItem(list.GetItemCount(),"id"); list.SetItemText(nItem,Type,"OBJECTNUM"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"REFERENCE"); sprintf(buffer,"%d",obj->id); list.SetItemText(nItem,Data,buffer); nItem = list.InsertItem(list.GetItemCount(),"class"); list.SetItemText(nItem,Type,"CLASSNAME"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"REFERENCE"); list.SetItemText(nItem,Data,obj->oclass->name); nItem = list.InsertItem(list.GetItemCount(),"size"); list.SetItemText(nItem,Type,"OBJECTRANK"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"REFERENCE"); sprintf(buffer,"%d",obj->oclass->size); list.SetItemText(nItem,Data,buffer); nItem = list.InsertItem(list.GetItemCount(),"parent"); list.SetItemText(nItem,Type,"OBJECTNAME"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,object_name(obj->parent)); nItem = list.InsertItem(list.GetItemCount(),"rank"); list.SetItemText(nItem,Type,"OBJECTRANK"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); sprintf(buffer,"%d",obj->rank); list.SetItemText(nItem,Data,buffer); nItem = list.InsertItem(list.GetItemCount(),"in_svc"); list.SetItemText(nItem,Type,"TIMESTAMP"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,convert_from_timestamp(obj->in_svc,buffer,sizeof(buffer))?buffer:""); nItem = list.InsertItem(list.GetItemCount(),"out_svc"); list.SetItemText(nItem,Type,"TIMESTAMP"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,convert_from_timestamp(obj->out_svc,buffer,sizeof(buffer))?buffer:""); nItem = list.InsertItem(list.GetItemCount(),"latitude"); list.SetItemText(nItem,Type,"double"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,convert_from_latitude(obj->latitude,buffer,sizeof(buffer))?buffer:""); nItem = list.InsertItem(list.GetItemCount(),"longitude"); list.SetItemText(nItem,Type,"double"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PUBLIC"); list.SetItemText(nItem,Data,convert_from_latitude(obj->longitude,buffer,sizeof(buffer))?buffer:""); nItem = list.InsertItem(list.GetItemCount(),"flags"); list.SetItemText(nItem,Type,"set"); list.SetItemText(nItem,Class,"OBJECTHDR"); list.SetItemText(nItem,Access,"PROTECTED"); list.SetItemText(nItem,Data,convert_from_set(buffer,sizeof(buffer),&(obj->flags),object_flag_property())?buffer:""); for (oclass=obj->oclass; oclass!=NULL; oclass=oclass->parent) { list.InsertItem(list.GetItemCount(),""); for (prop=class_get_first_property(oclass); prop!=NULL; prop=class_get_next_property(prop)) { nItem = list.InsertItem(list.GetItemCount(),prop->name); list.SetItemText(nItem,Type,class_get_property_typename(prop->ptype)); list.SetItemText(nItem,Class,oclass->name); list.SetItemText(nItem,Access,convert_from_enumeration(buffer,sizeof(buffer),&(prop->access),object_access_property())?buffer:""); list.SetItemText(nItem,Data,object_get_value_by_name(obj,prop->name,buffer,sizeof(buffer))?buffer:"(error)"); list.SetItemText(nItem,Description,prop->description); } }; }
int module_saveobj_xml(FILE *fp, MODULE *mod){ /**< the stream to write to */ unsigned count = 0; char buffer[1024]; PROPERTY *prop = NULL; OBJECT *obj; CLASS *oclass=NULL; CLASS *pclass = NULL; for(obj = object_get_first(); obj != NULL; obj = obj->next){ char32 oname = "(unidentified)"; if(obj->oclass->module != mod){ continue; } if(obj->name != NULL){ strcpy(oname, obj->name); } else { sprintf(oname, "%s:%i", obj->oclass->name, obj->id); } if ((oclass == NULL) || (obj->oclass != oclass)) oclass = obj->oclass; count += fprintf(fp,"\t\t<object type=\"%s\" id=\"%i\" name=\"%s\">\n", obj->oclass->name, obj->id, oname); /* dump internal properties */ if (obj->parent!=NULL){ if(obj->parent->name != NULL){ strcpy(oname, obj->parent->name); } else { sprintf(oname, "%s:%i", obj->parent->oclass->name, obj->parent->id); } count += fprintf(fp,"\t\t\t<parent>%s</parent>\n", oname); } else { count += fprintf(fp,"\t\t\t<parent>root</parent>\n"); } count += fprintf(fp,"\t\t\t<rank>%d</rank>\n", obj->rank); count += fprintf(fp,"\t\t\t<clock>\n", obj->clock); count += fprintf(fp,"\t\t\t\t <timestamp>%s</timestamp>\n", convert_from_timestamp(obj->clock,buffer,sizeof(buffer))>0?buffer:"(invalid)"); count += fprintf(fp,"\t\t\t</clock>\n"); /* why do latitude/longitude have 2 values? I currently only store as float in the schema... -dc */ if (!isnan(obj->latitude)) count += fprintf(fp,"\t\t\t<latitude>%s</latitude>\n", convert_from_latitude(obj->latitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp, "\t\t\t<latitude>NONE</latitude>\n"); if (!isnan(obj->longitude)) count += fprintf(fp,"\t\t\t<longitude>%s</longitude>\n",convert_from_longitude(obj->longitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp,"\t\t\t<longitude>NONE</longitude>\n"); /* dump properties */ for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass;prop=prop->next) { char *value = NULL; if((prop->access != PA_PUBLIC) && (prop->access != PA_REFERENCE)) continue; value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = oclass->parent; while(pclass != NULL){ /* inherited properties */ for (prop=pclass->pmap;prop!=NULL && prop->oclass==pclass;prop=prop->next){ char *value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = pclass->parent; } count += fprintf(fp,"\t\t</object>\n"); } return count; }
int convert_from_timestamp_stub(char *buffer, int size, void *data, PROPERTY *prop){ TIMESTAMP ts = *(int64 *)data; return convert_from_timestamp(ts, buffer, size); //return 0; }
/** Test the daylight saving time calculations @return the number of test the failed **/ int timestamp_test(void) { #define NYEARS 50 int year; static DATETIME last_t; TIMESTAMP step = SECOND; TIMESTAMP ts; char buf1[64], buf2[64]; char steptxt[32]; TIMESTAMP *event[]={dststart,dstend}; int failed=0, succeeded=0; output_test("BEGIN: daylight saving time event test for TZ=%s...", current_tzname); convert_from_timestamp(step,steptxt,sizeof(steptxt)); for (year=0; year<NYEARS; year++) { int test; for (test=0; test<2; test++) { for (ts=(event[test])[year]-2*step; ts<(event[test])[year]+2*step;ts+=step) { DATETIME t; if (local_datetime(ts,&t)) { if (last_t.is_dst!=t.is_dst) output_test("%s + %s = %s", strdatetime(&last_t,buf1,sizeof(buf1))?buf1:"(invalid)", steptxt, strdatetime(&t,buf2,sizeof(buf2))?buf2:"(invalid)"); last_t = t; succeeded++; } else { output_test("FAILED: unable to convert ts=%"FMT_INT64"d to local time", ts); failed++; } } } } output_test("END: daylight saving time event test"); step=HOUR; convert_from_timestamp(step,steptxt,sizeof(steptxt)); output_test("BEGIN: round robin test at %s timesteps",steptxt); for (ts=DAY+tzoffset; ts<DAY*365*NYEARS; ts+=step) { DATETIME t; if (local_datetime(ts,&t)) { TIMESTAMP tt = mkdatetime(&t); convert_from_timestamp(ts,buf1,sizeof(buf1)); convert_from_timestamp(tt,buf2,sizeof(buf2)); if (tt==TS_INVALID) { output_test("FAILED: unable to extract %04d-%02d-%02d %02d:%02d:%02d %s (dow=%s, doy=%d)", t.year,t.month,t.day,t.hour,t.minute,t.second,t.tz,dow[t.weekday],t.yearday); failed++; } else if (tt!=ts) { output_test("FAILED: unable to match %04d-%02d-%02d %02d:%02d:%02d %s (dow=%s, doy=%d)\n from=%s, to=%s", t.year,t.month,t.day,t.hour,t.minute,t.second,t.tz,dow[t.weekday],t.yearday,buf1,buf2); failed++; } else if (convert_to_timestamp(buf1)!=ts) { output_test("FAILED: unable to convert %04d-%02d-%02d %02d:%02d:%02d %s (dow=%s, doy=%d) back to a timestamp\n from=%s, to=%s", t.year,t.month,t.day,t.hour,t.minute,t.second,t.tz,dow[t.weekday],t.yearday,buf1,buf2); output_test(" expected %" FMT_INT64 "d but got %" FMT_INT64 "d", ts, convert_to_timestamp(buf1)); failed++; } else succeeded++; } else { output_test("FAILED: timestamp_test: unable to convert ts=%"FMT_INT64"d to local time", ts); failed++; } } output_test("END: round robin test",steptxt); output_test("END: daylight saving time tests for %d to %d", YEAR0, YEAR0+NYEARS); output_verbose("daylight saving time tests: %d succeeded, %d failed (see '%s' for details)", succeeded, failed, global_testoutputfile); return failed; }
static mxArray *get_object_data(OBJECT *obj) { mxArray *plhs[1]; /* set the standard info */ #define ERROR "(error)" #define NONE "(none)" char *fnames[1024] = {"id","class","parent","rank","clock","latitude","longitude","in_svc","out_svc","flags",NULL}; // }; int nFields = 0; int nData = 0; char value[1024]; PROPERTY *prop; mxArray *pId = mxCreateString(convert_from_object(value,sizeof(value),&obj,NULL)?value:ERROR); mxArray *pClass = mxCreateString(obj->oclass->name); mxArray *pParent = mxCreateString(obj->parent!=NULL&&convert_from_object(value,sizeof(value),&(obj->parent),NULL)?value:NONE); mxArray *pRank = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL); mxArray *pClock = mxCreateString(convert_from_timestamp(obj->clock,value,sizeof(value))?value:ERROR); mxArray *pLatitude = mxCreateString(convert_from_latitude(obj->latitude,value,sizeof(value))?value:NONE); mxArray *pLongitude = mxCreateString(convert_from_longitude(obj->longitude,value,sizeof(value))?value:NONE); mxArray *pInSvc = mxCreateString(convert_from_timestamp(obj->in_svc,value,sizeof(value))?value:ERROR); mxArray *pOutSvc = mxCreateString(convert_from_timestamp(obj->out_svc,value,sizeof(value))?value:ERROR); mxArray *pFlags = mxCreateString(convert_from_set(value,sizeof(value),(void*)&obj->flags,object_flag_property())?value:ERROR); *(OBJECTRANK*)mxGetPr(pRank) = obj->rank; /* count the number of header items */ while (fnames[nFields]!=NULL) nFields++; /* count the number of object properties and assign the field names */ for (prop=class_get_first_property(obj->oclass); prop!=NULL; prop=class_get_next_property(prop)) /** @todo don't damage the original fieldname when making it safe for Matlab */ fnames[nFields+nData++] = make_fieldname(prop->name); /* construct the return value */ plhs[0] = mxCreateStructMatrix(1,1,nFields+nData,fnames); /* construct the header fields */ mxSetFieldByNumber(plhs[0],0,0,pId); mxSetFieldByNumber(plhs[0],0,1,pClass); mxSetFieldByNumber(plhs[0],0,2,pParent); mxSetFieldByNumber(plhs[0],0,3,pRank); mxSetFieldByNumber(plhs[0],0,4,pClock); mxSetFieldByNumber(plhs[0],0,5,pLatitude); mxSetFieldByNumber(plhs[0],0,6,pLongitude); mxSetFieldByNumber(plhs[0],0,7,pInSvc); mxSetFieldByNumber(plhs[0],0,8,pOutSvc); mxSetFieldByNumber(plhs[0],0,9,pFlags); /* construct the data fields */ for (prop=class_get_first_property(obj->oclass); prop!=NULL; nFields++,prop=class_get_next_property(prop)) { mxArray *pValue; if (prop->ptype==PT_double) { pValue = mxCreateDoubleMatrix(1,1,mxREAL); *(double*)mxGetPr(pValue) = *object_get_double(obj,prop); } else if (prop->ptype==PT_int32) { pValue = mxCreateDoubleMatrix(1,1,mxREAL); *(double*)mxGetPr(pValue) = (double)*object_get_int32(obj,prop); } else if (prop->ptype==PT_complex) { complex *pData = object_get_complex(obj,prop); pValue = mxCreateDoubleMatrix(1,1,mxCOMPLEX); *(double*)mxGetPr(pValue) = pData->r; *(double*)mxGetPi(pValue) = pData->i; } else { pValue = mxCreateString(object_get_value_by_name(obj,prop->name,value,sizeof(value))?value:ERROR); } mxSetFieldByNumber(plhs[0],0,nFields,pValue); } return plhs[0]; }
/** Get a value (object, clock, or global) \verbatim gl('get',name) \endverbatim **/ void cmex_get(int nlhs, mxArray *plhs[], /**< {data} */ int nrhs, const mxArray *prhs[] ) /**< (name) */ { if (nrhs>0) { char name[1024]; OBJECT *obj=NULL; if (!mxIsChar(prhs[0])) output_error("entity name (arg 1) is not a string"); else if (nlhs>1) output_error("only one return value is possible"); else if (mxGetString(prhs[0],name,sizeof(name))!=0) output_error("object name too long"); else if (strcmp(name,"clock")==0) { char *fnames[] = {"timestamp","timestring","timezone"}; char buffer[256]; mxArray *pTimestamp = mxCreateDoubleMatrix(1,1,mxREAL); mxArray *pTimestring = mxCreateString(convert_from_timestamp(global_clock,buffer,sizeof(buffer))?buffer:"(error)"); mxArray *pTimezone = mxCreateString(timestamp_current_timezone()); *(double*)mxGetPr(pTimestamp) = ((double)global_clock)/TS_SECOND; plhs[0] = mxCreateStructMatrix(1,1,sizeof(fnames)/sizeof(fnames[0]),fnames); mxSetFieldByNumber(plhs[0],0,0,pTimestamp); mxSetFieldByNumber(plhs[0],0,1,pTimestring); mxSetFieldByNumber(plhs[0],0,2,pTimezone); } else if (strcmp(name,"property")==0 && nrhs>1) { if (mxGetString(prhs[1],name,sizeof(name))!=0) output_error("missing property name"); else { char classname[256]; char propname[256]; if (sscanf(name,"%[^.].%s",classname,propname)==2) { CLASS *pClass = class_get_class_from_classname(classname); if (pClass) { PROPERTY *pProp = class_find_property(pClass,propname); if (pProp) { char *fields[] = {"class","name","type","size","access","unit","delegation","keywords"}; int fn = 0; mxArray *oclass = mxCreateString(classname); mxArray *prop = mxCreateString(pProp->name); mxArray *type = mxCreateString(class_get_property_typename(pProp->ptype)); mxArray *size = mxCreateDoubleMatrix(1,1,mxREAL); mxArray *access = mxCreateString("(na)"); /** @todo implement get_property access info (ticket #187) */ mxArray *unit = mxCreateString(pProp->unit->name); mxArray *delegation = mxCreateString(pProp->delegation?pProp->delegation->oclass->name:"(none)"); mxArray *keywords = mxCreateString("(na)"); /** @todo implement get_property keywords (ticket #188) */ *(mxGetPr(size)) = pProp->size==0?1:pProp->size; plhs[0] = mxCreateStructMatrix(1,1,sizeof(fields)/sizeof(fields[0]),fields); mxSetFieldByNumber(plhs[0],0,fn++,oclass); mxSetFieldByNumber(plhs[0],0,fn++,prop); mxSetFieldByNumber(plhs[0],0,fn++,type); mxSetFieldByNumber(plhs[0],0,fn++,size); mxSetFieldByNumber(plhs[0],0,fn++,access); mxSetFieldByNumber(plhs[0],0,fn++,unit); mxSetFieldByNumber(plhs[0],0,fn++,delegation); mxSetFieldByNumber(plhs[0],0,fn++,keywords); } else output_error("property %s is not found in class %s", propname,classname); } else output_error("class %s is not found"); } else output_error("property name not in class.name format"); } } else if ((convert_to_object(name,&obj,NULL))==0) { GLOBALVAR *var = global_find(name); if (var==NULL) output_error("entity '%s' not found", name); else if (var->prop->ptype==PT_double) { size_t size = var->prop->size?var->prop->size:1; plhs[0] = mxCreateDoubleMatrix(size,1,mxREAL); memcpy(mxGetPr(plhs[0]),(void*)var->prop->addr,sizeof(double)*size); } else if (var->prop->ptype==PT_int32) { size_t size = var->prop->size?var->prop->size:1; plhs[0] = mxCreateDoubleMatrix(size,1,mxREAL); memcpy(mxGetPr(plhs[0]),(void*)var->prop->addr,sizeof(double)*size); } else if (var->prop->ptype!=PT_double) output_error("cannot retrieve globals that are of type %s",class_get_property_typename(var->prop->ptype)); } else if ((plhs[0]=get_object_data(obj))==NULL) output_error("unable to extract %s data", name); } else output_error("object not specified"); return; }