inputmat.sml

  Download

More scripts: Advanced

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
########################################################
# InputMat.sml
#
# Use a procedure and an added sml function to parse a
# string and fill in a matrix of known size
# from values found in a text file
#
# NOTE: script uses compiled instr.c tntsdk example
# to find numeric text position inside of a string.  
# SML will not work
# unless you have successfully compiled and run instr.exe
# See TNTSDK examples.
#
# an example would be to read in a covariance matrix needed
# for the RasterLinearCombine Function.  Once filled, the
# matrix is available to other functions.
#
# a sample text file is provided, and assumes a predetermined
# format.  The format contains leading descriptive text that
# precludes the use of fgetnum() to read in numbers from files.
#
# AUTHOR: 	      Ray L. Harris, Jr.
# 
# REQUESTED BY:
# CREATION DATE: 	March 17, 1998
# REVISION LIST: 
########################################################
########################################################
#
#	Example Data
#
#        Band_1 Band_2 Band_3 Band_4
#CROP  : 0.0388 0.0759 0.0459 0.0659
#SOIL  : 0.0402 0.0518 0.0477 0.0816
#ROAD  : 0.0843 0.1423 0.2324 0.2698
########################################################
#######################################################
#
#  proc readValuesFromFile(pathandfilename$, matHandle)
#
#  procedure to read values from file where file format is
#  descriptive text to the left of the colon, and data for each
#  band to the right. Data is separated by spaces.
#
#
#
#  parameters:
#             pathandfilename$  string of filename to process
#             matHandle         a matrix handle filled by this function
#######################################################
#  procedure and function definitions
	proc readValuesFromFile(pathandfilename$, matHandle) {
	print ("reading", pathandfilename$)
	#check for file existence
	If (fexists(pathandfilename$, "r")) {
		# open the file
		fHandle = fopen(pathandfilename$, "r")
		# trim spaces and print the first row, it contains headers
		valuestring$ = fgetline$(fHandle)
		while (left$(valuestring$, 1) == " " and strlen(valuestring$) > 0) {
			valuestring$ = right$(valuestring$, (strlen(valuestring$) - 1))
		}
		print(valuestring$)
		# outer loop to fill matrix rows
		# row size determined by the 
		# size of the passed matrix 
		loopend = GetMatrixRowSize(matHandle) - 1
		for s = 0 to loopend  {
			get the first line from the file
			valuestring$ = fgetline$(fHandle)
			# look for the colon separator
			# could replace this with a popup to ask the user
			# to pick the separator
			substr$ = ":"
			# get colon position using InStr function
			posvalue = InStr(valuestring$,substr$, 1)
			linelength = strlen(valuestring$) - posvalue
			# chop off descriptive text before the colon
			valuestring$ = right$(valuestring$, linelength)
			#get four values and at the same time test for leading spaces
			newsubstring$ = " "
			for v = 0 to 3 {
				while (left$(valuestring$, 1) == " " and strlen(valuestring$) > 0) {
					valuestring$ = right$(valuestring$, (strlen(valuestring$) - 1))
				}
				# use StrToNum to convert leading value
				value = StrToNum(valuestring$)
				# fill the matrix
				SetMatrixItem(matHandle, s,v,value)
				# find the next space, trim and repeat
				nextpos = InStr(valuestring$, newsubstring$, 1)
				valuestring$ = right$(valuestring$, strlen(valuestring$) - nextpos)
			}
		}
		# print the matrix number and contents comma separated
		printf("%s%d\n", "Matrix: ", matHandle)
		# get the size of the matrix
		lastRow = GetMatrixRowSize(matHandle) - 1
		lastCol = GetMatrixColSize(matHandle) - 1
		for row = 0 to lastRow {
			for col = 0 to lastCol {
				printf("%1.4f,", $2
				GetMatrixItem(matHandle, row, col))
				}
			print("  ")
			}
		print( "  " ) # print blank line after matrix output
	}
	# condition for wrong filename, could trap
	# an error, but not necessary
	else {
		print("Could not find or read file.")
		break
	}
	# close the text file 
	fclose(fHandle)
	} #end of proc printMatrix()
#######################################################
########################################################
# Main program
#
#
#
########################################################
# main program to test the procedure
clear()
# ask for the path and filename
prompt$ = "Enter path and filename."
filename$ = PopupString(prompt$, "c:/Program Files/MicroImages/TNT_70/smlsamples/inputmat_values.txt")
# make a matrix to fill
valuematrix = CreateMatrix(3,4)
# call the procedure
readValuesFromFile(filename$, valuematrix)
# test the returned matrix
testitem = GetMatrixItem(valuematrix,1,1)
printf("At row 1, col 1 the value is %1.4f\n", testitem)
# get rid of the matrix
DestroyMatrix(valuematrix)
#end main
########################################################