When performing TEM analysis, all my data are generated and coarsely analyzed on on-site computers, while exported into my local computer for deep analysis. The raw data folders could be really big containing raw data files which can only be recognized by the licensed vendor software, middle product of analysis, which are mainly images, and supporting files like excel sheets and csv files.
Take renaming for example, the auto generated images from vendor software may have a regid rule of naming which doesn't suit my need. If it is for elemental analysis, each folder may has tens of map files with name like "XXXXX_Au_XXXXX.tif", while you just need "Au.tif". Scripting will go into these elemental map folders and help you rename all of the map fils by a sigle click "Run". Such a life saver!
Below is the scope of work I needed to run, and my basic folder structure is shown below:
-root\job_folder
-SiteList.csv
-sample1_folder
-sample1_image1.tif
-...
-sample1_image10.tif
-map1_folder
-1-Au-wt.tif
-1-Cr-wt.tif
-...
-1-Pt-wt.tif
-map2_folder
-1-Au-net.tif
-1-Cr-net.tif
-...
-1-Pt-net.tif
-sample2_folder
-...
-...
-sample12_folder
My script is consisted of 4 steps:
#Paste in the job direction
$path = "\\....\...\FR2304-0005"
#Get job number. Change the index if the path is differnt in format.
$jobnumber = $path.split("\")[-1]
Set-Location $path
#Rename all elemental maps
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("1-", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("-wt", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("-net", "") }
I have a SiteList.csv file created by TEM tool when running the recipe to acquire data. In that .csv file, I found there is a inconvinience to flip the images in subfolders without flipping the images in folder. Also, for this script, only the '.tif' files are affected.
#Read the .csv file and check if there is any images needs to be flipped
$P = Import-Csv -Path .\SiteList.csv
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
for ($i = 0; $i -le 12; $i++){
if ($P.Flipped[$i] -eq "True"){
$sub = $path + "\" + $P.SampleID[$i]
$sample = $P.SampleID[$i]
#Flip all images first and flip back images not in subfolders
Get-ChildItem -recurse ($sub) -include @("*.tif") |
ForEach-Object {
$image = [System.Drawing.image]::FromFile( $_ )
$image.rotateflip("RotateNoneFlipY")
$image.save($_)
}
Get-ChildItem ($sub) -include @("$sample*")|
ForEach-Object {
$image = [System.Drawing.image]::FromFile( $_ )
$image.rotateflip("RotateNoneFlipY")
$image.save($_)
}
}
}
For compositional analysis, there are some elements that are included solely to help deconvolute signals. As most of the TEM samples are placed on Cu grid, Cu is usually selected for devonvolution purpose only. Therefore, we need a single line of code to remove all "Cu.tif" files.
Get-ChildItem ($path) Cu.tif -Recurse | Remove-Item
The raw data file is now well trimed and ready for export. However, the raw data files will bacome useless on other computers, so we'd like to skip them. Instead of using `ctrl + left click`to manually select files to move and repeating the same operation for each subfolders, we can use ROBOCOPY.
#Setup the desination folder directory
$des_path = '\\....\...\' + $jobnumber + '\TEM'
#Only copy the image files
ROBOCOPY $path $des_path *.tif *.jpg *.png /e
Note: I use Powershell version 5.1. Previous versions may not fully work with this script, especially the image flip section. To check your own Powshell version, type `$PSVersionTable` in your powshell command line. The Value for PSVersion would tell.