;+ ; NAME: ; struct_addfield ; PURPOSE: (one line) ; add fields to an existing structure ; DESCRIPTION: ; add fields to an existing structure ; CATEGORY: ; util ; CALLING SEQUENCE: ; new = struct_addfield(old, tag, val, new_structname = new_structname) ; INPUTS: ; old - existing structure ; tag - name of field ; val - value for field ; OPTIONAL INPUT PARAMETERS: ; none ; KEYWORD INPUT PARAMETERS: ; new_structname - if the structure is to be named (not anonymous), ; the name of the new structure ; KEYWORD OUTPUT PARAMETERS: ; none ; OUTPUTS: ; new - new structure with field added ; COMMON BLOCKS: ; None ; SIDE EFFECTS: ; none ; RESTRICTIONS: ; PROCEDURE: ; Constructs a call to create_struct ; Calls lexecute to execute the long string ; USAGE ; MODIFICATION HISTORY: ; 2005 Dec 31 Leslie Young ;- function struct_addfield, old, tag,var, new_structname = new_structname old_structname = tag_names(old, /structure_name) if old_structname ne '' and keyword_set(new_structname) then begin exstr = 'new = ' exstr = exstr + '{' + new_structname + ', ' exstr = exstr + 'inherits ' + old_structname + ', ' exstr = exstr + tag + ' : var ' exstr = exstr + '}' if strlen(exstr) lt 130 then begin result = execute(exstr) endif else begin exstr = 'new = $' exstr = [exstr, '{' + new_structname + ', $'] exstr = [exstr, 'inherits ' + old_structname + ', $'] exstr = [exstr, tag + ': var $'] exstr = [exstr, '}'] oldsav=tmpfile('struct_addfield','sav') ; get a temporary sav filename save, old, var,fil=oldsav ; save variables lexecute,exstr,newsav,lexecute_oldsav=oldsav restore,newsav ; restore newsav file_delete,newsav,oldsav ; cleanup endelse struct_assign, old, new new.(n_tags(new)-1) = var return, new endif else begin old_names = tag_names(old) n = n_tags(old) exstr = 'new = { $' if keyword_set(new_structname) then begin exstr = [exstr, new_structname + ', $'] end for i=0, n-1 do begin exstr = [exstr, old_names[i] + ': old.(' + string(i) + '), $'] endfor exstr = [exstr, tag + ': var $'] exstr = [exstr, '}'] oldsav=tmpfile('struct_addfield','sav') ; get a temporary sav filename save, old, var,fil=oldsav ; save variables lexecute,exstr,newsav,lexecute_oldsav=oldsav restore,newsav ; restore newsav file_delete,newsav,oldsav ; cleanup struct_assign, old, new new.(n_tags(new)-1) = var return, new endelse end