;+
;NAME:
; struct2ascii_read
;PURPOSE:
; Reads a structure from a file created using the struct2ascii
; procedure, calls itself recursively for nested structures
;CALLING SEQUENCE:
; struct2ascii_read, lun, structure
;INPUT:
; lun = a logical unit for input
;OUTPUT:
; structure =  a structure
;HISTORY:
; 23-apr-2007, jmm, jimm@ssl.berkeley.edu
;
;$LastChangedBy$
;$LastChangedDate$
;$LastChangedRevision$
;$URL$
;-
Pro struct2ascii_read, lun, structure


  structure = -1
  line = strarr(1)
  readf, lun, line
  test0 = strsplit(line, ':', /extract)
  If(strupcase(test0[0]) Ne 'STRUCTURE NAME') Then Begin
    message, /info, 'Missing ''STRUCTURE NAME'' Line'
    Return
  Endif

;Ok, now you know what to read:
  readf, lun, line
  test0 = strsplit(line, ':', /extract)
  If(strupcase(test0[0]) Ne 'STRUCTURE DIMENSIONS') Then Begin
    message, /info, 'Missing ''STRUCTURE DIMENSIONS'' Line'
    Return
  Endif
;Get the structure dimensions
  dim = long(test0[1:*])
  ndim = n_elements(dim)
  ntot = dim[0]
  If(ndim Gt 1) Then For j = 1, ndim-1 Do ntot = ntot*dim[j]
;Next the number of tags:
  readf, lun, line
  test0 = strsplit(line, ':', /extract)
  If(strupcase(test0[0]) Ne 'STRUCTURE N_TAGS') Then Begin
    message, /info, 'Missing ''STRUCTURE N_TAGS'' Line'
    Return
  Endif
  ntags = long(test0[1])
;For each structure element, for each tag
  For j = 0, ntot-1 Do Begin
    strj = {dummy:-1}
    For k = 0, ntags-1 Do Begin
;The next thing is the tag name:
      readf, lun, line
      test0 = strsplit(line, ':', /extract)
      If(strupcase(test0[0]) Ne 'TAG NAME') Then Begin
        message, /info, 'Missing ''TAG NAME'' Line'
        Return
      Endif
      tagk = test0[1]
;Next, is a tag type_name
      readf, lun, line
      test0 = strsplit(line, ':', /extract)
      If(strupcase(test0[0]) Ne 'TAG TYPE_NAME') Then Begin
        message, /info, 'Missing ''TAG TYPE_NAME'' Line'
        Return
      Endif
;If this is a structure, then call this program, else it's readable
      test2 = strcompress(/remove_all, strupcase(test0[1]))
      If(test2 Eq 'STRUCT') Then Begin
        struct2ascii_read, lun, temp
      Endif Else Begin
;Next is the tag data type
        readf, lun, line
        test0 = strsplit(line, ':', /extract)
        If(strupcase(test0[0]) Ne 'TAG TYPE') Then Begin
          message, /info, 'Missing ''TAG TYPE'' Line'
          Return
        Endif
        tag_type = fix(test0[1])
;Next is the tag n_dimensions
        readf, lun, line
        test0 = strsplit(line, ':', /extract)
        If(strupcase(test0[0]) Ne 'TAG N_DIMENSIONS') Then Begin
          message, /info, 'Missing ''TAG N_DIMENSIONS'' Line'
          Return
        Endif
        n_dimensions = long(test0[1])
;Next is the tag dimensions
        readf, lun, line
        test0 = strsplit(line, ':', /extract)
        If(strupcase(test0[0]) Ne 'TAG DIMENSIONS') Then Begin
          message, /info, 'Missing ''TAG DIMENSIONS'' Line'
          Return
        Endif
        test1 = strsplit(test0[1], ' ', /extract)
        dimensions = long(test1)
;Next line says tag values
        readf, lun, line
        test0 = strsplit(line, ':', /extract)
        If(strupcase(test0[0]) Ne 'TAG VALUES') Then Begin
          message, /info, 'Missing ''TAG VALUES'' Line'
          Return
        Endif
;Next is the input
        If(n_dimensions Eq 0 And dimensions[0] Eq 0) Then Begin
          scalar = 1b
          n_dimensions = 1
          dimensions = 1
        Endif Else scalar = 0b
        temp = make_array(type = tag_type, dimension = dimensions)
        readf, lun, temp
        If(scalar) Then temp = temp[0]
;Next line says end tag
        readf, lun, line
        test0 = strsplit(line, ':', /extract)
        If(strupcase(test0[0]) Ne 'END TAG') Then Begin
          message, /info, 'Missing ''END TAG'' Line'
          Return
        Endif
      Endelse
;add this element to the structure
      strj = add_tag(strj, temp, tagk, /no_copy)
;      str_element, /add, strj, tagk, temp
    Endfor
    ty = n_tags(strj)
    If(ty gt 1) Then Begin      ;something worked
      str_element, /delete, strj, 'dummy'
      If(is_struct(structure)) Then Begin
        structure = [temporary(structure), temporary(strj)]
      Endif Else structure = temporary(strj)
    Endif Else Begin
      message, /info, 'No structure Read'
      structure = -1
      Return
    Endelse
  Endfor
;Reform if necessary and possible
  If(ndim Gt 1) Then Begin
    If(n_elements(structure) Eq ntot) Then Begin
      xd = lonarr(8)+1
      xd[0:ndim-1] = dim
      structure = reform(structure, xd[0], xd[1], xd[2], xd[3], xd[4], $
                         xd[5], xd[6], xd[7], /overwrite)
    Endif Else message, /info, 'Could not reform structure'
  Endif


End