Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
############################################################################
# Fragstats.sml
# Created by: Mark Smith
# Most recent revision: 5-2001
# This script takes an input raster and a mask raster, and uses the mask to
# create a raster usable by fragstats. It writes the output raster to a text
# file and calls fragstats using a set of default parameters after querying
# the user for the edge distance value.
#
# The input raster must be an an integer-value, single raster with square cells.
# Later versions may support more formats. The mask raster must have exterior
# background areas marked -1, interior background areas marked 1, and landscape
# areas marked 0.
#
# This script requires the fragstats executable which is installed in the TNT
# mips win32 directory and that the temporary folder path specified by mips
# have folder names no greater than 8 characters long or have spaces in them.
# Variable declarations
numeric lins, cols, csize, edist, value;
string type$, frag$, tempFile$, fragout$;
class File outFile;
# Setup rasters
raster Rin, Rmask;
GetInputRasters(Rin, Rmask);
IgnoreNull(Rin);
IgnoreNull(Rmask);
lins = NumLins(Rin);
cols = NumCols(Rin);
frag$ = GetInputFileName("c:/tnt/win32/fragstat.exe", "Please locate the fragstat executable.", "exe");
if (FileNameGetName(frag$)=="")
goto cleanup;
tempFile$ = CreateTempFileName();
string realout$ = "";
fragout$ = GetToken(GetOutputFileName(_context.ScriptDir, "Where would you like the results?", ""), ".", 0);
numeric i;
for i = 1 to NumberTokens(fragout$, "\") {
string temp$ = GetToken(fragout$, "\", i);
if (strlen(temp$)>8 or NumberTokens(temp$, " ")>1) {
realout$ = fragout$;
fragout$ = FileNameGetPath(tempFile$)+FileNameGetName(tempFile$);
break;
}
}
if (FileNameGetName(fragout$)=="")
goto cleanup;
csize = (LinScale(Rin) + ColScale(Rin)) / 2;
edist = PopupNum("Enter the edge distance in meters:");
# Write to text file
outFile = fopen(tempFile$);
# Apply the mask for fragstats' use
value = 32071;
numeric row, column;
for row = 1 to lins step 1 {
for column = 1 to cols step 1 {
if (Rmask[row, column] == -1) then
fprintf(outFile, "%d ", -value);
if (Rmask[row, column] == 0) then
fprintf(outFile, "%d ", Rin[row, column]);
if (Rmask[row, column] == 1) then
fprintf(outFile, "%d ", value);
}
if (row != lins)
fprintf(outFile, "\n");
}
# Close files
fclose(outFile);
CloseRaster(Rin);
CloseRaster(Rmask);
# Run fragstats
run(sprintf("%s %s %s %d %d 2 %d %d %d $ $ $ $ $ y y y y y", frag$, tempFile$, fragout$, csize, edist, lins, cols, value), 1);
if (realout$ != "") {
CopyFile(fragout$+".cla", realout$+".cla");
DeleteFile(fragout$+".cla");
CopyFile(fragout$+".ful", realout$+".ful");
DeleteFile(fragout$+".ful");
CopyFile(fragout$+".lnd", realout$+".lnd");
DeleteFile(fragout$+".lnd");
CopyFile(fragout$+".pat", realout$+".pat");
DeleteFile(fragout$+".pat");
}
cleanup:
#Cleanup
#DeleteFile(Rout.$Info.Filename);
DeleteFile(tempFile$);