code_seg1 = """

import traceback
import sys
import numpy as np
import os
try:
	if not os.path.exists("scripts"):
		os.mkdir("scripts")
	os.chdir("./scripts")
except:
	pass

"""
code_seg2 = """

while not channel.isclosed():
	[typecode, byteorder, dataString, positionString] = channel.receive()
	# construct the Numpy data type out of the endianness and array typecode
	if byteorder == 'big':
		dtype = '>'
	else:
		dtype = '<'
	dtype += typecode
	
	# create a 2d Numpy array out of the data
	# Note that we make a copy because frombuffer returns a read-only array
	input = np.frombuffer(dataString, dtype=dtype).copy()
	position = np.frombuffer(positionString, dtype=dtype).copy()
	try:
		output = process(input.astype(np.float64),position)
	except:
		print("*********************traceback info for process**********************")
		print("********************************************************************")
		traceback.print_exc()
		print("********************************************************************")
		print("********************************************************************")
	channel.send(['d',sys.byteorder, output.tostring(), positionString])

"""

from java.lang import Math
import copy
from org.python.modules import jarray
from icy.main import Icy
from icy.sequence import Sequence
from icyexecnetgateway import IcyExecnetGateway, unpack_image
from array import array
import sys
gateway = IcyExecnetGateway()
gateway.remote_exec(code_seg1+code+code_seg2)
def process(input, position):
	'''
	input: 1d array
	position: 1d array indicate current position, format: [x,y,z,t,c]
	'''
	gateway.send(['d',sys.byteorder, input.tostring(), position.tostring()])
	[typecode, byteorder, dataString, positionString] = gateway.receive()
	output = array(typecode, dataString)
	if sys.byteorder <> byteorder:
		output.byteswap()
	return output