/************************************************************************************************
/*
/*&r DN2Temp.txt	
/*
/*By Glen Stichbury
/*g.stichbury@gmail.com
/*
/*
/************************************************************************************************
/*
/*
/*This AML script converts the digital numbers in Landsat7 ETM+ band 6 images to temperature in
/*degrees Celcius. The script is set to run through 3 times, once for each input in the original 
/*project. Increasing the number of inputs requires increasing the "do until" statement to 
/*include the new data.
/*
/*Algorythms based on Williams, D. (2008). Landsat 7 Science Data Users Handbook. 2008, 
/*				from http://landsathandbook.gsfc.nasa.gov/handbook.html
/*
/*Created Sep 2008
/* 												
/*
/************************************************************************************************														
/*
/*Notes:
/*
/*Variables required for this script to run are .inlocation = location of .img inputs, 
/*.outlocation = data output location, .clip = coverage for image rectifying area, .n = number
/*of cycles (number of datasets + 1)
/* 
/************************************************************************************************

&sv .inlocation E:\Glen\Masters\workstation\Landsat\images
&sv .outlocation E:\Glen\Masters\workstation\Landsat\Thermal
&sv .clip E:\Glen\Masters\workstation\Thermal\grids\clip

&sv .n 1
&do &until %.n% = 4

&if %.n% = 1 &then &sv .number le7052116000136150 
&if %.n% = 1 &then &sv .stack le7052
&if %.n% = 2 &then &sv .number le7056116000132551
&if %.n% = 2 &then &sv .stack le7056
&if %.n% = 3 &then &sv .number le7057116009935950 
&if %.n% = 3 &then &sv .stack le7057
&type %.n%  %.number% %.stack%

&call workspace
&call rectify
&call imagegrid
&call DN2Temp
&call Select

&sv .n = %.n% + 1
&end

&call mean

&if [show program] ne ARC &then quit
&type +++AML COMPLETE+++

&return
/************************************************************************************************
&routine workspace

&if [show program] ne ARC &then quit

&if [exists %.outlocation% -workspace] &then
&type +++Workspace %.outlocation% Exists+++
&else createworkspace %.outlocation%
&if [exists %.outlocation%/Grids -workspace] &then
&type +++Workspace %.outlocation%/Grids Exists+++
&else createworkspace %.outlocation%/Grids
&if [exists %.outlocation%/Grids/%.number% -workspace] &then
&type +++Workspace %.outlocation%/Grids/%.number% Exists+++
&else createworkspace %.outlocation%/Grids/%.number%
&if [exists %.outlocation%/Radiance -workspace] &then
&type +++Workspace %.outlocation%/Radiance Exists+++
&else createworkspace %.outlocation%/Radiance


&return
/************************************************************************************************
&routine Rectify

&if [show program] ne ARC &then quit

&if [exist %.outlocation%/%.number%.img -file] ~
  &then &sys erase %.outlocation%/%.number%.img

&type +++Rectifying %.number%+++
rectify %.inlocation%.\%.number% %.outlocation%\%.number% # # %.clip%
&return

/************************************************************************************************
&routine imagegrid

&if [show program] ne ARC &then quit

&if [exist %.outlocation%/Grids/%.number%/%.stack% -grid] ~
  &then kill %.outlocation%/Grids/%.number%/%.stack% all

&type +++Converting %.number% to Grid+++
imagegrid %.outlocation%\%.number%.img %.outlocation%/Grids/%.number%/%.stack%

&return
/************************************************************************************************
&routine DN2Temp

&if [show program] ne GRID &then &do
&if [show program] ne ARC &then quit
grid
&end

&if [exist %.outlocation%/Radiance/%.stack% -grid] ~
  &then kill %.outlocation%/Radiance/%.stack% all

%.outlocation%/Radiance/%.stack% = ((12.65 - 3.2)/(255 - 1)) * ~
(%.outlocation%/Grids/%.number%/%.stack%c2 - 1) + 3.2

&if [exist %.outlocation%/%.stack%_cel -grid] ~
  &then kill %.outlocation%/%.stack%_cel all

%.outlocation%/%.stack%_cel = (1282.71 / (ln(666.09 / ~
%.outlocation%/Radiance/%.stack% + 1))) - 273.15


&return
/************************************************************************************************
&routine Select

&if [show program] ne GRID &then &do
&if [show program] ne ARC &then quit
grid
&end

&if [exist %.outlocation%/%.stack%_sel -grid] ~
  &then kill %.outlocation%/%.stack%_sel all

%.outlocation%/%.stack%_sel = select(%.outlocation%/%.stack%_cel, 'value gt -33.6018')

&return
/************************************************************************************************
&routine mean

&if [show program] ne GRID &then &do
&if [show program] ne ARC &then quit
grid
&end

&if [exist %.outlocation%/Temp_mean -grid] ~
  &then kill %.outlocation%/Temp_mean all

%.outlocation%/Temp_mean = mean(%.outlocation%/le7052_sel, ~
%.outlocation%/le7056_sel, %.outlocation%/le7057_sel)

&return
/************************************************************************************************


