import os

'''
Input:
 - directory with detected sepal contour stacks .tif
 
Output:
 - segmented meshes (up and down surfaces treated as cells)
 - csv file / sepale containing : up and down surface areas

'''

# directory with detected sepal contours
filedata1="/home/akiss/RDP/Projects/Sepals/RNA-Seq_Final/06.ContourDetection/JL_22C/mgx-contours"


################### Parameters ###########

# cubesize for mesh creation
cubesize=str(15)

# size of neighbourhood for the segmented curvature map
curv_neighb=str(100)

# segmentation
signal_ratio=str(3) # minimal border / cell signal ratio (if less, cells are fused)
# - cell
cell_blur_radius=str(100)
seed_radius=str(150)
# - border
border_distance=str(100)
border_blur_radius=str(100)

#########################################

filedata=filedata1+'/'
meshresult=filedata1+'-MGX-mesh'+cubesize+'/'
curvaturecsvresult=filedata1+'-MGX-curvature-csv/'
arearesult=filedata1+'-MGX-area/'
segresult=filedata1+'-MGX-seg/'


os.system("mkdir "+meshresult)
os.system("mkdir "+curvaturecsvresult)
os.system("mkdir "+arearesult)
os.system("mkdir "+segresult)


listfiles=os.listdir(filedata)
listfiles=[ i for i in listfiles if '.tif' in i]


Process.Stack__System__Clear_Work_Stack('0')
Process.Stack__System__Clear_Main_Stack('0')
Process.Stack__System__Clear_Main_Stack('1')
Process.Stack__System__Clear_Work_Stack('1')
for i in listfiles:	
	Process.Stack__System__Set_Current_Stack('Main', '0')
	Process.Stack__System__Open(filedata+i, 'Main', '0')
	# --- creating surface mesh
	Process.Mesh__Creation__Marching_Cubes_Surface(cubesize, '5000')
	Process.Mesh__Structure__Smooth_Mesh('1', 'No')
	Process.Mesh__Structure__Smooth_Mesh('1', 'No')
	Process.Mesh__Structure__Smooth_Mesh('1', 'No')
	Process.Mesh__Structure__Smooth_Mesh('1', 'No')
	# --- computing maximal curvature
	csvname=i[:-4]+'-carte-courbure-Maximal'+curv_neighb+'.csv'
	Process.Mesh__Signal__Project_Mesh_Curvature(curvaturecsvresult+csvname, 'Maximal', curv_neighb, 'Yes', '-50.0', '50.0', '85')
	meshname=i[:-4]+'-mesh.mgxm'
	Process.Mesh__System__Save(meshresult+meshname, 'no', '0')
	Process.Stack__System__Clear_Work_Stack('0')
	Process.Stack__System__Clear_Main_Stack('0')
	Process.Stack__System__Clear_Main_Stack('1')
	Process.Stack__System__Clear_Work_Stack('1')
	# --- watershed segmentation of the maximal curvature map
	Process.Mesh__Segmentation__Auto_Segmentation('Yes', 'No', cell_blur_radius, seed_radius, border_blur_radius, '100.0', border_distance, signal_ratio)
	segname=i[:-4]+'-seg.mgxm'
	Process.Mesh__System__Save(segresult+segname, 'no', '0')
	# --- computing the area of the faces
	csvname=i[:-4]+'-area.csv'
	Process.Mesh__Heat_Map__Heat_Map_Classic('Area', 'Geometry', arearesult+csvname, 'Geometry', 'No', '0', '65535', 'Yes', 'No', 'None', 'No', 'Decreasing', 'Ratio', '0.001', '1')
	# --- computing teh perimeter
	#csvname=i[:-4]+'-perimeter.csv'
	#Process.Mesh__Heat_Map__Heat_Map('/Geometry/Perimeter', 'No', 'No', '', 'No', '', '', 'Yes', 'Yes')
	# --- computing gaussian curvature
	#Process.Mesh__Signal__Project_Mesh_Curvature('', 'Gaussian', '100', 'Yes', '-50.0', '50.0', '85')
	"""print("Please put two seeds on the two sides of the sepal :-) ... ")
	a=input("")
	Process.Mesh__Segmentation__Watershed_Segmentation('50000')
	csvname=i[:-4]+'-area.csv'
	Process.Mesh__Heat_Map__Heat_Map_Classic('Area', 'Geometry', csvresult+csvname, 'Geometry', 'No', '0', '65535', 'Yes', 'No', 'None', 'No', 'Decreasing', 'Ratio', '0.001', '1')
	"""
