annotate imagej2_create_image.py @ 3:bb65623b90a8 draft

planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
author iuc
date Tue, 04 Aug 2015 11:15:10 -0400
parents 26703988cdb3
children 48f25b8408e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
1 #!/usr/bin/env python
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
2 import argparse
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
3 import shutil
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
4 import subprocess
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
5 import tempfile
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
6 import imagej2_base_utils
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
7
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
8 if __name__=="__main__":
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
9 # Parse Command Line.
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
10 parser = argparse.ArgumentParser()
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
11 parser.add_argument( '--width', dest='width', type=int, help='Image width in pixels' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
12 parser.add_argument( '--height', dest='height', type=int, help='Image height in pixels' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
13 parser.add_argument( '--depth', dest='depth', type=int, help='Image depth (specifies the number of stack slices)' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
14 parser.add_argument( '--image_type', dest='image_type', help='Image type' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
15 parser.add_argument( '--image_title', dest='image_title', default='', help='Image title' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
16 parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
17 parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
18 parser.add_argument( '--output_datatype', dest='output_datatype', help='Output image format' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
19 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
20 parser.add_argument( '--out_fname', help='Path to the output file' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
21 args = parser.parse_args()
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
22
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
23 tmp_dir = imagej2_base_utils.get_temp_dir()
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
24 tmp_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
25 # Set the size of the memory allocation pool used by the JVM.
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
26 memory_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
27
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
28 # Define command response buffers.
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
29 tmp_out = tempfile.NamedTemporaryFile().name
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
30 tmp_stdout = open( tmp_out, 'wb' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
31 tmp_err = tempfile.NamedTemporaryFile().name
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
32 tmp_stderr = open( tmp_err, 'wb' )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
33 # Build the command line.
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
34 cmd = imagej2_base_utils.get_base_command_imagej2( memory_size, jython_script=args.jython_script )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
35 if cmd is None:
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
36 imagej2_base_utils.stop_err( "ImageJ not found!" )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
37 cmd += ' %s %d %d %d %s %s' % ( args.image_title, args.width, args.height, args.depth, args.image_type, tmp_image_path )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
38 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
39 rc = proc.wait()
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
40 if rc != 0:
3
bb65623b90a8 planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 1
diff changeset
41 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
0
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
42 imagej2_base_utils.stop_err( error_message )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
43 shutil.move( tmp_image_path, args.out_fname )
dfe318d97378 planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
iuc
parents:
diff changeset
44 imagej2_base_utils.cleanup_before_exit( tmp_dir )