TABLE OF CONTENTS


::pwtk::readCards

SYNOPSIS

proc ::pwtk::readCards {inputFile cardList {closingKeywordList {}}} {

PURPOSE

Read cards from the QE input file and return them in the PWTK syntax.

This routine is used internally by PWTK in load_from*** set of commands.

WARNING

This routine assumes that card-keywords are case sensitive in QE input files (i.e. ATOMIC_POSITIONS != atomic_positions)

This routine also implicitly assumes that cards are specified after the namelists, which is the recipe explicitly followed by QE. If instead namelists are specified after the cards in the input file, this routine will fail to correctly parse the cards from the input.

ARGUMENTS

SOURCE

    if { $cardList eq {} } {
        return ""
    }
    foreach card $cardList {
        # BEWARE: for closed cards, card may consist of "card closingKeywordList"
        lassign $card card close
        lappend cards $card
    }
    set card_re  [::pwtk::cardL2regexp $cards]
    set close_re [::pwtk::cardL2regexp $closingKeywordList]

    set output  {}
    set inside_card 0

    foreach line [split [::pwtk::readFile $inputFile] \n] {
                
        set line [string trim $line]
        set word [lindex $line 0]
        
        if { [regexp $card_re $word] } {
            if { $inside_card } {
                append output "\}\n"
            }
            append output "$word [lrange $line 1 end] \{\n"
            set inside_card 1       
            continue
        }
        if { $close_re != {} && [regexp $close_re $word] } {
            if { $inside_card } {
                # close the card
                append output "\}\n"
            }
            set inside_card 0
            continue
        }
        
        if { $inside_card && ! [regexp {^#|^!} $word] } {
            append output $line\n
        }
    }
    
    if { $inside_card } {
        # close the last card
        append output "\}\n"
    }

    return $output    
}