From ef7864878609187bfa1e42dbc1bd87e91c8514fc Mon Sep 17 00:00:00 2001
From: Plougonven Erwan <eplougonven@uliege.be>
Date: Mon, 13 May 2024 10:16:45 +0000
Subject: [PATCH] The code.

---
 Convert_Wyko_ASCII.py | 104 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
 create mode 100644 Convert_Wyko_ASCII.py

diff --git a/Convert_Wyko_ASCII.py b/Convert_Wyko_ASCII.py
new file mode 100644
index 0000000..cc43ef4
--- /dev/null
+++ b/Convert_Wyko_ASCII.py
@@ -0,0 +1,104 @@
+__author__ = "Erwan Plougonven"
+import os
+import sys
+import re
+import numpy as np
+from scipy.ndimage import median_filter
+
+def main():
+    input_file = "particle 2 a.asc"
+    output_file_h = "particle2a_height.am"
+    output_file_i = "particle2a_intensity.am"
+
+    dims, hfld, ifld = extract_Wyko_ASCII_data(input_file)
+
+    # Some spikes in height field raw data, removed with median filter
+    hfld = median_filter(hfld, footprint = np.array([[0,1,0],[1,1,1],[0,1,0]]))
+    
+    create_Avizo_ASCII(dims,hfld,output_file_h) 
+    create_Avizo_ASCII(dims,ifld,output_file_i)
+
+
+##########################
+### Internal functions ###
+##########################
+def extract_Wyko_ASCII_data(input_file):
+    allLines = [line.rstrip('\r\n') for line in open(input_file)]# if len(line)>0]
+
+    dataFormatKey = "Wyko ASCII Data File Format"
+    if dataFormatKey not in allLines[0]:
+        print("File "+input_file+" does not appear to be Wyko ASCII file. Exiting.")
+        sys.exit("Wrong file format. Header of file "+input_file+" is: \""+allLines[0]+"\". Was looking for: \""+dataFormatKey+"\".")
+
+    codes = [int(i) for i in allLines[0][len(dataFormatKey):].split()]
+    # First number is "Array format, 0 for standard, 2 for XYZ Triplet, Real".
+
+    nbPixelsKey = ["X Size","Y Size"]
+    dims = [int(allLines[x+1][len(nbPixelsKey[x]):]) for x in [0,1]]
+
+    # Find last line of header
+    endHeaderLine = r"OPD"
+    for i, line in enumerate(allLines):
+        if re.match(endHeaderLine+"[^\w]",line):
+            #print(line)
+            sepChar = line[len(endHeaderLine)]
+            break
+
+    data = allLines[i+1:]
+    startIntensityLine = r"Intensity" # There are two data sections
+    for i, line in enumerate(data):
+        if re.match(startIntensityLine,line):
+            #print(line)
+            break
+    data_H = data[0:i]
+    data_I = data[i+1:]
+
+    def parse_data(datalines):
+        arr = np.full(dims, np.nan)
+        if codes[0] == 0:
+            #print("Array Format: Standard")
+            for j,line in enumerate(datalines):
+                lineElems = line[:-1].split(sepChar)  # One too many separators at EOL
+                arr[j,:] = [x if x else np.nan for x in lineElems]
+            
+        if codes[0] == 2:
+            #print("Array Format: XYZ Triplet, Real")
+            for j in range(0,dims[0]):
+                for i in range(0,dims[1]):
+                    nums = [float(x) for x in datalines[j*dims[1] + i].split()]
+                    if(len(nums) == 3):
+                        arr[j,i] = nums[2]
+
+        np.nan_to_num(arr, copy=False, nan = np.nanmean(arr))
+        arr = np.transpose(arr)
+        return arr
+
+    height_field = parse_data(data_H)
+    intensity_field = parse_data(data_I)
+
+    return [dims,height_field,intensity_field]
+
+
+def create_Avizo_ASCII(dims,fld,output_file):
+    outputfile_header = """# Avizo 3D ASCII 2.0 
+
+define Lattice {X} {Y} 1
+Parameters {{
+    Content "{X}x{Y}x1 float, uniform coordinates",
+    BoundingBox 0 {BBx} 0 {BBy} 0 0.1,
+    CoordType "uniform"
+}}
+
+Lattice {{ float Data }} @1
+
+# Data section follows
+@1
+""".format(X = dims[0], Y = dims[1], BBx = dims[0]-1, BBy = dims[1]-1)
+
+    ofh = open(output_file, 'w')
+    ofh.write(outputfile_header)
+    np.savetxt(ofh,fld,fmt = "%f",delimiter = ' ')
+    ofh.close()
+
+if __name__ == '__main__':
+    main() # Defined this way so that user-defined parameters are at the beginning of the file.
\ No newline at end of file
-- 
GitLab