Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add json2glm converter options to ignore the clock #1233

Merged
merged 22 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions converters/json2glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ def help():
-i|--ifile <filename> [REQUIRED] JSON input file
-o|--ofile <filename> [OPTIONAL] GLM output file name
-t|--type type of input file
-n|--noclock omit clock data
-g|--ignoreglobals LIST omit globals
"""

def main():
filename_json = ''
filename_glm = ''
json_type = ''
clockflag = False
ignored_globals_list = ''
try :
opts, args = getopt.getopt(sys.argv[1:],"chi:o:t:",["config","help","ifile=","ofile=","type="])
opts, args = getopt.getopt(sys.argv[1:],"chni:o:t:g:",["config","help","noclock","ifile=","ofile=","type=","ignoreglobals="])
except getopt.GetoptError:
sys.exit(2)
if not opts :
Expand All @@ -38,18 +42,27 @@ def main():
filename_glm = arg
elif opt in ("-t", "--type"):
json_type = arg
elif opt in ("-n", "--noclock"):
clockflag = True
elif opt in ("-g", "--ignoreglobals"):
ignored_globals_list = arg
else :
error(f"{opt}={arg} is not a valid option")


convert(ifile=filename_json,ofile=filename_glm,json_type=json_type)
convert(ifile=filename_json,ofile=filename_glm,json_type=json_type,noclock=clockflag,ignore_globals=ignored_globals_list)

def convert(ifile,ofile,json_type) :
def convert(ifile,ofile,json_type="gridlabd",noclock=False,ignore_globals=None) :
if os.path.exists(ofile):
os.remove(ofile)
data = {}
objects_ignore = ["id", "class", "rank", "clock", "flags"]
globals_ignore = ['clock', 'timezone_locale', 'starttime', 'stoptime','glm_save_options'] # REMOVE glm_save_options when bug is fixed
if noclock :
globals_ignore = ['clock', 'timezone_locale', 'starttime', 'stoptime','glm_save_options'] # REMOVE glm_save_options when bug is fixed
else:
globals_ignore = []
if ignore_globals :
globals_ignore.extend(ignore_globals.split(','))
classkeys_ignore = ['object_size', 'trl', 'profiler.numobjs', 'profiler.clocks', 'profiler.count', 'parent']
house_variables = ['heating_system_type', 'gross_wall_area', 'floor_area', 'envelope_UA']

Expand Down Expand Up @@ -81,19 +94,19 @@ def convert(ifile,ofile,json_type) :
fw.write("\n }")

else :

# clock
header_str = '\n' + 'clock {'
tmzone_str = '\n' + '\t' + 'timezone ' + data['globals']['timezone_locale']['value']+';'
start_str = '\n' + '\t' + 'starttime'+ ' \"' + data['globals']['starttime']['value']+'\";'
stop_str = '\n' + '\t' + 'stoptime'+' \"'+ data['globals']['stoptime']['value']+'\";'
end_str = '\n' + '}'
fw.write('\n\n// CLOCK')
fw.write(header_str)
fw.write(tmzone_str)
fw.write(start_str)
fw.write(stop_str)
fw.write(end_str)
if not noclock :
# clock
header_str = '\n' + 'clock {'
tmzone_str = '\n' + '\t' + 'timezone ' + data['globals']['timezone_locale']['value']+';'
start_str = '\n' + '\t' + 'starttime'+ ' \"' + data['globals']['starttime']['value']+'\";'
stop_str = '\n' + '\t' + 'stoptime'+' \"'+ data['globals']['stoptime']['value']+'\";'
end_str = '\n' + '}'
fw.write('\n\n// CLOCK')
fw.write(header_str)
fw.write(tmzone_str)
fw.write(start_str)
fw.write(stop_str)
fw.write(end_str)

# modules
fw.write('\n\n// MODULES')
Expand All @@ -111,7 +124,7 @@ def convert(ifile,ofile,json_type) :
# globals
fw.write('\n\n// GLOBALS')
for p_id, p_info in data['globals'].items() :
if p_info['access'] == "PUBLIC" and p_info['value'] and 'infourl' not in p_id and "::" not in p_id:
if p_info['access'] == "PUBLIC" and p_info['value'] and 'infourl' not in p_id and "::" not in p_id and p_id not in globals_ignore:
ifndef_str = '\n' + '#ifndef ' + p_id
if 'int' in p_info['type'] or 'double' in p_info['type'] or 'bool' in p_info['type'] or \
'enumeration' in p_info['type'] or p_info['value']=='NONE' or 'set' in p_info['type'] \
Expand Down
21 changes: 19 additions & 2 deletions docs/Command/JSON input.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
# Synopsis

~~~
bash$ gridlabd filename.json
bash$ gridlabd filename.json [options]
~~~
Usage within GLM
~~~
#python -i filename.json -o filename.glm [options]
~~~


# Description

The loader does not directly support JSON input at this time. Instead an runtime converter is used, which executes the following command automatically when needed:

~~~
bash$ python3 gridlabd_src/python/jsonglm/json2glm.py -i input.json -o output.glm
bash$ python3 gridlabd_src/python/jsonglm/json2glm.py -i input.json -o output.glm [options]
~~~

The converter is automatically called when a `.json` file is specified on the input, e.g.,
Expand All @@ -28,6 +33,18 @@ results in internal execution of command

before `filename.glm` is loaded.

Additional options are available for conversion control:

No clock - produces a GLM file without a clock statement
~~~
#python ${GLD_ETC}/json2glm.py -i filename.json -o filename.glm -n
~~~
No globals - produces a GLM file without user-specified globals
~~~
#python ${GLD_ETC}/json2glm.py -i filename.json -o filename.glm -g relax_naming_rules,ignore_errors
~~~


# Caveat

The converter generates a GLM file with the same basename. Be careful not to accidentially overwrite an existing GLM file implicitly when using this option. In particularly, the following can be result in very unexpected behavior:
Expand Down
2 changes: 1 addition & 1 deletion module/powerflow/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ TIMESTAMP load::sync(TIMESTAMP t0)
else if (NR_node_reference == -99) //Child check us, since that can break things too
{
//See if our parent was initialized
if (*NR_subnode_reference == -1)
if ( NR_subnode_reference && *NR_subnode_reference == -1)
{
//Try to initialize it, for giggles
node *Temp_Node = OBJECTDATA(SubNodeParent,node);
Expand Down
6 changes: 4 additions & 2 deletions module/powerflow/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ int node::init(OBJECT *parent)
{ //Essentially a replication of the no-phase section with more check
if ((parNode->SubNode==CHILD) | (parNode->SubNode==DIFF_CHILD) | ((obj->parent->parent!=NR_swing_bus) && (obj->parent->parent!=NULL))) //Our parent is another child
{
GL_THROW("NR: Grandchildren are not supported at this time!");
obj->parent = obj->parent->parent;
warning("grandchildren objects are not supported, changing parent to %s:%d (%s) -- this might not be what you want",obj->parent->oclass->name,obj->parent->id,obj->parent->name ? obj->parent->name : "unnamed");
/* TROUBLESHOOT
Parent-child connections in Newton-Raphson may not go more than one level deep. Grandchildren
(a node parented to a node parented to a node) are unsupported at this time. Please rearrange your
Expand Down Expand Up @@ -862,7 +863,8 @@ int node::init(OBJECT *parent)
{
if ((parNode->SubNode==CHILD) | (parNode->SubNode==DIFF_CHILD) | (obj->parent->parent!=NULL)) //Our parent is another child
{
GL_THROW("NR: Grandchildren are not supported at this time!");
warning("grandchildren objects are not supported, changing parent to %s:%d (%s) -- this might not be what you want",obj->parent->oclass->name,obj->parent->id,obj->parent->name ? obj->parent->name : "unnamed");
obj->parent = obj->parent->parent;
//Defined above
}
else //Our parent is unchilded (or has the swing bus as a parent)
Expand Down
4 changes: 4 additions & 0 deletions source/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,10 @@ DEPRECATED const char *global_filetype(char *buffer, int size, const char *spec)
DEPRECATED const char *global_findobj(char *buffer, int size, const char *spec)
{
int len = 0;
if ( size > 0 && buffer != NULL )
{
buffer[0] = '\0';
}
FINDLIST *finder = findlist_create(FL_NEW, spec);
for ( OBJECT *obj = find_first(finder) ; obj != NULL ; obj = find_next(finder,obj) )
{
Expand Down
3 changes: 2 additions & 1 deletion source/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ int GldJsonWriter::write(const char *fmt,...)

#define FIRST(N,F,V) (len += write("\n\t\t\t\"%s\" : \"" F "\"",N,V))
#define TUPLE(N,F,V) (len += write(",\n\t\t\t\"%s\" : \"" F "\"",N,V))
#define TUPLE2(N,F,V,W) (len += write(",\n\t\t\t\"%s\" : \"" F "\"",N,V,W))

int GldJsonWriter::write_modules(FILE *fp)
{
Expand Down Expand Up @@ -467,7 +468,7 @@ int GldJsonWriter::write_objects(FILE *fp)
if ( obj->parent != NULL )
{
if ( obj->parent->name == NULL )
len += write(",\n\t\t\t\"parent\" : \"%s:%d\"",obj->parent->oclass->name,obj->parent->id);
TUPLE2("parent","%s:%d",obj->parent->oclass->name,obj->parent->id);
else
TUPLE("parent","%s",obj->parent->name);
}
Expand Down
61 changes: 34 additions & 27 deletions source/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ char *GldLoader::strip_right_white(char *b)

std::string GldLoader::forward_slashes(const char *a)
{
char buffer[MAXPATHNAMELEN];
char buffer[MAXPATHNAMELEN] = "";
char *b=buffer;
while ( *a != '\0' && b < buffer+sizeof(buffer)-1 )
{
Expand All @@ -173,7 +173,7 @@ std::string GldLoader::forward_slashes(const char *a)
void GldLoader::filename_parts(const char *fullname, char *path, char *name, char *ext)
{
/* fix delimiters (result is a static copy) */
char file[MAXPATHNAMELEN];
char file[MAXPATHNAMELEN] = "";
strcpy(file,forward_slashes(fullname).c_str());

/* find the last delimiter */
Expand Down Expand Up @@ -290,7 +290,7 @@ int GldLoader::append_global(const char* format,...)

void GldLoader::mark_linex(const char *filename, int linenum)
{
char buffer[64];
char buffer[64] = "";
if (global_getvar("noglmrefs",buffer, 63)==NULL)
{
char fname[MAXPATHNAMELEN];
Expand All @@ -306,7 +306,7 @@ void GldLoader::mark_line(void)

STATUS GldLoader::exec(const char *format,...)
{
char cmd[1024];
char cmd[1024] = "";
va_list ptr;
va_start(ptr,format);
vsnprintf(cmd,sizeof(cmd)-1,format,ptr);
Expand Down Expand Up @@ -361,8 +361,8 @@ std::string GldLoader::setup_class(CLASS *oclass)

int GldLoader::write_file(FILE *fp, const char *data, ...)
{
char buffer[65536];
char var_buf[64];
char buffer[65536] = "";
char var_buf[64] = "";
char *c, *d=buffer;
int len=0;
int diff = 0;
Expand Down Expand Up @@ -466,8 +466,8 @@ int GldLoader::mkdirs(const char *path)

STATUS GldLoader::compile_code(CLASS *oclass, int64 functions)
{
char include_file_str[1024];
char buffer[256];
char include_file_str[1024] = "";
char buffer[256] = "";
bool use_msvc = (global_getvar("use_msvc",buffer,255)!=NULL);

include_file_str[0] = '\0';
Expand Down Expand Up @@ -2981,7 +2981,7 @@ int GldLoader::property_type(PARSER, PROPERTYTYPE *ptype, KEYWORD **keys)

int GldLoader::class_intrinsic_function_name(PARSER, CLASS *oclass, int64 *function, const char **ftype, const char **fname)
{
char buffer[1024];
char buffer[1024] = "";
START;
if WHITE ACCEPT;
if LITERAL("create")
Expand Down Expand Up @@ -3109,7 +3109,7 @@ int GldLoader::source_code(PARSER, char *code, int size)
{
int _n = 0;
int nest = 0;
char buffer[64];
char buffer[64] = "";
enum {CODE,COMMENTBLOCK,COMMENTLINE,STRING,CHAR} state=CODE;
while (*_p!='\0')
{
Expand Down Expand Up @@ -3244,7 +3244,7 @@ int GldLoader::class_intrinsic_function(PARSER, CLASS *oclass, int64 *functions,
int GldLoader::class_export_function(PARSER, CLASS *oclass, char *fname, int fsize, char *arglist, int asize, char *code, int csize)
{
int startline;
char buffer[64];
char buffer[64] = "";
START;
if WHITE ACCEPT;
if (LITERAL("export")
Expand Down Expand Up @@ -3500,10 +3500,10 @@ int GldLoader::class_parent_definition(PARSER, CLASS *oclass)

int GldLoader::class_properties(PARSER, CLASS *oclass, int64 *functions, char *initcode, int initsize)
{
static char code[65536];
char arglist[1024];
char fname[64];
char buffer[64];
static char code[65536] = "";
char arglist[1024] = "";
char fname[64] = "";
char buffer[64] = "";
CLASS *eclass;
PROPERTYTYPE type;
char propname[64];
Expand Down Expand Up @@ -5304,7 +5304,7 @@ int GldLoader::schedule(PARSER)
if WHITE ACCEPT;
if (LITERAL("schedule") && WHITE && TERM(dashed_name(HERE,schedname,sizeof(schedname))) && (WHITE,LITERAL("{")))
{
char buffer[65536], *p=buffer;
char buffer[65536] = "", *p=buffer;
int nest=0;
for (nest=0; nest>=0; _m++)
{
Expand Down Expand Up @@ -5449,11 +5449,11 @@ int GldLoader::gnuplot(PARSER, GUIENTITY *entity)

int GldLoader::gui_entity_parameter(PARSER, GUIENTITY *entity)
{
char buffer[1024];
char varname[64];
char modname[64];
char objname[64];
char propname[64];
char buffer[1024] = "";
char varname[64] = "";
char modname[64] = "";
char objname[64] = "";
char propname[64] = "";
START;
if WHITE ACCEPT;
if LITERAL("global")
Expand Down Expand Up @@ -5719,7 +5719,6 @@ int GldLoader::gui_entity_type(PARSER, GUIENTITYTYPE *type)

int GldLoader::gui_entity(PARSER, GUIENTITY *parent)
{
//char buffer[1024];
GUIENTITYTYPE type;
START;
if WHITE ACCEPT;
Expand Down Expand Up @@ -7242,6 +7241,10 @@ bool found_in_list(const char *tag, const char* taglist, char delim=',')

int writefile(char *fname, char *specs)
{
if ( strcmp(specs,"") == 0 )
{
return saveall(fname);
}
// identify filetype based on extension
enum e_filetype {NONE, CSV, JSON, GLM} filetype = NONE;
char *ext = strrchr(fname,'.');
Expand Down Expand Up @@ -7441,7 +7444,7 @@ int readfile(char *fname, char *specs, char* line, int size)
int GldLoader::process_macro(char *line, int size, char *_filename, int linenum)
{
char *var, *val, *save;
char buffer[1024];
char buffer[1024] = "";
if ( get_language() )
{
const char *m = line;
Expand Down Expand Up @@ -8255,8 +8258,8 @@ int GldLoader::process_macro(char *line, int size, char *_filename, int linenum)
}
else if ( strncmp(line, "#write", 6) == 0 )
{
char fname[1024], specs[1024];
if ( sscanf(line+6,"%s %s",fname,specs) == 2)
char fname[1024], specs[1024] = "";
if ( sscanf(line+6,"%s %s",fname,specs) >= 1)
{
if ( writefile(fname,specs) < 0 )
{
Expand Down Expand Up @@ -8321,6 +8324,10 @@ int GldLoader::process_macro(char *line, int size, char *_filename, int linenum)
{
output_error("unable to resolve all object references");
}
for ( OBJECT *obj = object_get_first() ; obj != NULL ; obj = obj->next )
{
object_set_parent(obj,obj->parent);
}
strcpy(line,"\n");
return TRUE;
}
Expand Down Expand Up @@ -8357,7 +8364,7 @@ STATUS GldLoader::loadall_glm(const char *fname) /**< a pointer to the first cha
strcpy(file,fname);
OBJECT *obj, *first = object_get_first();
char *p = NULL;
char buffer[20480];
char buffer[20480] = "";
int fsize = 0;
STATUS status=FAILED;
struct stat stat;
Expand Down Expand Up @@ -8460,7 +8467,7 @@ STATUS GldLoader::loadall_glm(const char *fname) /**< a pointer to the first cha

TECHNOLOGYREADINESSLEVEL GldLoader::calculate_trl(void)
{
char buffer[1024];
char buffer[1024] = "";
CLASS *oclass;

// start optimistically
Expand Down
2 changes: 1 addition & 1 deletion source/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ static int set_header_value(OBJECT *obj, const char *name, const char *value,boo
output_error("object %s:%d parent %s not found", obj->oclass->name, obj->id, value);
return FAILED;
}
else if(object_set_parent(obj,parent)==FAILED && strcmp(value,"")!=0)
else if ( object_set_parent(obj,parent) < 0 && strcmp(value,"") !=0 )
{
output_error("object %s:%d cannot use parent %s", obj->oclass->name, obj->id, value);
return FAILED;
Expand Down
Loading