TIFF_TO_JP2_MIE_FromJob.sml

  Download

More scripts: Import & Export

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# TIFF_TO_JP2_MIE_FromJob.sml
# Sample script for tutorial Writing Scripts with SML
# Script called by a job file created with TIFF_TO_JP2_MIE_GetJobParms.sml.
# Converts a TIFF file to a JP2 file using parameters provided in the job file. 
# Requires version 2009 or later of the TNT products.  Script must be in the
# same directory as TiffToJP2getJobParms.sml.
# Script version 2 December 2009.
##################  variables read from the Job file  ######################
string inputPath$;		# complete directory path and name of input file
string outputDir$;		# complete path of output directory
string compType$;		# type of JPEG2000 compression; possible values = lossless, best, or user
numeric compRatio;	# compression ratio to set for user-defined lossy compression
###################################################################
# uncomment the following preprocessor command to run in test mode
# with hard-coded job parameters outside of the Job Control environment
#	 $define TEST
##############################################################
################  Job parameters for test mode  ###################
$ifdef TEST
		# modify the inputPath$ below to reference an available TIFF file on your computer 
		inputPath$ = _context.ScriptDir + "/DK1.TIF";
		outputDir$ = _context.ScriptDir+ "/";
		compType$= "LossyRatio";
		compRatio = 15;
$endif
#################  Other variables  #############################
class STRING logfileName$;		# variables for a log file to record processing information
class FILE logfile;
class DATETIME currentDT;		# the current local date/time
# error checking procedure
numeric err;
proc ReportError(numeric linenum, numeric err) {
	printf("FAILED -line: %d, error: %d\n", linenum - 1, err);
	fprintf(logfile, "FAILED -line: %d, error: %d\n", linenum - 1, err);
	PopupError(err); 
	}
######################  Main Program  ##############################
clear();
# Open log file in the same directory as this script to append status information;
# If no log file is present, a new one will be created.
logfileName$ = _context.ScriptDir + "/TIFF_TO_JP2_MIE.log";
logfile = fopen(logfileName$, "a");
currentDT.SetCurrent();				# get current date and time and write to log
fprintf(logfile, "\nJob initiated %s Central Standard Time\n", currentDT);
# set up filepath for the input TIFF file using the path string from the job file
class FILEPATH inFilepath(inputPath$);
fprintf(logfile, "Input filename = %s\n", inFilepath);
# print other processing parameters read from the job file
fprintf(logfile, "output directory = %s\n", outputDir$);
fprintf(logfile, "compType$ = %s\n", compType$);
fprintf(logfile, "compRatio = %.1f\n", compRatio);
# in TEST mode, print info to the console window
$ifdef TEST
	print(inFilepath);
	printf("input filepath = %s\n", inFilepath);
	printf("output directory = %s\n", outputDir$);
	printf("compType$ = %s\n", compType$);
	printf("compRatio = %.1f\n", compRatio);
$endif
#### set up to link to the GeoTIFF file using a temporary Project File
class RVC_OBJECT tempFile;		# for temporary Project File to hold bands for export
class MieGeoTIFF geotiff;			# for importing from or linking to GeoTIFF
class RVC_OBJITEM objItemList[];	 		# hash of objItems for the component rasters to be created
																					# from a single multiband GeoTIFF file
class STRING dest$;					# string with destination path and filename
numeric numBands;					# number of bands found in GeoTIFF file
numeric imported;					# number of bands successfully imported
numeric j;											# loop counter
# check source file to generate an ObjItemList with the proper number of components 
geotiff.GetObjItemList(inputPath$, objItemList);
numBands = objItemList.GetNumItems();
fprintf(logfile, "Linking to %d GeoTIFF bands.", numBands);
# make temporary Project File to hold rasters imported from GeoTIFF
tempFile.MakeTempFile(1);	# set to delete on close.
class FILEPATH tempFilepath;
tempFilepath = tempFile.GetObjItem().GetFilePath();
class RVC_DESCRIPTOR descriptor;		# descriptor for naming temporary raster objects
# set tempfile filepath and object names for each ObjItem in ObjItemList
for j = 1 to numBands
	{
	descriptor.SetName(sprintf( "Component%d", j) );
	objItemList[ j ].CreateNew(tempFile.GetObjItem(), "RASTER", descriptor);
	fprintf(logfile, "temp filepath = %s, object path = %s\n", 
				objItemList[ j ].GetFilePath(), objItemList[j ].GetObjectPath() ); 
	$ifdef TEST
		printf("temp filepath = %s, object path = %s\n", 
				objItemList[ j ].GetFilePath(), objItemList[j ].GetObjectPath() ); 
	$endif
	}
# import the GeoTIFF bands to the temp file
geotiff.DoLink = 1;
geotiff.ImportObjects(inputPath$, objItemList);
fprint(logfile, "Exporting to GeoJP2");
# Reset the object paths in the ObjItemList after import to associate the ObjItems with actual inodes in the file. 
# Check that all imported bands actually exist.
imported = 0;
for j = 1 to numBands
	{
	objItemList[ j ].SetObjectPath( sprintf("Component%d.RASTER", j) );
	imported += objItemList[ j ].IsExisting();		# returns 1 if object exists
	}
fprintf(logfile, "imported = %d\n", imported);
if (imported == numBands)
	{ 
	# set path and filename for export to GeoJP2
	class STRING filename$ = inFilepath.GetNameOnly();	# name of input GeoTIFF file 
	dest$ = outputDir$ + filename$ + ".jp2";
	fprintf(logfile, "tempFilepath = %s\n", tempFilepath);
	fprintf(logfile, "output file path = %s\n", dest$);
	
	$ifdef TEST
		printf("tempFilepath = %s\n", tempFilepath);
		printf("output file path = %s\n", dest$);
	$endif
	###$ set up to export from temporary Project File to JP2
	class MieGeoJP2 geoJP2;				# for exporting to GeoJP2
	# set compression options for export to GeoJP2
	geoJP2.CompressionType = compType$;
	geoJP2.CompressionRatio = compRatio;
	# export the linked bands to the designated GeoJP2 file
	geoJP2.ExportObjects(objItemList, dest$);
	fprint(logfile, "Export of file completed.");
	}
else
	fprint(logfile, "Some GeoTIFF bands not imported, aborting export.");
# close the tempfile
tempFile.Close();