Version2/GeneralFunctions.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#Version 2 of Vasp2Visual with general functions.
function Read-BigFile{
    <#
    .DESCRIPTION
    Returns a range of lines from a big file. Use Get-Content for small files.
    StremReader requires Absolute path.
    .EXAMPLE
        Read-BigFile -FilePath E:/Research/Current/pDOS.txt -StopIndex 5
        You can use -StartIndex to provide a range to read.
    #>

    [CmdletBinding(DefaultParameterSetName='interval')]
    param (
        [Parameter(Mandatory="True",ValueFromPipeline=$true)][String]$FilePath,
        [Parameter(ParameterSetName='interval')][int]$StartIndex =0,
        [Parameter(ParameterSetName='interval')][int]$StopIndex=0,
        [Parameter(ParameterSetName='full')][switch]$ReadAll
    )
    $AbsPath = (Get-Item $FilePath).FullName
    [System.IO.StreamReader] $reader = New-Object  -TypeName 'System.IO.StreamReader' -ArgumentList ($AbsPath, $false);
    [String] $line = $null;
    [Int32] $currentIndex = 0;

    try{
        if($PSCmdlet.ParameterSetName -eq 'interval'){
            while($currentIndex -le $StopIndex){
                $line = $reader.ReadLine()
                if ($null -ne $line -and $currentIndex -ge $StartIndex){
                    $line
                }
                $currentIndex++
            }
        }elseif ($PSCmdlet.ParameterSetName -eq 'full'){
            While($null -ne ($eachLine=$reader.ReadLine())){
                $eachLine 
                }
        }
    }
    finally{
        $reader.Close();
    }
}

function Write-BigStream{
    <#
    .DESCRIPTION
    Writes a given array to a file either on one line or as given object.
    StremWriter requires Absolute path.
    .EXAMPLE
        $x=Read-BigFile -FilePath E:/Research/Current/pDOS.txt -StopIndex 5
        Write-BigStream -StreamArray $x -FilePath E:/Research/Current/new.txt
        Write-BigStream -StreamArray $x -FilePath E:/Research/Current/new.txt -AsOneLine
 
        Oneline file is good for using in python numpy.reshape() function, which can not read file
        if there are lines with empty entries.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory="True",ValueFromPipeline = $true)][array]$StreamArray,
        [Parameter(Mandatory="True")]$FilePath,
        [Parameter()][switch]$AsOneLine
    )
    $AbsFilePath = (Get-Item $FilePath).FullName
    $sw = New-Object System.IO.StreamWriter $AbsFilePath
    [array]$StreamArray=$StreamArray
    if($AsOneLine.IsPresent){
        foreach ($line in $StreamArray) {
            if($null -ne $line){
            $sw.Write($line)}          
        }
    }else{
        foreach ($line in $StreamArray) {
            if($null -ne $line){
            $sw.WriteLine($line)}
        }
    } 
    $sw.Close();
}

function Get-POSCAR {
    [CmdletBinding(DefaultParameterSetName='SITES')]
    Param(
        [Parameter()]$Formula = 'GaAs',
        [Parameter(ParameterSetName='MPID')]$MP_ID,
        [Parameter(ParameterSetName='SITES')]$MaxSites,
        [Parameter()]$APIKey
    )
    $var_dict = "dict({})"
    if($PSBoundParameters.ContainsKey('MP_ID')){
        $var_dict = "dict(mp_id = '{0}')" -f $MP_ID
    }
    if($PSBoundParameters.ContainsKey('MaxSites')){
        $var_dict = "dict(max_sites = {0})" -f $MaxSites
    }
    if($PSBoundParameters.ContainsKey('APIKey')){
        $rep = $(",api_key = '{0}')" -f $APIKey)
        $var_dict = $var_dict.Replace(")", $rep) 
    }
    Write-Host "Use Pivotpy in Python for full functionality!" -ForegroundColor Yellow
    $py_str = "vd = {1}`nfrom pivotpy import sio`ngp = sio.get_poscar('{0}',**vd)`n" -f $Formula, $var_dict
    $py_str += "import json`ns=json.dumps([_g.to_dict() for _g in gp])`nprint(s)"
    # Run it finally Using Default python on System preferably.
    if($null -ne (Get-Command python3* -ErrorAction SilentlyContinue)){
        Write-Host ("Running using {0}" -f (python3 -V)) -ForegroundColor Green
        $json = $py_str | python3
        ConvertFrom-Json $json
    }elseif($null -ne (Get-Command python -ErrorAction SilentlyContinue)){
        Write-Host ("Running using {0}" -f (python -V)) -ForegroundColor Green
        $json = $py_str | python
        ConvertFrom-Json $json
    }elseif($null -ne (Get-Command pytnon2* -ErrorAction SilentlyContinue)){
        Write-Host ("Required Python >= 3.6, but {0} found, try upgrading Python." -f (python2 -V)) -ForegroundColor Red
    }else{
        Write-Host "Python Installation not found. Copy code below and run yourself or use '-SavePyFile'." -ForegroundColor Red
        Write-Host $py_str -ForegroundColor Yellow
    }
}

Export-ModuleMember -Function 'Read-BigFile'
Export-ModuleMember -Function 'Write-BigStream'
Export-ModuleMember -Function 'Get-POSCAR'