TABLE OF CONTENTS


::pwtk::write

SYNOPSIS

proc ::pwtk::write {args} {

USAGE

   ::pwtk::write ?-a? DATAFILE LINE

OPTION

ARGUMENTS

PURPOSE

"write" writes a line-of-data to a data file. If "write" is executed within the "scanpar" loops, the content of the line is prefixed with the list of scanning parameters. For example, the snipptet:

        scanpar k {4 8} {
           scanpar ecut {20 30} {
              write energy.dat [pwo_totene scf.k$k.ecut$ecut.out]
           }
        }

writes the lines in the form: $k $ecut energy

SOURCE

    variable write_fid
    variable scanpar
    variable scanparList
    
    # parse args

    set narg 2
    set usage "?-a? datafile line"
    set options {
        {a "open the datafile in the append mode"}
    }
    parseOpt_
    set mode     [expr { $opt(a) ? "a" : "w"}]
    set datafile [lindex $args 0]
    set line     [lindex $args 1]

    # write to datafile
    
    ifnotexist write_fid($datafile) {
        set write_fid($datafile) [open $datafile $mode]
        if  { [info exists scanparList] } {
            # write the first line as a comment documenting what columns are"
            puts $write_fid($datafile) "# columns are: $scanparList data"
        }
    }
    if { [info exists scanparList] && [string trim $line] ne {} } {
        # do not write "$params {}" line
        foreach pn $scanparList {
            upvar $pn p
            ifexist p {
                append params "$p "
            } else {                
                ifexist scanpar($pn) {
                    append params "$scanpar($pn) "
                }
            }
        }
        set line [concat $params $line]
    }    
    puts $write_fid($datafile) $line
    flush $write_fid($datafile)
    return $datafile
}