#!/bin/sh # the next line restarts using tclsh \ exec tclsh8.4 "$0" "$@" proc ::main {argc argv} { global pw_input_file global pw_output_file get_commandline $argc $argv convert_to_xyz } ########################## #A procedure to read commandline ########################## proc ::get_commandline {argc argv} { global pw_input_file global pw_output_file set fatzero 0 if { $argc < 1} { puts stderr "Usage: pwo2xsf \[parameters\]" puts stderr "Parameters:" puts stderr "none at the moment " exit 1 } if { $argc > 0 } { set state first foreach arg $argv { switch -- $state { first { set state flag #check if the infile exists set pw_input_file $arg if { [file exists $pw_input_file] } { puts "using input file $pw_input_file" } else { puts stderr "Input file $pw_input_file does not exits" exit 1 } } flag { switch -glob -- $arg { default { puts stderr "unknown flag $arg" exit 2 } } } } } } set pw_output_file "$pw_input_file.xyz" } proc ::convert_to_xyz {} { global pw_input_file global pw_output_file global n_at global lc_pos if [catch {open $pw_input_file r} fileId] { puts stderr "Cannot open $pw_input_file: $fileId" exit 1 } set tmpfile [read $fileId] close $fileId if [catch {open $pw_output_file w} fileId] { puts stderr "Cannot open $pw_output_file: $fileId" exit 1 } # read number of atoms if {![regexp -linestop -- {number of atoms/cell\s*=\s*([0-9]+)} $tmpfile match n_at]} { puts "Error reading n_at, exiting" exit 2 } if {![regexp -linestop -nocase -- {ATOMIC_POSITIONS\s*[{,(]\s*([a-z]+)\s*[},)]} $tmpfile match type] } { puts "Error reading ATOMIC_POSITIONS, exiting" exit 2 } #Determine coordinate type puts "Coordinates are of '$type' type" if {[regexp -nocase -- {ang} $type]} { puts " activating angstrom coordinate mode " } else { if {![regexp -nocase -- {alat} $type]} { puts "...which means it is too difficult for me, bailing out" exit 2 } } puts "Coordinates will be written to $pw_output_file" set lc_pos 0 while {[regexp -indices -start $lc_pos -nocase -- {ATOMIC_POSITIONS\s*\([a-z]+\)\s*} $tmpfile lc_pos] != 0 } { set lc_pos [lindex [split $lc_pos] 1] puts "Found coordinates at index:$lc_pos in output file" puts $fileId "$n_at" puts $fileId "" #Read atoms for {set i 0} {$i<$n_at} {incr i 1} { set search "regexp -start $lc_pos -linestop -nocase -- {\[a-z\]+\\s+-?\[0-9\]+\\.?\[0-9\]*\\s+-?\[0-9\]+\\.?\[0-9\]*\\s+-?\[0-9\]+\\.?\[0-9\]*} \$tmpfile match" eval $search puts $fileId $match set search "regexp -start $lc_pos -linestop -nocase -indices -- {\[a-z\]+\\s+-?\[0-9\]+\\.?\[0-9\]*\\s+-?\[0-9\]+\\.?\[0-9\]*\\s+-?\[0-9\]+\\.?\[0-9\]*\\s*} \$tmpfile match" eval $search set lc_pos [lindex [split $match] 1] } } close $fileId } main $argc $argv