home products news downloads documentation support gallery online maps resellers search
TNTmips Downloads Menu

HOME

CONTACT US

CURRENT RELEASE
  TNT 2013

DEVELOPMENT VERSION
  TNT 2014

TNTmips Pro
PRIOR RELEASES
  TNT 2012

FREE SOFTWARE
  TNTmips Free
  TNTatlas
  TNTsdk

MORE DOWNLOADS
  HASP Key Driver
  Screen Recorder
  TNT Language Kits
  Sample Geodata
  TNT Scripts

DOCUMENTATION
  TNTmips Tutorials
  Tutorial Datasets
  Technical Guides
  Scripts
  Quick Guides

MORE INFO
  Download FAQs
  FTP
  Download Managers
  Find Reseller

SITE MAP


LandsatCalibrationPilot.sml


##################################################################################################################
# DiskFile    : LandsatCalibrationPilot.sml
# Programmer  : Ralf Koller
# Created     : 09.10.2003
# Revisions   : 1.0 rc2
# Coming next : 1)  Slimming the code when the feature passing arrays to functions is implented by microimages
#               2)  A refined master selection routine... So far the routine is vulnerable to clouds in scene
#                   according to them the spread could be stilted...
#               3)  The possibility to choose the number of input scenes
#               4)  Adding the possibilty to select between (D)ark (O)bject (S)ubstraction 1-3
#               5)  The possibility to write the console output directly into a textfile
#               6)  If a under number of PIFs was found, the possibility to reenter their number via a pop-up
#               7)  The Implementation of a calibration routine for Landsat TM 4 sensors
#               8)  So far only congruent scenes get calibrated, but a future aim is to implent the BRDF correction
#                   so that it will be possible to normalize each part of a mosaiced scene too. Additionaly a auto-
#                   selection between PIF (for congruent) and BRDF (for mosaiced scenes) routines will be also pro-
#                   vided and if necessary the user will be able to select how many steps to perform. So it will be
#                   also possible to perform only the radiance raster computation or only a reflectance raster 
#                   output
#               9)  To pipeline all the routines into a externe c programm to slim and fasten up the whole compu-
#                   tation processes
#               10) The possibility to process scenes with a different cell size
#               11) To lookup the necessary calibration values directly out of the headerfiles
#
##################################################################################################################
#
# Function    : At first the script computes age adjusted radiance and atmosphere corrected (DOS 1) reflectance
#               rasters. In the next step four soil brightness index rasters are computed and the spreads are
#               compared. The scene with the widest spread becomes the master scene (In a mixed setup - MSS to-
#               gether with TM and/or ETM - only TM and ETM are able to become the new master). Now each scene
#               gets sorted and the dark and bright histogram tails which are similar to the invariant features
#               soil and water, are written to PIF masks. Only pixels which are available in all scenes or scene
#               pairs are taken into account. In the next step a user defined number of pseudo invariant features
#               is chosen randomly. Those are taken into account for the calculation of the regression coefficents,
#               correlation coefficents, the level of confidence and the standard deviation of the regression co-
#               efficients. So there is still the possibilty to rerun the regression calculation in a statistic
#               programm like SPSS or R in the case that the control values indicate a further investigation. In
#               the end the reflectance scene of the master is transfered into the output scene. Additionally the
#               slave scenes are adjusted via the regression coefficents and are also written into the userdefined
#               outputfiles. In the case MSS scenes were processed with a TM or ETM master scene only MSS band 1,
#               2 and 4 got normalized. Band 3 is just a simple not normalized reflectance band...
#
##################################################################################################################
#
# Usage       : - You have to enter 4 scenes. In the case you have less than 4 scenes just double enter one of
#                 them. If the double entered scene becomes the slave scene, everything should be ok, other-
#                 wise let me know and i'll send you a modified version of the script.
#               - All used scenes must have the same cell and image size while covering the same area
#
##################################################################################################################
#
# Credits     : Main Code Assistance - Wolfgang Riedl (Siemens A&D ATS 14 - Germany)
#               Shell Sort Algorithm - Dan Glasser (Tech Support - Microimages Inc. - Usa)
#
##################################################################################################################

##################################################################################################################
# 
# References  : Price, J.C. (1987): Calibration of Satellite Radiometers and the Comparison of Vegetation
#               Indices. In: Remote Sensing Of Environment, 21, p. 15-27
#
#               Samimi, C. (2003): Das Weidepotential im Gutu Distrikt (Zimbabwe) - Möglichkeiten und Grenzen
#               Modellierung unter Verwendung von Landsat TM-5. In: Karlsruher Schriften zur Geographie und 
#               Geoökologie, 19
#
#               Fedosejevs, G., Teillet, P.M. (1995): On the dark target approach to atmospheric correction of
#               remotely sensed data. In: Canadian Journal of Remote Sensing, 21, 4, p. 374-387
#		     
#               Teillet, P.M., Helder, D.L., Markham, B.L., Barker, J.L., Thome, K.J., Morfitt, R., Schott, J.R., 
#               Palluconi, F.D. (2001): A Lifetime Radiometric Calibration Record for the Landsat Thematic Mapper.
#               In: Proceedings of the 23rd Canadian Symposium on Remote Sensing. Quebec.
#
#               Song, C., Woodcock, C.E., Seto, K. C., Lenney M.P., Macomber S. A. (2001): Classification and
#               Change Detection Using Landsat TM Data: When and How to Correct Atmospheric Effects?. In: Remote
#               Sensing of Environment, 75, p. 230-244
#
#               Muramatsu, K., Furumi, S., Fujiwara, N. Hayashi, A., Daigo, M., Ochiai, F. (2000): Pattern 
#               decomposition method in the albedo space for Landsat TM and MSS data analysis. In: International
#               Journal of Remote Sensing, 21, 1, p. 99-119
#
#               Schott, J.R., Salvaggio, C., Volchok, W.J. (1988): Radiometric Scene Normalization Using Pseudo
#               Invariant Features. In: Remote Sensing Of Environment, 26, p. 1-16
#
#
#               ETM zones see: 
#               http://ltpwww.gsfc.nasa.gov/IAS/handbook/handbook_htmls/chapter6/chapter6.html#section6.4.2
#
##################################################################################################################


##################################################################################################################
#
# DISCLAIMER  : The usage of the script is for free but a commercial adoption isn't permitted. Please be so kind
#               to contact me and inform me if you have used or are using the script. Suggestions for optimization
#               purpose and submission of bug reports are also welcome. Thanks a lot... r.
#
# Contact     : Ralf Koller  ( rpk@gmx.net )
#               Student of Geography - FAU Erlangen-Nuernberg - Germany
#               Supervising: PD Dr. Cyrus Samimi
#
##################################################################################################################

$warnings 3

##############################################
######### Global Variable Declaration ########
##############################################

class XMLDOC docmain;                                               # class instance for XML text containing main dialog specification
class XMLNODE nodemain;                                             # XMLNODE for main dialog window
class GUI_DLG dlgmain;                                              # dialog class for main dialog window

class XMLDOC docs1;                                                 # class instance for XML text contain Scene 1 dialog specification
class XMLNODE nodes1;                                               # XMLNODE for Scene 1 dialog
class GUI_DLG dlgs1;	                                            # dialog class for Scene 1 dialog

class XMLDOC docs2;                                                 # class instance for XML text contain Scene 2 dialog specification
class XMLNODE nodes2;                                               # XMLNODE for Scene 2 dialog
class GUI_DLG dlgs2;	                                            # dialog class for Scene 2 dialog

class XMLDOC docs3;                                                 # class instance for XML text contain Scene 3 dialog specification
class XMLNODE nodes3;                                               # XMLNODE for Scene 3 dialog
class GUI_DLG dlgs3;	                                            # dialog class for Scene 3 dialog

class XMLDOC docs4;                                                 # class instance for XML text contain Scene 4 dialog specification
class XMLNODE nodes4;                                               # XMLNODE for Scene 4 dialog
class GUI_DLG dlgs4;	                                            # dialog class for Scene 4 dialog

class DATETIME s1yyyymmdd;
class DATETIME s2yyyymmdd;
class DATETIME s3yyyymmdd;
class DATETIME s4yyyymmdd;

array accumulate[12];                                               # Norm Year Value Array
array accumulateLY[12];                                             # Leap Year Value Array

numeric ESMSS11, ESMSS12, ESMSS13, ESMSS14;
numeric ESMSS41, ESMSS42, ESMSS43, ESMSS44;
numeric ESMSS51, ESMSS52, ESMSS53, ESMSS54;
numeric ESTM1, ESTM2, ESTM3, ESTM4, ESTM5, ESTM7; 
numeric ESETM1, ESETM2, ESETM3, ESETM4, ESETM5, ESETM7;
numeric QCALMAX, QCALMIN;
numeric B1LGLMINBef2000, B2LGLMINBef2000, B3LGLMINBef2000, B4LGLMINBef2000, B5LGLMINBef2000, B7LGLMINBef2000;
numeric B1LGLMAXBef2000, B2LGLMAXBef2000, B3LGLMAXBef2000, B4LGLMAXBef2000, B5LGLMAXBef2000, B7LGLMAXBef2000;
numeric B1HGLMINBef2000, B2HGLMINBef2000, B3HGLMINBef2000, B4HGLMINBef2000, B5HGLMINBef2000, B7HGLMINBef2000;
numeric B1HGLMAXBef2000, B2HGLMAXBef2000, B3HGLMAXBef2000, B4HGLMAXBef2000, B5HGLMAXBef2000, B7HGLMAXBef2000;
numeric B1LGLMINAFT2000, B2LGLMINAFT2000, B3LGLMINAFT2000, B4LGLMINAFT2000, B5LGLMINAFT2000, B7LGLMINAFT2000;
numeric B1LGLMAXAFT2000, B2LGLMAXAFT2000, B3LGLMAXAFT2000, B4LGLMAXAFT2000, B5LGLMAXAFT2000, B7LGLMAXAFT2000;
numeric B1HGLMINAFT2000, B2HGLMINAFT2000, B3HGLMINAFT2000, B4HGLMINAFT2000, B5HGLMINAFT2000, B7HGLMINAFT2000;
numeric B1HGLMAXAFT2000, B2HGLMAXAFT2000, B3HGLMAXAFT2000, B4HGLMAXAFT2000, B5HGLMAXAFT2000, B7HGLMAXAFT2000;
numeric L1B1gain, L1B2gain, L1B3gain, L1B4gain;
numeric L1B1bias; 
numeric L2B1bef75gain, L2B2bef75gain, L2B3bef75gain, L2B4bef75gain;
numeric L2B1bef75bias, L2B2bef75bias, L2B3bef75bias, L2B4bef75bias;
numeric L2B1aft75gain, L2B2aft75gain, L2B3aft75gain, L2B4aft75gain;
numeric L2B1aft75bias, L2B2aft75bias, L2B3aft75bias, L2B4aft75bias;
numeric L2B1aft79gain, L2B2aft79gain, L2B3aft79gain, L2B4aft79gain;
numeric L2B1aft79bias, L2B2aft79bias, L2B3aft79bias, L2B4aft79bias;
numeric L3B1bef78gain, L3B2bef78gain, L3B3bef78gain, L3B4bef78gain;
numeric L3B1bef78bias, L3B2bef78bias, L3B3bef78bias, L3B4bef78bias;
numeric L3B17879gain, L3B27879gain, L3B37879gain, L3B47879gain;
numeric L3B17879bias, L3B27879bias, L3B37879bias, L3B47879bias;
numeric L3B1aft79gain, L3B2aft79gain, L3B3aft79gain, L3B4aft79gain;
numeric L3B1aft79bias, L3B2aft79bias, L3B3aft79bias, L3B4aft79bias;
numeric L4B1bef83gain, L4B2bef83gain, L4B3bef83gain, L4B4bef83gain;
numeric L4B1bef83bias, L4B2bef83bias, L4B3bef83bias, L4B4bef83bias;
numeric L4B1aft83gain, L4B2aft83gain, L4B3aft83gain, L4B4aft83gain;
numeric L4B1aft83bias, L4B2aft83bias, L4B3aft83bias, L4B4aft83bias;
numeric L5B1bef84gain, L5B2bef84gain, L5B3bef84gain, L5B4bef84gain;
numeric L5B1bef84bias, L5B2bef84bias, L5B3bef84bias, L5B4bef84bias;
numeric L5B1aft84gain, L5B2aft84gain, L5B3aft84gain, L5B4aft84gain;
numeric L5B1aft84bias, L5B2aft84bias, L5B3aft84bias, L5B4aft84bias;

numeric biasnewTMb1, biasnewTMb2, biasnewTMb3, biasnewTMb4, biasnewTMb5, biasnewTMb7;

numeric i, j, m, n, o;
numeric ETMlins, ETMcols, TMlins, TMcols, MSSlins, MSScols;                                 # number of lines and columns in input raster set
numeric Scene1Lin, Scene2Lin, Scene3Lin, Scene4Lin;                                         # vertical size of a input raster cell
numeric Scene1Col, Scene2Col, Scene3Col, Scene4Col;                                         # horizontal size of a input raster cell
numeric sensors1, sensors2, sensors3, sensors4;				                    # sensor identification number (Differencing TM and 5 MSS sensor models)

string xmlmain$, xmls1$, xmls2$, xmls3$, xmls4$;                                            # string IDs for main and parameter windows

numeric err, err1, err2, err3, err4;						            # error numeric variables
numeric ret, ret1, ret2, ret3, ret4;						            # return numeric variables
numeric createMETA;

numeric earSurCat1, earSurCat2, earSurCat3, earSurCat4;		                            # Earth Surface Categories Identification numers
numeric s1aq, s2aq, s3aq, s4aq;                             	                            # numeric input for Aquisition Date of the scene
numeric s1ele, s2ele, s3ele, s4ele;   				                            # numeric input for sun elevation
numeric s1zenith, s2zenith, s3zenith, s4zenith;                                             # sun zenith
numeric s1zenithSIMPLE, s2zenithSIMPLE, s3zenithSIMPLE, s4zenithSIMPLE;
numeric s1leap, s2leap, s3leap, s4leap;
numeric s1jul, s2jul, s3jul, s4jul;                                                         # Julian date
numeric s1year, s2year, s3year, s4year;                                                     # Year number extracted from sxaqui
numeric s1month, s2month, s3month, s4month;                                                 # Month number extracted from sxaqui
numeric s1day, s2day, s3day, s4day;                                                         # Day number extracted from sxaqui
numeric dist1, dist2, dist3, dist4;                                                         # Earth Sundistance

numeric pifDarkVAL, pifBrightVAL, minPifVAL, histoTailInputVAL;                             # numeric input for PIF normalization
numeric sbi1min, sbi1max, sbi1mean, sbi1sd;                    			                    # numeric values for SBI 1 raster statistics
numeric sbi2min, sbi2max, sbi2mean, sbi2sd;                    			                    # numeric values for SBI 2 raster statistics
numeric sbi3min, sbi3max, sbi3mean, sbi3sd;                      			                # numeric values for SBI 3 raster statistics
numeric sbi4min, sbi4max, sbi4mean, sbi4sd;                         		                # numeric values for SBI 4 raster statistics
numeric sbi1spread, sbi2spread, sbi3spread, sbi4spread;				                        # sbi max min differences

numeric slaveMaster = 0; 						                                         	# Master 1=scene1 2=scene2 3=scene3 4=scene4
		                               
##############################################
######### Global Constant Declaration ########
##############################################

accumulate[1] = 0;                                                                          # Norm Year Values
accumulate[2] = 31;
accumulate[3] = 59;
accumulate[4] = 90;
accumulate[5] = 120;
accumulate[6] = 151;
accumulate[7] = 181;
accumulate[8] = 212;
accumulate[9] = 243;
accumulate[10] = 273;
accumulate[11] = 304;
accumulate[12] = 334;

accumulateLY[1] = 0;                                                                        # Leap Year Values
accumulateLY[2] = 31;
accumulateLY[3] = 60;
accumulateLY[4] = 91;
accumulateLY[5] = 121;
accumulateLY[6] = 152;
accumulateLY[7] = 182;
accumulateLY[8] = 213;
accumulateLY[9] = 244;
accumulateLY[10] = 274;
accumulateLY[11] = 305;
accumulateLY[12] = 335;

biasnewTMb1 = 2.523;												                        #Lifetime Biases for Landsat TM5
biasnewTMb2 = 2.417;
biasnewTMb3 = 1.452;
biasnewTMb4 = 1.854;
biasnewTMb5 = 3.423;
biasnewTMb7 = 2.633;

ESMSS11 = 1844.114888;                                                                      # ES Values for Landsat MSS1, MSS2, MSS3 - Price 1988
ESMSS12 = 1573.937919;
ESMSS13 = 1288.052988;
ESMSS14 = 901.6370916;				

ESMSS41 = 1834.69011;                                                                        # ES Values for Landsat MSS4 - Price 1988
ESMSS42 = 1595.929068;
ESMSS43 = 1266.061839;
ESMSS44 = 867.0795724;

ESMSS51 = 1834.69011;                                                                        # ES Values for Landsat MSS5 - Price 1988
ESMSS52 = 1595.929068;
ESMSS53 = 1262.920247;
ESMSS54 = 863.9379797;

ESTM1 = 1959.2;                                                                             # ES Values for Landsat TM - Fedosejevs 1995
ESTM2 = 1827.4;
ESTM3 = 1550.0;
ESTM4 = 1040.8;
ESTM5 = 220.75;
ESTM7 = 74.960;

ESETM1 = 1969.000;						                                            		# ES Values for Landsat ETM - User Handbook
ESETM2 = 1840.000;
ESETM3 = 1551.000;
ESETM4 = 1044.000;
ESETM5 = 225.700;
ESETM7 = 82.07;

QCALMAX = 255;							                                                   	# Maximum digital value for Landsat ETM
QCALMIN = 1;									                                            # Minimum digital value for Landsat ETM

B1LGLMINBef2000 = -6.2;							                                            # LOW GAIN BEFORE 2000 (LMIN)
B2LGLMINBef2000 = -6.0;
B3LGLMINBef2000 = -4.5;
B4LGLMINBef2000 = -4.5;
B5LGLMINBef2000 = -1.0;
B7LGLMINBef2000 = -0.35;

B1LGLMAXBef2000 = 297.5;							                                        # LOW GAIN BEFORE 2000 (LMAX)
B2LGLMAXBef2000 = 303.4;
B3LGLMAXBef2000 = 235.5;
B4LGLMAXBef2000 = 235.0;
B5LGLMAXBef2000 = 47.7;
B7LGLMAXBef2000 = 16.6;

B1HGLMINBef2000 = -6.2;							                                            #HIGH GAIN BEFORE 2000 (LMIN)
B2HGLMINBef2000 = -6.0;
B3HGLMINBef2000 = -4.5;
B4HGLMINBef2000 = -4.5;
B5HGLMINBef2000 = -1.0;
B7HGLMINBef2000 = -0.35;

B1HGLMAXBef2000 = 194.3;							                                         #HIGH GAIN BEFORE 2000 (LMAX)
B2HGLMAXBef2000 = 202.4;
B3HGLMAXBef2000 = 158.6;
B4HGLMAXBef2000 = 157.5;
B5HGLMAXBef2000 = 31.76;
B7HGLMAXBef2000 = 10.932;

B1LGLMINAFT2000 = -6.2;							                                              #LOW GAIN AFTER 2000 (LMIN)
B2LGLMINAFT2000 = -6.4;
B3LGLMINAFT2000 = -5.0;
B4LGLMINAFT2000 = -5.1;
B5LGLMINAFT2000 = -1.0;
B7LGLMINAFT2000 = -0.35;

B1LGLMAXAFT2000 = 293.7;							                                          #LOW GAIN AFTER 2000 (LMAX)
B2LGLMAXAFT2000 = 300.9;
B3LGLMAXAFT2000 = 234.4;
B4LGLMAXAFT2000 = 241.1;
B5LGLMAXAFT2000 = 47.57;
B7LGLMAXAFT2000 = 16.54;

B1HGLMINAFT2000 = -6.2;						                                                  #HIGH GAIN AFTER 2000 (LMIN)
B2HGLMINAFT2000 = -6.4;
B3HGLMINAFT2000 = -5.0;
B4HGLMINAFT2000 = -5.1;
B5HGLMINAFT2000 = -1.0;
B7HGLMINAFT2000 = -0.35;

B1HGLMAXAFT2000 = 191.6;							                                          #HIGH GAIN AFTER 2000 (LMAX)
B2HGLMAXAFT2000 = 196.5;
B3HGLMAXAFT2000 = 152.9;
B4HGLMAXAFT2000 = 157.4;
B5HGLMAXAFT2000 = 31.06;
B7HGLMAXAFT2000 = 10.80;

L1B1gain = 1.95;  	                                                                          # Landsat1 Gain and bias calibration constants
L1B2gain = 1.57;	 
L1B3gain = 1.39;  
L1B4gain = 2.43;	 

L1B1bias = 0.0;

L2B1bef75gain = 1.57;                                                                         # Landsat2 Gain and bias calibration constants before 16.Juli7
L2B2bef75gain = 1.17;
L2B3bef75gain = 1.05;
L2B4bef75gain = 2.12;

L2B1bef75bias = 10;
L2B2bef75bias = 7;
L2B3bef75bias = 7;
L2B4bef75bias = 5;

L2B1aft75gain = 2.01;                                                                        # Landsat2 Gain and bias calibration constants after 16.Juli75
L2B2aft75gain = 1.34;
L2B3aft75gain = 1.15;
L2B4aft75gain = 2.01;

L2B1aft75bias = 8;
L2B2aft75bias = 6;
L2B3aft75bias = 6;
L2B4aft75bias = 4;

L2B1aft79gain = 2.01;                                                                        # Landsat2 Gain and bias calibration constants after 1.Feb79
L2B2aft79gain = 1.34;
L2B3aft79gain = 1.15;
L2B4aft79gain = 1.01;

L2B1aft79bias = 8;
L2B2aft79bias = 6;
L2B3aft79bias = 6;
L2B4aft79bias = 4;

L3B1bef78gain = 1.70;                                                                         # Landsat3 Gain and bias calibration constants Before1Jun78
L3B2bef78gain = 1.35;
L3B3bef78gain = 1.12;
L3B4bef78gain = 2.32;

L3B1bef78bias = 4;
L3B2bef78bias = 3;
L3B3bef78bias = 3;
L3B4bef78bias = 1;

L3B17879gain = 2.01;                                                                        # Landsat3 Gain and bias calibration constants 2Jun78 to 1Feb7
L3B27879gain = 1.39;
L3B37879gain = 1.15;
L3B47879gain = 2.02;

L3B17879bias = 4;
L3B27879bias = 3;
L3B37879bias = 3;
L3B47879bias = 1;

L3B1aft79gain = 2.01;                                                                       # Landsat3 Gain and bias calibration constants after 1.Feb79
L3B2aft79gain = 1.39;
L3B3aft79gain = 1.15;
L3B4aft79gain = 1.00;

L3B1aft79bias = 4;
L3B2aft79bias = 3;
L3B3aft79bias = 3;
L3B4aft79bias = 1;

L4B1bef83gain = 1.80;	                                                                  # Landsat4 Gain and bias calibration constants Before 1april83
L4B2bef83gain = 1.39;
L4B3bef83gain = 0.99;
L4B4bef83gain = 1.02;

L4B1bef83bias = 2;
L4B2bef83bias = 4;
L4B3bef83bias = 4;
L4B4bef83bias = 3;

L4B1aft83gain = 1.84;                                                                        # Landsat4 Gain and bias calibration constants after 1 april83
L4B2aft83gain = 1.26;
L4B3aft83gain = 1.08;
L4B4aft83gain = 0.88;

L4B1aft83bias = 4;
L4B2aft83bias = 4;
L4B3aft83bias = 5;
L4B4aft83bias = 4;

L5B1bef84gain = 2.08;	                                                                       # Landsat5 Gain and bias calibration constants before 9 Nov 84
L5B2bef84gain = 1.39;
L5B3bef84gain = 1.22;
L5B4bef84gain = 0.94;

L5B1bef84bias = 3;
L5B2bef84bias = 3;
L5B3bef84bias = 4;
L5B4bef84bias = 4;

L5B1aft84gain = 2.08;                                                                           # Landsat5 Gain and bias calibration constants after 9 Nov 84
L5B2aft84gain = 1.39;
L5B3aft84gain = 1.13;
L5B4aft84gain = 0.94;

L5B1aft84bias = 3;
L5B2aft84bias = 3;
L5B3aft84bias = 5;
L5B4aft84bias = 4;

#####################################################################
#####################################################################
################  Procedure Definitions  ############################ 
#####################################################################
#####################################################################

#####################################
### Callback procedure to initally disable OK-
### button on main dialog window when it opens
#####################################

proc OnOpenMain () {

	dlgmain.SetOkEnabled( 0 );

	}

##################################
### Procedure to select SZENE 1
##################################

proc SELECTS1 () 
{
	sensors1 = dlgmain.GetCtrlByID( "comps1" ).GetValueNum();

	if ( sensors1 == 7 )
	{
		raster ETM1BAND1, ETM1BAND2, ETM1BAND3, ETM1BAND4, ETM1BAND5, ETM1BAND7;  		# szene 1 ETM input bands
		GetInputRasters(ETM1BAND1, ETM1BAND2, ETM1BAND3, ETM1BAND4, ETM1BAND5, ETM1BAND7);
		ETMlins = NumLins(ETM1BAND1);
		ETMcols = NumCols(ETM1BAND1);
		Scene1Lin = LinScale(ETM1BAND1);
		Scene1Col = ColScale(ETM1BAND1);
	}
	else if ( sensors1 == 6 )
	{
		raster TM1BAND1, TM1BAND2, TM1BAND3, TM1BAND4, TM1BAND5, TM1BAND7; 	  		# szene 1 TM input bands
		GetInputRasters(TM1BAND1, TM1BAND2, TM1BAND3, TM1BAND4, TM1BAND5, TM1BAND7);
		TMlins = NumLins(TM1BAND1);
		TMcols = NumCols(TM1BAND1);
		Scene1Lin = LinScale(TM1BAND1);
		Scene1Col = ColScale(TM1BAND1);
	}
	else
	{
		raster MSS1BAND1, MSS1BAND2, MSS1BAND3, MSS1BAND4;                 	  		# szene 1 MSS input bands
		GetInputRasters(MSS1BAND1, MSS1BAND2, MSS1BAND3, MSS1BAND4);
		MSSlins = NumLins(MSS1BAND1);
		MSScols = NumCols(MSS1BAND1);
		Scene1Lin = LinScale(MSS1BAND1);
		Scene1Col = ColScale(MSS1BAND1);
	}
}

##################################
### Procedure to select SZENE 2
##################################

proc SELECTS2 () 
{
    sensors2 = dlgmain.GetCtrlByID( "comps2" ).GetValueNum();

    if ( sensors2 == 7 )
    {
	   raster ETM2BAND1, ETM2BAND2, ETM2BAND3, ETM2BAND4, ETM2BAND5, ETM2BAND7;  		# szene 2 ETM input bands
	   GetInputRasters(ETM2BAND1, ETM2BAND2, ETM2BAND3, ETM2BAND4, ETM2BAND5, ETM2BAND7);
           Scene2Lin = LinScale(ETM2BAND1);
	   Scene2Col = ColScale(ETM2BAND1);
           if ( ETMlins == 0 )
	   {
            ETMlins = NumLins(ETM2BAND1);
            ETMcols = NumCols(ETM2BAND1);
	   }
    }
    else if ( sensors2 == 6 )
    {
	   raster TM2BAND1, TM2BAND2, TM2BAND3, TM2BAND4, TM2BAND5, TM2BAND7; 	  		# szene 2 TM input bands
	   GetInputRasters(TM2BAND1, TM2BAND2, TM2BAND3, TM2BAND4, TM2BAND5, TM2BAND7);
	   Scene2Lin = LinScale(TM2BAND1);
	   Scene2Col = ColScale(TM2BAND1);
	   if (TMlins == 0)
	   {
	    TMlins = NumLins(TM2BAND1);
	    TMcols = NumCols(TM2BAND1);
	   }
    }
    else
    {
	   raster MSS2BAND1, MSS2BAND2, MSS2BAND3, MSS2BAND4;                 	  		# szene 2 MSS input bands
	   GetInputRasters(MSS2BAND1, MSS2BAND2, MSS2BAND3, MSS2BAND4);
	   Scene2Lin = LinScale(MSS2BAND1);
	   Scene2Col = ColScale(MSS2BAND1);
	   if ( MSSlins == 0 )
	   {
	    MSSlins = NumLins(MSS2BAND1);
	    MSScols = NumCols(MSS2BAND1);
	   }
	}
}

##################################
### Procedure to select SZENE 3
##################################

proc SELECTS3 () 
{

	sensors3 = dlgmain.GetCtrlByID( "comps3" ).GetValueNum();

       if (sensors3 == 7)
       {
           raster ETM3BAND1, ETM3BAND2, ETM3BAND3, ETM3BAND4, ETM3BAND5, ETM3BAND7;  		# szene 3 ETM input bands
	   GetInputRasters(ETM3BAND1, ETM3BAND2, ETM3BAND3, ETM3BAND4, ETM3BAND5, ETM3BAND7);
           Scene3Lin = LinScale(ETM3BAND1);
	   Scene3Col = ColScale(ETM3BAND1);
	   if (ETMlins == 0)
	   {
	     ETMlins = NumLins(ETM3BAND1);
             ETMcols = NumCols(ETM3BAND1);
	   }
       }
       
       else if ( sensors3 == 6 )
       {
           raster TM3BAND1, TM3BAND2, TM3BAND3, TM3BAND4, TM3BAND5, TM3BAND7; 	  		# szene 3 TM input bands
	   GetInputRasters(TM3BAND1, TM3BAND2, TM3BAND3, TM3BAND4, TM3BAND5, TM3BAND7);
	   Scene3Lin = LinScale(TM3BAND1);
	   Scene3Col = ColScale(TM3BAND1);
	   if (TMlins == 0)
	   {
	     TMlins = NumLins(TM3BAND1);
	     TMcols = NumCols(TM3BAND1);
	   }
	}
	
	else
	{
	   raster MSS3BAND1, MSS3BAND2, MSS3BAND3, MSS3BAND4;                 	  		# szene 3 MSS input bands
	   GetInputRasters(MSS3BAND1, MSS3BAND2, MSS3BAND3, MSS3BAND4);
	   Scene3Lin = LinScale(MSS3BAND1);
	   Scene3Col = ColScale(MSS3BAND1);
	   if (MSSlins == 0)
	   {
	     MSSlins = NumLins(MSS3BAND1);
	     MSScols = NumCols(MSS3BAND1);
	   }
	}
}

##################################
### Procedure to select SZENE 4
##################################

proc SELECTS4 () 
{
	sensors4 = dlgmain.GetCtrlByID( "comps4" ).GetValueNum();

	if ( sensors4 == 7 )
    	{
	   raster ETM4BAND1, ETM4BAND2, ETM4BAND3, ETM4BAND4, ETM4BAND5, ETM4BAND7;  		# szene 4 ETM input bands
	   GetInputRasters(ETM4BAND1, ETM4BAND2, ETM4BAND3, ETM4BAND4, ETM4BAND5, ETM4BAND7);
           Scene4Lin = LinScale(ETM4BAND1);
	   Scene4Col = ColScale(ETM4BAND1);
	   if ( ETMlins == 0 )
	   {
	     ETMlins = NumLins(ETM4BAND1);
             ETMcols = NumCols(ETM4BAND1);
	   }
	}
	else if ( sensors4 == 6 )
	{
	   raster TM4BAND1, TM4BAND2, TM4BAND3, TM4BAND4, TM4BAND5, TM4BAND7; 	  		# szene 4 TM input bands
	   GetInputRasters(TM4BAND1, TM4BAND2, TM4BAND3, TM4BAND4, TM4BAND5, TM4BAND7);
	   Scene4Lin = LinScale(TM4BAND1);
	   Scene4Col = ColScale(TM4BAND1);
	   if ( TMlins == 0 )
	   {
	     TMlins = NumLins(TM4BAND1);
	     TMcols = NumCols(TM4BAND1);
	   }
	}
	else
	{
	   raster MSS4BAND1, MSS4BAND2, MSS4BAND3, MSS4BAND4;                 	  		# szene 4 MSS input bands
	   GetInputRasters(MSS4BAND1, MSS4BAND2, MSS4BAND3, MSS4BAND4);
           Scene4Lin = LinScale(MSS4BAND1);
	   Scene4Col = ColScale(MSS4BAND1);
	   if ( MSSlins == 0 )
	   {
	     MSSlins = NumLins(MSS4BAND1);
	     MSScols = NumCols(MSS4BAND1);
	   }
	}
}

###################################################################################################
###### Procedure to prompt for values for SZENE 1
###################################################################################################

proc SPECS1 () {

if ( sensors1 == 7 )
{
	xmls1$ = '
	
		
		      
				
					
					
				
				
					
					
				
				
				
					Desert(hs)                              #hs = high sun ms=mid sun ls=low sun
					Desert(ms)
					Desert Ice(ls)
					Ice(hs)
					Land(hs)
					Land Ocean(ls)
					Water
					Night(volcanos)
				
				
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}
	
	earSurCat1 = dlgs1.GetCtrlByID( "esc1" ).GetValueNum();
	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();	
}

else if ( sensors1 == 6 ) 
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );	# pop up an error dialog
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	numeric s1TMg1, s1TMg2, s1TMg3, s1TMg4, s1TMg5, s1TMg7;
	numeric s1TMb1, s1TMb2, s1TMb3, s1TMb4, s1TMb5, s1TMb7;

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
	s1TMg1 = dlgs1.GetCtrlByID("ss1TMg1").GetValueNum();
	s1TMg2 = dlgs1.GetCtrlByID("ss1TMg2").GetValueNum();
	s1TMg3 = dlgs1.GetCtrlByID("ss1TMg3").GetValueNum();
	s1TMg4 = dlgs1.GetCtrlByID("ss1TMg4").GetValueNum();
	s1TMg5 = dlgs1.GetCtrlByID("ss1TMg5").GetValueNum();
	s1TMg7 = dlgs1.GetCtrlByID("ss1TMg7").GetValueNum();
	s1TMb1 = dlgs1.GetCtrlByID("ss1TMb1").GetValueNum();
	s1TMb2 = dlgs1.GetCtrlByID("ss1TMb2").GetValueNum();
	s1TMb3 = dlgs1.GetCtrlByID("ss1TMb3").GetValueNum();
	s1TMb4 = dlgs1.GetCtrlByID("ss1TMb4").GetValueNum();
	s1TMb5 = dlgs1.GetCtrlByID("ss1TMb5").GetValueNum();
	s1TMb7 = dlgs1.GetCtrlByID("ss1TMb7").GetValueNum();
}

else if ( sensors1 == 5 )
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );	# pop up an error dialog
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
}

else if ( sensors1 == 4 )
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
}

else if ( sensors1 == 3 )
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );	# pop up an error dialog
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
}

else if ( sensors1 == 2 )
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );	# pop up an error dialog
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
}

else if ( sensors1 == 1 )
{
	xmls1$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err1 = docs1.Parse(xmls1$);

	if ( err1 < 0 ) {
		PopupError( err1 );	# pop up an error dialog
		Exit( );
		}

	nodes1 = docs1.GetElementByID( "dlgs1" );

	if ( nodes1 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs1.SetXMLNode( nodes1 );

	ret1 = dlgs1.DoModal();

	if (ret1 == -1)
	{
		Exit();
	}

	s1aq = dlgs1.GetCtrlByID("s1aqui").GetValueNum();
	s1ele = dlgs1.GetCtrlByID("s1elevation").GetValueNum();
}
}

###################################################################################################
###### Procedure to prompt for values for SZENE 2
###################################################################################################

proc SPECS2 () {

if ( sensors2 == 7 )
{
	xmls2$ = '
	
		
		      
				
					
					
				
				
					
					
				
				
				
					Desert(hs)                              #hs = high sun ms=mid sun ls=low sun
					Desert(ms)
					Desert Ice(ls)
					Ice(hs)
					Land(hs)
					Land Ocean(ls)
					Water
					Night(volcanos)
				
				
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	earSurCat2 = dlgs2.GetCtrlByID( "esc2" ).GetValueNum();
	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}

else if ( sensors2 == 6 ) 
{
	xmls2$ = '
	
		
			
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	numeric s2TMg1, s2TMg2, s2TMg3, s2TMg4, s2TMg5, s2TMg7;
    numeric s2TMb1, s2TMb2, s2TMb3, s2TMb4, s2TMb5, s2TMb7;
    
	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
	s2TMg1 = dlgs2.GetCtrlByID("ss2TMg1").GetValueNum();
	s2TMg2 = dlgs2.GetCtrlByID("ss2TMg2").GetValueNum();
	s2TMg3 = dlgs2.GetCtrlByID("ss2TMg3").GetValueNum();
	s2TMg4 = dlgs2.GetCtrlByID("ss2TMg4").GetValueNum();
	s2TMg5 = dlgs2.GetCtrlByID("ss2TMg5").GetValueNum();
	s2TMg7 = dlgs2.GetCtrlByID("ss2TMg7").GetValueNum();
	s2TMb1 = dlgs2.GetCtrlByID("ss2TMb1").GetValueNum();
	s2TMb2 = dlgs2.GetCtrlByID("ss2TMb2").GetValueNum();
	s2TMb3 = dlgs2.GetCtrlByID("ss2TMb3").GetValueNum();
	s2TMb4 = dlgs2.GetCtrlByID("ss2TMb4").GetValueNum();
	s2TMb5 = dlgs2.GetCtrlByID("ss2TMb5").GetValueNum();
	s2TMb7 = dlgs2.GetCtrlByID("ss2TMb7").GetValueNum();
}

else if ( sensors2 == 5 )
{
	xmls2$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}

else if ( sensors2 == 4 )
{
	xmls2$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}

else if ( sensors2 == 3 )
{
	xmls2$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}

else if ( sensors2 == 2 )
{
	xmls2$ = '
	
		
			
				
					
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err );
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}

else if ( sensors2 == 1 )
{
	xmls2$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err2 = docs2.Parse(xmls2$);

	if ( err2 < 0 ) {
		PopupError( err2 );	# pop up an error dialog
		Exit( );
		}

	nodes2 = docs2.GetElementByID( "dlgs2" );

	if ( nodes2 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs2.SetXMLNode( nodes2 );

	ret2 = dlgs2.DoModal();

	if (ret2 == -1)
	{
		Exit();
	}

	s2aq = dlgs2.GetCtrlByID("s2aqui").GetValueNum();
	s2ele = dlgs2.GetCtrlByID("s2elevation").GetValueNum();
}
}

###################################################################################################
###### Procedure to prompt for values for SZENE 3
###################################################################################################

proc SPECS3 () {

if ( sensors3 == 7 )
{
	xmls3$ = '
	
		
		      
				
					
					
				
				
					
					
				
				
				
					Desert(hs)                              #hs = high sun ms=mid sun ls=low sun
					Desert(ms)
					Desert Ice(ls)
					Ice(hs)
					Land(hs)
					Land Ocean(ls)
					Water
					Night(volcanos)
				
				
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	earSurCat3 = dlgs3.GetCtrlByID( "esc3" ).GetValueNum();
	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();	
}

else if ( sensors3 == 6 ) 
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	numeric s3TMg1, s3TMg2, s3TMg3, s3TMg4, s3TMg5, s3TMg7;
	numeric s3TMb1, s3TMb2, s3TMb3, s3TMb4, s3TMb5, s3TMb7;

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
	s3TMg1 = dlgs3.GetCtrlByID("ss3TMg1").GetValueNum();
	s3TMg2 = dlgs3.GetCtrlByID("ss3TMg2").GetValueNum();
	s3TMg3 = dlgs3.GetCtrlByID("ss3TMg3").GetValueNum();
	s3TMg4 = dlgs3.GetCtrlByID("ss3TMg4").GetValueNum();
	s3TMg5 = dlgs3.GetCtrlByID("ss3TMg5").GetValueNum();
	s3TMg7 = dlgs3.GetCtrlByID("ss3TMg7").GetValueNum();
	s3TMb1 = dlgs3.GetCtrlByID("ss3TMb1").GetValueNum();
	s3TMb2 = dlgs3.GetCtrlByID("ss3TMb2").GetValueNum();
	s3TMb3 = dlgs3.GetCtrlByID("ss3TMb3").GetValueNum();
	s3TMb4 = dlgs3.GetCtrlByID("ss3TMb4").GetValueNum();
	s3TMb5 = dlgs3.GetCtrlByID("ss3TMb5").GetValueNum();
	s3TMb7 = dlgs3.GetCtrlByID("ss3TMb7").GetValueNum();
}

else if ( sensors3 == 5 )
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
}

else if ( sensors3 == 4 )
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialognode in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
}

else if ( sensors3 == 3 )
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
}

else if ( sensors3 == 2 )
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
}

else if ( sensors3 == 1 )
{
	xmls3$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err3 = docs3.Parse(xmls3$);

	if ( err3 < 0 ) {
		PopupError( err3 );
		Exit( );
		}

	nodes3 = docs3.GetElementByID( "dlgs3" );

	if ( nodes3 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs3.SetXMLNode( nodes3 );

	ret3 = dlgs3.DoModal();

	if (ret3 == -1)
	{
		Exit();
	}

	s3aq = dlgs3.GetCtrlByID("s3aqui").GetValueNum();
	s3ele = dlgs3.GetCtrlByID("s3elevation").GetValueNum();
}
}

###################################################################################################
##### Procedure to prompt for values for SZENE 4
###################################################################################################

proc SPECS4 () {

if ( sensors4 == 7 )
{
	xmls4$ = '
	
		
		      
				
					
					
				
				
					
					
				
				
				
					Desert(hs)                              #hs = high sun ms=mid sun ls=low sun
					Desert(ms)
					Desert Ice(ls)
					Ice(hs)
					Land(hs)
					Land Ocean(ls)
					Water
					Night(volcanos)
				
				
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	earSurCat4 = dlgs4.GetCtrlByID( "esc4" ).GetValueNum();
	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 6 ) 
{
	xmls4$ = '
	
		
			
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
			
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	numeric s4TMg1, s4TMg2, s4TMg3, s4TMg4, s4TMg5, s4TMg7;
	numeric s4TMb1, s4TMb2, s4TMb3, s4TMb4, s4TMb5, s4TMb7;

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	s4TMg1 = dlgs4.GetCtrlByID("ss4TMg1").GetValueNum();
	s4TMg2 = dlgs4.GetCtrlByID("ss4TMg2").GetValueNum();
	s4TMg3 = dlgs4.GetCtrlByID("ss4TMg3").GetValueNum();
	s4TMg4 = dlgs4.GetCtrlByID("ss4TMg4").GetValueNum();
	s4TMg5 = dlgs4.GetCtrlByID("ss4TMg5").GetValueNum();
	s4TMg7 = dlgs4.GetCtrlByID("ss4TMg7").GetValueNum();
	s4TMb1 = dlgs4.GetCtrlByID("ss4TMb1").GetValueNum();
	s4TMb2 = dlgs4.GetCtrlByID("ss4TMb2").GetValueNum();
	s4TMb3 = dlgs4.GetCtrlByID("ss4TMb3").GetValueNum();
	s4TMb4 = dlgs4.GetCtrlByID("ss4TMb4").GetValueNum();
	s4TMb5 = dlgs4.GetCtrlByID("ss4TMb5").GetValueNum();
	s4TMb7 = dlgs4.GetCtrlByID("ss4TMb7").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 5 )
{
	xmls4$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );	# pop up an error dialog
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 4 )
{
	xmls4$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );	# pop up an error dialog
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 3 )
{
	xmls4$ = '
	
		
				
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );	# pop up an error dialog
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 2 )
{
	xmls4$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );	# pop up an error dialog
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 ==0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}

else if ( sensors4 == 1 )
{
	xmls4$ = '
	
		
			
				
					
					
				
				
					
					
				
			
		
	';

	err4 = docs4.Parse(xmls4$);

	if ( err4 < 0 ) {
		PopupError( err4 );
		Exit( );
		}

	nodes4 = docs4.GetElementByID( "dlgs4" );

	if ( nodes4 == 0 ) {
		PopupMessage( "Could not find dialog node in XML document" );
		Exit();
		}

	dlgs4.SetXMLNode( nodes4 );

	ret4 = dlgs4.DoModal();

	if (ret4 == -1)
	{
		Exit();
	}

	s4aq = dlgs4.GetCtrlByID("s4aqui").GetValueNum();
	s4ele = dlgs4.GetCtrlByID("s4elevation").GetValueNum();
	dlgmain.GetCtrlByID("rasrefdoss1").SetEnabled(1);
}
}


#######################################
### Procedure to select reflectance output files SZENE 1 DOS 1 corrected
#######################################

proc OUTREFDOSS1()
{
if ( sensors1 == 7 ) 
    { 
	raster REFS1ETM1, REFS1ETM2, REFS1ETM3, REFS1ETM4, REFS1ETM5, REFS1ETM7;

	GetOutputRaster(REFS1ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND1, REFS1ETM1, "georef");

	GetOutputRaster(REFS1ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND2, REFS1ETM2, "georef");

	GetOutputRaster(REFS1ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND3, REFS1ETM3, "georef");

	GetOutputRaster(REFS1ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND4, REFS1ETM4, "georef");

	GetOutputRaster(REFS1ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND5, REFS1ETM5, "georef");

	GetOutputRaster(REFS1ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND7, REFS1ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss2").SetEnabled(1);
    }

else if ( sensors1 == 6 ) 
    { 
	raster REFS1TM1, REFS1TM2, REFS1TM3, REFS1TM4, REFS1TM5, REFS1TM7;

	GetOutputRaster(REFS1TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND1, REFS1TM1, "georef");

	GetOutputRaster(REFS1TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND2, REFS1TM2, "georef");

	GetOutputRaster(REFS1TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND3, REFS1TM3, "georef");

	GetOutputRaster(REFS1TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND4, REFS1TM4, "georef");

	GetOutputRaster(REFS1TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND5, REFS1TM5, "georef");

	GetOutputRaster(REFS1TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND7, REFS1TM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss2").SetEnabled(1);
    }

else 
    {
	raster REFS1MSS1, REFS1MSS2, REFS1MSS3, REFS1MSS4;

	GetOutputRaster(REFS1MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND1, REFS1MSS1, "georef");

	GetOutputRaster(REFS1MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND2, REFS1MSS2, "georef");

	GetOutputRaster(REFS1MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND3, REFS1MSS3, "georef");

	GetOutputRaster(REFS1MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND4, REFS1MSS4, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss2").SetEnabled(1);
	}
}

#######################################
### Procedure to select reflectance output files SZENE 2 DOS 1 corrected
#######################################

proc OUTREFDOSS2()
{
if ( sensors2 == 7 ) 
    { 
	raster REFS2ETM1, REFS2ETM2, REFS2ETM3, REFS2ETM4, REFS2ETM5, REFS2ETM7;

	GetOutputRaster(REFS2ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND1, REFS2ETM1, "georef");

	GetOutputRaster(REFS2ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND2, REFS2ETM2, "georef");

	GetOutputRaster(REFS2ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND3, REFS2ETM3, "georef");

	GetOutputRaster(REFS2ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND4, REFS2ETM4, "georef");

	GetOutputRaster(REFS2ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND5, REFS2ETM5, "georef");

	GetOutputRaster(REFS2ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND7, REFS2ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss3").SetEnabled(1);
    }

else if ( sensors2 == 6 ) 
    { 
	raster REFS2TM1, REFS2TM2, REFS2TM3, REFS2TM4, REFS2TM5, REFS2TM7;

	GetOutputRaster(REFS2TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND1, REFS2TM1, "georef");

	GetOutputRaster(REFS2TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND2, REFS2TM2, "georef");

	GetOutputRaster(REFS2TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND3, REFS2TM3, "georef");

	GetOutputRaster(REFS2TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND4, REFS2TM4, "georef");

	GetOutputRaster(REFS2TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND5, REFS2TM5, "georef");

	GetOutputRaster(REFS2TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND7, REFS2TM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss3").SetEnabled(1);
    }

else 
    {
	raster REFS2MSS1, REFS2MSS2, REFS2MSS3, REFS2MSS4;

	GetOutputRaster(REFS2MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND1, REFS2MSS1, "georef");

	GetOutputRaster(REFS2MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND2, REFS2MSS2, "georef");

	GetOutputRaster(REFS2MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND3, REFS2MSS3, "georef");

	GetOutputRaster(REFS2MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND4, REFS2MSS4, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss3").SetEnabled(1);
	}
}

#######################################
### Procedure to select reflectance output files SZENE 3 DOS 1 corrected
#######################################

proc OUTREFDOSS3()
{
if ( sensors3 == 7 ) 
    { 
	raster REFS3ETM1, REFS3ETM2, REFS3ETM3, REFS3ETM4, REFS3ETM5, REFS3ETM7;

	GetOutputRaster(REFS3ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND1, REFS3ETM1, "georef");

	GetOutputRaster(REFS3ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND2, REFS3ETM2, "georef");

	GetOutputRaster(REFS3ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND3, REFS3ETM3, "georef");

	GetOutputRaster(REFS3ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND4, REFS3ETM4, "georef");

	GetOutputRaster(REFS3ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND5, REFS3ETM5, "georef");

	GetOutputRaster(REFS3ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND7, REFS3ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss4").SetEnabled(1);
    }

else if ( sensors3 == 6 ) 
    { 
	raster REFS3TM1, REFS3TM2, REFS3TM3, REFS3TM4, REFS3TM5, REFS3TM7;

	GetOutputRaster(REFS3TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND1, REFS3TM1, "georef");

	GetOutputRaster(REFS3TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND2, REFS3TM2, "georef");

	GetOutputRaster(REFS3TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND3, REFS3TM3, "georef");

	GetOutputRaster(REFS3TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND4, REFS3TM4, "georef");

	GetOutputRaster(REFS3TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND5, REFS3TM5, "georef");

	GetOutputRaster(REFS3TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND7, REFS3TM7, "georef");
	
	dlgmain.GetCtrlByID("rasrefdoss4").SetEnabled(1);
    }

else 
    {
	raster REFS3MSS1, REFS3MSS2, REFS3MSS3, REFS3MSS4;

	GetOutputRaster(REFS3MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND1, REFS3MSS1, "georef");

	GetOutputRaster(REFS3MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND2, REFS3MSS2, "georef");

	GetOutputRaster(REFS3MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND3, REFS3MSS3, "georef");

	GetOutputRaster(REFS3MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND4, REFS3MSS4, "georef");

	dlgmain.GetCtrlByID("rasrefdoss4").SetEnabled(1);
	}
}

#######################################
### Procedure to select reflectance output files SZENE 4 DOS 1 corrected
#######################################

proc OUTREFDOSS4()
{
    if ( sensors4 == 7 ) 
    { 
	raster REFS4ETM1, REFS4ETM2, REFS4ETM3, REFS4ETM4, REFS4ETM5, REFS4ETM7;

	GetOutputRaster(REFS4ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND1, REFS4ETM1, "georef");

	GetOutputRaster(REFS4ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND2, REFS4ETM2, "georef");

	GetOutputRaster(REFS4ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND3, REFS4ETM3, "georef");

	GetOutputRaster(REFS4ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND4, REFS4ETM4, "georef");

	GetOutputRaster(REFS4ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND5, REFS4ETM5, "georef");

	GetOutputRaster(REFS4ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND7, REFS4ETM7, "georef");
	
	dlgmain.GetCtrlByID("raspifglob").SetEnabled(1);
    }

else if ( sensors4 == 6 ) 
    { 
	raster REFS4TM1, REFS4TM2, REFS4TM3, REFS4TM4, REFS4TM5, REFS4TM7;

	GetOutputRaster(REFS4TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND1, REFS4TM1, "georef");

	GetOutputRaster(REFS4TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND2, REFS4TM2, "georef");

	GetOutputRaster(REFS4TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND3, REFS4TM3, "georef");

	GetOutputRaster(REFS4TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND4, REFS4TM4, "georef");

	GetOutputRaster(REFS4TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND5, REFS4TM5, "georef");

	GetOutputRaster(REFS4TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND7, REFS4TM7, "georef");
	
	dlgmain.GetCtrlByID("raspifglob").SetEnabled(1);
    }

else 
    {
	raster REFS4MSS1, REFS4MSS2, REFS4MSS3, REFS4MSS4;

	GetOutputRaster(REFS4MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND1, REFS4MSS1, "georef");

	GetOutputRaster(REFS4MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND2, REFS4MSS2, "georef");

	GetOutputRaster(REFS4MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND3, REFS4MSS3, "georef");

	GetOutputRaster(REFS4MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND4, REFS4MSS4, "georef");

	dlgmain.GetCtrlByID("raspifglob").SetEnabled(1);
	}
}
#######################################
### Procedure to select PIF output masks
#######################################

proc OUTCOMPIFS1()
{
    if ( sensors1 == 7 )
	{
	raster PIFMASK, PIFdarkMASK, PIFbrightMASK;

	GetOutputRaster(PIFMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, PIFMASK, "georef");

	GetOutputRaster(PIFdarkMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, PIFdarkMASK, "georef");

	GetOutputRaster(PIFbrightMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, PIFbrightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs1").SetEnabled(1);
	}
    
    else if ( sensors1 == 6 ) 
	{
	raster PIFMASK, PIFdarkMASK, PIFbrightMASK;

	GetOutputRaster(PIFMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, PIFMASK, "georef");

	GetOutputRaster(PIFdarkMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, PIFdarkMASK, "georef");

	GetOutputRaster(PIFbrightMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, PIFbrightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs1").SetEnabled(1);
	}
    
    else 
	{
	raster PIFMASK, PIFdarkMASK, PIFbrightMASK;

	GetOutputRaster(PIFMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, PIFMASK, "georef");

	GetOutputRaster(PIFdarkMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, PIFdarkMASK, "georef");

	GetOutputRaster(PIFbrightMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, PIFbrightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs1").SetEnabled(1);
	}
}

proc OUTPIFS1()
{
    if ( sensors1 == 7 ) 
	{
	raster S1MASK, S1darkMASK, S1brightMASK;

	GetOutputRaster(S1MASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, S1MASK, "georef");

	GetOutputRaster(S1darkMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, S1darkMASK, "georef");

	GetOutputRaster(S1brightMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM1BAND1, S1brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs2").SetEnabled(1);
	}

    else if ( sensors1 == 6 ) 
	{
	raster S1MASK, S1darkMASK, S1brightMASK;

	GetOutputRaster(S1MASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, S1MASK, "georef");

	GetOutputRaster(S1darkMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, S1darkMASK, "georef");

	GetOutputRaster(S1brightMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM1BAND1, S1brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs2").SetEnabled(1);
	}
    
    else 
	{
	raster S1MASK, S1darkMASK, S1brightMASK;

	GetOutputRaster(S1MASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, S1MASK, "georef");

	GetOutputRaster(S1darkMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, S1darkMASK, "georef");

	GetOutputRaster(S1brightMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS1BAND1, S1brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs2").SetEnabled(1);
	}
}

proc OUTPIFS2()
{
    if ( sensors2 == 7 ) 
	{
	raster S2MASK, S2darkMASK, S2brightMASK;

	GetOutputRaster(S2MASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM2BAND1, S2MASK, "georef");

	GetOutputRaster(S2darkMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM2BAND1, S2darkMASK, "georef");

	GetOutputRaster(S2brightMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM2BAND1, S2brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs3").SetEnabled(1);
	}

    else if ( sensors2 == 6 ) 
	{
	raster S2MASK, S2darkMASK, S2brightMASK;

	GetOutputRaster(S2MASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM2BAND1, S2MASK, "georef");

	GetOutputRaster(S2darkMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM2BAND1, S2darkMASK, "georef");

	GetOutputRaster(S2brightMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM2BAND1, S2brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs3").SetEnabled(1);
	}
    else 
	{
	raster S2MASK, S2darkMASK, S2brightMASK;

	GetOutputRaster(S2MASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS2BAND1, S2MASK, "georef");

	GetOutputRaster(S2darkMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS2BAND1, S2darkMASK, "georef");

	GetOutputRaster(S2brightMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS2BAND1, S2brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs3").SetEnabled(1);
	}
}

proc OUTPIFS3()
{
    if ( sensors3 == 7 ) 
	{
	raster S3MASK, S3darkMASK, S3brightMASK;

	GetOutputRaster(S3MASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM3BAND1, S3MASK, "georef");

	GetOutputRaster(S3darkMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM3BAND1, S3darkMASK, "georef");

	GetOutputRaster(S3brightMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM3BAND1, S3brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs4").SetEnabled(1);
	}

    else if ( sensors3 == 6 ) 
	{
	raster S3MASK, S3darkMASK, S3brightMASK;

	GetOutputRaster(S3MASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM3BAND1, S3MASK, "georef");

	GetOutputRaster(S3darkMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM3BAND1, S3darkMASK, "georef");

	GetOutputRaster(S3brightMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM3BAND1, S3brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs4").SetEnabled(1);
	}
    
    else 
	{
	raster S3MASK, S3darkMASK, S3brightMASK;

	GetOutputRaster(S3MASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS3BAND1, S3MASK, "georef");

	GetOutputRaster(S3darkMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS3BAND1, S3darkMASK, "georef");

	GetOutputRaster(S3brightMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS3BAND1, S3brightMASK, "georef");
	
	dlgmain.GetCtrlByID("raspifs4").SetEnabled(1);
	}
}

proc OUTPIFS4()
{
if ( sensors4 == 7 ) 
	{
	raster S4MASK, S4darkMASK, S4brightMASK;

	GetOutputRaster(S4MASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM4BAND1, S4MASK, "georef");

	GetOutputRaster(S4darkMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM4BAND1, S4darkMASK, "georef");

	GetOutputRaster(S4brightMASK, ETMlins, ETMcols, "8-bit unsigned");
	CopySubobjects(ETM4BAND1, S4brightMASK, "georef");
	
	dlgmain.GetCtrlByID("rasnorms1").SetEnabled(1);
	}

    else if ( sensors4 == 6 ) 
	{
	raster S4MASK, S4darkMASK, S4brightMASK;

	GetOutputRaster(S4MASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM4BAND1, S4MASK, "georef");

	GetOutputRaster(S4darkMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM4BAND1, S4darkMASK, "georef");

	GetOutputRaster(S4brightMASK, TMlins, TMcols, "8-bit unsigned");
	CopySubobjects(TM4BAND1, S4brightMASK, "georef");
	
	dlgmain.GetCtrlByID("rasnorms1").SetEnabled(1);
	}
    
    else 
	{
	raster S4MASK, S4darkMASK, S4brightMASK;

	GetOutputRaster(S4MASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS4BAND1, S4MASK, "georef");

	GetOutputRaster(S4darkMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS4BAND1, S4darkMASK, "georef");

	GetOutputRaster(S4brightMASK, MSSlins, MSScols, "8-bit unsigned");
	CopySubobjects(MSS4BAND1, S4brightMASK, "georef");
	
	dlgmain.GetCtrlByID("rasnorms1").SetEnabled(1);
	}
}

	
#######################################
### Procedure to select output files SZENE 1
#######################################

proc OUTS1 () {

if ( sensors1 == 7 ) 
    { 
	raster OUT1ETM1, OUT1ETM2, OUT1ETM3, OUT1ETM4, OUT1ETM5, OUT1ETM7;

	GetOutputRaster(OUT1ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND1, OUT1ETM1, "georef");

	GetOutputRaster(OUT1ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND2, OUT1ETM2, "georef");

	GetOutputRaster(OUT1ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND3, OUT1ETM3, "georef");

	GetOutputRaster(OUT1ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND4, OUT1ETM4, "georef");

	GetOutputRaster(OUT1ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND5, OUT1ETM5, "georef");

	GetOutputRaster(OUT1ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM1BAND7, OUT1ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms2").SetEnabled(1);
    }

else if ( sensors1 == 6 ) 
    { 
	raster OUT1TM1, OUT1TM2, OUT1TM3, OUT1TM4, OUT1TM5, OUT1TM7;

	GetOutputRaster(OUT1TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND1, OUT1TM1, "georef");

	GetOutputRaster(OUT1TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND2, OUT1TM2, "georef");

	GetOutputRaster(OUT1TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND3, OUT1TM3, "georef");

	GetOutputRaster(OUT1TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND4, OUT1TM4, "georef");

	GetOutputRaster(OUT1TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND5, OUT1TM5, "georef");

	GetOutputRaster(OUT1TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM1BAND7, OUT1TM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms2").SetEnabled(1);
    }

else 
    {
	raster OUT1MSS1, OUT1MSS2, OUT1MSS3, OUT1MSS4;

	GetOutputRaster(OUT1MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND1, OUT1MSS1, "georef");

	GetOutputRaster(OUT1MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND2, OUT1MSS2, "georef");

	GetOutputRaster(OUT1MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND3, OUT1MSS3, "georef");

	GetOutputRaster(OUT1MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS1BAND4, OUT1MSS4, "georef");
	
	dlgmain.GetCtrlByID("rasnorms2").SetEnabled(1);
	}
}

#######################################
### Procedure to select output files SZENE 2
#######################################

proc OUTS2 () {

if ( sensors2 == 7 ) 
    {
	raster OUT2ETM1, OUT2ETM2, OUT2ETM3, OUT2ETM4, OUT2ETM5, OUT2ETM7;

	GetOutputRaster(OUT2ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND1, OUT2ETM1, "georef");

	GetOutputRaster(OUT2ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND2, OUT2ETM2, "georef");

	GetOutputRaster(OUT2ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND3, OUT2ETM3, "georef");

	GetOutputRaster(OUT2ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND4, OUT2ETM4, "georef");

	GetOutputRaster(OUT2ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND5, OUT2ETM5, "georef");

	GetOutputRaster(OUT2ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM2BAND7, OUT2ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms3").SetEnabled(1);
    }

else if ( sensors2 == 6 )
    {
	raster OUT2TM1, OUT2TM2, OUT2TM3, OUT2TM4, OUT2TM5, OUT2TM7;

	GetOutputRaster(OUT2TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND1, OUT2TM1, "georef");

	GetOutputRaster(OUT2TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND2, OUT2TM2, "georef");

	GetOutputRaster(OUT2TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND3, OUT2TM3, "georef");

	GetOutputRaster(OUT2TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND4, OUT2TM4, "georef");

	GetOutputRaster(OUT2TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND5, OUT2TM5, "georef");

	GetOutputRaster(OUT2TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM2BAND7, OUT2TM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms3").SetEnabled(1);
    }
    
else 
    {
	raster OUT2MSS1, OUT2MSS2, OUT2MSS3, OUT2MSS4;

	GetOutputRaster(OUT2MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND1, OUT2MSS1, "georef");

	GetOutputRaster(OUT2MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND2, OUT2MSS2, "georef");

	GetOutputRaster(OUT2MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND3, OUT2MSS3, "georef");

	GetOutputRaster(OUT2MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS2BAND4, OUT2MSS4, "georef");
	
	dlgmain.GetCtrlByID("rasnorms3").SetEnabled(1);
    }
}

#######################################
### Procedure to select output files SZENE 3
#######################################

proc OUTS3 () {

if ( sensors3 == 7 ) 
    {
	raster OUT3ETM1, OUT3ETM2, OUT3ETM3, OUT3ETM4, OUT3ETM5, OUT3ETM7;

	GetOutputRaster(OUT3ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND1, OUT3ETM1, "georef");

	GetOutputRaster(OUT3ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND2, OUT3ETM2, "georef");

	GetOutputRaster(OUT3ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND3, OUT3ETM3, "georef");

	GetOutputRaster(OUT3ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND4, OUT3ETM4, "georef");

	GetOutputRaster(OUT3ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND5, OUT3ETM5, "georef");

	GetOutputRaster(OUT3ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM3BAND7, OUT3ETM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms4").SetEnabled(1);
    }

else if ( sensors3 == 6 )
    {
	raster OUT3TM1, OUT3TM2, OUT3TM3, OUT3TM4, OUT3TM5, OUT3TM7;

	GetOutputRaster(OUT3TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND1, OUT3TM1, "georef");

	GetOutputRaster(OUT3TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND2, OUT3TM2, "georef");

	GetOutputRaster(OUT3TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND3, OUT3TM3, "georef");

	GetOutputRaster(OUT3TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND4, OUT3TM4, "georef");

	GetOutputRaster(OUT3TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND5, OUT3TM5, "georef");

	GetOutputRaster(OUT3TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM3BAND7, OUT3TM7, "georef");
	
	dlgmain.GetCtrlByID("rasnorms4").SetEnabled(1);
    }
    
else 
	{
	raster OUT3MSS1, OUT3MSS2, OUT3MSS3, OUT3MSS4;

	GetOutputRaster(OUT3MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND1, OUT3MSS1, "georef");

	GetOutputRaster(OUT3MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND2, OUT3MSS2, "georef");

	GetOutputRaster(OUT3MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND3, OUT3MSS3, "georef");

	GetOutputRaster(OUT3MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS3BAND4, OUT3MSS4, "georef");
	
	dlgmain.GetCtrlByID("rasnorms4").SetEnabled(1);
    }
}

#######################################
### Procedure to select output files SZENE 4
#######################################

proc OUTS4 () {

if ( sensors4 == 7 ) 
    {
	raster OUT4ETM1, OUT4ETM2, OUT4ETM3, OUT4ETM4, OUT4ETM5, OUT4ETM7;

	GetOutputRaster(OUT4ETM1, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND1, OUT4ETM1, "georef");

	GetOutputRaster(OUT4ETM2, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND2, OUT4ETM2, "georef");

	GetOutputRaster(OUT4ETM3, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND3, OUT4ETM3, "georef");

	GetOutputRaster(OUT4ETM4, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND4, OUT4ETM4, "georef");

	GetOutputRaster(OUT4ETM5, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND5, OUT4ETM5, "georef");

	GetOutputRaster(OUT4ETM7, ETMlins, ETMcols, "32-bit float");
	CopySubobjects(ETM4BAND7, OUT4ETM7, "georef");
	
	dlgmain.SetOkEnabled(1);
    }

else if ( sensors4 == 6 )
    {
	raster OUT4TM1, OUT4TM2, OUT4TM3, OUT4TM4, OUT4TM5, OUT4TM7;

	GetOutputRaster(OUT4TM1, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND1, OUT4TM1, "georef");

	GetOutputRaster(OUT4TM2, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND2, OUT4TM2, "georef");

	GetOutputRaster(OUT4TM3, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND3, OUT4TM3, "georef");

	GetOutputRaster(OUT4TM4, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND4, OUT4TM4, "georef");

	GetOutputRaster(OUT4TM5, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND5, OUT4TM5, "georef");

	GetOutputRaster(OUT4TM7, TMlins, TMcols, "32-bit float");
	CopySubobjects(TM4BAND7, OUT4TM7, "georef");
	
	dlgmain.SetOkEnabled(1);
    }

else
    {
	raster OUT4MSS1, OUT4MSS2, OUT4MSS3, OUT4MSS4;

	GetOutputRaster(OUT4MSS1, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND1, OUT4MSS1, "georef");

	GetOutputRaster(OUT4MSS2, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND2, OUT4MSS2, "georef");

	GetOutputRaster(OUT4MSS3, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND3, OUT4MSS3, "georef");

	GetOutputRaster(OUT4MSS4, MSSlins, MSScols, "32-bit float");
	CopySubobjects(MSS4BAND4, OUT4MSS4, "georef");
	
	dlgmain.SetOkEnabled(1);
	}
}


##################################
### Function for Julian Date aquisition
##################################

func LeapYear(date)
{ 
    if ( (date/4)   != round(date/4) )   return false;
    if ( (date/100) != round(date/100) ) return true;
    if ( (date/400) != round(date/400) ) return false;
    return true;
}

func julD(day, month, year)
{
    if ( LeapYear(year) )
	{
        return (day + accumulateLY[month]);
	}    
    else
    {
        return (day + accumulate[month]);
    }
}

##################################
### Function for the calculation of the relative DNmin1000 value
##################################

func DNminCALC(extentLIN, extentCOL, pixel)
{
    numeric DNminCALClvl, extentLIN, extentCOL, pixel, DNminCALClevel, DNminCALClvl;
    DNminCALClevel = (1000*pixel)/((185000/extentLIN)*(185000/extentCOL));
    DNminCALClvl = round(DNminCALClevel);
    return DNminCALClvl;
}

######################################################################
######################################################################
####################  Main Program  ##################################
######################################################################
######################################################################

clear();

#################################################
### Set up main dialog window
#################################################

xmlmain$ = '

	
		
			
				
					ETM-7				
					TM-5
					MSS-5
					MSS-4
					MSS-3
					MSS-2
					MSS-1
				
				
				
			
			
				
					ETM-7
					TM-5
					MSS-5
					MSS-4
					MSS-3
					MSS-2
					MSS-1
				
				
				
			
			
				
					ETM-7
					TM-5
					MSS-5
					MSS-4
					MSS-3
					MSS-2
					MSS-1
				
				
				
			
			
				
					ETM-7
					TM-5
					MSS-5
					MSS-4
					MSS-3
					MSS-2
					MSS-1
				
				
				
			
		
		
			
				
				
			
			
				
				
			
			
				
				
			
			
				
				
			
		
		
				
					
				
				
					
				
				
					
				
				
					
				
		
		
			
				
			
			
				
			
			
				
			
			
				
			
			
				
			
		
		
			
				
			
			
				
			
			
				
			
			
				
			
		
	
';

err = docmain.Parse(xmlmain$);

if ( err < 0 ) {
	PopupError( err );	# pop up an error dialog
	Exit( );
	}

nodemain = docmain.GetElementByID( "maindlg" );

if ( nodemain == 0 ) {
	PopupMessage( "Could not find main dialog node in XML document" );
	Exit();
	}

dlgmain.SetXMLNode(nodemain);

ret = dlgmain.DoModal();

if (ret == -1)
{
	Exit();
}

######################################
###### Begin processing.
###### Calculate Radiance.
######################################

pifDarkVAL = dlgmain.GetCtrlByID("pifDark").GetValueNum();
pifBrightVAL = dlgmain.GetCtrlByID("pifBright").GetValueNum();
minPifVAL = dlgmain.GetCtrlByID("minPif").GetValueNum();
histoTailInputVAL = dlgmain.GetCtrlByID("histoTailInput").GetValueNum();

printf( "Entered PIF-Selection values passed to variables...\n\n" );

printf( "Starting off calculating...\n\n" );

s1zenith = ((2 * pi * (90 - s1ele)) / 360);                                        # sunzenith calculation via 90degree minus sunelevation
s2zenith = ((2 * pi * (90 - s2ele)) / 360);
s3zenith = ((2 * pi * (90 - s3ele)) / 360);
s4zenith = ((2 * pi * (90 - s4ele)) / 360);

s1zenithSIMPLE = 90 - s1ele;
s2zenithSIMPLE = 90 - s2ele;
s3zenithSIMPLE = 90 - s3ele;
s4zenithSIMPLE = 90 - s4ele;

printf("Sun zenith angle (Scene 1): %6f\n", s1zenithSIMPLE);
printf("Sun zenith angle (Scene 2): %6f\n", s2zenithSIMPLE);
printf("Sun zenith angle (Scene 3): %6f\n", s3zenithSIMPLE);
printf("Sun zenith angle (Scene 4): %6f\n\n", s4zenithSIMPLE);

printf( "Sun zenith calculated...\n\n" );

s1yyyymmdd.SetDateYYYYMMDD(s1aq);
s1year = s1yyyymmdd.GetYear();
s1month = s1yyyymmdd.GetMonth();
s1day = s1yyyymmdd.GetDayOfMonth();

s2yyyymmdd.SetDateYYYYMMDD(s2aq);
s2year = s2yyyymmdd.GetYear();
s2month = s2yyyymmdd.GetMonth();
s2day = s2yyyymmdd.GetDayOfMonth();

s3yyyymmdd.SetDateYYYYMMDD(s3aq);
s3year = s3yyyymmdd.GetYear();
s3month = s3yyyymmdd.GetMonth();
s3day = s3yyyymmdd.GetDayOfMonth();

s4yyyymmdd.SetDateYYYYMMDD(s4aq);
s4year = s4yyyymmdd.GetYear();
s4month = s4yyyymmdd.GetMonth();
s4day = s4yyyymmdd.GetDayOfMonth();

s1leap = LeapYear(s1year);				                                           #Leap Year = 1 Else = 0
s2leap = LeapYear(s2year);
s3leap = LeapYear(s3year);
s4leap = LeapYear(s4year);

s1jul = julD(s1day, s1month, s1year);                                              #julD(day, month, year)
s2jul = julD(s2day, s2month, s2year);
s3jul = julD(s3day, s3month, s3year);
s4jul = julD(s4day, s4month, s4year);

printf("Julian Date (Scene 1): %3i\n", s1jul);
printf("Julian Date (Scene 2): %3i\n", s2jul);
printf("Julian Date (Scene 3): %3i\n", s3jul);
printf("Julian Date (Scene 4): %3i\n\n", s4jul);

printf( "Julian Date calculated and year, month and day count extracted from aquisition date...\n\n" );

dist1 = 1 / (1 - 0.01674 * cos(0.9856 * (s1jul - 4)));
dist2 = 1 / (1 - 0.01674 * cos(0.9856 * (s2jul - 4)));
dist3 = 1 / (1 - 0.01674 * cos(0.9856 * (s3jul - 4)));
dist4 = 1 / (1 - 0.01674 * cos(0.9856 * (s4jul - 4)));

printf("Earth-Sun distance (Scene 1): %12f\n", dist1);
printf("Earth-Sun distance (Scene 2): %12f\n", dist2);
printf("Earth-Sun distance (Scene 3): %12f\n", dist3);
printf("Earth-Sun distance (Scene 4): %12f\n\n", dist4);

printf( "Earth Sundistance Calculated...\n\n" );

printf( "Computing Radiance...\n\n" );

###########################
### Radiance Scene 1
###########################

if ( sensors1 == 7 )
{
raster RADS1ETM1, RADS1ETM2, RADS1ETM3, RADS1ETM4, RADS1ETM5, RADS1ETM7;
CreateTempRaster(RADS1ETM1, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS1ETM2, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS1ETM3, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS1ETM4, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS1ETM5, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS1ETM7, ETMlins, ETMcols,"32-bit float");

  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{	
		if ( s1aq < 20000701)					# Before 1. July 2000
		{
			if ( earSurCat1 == 8 )				# Desert(hs)
			{
			RADS1ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS1ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS1ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS1ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS1ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS1ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat1 == 7 )			# Desert(ms)
			{
			RADS1ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS1ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS1ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS1ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS1ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS1ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat1 == 6 )			# Desert Ice(ls)
			{
			RADS1ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS1ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS1ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS1ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS1ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS1ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat1 == 5 )			# Ice(hs)
			{
			RADS1ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS1ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS1ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS1ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS1ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS1ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat1 == 4 )			# Land(hs)
			{
			RADS1ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS1ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS1ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS1ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS1ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS1ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat1 == 3 )			# Land Ocean(ls
			{
			RADS1ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS1ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS1ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS1ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS1ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS1ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat1 == 2 )			# Water
			{
			RADS1ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS1ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS1ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS1ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS1ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS1ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat1 == 1 )			# Night(volcanos
			{
			RADS1ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS1ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS1ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS1ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS1ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS1ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
		}
		else								# After 1. July 2000
		{
			if ( earSurCat1 == 8 )				# Desert(hs)
			{
			RADS1ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS1ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS1ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS1ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS1ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS1ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat1 == 7 )			# Desert(ms)
			{
			RADS1ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS1ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS1ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS1ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS1ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS1ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat1 == 6 )			# Desert Ice(ls)
			{
			RADS1ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS1ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS1ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS1ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS1ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS1ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat1 == 5 )			# Ice(hs)
			{
			RADS1ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS1ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS1ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS1ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS1ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS1ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat1 == 4 )			# Land(hs)
			{
			RADS1ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS1ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS1ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS1ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS1ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS1ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat1 == 3 )			# Land Ocean(ls
			{
			RADS1ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS1ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS1ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS1ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS1ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS1ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat1 == 2 )			# Water
			{
			RADS1ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS1ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS1ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS1ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS1ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS1ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat1 == 1 )			# Night(volcanos
			{
			RADS1ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS1ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS1ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS1ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS1ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS1ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM1BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
		}
	 }
   }
CloseRaster(ETM1BAND1);
CloseRaster(ETM1BAND2);
CloseRaster(ETM1BAND3);
CloseRaster(ETM1BAND4);
CloseRaster(ETM1BAND5);
CloseRaster(ETM1BAND7);
printf("Radiance calculation for Scene 1 (ETM) is done...\n");
}

else if ( sensors1 == 6 )
{
    raster RADS1TM1, RADS1TM2, RADS1TM3, RADS1TM4, RADS1TM5, RADS1TM7;
    CreateTempRaster(RADS1TM1, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS1TM2, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS1TM3, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS1TM4, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS1TM5, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS1TM7, TMlins, TMcols,"32-bit float");

	   for i = 1 to TMlins
	   {
	        for j = 1 to TMcols
	        {
		     if (s1leap == 0)
		     {
		         numeric gainnewTMS1b1, gainnewTMS1b2, gainnewTMS1b3, gainnewTMS1b4, gainnewTMS1b5, gainnewTMS1b7;

	             gainnewTMS1b1 = ((0.145658 * exp(-0.955113 * ((s1year + (s1jul / 365)) - 1984.2082))) + 1.243000);
	             gainnewTMS1b2 = ((0.058651 * exp(-0.835952 * ((s1year + (s1jul / 365)) - 1984.2082))) + 0.656100);
                 gainnewTMS1b3 = ((0.111858 * exp(-1.002090 * ((s1year + (s1jul / 365)) - 1984.2082))) + 0.905000);
                 gainnewTMS1b4 = ((0.107650 * exp(-1.277190 * ((s1year + (s1jul / 365)) - 1984.2082))) + 1.082000);
                 gainnewTMS1b5 = ((0.243364 * exp(-1.207080 * ((s1year + (s1jul / 365)) - 1984.2082))) + 7.944000);
                 gainnewTMS1b7 = ((0.403551 * exp(-0.999127 * ((s1year + (s1jul / 365)) - 1984.2082))) + 14.52000);

                 RADS1TM1[i,j] = ((s1TMg1 * ((gainnewTMS1b1 * ((s1TMg1 * TM1BAND1[i,j]) + s1TMb1)) + biasnewTMb1)) + s1TMb1);
                 RADS1TM2[i,j] = ((s1TMg2 * ((gainnewTMS1b2 * ((s1TMg2 * TM1BAND2[i,j]) + s1TMb2)) + biasnewTMb2)) + s1TMb2);
                 RADS1TM3[i,j] = ((s1TMg3 * ((gainnewTMS1b3 * ((s1TMg3 * TM1BAND3[i,j]) + s1TMb3)) + biasnewTMb3)) + s1TMb3);
                 RADS1TM4[i,j] = ((s1TMg4 * ((gainnewTMS1b4 * ((s1TMg4 * TM1BAND4[i,j]) + s1TMb4)) + biasnewTMb4)) + s1TMb4);
                 RADS1TM5[i,j] = ((s1TMg5 * ((gainnewTMS1b5 * ((s1TMg5 * TM1BAND5[i,j]) + s1TMb5)) + biasnewTMb5)) + s1TMb5);
                 RADS1TM7[i,j] = ((s1TMg7 * ((gainnewTMS1b7 * ((s1TMg7 * TM1BAND7[i,j]) + s1TMb7)) + biasnewTMb7)) + s1TMb7);
		     }
			
		     else if (s1leap == 1)
		     { 
	             numeric gainnewTMS1b1, gainnewTMS1b2, gainnewTMS1b3, gainnewTMS1b4, gainnewTMS1b5, gainnewTMS1b7;

	             gainnewTMS1b1 = ((0.145658 * exp(-0.955113 * ((s1year + (s1jul / 366)) - 1984.2082))) + 1.243000);
	             gainnewTMS1b2 = ((0.058651 * exp(-0.835952 * ((s1year + (s1jul / 366)) - 1984.2082))) + 0.656100);
                 gainnewTMS1b3 = ((0.111858 * exp(-1.002090 * ((s1year + (s1jul / 366)) - 1984.2082))) + 0.905000);
                 gainnewTMS1b4 = ((0.107650 * exp(-1.277190 * ((s1year + (s1jul / 366)) - 1984.2082))) + 1.082000);
                 gainnewTMS1b5 = ((0.243364 * exp(-1.207080 * ((s1year + (s1jul / 366)) - 1984.2082))) + 7.944000);
                 gainnewTMS1b7 = ((0.403551 * exp(-0.999127 * ((s1year + (s1jul / 366)) - 1984.2082))) + 14.52000);

                 RADS1TM1[i,j] = ((s1TMg1 * ((gainnewTMS1b1 * ((s1TMg1 * TM1BAND1[i,j]) + s1TMb1)) + biasnewTMb1)) + s1TMb1);
                 RADS1TM2[i,j] = ((s1TMg2 * ((gainnewTMS1b2 * ((s1TMg2 * TM1BAND2[i,j]) + s1TMb2)) + biasnewTMb2)) + s1TMb2);
                 RADS1TM3[i,j] = ((s1TMg3 * ((gainnewTMS1b3 * ((s1TMg3 * TM1BAND3[i,j]) + s1TMb3)) + biasnewTMb3)) + s1TMb3);
                 RADS1TM4[i,j] = ((s1TMg4 * ((gainnewTMS1b4 * ((s1TMg4 * TM1BAND4[i,j]) + s1TMb4)) + biasnewTMb4)) + s1TMb4);
                 RADS1TM5[i,j] = ((s1TMg5 * ((gainnewTMS1b5 * ((s1TMg5 * TM1BAND5[i,j]) + s1TMb5)) + biasnewTMb5)) + s1TMb5);
                 RADS1TM7[i,j] = ((s1TMg7 * ((gainnewTMS1b7 * ((s1TMg7 * TM1BAND7[i,j]) + s1TMb7)) + biasnewTMb7)) + s1TMb7);
		     }
	       }
	 }

CloseRaster(TM1BAND1);
CloseRaster(TM1BAND2);
CloseRaster(TM1BAND3);
CloseRaster(TM1BAND4);
CloseRaster(TM1BAND5);
CloseRaster(TM1BAND7);
printf("Radiance calculation for Scene 1 (TM) is done...\n");
}

else if ( sensors1 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
	raster RADS1MSS1, RADS1MSS2, RADS1MSS3, RADS1MSS4;
	CreateTempRaster(RADS1MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
         if ( s1aq <= 19841109)                                          # Before 9 November 1984
         {
          RADS1MSS1[i,j] = L5B1bef84gain * MSS1BAND1[i,j] + L5B1bef84bias;
          RADS1MSS2[i,j] = L5B2bef84gain * MSS1BAND2[i,j] + L5B2bef84bias;
          RADS1MSS3[i,j] = L5B3bef84gain * MSS1BAND3[i,j] + L5B3bef84bias;
          RADS1MSS4[i,j] = L5B4bef84gain * MSS1BAND4[i,j] + L5B4bef84bias;
         }
         else                                                             # after 9 November 1984
         {
          RADS1MSS1[i,j] = L5B1aft84gain * MSS1BAND1[i,j] + L5B1aft84bias;
          RADS1MSS2[i,j] = L5B2aft84gain * MSS1BAND2[i,j] + L5B2aft84bias;
          RADS1MSS3[i,j] = L5B3aft84gain * MSS1BAND3[i,j] + L5B3aft84bias;
          RADS1MSS4[i,j] = L5B4aft84gain * MSS1BAND4[i,j] + L5B4aft84bias;
         }
	   }
	}

CloseRaster(MSS1BAND1);
CloseRaster(MSS1BAND2);
CloseRaster(MSS1BAND3);
CloseRaster(MSS1BAND4);
printf("Radiance calculation for Scene 1 (MSS) is done...\n");
}

else if ( sensors1 == 4 )                                               # MSS 4
{
	raster RADS1MSS1, RADS1MSS2, RADS1MSS3, RADS1MSS4;
	CreateTempRaster(RADS1MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s1aq <= 19830401)                                        # Before 1 April 1983
          {
          RADS1MSS1[i,j]= L4B1bef83gain * MSS1BAND1[i,j] + L4B1bef83bias;
          RADS1MSS2[i,j] = L4B2bef83gain * MSS1BAND2[i,j] + L4B2bef83bias;
          RADS1MSS3[i,j] = L4B3bef83gain * MSS1BAND3[i,j] + L4B3bef83gain;
          RADS1MSS4[i,j] = L4B4bef83gain * MSS1BAND4[i,j] + L4B4bef83bias;
          }
          else                                                             # After 1 April 1983
          {
          RADS1MSS1[i,j] = L4B1aft83gain * MSS1BAND1[i,j] + L4B1aft83bias;
          RADS1MSS2[i,j] = L4B2aft83gain * MSS1BAND2[i,j] + L4B2aft83bias;
          RADS1MSS3[i,j] = L4B3aft83gain * MSS1BAND3[i,j] + L4B3aft83bias;
          RADS1MSS4[i,j] = L4B4aft83gain * MSS1BAND4[i,j] + L4B4aft83bias;
          }
	  }
	}

CloseRaster(MSS1BAND1);
CloseRaster(MSS1BAND2);
CloseRaster(MSS1BAND3);
CloseRaster(MSS1BAND4);
printf("Radiance calculation for Scene 1 (MSS) is done...\n");
}

else if ( sensors1 == 3 )                                               # MSS 3
{
	raster RADS1MSS1, RADS1MSS2, RADS1MSS3, RADS1MSS4;
	CreateTempRaster(RADS1MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS4, MSSlins, MSScols,"32-bit float");
    
    for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  if ( s1aq <= 19780601 )                                                  # Before 1 Juni 1978
          {
          RADS1MSS1[i,j] = L3B1bef78gain * MSS1BAND1[i,j] + L3B1bef78bias;
          RADS1MSS2[i,j] = L3B2bef78gain * MSS1BAND2[i,j] + L3B2bef78bias;
          RADS1MSS3[i,j] = L3B3bef78gain * MSS1BAND3[i,j] + L3B3bef78bias;
          RADS1MSS4[i,j] = L3B4bef78gain * MSS1BAND4[i,j] + L3B4bef78bias;
          }
          else if ( s1aq >= 19780602 and s1aq <= 19790201 )                        # 2 Juni 1978  bis  1 Februar 1979
          {
          RADS1MSS1[i,j] = L3B17879gain * MSS1BAND1[i,j] + L3B17879bias;
          RADS1MSS2[i,j] = L3B27879gain * MSS1BAND2[i,j] + L3B27879bias;
          RADS1MSS3[i,j] = L3B37879gain * MSS1BAND3[i,j] + L3B37879bias;
          RADS1MSS4[i,j] = L3B47879gain * MSS1BAND4[i,j] + L3B47879bias;
          }
          else if ( s1aq > 19790201 )                                             # After 1 Februar 1979
          {
          RADS1MSS1[i,j] = L3B1aft79gain * MSS1BAND1[i,j] + L3B1aft79bias;
          RADS1MSS2[i,j] = L3B2aft79gain * MSS1BAND2[i,j] + L3B2aft79bias;
          RADS1MSS3[i,j] = L3B3aft79gain * MSS1BAND3[i,j] + L3B3aft79bias;
          RADS1MSS4[i,j] = L3B4aft79gain * MSS1BAND4[i,j] + L3B4aft79bias;
          }
	   }
    }

CloseRaster(MSS1BAND1);
CloseRaster(MSS1BAND2);
CloseRaster(MSS1BAND3);
CloseRaster(MSS1BAND4);
printf("Radiance calculation for Scene 1 (MSS) is done...\n");
}

else if ( sensors1 == 2 )                                               # MSS 2
{
	raster RADS1MSS1, RADS1MSS2, RADS1MSS3, RADS1MSS4;
	CreateTempRaster(RADS1MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s1aq <= 19750716 )                                        # Before 16 Juli 1975
          {
          RADS1MSS1[i,j] = L2B1bef75gain * MSS1BAND1[i,j] + L2B1bef75bias;
          RADS1MSS2[i,j] = L2B2bef75gain * MSS1BAND2[i,j] + L2B2bef75bias;
          RADS1MSS3[i,j] = L2B3bef75gain * MSS1BAND3[i,j] + L2B3bef75bias;
          RADS1MSS4[i,j] = L2B4bef75gain * MSS1BAND4[i,j] + L2B4bef75bias;
          }
          else if ( s1aq > 19750716 and s1aq <= 19790201 )
          {
          RADS1MSS1[i,j] = L2B1aft75gain * MSS1BAND1[i,j] + L2B1aft75bias;                     # After 16. Juli 1975
          RADS1MSS2[i,j] = L2B2aft75gain * MSS1BAND2[i,j] + L2B2aft75bias;
          RADS1MSS3[i,j] = L2B3aft75gain * MSS1BAND3[i,j] + L2B3aft75bias;
          RADS1MSS4[i,j] = L2B4aft75gain * MSS1BAND4[i,j] + L2B4aft75bias;
          }
          else if ( s1aq > 19790201 )
          {
          RADS1MSS1[i,j] = L2B1aft79gain * MSS1BAND1[i,j] + L2B1aft79bias;                    # After 1. Februar 1979
          RADS1MSS2[i,j] = L2B2aft79gain * MSS1BAND2[i,j] + L2B2aft79bias;
          RADS1MSS3[i,j] = L2B3aft79gain * MSS1BAND3[i,j] + L2B3aft79bias;
          RADS1MSS4[i,j] = L2B4aft79gain * MSS1BAND4[i,j] + L2B4aft79bias;
          }
	   }  
    }

CloseRaster(MSS1BAND1);
CloseRaster(MSS1BAND2);
CloseRaster(MSS1BAND3);
CloseRaster(MSS1BAND4);
printf("Radiance calculation for Scene 1 (MSS) is done...\n");
}    

else if ( sensors1 == 1 )                                               # MSS 1
{
	raster RADS1MSS1, RADS1MSS2, RADS1MSS3, RADS1MSS4;
	CreateTempRaster(RADS1MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS1MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      RADS1MSS1[i,j] = L1B1gain * MSS1BAND1[i,j] + L1B1bias;
          RADS1MSS2[i,j] = L1B2gain * MSS1BAND2[i,j] + L1B1bias;
          RADS1MSS3[i,j] = L1B3gain * MSS1BAND3[i,j] + L1B1bias;
          RADS1MSS4[i,j] = L1B4gain * MSS1BAND4[i,j] + L1B1bias;
	   }
	}

CloseRaster(MSS1BAND1);
CloseRaster(MSS1BAND2);
CloseRaster(MSS1BAND3);
CloseRaster(MSS1BAND4);
printf("Radiance calculation for Scene 1 (MSS) is done...\n");
}
	
###########################
### Radiance Scene 2
###########################

if ( sensors2 == 7 )
{
raster RADS2ETM1, RADS2ETM2, RADS2ETM3, RADS2ETM4, RADS2ETM5, RADS2ETM7;
CreateTempRaster(RADS2ETM1, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS2ETM2, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS2ETM3, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS2ETM4, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS2ETM5, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS2ETM7, ETMlins, ETMcols,"32-bit float");

  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
		if ( s2aq < 20000701)					# Before 1. July 2000
		{
			if ( earSurCat2 == 8 )				# Desert(hs)
			{
			RADS2ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS2ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS2ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS2ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS2ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS2ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat2 == 7 )			# Desert(ms)
			{
			RADS2ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS2ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS2ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS2ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS2ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS2ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat2 == 6 )			# Desert Ice(ls)
			{
			RADS2ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS2ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS2ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS2ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS2ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS2ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat2 == 5 )			# Ice(hs)
			{
			RADS2ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS2ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS2ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS2ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS2ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS2ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat2 == 4 )			# Land(hs)
			{
			RADS2ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS2ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS2ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS2ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS2ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS2ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat2 == 3 )			# Land Ocean(ls
			{
			RADS2ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS2ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS2ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS2ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS2ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS2ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat2 == 2 )			# Water
			{
			RADS2ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS2ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS2ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS2ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS2ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS2ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat2 == 1 )			# Night(volcanos
			{
			RADS2ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS2ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS2ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS2ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS2ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS2ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
		}
		else								# After 1. July 2000
		{
			if ( earSurCat2 == 8 )				# Desert(hs)
			{
			RADS2ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS2ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS2ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS2ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS2ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS2ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat2 == 7 )			# Desert(ms)
			{
			RADS2ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS2ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS2ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS2ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS2ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS2ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat2 == 6 )			# Desert Ice(ls)
			{
			RADS2ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS2ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS2ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS2ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS2ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS2ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat2 == 5 )			# Ice(hs)
			{
			RADS2ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS2ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS2ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS2ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS2ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS2ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat2 == 4 )			# Land(hs)
			{
			RADS2ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS2ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS2ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS2ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS2ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS2ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat2 == 3 )			# Land Ocean(ls)
			{
			RADS2ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS2ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS2ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS2ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS2ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS2ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat2 == 2 )			# Water
			{
			RADS2ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS2ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS2ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS2ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS2ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS2ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat2 == 1 )			# Night(volcanos)
			{
			RADS2ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS2ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS2ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS2ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS2ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS2ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM2BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
		}
	}
  }
CloseRaster(ETM2BAND1);
CloseRaster(ETM2BAND2);
CloseRaster(ETM2BAND3);
CloseRaster(ETM2BAND4);
CloseRaster(ETM2BAND5);
CloseRaster(ETM2BAND7);
printf("Radiance calculation for Scene 2 (ETM) is done...\n");
}

else if ( sensors2 == 6 )
{
    raster RADS2TM1, RADS2TM2, RADS2TM3, RADS2TM4, RADS2TM5, RADS2TM7;
    CreateTempRaster(RADS2TM1, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS2TM2, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS2TM3, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS2TM4, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS2TM5, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS2TM7, TMlins, TMcols,"32-bit float");

	   for i = 1 to TMlins
	   {
	        for j = 1 to TMcols
	        {
		     if (s2leap == 0)
		     {
		     numeric gainnewTMS2b1, gainnewTMS2b2, gainnewTMS2b3, gainnewTMS2b4, gainnewTMS2b5, gainnewTMS2b7;

	             gainnewTMS2b1 = ((0.145658 * exp(-0.955113 * ((s2year + (s2jul / 365)) - 1984.2082))) + 1.243000);
	             gainnewTMS2b2 = ((0.058651 * exp(-0.835952 * ((s2year + (s2jul / 365)) - 1984.2082))) + 0.656100);
                 gainnewTMS2b3 = ((0.111858 * exp(-1.002090 * ((s2year + (s2jul / 365)) - 1984.2082))) + 0.905000);
                 gainnewTMS2b4 = ((0.107650 * exp(-1.277190 * ((s2year + (s2jul / 365)) - 1984.2082))) + 1.082000);
                 gainnewTMS2b5 = ((0.243364 * exp(-1.207080 * ((s2year + (s2jul / 365)) - 1984.2082))) + 7.944000);
                 gainnewTMS2b7 = ((0.403551 * exp(-0.999127 * ((s2year + (s2jul / 365)) - 1984.2082))) + 14.52000);

                 RADS2TM1[i,j] = ((s2TMg1 * ((gainnewTMS2b1 * ((s2TMg1 * TM2BAND1[i,j]) + s2TMb1)) + biasnewTMb1)) + s2TMb1);
                 RADS2TM2[i,j] = ((s2TMg2 * ((gainnewTMS2b2 * ((s2TMg2 * TM2BAND2[i,j]) + s2TMb2)) + biasnewTMb2)) + s2TMb2);
                 RADS2TM3[i,j] = ((s2TMg3 * ((gainnewTMS2b3 * ((s2TMg3 * TM2BAND3[i,j]) + s2TMb3)) + biasnewTMb3)) + s2TMb3);
                 RADS2TM4[i,j] = ((s2TMg4 * ((gainnewTMS2b4 * ((s2TMg4 * TM2BAND4[i,j]) + s2TMb4)) + biasnewTMb4)) + s2TMb4);
                 RADS2TM5[i,j] = ((s2TMg5 * ((gainnewTMS2b5 * ((s2TMg5 * TM2BAND5[i,j]) + s2TMb5)) + biasnewTMb5)) + s2TMb5);
                 RADS2TM7[i,j] = ((s2TMg7 * ((gainnewTMS2b7 * ((s2TMg7 * TM2BAND7[i,j]) + s2TMb7)) + biasnewTMb7)) + s2TMb7);
		     }

		     else if (s2leap == 1)
		     {
		     numeric gainnewTMS2b1, gainnewTMS2b2, gainnewTMS2b3, gainnewTMS2b4, gainnewTMS2b5, gainnewTMS2b7;

	             gainnewTMS2b1 = ((0.145658 * exp(-0.955113 * ((s2year + (s2jul / 366)) - 1984.2082))) + 1.243000);
	             gainnewTMS2b2 = ((0.058651 * exp(-0.835952 * ((s2year + (s2jul / 366)) - 1984.2082))) + 0.656100);
                 gainnewTMS2b3 = ((0.111858 * exp(-1.002090 * ((s2year + (s2jul / 366)) - 1984.2082))) + 0.905000);
                 gainnewTMS2b4 = ((0.107650 * exp(-1.277190 * ((s2year + (s2jul / 366)) - 1984.2082))) + 1.082000);
                 gainnewTMS2b5 = ((0.243364 * exp(-1.207080 * ((s2year + (s2jul / 366)) - 1984.2082))) + 7.944000);
                 gainnewTMS2b7 = ((0.403551 * exp(-0.999127 * ((s2year + (s2jul / 366)) - 1984.2082))) + 14.52000);

                 RADS2TM1[i,j] = ((s2TMg1 * ((gainnewTMS2b1 * ((s2TMg1 * TM2BAND1[i,j]) + s2TMb1)) + biasnewTMb1)) + s2TMb1);
                 RADS2TM2[i,j] = ((s2TMg2 * ((gainnewTMS2b2 * ((s2TMg2 * TM2BAND2[i,j]) + s2TMb2)) + biasnewTMb2)) + s2TMb2);
                 RADS2TM3[i,j] = ((s2TMg3 * ((gainnewTMS2b3 * ((s2TMg3 * TM2BAND3[i,j]) + s2TMb3)) + biasnewTMb3)) + s2TMb3);
                 RADS2TM4[i,j] = ((s2TMg4 * ((gainnewTMS2b4 * ((s2TMg4 * TM2BAND4[i,j]) + s2TMb4)) + biasnewTMb4)) + s2TMb4);
                 RADS2TM5[i,j] = ((s2TMg5 * ((gainnewTMS2b5 * ((s2TMg5 * TM2BAND5[i,j]) + s2TMb5)) + biasnewTMb5)) + s2TMb5);
                 RADS2TM7[i,j] = ((s2TMg7 * ((gainnewTMS2b7 * ((s2TMg7 * TM2BAND7[i,j]) + s2TMb7)) + biasnewTMb7)) + s2TMb7);
		     }
             }
	 }

CloseRaster(TM2BAND1);
CloseRaster(TM2BAND2);
CloseRaster(TM2BAND3);
CloseRaster(TM2BAND4);
CloseRaster(TM2BAND5);
CloseRaster(TM2BAND7);
printf("Radiance calculation for Scene 2 (TM) is done...\n");
}

else if ( sensors2 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
	raster RADS2MSS1, RADS2MSS2, RADS2MSS3, RADS2MSS4;
	CreateTempRaster(RADS2MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
         if ( s2aq <= 19841109)                                          # Before 9 November 1984
         {
          RADS2MSS1[i,j] = L5B1bef84gain * MSS2BAND1[i,j] + L5B1bef84bias;
          RADS2MSS2[i,j] = L5B2bef84gain * MSS2BAND2[i,j] + L5B2bef84bias;
          RADS2MSS3[i,j] = L5B3bef84gain * MSS2BAND3[i,j] + L5B3bef84bias;
          RADS2MSS4[i,j] = L5B4bef84gain * MSS2BAND4[i,j] + L5B4bef84bias;
         }
         else                                                             # after 9 November 1984
         {
          RADS2MSS1[i,j] = L5B1aft84gain * MSS2BAND1[i,j] + L5B1aft84bias;
          RADS2MSS2[i,j] = L5B2aft84gain * MSS2BAND2[i,j] + L5B2aft84bias;
          RADS2MSS3[i,j] = L5B3aft84gain * MSS2BAND3[i,j] + L5B3aft84bias;
          RADS2MSS4[i,j] = L5B4aft84gain * MSS2BAND4[i,j] + L5B4aft84bias;
         }
	   }
	}

CloseRaster(MSS2BAND1);
CloseRaster(MSS2BAND2);
CloseRaster(MSS2BAND3);
CloseRaster(MSS2BAND4);
printf("Radiance calculation for Scene 2 (MSS) is done...\n");
}

else if ( sensors2 == 4 )                                               # MSS 4
{
	raster RADS2MSS1, RADS2MSS2, RADS2MSS3, RADS2MSS4;
	CreateTempRaster(RADS2MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s2aq <= 19830401)                                        # Before 1 April 1983
          {
          RADS2MSS1[i,j] = L4B1bef83gain * MSS2BAND1[i,j] + L4B1bef83bias;
          RADS2MSS2[i,j] = L4B2bef83gain * MSS2BAND2[i,j] + L4B2bef83bias;
          RADS2MSS3[i,j] = L4B3bef83gain * MSS2BAND3[i,j] + L4B3bef83gain;
          RADS2MSS4[i,j] = L4B4bef83gain * MSS2BAND4[i,j] + L4B4bef83bias;
          }
          else                                                             # After 1 April 1983
          {
          RADS2MSS1[i,j] = L4B1aft83gain * MSS2BAND1[i,j] + L4B1aft83bias;
          RADS2MSS2[i,j] = L4B2aft83gain * MSS2BAND2[i,j] + L4B2aft83bias;
          RADS2MSS3[i,j] = L4B3aft83gain * MSS2BAND3[i,j] + L4B3aft83bias;
          RADS2MSS4[i,j] = L4B4aft83gain * MSS2BAND4[i,j] + L4B4aft83bias;
          }
	  }
	}

CloseRaster(MSS2BAND1);
CloseRaster(MSS2BAND2);
CloseRaster(MSS2BAND3);
CloseRaster(MSS2BAND4);
printf("Radiance calculation for Scene 2 (MSS) is done...\n");
}

else if ( sensors2 == 3 )                                               # MSS 3
{
	raster RADS2MSS1, RADS2MSS2, RADS2MSS3, RADS2MSS4;
	CreateTempRaster(RADS2MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS4, MSSlins, MSScols,"32-bit float");
    
    for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  if ( s2aq <= 19780601 )                                                  # Before 1 Juni 1978
          {
          RADS2MSS1[i,j] = L3B1bef78gain * MSS2BAND1[i,j] + L3B1bef78bias;
          RADS2MSS2[i,j] = L3B2bef78gain * MSS2BAND2[i,j] + L3B2bef78bias;
          RADS2MSS3[i,j] = L3B3bef78gain * MSS2BAND3[i,j] + L3B3bef78bias;
          RADS2MSS4[i,j] = L3B4bef78gain * MSS2BAND4[i,j] + L3B4bef78bias;
          }
          else if ( s2aq >= 19780602 and s2aq <= 19790201 )                        # 2 Juni 1978  bis  1 Februar 1979
          {
          RADS2MSS1[i,j] = L3B17879gain * MSS2BAND1[i,j] + L3B17879bias;
          RADS2MSS2[i,j] = L3B27879gain * MSS2BAND2[i,j] + L3B27879bias;
          RADS2MSS3[i,j] = L3B37879gain * MSS2BAND3[i,j] + L3B37879bias;
          RADS2MSS4[i,j] = L3B47879gain * MSS2BAND4[i,j] + L3B47879bias;
          }
          else if ( s2aq > 19790201 )                                             # After 1 Februar 1979
          {
          RADS2MSS1[i,j] = L3B1aft79gain * MSS2BAND1[i,j] + L3B1aft79bias;
          RADS2MSS2[i,j] = L3B2aft79gain * MSS2BAND2[i,j] + L3B2aft79bias;
          RADS2MSS3[i,j] = L3B3aft79gain * MSS2BAND3[i,j] + L3B3aft79bias;
          RADS2MSS4[i,j] = L3B4aft79gain * MSS2BAND4[i,j] + L3B4aft79bias;
          }
	   }
    }

CloseRaster(MSS2BAND1);
CloseRaster(MSS2BAND2);
CloseRaster(MSS2BAND3);
CloseRaster(MSS2BAND4);
printf("Radiance calculation for Scene 2 (MSS) is done...\n");
}

else if ( sensors2 == 2 )                                               # MSS 2
{
	raster RADS2MSS1, RADS2MSS2, RADS2MSS3, RADS2MSS4;
	CreateTempRaster(RADS2MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s2aq <= 19750716 )                                        # Before 16 Juli 1975
          {
          RADS2MSS1[i,j] = L2B1bef75gain * MSS2BAND1[i,j] + L2B1bef75bias;
          RADS2MSS2[i,j] = L2B2bef75gain * MSS2BAND2[i,j] + L2B2bef75bias;
          RADS2MSS3[i,j] = L2B3bef75gain * MSS2BAND3[i,j] + L2B3bef75bias;
          RADS2MSS4[i,j] = L2B4bef75gain * MSS2BAND4[i,j] + L2B4bef75bias;
          }
          else if ( s2aq > 19750716 and s2aq <= 19790201 )
          {
          RADS2MSS1[i,j] = L2B1aft75gain * MSS2BAND1[i,j] + L2B1aft75bias;                     # After 16. Juli 1975
          RADS2MSS2[i,j] = L2B2aft75gain * MSS2BAND2[i,j] + L2B2aft75bias;
          RADS2MSS3[i,j] = L2B3aft75gain * MSS2BAND3[i,j] + L2B3aft75bias;
          RADS2MSS4[i,j] = L2B4aft75gain * MSS2BAND4[i,j] + L2B4aft75bias;
          }
          else if ( s2aq > 19790201 )
          {
          RADS2MSS1[i,j] = L2B1aft79gain * MSS2BAND1[i,j] + L2B1aft79bias;                    # After 1. Februar 1979
          RADS2MSS2[i,j] = L2B2aft79gain * MSS2BAND2[i,j] + L2B2aft79bias;
          RADS2MSS3[i,j] = L2B3aft79gain * MSS2BAND3[i,j] + L2B3aft79bias;
          RADS2MSS4[i,j] = L2B4aft79gain * MSS2BAND4[i,j] + L2B4aft79bias;
          }
	   }  
    }

CloseRaster(MSS2BAND1);
CloseRaster(MSS2BAND2);
CloseRaster(MSS2BAND3);
CloseRaster(MSS2BAND4);
printf("Radiance calculation for Scene 2 (MSS) is done...\n");
}    

else if ( sensors2 == 1 )                                               # MSS 1
{
	raster RADS2MSS1, RADS2MSS2, RADS2MSS3, RADS2MSS4;
	CreateTempRaster(RADS2MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS2MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  RADS2MSS1[i,j] = L1B1gain * MSS2BAND1[i,j] + L1B1bias;
          RADS2MSS2[i,j] = L1B2gain * MSS2BAND2[i,j] + L1B1bias;
          RADS2MSS3[i,j] = L1B3gain * MSS2BAND3[i,j] + L1B1bias;
          RADS2MSS4[i,j] = L1B4gain * MSS2BAND4[i,j] + L1B1bias;
	   }
	}

CloseRaster(MSS2BAND1);
CloseRaster(MSS2BAND2);
CloseRaster(MSS2BAND3);
CloseRaster(MSS2BAND4);
printf("Radiance calculation for Scene 2 (MSS) is done...\n");
}

###########################
### Radiance Scene 3
###########################

if ( sensors3 == 7 )
{
raster RADS3ETM1, RADS3ETM2, RADS3ETM3, RADS3ETM4, RADS3ETM5, RADS3ETM7;
CreateTempRaster(RADS3ETM1, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS3ETM2, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS3ETM3, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS3ETM4, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS3ETM5, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS3ETM7, ETMlins, ETMcols,"32-bit float");

  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
		if ( s3aq < 20000701)					# Before 1. July 2000
		{
			if ( earSurCat3 == 8 )				# Desert(hs)
			{
			RADS3ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS3ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS3ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS3ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS3ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS3ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat3 == 7 )			# Desert(ms)
			{
			RADS3ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS3ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS3ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS3ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS3ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS3ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat3 == 6 )			# Desert Ice(ls)
			{
			RADS3ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS3ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS3ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS3ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS3ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS3ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat3 == 5 )			# Ice(hs)
			{
			RADS3ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS3ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS3ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS3ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS3ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS3ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat3 == 4 )			# Land(hs)
			{
			RADS3ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS3ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS3ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS3ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS3ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS3ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat3 == 3 )			# Land Ocean(ls
			{
			RADS3ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS3ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS3ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS3ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS3ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS3ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat3 == 2 )			# Water
			{
			RADS3ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS3ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS3ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS3ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS3ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS3ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat3 == 1 )			# Night(volcanos
			{
			RADS3ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS3ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS3ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS3ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS3ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS3ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
		}
		else								# After 1. July 2000
		{
			if ( earSurCat3 == 8 )				# Desert(hs)
			{
			RADS3ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS3ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS3ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS3ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS3ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS3ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat3 == 7 )			# Desert(ms)
			{
			RADS3ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS3ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS3ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS3ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS3ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS3ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat3 == 6 )			# Desert Ice(ls)
			{
			RADS3ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS3ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS3ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS3ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS3ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS3ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat3 == 5 )			# Ice(hs)
			{
			RADS3ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS3ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS3ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS3ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS3ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS3ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat3 == 4 )			# Land(hs)
			{
			RADS3ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS3ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS3ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS3ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS3ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS3ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat3 == 3 )			# Land Ocean(ls
			{
			RADS3ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS3ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS3ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS3ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS3ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS3ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat3 == 2 )			# Water
			{
			RADS3ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS3ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS3ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS3ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS3ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS3ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat3 == 1 )			# Night(volcanos
			{
			RADS3ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS3ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS3ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS3ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS3ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS3ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM3BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
		}
	 }
  }
  
CloseRaster(ETM3BAND1);
CloseRaster(ETM3BAND2);
CloseRaster(ETM3BAND3);
CloseRaster(ETM3BAND4);
CloseRaster(ETM3BAND5);
CloseRaster(ETM3BAND7);
printf("Radiance calculation for Scene 3 (ETM) is done...\n");
}

else if ( sensors3 == 6 )
{
    raster RADS3TM1, RADS3TM2, RADS3TM3, RADS3TM4, RADS3TM5, RADS3TM7;
    CreateTempRaster(RADS3TM1, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS3TM2, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS3TM3, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS3TM4, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS3TM5, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS3TM7, TMlins, TMcols,"32-bit float");

	   for i = 1 to TMlins
	   {
	        for j = 1 to TMcols
	        {
      	     if (s3leap == 0)
		     {
		     numeric gainnewTMS3b1, gainnewTMS3b2, gainnewTMS3b3, gainnewTMS3b4, gainnewTMS3b5, gainnewTMS3b7;

	             gainnewTMS3b1 = ((0.145658 * exp(-0.955113 * ((s3year + (s3jul / 365)) - 1984.2082))) + 1.243000);
	             gainnewTMS3b2 = ((0.058651 * exp(-0.835952 * ((s3year + (s3jul / 365)) - 1984.2082))) + 0.656100);
                 gainnewTMS3b3 = ((0.111858 * exp(-1.002090 * ((s3year + (s3jul / 365)) - 1984.2082))) + 0.905000);
                 gainnewTMS3b4 = ((0.107650 * exp(-1.277190 * ((s3year + (s3jul / 365)) - 1984.2082))) + 1.082000);
                 gainnewTMS3b5 = ((0.243364 * exp(-1.207080 * ((s3year + (s3jul / 365)) - 1984.2082))) + 7.944000);
                 gainnewTMS3b7 = ((0.403551 * exp(-0.999127 * ((s3year + (s3jul / 365)) - 1984.2082))) + 14.52000);

                 RADS3TM1[i,j] = ((s3TMg1 * ((gainnewTMS3b1 * ((s3TMg1 * TM3BAND1[i,j]) + s3TMb1)) + biasnewTMb1)) + s3TMb1);
                 RADS3TM2[i,j] = ((s3TMg2 * ((gainnewTMS3b2 * ((s3TMg2 * TM3BAND2[i,j]) + s3TMb2)) + biasnewTMb2)) + s3TMb2);
                 RADS3TM3[i,j] = ((s3TMg3 * ((gainnewTMS3b3 * ((s3TMg3 * TM3BAND3[i,j]) + s3TMb3)) + biasnewTMb3)) + s3TMb3);
                 RADS3TM4[i,j] = ((s3TMg4 * ((gainnewTMS3b4 * ((s3TMg4 * TM3BAND4[i,j]) + s3TMb4)) + biasnewTMb4)) + s3TMb4);
                 RADS3TM5[i,j] = ((s3TMg5 * ((gainnewTMS3b5 * ((s3TMg5 * TM3BAND5[i,j]) + s3TMb5)) + biasnewTMb5)) + s3TMb5);
                 RADS3TM7[i,j] = ((s3TMg7 * ((gainnewTMS3b7 * ((s3TMg7 * TM3BAND7[i,j]) + s3TMb7)) + biasnewTMb7)) + s3TMb7);
		     }

		     else if (s3leap == 1)
		     {
		     numeric gainnewTMS3b1, gainnewTMS3b2, gainnewTMS3b3, gainnewTMS3b4, gainnewTMS3b5, gainnewTMS3b7;

	             gainnewTMS3b1 = ((0.145658 * exp(-0.955113 * ((s3year + (s3jul / 366)) - 1984.2082))) + 1.243000);
	             gainnewTMS3b2 = ((0.058651 * exp(-0.835952 * ((s3year + (s3jul / 366)) - 1984.2082))) + 0.656100);
                 gainnewTMS3b3 = ((0.111858 * exp(-1.002090 * ((s3year + (s3jul / 366)) - 1984.2082))) + 0.905000);
                 gainnewTMS3b4 = ((0.107650 * exp(-1.277190 * ((s3year + (s3jul / 366)) - 1984.2082))) + 1.082000);
                 gainnewTMS3b5 = ((0.243364 * exp(-1.207080 * ((s3year + (s3jul / 366)) - 1984.2082))) + 7.944000);
                 gainnewTMS3b7 = ((0.403551 * exp(-0.999127 * ((s3year + (s3jul / 366)) - 1984.2082))) + 14.52000);

                 RADS3TM1[i,j] = ((s3TMg1 * ((gainnewTMS3b1 * ((s3TMg1 * TM3BAND1[i,j]) + s3TMb1)) + biasnewTMb1)) + s3TMb1);
                 RADS3TM2[i,j] = ((s3TMg2 * ((gainnewTMS3b2 * ((s3TMg2 * TM3BAND2[i,j]) + s3TMb2)) + biasnewTMb2)) + s3TMb2);
                 RADS3TM3[i,j] = ((s3TMg3 * ((gainnewTMS3b3 * ((s3TMg3 * TM3BAND3[i,j]) + s3TMb3)) + biasnewTMb3)) + s3TMb3);
                 RADS3TM4[i,j] = ((s3TMg4 * ((gainnewTMS3b4 * ((s3TMg4 * TM3BAND4[i,j]) + s3TMb4)) + biasnewTMb4)) + s3TMb4);
                 RADS3TM5[i,j] = ((s3TMg5 * ((gainnewTMS3b5 * ((s3TMg5 * TM3BAND5[i,j]) + s3TMb5)) + biasnewTMb5)) + s3TMb5);
                 RADS3TM7[i,j] = ((s3TMg7 * ((gainnewTMS3b7 * ((s3TMg7 * TM3BAND7[i,j]) + s3TMb7)) + biasnewTMb7)) + s3TMb7);
		     }
	       }
	 }

CloseRaster(TM3BAND1);
CloseRaster(TM3BAND2);
CloseRaster(TM3BAND3);
CloseRaster(TM3BAND4);
CloseRaster(TM3BAND5);
CloseRaster(TM3BAND7);
printf("Radiance calculation for Scene 3 (TM) is done...\n");
}

else if ( sensors3 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
	raster RADS3MSS1, RADS3MSS2, RADS3MSS3, RADS3MSS4;
	CreateTempRaster(RADS3MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
         if ( s3aq <= 19841109)                                          # Before 9 November 1984
         {
          RADS3MSS1[i,j] = L5B1bef84gain * MSS3BAND1[i,j] + L5B1bef84bias;
          RADS3MSS2[i,j] = L5B2bef84gain * MSS3BAND2[i,j] + L5B2bef84bias;
          RADS3MSS3[i,j] = L5B3bef84gain * MSS3BAND3[i,j] + L5B3bef84bias;
          RADS3MSS4[i,j] = L5B4bef84gain * MSS3BAND4[i,j] + L5B4bef84bias;
         }
         else                                                             # after 9 November 1984
         {
          RADS3MSS1[i,j] = L5B1aft84gain * MSS3BAND1[i,j] + L5B1aft84bias;
          RADS3MSS2[i,j] = L5B2aft84gain * MSS3BAND2[i,j] + L5B2aft84bias;
          RADS3MSS3[i,j] = L5B3aft84gain * MSS3BAND3[i,j] + L5B3aft84bias;
          RADS3MSS4[i,j] = L5B4aft84gain * MSS3BAND4[i,j] + L5B4aft84bias;
         }
	   }
	}

CloseRaster(MSS3BAND1);
CloseRaster(MSS3BAND2);
CloseRaster(MSS3BAND3);
CloseRaster(MSS3BAND4);
printf("Radiance calculation for Scene 3 (MSS) is done...\n");
}

else if ( sensors3 == 4 )                                               # MSS 4
{
	raster RADS3MSS1, RADS3MSS2, RADS3MSS3, RADS3MSS4;
	CreateTempRaster(RADS3MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s3aq <= 19830401)                                        # Before 1 April 1983
          {
          RADS3MSS1[i,j] = L4B1bef83gain * MSS3BAND1[i,j] + L4B1bef83bias;
          RADS3MSS2[i,j] = L4B2bef83gain * MSS3BAND2[i,j] + L4B2bef83bias;
          RADS3MSS3[i,j] = L4B3bef83gain * MSS3BAND3[i,j] + L4B3bef83gain;
          RADS3MSS4[i,j] = L4B4bef83gain * MSS3BAND4[i,j] + L4B4bef83bias;
          }
          else                                                             # After 1 April 1983
          {
          RADS3MSS1[i,j] = L4B1aft83gain * MSS3BAND1[i,j] + L4B1aft83bias;
          RADS3MSS2[i,j] = L4B2aft83gain * MSS3BAND2[i,j] + L4B2aft83bias;
          RADS3MSS3[i,j] = L4B3aft83gain * MSS3BAND3[i,j] + L4B3aft83bias;
          RADS3MSS4[i,j] = L4B4aft83gain * MSS3BAND4[i,j] + L4B4aft83bias;
          }
	  }
	}

CloseRaster(MSS3BAND1);
CloseRaster(MSS3BAND2);
CloseRaster(MSS3BAND3);
CloseRaster(MSS3BAND4);
printf("Radiance calculation for Scene 3 (MSS) is done...\n");
}

else if ( sensors3 == 3 )                                               # MSS 3
{
	raster RADS3MSS1, RADS3MSS2, RADS3MSS3, RADS3MSS4;
	CreateTempRaster(RADS3MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS4, MSSlins, MSScols,"32-bit float");
    
    for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  if ( s3aq <= 19780601 )                                                  # Before 1 Juni 1978
          {
          RADS3MSS1[i,j] = L3B1bef78gain * MSS3BAND1[i,j] + L3B1bef78bias;
          RADS3MSS2[i,j] = L3B2bef78gain * MSS3BAND2[i,j] + L3B2bef78bias;
          RADS3MSS3[i,j] = L3B3bef78gain * MSS3BAND3[i,j] + L3B3bef78bias;
          RADS3MSS4[i,j] = L3B4bef78gain * MSS3BAND4[i,j] + L3B4bef78bias;
          }
          else if ( s3aq >= 19780602 and s3aq <= 19790201 )                        # 2 Juni 1978  bis  1 Februar 1979
          {
          RADS3MSS1[i,j] = L3B17879gain * MSS3BAND1[i,j] + L3B17879bias;
          RADS3MSS2[i,j] = L3B27879gain * MSS3BAND2[i,j] + L3B27879bias;
          RADS3MSS3[i,j] = L3B37879gain * MSS3BAND3[i,j] + L3B37879bias;
          RADS3MSS4[i,j] = L3B47879gain * MSS3BAND4[i,j] + L3B47879bias;
          }
          else if ( s3aq > 19790201 )                                             # After 1 Februar 1979
          {
          RADS3MSS1[i,j] = L3B1aft79gain * MSS3BAND1[i,j] + L3B1aft79bias;
          RADS3MSS2[i,j] = L3B2aft79gain * MSS3BAND2[i,j] + L3B2aft79bias;
          RADS3MSS3[i,j] = L3B3aft79gain * MSS3BAND3[i,j] + L3B3aft79bias;
          RADS3MSS4[i,j] = L3B4aft79gain * MSS3BAND4[i,j] + L3B4aft79bias;
          }
	   }
    }

CloseRaster(MSS3BAND1);
CloseRaster(MSS3BAND2);
CloseRaster(MSS3BAND3);
CloseRaster(MSS3BAND4);
printf("Radiance calculation for Scene 3 (MSS) is done...\n");
}

else if ( sensors3 == 2 )                                               # MSS 2
{
	raster RADS3MSS1, RADS3MSS2, RADS3MSS3, RADS3MSS4;
	CreateTempRaster(RADS3MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s3aq <= 19750716 )                                        # Before 16 Juli 1975
          {
          RADS3MSS1[i,j] = L2B1bef75gain * MSS3BAND1[i,j] + L2B1bef75bias;
          RADS3MSS2[i,j] = L2B2bef75gain * MSS3BAND2[i,j] + L2B2bef75bias;
          RADS3MSS3[i,j] = L2B3bef75gain * MSS3BAND3[i,j] + L2B3bef75bias;
          RADS3MSS4[i,j] = L2B4bef75gain * MSS3BAND4[i,j] + L2B4bef75bias;
          }
          else if ( s3aq > 19750716 and s3aq <= 19790201 )
          {
          RADS3MSS1[i,j] = L2B1aft75gain * MSS3BAND1[i,j] + L2B1aft75bias;                     # After 16. Juli 1975
          RADS3MSS2[i,j] = L2B2aft75gain * MSS3BAND2[i,j] + L2B2aft75bias;
          RADS3MSS3[i,j] = L2B3aft75gain * MSS3BAND3[i,j] + L2B3aft75bias;
          RADS3MSS4[i,j] = L2B4aft75gain * MSS3BAND4[i,j] + L2B4aft75bias;
          }
          else if ( s3aq > 19790201 )
          {
          RADS3MSS1[i,j] = L2B1aft79gain * MSS3BAND1[i,j] + L2B1aft79bias;                    # After 1. Februar 1979
          RADS3MSS2[i,j] = L2B2aft79gain * MSS3BAND2[i,j] + L2B2aft79bias;
          RADS3MSS3[i,j] = L2B3aft79gain * MSS3BAND3[i,j] + L2B3aft79bias;
          RADS3MSS4[i,j] = L2B4aft79gain * MSS3BAND4[i,j] + L2B4aft79bias;
          }
	   }  
    }

CloseRaster(MSS3BAND1);
CloseRaster(MSS3BAND2);
CloseRaster(MSS3BAND3);
CloseRaster(MSS3BAND4);
printf("Radiance calculation for Scene 3 (MSS) is done...\n");
}    

else if ( sensors3 == 1 )                                               # MSS 1
{
	raster RADS3MSS1, RADS3MSS2, RADS3MSS3, RADS3MSS4;
	CreateTempRaster(RADS3MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS3MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	    RADS3MSS1[i,j] = L1B1gain * MSS3BAND1[i,j] + L1B1bias;
          RADS3MSS2[i,j] = L1B2gain * MSS3BAND2[i,j] + L1B1bias;
          RADS3MSS3[i,j] = L1B3gain * MSS3BAND3[i,j] + L1B1bias;
          RADS3MSS4[i,j] = L1B4gain * MSS3BAND4[i,j] + L1B1bias;
	   }
	}

CloseRaster(MSS3BAND1);
CloseRaster(MSS3BAND2);
CloseRaster(MSS3BAND3);
CloseRaster(MSS3BAND4);
printf("Radiance calculation for Scene 3 (MSS) is done...\n");
}

###########################
### Radiance Scene 4
###########################

if ( sensors4 == 7 )
{
raster RADS4ETM1, RADS4ETM2, RADS4ETM3, RADS4ETM4, RADS4ETM5, RADS4ETM7;
CreateTempRaster(RADS4ETM1, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS4ETM2, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS4ETM3, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS4ETM4, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS4ETM5, ETMlins, ETMcols,"32-bit float");
CreateTempRaster(RADS4ETM7, ETMlins, ETMcols,"32-bit float");

  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
		if ( s4aq < 20000701)					# Before 1. July 2000
		{
			if ( earSurCat4 == 8 )				# Desert(hs)
			{
			RADS4ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS4ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS4ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS4ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS4ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS4ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat4 == 7 )			# Desert(ms)
			{
			RADS4ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS4ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS4ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS4ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS4ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS4ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
			else if ( earSurCat4 == 6 )			# Desert Ice(ls)
			{
			RADS4ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS4ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS4ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS4ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS4ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS4ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat4 == 5 )			# Ice(hs)
			{
			RADS4ETM1[i,j] = (B1LGLMAXBef2000-B1LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINBef2000;
			RADS4ETM2[i,j] = (B2LGLMAXBef2000-B2LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINBef2000;
			RADS4ETM3[i,j] = (B3LGLMAXBef2000-B3LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINBef2000;
			RADS4ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS4ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS4ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat4 == 4 )			# Land(hs)
			{
			RADS4ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS4ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS4ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS4ETM4[i,j] = (B4LGLMAXBef2000-B4LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINBef2000;
			RADS4ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS4ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat4 == 3 )			# Land Ocean(ls
			{
			RADS4ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS4ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS4ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS4ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS4ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS4ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat4 == 2 )			# Water
			{
			RADS4ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS4ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS4ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS4ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS4ETM5[i,j] = (B5HGLMAXBef2000-B5HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINBef2000;
			RADS4ETM7[i,j] = (B7HGLMAXBef2000-B7HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINBef2000;
			}
			else if ( earSurCat4 == 1 )			# Night(volcanos
			{
			RADS4ETM1[i,j] = (B1HGLMAXBef2000-B1HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINBef2000;
			RADS4ETM2[i,j] = (B2HGLMAXBef2000-B2HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINBef2000;
			RADS4ETM3[i,j] = (B3HGLMAXBef2000-B3HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINBef2000;
			RADS4ETM4[i,j] = (B4HGLMAXBef2000-B4HGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINBef2000;
			RADS4ETM5[i,j] = (B5LGLMAXBef2000-B5LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINBef2000;
			RADS4ETM7[i,j] = (B7LGLMAXBef2000-B7LGLMINBef2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINBef2000;
			}
		}
		else								# After 1. July 2000
		{
			if ( earSurCat4 == 8 )				# Desert(hs)
			{
			RADS4ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS4ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS4ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS4ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS4ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS4ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat4 == 7 )			# Desert(ms)
			{
			RADS4ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS4ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS4ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS4ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS4ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS4ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
			else if ( earSurCat4 == 6 )			# Desert Ice(ls)
			{
			RADS4ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS4ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS4ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS4ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS4ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS4ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat4 == 5 )			# Ice(hs)
			{
			RADS4ETM1[i,j] = (B1LGLMAXAFT2000-B1LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1LGLMINAFT2000;
			RADS4ETM2[i,j] = (B2LGLMAXAFT2000-B2LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2LGLMINAFT2000;
			RADS4ETM3[i,j] = (B3LGLMAXAFT2000-B3LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3LGLMINAFT2000;
			RADS4ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS4ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS4ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat4 == 4 )			# Land(hs)
			{
			RADS4ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS4ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS4ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS4ETM4[i,j] = (B4LGLMAXAFT2000-B4LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4LGLMINAFT2000;
			RADS4ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS4ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat4 == 3 )			# Land Ocean(ls
			{
			RADS4ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS4ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS4ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS4ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS4ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS4ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat4 == 2 )			# Water
			{
			RADS4ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS4ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS4ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS4ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS4ETM5[i,j] = (B5HGLMAXAFT2000-B5HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5HGLMINAFT2000;
			RADS4ETM7[i,j] = (B7HGLMAXAFT2000-B7HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7HGLMINAFT2000;
			}
			else if ( earSurCat4 == 1 )			# Night(volcanos
			{
			RADS4ETM1[i,j] = (B1HGLMAXAFT2000-B1HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND1[i,j]-QCALMIN)+B1HGLMINAFT2000;
			RADS4ETM2[i,j] = (B2HGLMAXAFT2000-B2HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND2[i,j]-QCALMIN)+B2HGLMINAFT2000;
			RADS4ETM3[i,j] = (B3HGLMAXAFT2000-B3HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND3[i,j]-QCALMIN)+B3HGLMINAFT2000;
			RADS4ETM4[i,j] = (B4HGLMAXAFT2000-B4HGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND4[i,j]-QCALMIN)+B4HGLMINAFT2000;
			RADS4ETM5[i,j] = (B5LGLMAXAFT2000-B5LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND5[i,j]-QCALMIN)+B5LGLMINAFT2000;
			RADS4ETM7[i,j] = (B7LGLMAXAFT2000-B7LGLMINAFT2000)/(QCALMAX-QCALMIN)*(ETM4BAND7[i,j]-QCALMIN)+B7LGLMINAFT2000;
			}
		}
	}
  }

CloseRaster(ETM4BAND1);
CloseRaster(ETM4BAND2);
CloseRaster(ETM4BAND3);
CloseRaster(ETM4BAND4);
CloseRaster(ETM4BAND5);
CloseRaster(ETM4BAND7);
printf("Radiance calculation for Scene 4 (ETM) is done...\n\n");
}

else if ( sensors4 == 6 )
{
    raster RADS4TM1, RADS4TM2, RADS4TM3, RADS4TM4, RADS4TM5, RADS4TM7;
    CreateTempRaster(RADS4TM1, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS4TM2, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS4TM3, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS4TM4, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS4TM5, TMlins, TMcols,"32-bit float");
    CreateTempRaster(RADS4TM7, TMlins, TMcols,"32-bit float");

	   for i = 1 to TMlins
	   {
	        for j = 1 to TMcols
	        {
		     if (s4leap == 0)
		     {
		     numeric gainnewTMS4b1, gainnewTMS4b2, gainnewTMS4b3, gainnewTMS4b4, gainnewTMS4b5, gainnewTMS4b7;

	             gainnewTMS4b1 = ((0.145658 * exp(-0.955113 * ((s4year + (s4jul / 365)) - 1984.2082))) + 1.243000);
	             gainnewTMS4b2 = ((0.058651 * exp(-0.835952 * ((s4year + (s4jul / 365)) - 1984.2082))) + 0.656100);
                 gainnewTMS4b3 = ((0.111858 * exp(-1.002090 * ((s4year + (s4jul / 365)) - 1984.2082))) + 0.905000);
                 gainnewTMS4b4 = ((0.107650 * exp(-1.277190 * ((s4year + (s4jul / 365)) - 1984.2082))) + 1.082000);
                 gainnewTMS4b5 = ((0.243364 * exp(-1.207080 * ((s4year + (s4jul / 365)) - 1984.2082))) + 7.944000);
                 gainnewTMS4b7 = ((0.403551 * exp(-0.999127 * ((s4year + (s4jul / 365)) - 1984.2082))) + 14.52000);

                 RADS4TM1[i,j] = ((s4TMg1 * ((gainnewTMS4b1 * ((s4TMg1 * TM4BAND1[i,j]) + s4TMb1)) + biasnewTMb1)) + s4TMb1);
                 RADS4TM2[i,j] = ((s4TMg2 * ((gainnewTMS4b2 * ((s4TMg2 * TM4BAND2[i,j]) + s4TMb2)) + biasnewTMb2)) + s4TMb2);
                 RADS4TM3[i,j] = ((s4TMg3 * ((gainnewTMS4b3 * ((s4TMg3 * TM4BAND3[i,j]) + s4TMb3)) + biasnewTMb3)) + s4TMb3);
                 RADS4TM4[i,j] = ((s4TMg4 * ((gainnewTMS4b4 * ((s4TMg4 * TM4BAND4[i,j]) + s4TMb4)) + biasnewTMb4)) + s4TMb4);
                 RADS4TM5[i,j] = ((s4TMg5 * ((gainnewTMS4b5 * ((s4TMg5 * TM4BAND5[i,j]) + s4TMb5)) + biasnewTMb5)) + s4TMb5);
                 RADS4TM7[i,j] = ((s4TMg7 * ((gainnewTMS4b7 * ((s4TMg7 * TM4BAND7[i,j]) + s4TMb7)) + biasnewTMb7)) + s4TMb7);
		     }

		     else if (s4leap == 1)
		     {
		     numeric gainnewTMS4b1, gainnewTMS4b2, gainnewTMS4b3, gainnewTMS4b4, gainnewTMS4b5, gainnewTMS4b7;

	           gainnewTMS4b1 = ((0.145658 * exp(-0.955113 * ((s4year + (s4jul / 366)) - 1984.2082))) + 1.243000);
	           gainnewTMS4b2 = ((0.058651 * exp(-0.835952 * ((s4year + (s4jul / 366)) - 1984.2082))) + 0.656100);
                 gainnewTMS4b3 = ((0.111858 * exp(-1.002090 * ((s4year + (s4jul / 366)) - 1984.2082))) + 0.905000);
                 gainnewTMS4b4 = ((0.107650 * exp(-1.277190 * ((s4year + (s4jul / 366)) - 1984.2082))) + 1.082000);
                 gainnewTMS4b5 = ((0.243364 * exp(-1.207080 * ((s4year + (s4jul / 366)) - 1984.2082))) + 7.944000);
                 gainnewTMS4b7 = ((0.403551 * exp(-0.999127 * ((s4year + (s4jul / 366)) - 1984.2082))) + 14.52000);

                 RADS4TM1[i,j] = ((s4TMg1 * ((gainnewTMS4b1 * ((s4TMg1 * TM4BAND1[i,j]) + s4TMb1)) + biasnewTMb1)) + s4TMb1);
                 RADS4TM2[i,j] = ((s4TMg2 * ((gainnewTMS4b2 * ((s4TMg2 * TM4BAND2[i,j]) + s4TMb2)) + biasnewTMb2)) + s4TMb2);
                 RADS4TM3[i,j] = ((s4TMg3 * ((gainnewTMS4b3 * ((s4TMg3 * TM4BAND3[i,j]) + s4TMb3)) + biasnewTMb3)) + s4TMb3);
                 RADS4TM4[i,j] = ((s4TMg4 * ((gainnewTMS4b4 * ((s4TMg4 * TM4BAND4[i,j]) + s4TMb4)) + biasnewTMb4)) + s4TMb4);
                 RADS4TM5[i,j] = ((s4TMg5 * ((gainnewTMS4b5 * ((s4TMg5 * TM4BAND5[i,j]) + s4TMb5)) + biasnewTMb5)) + s4TMb5);
                 RADS4TM7[i,j] = ((s4TMg7 * ((gainnewTMS4b7 * ((s4TMg7 * TM4BAND7[i,j]) + s4TMb7)) + biasnewTMb7)) + s4TMb7);
		     }
 	       }
	 }

CloseRaster(TM4BAND1);
CloseRaster(TM4BAND2);
CloseRaster(TM4BAND3);
CloseRaster(TM4BAND4);
CloseRaster(TM4BAND5);
CloseRaster(TM4BAND7);
printf("Radiance calculation for Scene 4 (TM) is done...\n\n");
}

else if ( sensors4 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
	raster RADS4MSS1, RADS4MSS2, RADS4MSS3, RADS4MSS4;
	CreateTempRaster(RADS4MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
         if ( s4aq <= 19841109 )                                          # Before 9 November 1984
         {
          RADS4MSS1[i,j] = L5B1bef84gain * MSS4BAND1[i,j] + L5B1bef84bias;
          RADS4MSS2[i,j] = L5B2bef84gain * MSS4BAND2[i,j] + L5B2bef84bias;
          RADS4MSS3[i,j] = L5B3bef84gain * MSS4BAND3[i,j] + L5B3bef84bias;
          RADS4MSS4[i,j] = L5B4bef84gain * MSS4BAND4[i,j] + L5B4bef84bias;
         }
         else                                                             # after 9 November 1984
         {
          RADS4MSS1[i,j] = L5B1aft84gain * MSS4BAND1[i,j] + L5B1aft84bias;
          RADS4MSS2[i,j] = L5B2aft84gain * MSS4BAND2[i,j] + L5B2aft84bias;
          RADS4MSS3[i,j] = L5B3aft84gain * MSS4BAND3[i,j] + L5B3aft84bias;
          RADS4MSS4[i,j] = L5B4aft84gain * MSS4BAND4[i,j] + L5B4aft84bias;
         }
	   }
	}

CloseRaster(MSS4BAND1);
CloseRaster(MSS4BAND2);
CloseRaster(MSS4BAND3);
CloseRaster(MSS4BAND4);
printf("Radiance calculation for Scene 4 (MSS) is done...\n\n");
}

else if ( sensors4 == 4 )                                               # MSS 4
{
	raster RADS4MSS1, RADS4MSS2, RADS4MSS3, RADS4MSS4;
	CreateTempRaster(RADS4MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s4aq <= 19830401)                                        # Before 1 April 1983
          {
          RADS4MSS1[i,j] = L4B1bef83gain * MSS4BAND1[i,j] + L4B1bef83bias;
          RADS4MSS2[i,j] = L4B2bef83gain * MSS4BAND2[i,j] + L4B2bef83bias;
          RADS4MSS3[i,j] = L4B3bef83gain * MSS4BAND3[i,j] + L4B3bef83gain;
          RADS4MSS4[i,j] = L4B4bef83gain * MSS4BAND4[i,j] + L4B4bef83bias;
          }
          else                                                             # After 1 April 1983
          {
          RADS4MSS1[i,j] = L4B1aft83gain * MSS4BAND1[i,j] + L4B1aft83bias;
          RADS4MSS2[i,j] = L4B2aft83gain * MSS4BAND2[i,j] + L4B2aft83bias;
          RADS4MSS3[i,j] = L4B3aft83gain * MSS4BAND3[i,j] + L4B3aft83bias;
          RADS4MSS4[i,j] = L4B4aft83gain * MSS4BAND4[i,j] + L4B4aft83bias;
          }
	  }
	}

CloseRaster(MSS4BAND1);
CloseRaster(MSS4BAND2);
CloseRaster(MSS4BAND3);
CloseRaster(MSS4BAND4);
printf("Radiance calculation for Scene 4 (MSS) is done...\n\n");
}

else if ( sensors4 == 3 )                                               # MSS 3
{
	raster RADS4MSS1, RADS4MSS2, RADS4MSS3, RADS4MSS4;
	CreateTempRaster(RADS4MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS4, MSSlins, MSScols,"32-bit float");
    
    for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  if ( s4aq <= 19780601 )                                                  # Before 1 Juni 1978
          {
          RADS4MSS1[i,j] = L3B1bef78gain * MSS4BAND1[i,j] + L3B1bef78bias;
          RADS4MSS2[i,j] = L3B2bef78gain * MSS4BAND2[i,j] + L3B2bef78bias;
          RADS4MSS3[i,j] = L3B3bef78gain * MSS4BAND3[i,j] + L3B3bef78bias;
          RADS4MSS4[i,j] = L3B4bef78gain * MSS4BAND4[i,j] + L3B4bef78bias;
          }
          else if ( s4aq >= 19780602 and s4aq <= 19790201 )                        # 2 Juni 1978  bis  1 Februar 1979
          {
          RADS4MSS1[i,j] = L3B17879gain * MSS4BAND1[i,j] + L3B17879bias;
          RADS4MSS2[i,j] = L3B27879gain * MSS4BAND2[i,j] + L3B27879bias;
          RADS4MSS3[i,j] = L3B37879gain * MSS4BAND3[i,j] + L3B37879bias;
          RADS4MSS4[i,j] = L3B47879gain * MSS4BAND4[i,j] + L3B47879bias;
          }
          else if ( s1aq > 19790201 )                                             # After 1 Februar 1979
          {
          RADS4MSS1[i,j] = L3B1aft79gain * MSS4BAND1[i,j] + L3B1aft79bias;
          RADS4MSS2[i,j] = L3B2aft79gain * MSS4BAND2[i,j] + L3B2aft79bias;
          RADS4MSS3[i,j] = L3B3aft79gain * MSS4BAND3[i,j] + L3B3aft79bias;
          RADS4MSS4[i,j] = L3B4aft79gain * MSS4BAND4[i,j] + L3B4aft79bias;
          }
	   }
    }

CloseRaster(MSS4BAND1);
CloseRaster(MSS4BAND2);
CloseRaster(MSS4BAND3);
CloseRaster(MSS4BAND4);
printf("Radiance calculation for Scene 4 (MSS) is done...\n\n");
}

else if ( sensors4 == 2 )                                               # MSS 2
{
	raster RADS4MSS1, RADS4MSS2, RADS4MSS3, RADS4MSS4;
	CreateTempRaster(RADS4MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
	      if ( s4aq <= 19750716 )                                        # Before 16 Juli 1975
          {
          RADS4MSS1[i,j] = L2B1bef75gain * MSS4BAND1[i,j] + L2B1bef75bias;
          RADS4MSS2[i,j] = L2B2bef75gain * MSS4BAND2[i,j] + L2B2bef75bias;
          RADS4MSS3[i,j] = L2B3bef75gain * MSS4BAND3[i,j] + L2B3bef75bias;
          RADS4MSS4[i,j] = L2B4bef75gain * MSS4BAND4[i,j] + L2B4bef75bias;
          }
          else if ( s4aq > 19750716 and s4aq <= 19790201 )
          {
          RADS4MSS1[i,j] = L2B1aft75gain * MSS4BAND1[i,j] + L2B1aft75bias;                     # After 16. Juli 1975
          RADS4MSS2[i,j] = L2B2aft75gain * MSS4BAND2[i,j] + L2B2aft75bias;
          RADS4MSS3[i,j] = L2B3aft75gain * MSS4BAND3[i,j] + L2B3aft75bias;
          RADS4MSS4[i,j] = L2B4aft75gain * MSS4BAND4[i,j] + L2B4aft75bias;
          }
          else if ( s4aq > 19790201 )
          {
          RADS4MSS1[i,j] = L2B1aft79gain * MSS4BAND1[i,j] + L2B1aft79bias;                    # After 1. Februar 1979
          RADS4MSS2[i,j] = L2B2aft79gain * MSS4BAND2[i,j] + L2B2aft79bias;
          RADS4MSS3[i,j] = L2B3aft79gain * MSS4BAND3[i,j] + L2B3aft79bias;
          RADS4MSS4[i,j] = L2B4aft79gain * MSS4BAND4[i,j] + L2B4aft79bias;
          }
	   }  
    }

CloseRaster(MSS4BAND1);
CloseRaster(MSS4BAND2);
CloseRaster(MSS4BAND3);
CloseRaster(MSS4BAND4);
printf("Radiance calculation for Scene 4 (MSS) is done...\n\n");
}    

else if ( sensors4 == 1 )                                               # MSS 1
{
	raster RADS4MSS1, RADS4MSS2, RADS4MSS3, RADS4MSS4;
	CreateTempRaster(RADS4MSS1, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS2, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS3, MSSlins, MSScols,"32-bit float");
    CreateTempRaster(RADS4MSS4, MSSlins, MSScols,"32-bit float");

	for i = 1 to MSSlins
	{
	   for j = 1 to MSScols
	   {
		  RADS4MSS1[i,j] = L1B1gain * MSS4BAND1[i,j] + L1B1bias;
          RADS4MSS2[i,j] = L1B2gain * MSS4BAND2[i,j] + L1B1bias;
          RADS4MSS3[i,j] = L1B3gain * MSS4BAND3[i,j] + L1B1bias;
          RADS4MSS4[i,j] = L1B4gain * MSS4BAND4[i,j] + L1B1bias;
	   }
	}

CloseRaster(MSS4BAND1);
CloseRaster(MSS4BAND2);
CloseRaster(MSS4BAND3);
CloseRaster(MSS4BAND4);
printf("Radiance calculation for Scene 4 (MSS) is done...\n\n");
}

printf("The computation of radiance rasters is done...\n\n\n");

######################################
###### Begin processing.
###### Calculate Refelectance.
######################################

printf( "Starting of to compute the reflectance rasters...\n\n" );

#############################
#### Reflectance Scene 1
#############################

if ( sensors1 == 7 )
{
raster REFS1ETM1, REFS1ETM2, REFS1ETM3, REFS1ETM4, REFS1ETM5, REFS1ETM7;
numeric radsurfETMS1b1, radsurfETMS1b2, radsurfETMS1b3, radsurfETMS1b4, radsurfETMS1b5, radsurfETMS1b7;
numeric valueS1ETM1, valueS1ETM2, valueS1ETM3, valueS1ETM4, valueS1ETM5, valueS1ETM7;

numeric dosS1ETM1nullValCount = 0;
numeric dosS1ETM2nullValCount = 0;
numeric dosS1ETM3nullValCount = 0;
numeric dosS1ETM4nullValCount = 0;
numeric dosS1ETM5nullValCount = 0;
numeric dosS1ETM7nullValCount = 0;

numeric dosS1ETM1realArrayCount = 1;
numeric dosS1ETM2realArrayCount = 1;
numeric dosS1ETM3realArrayCount = 1;
numeric dosS1ETM4realArrayCount = 1;
numeric dosS1ETM5realArrayCount = 1;
numeric dosS1ETM7realArrayCount = 1;

numeric countS1REFnullETM1 = 0;
numeric countS1REFnullETM2 = 0;
numeric countS1REFnullETM3 = 0;
numeric countS1REFnullETM4 = 0;
numeric countS1REFnullETM5 = 0;
numeric countS1REFnullETM7 = 0;

numeric countS1REFoneETM1 = 0;
numeric countS1REFoneETM2 = 0;
numeric countS1REFoneETM3 = 0;
numeric countS1REFoneETM4 = 0;
numeric countS1REFoneETM5 = 0;
numeric countS1REFoneETM7 = 0;

radsurfETMS1b1 = ESETM1 / (dist1^2);
radsurfETMS1b2 = ESETM2 / (dist1^2);
radsurfETMS1b3 = ESETM3 / (dist1^2);
radsurfETMS1b4 = ESETM4 / (dist1^2);
radsurfETMS1b5 = ESETM5 / (dist1^2);
radsurfETMS1b7 = ESETM7 / (dist1^2);

printf("Surface Radiance ETM Band 1 in W/m^2µm (Scene 1): %12f\n", radsurfETMS1b1);
printf("Surface Radiance ETM Band 2 in W/m^2µm (Scene 1): %12f\n", radsurfETMS1b2);
printf("Surface Radiance ETM Band 3 in W/m^2µm (Scene 1): %12f\n", radsurfETMS1b3);
printf("Surface Radiance ETM Band 4 in W/m^2µm (Scene 1): %12f\n", radsurfETMS1b4);
printf("Surface Radiance ETM Band 5 in W/m^2µm (Scene 1): %12f\n", radsurfETMS1b5);
printf("Surface Radiance ETM Band 7 in W/m^2µm (Scene 1): %12f\n\n", radsurfETMS1b7);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {       
		if (IsNull(RADS1ETM1[i,j]) == 1)
		{
		dosS1ETM1nullValCount = dosS1ETM1nullValCount + 1;
		}
		if (IsNull(RADS1ETM2[i,j]) == 1)
		{
		dosS1ETM2nullValCount = dosS1ETM2nullValCount + 1;
		}		 
		if (IsNull(RADS1ETM3[i,j]) == 1)
		{
		dosS1ETM3nullValCount = dosS1ETM3nullValCount + 1;
		}		 
		if (IsNull(RADS1ETM4[i,j]) == 1)
		{
		dosS1ETM4nullValCount = dosS1ETM4nullValCount + 1;
		}		 
		if (IsNull(RADS1ETM5[i,j]) == 1)
		{
		dosS1ETM5nullValCount = dosS1ETM5nullValCount + 1;
		}		 
		if (IsNull(RADS1ETM7[i,j]) == 1)
		{
		dosS1ETM7nullValCount = dosS1ETM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS1ETM1realSize = (ETMlins * ETMcols) - dosS1ETM1nullValCount;
numeric dosS1ETM2realSize = (ETMlins * ETMcols) - dosS1ETM2nullValCount;
numeric dosS1ETM3realSize = (ETMlins * ETMcols) - dosS1ETM3nullValCount;
numeric dosS1ETM4realSize = (ETMlins * ETMcols) - dosS1ETM4nullValCount;
numeric dosS1ETM5realSize = (ETMlins * ETMcols) - dosS1ETM5nullValCount;
numeric dosS1ETM7realSize = (ETMlins * ETMcols) - dosS1ETM7nullValCount;

array allValuesDosS1ETM1[dosS1ETM1realSize];
array allValuesDosS1ETM2[dosS1ETM2realSize];
array allValuesDosS1ETM3[dosS1ETM3realSize];
array allValuesDosS1ETM4[dosS1ETM4realSize];
array allValuesDosS1ETM5[dosS1ETM5realSize];
array allValuesDosS1ETM7[dosS1ETM7realSize];

numeric DNminCALCS1ETM1 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM1realSize);
numeric DNminCALCS1ETM2 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM2realSize);
numeric DNminCALCS1ETM3 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM3realSize);
numeric DNminCALCS1ETM4 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM4realSize);
numeric DNminCALCS1ETM5 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM5realSize);
numeric DNminCALCS1ETM7 = DNminCALC(Scene1Lin, Scene1Col, dosS1ETM7realSize);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(RADS1ETM1[i,j]) == 0)
		{
		  allValuesDosS1ETM1[dosS1ETM1realArrayCount] = RADS1ETM1[i,j];		 
		  dosS1ETM1realArrayCount = dosS1ETM1realArrayCount + 1;
		}
		if (IsNull(RADS1ETM2[i,j]) == 0)
		{
		  allValuesDosS1ETM2[dosS1ETM2realArrayCount] = RADS1ETM2[i,j];		 
		  dosS1ETM2realArrayCount = dosS1ETM2realArrayCount + 1;
		}
		if (IsNull(RADS1ETM3[i,j]) == 0)
		{
		  allValuesDosS1ETM3[dosS1ETM3realArrayCount] = RADS1ETM3[i,j];		 
		  dosS1ETM3realArrayCount = dosS1ETM3realArrayCount + 1;
		}
		if (IsNull(RADS1ETM4[i,j]) == 0)
		{
		  allValuesDosS1ETM4[dosS1ETM4realArrayCount] = RADS1ETM4[i,j];		 
		  dosS1ETM4realArrayCount = dosS1ETM4realArrayCount + 1;
		}
		if (IsNull(RADS1ETM5[i,j]) == 0)
		{
		  allValuesDosS1ETM5[dosS1ETM5realArrayCount] = RADS1ETM5[i,j];		 
		  dosS1ETM5realArrayCount = dosS1ETM5realArrayCount + 1;
		}
		if (IsNull(RADS1ETM7[i,j]) == 0)
		{
		  allValuesDosS1ETM7[dosS1ETM7realArrayCount] = RADS1ETM7[i,j];		 
		  dosS1ETM7realArrayCount = dosS1ETM7realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS1ETM1 = dosS1ETM1realSize;
numeric hDOSS1ETM1 = 1;

while ( (hDOSS1ETM1 * 3 + 1) < lastDOSS1ETM1 - 1 )
{
    hDOSS1ETM1 = 3 * hDOSS1ETM1 + 1;
}

while ( hDOSS1ETM1 > 0 )
{
    for i = hDOSS1ETM1 - 1 to lastDOSS1ETM1               # for each of the h sets of elements
    {
        numeric keyDOSS1ETM1 = allValuesDosS1ETM1[i];
        numeric jDOSS1ETM1 = i;

        while (jDOSS1ETM1 >= hDOSS1ETM1 && allValuesDosS1ETM1[jDOSS1ETM1 - hDOSS1ETM1] > keyDOSS1ETM1)
        {
            allValuesDosS1ETM1[jDOSS1ETM1] = allValuesDosS1ETM1[jDOSS1ETM1 - hDOSS1ETM1];
            jDOSS1ETM1 = jDOSS1ETM1 - hDOSS1ETM1;
        }
        allValuesDosS1ETM1[jDOSS1ETM1] = keyDOSS1ETM1;
    }
    hDOSS1ETM1 = floor(hDOSS1ETM1/3);
}

numeric minValueDosS1ETM1 = allValuesDosS1ETM1[1];
numeric dn1000S1ETM1 = 10000;

numeric actualCountDosS1ETM1 = 0;
for i = 1 to dosS1ETM1realSize 
{
    if (allValuesDosS1ETM1[i] == minValueDosS1ETM1)
    {
       actualCountDosS1ETM1 = actualCountDosS1ETM1 + 1;
       if (actualCountDosS1ETM1 >= DNminCALCS1ETM1)
       {
           dn1000S1ETM1 = minValueDosS1ETM1;
           go to dn1000S1ETM1jumper;
       }
    }
    else
    {
        minValueDosS1ETM1 = allValuesDosS1ETM1[i+1];
        actualCountDosS1ETM1 = 0;
    }
}

dn1000S1ETM1jumper:

if (dn1000S1ETM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM1 = allValuesDosS1ETM1[1];
}
if (dn1000S1ETM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM1 = allValuesDosS1ETM1[1];
}

printf("dn1000 Scene 1 ETM1: %12f\n", dn1000S1ETM1);

########################### Scene 1 - Dn1000 - Band 2

numeric lastDOSS1ETM2 = dosS1ETM1realSize;
numeric hDOSS1ETM2 = 1;

while ( (hDOSS1ETM2 * 3 + 1) < lastDOSS1ETM2 - 1 )
{
    hDOSS1ETM2 = 3 * hDOSS1ETM2 + 1;
}

while ( hDOSS1ETM2 > 0 )
{
    for i = hDOSS1ETM2 - 1 to lastDOSS1ETM2
    {
        numeric keyDOSS1ETM2 = allValuesDosS1ETM2[i];
        numeric jDOSS1ETM2 = i;

        while (jDOSS1ETM2 >= hDOSS1ETM2 && allValuesDosS1ETM2[jDOSS1ETM2 - hDOSS1ETM2] > keyDOSS1ETM2)
        {
            allValuesDosS1ETM2[jDOSS1ETM2] = allValuesDosS1ETM2[jDOSS1ETM2 - hDOSS1ETM2];
            jDOSS1ETM2 = jDOSS1ETM2 - hDOSS1ETM2;
        }
        allValuesDosS1ETM2[jDOSS1ETM2] = keyDOSS1ETM2;
    }
    hDOSS1ETM2 = floor(hDOSS1ETM2/3);
}

numeric minValueDosS1ETM2 = allValuesDosS1ETM2[1];
numeric dn1000S1ETM2 = 10000;

numeric actualCountDosS1ETM2 = 0;
for i = 1 to dosS1ETM2realSize 
{
    if (allValuesDosS1ETM2[i] == minValueDosS1ETM2)
    {
       actualCountDosS1ETM2 = actualCountDosS1ETM2 + 1;
       if (actualCountDosS1ETM2 >= DNminCALCS1ETM2)
       {
           dn1000S1ETM2 = minValueDosS1ETM2;
           go to dn1000S1ETM2jumper;
       }
    }
    else
    {
        minValueDosS1ETM2 = allValuesDosS1ETM2[i+1];
        actualCountDosS1ETM2 = 0;
    }
}

dn1000S1ETM2jumper:

if (dn1000S1ETM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM2 = allValuesDosS1ETM2[1];
}
if (dn1000S1ETM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM2 = allValuesDosS1ETM2[1];
}

printf("dn1000 Scene 1 ETM2: %12f\n", dn1000S1ETM2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS1ETM3 = dosS1ETM3realSize;
numeric hDOSS1ETM3 = 1;

while ( (hDOSS1ETM3 * 3 + 1) < lastDOSS1ETM3 - 1 )
{
    hDOSS1ETM3 = 3 * hDOSS1ETM3 + 1;
}

while ( hDOSS1ETM3 > 0 )
{
    for i = hDOSS1ETM3 - 1 to lastDOSS1ETM3               # for each of the h sets of elements
    {
        numeric keyDOSS1ETM3 = allValuesDosS1ETM3[i];
        numeric jDOSS1ETM3 = i;

        while (jDOSS1ETM3 >= hDOSS1ETM3 && allValuesDosS1ETM3[jDOSS1ETM3 - hDOSS1ETM3] > keyDOSS1ETM3)
        {
            allValuesDosS1ETM3[jDOSS1ETM3] = allValuesDosS1ETM3[jDOSS1ETM3 - hDOSS1ETM3];
            jDOSS1ETM3 = jDOSS1ETM3 - hDOSS1ETM3;
        }
        allValuesDosS1ETM3[jDOSS1ETM3] = keyDOSS1ETM3;
    }
    hDOSS1ETM3 = floor(hDOSS1ETM3/3);
}

numeric minValueDosS1ETM3 = allValuesDosS1ETM3[1];
numeric dn1000S1ETM3 = 10000;

numeric actualCountDosS1ETM3 = 0;
for i = 1 to dosS1ETM3realSize 
{
    if (allValuesDosS1ETM3[i] == minValueDosS1ETM3)
    {
        actualCountDosS1ETM3 = actualCountDosS1ETM3 + 1;
        if (actualCountDosS1ETM3 >= DNminCALCS1ETM3)
        {
           dn1000S1ETM3 = minValueDosS1ETM3;
           go to dn1000S1ETM3jumper;
        }
    }
    else
    {
        minValueDosS1ETM3 = allValuesDosS1ETM3[i+1];
        actualCountDosS1ETM3 = 0;
    }
}

dn1000S1ETM3jumper:

if (dn1000S1ETM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM3 = allValuesDosS1ETM3[1];
}
if (dn1000S1ETM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM3 = allValuesDosS1ETM3[1];
}
	
printf("dn1000 Scene 1 ETM3: %12f\n", dn1000S1ETM3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS1ETM4 = dosS1ETM4realSize;
numeric hDOSS1ETM4 = 1;

while ( (hDOSS1ETM4 * 3 + 1) < lastDOSS1ETM4 - 1 )
{
    hDOSS1ETM4 = 3 * hDOSS1ETM4 + 1;
}

while ( hDOSS1ETM4 > 0 )
{
    for i = hDOSS1ETM4 - 1 to lastDOSS1ETM4
    {
        numeric keyDOSS1ETM4 = allValuesDosS1ETM4[i];
        numeric jDOSS1ETM4 = i;

        while (jDOSS1ETM4 >= hDOSS1ETM4 && allValuesDosS1ETM4[jDOSS1ETM4 - hDOSS1ETM4] > keyDOSS1ETM4)
        {
            allValuesDosS1ETM4[jDOSS1ETM4] = allValuesDosS1ETM4[jDOSS1ETM4 - hDOSS1ETM4];
            jDOSS1ETM4 = jDOSS1ETM4 - hDOSS1ETM4;
        }
        allValuesDosS1ETM4[jDOSS1ETM4] = keyDOSS1ETM4;
    }
    hDOSS1ETM4 = floor(hDOSS1ETM4/3);
}

numeric minValueDosS1ETM4 = allValuesDosS1ETM4[1];
numeric dn1000S1ETM4 = 10000;

numeric actualCountDosS1ETM4 = 0;
for i = 1 to dosS1ETM4realSize 
{
    if (allValuesDosS1ETM4[i] == minValueDosS1ETM4)
    {
        actualCountDosS1ETM4 = actualCountDosS1ETM4 + 1;
        if (actualCountDosS1ETM4 >= DNminCALCS1ETM4)
        {
           dn1000S1ETM4 = minValueDosS1ETM4;
           go to dn1000S1ETM4jumper;
        }
    }
    else
    {
        minValueDosS1ETM4 = allValuesDosS1ETM4[i+1];
        actualCountDosS1ETM4 = 0;
    }
}

dn1000S1ETM4jumper:

if (dn1000S1ETM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM4 = allValuesDosS1ETM4[1];
}
if (dn1000S1ETM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM4 = allValuesDosS1ETM4[1];
}
	
printf("dn1000 Scene 1 ETM4: %12f\n", dn1000S1ETM4);

########################### Scene 1 - Dn1000 - Band 5

numeric lastDOSS1ETM5 = dosS1ETM5realSize;
numeric hDOSS1ETM5 = 1;

while ( (hDOSS1ETM5 * 3 + 1) < lastDOSS1ETM5 - 1 )
{
    hDOSS1ETM5 = 3 * hDOSS1ETM5 + 1;
}

while ( hDOSS1ETM5 > 0 )
{
    for i = hDOSS1ETM5 - 1 to lastDOSS1ETM5               # for each of the h sets of elements
    {
        numeric keyDOSS1ETM5 = allValuesDosS1ETM5[i];
        numeric jDOSS1ETM5 = i;

        while (jDOSS1ETM5 >= hDOSS1ETM5 && allValuesDosS1ETM5[jDOSS1ETM5 - hDOSS1ETM5] > keyDOSS1ETM5)
        {
            allValuesDosS1ETM5[jDOSS1ETM5] = allValuesDosS1ETM5[jDOSS1ETM5 - hDOSS1ETM5];
            jDOSS1ETM5 = jDOSS1ETM5 - hDOSS1ETM5;
        }
        allValuesDosS1ETM5[jDOSS1ETM5] = keyDOSS1ETM5;
    }
    hDOSS1ETM5 = floor(hDOSS1ETM5/3);
}


numeric minValueDosS1ETM5 = allValuesDosS1ETM5[1];
numeric dn1000S1ETM5 = 10000;

numeric actualCountDosS1ETM5 = 0;
for i = 1 to dosS1ETM5realSize 
{
    if (allValuesDosS1ETM5[i] == minValueDosS1ETM5)
    {
        actualCountDosS1ETM5 = actualCountDosS1ETM5 + 1;
        if (actualCountDosS1ETM5 >= DNminCALCS1ETM5)
        {
           dn1000S1ETM5 = minValueDosS1ETM5;
           go to dn1000S1ETM5jumper;
        }
    }
    else
    {
        minValueDosS1ETM5 = allValuesDosS1ETM5[i+1];
        actualCountDosS1ETM5 = 0;
    }
}

dn1000S1ETM5jumper:

if (dn1000S1ETM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM5  = allValuesDosS1ETM5[1];
}
if (dn1000S1ETM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM5  = allValuesDosS1ETM5[1];
}
	
printf("dn1000 Scene 1 ETM5: %12f\n", dn1000S1ETM5);

########################### Scene 1 - Dn1000 - Band 7

numeric lastDOSS1ETM7 = dosS1ETM7realSize;
numeric hDOSS1ETM7 = 1;

while ( (hDOSS1ETM7 * 3 + 1) < lastDOSS1ETM7 - 1 )
{
    hDOSS1ETM7 = 3 * hDOSS1ETM7 + 1;
}

while ( hDOSS1ETM7 > 0 )
{
    for i = hDOSS1ETM7 - 1 to lastDOSS1ETM7               # for each of the h sets of elements
    {
        numeric keyDOSS1ETM7 = allValuesDosS1ETM7[i];
        numeric jDOSS1ETM7 = i;

        while (jDOSS1ETM7 >= hDOSS1ETM7 && allValuesDosS1ETM7[jDOSS1ETM7 - hDOSS1ETM7] > keyDOSS1ETM7)
        {
            allValuesDosS1ETM7[jDOSS1ETM7] = allValuesDosS1ETM7[jDOSS1ETM7 - hDOSS1ETM7];
            jDOSS1ETM7 = jDOSS1ETM7 - hDOSS1ETM7;
        }
        allValuesDosS1ETM7[jDOSS1ETM7] = keyDOSS1ETM7;
    }
    hDOSS1ETM7 = floor(hDOSS1ETM7/3);
}

numeric minValueDosS1ETM7 = allValuesDosS1ETM7[1];
numeric dn1000S1ETM7 = 10000;

numeric actualCountDosS1ETM7 = 0;
for i = 1 to dosS1ETM7realSize 
{
    if (allValuesDosS1ETM7[i] == minValueDosS1ETM7)
    {
        actualCountDosS1ETM7 = actualCountDosS1ETM7 + 1;
        if (actualCountDosS1ETM7 >= DNminCALCS1ETM7)
        {
           dn1000S1ETM7 = minValueDosS1ETM7;
           go to dn1000S1ETM7jumper;
        }
    }
    else
    {
        minValueDosS1ETM7 = allValuesDosS1ETM7[i+1];
        actualCountDosS1ETM7 = 0;
    }
}

dn1000S1ETM7jumper:

if (dn1000S1ETM7 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1ETM7  = allValuesDosS1ETM7[1];
}
if (dn1000S1ETM7 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1ETM7  = allValuesDosS1ETM7[1];
}
	
printf("dn1000 Scene 1 ETM7: %12f\n\n", dn1000S1ETM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS1ETM1 = dn1000S1ETM1 - (0.01 * (radsurfETMS1b1 * cos(s1zenith)) / pi);
numeric pathRadS1ETM2 = dn1000S1ETM2 - (0.01 * (radsurfETMS1b2 * cos(s1zenith)) / pi);
numeric pathRadS1ETM3 = dn1000S1ETM3 - (0.01 * (radsurfETMS1b3 * cos(s1zenith)) / pi);
numeric pathRadS1ETM4 = dn1000S1ETM4 - (0.01 * (radsurfETMS1b4 * cos(s1zenith)) / pi);
numeric pathRadS1ETM5 = dn1000S1ETM5 - (0.01 * (radsurfETMS1b5 * cos(s1zenith)) / pi);
numeric pathRadS1ETM7 = dn1000S1ETM7 - (0.01 * (radsurfETMS1b7 * cos(s1zenith)) / pi);

printf("Pathradiance Scene 1 Band 1: %12f\n", pathRadS1ETM1);
printf("Pathradiance Scene 1 Band 2: %12f\n", pathRadS1ETM2);
printf("Pathradiance Scene 1 Band 3: %12f\n", pathRadS1ETM3);
printf("Pathradiance Scene 1 Band 4: %12f\n", pathRadS1ETM4);
printf("Pathradiance Scene 1 Band 5: %12f\n", pathRadS1ETM5);
printf("Pathradiance Scene 1 Band 7: %12f\n\n", pathRadS1ETM7);


################################# Reflectance Calculation with athmo correction

	 for i = 1 to ETMlins
	 {
	        for j = 1 to ETMcols
	        {
                  valueS1ETM1 = (pi * (RADS1ETM1[i,j] - pathRadS1ETM1)) / (radsurfETMS1b1 * cos(s1zenith));

                  if ( valueS1ETM1 < 0 )
       		{
          			valueS1ETM1 = 0;
				countS1REFnullETM1 = countS1REFnullETM1 + 1;
       		}

			else if ( valueS1ETM1 > 1 )
			{
				valueS1ETM1 = 1;
				countS1REFoneETM1 = countS1REFoneETM1 + 1;
			}
       		
			REFS1ETM1[i,j] = valueS1ETM1;

               valueS1ETM2 = (pi * (RADS1ETM2[i,j] - pathRadS1ETM2)) / (radsurfETMS1b2 * cos(s1zenith));

                  if ( valueS1ETM2 < 0 )
       		{
          			valueS1ETM2 = 0;
				countS1REFnullETM2 = countS1REFnullETM2 + 1;
       		}

			else if ( valueS1ETM2 > 1 )
			{
				valueS1ETM2 = 1;
				countS1REFoneETM2 = countS1REFoneETM2 + 1;
			}
       		
			REFS1ETM2[i,j] = valueS1ETM2;

               valueS1ETM3 = (pi * (RADS1ETM3[i,j] - pathRadS1ETM3)) / (radsurfETMS1b3 * cos(s1zenith));

                  if ( valueS1ETM3 < 0 )
       		{
          			valueS1ETM3 = 0;
				countS1REFnullETM3 = countS1REFnullETM3 + 1;
       		}

			else if ( valueS1ETM3 > 1 )
			{
				valueS1ETM3 = 1;
				countS1REFoneETM3 = countS1REFoneETM3 + 1;
			}
       		
			REFS1ETM3[i,j] = valueS1ETM3;

               valueS1ETM4 = (pi * (RADS1ETM4[i,j] - pathRadS1ETM4)) / (radsurfETMS1b4 * cos(s1zenith));

                  if ( valueS1ETM4 < 0 )
       		{
          			valueS1ETM4 = 0;
				countS1REFnullETM4 = countS1REFnullETM4 + 1;
       		}

			else if ( valueS1ETM4 > 1 )
			{
				valueS1ETM4 = 1;
				countS1REFoneETM4 = countS1REFoneETM4 + 1;
			}
       		
			REFS1ETM4[i,j] = valueS1ETM4;

               valueS1ETM5 = (pi * (RADS1ETM5[i,j] - pathRadS1ETM5)) / (radsurfETMS1b5 * cos(s1zenith));

                  if ( valueS1ETM5 < 0 )
       		{
          			valueS1ETM5 = 0;
				countS1REFnullETM5 = countS1REFnullETM5 + 1;
       		}

			else if ( valueS1ETM5 > 1 )
			{
				valueS1ETM5 = 1;
				countS1REFoneETM5 = countS1REFoneETM5 + 1;
			}
       		
			REFS1ETM5[i,j] = valueS1ETM5;

               valueS1ETM7 = (pi * (RADS1ETM7[i,j] - pathRadS1ETM7)) / (radsurfETMS1b7 * cos(s1zenith));

                  if ( valueS1ETM7 < 0 )
       		{
          			valueS1ETM7 = 0;
				countS1REFnullETM7 = countS1REFnullETM7 + 1;
       		}

			else if ( valueS1ETM7 > 1 )
			{
				valueS1ETM7 = 1;
				countS1REFoneETM7 = countS1REFoneETM7 + 1;
			}
       		
			REFS1ETM7[i,j] = valueS1ETM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullETM1, countS1REFoneETM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullETM2, countS1REFoneETM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullETM3, countS1REFoneETM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullETM4, countS1REFoneETM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullETM5, countS1REFoneETM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS1REFnullETM7, countS1REFoneETM7);

CreatePyramid(REFS1ETM1);
CreatePyramid(REFS1ETM2);
CreatePyramid(REFS1ETM3);
CreatePyramid(REFS1ETM4);
CreatePyramid(REFS1ETM5);
CreatePyramid(REFS1ETM7);
        
CreateHistogram(REFS1ETM1);
CreateHistogram(REFS1ETM2);
CreateHistogram(REFS1ETM3);
CreateHistogram(REFS1ETM4);
CreateHistogram(REFS1ETM5);
CreateHistogram(REFS1ETM7);

CloseRaster(RADS1ETM1);
CloseRaster(RADS1ETM2);
CloseRaster(RADS1ETM3);
CloseRaster(RADS1ETM4);
CloseRaster(RADS1ETM5);
CloseRaster(RADS1ETM7);

printf("Reflectance calculation for Scene 1 (ETM) is done...\n\n\n");
}

else if ( sensors1 == 6 )
{
raster REFS1TM1, REFS1TM2, REFS1TM3, REFS1TM4, REFS1TM5, REFS1TM7;
numeric radsurfTMS1b1, radsurfTMS1b2, radsurfTMS1b3, radsurfTMS1b4, radsurfTMS1b5, radsurfTMS1b7;
numeric valueS1TM1, valueS1TM2, valueS1TM3, valueS1TM4, valueS1TM5, valueS1TM7;

numeric dosS1TM1nullValCount = 0;
numeric dosS1TM2nullValCount = 0;
numeric dosS1TM3nullValCount = 0;
numeric dosS1TM4nullValCount = 0;
numeric dosS1TM5nullValCount = 0;
numeric dosS1TM7nullValCount = 0;

numeric dosS1TM1realArrayCount = 1;
numeric dosS1TM2realArrayCount = 1;
numeric dosS1TM3realArrayCount = 1;
numeric dosS1TM4realArrayCount = 1;
numeric dosS1TM5realArrayCount = 1;
numeric dosS1TM7realArrayCount = 1;

numeric countS1REFnullTM1 = 0;
numeric countS1REFnullTM2 = 0;
numeric countS1REFnullTM3 = 0;
numeric countS1REFnullTM4 = 0;
numeric countS1REFnullTM5 = 0;
numeric countS1REFnullTM7 = 0;

numeric countS1REFoneTM1 = 0;
numeric countS1REFoneTM2 = 0;
numeric countS1REFoneTM3 = 0;
numeric countS1REFoneTM4 = 0;
numeric countS1REFoneTM5 = 0;
numeric countS1REFoneTM7 = 0;

radsurfTMS1b1 = ESTM1 / (dist1^2);
radsurfTMS1b2 = ESTM2 / (dist1^2);
radsurfTMS1b3 = ESTM3 / (dist1^2);
radsurfTMS1b4 = ESTM4 / (dist1^2);
radsurfTMS1b5 = ESTM5 / (dist1^2);
radsurfTMS1b7 = ESTM7 / (dist1^2);

printf("Surface Radiance TM Band 1 in W/m^2µm (Scene 1): %12f\n", radsurfTMS1b1);
printf("Surface Radiance TM Band 2 in W/m^2µm (Scene 1): %12f\n", radsurfTMS1b2);
printf("Surface Radiance TM Band 3 in W/m^2µm (Scene 1): %12f\n", radsurfTMS1b3);
printf("Surface Radiance TM Band 4 in W/m^2µm (Scene 1): %12f\n", radsurfTMS1b4);
printf("Surface Radiance TM Band 5 in W/m^2µm (Scene 1): %12f\n", radsurfTMS1b5);
printf("Surface Radiance TM Band 7 in W/m^2µm (Scene 1): %12f\n\n", radsurfTMS1b7);


for i = 1 to TMlins
{
    for j = 1 to TMcols
    {       
		if (IsNull(RADS1TM1[i,j]) == 1)
		{
		dosS1TM1nullValCount = dosS1TM1nullValCount + 1;
		}
		if (IsNull(RADS1TM2[i,j]) == 1)
		{
		dosS1TM2nullValCount = dosS1TM2nullValCount + 1;
		}		 
		if (IsNull(RADS1TM3[i,j]) == 1)
		{
		dosS1TM3nullValCount = dosS1TM3nullValCount + 1;
		}		 
		if (IsNull(RADS1TM4[i,j]) == 1)
		{
		dosS1TM4nullValCount = dosS1TM4nullValCount + 1;
		}		 
		if (IsNull(RADS1TM5[i,j]) == 1)
		{
		dosS1TM5nullValCount = dosS1TM5nullValCount + 1;
		}		 
		if (IsNull(RADS1TM7[i,j]) == 1)
		{
		dosS1TM7nullValCount = dosS1TM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS1TM1realSize = (TMlins * TMcols) - dosS1TM1nullValCount;
numeric dosS1TM2realSize = (TMlins * TMcols) - dosS1TM2nullValCount;
numeric dosS1TM3realSize = (TMlins * TMcols) - dosS1TM3nullValCount;
numeric dosS1TM4realSize = (TMlins * TMcols) - dosS1TM4nullValCount;
numeric dosS1TM5realSize = (TMlins * TMcols) - dosS1TM5nullValCount;
numeric dosS1TM7realSize = (TMlins * TMcols) - dosS1TM7nullValCount;

array allValuesDosS1TM1[dosS1TM1realSize];
array allValuesDosS1TM2[dosS1TM2realSize];
array allValuesDosS1TM3[dosS1TM3realSize];
array allValuesDosS1TM4[dosS1TM4realSize];
array allValuesDosS1TM5[dosS1TM5realSize];
array allValuesDosS1TM7[dosS1TM7realSize];

numeric DNminCALCS1TM1 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM1realSize);
numeric DNminCALCS1TM2 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM2realSize);
numeric DNminCALCS1TM3 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM3realSize);
numeric DNminCALCS1TM4 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM4realSize);
numeric DNminCALCS1TM5 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM5realSize);
numeric DNminCALCS1TM7 = DNminCALC(Scene1Lin, Scene1Col, dosS1TM7realSize);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(RADS1TM1[i,j]) == 0)
		{
		  allValuesDosS1TM1[dosS1TM1realArrayCount] = RADS1TM1[i,j];		 
		  dosS1TM1realArrayCount = dosS1TM1realArrayCount + 1;
		}
		if (IsNull(RADS1TM2[i,j]) == 0)
		{
		  allValuesDosS1TM2[dosS1TM2realArrayCount] = RADS1TM2[i,j];		 
		  dosS1TM2realArrayCount = dosS1TM2realArrayCount + 1;
		}
		if (IsNull(RADS1TM3[i,j]) == 0)
		{
		  allValuesDosS1TM3[dosS1TM3realArrayCount] = RADS1TM3[i,j];		 
		  dosS1TM3realArrayCount = dosS1TM3realArrayCount + 1;
		}
		if (IsNull(RADS1TM4[i,j]) == 0)
		{
		  allValuesDosS1TM4[dosS1TM4realArrayCount] = RADS1TM4[i,j];		 
		  dosS1TM4realArrayCount = dosS1TM4realArrayCount + 1;
		}
		if (IsNull(RADS1TM5[i,j]) == 0)
		{
		  allValuesDosS1TM5[dosS1TM5realArrayCount] = RADS1TM5[i,j];		 
		  dosS1TM5realArrayCount = dosS1TM5realArrayCount + 1;
		}
		if (IsNull(RADS1TM7[i,j]) == 0)
		{
		  allValuesDosS1TM7[dosS1TM7realArrayCount] = RADS1TM7[i,j];		 
		  dosS1TM7realArrayCount = dosS1TM7realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS1TM1 = dosS1TM1realSize;
numeric hDOSS1TM1 = 1;

while ( (hDOSS1TM1 * 3 + 1) < lastDOSS1TM1 - 1 )
{
    hDOSS1TM1 = 3 * hDOSS1TM1 + 1;
}

while ( hDOSS1TM1 > 0 )
{
    for i = hDOSS1TM1 - 1 to lastDOSS1TM1               # for each of the h sets of elements
    {
        numeric keyDOSS1TM1 = allValuesDosS1TM1[i];
        numeric jDOSS1TM1 = i;

        while (jDOSS1TM1 >= hDOSS1TM1 && allValuesDosS1TM1[jDOSS1TM1 - hDOSS1TM1] > keyDOSS1TM1)
        {
            allValuesDosS1TM1[jDOSS1TM1] = allValuesDosS1TM1[jDOSS1TM1 - hDOSS1TM1];
            jDOSS1TM1 = jDOSS1TM1 - hDOSS1TM1;
        }
        allValuesDosS1TM1[jDOSS1TM1] = keyDOSS1TM1;
    }
    hDOSS1TM1 = floor(hDOSS1TM1/3);
}

numeric minValueDosS1TM1 = allValuesDosS1TM1[1];
numeric dn1000S1TM1 = 10000;

numeric actualCountDosS1TM1 = 0;
for i = 1 to dosS1TM1realSize 
{
    if (allValuesDosS1TM1[i] == minValueDosS1TM1)
    {
        actualCountDosS1TM1 = actualCountDosS1TM1 + 1;
        if (actualCountDosS1TM1 >= DNminCALCS1TM1)
        {
           dn1000S1TM1 = minValueDosS1TM1;
           go to dn1000S1TM1jumper;
        }
    }
    else
    {
        minValueDosS1TM1 = allValuesDosS1TM1[i+1];
        actualCountDosS1TM1 = 0;
    }
}

dn1000S1TM1jumper:

if (dn1000S1TM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM1 = allValuesDosS1TM1[1];
}
if (dn1000S1TM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM1 = allValuesDosS1TM1[1];
}
	
printf("dn1000 Scene 1 TM1: %12f\n", dn1000S1TM1);

########################### Scene 1 - Dn1000 - Band 2

numeric lastDOSS1TM2 = dosS1TM1realSize;
numeric hDOSS1TM2 = 1;

while ( (hDOSS1TM2 * 3 + 1) < lastDOSS1TM2 - 1 )
{
    hDOSS1TM2 = 3 * hDOSS1TM2 + 1;
}

while ( hDOSS1TM2 > 0 )
{
    for i = hDOSS1TM2 - 1 to lastDOSS1TM2
    {
        numeric keyDOSS1TM2 = allValuesDosS1TM2[i];
        numeric jDOSS1TM2 = i;

        while (jDOSS1TM2 >= hDOSS1TM2 && allValuesDosS1TM2[jDOSS1TM2 - hDOSS1TM2] > keyDOSS1TM2)
        {
            allValuesDosS1TM2[jDOSS1TM2] = allValuesDosS1TM2[jDOSS1TM2 - hDOSS1TM2];
            jDOSS1TM2 = jDOSS1TM2 - hDOSS1TM2;
        }
        allValuesDosS1TM2[jDOSS1TM2] = keyDOSS1TM2;
    }
    hDOSS1TM2 = floor(hDOSS1TM2/3);
}

numeric minValueDosS1TM2 = allValuesDosS1TM2[1];
numeric dn1000S1TM2 = 10000;

numeric actualCountDosS1TM2 = 0;
for i = 1 to dosS1TM2realSize 
{
    if (allValuesDosS1TM2[i] == minValueDosS1TM2)
    {
        actualCountDosS1TM2 = actualCountDosS1TM2 + 1;
        if (actualCountDosS1TM2 >= DNminCALCS1TM2)
        {
           dn1000S1TM2 = minValueDosS1TM2;
           go to dn1000S1TM2jumper;
        }
    }
    
    else
    {
        minValueDosS1TM2 = allValuesDosS1TM2[i+1];
        actualCountDosS1TM2 = 0;
    }
}

dn1000S1TM2jumper:

if (dn1000S1TM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM2 = allValuesDosS1TM2[1];
}

if (dn1000S1TM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM2 = allValuesDosS1TM2[1];
}

printf("dn1000 Scene 1 TM2: %12f\n", dn1000S1TM2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS1TM3 = dosS1TM3realSize;
numeric hDOSS1TM3 = 1;

while ( (hDOSS1TM3 * 3 + 1) < lastDOSS1TM3 - 1 )
{
    hDOSS1TM3 = 3 * hDOSS1TM3 + 1;
}

while ( hDOSS1TM3 > 0 )
{
    for i = hDOSS1TM3 - 1 to lastDOSS1TM3               # for each of the h sets of elements
    {
        numeric keyDOSS1TM3 = allValuesDosS1TM3[i];
        numeric jDOSS1TM3 = i;

        while (jDOSS1TM3 >= hDOSS1TM3 && allValuesDosS1TM3[jDOSS1TM3 - hDOSS1TM3] > keyDOSS1TM3)
        {
            allValuesDosS1TM3[jDOSS1TM3] = allValuesDosS1TM3[jDOSS1TM3 - hDOSS1TM3];
            jDOSS1TM3 = jDOSS1TM3 - hDOSS1TM3;
        }
        allValuesDosS1TM3[jDOSS1TM3] = keyDOSS1TM3;
    }
    hDOSS1TM3 = floor(hDOSS1TM3/3);
}

numeric minValueDosS1TM3 = allValuesDosS1TM3[1];
numeric dn1000S1TM3 = 10000;

numeric actualCountDosS1TM3 = 0;
for i = 1 to dosS1TM3realSize 
{
    if (allValuesDosS1TM3[i] == minValueDosS1TM3)
    {
        actualCountDosS1TM3 = actualCountDosS1TM3 + 1;
        if (actualCountDosS1TM3 >= DNminCALCS1TM3)
        {
            dn1000S1TM3 = minValueDosS1TM3;
            go to dn1000S1TM3jumper;
        }
    }
    else
    {
        minValueDosS1TM3 = allValuesDosS1TM3[i+1];
        actualCountDosS1TM3 = 0;
    }
}

dn1000S1TM3jumper:

if (dn1000S1TM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM3 = allValuesDosS1TM3[1];
}
if (dn1000S1TM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM3 = allValuesDosS1TM3[1];
}

printf("dn1000 Scene 1 TM3: %12f\n", dn1000S1TM3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS1TM4 = dosS1TM4realSize;
numeric hDOSS1TM4 = 1;

while ( (hDOSS1TM4 * 3 + 1) < lastDOSS1TM4 - 1 )
{
    hDOSS1TM4 = 3 * hDOSS1TM4 + 1;
}

while ( hDOSS1TM4 > 0 )
{
    for i = hDOSS1TM4 - 1 to lastDOSS1TM4
    {
        numeric keyDOSS1TM4 = allValuesDosS1TM4[i];
        numeric jDOSS1TM4 = i;

        while (jDOSS1TM4 >= hDOSS1TM4 && allValuesDosS1TM4[jDOSS1TM4 - hDOSS1TM4] > keyDOSS1TM4)
        {
            allValuesDosS1TM4[jDOSS1TM4] = allValuesDosS1TM4[jDOSS1TM4 - hDOSS1TM4];
            jDOSS1TM4 = jDOSS1TM4 - hDOSS1TM4;
        }
        allValuesDosS1TM4[jDOSS1TM4] = keyDOSS1TM4;
    }
    hDOSS1TM4 = floor(hDOSS1TM4/3);
}

numeric minValueDosS1TM4 = allValuesDosS1TM4[1];
numeric dn1000S1TM4 = 10000;

numeric actualCountDosS1TM4 = 0;
for i = 1 to dosS1TM4realSize 
{
    if (allValuesDosS1TM4[i] == minValueDosS1TM4)
    {
        actualCountDosS1TM4 = actualCountDosS1TM4 + 1;
        if (actualCountDosS1TM4 >= DNminCALCS1TM4)
        {
           dn1000S1TM4 = minValueDosS1TM4;
           go to dn1000S1TM4jumper;
        }
    }
    else
    {
        minValueDosS1TM4 = allValuesDosS1TM4[i+1];
        actualCountDosS1TM4 = 0;
    }
}

dn1000S1TM4jumper:

if (dn1000S1TM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM4 = allValuesDosS1TM4[1];
}
if (dn1000S1TM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM4 = allValuesDosS1TM4[1];
}
	
printf("dn1000 Scene 1 TM4: %12f\n", dn1000S1TM4);

########################### Scene 1 - Dn1000 - Band 5

numeric lastDOSS1TM5 = dosS1TM5realSize;
numeric hDOSS1TM5 = 1;

while ( (hDOSS1TM5 * 3 + 1) < lastDOSS1TM5 - 1 )
{
    hDOSS1TM5 = 3 * hDOSS1TM5 + 1;
}

while ( hDOSS1TM5 > 0 )
{
    for i = hDOSS1TM5 - 1 to lastDOSS1TM5               # for each of the h sets of elements
    {
        numeric keyDOSS1TM5 = allValuesDosS1TM5[i];
        numeric jDOSS1TM5 = i;

        while (jDOSS1TM5 >= hDOSS1TM5 && allValuesDosS1TM5[jDOSS1TM5 - hDOSS1TM5] > keyDOSS1TM5)
        {
            allValuesDosS1TM5[jDOSS1TM5] = allValuesDosS1TM5[jDOSS1TM5 - hDOSS1TM5];
            jDOSS1TM5 = jDOSS1TM5 - hDOSS1TM5;
        }
        allValuesDosS1TM5[jDOSS1TM5] = keyDOSS1TM5;
    }
    hDOSS1TM5 = floor(hDOSS1TM5/3);
}

numeric minValueDosS1TM5 = allValuesDosS1TM5[1];
numeric dn1000S1TM5 = 10000;

numeric actualCountDosS1TM5 = 0;
for i = 1 to dosS1TM5realSize 
{
    if (allValuesDosS1TM5[i] == minValueDosS1TM5)
    {
        actualCountDosS1TM5 = actualCountDosS1TM5 + 1;
        if (actualCountDosS1TM5 >= DNminCALCS1TM5)
        {
           dn1000S1TM5 = minValueDosS1TM5;
           go to dn1000S1TM5jumper;
        }
    }
    else
    {
        minValueDosS1TM5 = allValuesDosS1TM5[i+1];
        actualCountDosS1TM5 = 0;
    }
}

dn1000S1TM5jumper:

if (dn1000S1TM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM5  = allValuesDosS1TM5[1];
}
if (dn1000S1TM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM5  = allValuesDosS1TM5[1];
}
	
printf("dn1000 Scene 1 TM5: %12f\n", dn1000S1TM5);

########################### Scene 1 - Dn1000 - Band 7

numeric lastDOSS1TM7 = dosS1TM7realSize;
numeric hDOSS1TM7 = 1;

while ( (hDOSS1TM7 * 3 + 1) < lastDOSS1TM7 - 1 )
{
    hDOSS1TM7 = 3 * hDOSS1TM7 + 1;
}

while ( hDOSS1TM7 > 0 )
{
    for i = hDOSS1TM7 - 1 to lastDOSS1TM7               # for each of the h sets of elements
    {
        numeric keyDOSS1TM7 = allValuesDosS1TM7[i];
        numeric jDOSS1TM7 = i;

        while (jDOSS1TM7 >= hDOSS1TM7 && allValuesDosS1TM7[jDOSS1TM7 - hDOSS1TM7] > keyDOSS1TM7)
        {
            allValuesDosS1TM7[jDOSS1TM7] = allValuesDosS1TM7[jDOSS1TM7 - hDOSS1TM7];
            jDOSS1TM7 = jDOSS1TM7 - hDOSS1TM7;
        }
        allValuesDosS1TM7[jDOSS1TM7] = keyDOSS1TM7;
    }
    hDOSS1TM7 = floor(hDOSS1TM7/3);
}

numeric minValueDosS1TM7 = allValuesDosS1TM7[1];
numeric dn1000S1TM7 = 10000;

numeric actualCountDosS1TM7 = 0;
for i = 1 to dosS1TM7realSize 
{
    if (allValuesDosS1TM7[i] == minValueDosS1TM7)
    {
        actualCountDosS1TM7 = actualCountDosS1TM7 + 1;
        if (actualCountDosS1TM7 >= DNminCALCS1TM7)
        {
           dn1000S1TM7 = minValueDosS1TM7;
           go to dn1000S1TM7jumper;
        }
    }
    else
    {
        minValueDosS1TM7 = allValuesDosS1TM7[i+1];
        actualCountDosS1TM7 = 0;
    }
}

dn1000S1TM7jumper:

if (dn1000S1TM7 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1TM7  = allValuesDosS1TM7[1];
}
if (dn1000S1TM7 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1TM7  = allValuesDosS1TM7[1];
}
	
printf("dn1000 Scene 1 TM7: %12f\n\n", dn1000S1TM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS1TM1 = dn1000S1TM1 - (0.01 * (radsurfTMS1b1 * cos(s1zenith)) / pi);
numeric pathRadS1TM2 = dn1000S1TM2 - (0.01 * (radsurfTMS1b2 * cos(s1zenith)) / pi);
numeric pathRadS1TM3 = dn1000S1TM3 - (0.01 * (radsurfTMS1b3 * cos(s1zenith)) / pi);
numeric pathRadS1TM4 = dn1000S1TM4 - (0.01 * (radsurfTMS1b4 * cos(s1zenith)) / pi);
numeric pathRadS1TM5 = dn1000S1TM5 - (0.01 * (radsurfTMS1b5 * cos(s1zenith)) / pi);
numeric pathRadS1TM7 = dn1000S1TM7 - (0.01 * (radsurfTMS1b7 * cos(s1zenith)) / pi);

printf("Pathradiance Scene 1 Band 1: %12f\n", pathRadS1TM1);
printf("Pathradiance Scene 1 Band 2: %12f\n", pathRadS1TM2);
printf("Pathradiance Scene 1 Band 3: %12f\n", pathRadS1TM3);
printf("Pathradiance Scene 1 Band 4: %12f\n", pathRadS1TM4);
printf("Pathradiance Scene 1 Band 5: %12f\n", pathRadS1TM5);
printf("Pathradiance Scene 1 Band 7: %12f\n\n", pathRadS1TM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to TMlins
	 {
	        for j = 1 to TMcols
	        {
                  valueS1TM1 = (pi * (RADS1TM1[i,j] - pathRadS1TM1)) / (radsurfTMS1b1 * cos(s1zenith));

                  if ( valueS1TM1 < 0 )
       		{
          			valueS1TM1 = 0;
				countS1REFnullTM1 = countS1REFnullTM1 + 1;
       		}

			else if ( valueS1TM1 > 1 )
			{
				valueS1TM1 = 1;
				countS1REFoneTM1 = countS1REFoneTM1 + 1;
			}
       		
			REFS1TM1[i,j] = valueS1TM1;

               valueS1TM2 = (pi * (RADS1TM2[i,j] - pathRadS1TM2)) / (radsurfTMS1b2 * cos(s1zenith));

                  if ( valueS1TM2 < 0 )
       		{
          			valueS1TM2 = 0;
				countS1REFnullTM2 = countS1REFnullTM2 + 1;
       		}

			else if ( valueS1TM2 > 1 )
			{
				valueS1TM2 = 1;
				countS1REFoneTM2 = countS1REFoneTM2 + 1;
			}
       		
			REFS1TM2[i,j] = valueS1TM2;

               valueS1TM3 = (pi * (RADS1TM3[i,j] - pathRadS1TM3)) / (radsurfTMS1b3 * cos(s1zenith));

                  if ( valueS1TM3 < 0 )
       		{
          			valueS1TM3 = 0;
				countS1REFnullTM3 = countS1REFnullTM3 + 1;
       		}

			else if ( valueS1TM3 > 1 )
			{
				valueS1TM3 = 1;
				countS1REFoneTM3 = countS1REFoneTM3 + 1;
			}
       		
			REFS1TM3[i,j] = valueS1TM3;

               valueS1TM4 = (pi * (RADS1TM4[i,j] - pathRadS1TM4)) / (radsurfTMS1b4 * cos(s1zenith));

                  if ( valueS1TM4 < 0 )
       		{
          			valueS1TM4 = 0;
				countS1REFnullTM4 = countS1REFnullTM4 + 1;
       		}

			else if ( valueS1TM4 > 1 )
			{
				valueS1TM4 = 1;
				countS1REFoneTM4 = countS1REFoneTM4 + 1;
			}
       		
			REFS1TM4[i,j] = valueS1TM4;

               valueS1TM5 = (pi * (RADS1TM5[i,j] - pathRadS1TM5)) / (radsurfTMS1b5 * cos(s1zenith));

                  if ( valueS1TM5 < 0 )
       		{
          			valueS1TM5 = 0;
				countS1REFnullTM5 = countS1REFnullTM5 + 1;
       		}

			else if ( valueS1TM5 > 1 )
			{
				valueS1TM5 = 1;
				countS1REFoneTM5 = countS1REFoneTM5 + 1;
			}
       		
			REFS1TM5[i,j] = valueS1TM5;

               valueS1TM7 = (pi * (RADS1TM7[i,j] - pathRadS1TM7)) / (radsurfTMS1b7 * cos(s1zenith));

                  if ( valueS1TM7 < 0 )
       		{
          			valueS1TM7 = 0;
				countS1REFnullTM7 = countS1REFnullTM7 + 1;
       		}

			else if ( valueS1TM7 > 1 )
			{
				valueS1TM7 = 1;
				countS1REFoneTM7 = countS1REFoneTM7 + 1;
			}
       		
			REFS1TM7[i,j] = valueS1TM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullTM1, countS1REFoneTM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullTM2, countS1REFoneTM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullTM3, countS1REFoneTM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullTM4, countS1REFoneTM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullTM5, countS1REFoneTM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS1REFnullTM7, countS1REFoneTM7);

CreatePyramid(REFS1TM1);
CreatePyramid(REFS1TM2);
CreatePyramid(REFS1TM3);
CreatePyramid(REFS1TM4);
CreatePyramid(REFS1TM5);
CreatePyramid(REFS1TM7);
        
CreateHistogram(REFS1TM1);
CreateHistogram(REFS1TM2);
CreateHistogram(REFS1TM3);
CreateHistogram(REFS1TM4);
CreateHistogram(REFS1TM5);
CreateHistogram(REFS1TM7);

CloseRaster(RADS1TM1);
CloseRaster(RADS1TM2);
CloseRaster(RADS1TM3);
CloseRaster(RADS1TM4);
CloseRaster(RADS1TM5);
CloseRaster(RADS1TM7);

printf("Reflectance calculation for Scene 1 (TM) is done...\n\n\n");
}

else if ( sensors1 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
raster REFS1MSS1, REFS1MSS2, REFS1MSS3, REFS1MSS4;
numeric radsurfMSS1b1, radsurfMSS1b2, radsurfMSS1b3, radsurfMSS1b4;
numeric valueS1MSS1, valueS1MSS2, valueS1MSS3, valueS1MSS4;

numeric dosS1MSS1nullValCount = 0;
numeric dosS1MSS2nullValCount = 0;
numeric dosS1MSS3nullValCount = 0;
numeric dosS1MSS4nullValCount = 0;

numeric dosS1MSS1realArrayCount = 1;
numeric dosS1MSS2realArrayCount = 1;
numeric dosS1MSS3realArrayCount = 1;
numeric dosS1MSS4realArrayCount = 1;

numeric countS1REFnullMSS1 = 0;
numeric countS1REFnullMSS2 = 0;
numeric countS1REFnullMSS3 = 0;
numeric countS1REFnullMSS4 = 0;

numeric countS1REFoneMSS1 = 0;
numeric countS1REFoneMSS2 = 0;
numeric countS1REFoneMSS3 = 0;
numeric countS1REFoneMSS4 = 0;

radsurfMSS1b1 = ESMSS51 / (dist1^2);
radsurfMSS1b2 = ESMSS52 / (dist1^2);
radsurfMSS1b3 = ESMSS53 / (dist1^2);
radsurfMSS1b4 = ESMSS54 / (dist1^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 1): %12f\n\n", radsurfMSS1b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS1MSS1[i,j]) == 1)
		{
		dosS1MSS1nullValCount = dosS1MSS1nullValCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 1)
		{
		dosS1MSS2nullValCount = dosS1MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS3[i,j]) == 1)
		{
		dosS1MSS3nullValCount = dosS1MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS4[i,j]) == 1)
		{
		dosS1MSS4nullValCount = dosS1MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS1MSS1realSize = (MSSlins * MSScols) - dosS1MSS1nullValCount;
numeric dosS1MSS2realSize = (MSSlins * MSScols) - dosS1MSS2nullValCount;
numeric dosS1MSS3realSize = (MSSlins * MSScols) - dosS1MSS3nullValCount;
numeric dosS1MSS4realSize = (MSSlins * MSScols) - dosS1MSS4nullValCount;

array allValuesDosS1MSS1[dosS1MSS1realSize];
array allValuesDosS1MSS2[dosS1MSS2realSize];
array allValuesDosS1MSS3[dosS1MSS3realSize];
array allValuesDosS1MSS4[dosS1MSS4realSize];

numeric DNminCALCS1MSS1 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS1realSize);
numeric DNminCALCS1MSS2 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS2realSize);
numeric DNminCALCS1MSS3 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS3realSize);
numeric DNminCALCS1MSS4 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS1MSS1[i,j]) == 0)
		{
		  allValuesDosS1MSS1[dosS1MSS1realArrayCount] = RADS1MSS1[i,j];		 
		  dosS1MSS1realArrayCount = dosS1MSS1realArrayCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 0)
		{
		  allValuesDosS1MSS2[dosS1MSS2realArrayCount] = RADS1MSS2[i,j];		 
		  dosS1MSS2realArrayCount = dosS1MSS2realArrayCount + 1;
		}
		if (IsNull(RADS1MSS3[i,j]) == 0)
		{
		  allValuesDosS1MSS3[dosS1MSS3realArrayCount] = RADS1MSS3[i,j];		 
		  dosS1MSS3realArrayCount = dosS1MSS3realArrayCount + 1;
		}
		if (IsNull(RADS1MSS4[i,j]) == 0)
		{
		  allValuesDosS1MSS4[dosS1MSS4realArrayCount] = RADS1MSS4[i,j];		 
		  dosS1MSS4realArrayCount = dosS1MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS1MSS1 = dosS1MSS1realSize;
numeric hDOSS1MSS1 = 1;

while ( (hDOSS1MSS1 * 3 + 1) < lastDOSS1MSS1 - 1 )
{
    hDOSS1MSS1 = 3 * hDOSS1MSS1 + 1;
}

while ( hDOSS1MSS1 > 0 )
{
    for i = hDOSS1MSS1 - 1 to lastDOSS1MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS1 = allValuesDosS1MSS1[i];
        numeric jDOSS1MSS1 = i;

        while (jDOSS1MSS1 >= hDOSS1MSS1 && allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1] > keyDOSS1MSS1)
        {
            allValuesDosS1MSS1[jDOSS1MSS1] = allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1];
            jDOSS1MSS1 = jDOSS1MSS1 - hDOSS1MSS1;
        }
        allValuesDosS1MSS1[jDOSS1MSS1] = keyDOSS1MSS1;
    }
    hDOSS1MSS1 = floor(hDOSS1MSS1/3);
}

numeric minValueDosS1MSS1 = allValuesDosS1MSS1[1];
numeric dn1000S1MSS1 = 10000;

numeric actualCountDosS1MSS1 = 0;
for i = 1 to dosS1MSS1realSize 
{
    if (allValuesDosS1MSS1[i] == minValueDosS1MSS1)
    {
        actualCountDosS1MSS1 = actualCountDosS1MSS1 + 1;
        if (actualCountDosS1MSS1 >= DNminCALCS1MSS1)
        {
           dn1000S1MSS1 = minValueDosS1MSS1;
           go to dn1000S1MSS1jumper;
        }
    }
    else
    {
        minValueDosS1MSS1 = allValuesDosS1MSS1[i+1];
        actualCountDosS1MSS1 = 0;
    }
}

dn1000S1MSS1jumper:

if (dn1000S1MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}
if (dn1000S1MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}
	
printf("dn1000 Scene 1 MSS1: %12f\n", dn1000S1MSS1);

########################### Scene 1 - Dn1000 - Band 2

numeric lastDOSS1MSS2 = dosS1MSS1realSize;
numeric hDOSS1MSS2 = 1;

while ( (hDOSS1MSS2 * 3 + 1) < lastDOSS1MSS2 - 1 )
{
    hDOSS1MSS2 = 3 * hDOSS1MSS2 + 1;
}

while ( hDOSS1MSS2 > 0 )
{
    for i = hDOSS1MSS2 - 1 to lastDOSS1MSS2
    {
        numeric keyDOSS1MSS2 = allValuesDosS1MSS2[i];
        numeric jDOSS1MSS2 = i;

        while (jDOSS1MSS2 >= hDOSS1MSS2 && allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2] > keyDOSS1MSS2)
        {
            allValuesDosS1MSS2[jDOSS1MSS2] = allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2];
            jDOSS1MSS2 = jDOSS1MSS2 - hDOSS1MSS2;
        }
        allValuesDosS1MSS2[jDOSS1MSS2] = keyDOSS1MSS2;
    }
    hDOSS1MSS2 = floor(hDOSS1MSS2/3);
}

numeric minValueDosS1MSS2 = allValuesDosS1MSS2[1];
numeric dn1000S1MSS2 = 10000;

numeric actualCountDosS1MSS2 = 0;
for i = 1 to dosS1MSS2realSize 
{
    if (allValuesDosS1MSS2[i] == minValueDosS1MSS2)
    {
        actualCountDosS1MSS2 = actualCountDosS1MSS2 + 1;
        if (actualCountDosS1MSS2 >= DNminCALCS1MSS2)
        {
           dn1000S1MSS2 = minValueDosS1MSS2;
           go to dn1000S1MSS2jumper;
        }
    }
    else
    {
        minValueDosS1MSS2 = allValuesDosS1MSS2[i+1];
        actualCountDosS1MSS2 = 0;
    }
}

dn1000S1MSS2jumper:

if (dn1000S1MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}
if (dn1000S1MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}

printf("dn1000 Scene 1 MSS2: %12f\n", dn1000S1MSS2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS1MSS3 = dosS1MSS3realSize;
numeric hDOSS1MSS3 = 1;

while ( (hDOSS1MSS3 * 3 + 1) < lastDOSS1MSS3 - 1 )
{
    hDOSS1MSS3 = 3 * hDOSS1MSS3 + 1;
}

while ( hDOSS1MSS3 > 0 )
{
    for i = hDOSS1MSS3 - 1 to lastDOSS1MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS3 = allValuesDosS1MSS3[i];
        numeric jDOSS1MSS3 = i;

        while (jDOSS1MSS3 >= hDOSS1MSS3 && allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3] > keyDOSS1MSS3)
        {
            allValuesDosS1MSS3[jDOSS1MSS3] = allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3];
            jDOSS1MSS3 = jDOSS1MSS3 - hDOSS1MSS3;
        }
        allValuesDosS1MSS3[jDOSS1MSS3] = keyDOSS1MSS3;
    }
    hDOSS1MSS3 = floor(hDOSS1MSS3/3);
}

numeric minValueDosS1MSS3 = allValuesDosS1MSS3[1];
numeric dn1000S1MSS3 = 10000;

numeric actualCountDosS1MSS3 = 0;
for i = 1 to dosS1MSS3realSize 
{
    if (allValuesDosS1MSS3[i] == minValueDosS1MSS3)
    {
        actualCountDosS1MSS3 = actualCountDosS1MSS3 + 1;
        if (actualCountDosS1MSS3 >= DNminCALCS1MSS3)
        {
            dn1000S1MSS3 = minValueDosS1MSS3;
            go to dn1000S1MSS3jumper;
        }
    }
    else
    {
        minValueDosS1MSS3 = allValuesDosS1MSS3[i+1];
        actualCountDosS1MSS3 = 0;
    }
}

dn1000S1MSS3jumper:

if (dn1000S1MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}
if (dn1000S1MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}

printf("dn1000 Scene 1 MSS3: %12f\n", dn1000S1MSS3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS1MSS4 = dosS1MSS4realSize;
numeric hDOSS1MSS4 = 1;

while ( (hDOSS1MSS4 * 3 + 1) < lastDOSS1MSS4 - 1 )
{
    hDOSS1MSS4 = 3 * hDOSS1MSS4 + 1;
}

while ( hDOSS1MSS4 > 0 )
{
    for i = hDOSS1MSS4 - 1 to lastDOSS1MSS4
    {
        numeric keyDOSS1MSS4 = allValuesDosS1MSS4[i];
        numeric jDOSS1MSS4 = i;

        while (jDOSS1MSS4 >= hDOSS1MSS4 && allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4] > keyDOSS1MSS4)
        {
            allValuesDosS1MSS4[jDOSS1MSS4] = allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4];
            jDOSS1MSS4 = jDOSS1MSS4 - hDOSS1MSS4;
        }
        allValuesDosS1MSS4[jDOSS1MSS4] = keyDOSS1MSS4;
    }
    hDOSS1MSS4 = floor(hDOSS1MSS4/3);
}

numeric minValueDosS1MSS4 = allValuesDosS1MSS4[1];
numeric dn1000S1MSS4 = 10000;

numeric actualCountDosS1MSS4 = 0;
for i = 1 to dosS1MSS4realSize 
{
    if (allValuesDosS1MSS4[i] == minValueDosS1MSS4)
    {
        actualCountDosS1MSS4 = actualCountDosS1MSS4 + 1;
        if (actualCountDosS1MSS4 >= DNminCALCS1MSS4)
        {
            dn1000S1MSS4 = minValueDosS1MSS4;
            go to dn1000S1MSS4jumper;
        }
    }
    else
    {
        minValueDosS1MSS4 = allValuesDosS1MSS4[i+1];
        actualCountDosS1MSS4 = 0;
    }
}

dn1000S1MSS4jumper:

if (dn1000S1MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
if (dn1000S1MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
	
printf("dn1000 Scene 1 MSS4: %12f\n", dn1000S1MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS1MSS1 = dn1000S1MSS1 - (0.01 * (radsurfMSS1b1 * cos(s1zenith)) / pi);
numeric pathRadS1MSS2 = dn1000S1MSS2 - (0.01 * (radsurfMSS1b2 * cos(s1zenith)) / pi);
numeric pathRadS1MSS3 = dn1000S1MSS3 - (0.01 * (radsurfMSS1b3 * cos(s1zenith)) / pi);
numeric pathRadS1MSS4 = dn1000S1MSS4 - (0.01 * (radsurfMSS1b4 * cos(s1zenith)) / pi);

printf("Pathradiance Scene 1 Band 1: %12f\n", pathRadS1MSS1);
printf("Pathradiance Scene 1 Band 2: %12f\n", pathRadS1MSS2);
printf("Pathradiance Scene 1 Band 3: %12f\n", pathRadS1MSS3);
printf("Pathradiance Scene 1 Band 4: %12f\n\n", pathRadS1MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS1MSS1 = (pi * (RADS1MSS1[i,j] - pathRadS1MSS1)) / (radsurfMSS1b1 * cos(s1zenith));

                  if ( valueS1MSS1 < 0 )
       		{
          			valueS1MSS1 = 0;
				countS1REFnullMSS1 = countS1REFnullMSS1 + 1;
       		}

			else if ( valueS1MSS1 > 1 )
			{
				valueS1MSS1 = 1;
				countS1REFoneMSS1 = countS1REFoneMSS1 + 1;
			}
       		
			REFS1MSS1[i,j] = valueS1MSS1;

               valueS1MSS2 = (pi * (RADS1MSS2[i,j] - pathRadS1MSS2)) / (radsurfMSS1b2 * cos(s1zenith));

                  if ( valueS1MSS2 < 0 )
       		{
          			valueS1MSS2 = 0;
				countS1REFnullMSS2 = countS1REFnullMSS2 + 1;
       		}

			else if ( valueS1MSS2 > 1 )
			{
				valueS1MSS2 = 1;
				countS1REFoneMSS2 = countS1REFoneMSS2 + 1;
			}
       		
			REFS1MSS2[i,j] = valueS1MSS2;

               valueS1MSS3 = (pi * (RADS1MSS3[i,j] - pathRadS1MSS3)) / (radsurfMSS1b3 * cos(s1zenith));

                  if ( valueS1MSS3 < 0 )
       		{
          			valueS1MSS3 = 0;
				countS1REFnullMSS3 = countS1REFnullMSS3 + 1;
       		}

			else if ( valueS1MSS3 > 1 )
			{
				valueS1MSS3 = 1;
				countS1REFoneMSS3 = countS1REFoneMSS3 + 1;
			}
       		
			REFS1MSS3[i,j] = valueS1MSS3;

               valueS1MSS4 = (pi * (RADS1MSS4[i,j] - pathRadS1MSS4)) / (radsurfMSS1b4 * cos(s1zenith));

                  if ( valueS1MSS4 < 0 )
       		{
          			valueS1MSS4 = 0;
				countS1REFnullMSS4 = countS1REFnullMSS4 + 1;
       		}

			else if ( valueS1MSS4 > 1 )
			{
				valueS1MSS4 = 1;
				countS1REFoneMSS4 = countS1REFoneMSS4 + 1;
			}
       		
			REFS1MSS4[i,j] = valueS1MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS1, countS1REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS2, countS1REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS3, countS1REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS4, countS1REFoneMSS4);

CreatePyramid(REFS1MSS1);
CreatePyramid(REFS1MSS2);
CreatePyramid(REFS1MSS3);
CreatePyramid(REFS1MSS4);
        
CreateHistogram(REFS1MSS1);
CreateHistogram(REFS1MSS2);
CreateHistogram(REFS1MSS3);
CreateHistogram(REFS1MSS4);

CloseRaster(RADS1MSS1);
CloseRaster(RADS1MSS2);
CloseRaster(RADS1MSS3);
CloseRaster(RADS1MSS4);

printf("Reflectance calculation for Scene 1 (MSS) is done...\n\n\n");
}

else if ( sensors1 == 4 )                                               # MSS 4
{
raster REFS1MSS1, REFS1MSS2, REFS1MSS3, REFS1MSS4;
numeric radsurfMSS1b1, radsurfMSS1b2, radsurfMSS1b3, radsurfMSS1b4;
numeric valueS1MSS1, valueS1MSS2, valueS1MSS3, valueS1MSS4;

numeric dosS1MSS1nullValCount = 0;
numeric dosS1MSS2nullValCount = 0;
numeric dosS1MSS3nullValCount = 0;
numeric dosS1MSS4nullValCount = 0;

numeric dosS1MSS1realArrayCount = 1;
numeric dosS1MSS2realArrayCount = 1;
numeric dosS1MSS3realArrayCount = 1;
numeric dosS1MSS4realArrayCount = 1;

numeric countS1REFnullMSS1 = 0;
numeric countS1REFnullMSS2 = 0;
numeric countS1REFnullMSS3 = 0;
numeric countS1REFnullMSS4 = 0;

numeric countS1REFoneMSS1 = 0;
numeric countS1REFoneMSS2 = 0;
numeric countS1REFoneMSS3 = 0;
numeric countS1REFoneMSS4 = 0;

radsurfMSS1b1 = ESMSS41 / (dist1^2);
radsurfMSS1b2 = ESMSS42 / (dist1^2);
radsurfMSS1b3 = ESMSS43 / (dist1^2);
radsurfMSS1b4 = ESMSS44 / (dist1^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 1): %12f\n\n", radsurfMSS1b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS1MSS1[i,j]) == 1)
		{
		dosS1MSS1nullValCount = dosS1MSS1nullValCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 1)
		{
		dosS1MSS2nullValCount = dosS1MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS3[i,j]) == 1)
		{
		dosS1MSS3nullValCount = dosS1MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS4[i,j]) == 1)
		{
		dosS1MSS4nullValCount = dosS1MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS1MSS1realSize = (MSSlins * MSScols) - dosS1MSS1nullValCount;
numeric dosS1MSS2realSize = (MSSlins * MSScols) - dosS1MSS2nullValCount;
numeric dosS1MSS3realSize = (MSSlins * MSScols) - dosS1MSS3nullValCount;
numeric dosS1MSS4realSize = (MSSlins * MSScols) - dosS1MSS4nullValCount;

array allValuesDosS1MSS1[dosS1MSS1realSize];
array allValuesDosS1MSS2[dosS1MSS2realSize];
array allValuesDosS1MSS3[dosS1MSS3realSize];
array allValuesDosS1MSS4[dosS1MSS4realSize];

numeric DNminCALCS1MSS1 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS1realSize);
numeric DNminCALCS1MSS2 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS2realSize);
numeric DNminCALCS1MSS3 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS3realSize);
numeric DNminCALCS1MSS4 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS1MSS1[i,j]) == 0)
		{
		  allValuesDosS1MSS1[dosS1MSS1realArrayCount] = RADS1MSS1[i,j];		 
		  dosS1MSS1realArrayCount = dosS1MSS1realArrayCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 0)
		{
		  allValuesDosS1MSS2[dosS1MSS2realArrayCount] = RADS1MSS2[i,j];		 
		  dosS1MSS2realArrayCount = dosS1MSS2realArrayCount + 1;
		}
		if (IsNull(RADS1MSS3[i,j]) == 0)
		{
		  allValuesDosS1MSS3[dosS1MSS3realArrayCount] = RADS1MSS3[i,j];		 
		  dosS1MSS3realArrayCount = dosS1MSS3realArrayCount + 1;
		}
		if (IsNull(RADS1MSS4[i,j]) == 0)
		{
		  allValuesDosS1MSS4[dosS1MSS4realArrayCount] = RADS1MSS4[i,j];		 
		  dosS1MSS4realArrayCount = dosS1MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS1MSS1 = dosS1MSS1realSize;
numeric hDOSS1MSS1 = 1;

while ( (hDOSS1MSS1 * 3 + 1) < lastDOSS1MSS1 - 1 )
{
    hDOSS1MSS1 = 3 * hDOSS1MSS1 + 1;
}

while ( hDOSS1MSS1 > 0 )
{
    for i = hDOSS1MSS1 - 1 to lastDOSS1MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS1 = allValuesDosS1MSS1[i];
        numeric jDOSS1MSS1 = i;

        while (jDOSS1MSS1 >= hDOSS1MSS1 && allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1] > keyDOSS1MSS1)
        {
            allValuesDosS1MSS1[jDOSS1MSS1] = allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1];
            jDOSS1MSS1 = jDOSS1MSS1 - hDOSS1MSS1;
        }
        allValuesDosS1MSS1[jDOSS1MSS1] = keyDOSS1MSS1;
    }
    hDOSS1MSS1 = floor(hDOSS1MSS1/3);
}

numeric minValueDosS1MSS1 = allValuesDosS1MSS1[1];
numeric dn1000S1MSS1 = 10000;

numeric actualCountDosS1MSS1 = 0;
for i = 1 to dosS1MSS1realSize 
{
    if (allValuesDosS1MSS1[i] == minValueDosS1MSS1)
    {
        actualCountDosS1MSS1 = actualCountDosS1MSS1 + 1;
        if (actualCountDosS1MSS1 >= DNminCALCS1MSS1)
        {
           dn1000S1MSS1 = minValueDosS1MSS1;
           go to dn1000S1MSS1jumper;
        }
    }
    else
    {
        minValueDosS1MSS1 = allValuesDosS1MSS1[i+1];
        actualCountDosS1MSS1 = 0;
    }
}

dn1000S1MSS1jumper:

if (dn1000S1MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}
if (dn1000S1MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}

printf("dn1000 Scene 1 MSS1: %12f\n", dn1000S1MSS1);

########################### Scene 1 - Dn1000 - Band 2

numeric lastDOSS1MSS2 = dosS1MSS1realSize;
numeric hDOSS1MSS2 = 1;

while ( (hDOSS1MSS2 * 3 + 1) < lastDOSS1MSS2 - 1 )
{
    hDOSS1MSS2 = 3 * hDOSS1MSS2 + 1;
}

while ( hDOSS1MSS2 > 0 )
{
    for i = hDOSS1MSS2 - 1 to lastDOSS1MSS2
    {
        numeric keyDOSS1MSS2 = allValuesDosS1MSS2[i];
        numeric jDOSS1MSS2 = i;

        while (jDOSS1MSS2 >= hDOSS1MSS2 && allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2] > keyDOSS1MSS2)
        {
            allValuesDosS1MSS2[jDOSS1MSS2] = allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2];
            jDOSS1MSS2 = jDOSS1MSS2 - hDOSS1MSS2;
        }
        allValuesDosS1MSS2[jDOSS1MSS2] = keyDOSS1MSS2;
    }
    hDOSS1MSS2 = floor(hDOSS1MSS2/3);
}

numeric minValueDosS1MSS2 = allValuesDosS1MSS2[1];
numeric dn1000S1MSS2 = 10000;

numeric actualCountDosS1MSS2 = 0;
for i = 1 to dosS1MSS2realSize 
{
    if (allValuesDosS1MSS2[i] == minValueDosS1MSS2)
    {
        actualCountDosS1MSS2 = actualCountDosS1MSS2 + 1;
        if (actualCountDosS1MSS2 >= DNminCALCS1MSS2)
        {
           dn1000S1MSS2 = minValueDosS1MSS2;
           go to dn1000S1MSS2jumper;
        }
    }
    else
    {
        minValueDosS1MSS2 = allValuesDosS1MSS2[i+1];
        actualCountDosS1MSS2 = 0;
    }
}

dn1000S1MSS2jumper:

if (dn1000S1MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}
if (dn1000S1MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}

printf("dn1000 Scene 1 MSS2: %12f\n", dn1000S1MSS2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS1MSS3 = dosS1MSS3realSize;
numeric hDOSS1MSS3 = 1;

while ( (hDOSS1MSS3 * 3 + 1) < lastDOSS1MSS3 - 1 )
{
    hDOSS1MSS3 = 3 * hDOSS1MSS3 + 1;
}

while ( hDOSS1MSS3 > 0 )
{
    for i = hDOSS1MSS3 - 1 to lastDOSS1MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS3 = allValuesDosS1MSS3[i];
        numeric jDOSS1MSS3 = i;

        while (jDOSS1MSS3 >= hDOSS1MSS3 && allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3] > keyDOSS1MSS3)
        {
            allValuesDosS1MSS3[jDOSS1MSS3] = allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3];
            jDOSS1MSS3 = jDOSS1MSS3 - hDOSS1MSS3;
        }
        allValuesDosS1MSS3[jDOSS1MSS3] = keyDOSS1MSS3;
    }
    hDOSS1MSS3 = floor(hDOSS1MSS3/3);
}

numeric minValueDosS1MSS3 = allValuesDosS1MSS3[1];
numeric dn1000S1MSS3 = 10000;

numeric actualCountDosS1MSS3 = 0;
for i = 1 to dosS1MSS3realSize 
{
    if (allValuesDosS1MSS3[i] == minValueDosS1MSS3)
    {
        actualCountDosS1MSS3 = actualCountDosS1MSS3 + 1;
        if (actualCountDosS1MSS3 >= DNminCALCS1MSS3)
        {
            dn1000S1MSS3 = minValueDosS1MSS3;
            go to dn1000S1MSS3jumper;
        }
    }
    else
    {
        minValueDosS1MSS3 = allValuesDosS1MSS3[i+1];
        actualCountDosS1MSS3 = 0;
    }
}

dn1000S1MSS3jumper:

if (dn1000S1MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}
if (dn1000S1MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}

printf("dn1000 Scene 1 MSS3: %12f\n", dn1000S1MSS3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS1MSS4 = dosS1MSS4realSize;
numeric hDOSS1MSS4 = 1;

while ( (hDOSS1MSS4 * 3 + 1) < lastDOSS1MSS4 - 1 )
{
    hDOSS1MSS4 = 3 * hDOSS1MSS4 + 1;
}

while ( hDOSS1MSS4 > 0 )
{
    for i = hDOSS1MSS4 - 1 to lastDOSS1MSS4
    {
        numeric keyDOSS1MSS4 = allValuesDosS1MSS4[i];
        numeric jDOSS1MSS4 = i;

        while (jDOSS1MSS4 >= hDOSS1MSS4 && allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4] > keyDOSS1MSS4)
        {
            allValuesDosS1MSS4[jDOSS1MSS4] = allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4];
            jDOSS1MSS4 = jDOSS1MSS4 - hDOSS1MSS4;
        }
        allValuesDosS1MSS4[jDOSS1MSS4] = keyDOSS1MSS4;
    }
    hDOSS1MSS4 = floor(hDOSS1MSS4/3);
}

numeric minValueDosS1MSS4 = allValuesDosS1MSS4[1];
numeric dn1000S1MSS4 = 10000;

numeric actualCountDosS1MSS4 = 0;
for i = 1 to dosS1MSS4realSize 
{
    if (allValuesDosS1MSS4[i] == minValueDosS1MSS4)
    {
        actualCountDosS1MSS4 = actualCountDosS1MSS4 + 1;
        if (actualCountDosS1MSS4 >= DNminCALCS1MSS4)
        {
            dn1000S1MSS4 = minValueDosS1MSS4;
            go to dn1000S1MSS4jumper;
        }
    }
    else
    {
        minValueDosS1MSS4 = allValuesDosS1MSS4[i+1];
        actualCountDosS1MSS4 = 0;
    }
}

dn1000S1MSS4jumper:

if (dn1000S1MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
if (dn1000S1MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
	
printf("dn1000 Scene 1 MSS4: %12f\n", dn1000S1MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS1MSS1 = dn1000S1MSS1 - (0.01 * (radsurfMSS1b1 * cos(s1zenith)) / pi);
numeric pathRadS1MSS2 = dn1000S1MSS2 - (0.01 * (radsurfMSS1b2 * cos(s1zenith)) / pi);
numeric pathRadS1MSS3 = dn1000S1MSS3 - (0.01 * (radsurfMSS1b3 * cos(s1zenith)) / pi);
numeric pathRadS1MSS4 = dn1000S1MSS4 - (0.01 * (radsurfMSS1b4 * cos(s1zenith)) / pi);

printf("Pathradiance Scene 1 Band 1: %12f\n", pathRadS1MSS1);
printf("Pathradiance Scene 1 Band 2: %12f\n", pathRadS1MSS2);
printf("Pathradiance Scene 1 Band 3: %12f\n", pathRadS1MSS3);
printf("Pathradiance Scene 1 Band 4: %12f\n\n", pathRadS1MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS1MSS1 = (pi * (RADS1MSS1[i,j] - pathRadS1MSS1)) / (radsurfMSS1b1 * cos(s1zenith));

                  if ( valueS1MSS1 < 0 )
       		{
          			valueS1MSS1 = 0;
				countS1REFnullMSS1 = countS1REFnullMSS1 + 1;
       		}

			else if ( valueS1MSS1 > 1 )
			{
				valueS1MSS1 = 1;
				countS1REFoneMSS1 = countS1REFoneMSS1 + 1;
			}
       		
			REFS1MSS1[i,j] = valueS1MSS1;

               valueS1MSS2 = (pi * (RADS1MSS2[i,j] - pathRadS1MSS2)) / (radsurfMSS1b2 * cos(s1zenith));

                  if ( valueS1MSS2 < 0 )
       		{
          			valueS1MSS2 = 0;
				countS1REFnullMSS2 = countS1REFnullMSS2 + 1;
       		}

			else if ( valueS1MSS2 > 1 )
			{
				valueS1MSS2 = 1;
				countS1REFoneMSS2 = countS1REFoneMSS2 + 1;
			}
       		
			REFS1MSS2[i,j] = valueS1MSS2;

               valueS1MSS3 = (pi * (RADS1MSS3[i,j] - pathRadS1MSS3)) / (radsurfMSS1b3 * cos(s1zenith));

                  if ( valueS1MSS3 < 0 )
       		{
          			valueS1MSS3 = 0;
				countS1REFnullMSS3 = countS1REFnullMSS3 + 1;
       		}

			else if ( valueS1MSS3 > 1 )
			{
				valueS1MSS3 = 1;
				countS1REFoneMSS3 = countS1REFoneMSS3 + 1;
			}
       		
			REFS1MSS3[i,j] = valueS1MSS3;

               valueS1MSS4 = (pi * (RADS1MSS4[i,j] - pathRadS1MSS4)) / (radsurfMSS1b4 * cos(s1zenith));

                  if ( valueS1MSS4 < 0 )
       		{
          			valueS1MSS4 = 0;
				countS1REFnullMSS4 = countS1REFnullMSS4 + 1;
       		}

			else if ( valueS1MSS4 > 1 )
			{
				valueS1MSS4 = 1;
				countS1REFoneMSS4 = countS1REFoneMSS4 + 1;
			}
       		
			REFS1MSS4[i,j] = valueS1MSS4;
           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS1, countS1REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS2, countS1REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS3, countS1REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS4, countS1REFoneMSS4);

CreatePyramid(REFS1MSS1);
CreatePyramid(REFS1MSS2);
CreatePyramid(REFS1MSS3);
CreatePyramid(REFS1MSS4);
        
CreateHistogram(REFS1MSS1);
CreateHistogram(REFS1MSS2);
CreateHistogram(REFS1MSS3);
CreateHistogram(REFS1MSS4);

CloseRaster(RADS1MSS1);
CloseRaster(RADS1MSS2);
CloseRaster(RADS1MSS3);
CloseRaster(RADS1MSS4);

printf("Reflectance calculation for Scene 1 (MSS) is done...\n\n\n");
}

else 																# MSS 1, 2 and 3
{
raster REFS1MSS1, REFS1MSS2, REFS1MSS3, REFS1MSS4;
numeric radsurfMSS1b1, radsurfMSS1b2, radsurfMSS1b3, radsurfMSS1b4;
numeric valueS1MSS1, valueS1MSS2, valueS1MSS3, valueS1MSS4;

numeric dosS1MSS1nullValCount = 0;
numeric dosS1MSS2nullValCount = 0;
numeric dosS1MSS3nullValCount = 0;
numeric dosS1MSS4nullValCount = 0;

numeric dosS1MSS1realArrayCount = 1;
numeric dosS1MSS2realArrayCount = 1;
numeric dosS1MSS3realArrayCount = 1;
numeric dosS1MSS4realArrayCount = 1;

numeric countS1REFnullMSS1 = 0;
numeric countS1REFnullMSS2 = 0;
numeric countS1REFnullMSS3 = 0;
numeric countS1REFnullMSS4 = 0;

numeric countS1REFoneMSS1 = 0;
numeric countS1REFoneMSS2 = 0;
numeric countS1REFoneMSS3 = 0;
numeric countS1REFoneMSS4 = 0;

radsurfMSS1b1 = ESMSS11 / (dist1^2);
radsurfMSS1b2 = ESMSS12 / (dist1^2);
radsurfMSS1b3 = ESMSS13 / (dist1^2);
radsurfMSS1b4 = ESMSS14 / (dist1^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 1): %12f\n", radsurfMSS1b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 1): %12f\n\n", radsurfMSS1b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS1MSS1[i,j]) == 1)
		{
		dosS1MSS1nullValCount = dosS1MSS1nullValCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 1)
		{
		dosS1MSS2nullValCount = dosS1MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS3[i,j]) == 1)
		{
		dosS1MSS3nullValCount = dosS1MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS1MSS4[i,j]) == 1)
		{
		dosS1MSS4nullValCount = dosS1MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS1MSS1realSize = (MSSlins * MSScols) - dosS1MSS1nullValCount;
numeric dosS1MSS2realSize = (MSSlins * MSScols) - dosS1MSS2nullValCount;
numeric dosS1MSS3realSize = (MSSlins * MSScols) - dosS1MSS3nullValCount;
numeric dosS1MSS4realSize = (MSSlins * MSScols) - dosS1MSS4nullValCount;

array allValuesDosS1MSS1[dosS1MSS1realSize];
array allValuesDosS1MSS2[dosS1MSS2realSize];
array allValuesDosS1MSS3[dosS1MSS3realSize];
array allValuesDosS1MSS4[dosS1MSS4realSize];

numeric DNminCALCS1MSS1 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS1realSize);
numeric DNminCALCS1MSS2 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS2realSize);
numeric DNminCALCS1MSS3 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS3realSize);
numeric DNminCALCS1MSS4 = DNminCALC(Scene1Lin, Scene1Col, dosS1MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS1MSS1[i,j]) == 0)
		{
		  allValuesDosS1MSS1[dosS1MSS1realArrayCount] = RADS1MSS1[i,j];		 
		  dosS1MSS1realArrayCount = dosS1MSS1realArrayCount + 1;
		}
		if (IsNull(RADS1MSS2[i,j]) == 0)
		{
		  allValuesDosS1MSS2[dosS1MSS2realArrayCount] = RADS1MSS2[i,j];		 
		  dosS1MSS2realArrayCount = dosS1MSS2realArrayCount + 1;
		}
		if (IsNull(RADS1MSS3[i,j]) == 0)
		{
		  allValuesDosS1MSS3[dosS1MSS3realArrayCount] = RADS1MSS3[i,j];		 
		  dosS1MSS3realArrayCount = dosS1MSS3realArrayCount + 1;
		}
		if (IsNull(RADS1MSS4[i,j]) == 0)
		{
		  allValuesDosS1MSS4[dosS1MSS4realArrayCount] = RADS1MSS4[i,j];		 
		  dosS1MSS4realArrayCount = dosS1MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS1MSS1 = dosS1MSS1realSize;
numeric hDOSS1MSS1 = 1;

while ( (hDOSS1MSS1 * 3 + 1) < lastDOSS1MSS1 - 1 )
{
    hDOSS1MSS1 = 3 * hDOSS1MSS1 + 1;
}

while ( hDOSS1MSS1 > 0 )
{
    for i = hDOSS1MSS1 - 1 to lastDOSS1MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS1 = allValuesDosS1MSS1[i];
        numeric jDOSS1MSS1 = i;

        while (jDOSS1MSS1 >= hDOSS1MSS1 && allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1] > keyDOSS1MSS1)
        {
            allValuesDosS1MSS1[jDOSS1MSS1] = allValuesDosS1MSS1[jDOSS1MSS1 - hDOSS1MSS1];
            jDOSS1MSS1 = jDOSS1MSS1 - hDOSS1MSS1;
        }
        allValuesDosS1MSS1[jDOSS1MSS1] = keyDOSS1MSS1;
    }
    hDOSS1MSS1 = floor(hDOSS1MSS1/3);
}

numeric minValueDosS1MSS1 = allValuesDosS1MSS1[1];
numeric dn1000S1MSS1 = 10000;

numeric actualCountDosS1MSS1 = 0;
for i = 1 to dosS1MSS1realSize 
{
    if (allValuesDosS1MSS1[i] == minValueDosS1MSS1)
    {
        actualCountDosS1MSS1 = actualCountDosS1MSS1 + 1;
        if (actualCountDosS1MSS1 >= DNminCALCS1MSS1)
        {
            dn1000S1MSS1 = minValueDosS1MSS1;
            go to dn1000S1MSS1jumper;
        }
    }
    else
    {
        minValueDosS1MSS1 = allValuesDosS1MSS1[i+1];
        actualCountDosS1MSS1 = 0;
    }
}

dn1000S1MSS1jumper:

if (dn1000S1MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}
if (dn1000S1MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS1 = allValuesDosS1MSS1[1];
}
	
printf("dn1000 Scene 1 MSS1: %12f\n", dn1000S1MSS1);

########################### Scene 1 - Dn1000 - Band 2

numeric lastDOSS1MSS2 = dosS1MSS1realSize;
numeric hDOSS1MSS2 = 1;

while ( (hDOSS1MSS2 * 3 + 1) < lastDOSS1MSS2 - 1 )
{
    hDOSS1MSS2 = 3 * hDOSS1MSS2 + 1;
}

while ( hDOSS1MSS2 > 0 )
{
    for i = hDOSS1MSS2 - 1 to lastDOSS1MSS2
    {
        numeric keyDOSS1MSS2 = allValuesDosS1MSS2[i];
        numeric jDOSS1MSS2 = i;

        while (jDOSS1MSS2 >= hDOSS1MSS2 && allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2] > keyDOSS1MSS2)
        {
            allValuesDosS1MSS2[jDOSS1MSS2] = allValuesDosS1MSS2[jDOSS1MSS2 - hDOSS1MSS2];
            jDOSS1MSS2 = jDOSS1MSS2 - hDOSS1MSS2;
        }
        allValuesDosS1MSS2[jDOSS1MSS2] = keyDOSS1MSS2;
    }
    hDOSS1MSS2 = floor(hDOSS1MSS2/3);
}

numeric minValueDosS1MSS2 = allValuesDosS1MSS2[1];
numeric dn1000S1MSS2 = 10000;

numeric actualCountDosS1MSS2 = 0;
for i = 1 to dosS1MSS2realSize 
{
    if (allValuesDosS1MSS2[i] == minValueDosS1MSS2)
    {
        actualCountDosS1MSS2 = actualCountDosS1MSS2 + 1;
        if (actualCountDosS1MSS2 >= DNminCALCS1MSS2)
        {
            dn1000S1MSS2 = minValueDosS1MSS2;
            go to dn1000S1MSS2jumper;
        }
    }
    else
    {
        minValueDosS1MSS2 = allValuesDosS1MSS2[i+1];
        actualCountDosS1MSS2 = 0;
    }
}

dn1000S1MSS2jumper:

if (dn1000S1MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}
if (dn1000S1MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS2 = allValuesDosS1MSS2[1];
}

printf("dn1000 Scene 1 MSS2: %12f\n", dn1000S1MSS2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS1MSS3 = dosS1MSS3realSize;
numeric hDOSS1MSS3 = 1;

while ( (hDOSS1MSS3 * 3 + 1) < lastDOSS1MSS3 - 1 )
{
    hDOSS1MSS3 = 3 * hDOSS1MSS3 + 1;
}

while ( hDOSS1MSS3 > 0 )
{
    for i = hDOSS1MSS3 - 1 to lastDOSS1MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS1MSS3 = allValuesDosS1MSS3[i];
        numeric jDOSS1MSS3 = i;

        while (jDOSS1MSS3 >= hDOSS1MSS3 && allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3] > keyDOSS1MSS3)
        {
            allValuesDosS1MSS3[jDOSS1MSS3] = allValuesDosS1MSS3[jDOSS1MSS3 - hDOSS1MSS3];
            jDOSS1MSS3 = jDOSS1MSS3 - hDOSS1MSS3;
        }
        allValuesDosS1MSS3[jDOSS1MSS3] = keyDOSS1MSS3;
    }
    hDOSS1MSS3 = floor(hDOSS1MSS3/3);
}

numeric minValueDosS1MSS3 = allValuesDosS1MSS3[1];
numeric dn1000S1MSS3 = 10000;

numeric actualCountDosS1MSS3 = 0;
for i = 1 to dosS1MSS3realSize 
{
    if (allValuesDosS1MSS3[i] == minValueDosS1MSS3)
    {
        actualCountDosS1MSS3 = actualCountDosS1MSS3 + 1;
        if (actualCountDosS1MSS3 >= DNminCALCS1MSS3)
        {
            dn1000S1MSS3 = minValueDosS1MSS3;
            go to dn1000S1MSS3jumper;
        }
    }
    else
    {
        minValueDosS1MSS3 = allValuesDosS1MSS3[i+1];
        actualCountDosS1MSS3 = 0;
    }
}

dn1000S1MSS3jumper:

if (dn1000S1MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}
if (dn1000S1MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS3 = allValuesDosS1MSS3[1];
}
	
printf("dn1000 Scene 1 MSS3: %12f\n", dn1000S1MSS3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS1MSS4 = dosS1MSS4realSize;
numeric hDOSS1MSS4 = 1;

while ( (hDOSS1MSS4 * 3 + 1) < lastDOSS1MSS4 - 1 )
{
    hDOSS1MSS4 = 3 * hDOSS1MSS4 + 1;
}

while ( hDOSS1MSS4 > 0 )
{
    for i = hDOSS1MSS4 - 1 to lastDOSS1MSS4
    {
        numeric keyDOSS1MSS4 = allValuesDosS1MSS4[i];
        numeric jDOSS1MSS4 = i;

        while (jDOSS1MSS4 >= hDOSS1MSS4 && allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4] > keyDOSS1MSS4)
        {
            allValuesDosS1MSS4[jDOSS1MSS4] = allValuesDosS1MSS4[jDOSS1MSS4 - hDOSS1MSS4];
            jDOSS1MSS4 = jDOSS1MSS4 - hDOSS1MSS4;
        }
        allValuesDosS1MSS4[jDOSS1MSS4] = keyDOSS1MSS4;
    }
    hDOSS1MSS4 = floor(hDOSS1MSS4/3);
}

numeric minValueDosS1MSS4 = allValuesDosS1MSS4[1];
numeric dn1000S1MSS4 = 10000;

numeric actualCountDosS1MSS4 = 0;
for i = 1 to dosS1MSS4realSize 
{
    if (allValuesDosS1MSS4[i] == minValueDosS1MSS4)
    {
        actualCountDosS1MSS4 = actualCountDosS1MSS4 + 1;
        if (actualCountDosS1MSS4 >= DNminCALCS1MSS4)
        {
            dn1000S1MSS4 = minValueDosS1MSS4;
            go to dn1000S1MSS4jumper;
        }
    }
    else
    {
        minValueDosS1MSS4 = allValuesDosS1MSS4[i+1];
        actualCountDosS1MSS4 = 0;
    }
}

dn1000S1MSS4jumper:

if (dn1000S1MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
if (dn1000S1MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S1MSS4 = allValuesDosS1MSS4[1];
}
	
printf("dn1000 Scene 1 MSS4: %12f\n", dn1000S1MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS1MSS1 = dn1000S1MSS1 - (0.01 * (radsurfMSS1b1 * cos(s1zenith)) / pi);
numeric pathRadS1MSS2 = dn1000S1MSS2 - (0.01 * (radsurfMSS1b2 * cos(s1zenith)) / pi);
numeric pathRadS1MSS3 = dn1000S1MSS3 - (0.01 * (radsurfMSS1b3 * cos(s1zenith)) / pi);
numeric pathRadS1MSS4 = dn1000S1MSS4 - (0.01 * (radsurfMSS1b4 * cos(s1zenith)) / pi);

printf("Pathradiance Scene 1 Band 1: %12f\n", pathRadS1MSS1);
printf("Pathradiance Scene 1 Band 2: %12f\n", pathRadS1MSS2);
printf("Pathradiance Scene 1 Band 3: %12f\n", pathRadS1MSS3);
printf("Pathradiance Scene 1 Band 4: %12f\n\n", pathRadS1MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS1MSS1 = (pi * (RADS1MSS1[i,j] - pathRadS1MSS1)) / (radsurfMSS1b1 * cos(s1zenith));

                  if ( valueS1MSS1 < 0 )
       		{
          			valueS1MSS1 = 0;
				countS1REFnullMSS1 = countS1REFnullMSS1 + 1;
       		}

			else if ( valueS1MSS1 > 1 )
			{
				valueS1MSS1 = 1;
				countS1REFoneMSS1 = countS1REFoneMSS1 + 1;
			}
       		
			REFS1MSS1[i,j] = valueS1MSS1;

               valueS1MSS2 = (pi * (RADS1MSS2[i,j] - pathRadS1MSS2)) / (radsurfMSS1b2 * cos(s1zenith));

                  if ( valueS1MSS2 < 0 )
       		{
          			valueS1MSS2 = 0;
				countS1REFnullMSS2 = countS1REFnullMSS2 + 1;
       		}

			else if ( valueS1MSS2 > 1 )
			{
				valueS1MSS2 = 1;
				countS1REFoneMSS2 = countS1REFoneMSS2 + 1;
			}
       		
			REFS1MSS2[i,j] = valueS1MSS2;

               valueS1MSS3 = (pi * (RADS1MSS3[i,j] - pathRadS1MSS3)) / (radsurfMSS1b3 * cos(s1zenith));

                  if ( valueS1MSS3 < 0 )
       		{
          			valueS1MSS3 = 0;
				countS1REFnullMSS3 = countS1REFnullMSS3 + 1;
       		}

			else if ( valueS1MSS3 > 1 )
			{
				valueS1MSS3 = 1;
				countS1REFoneMSS3 = countS1REFoneMSS3 + 1;
			}
       		
			REFS1MSS3[i,j] = valueS1MSS3;

               valueS1MSS4 = (pi * (RADS1MSS4[i,j] - pathRadS1MSS4)) / (radsurfMSS1b4 * cos(s1zenith));

                  if ( valueS1MSS4 < 0 )
       		{
          			valueS1MSS4 = 0;
				countS1REFnullMSS4 = countS1REFnullMSS4 + 1;
       		}

			else if ( valueS1MSS4 > 1 )
			{
				valueS1MSS4 = 1;
				countS1REFoneMSS4 = countS1REFoneMSS4 + 1;
			}
       		
			REFS1MSS4[i,j] = valueS1MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS1, countS1REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS2, countS1REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS3, countS1REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 1 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS1REFnullMSS4, countS1REFoneMSS4);

CreatePyramid(REFS1MSS1);
CreatePyramid(REFS1MSS2);
CreatePyramid(REFS1MSS3);
CreatePyramid(REFS1MSS4);
        
CreateHistogram(REFS1MSS1);
CreateHistogram(REFS1MSS2);
CreateHistogram(REFS1MSS3);
CreateHistogram(REFS1MSS4);

CloseRaster(RADS1MSS1);
CloseRaster(RADS1MSS2);
CloseRaster(RADS1MSS3);
CloseRaster(RADS1MSS4);

printf("Reflectance calculation for Scene 1 (MSS) is done...\n\n\n");
}

#############################
#### Reflectance Scene 2
#############################

if ( sensors2 == 7 )
{
raster REFS2ETM1, REFS2ETM2, REFS2ETM3, REFS2ETM4, REFS2ETM5, REFS2ETM7;
numeric radsurfETMS2b1, radsurfETMS2b2, radsurfETMS2b3, radsurfETMS2b4, radsurfETMS2b5, radsurfETMS2b7;
numeric valueS2ETM1, valueS2ETM2, valueS2ETM3, valueS2ETM4, valueS2ETM5, valueS2ETM7;

numeric dosS2ETM1nullValCount = 0;
numeric dosS2ETM2nullValCount = 0;
numeric dosS2ETM3nullValCount = 0;
numeric dosS2ETM4nullValCount = 0;
numeric dosS2ETM5nullValCount = 0;
numeric dosS2ETM7nullValCount = 0;

numeric dosS2ETM1realArrayCount = 1;
numeric dosS2ETM2realArrayCount = 1;
numeric dosS2ETM3realArrayCount = 1;
numeric dosS2ETM4realArrayCount = 1;
numeric dosS2ETM5realArrayCount = 1;
numeric dosS2ETM7realArrayCount = 1;

numeric countS2REFnullETM1 = 0;
numeric countS2REFnullETM2 = 0;
numeric countS2REFnullETM3 = 0;
numeric countS2REFnullETM4 = 0;
numeric countS2REFnullETM5 = 0;
numeric countS2REFnullETM7 = 0;

numeric countS2REFoneETM1 = 0;
numeric countS2REFoneETM2 = 0;
numeric countS2REFoneETM3 = 0;
numeric countS2REFoneETM4 = 0;
numeric countS2REFoneETM5 = 0;
numeric countS2REFoneETM7 = 0;

radsurfETMS2b1 = ESETM1 / (dist2^2);
radsurfETMS2b2 = ESETM2 / (dist2^2);
radsurfETMS2b3 = ESETM3 / (dist2^2);
radsurfETMS2b4 = ESETM4 / (dist2^2);
radsurfETMS2b5 = ESETM5 / (dist2^2);
radsurfETMS2b7 = ESETM7 / (dist2^2);

printf("Surface Radiance ETM Band 1 in W/m^2µm (Scene 2): %12f\n", radsurfETMS2b1);
printf("Surface Radiance ETM Band 2 in W/m^2µm (Scene 2): %12f\n", radsurfETMS2b2);
printf("Surface Radiance ETM Band 3 in W/m^2µm (Scene 2): %12f\n", radsurfETMS2b3);
printf("Surface Radiance ETM Band 4 in W/m^2µm (Scene 2): %12f\n", radsurfETMS2b4);
printf("Surface Radiance ETM Band 5 in W/m^2µm (Scene 2): %12f\n", radsurfETMS2b5);
printf("Surface Radiance ETM Band 7 in W/m^2µm (Scene 2): %12f\n\n", radsurfETMS2b7);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {       
		if (IsNull(RADS2ETM1[i,j]) == 1)
		{
		dosS2ETM1nullValCount = dosS2ETM1nullValCount + 1;
		}
		if (IsNull(RADS2ETM2[i,j]) == 1)
		{
		dosS2ETM2nullValCount = dosS2ETM2nullValCount + 1;
		}		 
		if (IsNull(RADS2ETM3[i,j]) == 1)
		{
		dosS2ETM3nullValCount = dosS2ETM3nullValCount + 1;
		}		 
		if (IsNull(RADS2ETM4[i,j]) == 1)
		{
		dosS2ETM4nullValCount = dosS2ETM4nullValCount + 1;
		}		 
		if (IsNull(RADS2ETM5[i,j]) == 1)
		{
		dosS2ETM5nullValCount = dosS2ETM5nullValCount + 1;
		}		 
		if (IsNull(RADS2ETM7[i,j]) == 1)
		{
		dosS2ETM7nullValCount = dosS2ETM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS2ETM1realSize = (ETMlins * ETMcols) - dosS2ETM1nullValCount;
numeric dosS2ETM2realSize = (ETMlins * ETMcols) - dosS2ETM2nullValCount;
numeric dosS2ETM3realSize = (ETMlins * ETMcols) - dosS2ETM3nullValCount;
numeric dosS2ETM4realSize = (ETMlins * ETMcols) - dosS2ETM4nullValCount;
numeric dosS2ETM5realSize = (ETMlins * ETMcols) - dosS2ETM5nullValCount;
numeric dosS2ETM7realSize = (ETMlins * ETMcols) - dosS2ETM7nullValCount;

array allValuesDosS2ETM1[dosS2ETM1realSize];
array allValuesDosS2ETM2[dosS2ETM2realSize];
array allValuesDosS2ETM3[dosS2ETM3realSize];
array allValuesDosS2ETM4[dosS2ETM4realSize];
array allValuesDosS2ETM5[dosS2ETM5realSize];
array allValuesDosS2ETM7[dosS2ETM7realSize];

numeric DNminCALCS2ETM1 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM1realSize);
numeric DNminCALCS2ETM2 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM2realSize);
numeric DNminCALCS2ETM3 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM3realSize);
numeric DNminCALCS2ETM4 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM4realSize);
numeric DNminCALCS2ETM5 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM5realSize);
numeric DNminCALCS2ETM7 = DNminCALC(Scene2Lin, Scene2Col, dosS2ETM7realSize);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(RADS2ETM1[i,j]) == 0)
		{
		  allValuesDosS2ETM1[dosS2ETM1realArrayCount] = RADS2ETM1[i,j];		 
		  dosS2ETM1realArrayCount = dosS2ETM1realArrayCount + 1;
		}
		if (IsNull(RADS2ETM2[i,j]) == 0)
		{
		  allValuesDosS2ETM2[dosS2ETM2realArrayCount] = RADS2ETM2[i,j];		 
		  dosS2ETM2realArrayCount = dosS2ETM2realArrayCount + 1;
		}
		if (IsNull(RADS2ETM3[i,j]) == 0)
		{
		  allValuesDosS2ETM3[dosS2ETM3realArrayCount] = RADS2ETM3[i,j];		 
		  dosS2ETM3realArrayCount = dosS2ETM3realArrayCount + 1;
		}
		if (IsNull(RADS2ETM4[i,j]) == 0)
		{
		  allValuesDosS2ETM4[dosS2ETM4realArrayCount] = RADS2ETM4[i,j];		 
		  dosS2ETM4realArrayCount = dosS2ETM4realArrayCount + 1;
		}
		if (IsNull(RADS2ETM5[i,j]) == 0)
		{
		  allValuesDosS2ETM5[dosS2ETM5realArrayCount] = RADS2ETM5[i,j];		 
		  dosS2ETM5realArrayCount = dosS2ETM5realArrayCount + 1;
		}
		if (IsNull(RADS2ETM7[i,j]) == 0)
		{
		  allValuesDosS2ETM7[dosS2ETM7realArrayCount] = RADS2ETM7[i,j];		 
		  dosS2ETM7realArrayCount = dosS2ETM7realArrayCount + 1;
		}
    }
}

########################### Scene 2 - Dn1000 - Band 1

numeric lastDOSS2ETM1 = dosS2ETM1realSize;
numeric hDOSS2ETM1 = 1;

while ( (hDOSS2ETM1 * 3 + 1) < lastDOSS2ETM1 - 1 )
{
    hDOSS2ETM1 = 3 * hDOSS2ETM1 + 1;
}

while ( hDOSS2ETM1 > 0 )
{
    for i = hDOSS2ETM1 - 1 to lastDOSS2ETM1               # for each of the h sets of elements
    {
        numeric keyDOSS2ETM1 = allValuesDosS2ETM1[i];
        numeric jDOSS2ETM1 = i;

        while (jDOSS2ETM1 >= hDOSS2ETM1 && allValuesDosS2ETM1[jDOSS2ETM1 - hDOSS2ETM1] > keyDOSS2ETM1)
        {
            allValuesDosS2ETM1[jDOSS2ETM1] = allValuesDosS2ETM1[jDOSS2ETM1 - hDOSS2ETM1];
            jDOSS2ETM1 = jDOSS2ETM1 - hDOSS2ETM1;
        }
        allValuesDosS2ETM1[jDOSS2ETM1] = keyDOSS2ETM1;
    }
    hDOSS2ETM1 = floor(hDOSS2ETM1/3);
}

numeric minValueDosS2ETM1 = allValuesDosS2ETM1[1];
numeric dn1000S2ETM1 = 10000;

numeric actualCountDosS2ETM1 = 0;
for i = 1 to dosS2ETM1realSize 
{
    if (allValuesDosS2ETM1[i] == minValueDosS2ETM1)
    {
        actualCountDosS2ETM1 = actualCountDosS2ETM1 + 1;
        if (actualCountDosS2ETM1 >= DNminCALCS2ETM1)
        {
            dn1000S2ETM1 = minValueDosS2ETM1;
            go to dn1000S2ETM1jumper;
        }
    }
    else
    {
        minValueDosS2ETM1 = allValuesDosS2ETM1[i+1];
        actualCountDosS2ETM1 = 0;
    }
}

dn1000S2ETM1jumper:

if (dn1000S2ETM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM1 = allValuesDosS2ETM1[1];
}
if (dn1000S2ETM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM1 = allValuesDosS2ETM1[1];
}
	
printf("dn1000 Scene 2 ETM1: %12f\n", dn1000S2ETM1);

########################### Scene 2 - Dn1000 - Band 2

numeric lastDOSS2ETM2 = dosS2ETM1realSize;
numeric hDOSS2ETM2 = 1;

while ( (hDOSS2ETM2 * 3 + 1) < lastDOSS2ETM2 - 1 )
{
    hDOSS2ETM2 = 3 * hDOSS2ETM2 + 1;
}

while ( hDOSS2ETM2 > 0 )
{
    for i = hDOSS2ETM2 - 1 to lastDOSS2ETM2
    {
        numeric keyDOSS2ETM2 = allValuesDosS2ETM2[i];
        numeric jDOSS2ETM2 = i;

        while (jDOSS2ETM2 >= hDOSS2ETM2 && allValuesDosS2ETM2[jDOSS2ETM2 - hDOSS2ETM2] > keyDOSS2ETM2)
        {
            allValuesDosS2ETM2[jDOSS2ETM2] = allValuesDosS2ETM2[jDOSS2ETM2 - hDOSS2ETM2];
            jDOSS2ETM2 = jDOSS2ETM2 - hDOSS2ETM2;
        }
        allValuesDosS2ETM2[jDOSS2ETM2] = keyDOSS2ETM2;
    }
    hDOSS2ETM2 = floor(hDOSS2ETM2/3);
}

numeric minValueDosS2ETM2 = allValuesDosS2ETM2[1];
numeric dn1000S2ETM2 = 10000;

numeric actualCountDosS2ETM2 = 0;
for i = 1 to dosS2ETM2realSize 
{
    if (allValuesDosS2ETM2[i] == minValueDosS2ETM2)
    {
        actualCountDosS2ETM2 = actualCountDosS2ETM2 + 1;
        if (actualCountDosS2ETM2 >= DNminCALCS2ETM2)
        {
            dn1000S2ETM2 = minValueDosS2ETM2;
            go to dn1000S2ETM2jumper;
        }
    }
    else
    {
        minValueDosS2ETM2 = allValuesDosS2ETM2[i+1];
        actualCountDosS2ETM2 = 0;
    }
}

dn1000S2ETM2jumper:

if (dn1000S2ETM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM2 = allValuesDosS2ETM2[1];
}
if (dn1000S2ETM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM2 = allValuesDosS2ETM2[1];
}

printf("dn1000 Scene 2 ETM2: %12f\n", dn1000S2ETM2);

########################### Scene 2 - Dn1000 - Band 3

numeric lastDOSS2ETM3 = dosS2ETM3realSize;
numeric hDOSS2ETM3 = 1;

while ( (hDOSS2ETM3 * 3 + 1) < lastDOSS2ETM3 - 1 )
{
    hDOSS2ETM3 = 3 * hDOSS2ETM3 + 1;
}

while ( hDOSS2ETM3 > 0 )
{
    for i = hDOSS2ETM3 - 1 to lastDOSS2ETM3               # for each of the h sets of elements
    {
        numeric keyDOSS2ETM3 = allValuesDosS2ETM3[i];
        numeric jDOSS2ETM3 = i;

        while (jDOSS2ETM3 >= hDOSS2ETM3 && allValuesDosS2ETM3[jDOSS2ETM3 - hDOSS2ETM3] > keyDOSS2ETM3)
        {
            allValuesDosS2ETM3[jDOSS2ETM3] = allValuesDosS2ETM3[jDOSS2ETM3 - hDOSS2ETM3];
            jDOSS2ETM3 = jDOSS2ETM3 - hDOSS2ETM3;
        }
        allValuesDosS2ETM3[jDOSS2ETM3] = keyDOSS2ETM3;
    }
    hDOSS2ETM3 = floor(hDOSS2ETM3/3);
}

numeric minValueDosS2ETM3 = allValuesDosS2ETM3[1];
numeric dn1000S2ETM3 = 10000;

numeric actualCountDosS2ETM3 = 0;
for i = 1 to dosS2ETM3realSize 
{
    if (allValuesDosS2ETM3[i] == minValueDosS2ETM3)
    {
        actualCountDosS2ETM3 = actualCountDosS2ETM3 + 1;
        if (actualCountDosS2ETM3 >= DNminCALCS2ETM3)
        {
            dn1000S2ETM3 = minValueDosS2ETM3;
            go to dn1000S2ETM3jumper;
        }
    }
    else
    {
        minValueDosS2ETM3 = allValuesDosS2ETM3[i+1];
        actualCountDosS2ETM3 = 0;
    }
}

dn1000S2ETM3jumper:

if (dn1000S2ETM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM3 = allValuesDosS2ETM3[1];
}
if (dn1000S2ETM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM3 = allValuesDosS2ETM3[1];
}
	
printf("dn1000 Scene 2 ETM3: %12f\n", dn1000S2ETM3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS2ETM4 = dosS2ETM4realSize;
numeric hDOSS2ETM4 = 1;

while ( (hDOSS2ETM4 * 3 + 1) < lastDOSS2ETM4 - 1 )
{
    hDOSS2ETM4 = 3 * hDOSS2ETM4 + 1;
}

while ( hDOSS2ETM4 > 0 )
{
    for i = hDOSS2ETM4 - 1 to lastDOSS2ETM4
    {
        numeric keyDOSS2ETM4 = allValuesDosS2ETM4[i];
        numeric jDOSS2ETM4 = i;

        while (jDOSS2ETM4 >= hDOSS2ETM4 && allValuesDosS2ETM4[jDOSS2ETM4 - hDOSS2ETM4] > keyDOSS2ETM4)
        {
            allValuesDosS2ETM4[jDOSS2ETM4] = allValuesDosS2ETM4[jDOSS2ETM4 - hDOSS2ETM4];
            jDOSS2ETM4 = jDOSS2ETM4 - hDOSS2ETM4;
        }
        allValuesDosS2ETM4[jDOSS2ETM4] = keyDOSS2ETM4;
    }
    hDOSS2ETM4 = floor(hDOSS2ETM4/3);
}

numeric minValueDosS2ETM4 = allValuesDosS2ETM4[1];
numeric dn1000S2ETM4 = 10000;

numeric actualCountDosS2ETM4 = 0;
for i = 1 to dosS2ETM4realSize 
{
    if (allValuesDosS2ETM4[i] == minValueDosS2ETM4)
    {
        actualCountDosS2ETM4 = actualCountDosS2ETM4 + 1;
        if (actualCountDosS2ETM4 >= DNminCALCS2ETM4)
        {
            dn1000S2ETM4 = minValueDosS2ETM4;
            go to dn1000S2ETM4jumper;
        }
    }
    else
    {
        minValueDosS2ETM4 = allValuesDosS2ETM4[i+1];
        actualCountDosS2ETM4 = 0;
    }
}

dn1000S2ETM4jumper:

if (dn1000S2ETM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM4 = allValuesDosS2ETM4[1];
}
if (dn1000S2ETM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM4 = allValuesDosS2ETM4[1];
}
	
printf("dn1000 Scene 2 ETM4: %12f\n", dn1000S2ETM4);

########################### Scene 2 - Dn1000 - Band 5

numeric lastDOSS2ETM5 = dosS2ETM5realSize;
numeric hDOSS2ETM5 = 1;

while ( (hDOSS2ETM5 * 3 + 1) < lastDOSS2ETM5 - 1 )
{
    hDOSS2ETM5 = 3 * hDOSS2ETM5 + 1;
}

while ( hDOSS2ETM5 > 0 )
{
    for i = hDOSS2ETM5 - 1 to lastDOSS2ETM5               # for each of the h sets of elements
    {
        numeric keyDOSS2ETM5 = allValuesDosS2ETM5[i];
        numeric jDOSS2ETM5 = i;

        while (jDOSS2ETM5 >= hDOSS2ETM5 && allValuesDosS2ETM5[jDOSS2ETM5 - hDOSS2ETM5] > keyDOSS2ETM5)
        {
            allValuesDosS2ETM5[jDOSS2ETM5] = allValuesDosS2ETM5[jDOSS2ETM5 - hDOSS2ETM5];
            jDOSS2ETM5 = jDOSS2ETM5 - hDOSS2ETM5;
        }
        allValuesDosS2ETM5[jDOSS2ETM5] = keyDOSS2ETM5;
    }
    hDOSS2ETM5 = floor(hDOSS2ETM5/3);
}

numeric minValueDosS2ETM5 = allValuesDosS2ETM5[1];
numeric dn1000S2ETM5 = 10000;

numeric actualCountDosS2ETM5 = 0;
for i = 1 to dosS2ETM5realSize 
{
    if (allValuesDosS2ETM5[i] == minValueDosS2ETM5)
    {
        actualCountDosS2ETM5 = actualCountDosS2ETM5 + 1;
        if (actualCountDosS2ETM5 >= DNminCALCS2ETM5)
        {
            dn1000S2ETM5 = minValueDosS2ETM5;
            go to dn1000S2ETM5jumper;
        }
    }
    else
    {
        minValueDosS2ETM5 = allValuesDosS2ETM5[i+1];
        actualCountDosS2ETM5 = 0;
    }
}

dn1000S2ETM5jumper:

if (dn1000S2ETM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM5  = allValuesDosS2ETM5[1];
}
if (dn1000S2ETM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM5  = allValuesDosS2ETM5[1];
}
	
printf("dn1000 Scene 2 ETM5: %12f\n", dn1000S2ETM5);

########################### Scene 2 - Dn1000 - Band 7

numeric lastDOSS2ETM7 = dosS2ETM7realSize;
numeric hDOSS2ETM7 = 1;

while ( (hDOSS2ETM7 * 3 + 1) < lastDOSS2ETM7 - 1 )
{
    hDOSS2ETM7 = 3 * hDOSS2ETM7 + 1;
}

while ( hDOSS2ETM7 > 0 )
{
    for i = hDOSS2ETM7 - 1 to lastDOSS2ETM7               # for each of the h sets of elements
    {
        numeric keyDOSS2ETM7 = allValuesDosS2ETM7[i];
        numeric jDOSS2ETM7 = i;

        while (jDOSS2ETM7 >= hDOSS2ETM7 && allValuesDosS2ETM7[jDOSS2ETM7 - hDOSS2ETM7] > keyDOSS2ETM7)
        {
            allValuesDosS2ETM7[jDOSS2ETM7] = allValuesDosS2ETM7[jDOSS2ETM7 - hDOSS2ETM7];
            jDOSS2ETM7 = jDOSS2ETM7 - hDOSS2ETM7;
        }
        allValuesDosS2ETM7[jDOSS2ETM7] = keyDOSS2ETM7;
    }
    hDOSS2ETM7 = floor(hDOSS2ETM7/3);
}

numeric minValueDosS2ETM7 = allValuesDosS2ETM7[1];
numeric dn1000S2ETM7 = 10000;

numeric actualCountDosS2ETM7 = 0;
for i = 1 to dosS2ETM7realSize 
{
    if (allValuesDosS2ETM7[i] == minValueDosS2ETM7)
    {
        actualCountDosS2ETM7 = actualCountDosS2ETM7 + 1;
        if (actualCountDosS2ETM7 >= DNminCALCS2ETM7)
        {
            dn1000S2ETM7 = minValueDosS2ETM7;
            go to dn1000S2ETM7jumper;
        }
    }
    else
    {
        minValueDosS2ETM7 = allValuesDosS2ETM7[i+1];
        actualCountDosS2ETM7 = 0;
    }
}

dn1000S2ETM7jumper:

if (dn1000S2ETM7  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2ETM7  = allValuesDosS2ETM7[1];
}
if (dn1000S2ETM7  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2ETM7  = allValuesDosS2ETM7[1];
}
	
printf("dn1000 Scene 2 ETM7: %12f\n\n", dn1000S2ETM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS2ETM1 = dn1000S2ETM1 - (0.01 * (radsurfETMS2b1 * cos(s2zenith)) / pi);
numeric pathRadS2ETM2 = dn1000S2ETM2 - (0.01 * (radsurfETMS2b2 * cos(s2zenith)) / pi);
numeric pathRadS2ETM3 = dn1000S2ETM3 - (0.01 * (radsurfETMS2b3 * cos(s2zenith)) / pi);
numeric pathRadS2ETM4 = dn1000S2ETM4 - (0.01 * (radsurfETMS2b4 * cos(s2zenith)) / pi);
numeric pathRadS2ETM5 = dn1000S2ETM5 - (0.01 * (radsurfETMS2b5 * cos(s2zenith)) / pi);
numeric pathRadS2ETM7 = dn1000S2ETM7 - (0.01 * (radsurfETMS2b7 * cos(s2zenith)) / pi);

printf("Pathradiance Scene 2 Band 1: %12f\n", pathRadS2ETM1);
printf("Pathradiance Scene 2 Band 2: %12f\n", pathRadS2ETM2);
printf("Pathradiance Scene 2 Band 3: %12f\n", pathRadS2ETM3);
printf("Pathradiance Scene 2 Band 4: %12f\n", pathRadS2ETM4);
printf("Pathradiance Scene 2 Band 5: %12f\n", pathRadS2ETM5);
printf("Pathradiance Scene 2 Band 7: %12f\n\n", pathRadS2ETM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to ETMlins
	 {
	        for j = 1 to ETMcols
	        {
                  valueS2ETM1 = (pi * (RADS2ETM1[i,j] - pathRadS2ETM1)) / (radsurfETMS2b1 * cos(s2zenith));

                  if ( valueS2ETM1 < 0 )
       		{
          			valueS2ETM1 = 0;
				countS2REFnullETM1 = countS2REFnullETM1 + 1;
       		}

			else if ( valueS2ETM1 > 1 )
			{
				valueS2ETM1 = 1;
				countS2REFoneETM1 = countS2REFoneETM1 + 1;
			}
       		
			REFS2ETM1[i,j] = valueS2ETM1;

               valueS2ETM2 = (pi * (RADS2ETM2[i,j] - pathRadS2ETM2)) / (radsurfETMS2b2 * cos(s2zenith));

                  if ( valueS2ETM2 < 0 )
       		{
          			valueS2ETM2 = 0;
				countS2REFnullETM2 = countS2REFnullETM2 + 1;
       		}

			else if ( valueS2ETM2 > 1 )
			{
				valueS2ETM2 = 1;
				countS2REFoneETM2 = countS2REFoneETM2 + 1;
			}
       		
			REFS2ETM2[i,j] = valueS2ETM2;

               valueS2ETM3 = (pi * (RADS2ETM3[i,j] - pathRadS2ETM3)) / (radsurfETMS2b3 * cos(s2zenith));

                  if ( valueS2ETM3 < 0 )
       		{
          			valueS2ETM3 = 0;
				countS2REFnullETM3 = countS2REFnullETM3 + 1;
       		}

			else if ( valueS2ETM3 > 1 )
			{
				valueS2ETM3 = 1;
				countS2REFoneETM3 = countS2REFoneETM3 + 1;
			}
       		
			REFS2ETM3[i,j] = valueS2ETM3;

               valueS2ETM4 = (pi * (RADS2ETM4[i,j] - pathRadS2ETM4)) / (radsurfETMS2b4 * cos(s2zenith));

                  if ( valueS2ETM4 < 0 )
       		{
          			valueS2ETM4 = 0;
				countS2REFnullETM4 = countS2REFnullETM4 + 1;
       		}

			else if ( valueS2ETM4 > 1 )
			{
				valueS2ETM4 = 1;
				countS2REFoneETM4 = countS2REFoneETM4 + 1;
			}
       		
			REFS2ETM4[i,j] = valueS2ETM4;

               valueS2ETM5 = (pi * (RADS2ETM5[i,j] - pathRadS2ETM5)) / (radsurfETMS2b5 * cos(s2zenith));

                  if ( valueS2ETM5 < 0 )
       		{
          			valueS2ETM5 = 0;
				countS2REFnullETM5 = countS2REFnullETM5 + 1;
       		}

			else if ( valueS2ETM5 > 1 )
			{
				valueS2ETM5 = 1;
				countS2REFoneETM5 = countS2REFoneETM5 + 1;
			}
       		
			REFS2ETM5[i,j] = valueS2ETM5;

               valueS2ETM7 = (pi * (RADS2ETM7[i,j] - pathRadS2ETM7)) / (radsurfETMS2b7 * cos(s2zenith));

                  if ( valueS2ETM7 < 0 )
       		{
          			valueS2ETM7 = 0;
				countS2REFnullETM7 = countS2REFnullETM7 + 1;
       		}

			else if ( valueS2ETM7 > 1 )
			{
				valueS2ETM7 = 1;
				countS2REFoneETM7 = countS2REFoneETM7 + 1;
			}
       		
			REFS2ETM7[i,j] = valueS2ETM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullETM1, countS2REFoneETM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullETM2, countS2REFoneETM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullETM3, countS2REFoneETM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullETM4, countS2REFoneETM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullETM5, countS2REFoneETM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS2REFnullETM7, countS2REFoneETM7);

CreatePyramid(REFS2ETM1);
CreatePyramid(REFS2ETM2);
CreatePyramid(REFS2ETM3);
CreatePyramid(REFS2ETM4);
CreatePyramid(REFS2ETM5);
CreatePyramid(REFS2ETM7);
        
CreateHistogram(REFS2ETM1);
CreateHistogram(REFS2ETM2);
CreateHistogram(REFS2ETM3);
CreateHistogram(REFS2ETM4);
CreateHistogram(REFS2ETM5);
CreateHistogram(REFS2ETM7);

CloseRaster(RADS2ETM1);
CloseRaster(RADS2ETM2);
CloseRaster(RADS2ETM3);
CloseRaster(RADS2ETM4);
CloseRaster(RADS2ETM5);
CloseRaster(RADS2ETM7);

printf("Reflectance calculation for Scene 2 (ETM) is done...\n\n\n");
}

else if ( sensors2 == 6 )
{
raster REFS2TM1, REFS2TM2, REFS2TM3, REFS2TM4, REFS2TM5, REFS2TM7;
numeric radsurfTMS2b1, radsurfTMS2b2, radsurfTMS2b3, radsurfTMS2b4, radsurfTMS2b5, radsurfTMS2b7;
numeric valueS2TM1, valueS2TM2, valueS2TM3, valueS2TM4, valueS2TM5, valueS2TM7;

numeric dosS2TM1nullValCount = 0;
numeric dosS2TM2nullValCount = 0;
numeric dosS2TM3nullValCount = 0;
numeric dosS2TM4nullValCount = 0;
numeric dosS2TM5nullValCount = 0;
numeric dosS2TM7nullValCount = 0;

numeric dosS2TM1realArrayCount = 1;
numeric dosS2TM2realArrayCount = 1;
numeric dosS2TM3realArrayCount = 1;
numeric dosS2TM4realArrayCount = 1;
numeric dosS2TM5realArrayCount = 1;
numeric dosS2TM7realArrayCount = 1;

numeric countS2REFnullTM1 = 0;
numeric countS2REFnullTM2 = 0;
numeric countS2REFnullTM3 = 0;
numeric countS2REFnullTM4 = 0;
numeric countS2REFnullTM5 = 0;
numeric countS2REFnullTM7 = 0;

numeric countS2REFoneTM1 = 0;
numeric countS2REFoneTM2 = 0;
numeric countS2REFoneTM3 = 0;
numeric countS2REFoneTM4 = 0;
numeric countS2REFoneTM5 = 0;
numeric countS2REFoneTM7 = 0;

radsurfTMS2b1 = ESTM1 / (dist2^2);
radsurfTMS2b2 = ESTM2 / (dist2^2);
radsurfTMS2b3 = ESTM3 / (dist2^2);
radsurfTMS2b4 = ESTM4 / (dist2^2);
radsurfTMS2b5 = ESTM5 / (dist2^2);
radsurfTMS2b7 = ESTM7 / (dist2^2);

printf("Surface Radiance TM Band 1 in W/m^2µm (Scene 2): %12f\n", radsurfTMS2b1);
printf("Surface Radiance TM Band 2 in W/m^2µm (Scene 2): %12f\n", radsurfTMS2b2);
printf("Surface Radiance TM Band 3 in W/m^2µm (Scene 2): %12f\n", radsurfTMS2b3);
printf("Surface Radiance TM Band 4 in W/m^2µm (Scene 2): %12f\n", radsurfTMS2b4);
printf("Surface Radiance TM Band 5 in W/m^2µm (Scene 2): %12f\n", radsurfTMS2b5);
printf("Surface Radiance TM Band 7 in W/m^2µm (Scene 2): %12f\n\n", radsurfTMS2b7);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {       
		if (IsNull(RADS2TM1[i,j]) == 1)
		{
		dosS2TM1nullValCount = dosS2TM1nullValCount + 1;
		}
		if (IsNull(RADS2TM2[i,j]) == 1)
		{
		dosS2TM2nullValCount = dosS2TM2nullValCount + 1;
		}		 
		if (IsNull(RADS2TM3[i,j]) == 1)
		{
		dosS2TM3nullValCount = dosS2TM3nullValCount + 1;
		}		 
		if (IsNull(RADS2TM4[i,j]) == 1)
		{
		dosS2TM4nullValCount = dosS2TM4nullValCount + 1;
		}		 
		if (IsNull(RADS2TM5[i,j]) == 1)
		{
		dosS2TM5nullValCount = dosS2TM5nullValCount + 1;
		}		 
		if (IsNull(RADS2TM7[i,j]) == 1)
		{
		dosS2TM7nullValCount = dosS2TM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS2TM1realSize = (TMlins * TMcols) - dosS2TM1nullValCount;
numeric dosS2TM2realSize = (TMlins * TMcols) - dosS2TM2nullValCount;
numeric dosS2TM3realSize = (TMlins * TMcols) - dosS2TM3nullValCount;
numeric dosS2TM4realSize = (TMlins * TMcols) - dosS2TM4nullValCount;
numeric dosS2TM5realSize = (TMlins * TMcols) - dosS2TM5nullValCount;
numeric dosS2TM7realSize = (TMlins * TMcols) - dosS2TM7nullValCount;

array allValuesDosS2TM1[dosS2TM1realSize];
array allValuesDosS2TM2[dosS2TM2realSize];
array allValuesDosS2TM3[dosS2TM3realSize];
array allValuesDosS2TM4[dosS2TM4realSize];
array allValuesDosS2TM5[dosS2TM5realSize];
array allValuesDosS2TM7[dosS2TM7realSize];

numeric DNminCALCS2TM1 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM1realSize);
numeric DNminCALCS2TM2 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM2realSize);
numeric DNminCALCS2TM3 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM3realSize);
numeric DNminCALCS2TM4 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM4realSize);
numeric DNminCALCS2TM5 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM5realSize);
numeric DNminCALCS2TM7 = DNminCALC(Scene2Lin, Scene2Col, dosS2TM7realSize);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(RADS2TM1[i,j]) == 0)
		{
		  allValuesDosS2TM1[dosS2TM1realArrayCount] = RADS2TM1[i,j];		 
		  dosS2TM1realArrayCount = dosS2TM1realArrayCount + 1;
		}
		if (IsNull(RADS2TM2[i,j]) == 0)
		{
		  allValuesDosS2TM2[dosS2TM2realArrayCount] = RADS2TM2[i,j];		 
		  dosS2TM2realArrayCount = dosS2TM2realArrayCount + 1;
		}
		if (IsNull(RADS2TM3[i,j]) == 0)
		{
		  allValuesDosS2TM3[dosS2TM3realArrayCount] = RADS2TM3[i,j];		 
		  dosS2TM3realArrayCount = dosS2TM3realArrayCount + 1;
		}
		if (IsNull(RADS2TM4[i,j]) == 0)
		{
		  allValuesDosS2TM4[dosS2TM4realArrayCount] = RADS2TM4[i,j];		 
		  dosS2TM4realArrayCount = dosS2TM4realArrayCount + 1;
		}
		if (IsNull(RADS2TM5[i,j]) == 0)
		{
		  allValuesDosS2TM5[dosS2TM5realArrayCount] = RADS2TM5[i,j];		 
		  dosS2TM5realArrayCount = dosS2TM5realArrayCount + 1;
		}
		if (IsNull(RADS2TM7[i,j]) == 0)
		{
		  allValuesDosS2TM7[dosS2TM7realArrayCount] = RADS2TM7[i,j];		 
		  dosS2TM7realArrayCount = dosS2TM7realArrayCount + 1;
		}
    }
}

########################### Scene 2 - Dn1000 - Band 1

numeric lastDOSS2TM1 = dosS2TM1realSize;
numeric hDOSS2TM1 = 1;

while ( (hDOSS2TM1 * 3 + 1) < lastDOSS2TM1 - 1 )
{
    hDOSS2TM1 = 3 * hDOSS2TM1 + 1;
}

while ( hDOSS2TM1 > 0 )
{
    for i = hDOSS2TM1 - 1 to lastDOSS2TM1               # for each of the h sets of elements
    {
        numeric keyDOSS2TM1 = allValuesDosS2TM1[i];
        numeric jDOSS2TM1 = i;

        while (jDOSS2TM1 >= hDOSS2TM1 && allValuesDosS2TM1[jDOSS2TM1 - hDOSS2TM1] > keyDOSS2TM1)
        {
            allValuesDosS2TM1[jDOSS2TM1] = allValuesDosS2TM1[jDOSS2TM1 - hDOSS2TM1];
            jDOSS2TM1 = jDOSS2TM1 - hDOSS2TM1;
        }
        allValuesDosS2TM1[jDOSS2TM1] = keyDOSS2TM1;
    }
    hDOSS2TM1 = floor(hDOSS2TM1/3);
}

numeric minValueDosS2TM1 = allValuesDosS2TM1[1];
numeric dn1000S2TM1 = 10000;

numeric actualCountDosS2TM1 = 0;
for i = 1 to dosS2TM1realSize 
{
    if (allValuesDosS2TM1[i] == minValueDosS2TM1)
    {
        actualCountDosS2TM1 = actualCountDosS2TM1 + 1;
        if (actualCountDosS2TM1 >= DNminCALCS2TM1)
        {
            dn1000S2TM1 = minValueDosS2TM1;
            go to dn1000S2TM1jumper;
        }
    }
    else
    {
        minValueDosS2TM1 = allValuesDosS2TM1[i+1];
        actualCountDosS2TM1 = 0;
    }
}

dn1000S2TM1jumper:

if (dn1000S2TM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM1 = allValuesDosS2TM1[1];
}
if (dn1000S2TM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM1 = allValuesDosS2TM1[1];
}
	
printf("dn1000 Scene 2 TM1: %12f\n", dn1000S2TM1);

########################### Scene 2 - Dn1000 - Band 2

numeric lastDOSS2TM2 = dosS2TM1realSize;
numeric hDOSS2TM2 = 1;

while ( (hDOSS2TM2 * 3 + 1) < lastDOSS2TM2 - 1 )
{
    hDOSS2TM2 = 3 * hDOSS2TM2 + 1;
}

while ( hDOSS2TM2 > 0 )
{
    for i = hDOSS2TM2 - 1 to lastDOSS2TM2
    {
        numeric keyDOSS2TM2 = allValuesDosS2TM2[i];
        numeric jDOSS2TM2 = i;

        while (jDOSS2TM2 >= hDOSS2TM2 && allValuesDosS2TM2[jDOSS2TM2 - hDOSS2TM2] > keyDOSS2TM2)
        {
            allValuesDosS2TM2[jDOSS2TM2] = allValuesDosS2TM2[jDOSS2TM2 - hDOSS2TM2];
            jDOSS2TM2 = jDOSS2TM2 - hDOSS2TM2;
        }
        allValuesDosS2TM2[jDOSS2TM2] = keyDOSS2TM2;
    }
    hDOSS2TM2 = floor(hDOSS2TM2/3);
}

numeric minValueDosS2TM2 = allValuesDosS2TM2[1];
numeric dn1000S2TM2 = 10000;

numeric actualCountDosS2TM2 = 0;
for i = 1 to dosS2TM2realSize 
{
    if (allValuesDosS2TM2[i] == minValueDosS2TM2)
    {
        actualCountDosS2TM2 = actualCountDosS2TM2 + 1;
        if (actualCountDosS2TM2 >= DNminCALCS2TM2)
        {
            dn1000S2TM2 = minValueDosS2TM2;
            go to dn1000S2TM2jumper;
        }
    }
    else
    {
        minValueDosS2TM2 = allValuesDosS2TM2[i+1];
        actualCountDosS2TM2 = 0;
    }
}

dn1000S2TM2jumper:

if (dn1000S2TM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM2 = allValuesDosS2TM2[1];
}
if (dn1000S2TM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM2 = allValuesDosS2TM2[1];
}

printf("dn1000 Scene 2 TM2: %12f\n", dn1000S2TM2);

########################### Scene 2 - Dn1000 - Band 3

numeric lastDOSS2TM3 = dosS2TM3realSize;
numeric hDOSS2TM3 = 1;

while ( (hDOSS2TM3 * 3 + 1) < lastDOSS2TM3 - 1 )
{
    hDOSS2TM3 = 3 * hDOSS2TM3 + 1;
}

while ( hDOSS2TM3 > 0 )
{
    for i = hDOSS2TM3 - 1 to lastDOSS2TM3               # for each of the h sets of elements
    {
        numeric keyDOSS2TM3 = allValuesDosS2TM3[i];
        numeric jDOSS2TM3 = i;

        while (jDOSS2TM3 >= hDOSS2TM3 && allValuesDosS2TM3[jDOSS2TM3 - hDOSS2TM3] > keyDOSS2TM3)
        {
            allValuesDosS2TM3[jDOSS2TM3] = allValuesDosS2TM3[jDOSS2TM3 - hDOSS2TM3];
            jDOSS2TM3 = jDOSS2TM3 - hDOSS2TM3;
        }
        allValuesDosS2TM3[jDOSS2TM3] = keyDOSS2TM3;
    }
    hDOSS2TM3 = floor(hDOSS2TM3/3);
}

numeric minValueDosS2TM3 = allValuesDosS2TM3[1];
numeric dn1000S2TM3 = 10000;

numeric actualCountDosS2TM3 = 0;
for i = 1 to dosS2TM3realSize 
{
    if (allValuesDosS2TM3[i] == minValueDosS2TM3)
    {
        actualCountDosS2TM3 = actualCountDosS2TM3 + 1;
        if (actualCountDosS2TM3 >= DNminCALCS2TM3)
        {
            dn1000S2TM3 = minValueDosS2TM3;
            go to dn1000S2TM3jumper;
        }
    }
    else
    {
        minValueDosS2TM3 = allValuesDosS2TM3[i+1];
        actualCountDosS2TM3 = 0;
    }
}

dn1000S2TM3jumper:

if (dn1000S2TM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM3 = allValuesDosS2TM3[1];
}
if (dn1000S2TM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM3 = allValuesDosS2TM3[1];
}

printf("dn1000 Scene 2 TM3: %12f\n", dn1000S2TM3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS2TM4 = dosS2TM4realSize;
numeric hDOSS2TM4 = 1;

while ( (hDOSS2TM4 * 3 + 1) < lastDOSS2TM4 - 1 )
{
    hDOSS2TM4 = 3 * hDOSS2TM4 + 1;
}

while ( hDOSS2TM4 > 0 )
{
    for i = hDOSS2TM4 - 1 to lastDOSS2TM4
    {
        numeric keyDOSS2TM4 = allValuesDosS2TM4[i];
        numeric jDOSS2TM4 = i;

        while (jDOSS2TM4 >= hDOSS2TM4 && allValuesDosS2TM4[jDOSS2TM4 - hDOSS2TM4] > keyDOSS2TM4)
        {
            allValuesDosS2TM4[jDOSS2TM4] = allValuesDosS2TM4[jDOSS2TM4 - hDOSS2TM4];
            jDOSS2TM4 = jDOSS2TM4 - hDOSS2TM4;
        }
        allValuesDosS2TM4[jDOSS2TM4] = keyDOSS2TM4;
    }
    hDOSS2TM4 = floor(hDOSS2TM4/3);
}

numeric minValueDosS2TM4 = allValuesDosS2TM4[1];
numeric dn1000S2TM4 = 10000;

numeric actualCountDosS2TM4 = 0;
for i = 1 to dosS2TM4realSize 
{
    if (allValuesDosS2TM4[i] == minValueDosS2TM4)
    {
        actualCountDosS2TM4 = actualCountDosS2TM4 + 1;
        if (actualCountDosS2TM4 >= DNminCALCS2TM4)
        {
            dn1000S2TM4 = minValueDosS2TM4;
            go to dn1000S2TM4jumper;
        }
    }
    else
    {
        minValueDosS2TM4 = allValuesDosS2TM4[i+1];
        actualCountDosS2TM4 = 0;
    }
}

dn1000S2TM4jumper:

if (dn1000S2TM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM4 = allValuesDosS2TM4[1];
}
if (dn1000S2TM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM4 = allValuesDosS2TM4[1];
}
	
printf("dn1000 Scene 2 TM4: %12f\n", dn1000S2TM4);

########################### Scene 2 - Dn1000 - Band 5

numeric lastDOSS2TM5 = dosS2TM5realSize;
numeric hDOSS2TM5 = 1;

while ( (hDOSS2TM5 * 3 + 1) < lastDOSS2TM5 - 1 )
{
    hDOSS2TM5 = 3 * hDOSS2TM5 + 1;
}

while ( hDOSS2TM5 > 0 )
{
    for i = hDOSS2TM5 - 1 to lastDOSS2TM5               # for each of the h sets of elements
    {
        numeric keyDOSS2TM5 = allValuesDosS2TM5[i];
        numeric jDOSS2TM5 = i;

        while (jDOSS2TM5 >= hDOSS2TM5 && allValuesDosS2TM5[jDOSS2TM5 - hDOSS2TM5] > keyDOSS2TM5)
        {
            allValuesDosS2TM5[jDOSS2TM5] = allValuesDosS2TM5[jDOSS2TM5 - hDOSS2TM5];
            jDOSS2TM5 = jDOSS2TM5 - hDOSS2TM5;
        }
        allValuesDosS2TM5[jDOSS2TM5] = keyDOSS2TM5;
    }
    hDOSS2TM5 = floor(hDOSS2TM5/3);
}

numeric minValueDosS2TM5 = allValuesDosS2TM5[1];
numeric dn1000S2TM5 = 10000;

numeric actualCountDosS2TM5 = 0;
for i = 1 to dosS2TM5realSize 
{
    if (allValuesDosS2TM5[i] == minValueDosS2TM5)
    {
        actualCountDosS2TM5 = actualCountDosS2TM5 + 1;
        if (actualCountDosS2TM5 >= DNminCALCS2TM5)
        {
            dn1000S2TM5 = minValueDosS2TM5;
            go to dn1000S2TM5jumper;
        }
    }
    else
    {
        minValueDosS2TM5 = allValuesDosS2TM5[i+1];
        actualCountDosS2TM5 = 0;
    }
}

dn1000S2TM5jumper:

if (dn1000S2TM5  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM5  = allValuesDosS2TM5[1];
}
if (dn1000S2TM5  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM5  = allValuesDosS2TM5[1];
}
	
printf("dn1000 Scene 2 TM5: %12f\n", dn1000S2TM5);

########################### Scene 2 - Dn1000 - Band 7

numeric lastDOSS2TM7 = dosS2TM7realSize;
numeric hDOSS2TM7 = 1;

while ( (hDOSS2TM7 * 3 + 1) < lastDOSS2TM7 - 1 )
{
    hDOSS2TM7 = 3 * hDOSS2TM7 + 1;
}

while ( hDOSS2TM7 > 0 )
{
    for i = hDOSS2TM7 - 1 to lastDOSS2TM7               # for each of the h sets of elements
    {
        numeric keyDOSS2TM7 = allValuesDosS2TM7[i];
        numeric jDOSS2TM7 = i;

        while (jDOSS2TM7 >= hDOSS2TM7 && allValuesDosS2TM7[jDOSS2TM7 - hDOSS2TM7] > keyDOSS2TM7)
        {
            allValuesDosS2TM7[jDOSS2TM7] = allValuesDosS2TM7[jDOSS2TM7 - hDOSS2TM7];
            jDOSS2TM7 = jDOSS2TM7 - hDOSS2TM7;
        }
        allValuesDosS2TM7[jDOSS2TM7] = keyDOSS2TM7;
    }
    hDOSS2TM7 = floor(hDOSS2TM7/3);
}

numeric minValueDosS2TM7 = allValuesDosS2TM7[1];
numeric dn1000S2TM7 = 10000;

numeric actualCountDosS2TM7 = 0;
for i = 1 to dosS2TM7realSize 
{
    if (allValuesDosS2TM7[i] == minValueDosS2TM7)
    {
        actualCountDosS2TM7 = actualCountDosS2TM7 + 1;
        if (actualCountDosS2TM7 >= DNminCALCS2TM7)
        {
            dn1000S2TM7 = minValueDosS2TM7;
            go to dn1000S2TM7jumper;
        }
    }
    else
    {
        minValueDosS2TM7 = allValuesDosS2TM7[i+1];
        actualCountDosS2TM7 = 0;
    }
}

dn1000S2TM7jumper:

if (dn1000S2TM7  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2TM7  = allValuesDosS2TM7[1];
}
if (dn1000S2TM7  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2TM7  = allValuesDosS2TM7[1];
}
	
printf("dn1000 Scene 2 TM7: %12f\n\n", dn1000S2TM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS2TM1 = dn1000S2TM1 - (0.01 * (radsurfTMS2b1 * cos(s2zenith)) / pi);
numeric pathRadS2TM2 = dn1000S2TM2 - (0.01 * (radsurfTMS2b2 * cos(s2zenith)) / pi);
numeric pathRadS2TM3 = dn1000S2TM3 - (0.01 * (radsurfTMS2b3 * cos(s2zenith)) / pi);
numeric pathRadS2TM4 = dn1000S2TM4 - (0.01 * (radsurfTMS2b4 * cos(s2zenith)) / pi);
numeric pathRadS2TM5 = dn1000S2TM5 - (0.01 * (radsurfTMS2b5 * cos(s2zenith)) / pi);
numeric pathRadS2TM7 = dn1000S2TM7 - (0.01 * (radsurfTMS2b7 * cos(s2zenith)) / pi);

printf("Pathradiance Scene 2 Band 1: %12f\n", pathRadS2TM1);
printf("Pathradiance Scene 2 Band 2: %12f\n", pathRadS2TM2);
printf("Pathradiance Scene 2 Band 3: %12f\n", pathRadS2TM3);
printf("Pathradiance Scene 2 Band 4: %12f\n", pathRadS2TM4);
printf("Pathradiance Scene 2 Band 5: %12f\n", pathRadS2TM5);
printf("Pathradiance Scene 2 Band 7: %12f\n\n", pathRadS2TM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to TMlins
	 {
	        for j = 1 to TMcols
	        {
                  valueS2TM1 = (pi * (RADS2TM1[i,j] - pathRadS2TM1)) / (radsurfTMS2b1 * cos(s2zenith));

                  if ( valueS2TM1 < 0 )
       		{
          			valueS2TM1 = 0;
				countS2REFnullTM1 = countS2REFnullTM1 + 1;
       		}

			else if ( valueS2TM1 > 1 )
			{
				valueS2TM1 = 1;
				countS2REFoneTM1 = countS2REFoneTM1 + 1;
			}
       		
			REFS2TM1[i,j] = valueS2TM1;

               valueS2TM2 = (pi * (RADS2TM2[i,j] - pathRadS2TM2)) / (radsurfTMS2b2 * cos(s2zenith));

                  if ( valueS2TM2 < 0 )
       		{
          			valueS2TM2 = 0;
				countS2REFnullTM2 = countS2REFnullTM2 + 1;
       		}

			else if ( valueS2TM2 > 1 )
			{
				valueS2TM2 = 1;
				countS2REFoneTM2 = countS2REFoneTM2 + 1;
			}
       		
			REFS2TM2[i,j] = valueS2TM2;

               valueS2TM3 = (pi * (RADS2TM3[i,j] - pathRadS2TM3)) / (radsurfTMS2b3 * cos(s2zenith));

                  if ( valueS2TM3 < 0 )
       		{
          			valueS2TM3 = 0;
				countS2REFnullTM3 = countS2REFnullTM3 + 1;
       		}

			else if ( valueS2TM3 > 1 )
			{
				valueS2TM3 = 1;
				countS2REFoneTM3 = countS2REFoneTM3 + 1;
			}
       		
			REFS2TM3[i,j] = valueS2TM3;

               valueS2TM4 = (pi * (RADS2TM4[i,j] - pathRadS2TM4)) / (radsurfTMS2b4 * cos(s2zenith));

                  if ( valueS2TM4 < 0 )
       		{
          			valueS2TM4 = 0;
				countS2REFnullTM4 = countS2REFnullTM4 + 1;
       		}

			else if ( valueS2TM4 > 1 )
			{
				valueS2TM4 = 1;
				countS2REFoneTM4 = countS2REFoneTM4 + 1;
			}
       		
			REFS2TM4[i,j] = valueS2TM4;

               valueS2TM5 = (pi * (RADS2TM5[i,j] - pathRadS2TM5)) / (radsurfTMS2b5 * cos(s2zenith));

                  if ( valueS2TM5 < 0 )
       		{
          			valueS2TM5 = 0;
				countS2REFnullTM5 = countS2REFnullTM5 + 1;
       		}

			else if ( valueS2TM5 > 1 )
			{
				valueS2TM5 = 1;
				countS2REFoneTM5 = countS2REFoneTM5 + 1;
			}
       		
			REFS2TM5[i,j] = valueS2TM5;

               valueS2TM7 = (pi * (RADS2TM7[i,j] - pathRadS2TM7)) / (radsurfTMS2b7 * cos(s2zenith));

                  if ( valueS2TM7 < 0 )
       		{
          			valueS2TM7 = 0;
				countS2REFnullTM7 = countS2REFnullTM7 + 1;
       		}

			else if ( valueS2TM7 > 1 )
			{
				valueS2TM7 = 1;
				countS2REFoneTM7 = countS2REFoneTM7 + 1;
			}
       		
			REFS2TM7[i,j] = valueS2TM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullTM1, countS2REFoneTM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullTM2, countS2REFoneTM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullTM3, countS2REFoneTM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullTM4, countS2REFoneTM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullTM5, countS2REFoneTM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS2REFnullTM7, countS2REFoneTM7);

CreatePyramid(REFS2TM1);
CreatePyramid(REFS2TM2);
CreatePyramid(REFS2TM3);
CreatePyramid(REFS2TM4);
CreatePyramid(REFS2TM5);
CreatePyramid(REFS2TM7);
        
CreateHistogram(REFS2TM1);
CreateHistogram(REFS2TM2);
CreateHistogram(REFS2TM3);
CreateHistogram(REFS2TM4);
CreateHistogram(REFS2TM5);
CreateHistogram(REFS2TM7);

CloseRaster(RADS2TM1);
CloseRaster(RADS2TM2);
CloseRaster(RADS2TM3);
CloseRaster(RADS2TM4);
CloseRaster(RADS2TM5);
CloseRaster(RADS2TM7);

printf("Reflectance calculation for Scene 2 (TM) is done...\n\n\n");
}

else if ( sensors2 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
raster REFS2MSS1, REFS2MSS2, REFS2MSS3, REFS2MSS4;
numeric radsurfMSS2b1, radsurfMSS2b2, radsurfMSS2b3, radsurfMSS2b4;
numeric valueS2MSS1, valueS2MSS2, valueS2MSS3, valueS2MSS4;

numeric dosS2MSS1nullValCount = 0;
numeric dosS2MSS2nullValCount = 0;
numeric dosS2MSS3nullValCount = 0;
numeric dosS2MSS4nullValCount = 0;

numeric dosS2MSS1realArrayCount = 1;
numeric dosS2MSS2realArrayCount = 1;
numeric dosS2MSS3realArrayCount = 1;
numeric dosS2MSS4realArrayCount = 1;

numeric countS2REFnullMSS1 = 0;
numeric countS2REFnullMSS2 = 0;
numeric countS2REFnullMSS3 = 0;
numeric countS2REFnullMSS4 = 0;

numeric countS2REFoneMSS1 = 0;
numeric countS2REFoneMSS2 = 0;
numeric countS2REFoneMSS3 = 0;
numeric countS2REFoneMSS4 = 0;

radsurfMSS2b1 = ESMSS51 / (dist2^2);
radsurfMSS2b2 = ESMSS52 / (dist2^2);
radsurfMSS2b3 = ESMSS53 / (dist2^2);
radsurfMSS2b4 = ESMSS54 / (dist2^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 2): %12f\n\n", radsurfMSS2b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS2MSS1[i,j]) == 1)
		{
		dosS2MSS1nullValCount = dosS2MSS1nullValCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 1)
		{
		dosS2MSS2nullValCount = dosS2MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS3[i,j]) == 1)
		{
		dosS2MSS3nullValCount = dosS2MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS4[i,j]) == 1)
		{
		dosS2MSS4nullValCount = dosS2MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS2MSS1realSize = (MSSlins * MSScols) - dosS2MSS1nullValCount;
numeric dosS2MSS2realSize = (MSSlins * MSScols) - dosS2MSS2nullValCount;
numeric dosS2MSS3realSize = (MSSlins * MSScols) - dosS2MSS3nullValCount;
numeric dosS2MSS4realSize = (MSSlins * MSScols) - dosS2MSS4nullValCount;

array allValuesDosS2MSS1[dosS2MSS1realSize];
array allValuesDosS2MSS2[dosS2MSS2realSize];
array allValuesDosS2MSS3[dosS2MSS3realSize];
array allValuesDosS2MSS4[dosS2MSS4realSize];

numeric DNminCALCS2MSS1 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS1realSize);
numeric DNminCALCS2MSS2 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS2realSize);
numeric DNminCALCS2MSS3 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS3realSize);
numeric DNminCALCS2MSS4 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS2MSS1[i,j]) == 0)
		{
		  allValuesDosS2MSS1[dosS2MSS1realArrayCount] = RADS2MSS1[i,j];		 
		  dosS2MSS1realArrayCount = dosS2MSS1realArrayCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 0)
		{
		  allValuesDosS2MSS2[dosS2MSS2realArrayCount] = RADS2MSS2[i,j];		 
		  dosS2MSS2realArrayCount = dosS2MSS2realArrayCount + 1;
		}
		if (IsNull(RADS2MSS3[i,j]) == 0)
		{
		  allValuesDosS2MSS3[dosS2MSS3realArrayCount] = RADS2MSS3[i,j];		 
		  dosS2MSS3realArrayCount = dosS2MSS3realArrayCount + 1;
		}
		if (IsNull(RADS2MSS4[i,j]) == 0)
		{
		  allValuesDosS2MSS4[dosS2MSS4realArrayCount] = RADS2MSS4[i,j];		 
		  dosS2MSS4realArrayCount = dosS2MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 2 - Dn1000 - Band 1

numeric lastDOSS2MSS1 = dosS2MSS1realSize;
numeric hDOSS2MSS1 = 1;

while ( (hDOSS2MSS1 * 3 + 1) < lastDOSS2MSS1 - 1 )
{
    hDOSS2MSS1 = 3 * hDOSS2MSS1 + 1;
}

while ( hDOSS2MSS1 > 0 )
{
    for i = hDOSS2MSS1 - 1 to lastDOSS2MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS1 = allValuesDosS2MSS1[i];
        numeric jDOSS2MSS1 = i;

        while (jDOSS2MSS1 >= hDOSS2MSS1 && allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1] > keyDOSS2MSS1)
        {
            allValuesDosS2MSS1[jDOSS2MSS1] = allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1];
            jDOSS2MSS1 = jDOSS2MSS1 - hDOSS2MSS1;
        }
        allValuesDosS2MSS1[jDOSS2MSS1] = keyDOSS2MSS1;
    }
    hDOSS2MSS1 = floor(hDOSS2MSS1/3);
}

numeric minValueDosS2MSS1 = allValuesDosS2MSS1[1];
numeric dn1000S2MSS1 = 10000;

numeric actualCountDosS2MSS1 = 0;
for i = 1 to dosS2MSS1realSize 
{
    if (allValuesDosS2MSS1[i] == minValueDosS2MSS1)
    {
        actualCountDosS2MSS1 = actualCountDosS2MSS1 + 1;
        if (actualCountDosS2MSS1 >= DNminCALCS2MSS1)
        {
            dn1000S2MSS1 = minValueDosS2MSS1;
            go to dn1000S2MSS1jumper;
        }
    }
    else
    {
        minValueDosS2MSS1 = allValuesDosS2MSS1[i+1];
        actualCountDosS2MSS1 = 0;
    }
}

dn1000S2MSS1jumper:

if (dn1000S2MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
if (dn1000S2MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
	
printf("dn1000 Scene 2 MSS1: %12f\n", dn1000S2MSS1);

########################### Scene 2 - Dn1000 - Band 2

numeric lastDOSS2MSS2 = dosS2MSS1realSize;
numeric hDOSS2MSS2 = 1;

while ( (hDOSS2MSS2 * 3 + 1) < lastDOSS2MSS2 - 1 )
{
    hDOSS2MSS2 = 3 * hDOSS2MSS2 + 1;
}

while ( hDOSS2MSS2 > 0 )
{
    for i = hDOSS2MSS2 - 1 to lastDOSS2MSS2
    {
        numeric keyDOSS2MSS2 = allValuesDosS2MSS2[i];
        numeric jDOSS2MSS2 = i;

        while (jDOSS2MSS2 >= hDOSS2MSS2 && allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2] > keyDOSS2MSS2)
        {
            allValuesDosS2MSS2[jDOSS2MSS2] = allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2];
            jDOSS2MSS2 = jDOSS2MSS2 - hDOSS2MSS2;
        }
        allValuesDosS2MSS2[jDOSS2MSS2] = keyDOSS2MSS2;
    }
    hDOSS2MSS2 = floor(hDOSS2MSS2/3);
}

numeric minValueDosS2MSS2 = allValuesDosS2MSS2[1];
numeric dn1000S2MSS2 = 10000;

numeric actualCountDosS2MSS2 = 0;
for i = 1 to dosS2MSS2realSize 
{
    if (allValuesDosS2MSS2[i] == minValueDosS2MSS2)
    {
        actualCountDosS2MSS2 = actualCountDosS2MSS2 + 1;
        if (actualCountDosS2MSS2 >= DNminCALCS2MSS2)
        {
            dn1000S2MSS2 = minValueDosS2MSS2;
            go to dn1000S2MSS2jumper;
        }
    }
    else
    {
        minValueDosS2MSS2 = allValuesDosS2MSS2[i+1];
        actualCountDosS2MSS2 = 0;
    }
}

dn1000S2MSS2jumper:

if (dn1000S2MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}
if (dn1000S2MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}

printf("dn1000 Scene 2 MSS2: %12f\n", dn1000S2MSS2);

########################### Scene 2 - Dn1000 - Band 3

numeric lastDOSS2MSS3 = dosS2MSS3realSize;
numeric hDOSS2MSS3 = 1;

while ( (hDOSS2MSS3 * 3 + 1) < lastDOSS2MSS3 - 1 )
{
    hDOSS2MSS3 = 3 * hDOSS2MSS3 + 1;
}

while ( hDOSS2MSS3 > 0 )
{
    for i = hDOSS2MSS3 - 1 to lastDOSS2MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS3 = allValuesDosS2MSS3[i];
        numeric jDOSS2MSS3 = i;

        while (jDOSS2MSS3 >= hDOSS2MSS3 && allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3] > keyDOSS2MSS3)
        {
            allValuesDosS2MSS3[jDOSS2MSS3] = allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3];
            jDOSS2MSS3 = jDOSS2MSS3 - hDOSS2MSS3;
        }
        allValuesDosS2MSS3[jDOSS2MSS3] = keyDOSS2MSS3;
    }
    hDOSS2MSS3 = floor(hDOSS2MSS3/3);
}

numeric minValueDosS2MSS3 = allValuesDosS2MSS3[1];
numeric dn1000S2MSS3 = 10000;

numeric actualCountDosS2MSS3 = 0;
for i = 1 to dosS2MSS3realSize 
{
    if (allValuesDosS2MSS3[i] == minValueDosS2MSS3)
    {
        actualCountDosS2MSS3 = actualCountDosS2MSS3 + 1;
        if (actualCountDosS2MSS3 >= DNminCALCS2MSS3)
        {
            dn1000S2MSS3 = minValueDosS2MSS3;
            go to dn1000S2MSS3jumper;
        }
    }
    else
    {
        minValueDosS2MSS3 = allValuesDosS2MSS3[i+1];
        actualCountDosS2MSS3 = 0;
    }
}

dn1000S2MSS3jumper:

if (dn1000S2MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}
if (dn1000S2MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}

printf("dn1000 Scene 2 MSS3: %12f\n", dn1000S2MSS3);

########################### Scene 2 - Dn1000 - Band 4

numeric lastDOSS2MSS4 = dosS2MSS4realSize;
numeric hDOSS2MSS4 = 1;

while ( (hDOSS2MSS4 * 3 + 1) < lastDOSS2MSS4 - 1 )
{
    hDOSS2MSS4 = 3 * hDOSS2MSS4 + 1;
}

while ( hDOSS2MSS4 > 0 )
{
    for i = hDOSS2MSS4 - 1 to lastDOSS2MSS4
    {
        numeric keyDOSS2MSS4 = allValuesDosS2MSS4[i];
        numeric jDOSS2MSS4 = i;

        while (jDOSS2MSS4 >= hDOSS2MSS4 && allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4] > keyDOSS2MSS4)
        {
            allValuesDosS2MSS4[jDOSS2MSS4] = allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4];
            jDOSS2MSS4 = jDOSS2MSS4 - hDOSS2MSS4;
        }
        allValuesDosS2MSS4[jDOSS2MSS4] = keyDOSS2MSS4;
    }
    hDOSS2MSS4 = floor(hDOSS2MSS4/3);
}

numeric minValueDosS2MSS4 = allValuesDosS2MSS4[1];
numeric dn1000S2MSS4 = 10000;

numeric actualCountDosS2MSS4 = 0;
for i = 1 to dosS2MSS4realSize 
{
    if (allValuesDosS2MSS4[i] == minValueDosS2MSS4)
    {
        actualCountDosS2MSS4 = actualCountDosS2MSS4 + 1;
        if (actualCountDosS2MSS4 >= DNminCALCS2MSS4)
        {
            dn1000S2MSS4 = minValueDosS2MSS4;
            go to dn1000S2MSS4jumper;
        }
    }
    else
    {
        minValueDosS2MSS4 = allValuesDosS2MSS4[i+1];
        actualCountDosS2MSS4 = 0;
    }
}

dn1000S2MSS4jumper:

if (dn1000S2MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
if (dn1000S2MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
	
printf("dn1000 Scene 2 MSS4: %12f\n", dn1000S2MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS2MSS1 = dn1000S2MSS1 - (0.01 * (radsurfMSS2b1 * cos(s2zenith)) / pi);
numeric pathRadS2MSS2 = dn1000S2MSS2 - (0.01 * (radsurfMSS2b2 * cos(s2zenith)) / pi);
numeric pathRadS2MSS3 = dn1000S2MSS3 - (0.01 * (radsurfMSS2b3 * cos(s2zenith)) / pi);
numeric pathRadS2MSS4 = dn1000S2MSS4 - (0.01 * (radsurfMSS2b4 * cos(s2zenith)) / pi);

printf("Pathradiance Scene 2 Band 1: %12f\n", pathRadS2MSS1);
printf("Pathradiance Scene 2 Band 2: %12f\n", pathRadS2MSS2);
printf("Pathradiance Scene 2 Band 3: %12f\n", pathRadS2MSS3);
printf("Pathradiance Scene 2 Band 4: %12f\n\n", pathRadS2MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS2MSS1 = (pi * (RADS2MSS1[i,j] - pathRadS2MSS1)) / (radsurfMSS2b1 * cos(s2zenith));

                  if ( valueS2MSS1 < 0 )
       		{
          			valueS2MSS1 = 0;
				countS2REFnullMSS1 = countS2REFnullMSS1 + 1;
       		}

			else if ( valueS2MSS1 > 1 )
			{
				valueS2MSS1 = 1;
				countS2REFoneMSS1 = countS2REFoneMSS1 + 1;
			}
       		
			REFS2MSS1[i,j] = valueS2MSS1;

               valueS2MSS2 = (pi * (RADS2MSS2[i,j] - pathRadS2MSS2)) / (radsurfMSS2b2 * cos(s2zenith));

                  if ( valueS2MSS2 < 0 )
       		{
          			valueS2MSS2 = 0;
				countS2REFnullMSS2 = countS2REFnullMSS2 + 1;
       		}

			else if ( valueS2MSS2 > 1 )
			{
				valueS2MSS2 = 1;
				countS2REFoneMSS2 = countS2REFoneMSS2 + 1;
			}
       		
			REFS2MSS2[i,j] = valueS2MSS2;

               valueS2MSS3 = (pi * (RADS2MSS3[i,j] - pathRadS2MSS3)) / (radsurfMSS2b3 * cos(s2zenith));

                  if ( valueS2MSS3 < 0 )
       		{
          			valueS2MSS3 = 0;
				countS2REFnullMSS3 = countS2REFnullMSS3 + 1;
       		}

			else if ( valueS2MSS3 > 1 )
			{
				valueS2MSS3 = 1;
				countS2REFoneMSS3 = countS2REFoneMSS3 + 1;
			}
       		
			REFS2MSS3[i,j] = valueS2MSS3;

               valueS2MSS4 = (pi * (RADS2MSS4[i,j] - pathRadS2MSS4)) / (radsurfMSS2b4 * cos(s2zenith));

                  if ( valueS2MSS4 < 0 )
       		{
          			valueS2MSS4 = 0;
				countS2REFnullMSS4 = countS2REFnullMSS4 + 1;
       		}

			else if ( valueS2MSS4 > 1 )
			{
				valueS2MSS4 = 1;
				countS2REFoneMSS4 = countS2REFoneMSS4 + 1;
			}
       		
			REFS2MSS4[i,j] = valueS2MSS4;
           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS1, countS2REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS2, countS2REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS3, countS2REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS4, countS2REFoneMSS4);

CreatePyramid(REFS2MSS1);
CreatePyramid(REFS2MSS2);
CreatePyramid(REFS2MSS3);
CreatePyramid(REFS2MSS4);
        
CreateHistogram(REFS2MSS1);
CreateHistogram(REFS2MSS2);
CreateHistogram(REFS2MSS3);
CreateHistogram(REFS2MSS4);

CloseRaster(RADS2MSS1);
CloseRaster(RADS2MSS2);
CloseRaster(RADS2MSS3);
CloseRaster(RADS2MSS4);

printf("Reflectance calculation for Scene 2 (MSS) is done...\n\n\n");
}

else if ( sensors2 == 4 )                                               # MSS 4
{
raster REFS2MSS1, REFS2MSS2, REFS2MSS3, REFS2MSS4;
numeric radsurfMSS2b1, radsurfMSS2b2, radsurfMSS2b3, radsurfMSS2b4;
numeric valueS2MSS1, valueS2MSS2, valueS2MSS3, valueS2MSS4;

numeric dosS2MSS1nullValCount = 0;
numeric dosS2MSS2nullValCount = 0;
numeric dosS2MSS3nullValCount = 0;
numeric dosS2MSS4nullValCount = 0;

numeric dosS2MSS1realArrayCount = 1;
numeric dosS2MSS2realArrayCount = 1;
numeric dosS2MSS3realArrayCount = 1;
numeric dosS2MSS4realArrayCount = 1;

numeric countS2REFnullMSS1 = 0;
numeric countS2REFnullMSS2 = 0;
numeric countS2REFnullMSS3 = 0;
numeric countS2REFnullMSS4 = 0;

numeric countS2REFoneMSS1 = 0;
numeric countS2REFoneMSS2 = 0;
numeric countS2REFoneMSS3 = 0;
numeric countS2REFoneMSS4 = 0;

radsurfMSS2b1 = ESMSS41 / (dist2^2);
radsurfMSS2b2 = ESMSS42 / (dist2^2);
radsurfMSS2b3 = ESMSS43 / (dist2^2);
radsurfMSS2b4 = ESMSS44 / (dist2^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 2): %12f\n\n", radsurfMSS2b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS2MSS1[i,j]) == 1)
		{
		dosS2MSS1nullValCount = dosS2MSS1nullValCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 1)
		{
		dosS2MSS2nullValCount = dosS2MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS3[i,j]) == 1)
		{
		dosS2MSS3nullValCount = dosS2MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS4[i,j]) == 1)
		{
		dosS2MSS4nullValCount = dosS2MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS2MSS1realSize = (MSSlins * MSScols) - dosS2MSS1nullValCount;
numeric dosS2MSS2realSize = (MSSlins * MSScols) - dosS2MSS2nullValCount;
numeric dosS2MSS3realSize = (MSSlins * MSScols) - dosS2MSS3nullValCount;
numeric dosS2MSS4realSize = (MSSlins * MSScols) - dosS2MSS4nullValCount;

array allValuesDosS2MSS1[dosS2MSS1realSize];
array allValuesDosS2MSS2[dosS2MSS2realSize];
array allValuesDosS2MSS3[dosS2MSS3realSize];
array allValuesDosS2MSS4[dosS2MSS4realSize];

numeric DNminCALCS2MSS1 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS1realSize);
numeric DNminCALCS2MSS2 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS2realSize);
numeric DNminCALCS2MSS3 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS3realSize);
numeric DNminCALCS2MSS4 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS2MSS1[i,j]) == 0)
		{
		  allValuesDosS2MSS1[dosS2MSS1realArrayCount] = RADS2MSS1[i,j];		 
		  dosS2MSS1realArrayCount = dosS2MSS1realArrayCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 0)
		{
		  allValuesDosS2MSS2[dosS2MSS2realArrayCount] = RADS2MSS2[i,j];		 
		  dosS2MSS2realArrayCount = dosS2MSS2realArrayCount + 1;
		}
		if (IsNull(RADS2MSS3[i,j]) == 0)
		{
		  allValuesDosS2MSS3[dosS2MSS3realArrayCount] = RADS2MSS3[i,j];		 
		  dosS2MSS3realArrayCount = dosS2MSS3realArrayCount + 1;
		}
		if (IsNull(RADS2MSS4[i,j]) == 0)
		{
		  allValuesDosS2MSS4[dosS2MSS4realArrayCount] = RADS2MSS4[i,j];		 
		  dosS2MSS4realArrayCount = dosS2MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 2 - Dn1000 - Band 1

numeric lastDOSS2MSS1 = dosS2MSS1realSize;
numeric hDOSS2MSS1 = 1;

while ( (hDOSS2MSS1 * 3 + 1) < lastDOSS2MSS1 - 1 )
{
    hDOSS2MSS1 = 3 * hDOSS2MSS1 + 1;
}

while ( hDOSS2MSS1 > 0 )
{
    for i = hDOSS2MSS1 - 1 to lastDOSS2MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS1 = allValuesDosS2MSS1[i];
        numeric jDOSS2MSS1 = i;

        while (jDOSS2MSS1 >= hDOSS2MSS1 && allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1] > keyDOSS2MSS1)
        {
            allValuesDosS2MSS1[jDOSS2MSS1] = allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1];
            jDOSS2MSS1 = jDOSS2MSS1 - hDOSS2MSS1;
        }
        allValuesDosS2MSS1[jDOSS2MSS1] = keyDOSS2MSS1;
    }
    hDOSS2MSS1 = floor(hDOSS2MSS1/3);
}

numeric minValueDosS2MSS1 = allValuesDosS2MSS1[1];
numeric dn1000S2MSS1 = 10000;

numeric actualCountDosS2MSS1 = 0;
for i = 1 to dosS2MSS1realSize 
{
    if (allValuesDosS2MSS1[i] == minValueDosS2MSS1)
    {
        actualCountDosS2MSS1 = actualCountDosS2MSS1 + 1;
        if (actualCountDosS2MSS1 >= DNminCALCS2MSS1)
        {
            dn1000S2MSS1 = minValueDosS2MSS1;
            go to dn1000S2MSS1jumper;
        }
    }
    else
    {
        minValueDosS2MSS1 = allValuesDosS2MSS1[i+1];
        actualCountDosS2MSS1 = 0;
    }
}

dn1000S2MSS1jumper:

if (dn1000S2MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
if (dn1000S2MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
	
printf("dn1000 Scene 2 MSS1: %12f\n", dn1000S2MSS1);

########################### Scene 2 - Dn1000 - Band 2

numeric lastDOSS2MSS2 = dosS2MSS1realSize;
numeric hDOSS2MSS2 = 1;

while ( (hDOSS2MSS2 * 3 + 1) < lastDOSS2MSS2 - 1 )
{
    hDOSS2MSS2 = 3 * hDOSS2MSS2 + 1;
}

while ( hDOSS2MSS2 > 0 )
{
    for i = hDOSS2MSS2 - 1 to lastDOSS2MSS2
    {
        numeric keyDOSS2MSS2 = allValuesDosS2MSS2[i];
        numeric jDOSS2MSS2 = i;

        while (jDOSS2MSS2 >= hDOSS2MSS2 && allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2] > keyDOSS2MSS2)
        {
            allValuesDosS2MSS2[jDOSS2MSS2] = allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2];
            jDOSS2MSS2 = jDOSS2MSS2 - hDOSS2MSS2;
        }
        allValuesDosS2MSS2[jDOSS2MSS2] = keyDOSS2MSS2;
    }
    hDOSS2MSS2 = floor(hDOSS2MSS2/3);
}

numeric minValueDosS2MSS2 = allValuesDosS2MSS2[1];
numeric dn1000S2MSS2 = 10000;

numeric actualCountDosS2MSS2 = 0;
for i = 1 to dosS2MSS2realSize 
{
    if (allValuesDosS2MSS2[i] == minValueDosS2MSS2)
    {
        actualCountDosS2MSS2 = actualCountDosS2MSS2 + 1;
        if (actualCountDosS2MSS2 >= DNminCALCS2MSS2)
        {
            dn1000S2MSS2 = minValueDosS2MSS2;
            go to dn1000S2MSS2jumper;
        }
    }
    else
    {
        minValueDosS2MSS2 = allValuesDosS2MSS2[i+1];
        actualCountDosS2MSS2 = 0;
    }
}

dn1000S2MSS2jumper:

if (dn1000S2MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}
if (dn1000S2MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}

printf("dn1000 Scene 2 MSS2: %12f\n", dn1000S2MSS2);

########################### Scene 2 - Dn1000 - Band 3

numeric lastDOSS2MSS3 = dosS2MSS3realSize;
numeric hDOSS2MSS3 = 1;

while ( (hDOSS2MSS3 * 3 + 1) < lastDOSS2MSS3 - 1 )
{
    hDOSS2MSS3 = 3 * hDOSS2MSS3 + 1;
}

while ( hDOSS2MSS3 > 0 )
{
    for i = hDOSS2MSS3 - 1 to lastDOSS2MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS3 = allValuesDosS2MSS3[i];
        numeric jDOSS2MSS3 = i;

        while (jDOSS2MSS3 >= hDOSS2MSS3 && allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3] > keyDOSS2MSS3)
        {
            allValuesDosS2MSS3[jDOSS2MSS3] = allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3];
            jDOSS2MSS3 = jDOSS2MSS3 - hDOSS2MSS3;
        }
        allValuesDosS2MSS3[jDOSS2MSS3] = keyDOSS2MSS3;
    }
    hDOSS2MSS3 = floor(hDOSS2MSS3/3);
}

numeric minValueDosS2MSS3 = allValuesDosS2MSS3[1];
numeric dn1000S2MSS3 = 10000;

numeric actualCountDosS2MSS3 = 0;
for i = 1 to dosS2MSS3realSize 
{
    if (allValuesDosS2MSS3[i] == minValueDosS2MSS3)
    {
        actualCountDosS2MSS3 = actualCountDosS2MSS3 + 1;
        if (actualCountDosS2MSS3 >= DNminCALCS2MSS3)
        {
            dn1000S2MSS3 = minValueDosS2MSS3;
            go to dn1000S2MSS3jumper;
        }
    }
    else
    {
        minValueDosS2MSS3 = allValuesDosS2MSS3[i+1];
        actualCountDosS2MSS3 = 0;
    }
}

dn1000S2MSS3jumper:

if (dn1000S2MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}
if (dn1000S2MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}
	
printf("dn1000 Scene 2 MSS3: %12f\n", dn1000S2MSS3);

########################### Scene 2 - Dn1000 - Band 4

numeric lastDOSS2MSS4 = dosS2MSS4realSize;
numeric hDOSS2MSS4 = 1;

while ( (hDOSS2MSS4 * 3 + 1) < lastDOSS2MSS4 - 1 )
{
    hDOSS2MSS4 = 3 * hDOSS2MSS4 + 1;
}

while ( hDOSS2MSS4 > 0 )
{
    for i = hDOSS2MSS4 - 1 to lastDOSS2MSS4
    {
        numeric keyDOSS2MSS4 = allValuesDosS2MSS4[i];
        numeric jDOSS2MSS4 = i;

        while (jDOSS2MSS4 >= hDOSS2MSS4 && allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4] > keyDOSS2MSS4)
        {
            allValuesDosS2MSS4[jDOSS2MSS4] = allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4];
            jDOSS2MSS4 = jDOSS2MSS4 - hDOSS2MSS4;
        }
        allValuesDosS2MSS4[jDOSS2MSS4] = keyDOSS2MSS4;
    }
    hDOSS2MSS4 = floor(hDOSS2MSS4/3);
}

numeric minValueDosS2MSS4 = allValuesDosS2MSS4[1];
numeric dn1000S2MSS4 = 10000;

numeric actualCountDosS2MSS4 = 0;
for i = 1 to dosS2MSS4realSize 
{
    if (allValuesDosS2MSS4[i] == minValueDosS2MSS4)
    {
        actualCountDosS2MSS4 = actualCountDosS2MSS4 + 1;
        if (actualCountDosS2MSS4 >= DNminCALCS2MSS4)
        {
            dn1000S2MSS4 = minValueDosS2MSS4;
            go to dn1000S2MSS4jumper;
        }
    }
    else
    {
        minValueDosS2MSS4 = allValuesDosS2MSS4[i+1];
        actualCountDosS2MSS4 = 0;
    }
}

dn1000S2MSS4jumper:

if (dn1000S2MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
if (dn1000S2MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
	
printf("dn1000 Scene 2 MSS4: %12f\n", dn1000S2MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS2MSS1 = dn1000S2MSS1 - (0.01 * (radsurfMSS2b1 * cos(s2zenith)) / pi);
numeric pathRadS2MSS2 = dn1000S2MSS2 - (0.01 * (radsurfMSS2b2 * cos(s2zenith)) / pi);
numeric pathRadS2MSS3 = dn1000S2MSS3 - (0.01 * (radsurfMSS2b3 * cos(s2zenith)) / pi);
numeric pathRadS2MSS4 = dn1000S2MSS4 - (0.01 * (radsurfMSS2b4 * cos(s2zenith)) / pi);

printf("Pathradiance Scene 2 Band 1: %12f\n", pathRadS2MSS1);
printf("Pathradiance Scene 2 Band 2: %12f\n", pathRadS2MSS2);
printf("Pathradiance Scene 2 Band 3: %12f\n", pathRadS2MSS3);
printf("Pathradiance Scene 2 Band 4: %12f\n\n", pathRadS2MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS2MSS1 = (pi * (RADS2MSS1[i,j] - pathRadS2MSS1)) / (radsurfMSS2b1 * cos(s2zenith));

                  if ( valueS2MSS1 < 0 )
       		{
          			valueS2MSS1 = 0;
				countS2REFnullMSS1 = countS2REFnullMSS1 + 1;
       		}

			else if ( valueS2MSS1 > 1 )
			{
				valueS2MSS1 = 1;
				countS2REFoneMSS1 = countS2REFoneMSS1 + 1;
			}
       		
			REFS2MSS1[i,j] = valueS2MSS1;

               valueS2MSS2 = (pi * (RADS2MSS2[i,j] - pathRadS2MSS2)) / (radsurfMSS2b2 * cos(s2zenith));

                  if ( valueS2MSS2 < 0 )
       		{
          			valueS2MSS2 = 0;
				countS2REFnullMSS2 = countS2REFnullMSS2 + 1;
       		}

			else if ( valueS2MSS2 > 1 )
			{
				valueS2MSS2 = 1;
				countS2REFoneMSS2 = countS2REFoneMSS2 + 1;
			}
       		
			REFS2MSS2[i,j] = valueS2MSS2;

               valueS2MSS3 = (pi * (RADS2MSS3[i,j] - pathRadS2MSS3)) / (radsurfMSS2b3 * cos(s2zenith));

                  if ( valueS2MSS3 < 0 )
       		{
          			valueS2MSS3 = 0;
				countS2REFnullMSS3 = countS2REFnullMSS3 + 1;
       		}

			else if ( valueS2MSS3 > 1 )
			{
				valueS2MSS3 = 1;
				countS2REFoneMSS3 = countS2REFoneMSS3 + 1;
			}
       		
			REFS2MSS3[i,j] = valueS2MSS3;

               valueS2MSS4 = (pi * (RADS2MSS4[i,j] - pathRadS2MSS4)) / (radsurfMSS2b4 * cos(s2zenith));

                  if ( valueS2MSS4 < 0 )
       		{
          			valueS2MSS4 = 0;
				countS2REFnullMSS4 = countS2REFnullMSS4 + 1;
       		}

			else if ( valueS2MSS4 > 1 )
			{
				valueS2MSS4 = 1;
				countS2REFoneMSS4 = countS2REFoneMSS4 + 1;
			}
       		
			REFS2MSS4[i,j] = valueS2MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS1, countS2REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS2, countS2REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS3, countS2REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS4, countS2REFoneMSS4);

CreatePyramid(REFS2MSS1);
CreatePyramid(REFS2MSS2);
CreatePyramid(REFS2MSS3);
CreatePyramid(REFS2MSS4);
        
CreateHistogram(REFS2MSS1);
CreateHistogram(REFS2MSS2);
CreateHistogram(REFS2MSS3);
CreateHistogram(REFS2MSS4);

CloseRaster(RADS2MSS1);
CloseRaster(RADS2MSS2);
CloseRaster(RADS2MSS3);
CloseRaster(RADS2MSS4);

printf("Reflectance calculation for Scene 2 (MSS) is done...\n\n\n");
}

else 																# MSS 1, 2 and 3
{

raster REFS2MSS1, REFS2MSS2, REFS2MSS3, REFS2MSS4;
numeric radsurfMSS2b1, radsurfMSS2b2, radsurfMSS2b3, radsurfMSS2b4;
numeric valueS2MSS1, valueS2MSS2, valueS2MSS3, valueS2MSS4;

numeric dosS2MSS1nullValCount = 0;
numeric dosS2MSS2nullValCount = 0;
numeric dosS2MSS3nullValCount = 0;
numeric dosS2MSS4nullValCount = 0;

numeric dosS2MSS1realArrayCount = 1;
numeric dosS2MSS2realArrayCount = 1;
numeric dosS2MSS3realArrayCount = 1;
numeric dosS2MSS4realArrayCount = 1;

numeric countS2REFnullMSS1 = 0;
numeric countS2REFnullMSS2 = 0;
numeric countS2REFnullMSS3 = 0;
numeric countS2REFnullMSS4 = 0;

numeric countS2REFoneMSS1 = 0;
numeric countS2REFoneMSS2 = 0;
numeric countS2REFoneMSS3 = 0;
numeric countS2REFoneMSS4 = 0;

radsurfMSS2b1 = ESMSS11 / (dist2^2);
radsurfMSS2b2 = ESMSS12 / (dist2^2);
radsurfMSS2b3 = ESMSS13 / (dist2^2);
radsurfMSS2b4 = ESMSS14 / (dist2^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 2): %12f\n", radsurfMSS2b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 2): %12f\n\n", radsurfMSS2b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS2MSS1[i,j]) == 1)
		{
		dosS2MSS1nullValCount = dosS2MSS1nullValCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 1)
		{
		dosS2MSS2nullValCount = dosS2MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS3[i,j]) == 1)
		{
		dosS2MSS3nullValCount = dosS2MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS2MSS4[i,j]) == 1)
		{
		dosS2MSS4nullValCount = dosS2MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS2MSS1realSize = (MSSlins * MSScols) - dosS2MSS1nullValCount;
numeric dosS2MSS2realSize = (MSSlins * MSScols) - dosS2MSS2nullValCount;
numeric dosS2MSS3realSize = (MSSlins * MSScols) - dosS2MSS3nullValCount;
numeric dosS2MSS4realSize = (MSSlins * MSScols) - dosS2MSS4nullValCount;

array allValuesDosS2MSS1[dosS2MSS1realSize];
array allValuesDosS2MSS2[dosS2MSS2realSize];
array allValuesDosS2MSS3[dosS2MSS3realSize];
array allValuesDosS2MSS4[dosS2MSS4realSize];

numeric DNminCALCS2MSS1 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS1realSize);
numeric DNminCALCS2MSS2 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS2realSize);
numeric DNminCALCS2MSS3 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS3realSize);
numeric DNminCALCS2MSS4 = DNminCALC(Scene2Lin, Scene2Col, dosS2MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS2MSS1[i,j]) == 0)
		{
		  allValuesDosS2MSS1[dosS2MSS1realArrayCount] = RADS2MSS1[i,j];		 
		  dosS2MSS1realArrayCount = dosS2MSS1realArrayCount + 1;
		}
		if (IsNull(RADS2MSS2[i,j]) == 0)
		{
		  allValuesDosS2MSS2[dosS2MSS2realArrayCount] = RADS2MSS2[i,j];		 
		  dosS2MSS2realArrayCount = dosS2MSS2realArrayCount + 1;
		}
		if (IsNull(RADS2MSS3[i,j]) == 0)
		{
		  allValuesDosS2MSS3[dosS2MSS3realArrayCount] = RADS2MSS3[i,j];		 
		  dosS2MSS3realArrayCount = dosS2MSS3realArrayCount + 1;
		}
		if (IsNull(RADS2MSS4[i,j]) == 0)
		{
		  allValuesDosS2MSS4[dosS2MSS4realArrayCount] = RADS2MSS4[i,j];		 
		  dosS2MSS4realArrayCount = dosS2MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 2 - Dn1000 - Band 1

numeric lastDOSS2MSS1 = dosS2MSS1realSize;
numeric hDOSS2MSS1 = 1;

while ( (hDOSS2MSS1 * 3 + 1) < lastDOSS2MSS1 - 1 )
{
    hDOSS2MSS1 = 3 * hDOSS2MSS1 + 1;
}

while ( hDOSS2MSS1 > 0 )
{
    for i = hDOSS2MSS1 - 1 to lastDOSS2MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS1 = allValuesDosS2MSS1[i];
        numeric jDOSS2MSS1 = i;

        while (jDOSS2MSS1 >= hDOSS2MSS1 && allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1] > keyDOSS2MSS1)
        {
            allValuesDosS2MSS1[jDOSS2MSS1] = allValuesDosS2MSS1[jDOSS2MSS1 - hDOSS2MSS1];
            jDOSS2MSS1 = jDOSS2MSS1 - hDOSS2MSS1;
        }
        allValuesDosS2MSS1[jDOSS2MSS1] = keyDOSS2MSS1;
    }
    hDOSS2MSS1 = floor(hDOSS2MSS1/3);
}

numeric minValueDosS2MSS1 = allValuesDosS2MSS1[1];
numeric dn1000S2MSS1 = 10000;

numeric actualCountDosS2MSS1 = 0;
for i = 1 to dosS2MSS1realSize 
{
    if (allValuesDosS2MSS1[i] == minValueDosS2MSS1)
    {
        actualCountDosS2MSS1 = actualCountDosS2MSS1 + 1;
        if (actualCountDosS2MSS1 >= DNminCALCS2MSS1)
        {
            dn1000S2MSS1 = minValueDosS2MSS1;
            go to dn1000S2MSS1jumper;
        }
    }
    else
    {
        minValueDosS2MSS1 = allValuesDosS2MSS1[i+1];
        actualCountDosS2MSS1 = 0;
    }
}

dn1000S2MSS1jumper:

if (dn1000S2MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
if (dn1000S2MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS1 = allValuesDosS2MSS1[1];
}
	
printf("dn1000 Scene 2 MSS1: %12f\n", dn1000S2MSS1);

########################### Scene 2 - Dn1000 - Band 2

numeric lastDOSS2MSS2 = dosS2MSS1realSize;
numeric hDOSS2MSS2 = 1;

while ( (hDOSS2MSS2 * 3 + 1) < lastDOSS2MSS2 - 1 )
{
    hDOSS2MSS2 = 3 * hDOSS2MSS2 + 1;
}

while ( hDOSS2MSS2 > 0 )
{
    for i = hDOSS2MSS2 - 1 to lastDOSS2MSS2
    {
        numeric keyDOSS2MSS2 = allValuesDosS2MSS2[i];
        numeric jDOSS2MSS2 = i;

        while (jDOSS2MSS2 >= hDOSS2MSS2 && allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2] > keyDOSS2MSS2)
        {
            allValuesDosS2MSS2[jDOSS2MSS2] = allValuesDosS2MSS2[jDOSS2MSS2 - hDOSS2MSS2];
            jDOSS2MSS2 = jDOSS2MSS2 - hDOSS2MSS2;
        }
        allValuesDosS2MSS2[jDOSS2MSS2] = keyDOSS2MSS2;
    }
    hDOSS2MSS2 = floor(hDOSS2MSS2/3);
}

numeric minValueDosS2MSS2 = allValuesDosS2MSS2[1];
numeric dn1000S2MSS2 = 10000;

numeric actualCountDosS2MSS2 = 0;
for i = 1 to dosS2MSS2realSize 
{
    if (allValuesDosS2MSS2[i] == minValueDosS2MSS2)
    {
        actualCountDosS2MSS2 = actualCountDosS2MSS2 + 1;
        if (actualCountDosS2MSS2 >= DNminCALCS2MSS2)
        {
            dn1000S2MSS2 = minValueDosS2MSS2;
            go to dn1000S2MSS2jumper;
        }
    }
    else
    {
        minValueDosS2MSS2 = allValuesDosS2MSS2[i+1];
        actualCountDosS2MSS2 = 0;
    }
}

dn1000S2MSS2jumper:

if (dn1000S2MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}
if (dn1000S2MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS2 = allValuesDosS2MSS2[1];
}

printf("dn1000 Scene 2 MSS2: %12f\n", dn1000S2MSS2);

########################### Scene 2 - Dn1000 - Band 3

numeric lastDOSS2MSS3 = dosS2MSS3realSize;
numeric hDOSS2MSS3 = 1;

while ( (hDOSS2MSS3 * 3 + 1) < lastDOSS2MSS3 - 1 )
{
    hDOSS2MSS3 = 3 * hDOSS2MSS3 + 1;
}

while ( hDOSS2MSS3 > 0 )
{
    for i = hDOSS2MSS3 - 1 to lastDOSS2MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS2MSS3 = allValuesDosS2MSS3[i];
        numeric jDOSS2MSS3 = i;

        while (jDOSS2MSS3 >= hDOSS2MSS3 && allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3] > keyDOSS2MSS3)
        {
            allValuesDosS2MSS3[jDOSS2MSS3] = allValuesDosS2MSS3[jDOSS2MSS3 - hDOSS2MSS3];
            jDOSS2MSS3 = jDOSS2MSS3 - hDOSS2MSS3;
        }
        allValuesDosS2MSS3[jDOSS2MSS3] = keyDOSS2MSS3;
    }
    hDOSS2MSS3 = floor(hDOSS2MSS3/3);
}

numeric minValueDosS2MSS3 = allValuesDosS2MSS3[1];
numeric dn1000S2MSS3 = 10000;

numeric actualCountDosS2MSS3 = 0;
for i = 1 to dosS2MSS3realSize 
{
    if (allValuesDosS2MSS3[i] == minValueDosS2MSS3)
    {
        actualCountDosS2MSS3 = actualCountDosS2MSS3 + 1;
        if (actualCountDosS2MSS3 >= DNminCALCS2MSS3)
        {
            dn1000S2MSS3 = minValueDosS2MSS3;
            go to dn1000S2MSS3jumper;
        }
    }
    else
    {
        minValueDosS2MSS3 = allValuesDosS2MSS3[i+1];
        actualCountDosS2MSS3 = 0;
    }
}

dn1000S2MSS3jumper:

if (dn1000S2MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}
if (dn1000S2MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS3 = allValuesDosS2MSS3[1];
}
	
printf("dn1000 Scene 2 MSS3: %12f\n", dn1000S2MSS3);

########################### Scene 1 - Dn1000 - Band 4

numeric lastDOSS2MSS4 = dosS2MSS4realSize;
numeric hDOSS2MSS4 = 1;

while ( (hDOSS2MSS4 * 3 + 1) < lastDOSS2MSS4 - 1 )
{
    hDOSS2MSS4 = 3 * hDOSS2MSS4 + 1;
}

while ( hDOSS2MSS4 > 0 )
{
    for i = hDOSS2MSS4 - 1 to lastDOSS2MSS4
    {
        numeric keyDOSS2MSS4 = allValuesDosS2MSS4[i];
        numeric jDOSS2MSS4 = i;

        while (jDOSS2MSS4 >= hDOSS2MSS4 && allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4] > keyDOSS2MSS4)
        {
            allValuesDosS2MSS4[jDOSS2MSS4] = allValuesDosS2MSS4[jDOSS2MSS4 - hDOSS2MSS4];
            jDOSS2MSS4 = jDOSS2MSS4 - hDOSS2MSS4;
        }
        allValuesDosS2MSS4[jDOSS2MSS4] = keyDOSS2MSS4;
    }
    hDOSS2MSS4 = floor(hDOSS2MSS4/3);
}

numeric minValueDosS2MSS4 = allValuesDosS2MSS4[1];
numeric dn1000S2MSS4 = 10000;

numeric actualCountDosS2MSS4 = 0;
for i = 1 to dosS2MSS4realSize 
{
    if (allValuesDosS2MSS4[i] == minValueDosS2MSS4)
    {
        actualCountDosS2MSS4 = actualCountDosS2MSS4 + 1;
        if (actualCountDosS2MSS4 >= DNminCALCS2MSS4)
        {
            dn1000S2MSS4 = minValueDosS2MSS4;
            go to dn1000S2MSS4jumper;
        }
    }
    else
    {
        minValueDosS2MSS4 = allValuesDosS2MSS4[i+1];
        actualCountDosS2MSS4 = 0;
    }
}

dn1000S2MSS4jumper:

if (dn1000S2MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
if (dn1000S2MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S2MSS4 = allValuesDosS2MSS4[1];
}
	
printf("dn1000 Scene 2 MSS4: %12f\n", dn1000S2MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS2MSS1 = dn1000S2MSS1 - (0.01 * (radsurfMSS2b1 * cos(s2zenith)) / pi);
numeric pathRadS2MSS2 = dn1000S2MSS2 - (0.01 * (radsurfMSS2b2 * cos(s2zenith)) / pi);
numeric pathRadS2MSS3 = dn1000S2MSS3 - (0.01 * (radsurfMSS2b3 * cos(s2zenith)) / pi);
numeric pathRadS2MSS4 = dn1000S2MSS4 - (0.01 * (radsurfMSS2b4 * cos(s2zenith)) / pi);

printf("Pathradiance Scene 2 Band 1: %12f\n", pathRadS2MSS1);
printf("Pathradiance Scene 2 Band 2: %12f\n", pathRadS2MSS2);
printf("Pathradiance Scene 2 Band 3: %12f\n", pathRadS2MSS3);
printf("Pathradiance Scene 2 Band 4: %12f\n\n", pathRadS2MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS2MSS1 = (pi * (RADS2MSS1[i,j] - pathRadS2MSS1)) / (radsurfMSS2b1 * cos(s2zenith));

                  if ( valueS2MSS1 < 0 )
       		{
          			valueS2MSS1 = 0;
				countS2REFnullMSS1 = countS2REFnullMSS1 + 1;
       		}

			else if ( valueS2MSS1 > 1 )
			{
				valueS2MSS1 = 1;
				countS2REFoneMSS1 = countS2REFoneMSS1 + 1;
			}
       		
			REFS2MSS1[i,j] = valueS2MSS1;

               valueS2MSS2 = (pi * (RADS2MSS2[i,j] - pathRadS2MSS2)) / (radsurfMSS2b2 * cos(s2zenith));

                  if ( valueS2MSS2 < 0 )
       		{
          			valueS2MSS2 = 0;
				countS2REFnullMSS2 = countS2REFnullMSS2 + 1;
       		}

			else if ( valueS2MSS2 > 1 )
			{
				valueS2MSS2 = 1;
				countS2REFoneMSS2 = countS2REFoneMSS2 + 1;
			}
       		
			REFS2MSS2[i,j] = valueS2MSS2;

               valueS2MSS3 = (pi * (RADS2MSS3[i,j] - pathRadS2MSS3)) / (radsurfMSS2b3 * cos(s2zenith));

                  if ( valueS2MSS3 < 0 )
       		{
          			valueS2MSS3 = 0;
				countS2REFnullMSS3 = countS2REFnullMSS3 + 1;
       		}

			else if ( valueS2MSS3 > 1 )
			{
				valueS2MSS3 = 1;
				countS2REFoneMSS3 = countS2REFoneMSS3 + 1;
			}
       		
			REFS2MSS3[i,j] = valueS2MSS3;

               valueS2MSS4 = (pi * (RADS2MSS4[i,j] - pathRadS2MSS4)) / (radsurfMSS2b4 * cos(s2zenith));

                  if ( valueS2MSS4 < 0 )
       		{
          			valueS2MSS4 = 0;
				countS2REFnullMSS4 = countS2REFnullMSS4 + 1;
       		}

			else if ( valueS2MSS4 > 1 )
			{
				valueS2MSS4 = 1;
				countS2REFoneMSS4 = countS2REFoneMSS4 + 1;
			}
       		
			REFS2MSS4[i,j] = valueS2MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS1, countS2REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS2, countS2REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS3, countS2REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 2 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS2REFnullMSS4, countS2REFoneMSS4);

CreatePyramid(REFS2MSS1);
CreatePyramid(REFS2MSS2);
CreatePyramid(REFS2MSS3);
CreatePyramid(REFS2MSS4);
        
CreateHistogram(REFS2MSS1);
CreateHistogram(REFS2MSS2);
CreateHistogram(REFS2MSS3);
CreateHistogram(REFS2MSS4);

CloseRaster(RADS2MSS1);
CloseRaster(RADS2MSS2);
CloseRaster(RADS2MSS3);
CloseRaster(RADS2MSS4);

printf("Reflectance calculation for Scene 2 (MSS) is done...\n\n\n");
}

#############################
#### Reflectance Scene 3
#############################

if ( sensors3 == 7 )
{
raster REFS3ETM1, REFS3ETM2, REFS3ETM3, REFS3ETM4, REFS3ETM5, REFS3ETM7;
numeric radsurfETMS3b1, radsurfETMS3b2, radsurfETMS3b3, radsurfETMS3b4, radsurfETMS3b5, radsurfETMS3b7;
numeric valueS3ETM1, valueS3ETM2, valueS3ETM3, valueS3ETM4, valueS3ETM5, valueS3ETM7;

numeric dosS3ETM1nullValCount = 0;
numeric dosS3ETM2nullValCount = 0;
numeric dosS3ETM3nullValCount = 0;
numeric dosS3ETM4nullValCount = 0;
numeric dosS3ETM5nullValCount = 0;
numeric dosS3ETM7nullValCount = 0;

numeric dosS3ETM1realArrayCount = 1;
numeric dosS3ETM2realArrayCount = 1;
numeric dosS3ETM3realArrayCount = 1;
numeric dosS3ETM4realArrayCount = 1;
numeric dosS3ETM5realArrayCount = 1;
numeric dosS3ETM7realArrayCount = 1;

numeric countS3REFnullETM1 = 0;
numeric countS3REFnullETM2 = 0;
numeric countS3REFnullETM3 = 0;
numeric countS3REFnullETM4 = 0;
numeric countS3REFnullETM5 = 0;
numeric countS3REFnullETM7 = 0;

numeric countS3REFoneETM1 = 0;
numeric countS3REFoneETM2 = 0;
numeric countS3REFoneETM3 = 0;
numeric countS3REFoneETM4 = 0;
numeric countS3REFoneETM5 = 0;
numeric countS3REFoneETM7 = 0;

radsurfETMS3b1 = ESETM1 / (dist3^2);
radsurfETMS3b2 = ESETM2 / (dist3^2);
radsurfETMS3b3 = ESETM3 / (dist3^2);
radsurfETMS3b4 = ESETM4 / (dist3^2);
radsurfETMS3b5 = ESETM5 / (dist3^2);
radsurfETMS3b7 = ESETM7 / (dist3^2);

printf("Surface Radiance ETM Band 1 in W/m^2µm (Scene 3): %12f\n", radsurfETMS3b1);
printf("Surface Radiance ETM Band 2 in W/m^2µm (Scene 3): %12f\n", radsurfETMS3b2);
printf("Surface Radiance ETM Band 3 in W/m^2µm (Scene 3): %12f\n", radsurfETMS3b3);
printf("Surface Radiance ETM Band 4 in W/m^2µm (Scene 3): %12f\n", radsurfETMS3b4);
printf("Surface Radiance ETM Band 5 in W/m^2µm (Scene 3): %12f\n", radsurfETMS3b5);
printf("Surface Radiance ETM Band 7 in W/m^2µm (Scene 3): %12f\n\n", radsurfETMS3b7);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {       
		if (IsNull(RADS3ETM1[i,j]) == 1)
		{
		dosS3ETM1nullValCount = dosS3ETM1nullValCount + 1;
		}
		if (IsNull(RADS3ETM2[i,j]) == 1)
		{
		dosS3ETM2nullValCount = dosS3ETM2nullValCount + 1;
		}		 
		if (IsNull(RADS3ETM3[i,j]) == 1)
		{
		dosS3ETM3nullValCount = dosS3ETM3nullValCount + 1;
		}		 
		if (IsNull(RADS3ETM4[i,j]) == 1)
		{
		dosS3ETM4nullValCount = dosS3ETM4nullValCount + 1;
		}		 
		if (IsNull(RADS3ETM5[i,j]) == 1)
		{
		dosS3ETM5nullValCount = dosS3ETM5nullValCount + 1;
		}		 
		if (IsNull(RADS3ETM7[i,j]) == 1)
		{
		dosS3ETM7nullValCount = dosS3ETM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS3ETM1realSize = (ETMlins * ETMcols) - dosS3ETM1nullValCount;
numeric dosS3ETM2realSize = (ETMlins * ETMcols) - dosS3ETM2nullValCount;
numeric dosS3ETM3realSize = (ETMlins * ETMcols) - dosS3ETM3nullValCount;
numeric dosS3ETM4realSize = (ETMlins * ETMcols) - dosS3ETM4nullValCount;
numeric dosS3ETM5realSize = (ETMlins * ETMcols) - dosS3ETM5nullValCount;
numeric dosS3ETM7realSize = (ETMlins * ETMcols) - dosS3ETM7nullValCount;

array allValuesDosS3ETM1[dosS3ETM1realSize];
array allValuesDosS3ETM2[dosS3ETM2realSize];
array allValuesDosS3ETM3[dosS3ETM3realSize];
array allValuesDosS3ETM4[dosS3ETM4realSize];
array allValuesDosS3ETM5[dosS3ETM5realSize];
array allValuesDosS3ETM7[dosS3ETM7realSize];

numeric DNminCALCS3ETM1 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM1realSize);
numeric DNminCALCS3ETM2 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM2realSize);
numeric DNminCALCS3ETM3 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM3realSize);
numeric DNminCALCS3ETM4 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM4realSize);
numeric DNminCALCS3ETM5 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM5realSize);
numeric DNminCALCS3ETM7 = DNminCALC(Scene3Lin, Scene3Col, dosS3ETM7realSize);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(RADS3ETM1[i,j]) == 0)
		{
		  allValuesDosS3ETM1[dosS3ETM1realArrayCount] = RADS3ETM1[i,j];		 
		  dosS3ETM1realArrayCount = dosS3ETM1realArrayCount + 1;
		}
		if (IsNull(RADS3ETM2[i,j]) == 0)
		{
		  allValuesDosS3ETM2[dosS3ETM2realArrayCount] = RADS3ETM2[i,j];		 
		  dosS3ETM2realArrayCount = dosS3ETM2realArrayCount + 1;
		}
		if (IsNull(RADS3ETM3[i,j]) == 0)
		{
		  allValuesDosS3ETM3[dosS3ETM3realArrayCount] = RADS3ETM3[i,j];		 
		  dosS3ETM3realArrayCount = dosS3ETM3realArrayCount + 1;
		}
		if (IsNull(RADS3ETM4[i,j]) == 0)
		{
		  allValuesDosS3ETM4[dosS3ETM4realArrayCount] = RADS3ETM4[i,j];		 
		  dosS3ETM4realArrayCount = dosS3ETM4realArrayCount + 1;
		}
		if (IsNull(RADS3ETM5[i,j]) == 0)
		{
		  allValuesDosS3ETM5[dosS3ETM5realArrayCount] = RADS3ETM5[i,j];		 
		  dosS3ETM5realArrayCount = dosS3ETM5realArrayCount + 1;
		}
		if (IsNull(RADS3ETM7[i,j]) == 0)
		{
		  allValuesDosS3ETM7[dosS3ETM7realArrayCount] = RADS3ETM7[i,j];		 
		  dosS3ETM7realArrayCount = dosS3ETM7realArrayCount + 1;
		}
    }
}

########################### Scene 3 - Dn1000 - Band 1

numeric lastDOSS3ETM1 = dosS3ETM1realSize;
numeric hDOSS3ETM1 = 1;

while ( (hDOSS3ETM1 * 3 + 1) < lastDOSS3ETM1 - 1 )
{
    hDOSS3ETM1 = 3 * hDOSS3ETM1 + 1;
}

while ( hDOSS3ETM1 > 0 )
{
    for i = hDOSS3ETM1 - 1 to lastDOSS3ETM1               # for each of the h sets of elements
    {
        numeric keyDOSS3ETM1 = allValuesDosS3ETM1[i];
        numeric jDOSS3ETM1 = i;

        while (jDOSS3ETM1 >= hDOSS3ETM1 && allValuesDosS3ETM1[jDOSS3ETM1 - hDOSS3ETM1] > keyDOSS3ETM1)
        {
            allValuesDosS3ETM1[jDOSS3ETM1] = allValuesDosS3ETM1[jDOSS3ETM1 - hDOSS3ETM1];
            jDOSS3ETM1 = jDOSS3ETM1 - hDOSS3ETM1;
        }
        allValuesDosS3ETM1[jDOSS3ETM1] = keyDOSS3ETM1;
    }
    hDOSS3ETM1 = floor(hDOSS3ETM1/3);
}

numeric minValueDosS3ETM1 = allValuesDosS3ETM1[1];
numeric dn1000S3ETM1 = 10000;

numeric actualCountDosS3ETM1 = 0;
for i = 1 to dosS3ETM1realSize 
{
    if (allValuesDosS3ETM1[i] == minValueDosS3ETM1)
    {
        actualCountDosS3ETM1 = actualCountDosS3ETM1 + 1;
        if (actualCountDosS3ETM1 >= DNminCALCS3ETM1)
        {
            dn1000S3ETM1 = minValueDosS3ETM1;
            go to dn1000S3ETM1jumper;
        }
    }
    else
    {
        minValueDosS3ETM1 = allValuesDosS3ETM1[i+1];
        actualCountDosS3ETM1 = 0;
    }
}

dn1000S3ETM1jumper:

if (dn1000S3ETM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM1 = allValuesDosS3ETM1[1];
}
if (dn1000S3ETM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM1 = allValuesDosS3ETM1[1];
}
	
printf("dn1000 Scene 3 ETM1: %12f\n", dn1000S3ETM1);

########################### Scene 3 - Dn1000 - Band 2

numeric lastDOSS3ETM2 = dosS3ETM1realSize;
numeric hDOSS3ETM2 = 1;

while ( (hDOSS3ETM2 * 3 + 1) < lastDOSS3ETM2 - 1 )
{
    hDOSS3ETM2 = 3 * hDOSS3ETM2 + 1;
}

while ( hDOSS3ETM2 > 0 )
{
    for i = hDOSS3ETM2 - 1 to lastDOSS3ETM2
    {
        numeric keyDOSS3ETM2 = allValuesDosS3ETM2[i];
        numeric jDOSS3ETM2 = i;

        while (jDOSS3ETM2 >= hDOSS3ETM2 && allValuesDosS3ETM2[jDOSS3ETM2 - hDOSS3ETM2] > keyDOSS3ETM2)
        {
            allValuesDosS3ETM2[jDOSS3ETM2] = allValuesDosS3ETM2[jDOSS3ETM2 - hDOSS3ETM2];
            jDOSS3ETM2 = jDOSS3ETM2 - hDOSS3ETM2;
        }
        allValuesDosS3ETM2[jDOSS3ETM2] = keyDOSS3ETM2;
    }
    hDOSS3ETM2 = floor(hDOSS3ETM2/3);
}

numeric minValueDosS3ETM2 = allValuesDosS3ETM2[1];
numeric dn1000S3ETM2 = 10000;

numeric actualCountDosS3ETM2 = 0;
for i = 1 to dosS3ETM2realSize 
{
    if (allValuesDosS3ETM2[i] == minValueDosS3ETM2)
    {
        actualCountDosS3ETM2 = actualCountDosS3ETM2 + 1;
        if (actualCountDosS3ETM2 >= DNminCALCS3ETM2)
        {
            dn1000S3ETM2 = minValueDosS3ETM2;
            go to dn1000S3ETM2jumper;
        }
    }
    else
    {
        minValueDosS3ETM2 = allValuesDosS3ETM2[i+1];
        actualCountDosS3ETM2 = 0;
    }
}

dn1000S3ETM2jumper:

if (dn1000S3ETM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM2 = allValuesDosS3ETM2[1];
}
if (dn1000S3ETM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM2 = allValuesDosS3ETM2[1];
}

printf("dn1000 Scene 3 ETM2: %12f\n", dn1000S3ETM2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS3ETM3 = dosS3ETM3realSize;
numeric hDOSS3ETM3 = 1;

while ( (hDOSS3ETM3 * 3 + 1) < lastDOSS3ETM3 - 1 )
{
    hDOSS3ETM3 = 3 * hDOSS3ETM3 + 1;
}

while ( hDOSS3ETM3 > 0 )
{
    for i = hDOSS3ETM3 - 1 to lastDOSS3ETM3               # for each of the h sets of elements
    {
        numeric keyDOSS3ETM3 = allValuesDosS3ETM3[i];
        numeric jDOSS3ETM3 = i;

        while (jDOSS3ETM3 >= hDOSS3ETM3 && allValuesDosS3ETM3[jDOSS3ETM3 - hDOSS3ETM3] > keyDOSS3ETM3)
        {
            allValuesDosS3ETM3[jDOSS3ETM3] = allValuesDosS3ETM3[jDOSS3ETM3 - hDOSS3ETM3];
            jDOSS3ETM3 = jDOSS3ETM3 - hDOSS3ETM3;
        }
        allValuesDosS3ETM3[jDOSS3ETM3] = keyDOSS3ETM3;
    }
    hDOSS3ETM3 = floor(hDOSS3ETM3/3);
}

numeric minValueDosS3ETM3 = allValuesDosS3ETM3[1];
numeric dn1000S3ETM3 = 10000;

numeric actualCountDosS3ETM3 = 0;
for i = 1 to dosS3ETM3realSize 
{
    if (allValuesDosS3ETM3[i] == minValueDosS3ETM3)
    {
        actualCountDosS3ETM3 = actualCountDosS3ETM3 + 1;
        if (actualCountDosS3ETM3 >= DNminCALCS3ETM3)
        {
            dn1000S3ETM3 = minValueDosS3ETM3;
            go to dn1000S3ETM3jumper;
        }
    }
    else
    {
        minValueDosS3ETM3 = allValuesDosS3ETM3[i+1];
        actualCountDosS3ETM3 = 0;
    }
}

dn1000S3ETM3jumper:

if (dn1000S3ETM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM3 = allValuesDosS3ETM3[1];
}
if (dn1000S3ETM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM3 = allValuesDosS3ETM3[1];
}
	
printf("dn1000 Scene 3 ETM3: %12f\n", dn1000S3ETM3);

########################### Scene 3 - Dn1000 - Band 4

numeric lastDOSS3ETM4 = dosS3ETM4realSize;
numeric hDOSS3ETM4 = 1;

while ( (hDOSS3ETM4 * 3 + 1) < lastDOSS3ETM4 - 1 )
{
    hDOSS3ETM4 = 3 * hDOSS3ETM4 + 1;
}

while ( hDOSS3ETM4 > 0 )
{
    for i = hDOSS3ETM4 - 1 to lastDOSS3ETM4
    {
        numeric keyDOSS3ETM4 = allValuesDosS3ETM4[i];
        numeric jDOSS3ETM4 = i;

        while (jDOSS3ETM4 >= hDOSS3ETM4 && allValuesDosS3ETM4[jDOSS3ETM4 - hDOSS3ETM4] > keyDOSS3ETM4)
        {
            allValuesDosS3ETM4[jDOSS3ETM4] = allValuesDosS3ETM4[jDOSS3ETM4 - hDOSS3ETM4];
            jDOSS3ETM4 = jDOSS3ETM4 - hDOSS3ETM4;
        }
        allValuesDosS3ETM4[jDOSS3ETM4] = keyDOSS3ETM4;
    }
    hDOSS3ETM4 = floor(hDOSS3ETM4/3);
}

numeric minValueDosS3ETM4 = allValuesDosS3ETM4[1];
numeric dn1000S3ETM4 = 10000;

numeric actualCountDosS3ETM4 = 0;
for i = 1 to dosS3ETM4realSize 
{
    if (allValuesDosS3ETM4[i] == minValueDosS3ETM4)
    {
        actualCountDosS3ETM4 = actualCountDosS3ETM4 + 1;
        if (actualCountDosS3ETM4 >= DNminCALCS3ETM4)
        {
            dn1000S3ETM4 = minValueDosS3ETM4;
            go to dn1000S3ETM4jumper;
        }
    }
    else
    {
        minValueDosS3ETM4 = allValuesDosS3ETM4[i+1];
        actualCountDosS3ETM4 = 0;
    }
}

dn1000S3ETM4jumper:

if (dn1000S3ETM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM4 = allValuesDosS3ETM4[1];
}
if (dn1000S3ETM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM4 = allValuesDosS3ETM4[1];
}
	
printf("dn1000 Scene 3 ETM4: %12f\n", dn1000S3ETM4);

########################### Scene 3 - Dn1000 - Band 5

numeric lastDOSS3ETM5 = dosS3ETM5realSize;
numeric hDOSS3ETM5 = 1;

while ( (hDOSS3ETM5 * 3 + 1) < lastDOSS3ETM5 - 1 )
{
    hDOSS3ETM5 = 3 * hDOSS3ETM5 + 1;
}

while ( hDOSS3ETM5 > 0 )
{
    for i = hDOSS3ETM5 - 1 to lastDOSS3ETM5               # for each of the h sets of elements
    {
        numeric keyDOSS3ETM5 = allValuesDosS3ETM5[i];
        numeric jDOSS3ETM5 = i;

        while (jDOSS3ETM5 >= hDOSS3ETM5 && allValuesDosS3ETM5[jDOSS3ETM5 - hDOSS3ETM5] > keyDOSS3ETM5)
        {
            allValuesDosS3ETM5[jDOSS3ETM5] = allValuesDosS3ETM5[jDOSS3ETM5 - hDOSS3ETM5];
            jDOSS3ETM5 = jDOSS3ETM5 - hDOSS3ETM5;
        }
        allValuesDosS3ETM5[jDOSS3ETM5] = keyDOSS3ETM5;
    }
    hDOSS3ETM5 = floor(hDOSS3ETM5/3);
}

numeric minValueDosS3ETM5 = allValuesDosS3ETM5[1];
numeric dn1000S3ETM5 = 10000;

numeric actualCountDosS3ETM5 = 0;
for i = 1 to dosS3ETM5realSize 
{
    if (allValuesDosS3ETM5[i] == minValueDosS3ETM5)
    {
        actualCountDosS3ETM5 = actualCountDosS3ETM5 + 1;
        if (actualCountDosS3ETM5 >= DNminCALCS3ETM5)
        {
            dn1000S3ETM5 = minValueDosS3ETM5;
            go to dn1000S3ETM5jumper;
        }
    }
    else
    {
        minValueDosS3ETM5 = allValuesDosS3ETM5[i+1];
        actualCountDosS3ETM5 = 0;
    }
}

dn1000S3ETM5jumper:

if (dn1000S3ETM5  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM5  = allValuesDosS3ETM5[1];
}
if (dn1000S3ETM5  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM5  = allValuesDosS3ETM5[1];
}
	
printf("dn1000 Scene 3 ETM5: %12f\n", dn1000S3ETM5);

########################### Scene 3 - Dn1000 - Band 7

numeric lastDOSS3ETM7 = dosS3ETM7realSize;
numeric hDOSS3ETM7 = 1;

while ( (hDOSS3ETM7 * 3 + 1) < lastDOSS3ETM7 - 1 )
{
    hDOSS3ETM7 = 3 * hDOSS3ETM7 + 1;
}

while ( hDOSS3ETM7 > 0 )
{
    for i = hDOSS3ETM7 - 1 to lastDOSS3ETM7               # for each of the h sets of elements
    {
        numeric keyDOSS3ETM7 = allValuesDosS3ETM7[i];
        numeric jDOSS3ETM7 = i;

        while (jDOSS3ETM7 >= hDOSS3ETM7 && allValuesDosS3ETM7[jDOSS3ETM7 - hDOSS3ETM7] > keyDOSS3ETM7)
        {
            allValuesDosS3ETM7[jDOSS3ETM7] = allValuesDosS3ETM7[jDOSS3ETM7 - hDOSS3ETM7];
            jDOSS3ETM7 = jDOSS3ETM7 - hDOSS3ETM7;
        }
        allValuesDosS3ETM7[jDOSS3ETM7] = keyDOSS3ETM7;
    }
    hDOSS3ETM7 = floor(hDOSS3ETM7/3);
}

numeric minValueDosS3ETM7 = allValuesDosS3ETM7[1];
numeric dn1000S3ETM7 = 10000;

numeric actualCountDosS3ETM7 = 0;
for i = 1 to dosS3ETM7realSize 
{
    if (allValuesDosS3ETM7[i] == minValueDosS3ETM7)
    {
        actualCountDosS3ETM7 = actualCountDosS3ETM7 + 1;
        if (actualCountDosS3ETM7 >= DNminCALCS3ETM7)
        {
            dn1000S3ETM7 = minValueDosS3ETM7;
            go to dn1000S3ETM7jumper;
        }
    }
    else
    {
        minValueDosS3ETM7 = allValuesDosS3ETM7[i+1];
        actualCountDosS3ETM7 = 0;
    }
}

dn1000S3ETM7jumper:

if (dn1000S3ETM7 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3ETM7 = allValuesDosS3ETM7[1];
}
if (dn1000S3ETM7 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3ETM7 = allValuesDosS3ETM7[1];
}
	
printf("dn1000 Scene 3 ETM7: %12f\n\n", dn1000S3ETM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS3ETM1 = dn1000S3ETM1 - (0.01 * (radsurfETMS3b1 * cos(s3zenith)) / pi);
numeric pathRadS3ETM2 = dn1000S3ETM2 - (0.01 * (radsurfETMS3b2 * cos(s3zenith)) / pi);
numeric pathRadS3ETM3 = dn1000S3ETM3 - (0.01 * (radsurfETMS3b3 * cos(s3zenith)) / pi);
numeric pathRadS3ETM4 = dn1000S3ETM4 - (0.01 * (radsurfETMS3b4 * cos(s3zenith)) / pi);
numeric pathRadS3ETM5 = dn1000S3ETM5 - (0.01 * (radsurfETMS3b5 * cos(s3zenith)) / pi);
numeric pathRadS3ETM7 = dn1000S3ETM7 - (0.01 * (radsurfETMS3b7 * cos(s3zenith)) / pi);

printf("Pathradiance Scene 3 Band 1: %12f\n", pathRadS3ETM1);
printf("Pathradiance Scene 3 Band 2: %12f\n", pathRadS3ETM2);
printf("Pathradiance Scene 3 Band 3: %12f\n", pathRadS3ETM3);
printf("Pathradiance Scene 3 Band 4: %12f\n", pathRadS3ETM4);
printf("Pathradiance Scene 3 Band 5: %12f\n", pathRadS3ETM5);
printf("Pathradiance Scene 3 Band 7: %12f\n\n", pathRadS3ETM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to ETMlins
	 {
	        for j = 1 to ETMcols
	        {
                  valueS3ETM1 = (pi * (RADS3ETM1[i,j] - pathRadS3ETM1)) / (radsurfETMS3b1 * cos(s3zenith));

                  if ( valueS3ETM1 < 0 )
       		{
          			valueS3ETM1 = 0;
				countS3REFnullETM1 = countS3REFnullETM1 + 1;
       		}

			else if ( valueS3ETM1 > 1 )
			{
				valueS3ETM1 = 1;
				countS3REFoneETM1 = countS3REFoneETM1 + 1;
			}
       		
			REFS3ETM1[i,j] = valueS3ETM1;

               valueS3ETM2 = (pi * (RADS3ETM2[i,j] - pathRadS3ETM2)) / (radsurfETMS3b2 * cos(s3zenith));

                  if ( valueS3ETM2 < 0 )
       		{
          			valueS3ETM2 = 0;
				countS3REFnullETM2 = countS3REFnullETM2 + 1;
       		}

			else if ( valueS3ETM2 > 1 )
			{
				valueS3ETM2 = 1;
				countS3REFoneETM2 = countS3REFoneETM2 + 1;
			}
       		
			REFS3ETM2[i,j] = valueS3ETM2;

               valueS3ETM3 = (pi * (RADS3ETM3[i,j] - pathRadS3ETM3)) / (radsurfETMS3b3 * cos(s3zenith));

                  if ( valueS3ETM3 < 0 )
       		{
          			valueS3ETM3 = 0;
				countS3REFnullETM3 = countS3REFnullETM3 + 1;
       		}

			else if ( valueS3ETM3 > 1 )
			{
				valueS3ETM3 = 1;
				countS3REFoneETM3 = countS3REFoneETM3 + 1;
			}
       		
			REFS3ETM3[i,j] = valueS3ETM3;

               valueS3ETM4 = (pi * (RADS3ETM4[i,j] - pathRadS3ETM4)) / (radsurfETMS3b4 * cos(s3zenith));

                  if ( valueS3ETM4 < 0 )
       		{
          			valueS3ETM4 = 0;
				countS3REFnullETM4 = countS3REFnullETM4 + 1;
       		}

			else if ( valueS3ETM4 > 1 )
			{
				valueS3ETM4 = 1;
				countS3REFoneETM4 = countS3REFoneETM4 + 1;
			}
       		
			REFS3ETM4[i,j] = valueS3ETM4;

               valueS3ETM5 = (pi * (RADS3ETM5[i,j] - pathRadS3ETM5)) / (radsurfETMS3b5 * cos(s3zenith));

                  if ( valueS3ETM5 < 0 )
       		{
          			valueS3ETM5 = 0;
				countS3REFnullETM5 = countS3REFnullETM5 + 1;
       		}

			else if ( valueS3ETM5 > 1 )
			{
				valueS3ETM5 = 1;
				countS3REFoneETM5 = countS3REFoneETM5 + 1;
			}
       		
			REFS3ETM5[i,j] = valueS3ETM5;

               valueS3ETM7 = (pi * (RADS3ETM7[i,j] - pathRadS3ETM7)) / (radsurfETMS3b7 * cos(s3zenith));

                  if ( valueS3ETM7 < 0 )
       		{
          			valueS3ETM7 = 0;
				countS3REFnullETM7 = countS3REFnullETM7 + 1;
       		}

			else if ( valueS3ETM7 > 1 )
			{
				valueS3ETM7 = 1;
				countS3REFoneETM7 = countS3REFoneETM7 + 1;
			}
       		
			REFS3ETM7[i,j] = valueS3ETM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullETM1, countS3REFoneETM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullETM2, countS3REFoneETM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullETM3, countS3REFoneETM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullETM4, countS3REFoneETM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullETM5, countS3REFoneETM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS3REFnullETM7, countS3REFoneETM7);

CreatePyramid(REFS3ETM1);
CreatePyramid(REFS3ETM2);
CreatePyramid(REFS3ETM3);
CreatePyramid(REFS3ETM4);
CreatePyramid(REFS3ETM5);
CreatePyramid(REFS3ETM7);
        
CreateHistogram(REFS3ETM1);
CreateHistogram(REFS3ETM2);
CreateHistogram(REFS3ETM3);
CreateHistogram(REFS3ETM4);
CreateHistogram(REFS3ETM5);
CreateHistogram(REFS3ETM7);

CloseRaster(RADS3ETM1);
CloseRaster(RADS3ETM2);
CloseRaster(RADS3ETM3);
CloseRaster(RADS3ETM4);
CloseRaster(RADS3ETM5);
CloseRaster(RADS3ETM7);

printf("Reflectance calculation for Scene 3 (ETM) is done...\n\n\n");
}

else if ( sensors3 == 6 )
{	
raster REFS3TM1, REFS3TM2, REFS3TM3, REFS3TM4, REFS3TM5, REFS3TM7;
numeric radsurfTMS3b1, radsurfTMS3b2, radsurfTMS3b3, radsurfTMS3b4, radsurfTMS3b5, radsurfTMS3b7;
numeric valueS3TM1, valueS3TM2, valueS3TM3, valueS3TM4, valueS3TM5, valueS3TM7;

numeric dosS3TM1nullValCount = 0;
numeric dosS3TM2nullValCount = 0;
numeric dosS3TM3nullValCount = 0;
numeric dosS3TM4nullValCount = 0;
numeric dosS3TM5nullValCount = 0;
numeric dosS3TM7nullValCount = 0;

numeric dosS3TM1realArrayCount = 1;
numeric dosS3TM2realArrayCount = 1;
numeric dosS3TM3realArrayCount = 1;
numeric dosS3TM4realArrayCount = 1;
numeric dosS3TM5realArrayCount = 1;
numeric dosS3TM7realArrayCount = 1;

numeric countS3REFnullTM1 = 0;
numeric countS3REFnullTM2 = 0;
numeric countS3REFnullTM3 = 0;
numeric countS3REFnullTM4 = 0;
numeric countS3REFnullTM5 = 0;
numeric countS3REFnullTM7 = 0;

numeric countS3REFoneTM1 = 0;
numeric countS3REFoneTM2 = 0;
numeric countS3REFoneTM3 = 0;
numeric countS3REFoneTM4 = 0;
numeric countS3REFoneTM5 = 0;
numeric countS3REFoneTM7 = 0;

radsurfTMS3b1 = ESTM1 / (dist3^2);
radsurfTMS3b2 = ESTM2 / (dist3^2);
radsurfTMS3b3 = ESTM3 / (dist3^2);
radsurfTMS3b4 = ESTM4 / (dist3^2);
radsurfTMS3b5 = ESTM5 / (dist3^2);
radsurfTMS3b7 = ESTM7 / (dist3^2);

printf("Surface Radiance TM Band 1 in W/m^2µm (Scene 3): %12f\n", radsurfTMS3b1);
printf("Surface Radiance TM Band 2 in W/m^2µm (Scene 3): %12f\n", radsurfTMS3b2);
printf("Surface Radiance TM Band 3 in W/m^2µm (Scene 3): %12f\n", radsurfTMS3b3);
printf("Surface Radiance TM Band 4 in W/m^2µm (Scene 3): %12f\n", radsurfTMS3b4);
printf("Surface Radiance TM Band 5 in W/m^2µm (Scene 3): %12f\n", radsurfTMS3b5);
printf("Surface Radiance TM Band 7 in W/m^2µm (Scene 3): %12f\n\n", radsurfTMS3b7);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {       
		if (IsNull(RADS3TM1[i,j]) == 1)
		{
		dosS3TM1nullValCount = dosS3TM1nullValCount + 1;
		}
		if (IsNull(RADS3TM2[i,j]) == 1)
		{
		dosS3TM2nullValCount = dosS3TM2nullValCount + 1;
		}		 
		if (IsNull(RADS3TM3[i,j]) == 1)
		{
		dosS3TM3nullValCount = dosS3TM3nullValCount + 1;
		}		 
		if (IsNull(RADS3TM4[i,j]) == 1)
		{
		dosS3TM4nullValCount = dosS3TM4nullValCount + 1;
		}		 
		if (IsNull(RADS3TM5[i,j]) == 1)
		{
		dosS3TM5nullValCount = dosS3TM5nullValCount + 1;
		}		 
		if (IsNull(RADS3TM7[i,j]) == 1)
		{
		dosS3TM7nullValCount = dosS3TM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS3TM1realSize = (TMlins * TMcols) - dosS3TM1nullValCount;
numeric dosS3TM2realSize = (TMlins * TMcols) - dosS3TM2nullValCount;
numeric dosS3TM3realSize = (TMlins * TMcols) - dosS3TM3nullValCount;
numeric dosS3TM4realSize = (TMlins * TMcols) - dosS3TM4nullValCount;
numeric dosS3TM5realSize = (TMlins * TMcols) - dosS3TM5nullValCount;
numeric dosS3TM7realSize = (TMlins * TMcols) - dosS3TM7nullValCount;

array allValuesDosS3TM1[dosS3TM1realSize];
array allValuesDosS3TM2[dosS3TM2realSize];
array allValuesDosS3TM3[dosS3TM3realSize];
array allValuesDosS3TM4[dosS3TM4realSize];
array allValuesDosS3TM5[dosS3TM5realSize];
array allValuesDosS3TM7[dosS3TM7realSize];

numeric DNminCALCS3TM1 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM1realSize);
numeric DNminCALCS3TM2 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM2realSize);
numeric DNminCALCS3TM3 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM3realSize);
numeric DNminCALCS3TM4 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM4realSize);
numeric DNminCALCS3TM5 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM5realSize);
numeric DNminCALCS3TM7 = DNminCALC(Scene3Lin, Scene3Col, dosS3TM7realSize);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(RADS3TM1[i,j]) == 0)
		{
		  allValuesDosS3TM1[dosS3TM1realArrayCount] = RADS3TM1[i,j];		 
		  dosS3TM1realArrayCount = dosS3TM1realArrayCount + 1;
		}
		if (IsNull(RADS3TM2[i,j]) == 0)
		{
		  allValuesDosS3TM2[dosS3TM2realArrayCount] = RADS3TM2[i,j];		 
		  dosS3TM2realArrayCount = dosS3TM2realArrayCount + 1;
		}
		if (IsNull(RADS3TM3[i,j]) == 0)
		{
		  allValuesDosS3TM3[dosS3TM3realArrayCount] = RADS3TM3[i,j];		 
		  dosS3TM3realArrayCount = dosS3TM3realArrayCount + 1;
		}
		if (IsNull(RADS3TM4[i,j]) == 0)
		{
		  allValuesDosS3TM4[dosS3TM4realArrayCount] = RADS3TM4[i,j];		 
		  dosS3TM4realArrayCount = dosS3TM4realArrayCount + 1;
		}
		if (IsNull(RADS3TM5[i,j]) == 0)
		{
		  allValuesDosS3TM5[dosS3TM5realArrayCount] = RADS3TM5[i,j];		 
		  dosS3TM5realArrayCount = dosS3TM5realArrayCount + 1;
		}
		if (IsNull(RADS3TM7[i,j]) == 0)
		{
		  allValuesDosS3TM7[dosS3TM7realArrayCount] = RADS3TM7[i,j];		 
		  dosS3TM7realArrayCount = dosS3TM7realArrayCount + 1;
		}
    }
}

########################### Scene 3 - Dn1000 - Band 1

numeric lastDOSS3TM1 = dosS3TM1realSize;
numeric hDOSS3TM1 = 1;

while ( (hDOSS3TM1 * 3 + 1) < lastDOSS3TM1 - 1 )
{
    hDOSS3TM1 = 3 * hDOSS3TM1 + 1;
}

while ( hDOSS3TM1 > 0 )
{
    for i = hDOSS3TM1 - 1 to lastDOSS3TM1               # for each of the h sets of elements
    {
        numeric keyDOSS3TM1 = allValuesDosS3TM1[i];
        numeric jDOSS3TM1 = i;

        while (jDOSS3TM1 >= hDOSS3TM1 && allValuesDosS3TM1[jDOSS3TM1 - hDOSS3TM1] > keyDOSS3TM1)
        {
            allValuesDosS3TM1[jDOSS3TM1] = allValuesDosS3TM1[jDOSS3TM1 - hDOSS3TM1];
            jDOSS3TM1 = jDOSS3TM1 - hDOSS3TM1;
        }
        allValuesDosS3TM1[jDOSS3TM1] = keyDOSS3TM1;
    }
    hDOSS3TM1 = floor(hDOSS3TM1/3);
}

numeric minValueDosS3TM1 = allValuesDosS3TM1[1];
numeric dn1000S3TM1 = 10000;

numeric actualCountDosS3TM1 = 0;
for i = 1 to dosS3TM1realSize 
{
    if (allValuesDosS3TM1[i] == minValueDosS3TM1)
    {
        actualCountDosS3TM1 = actualCountDosS3TM1 + 1;
        if (actualCountDosS3TM1 >= DNminCALCS3TM1)
        {
            dn1000S3TM1 = minValueDosS3TM1;
            go to dn1000S3TM1jumper;
        }
    }
    else
    {
        minValueDosS3TM1 = allValuesDosS3TM1[i+1];
        actualCountDosS3TM1 = 0;
    }
}

dn1000S3TM1jumper:

if (dn1000S3TM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM1 = allValuesDosS3TM1[1];
}
if (dn1000S3TM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM1 = allValuesDosS3TM1[1];
}
	
printf("dn1000 Scene 3 TM1: %12f\n", dn1000S3TM1);

########################### Scene 3 - Dn1000 - Band 2

numeric lastDOSS3TM2 = dosS3TM1realSize;
numeric hDOSS3TM2 = 1;

while ( (hDOSS3TM2 * 3 + 1) < lastDOSS3TM2 - 1 )
{
    hDOSS3TM2 = 3 * hDOSS3TM2 + 1;
}

while ( hDOSS3TM2 > 0 )
{
    for i = hDOSS3TM2 - 1 to lastDOSS3TM2
    {
        numeric keyDOSS3TM2 = allValuesDosS3TM2[i];
        numeric jDOSS3TM2 = i;

        while (jDOSS3TM2 >= hDOSS3TM2 && allValuesDosS3TM2[jDOSS3TM2 - hDOSS3TM2] > keyDOSS3TM2)
        {
            allValuesDosS3TM2[jDOSS3TM2] = allValuesDosS3TM2[jDOSS3TM2 - hDOSS3TM2];
            jDOSS3TM2 = jDOSS3TM2 - hDOSS3TM2;
        }
        allValuesDosS3TM2[jDOSS3TM2] = keyDOSS3TM2;
    }
    hDOSS3TM2 = floor(hDOSS3TM2/3);
}

numeric minValueDosS3TM2 = allValuesDosS3TM2[1];
numeric dn1000S3TM2 = 10000;

numeric actualCountDosS3TM2 = 0;
for i = 1 to dosS3TM2realSize 
{
    if (allValuesDosS3TM2[i] == minValueDosS3TM2)
    {
        actualCountDosS3TM2 = actualCountDosS3TM2 + 1;
        if (actualCountDosS3TM2 >= DNminCALCS3TM2)
        {
            dn1000S3TM2 = minValueDosS3TM2;
            go to dn1000S3TM2jumper;
        }
    }
    else
    {
        minValueDosS3TM2 = allValuesDosS3TM2[i+1];
        actualCountDosS3TM2 = 0;
    }
}

dn1000S3TM2jumper:

if (dn1000S3TM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM2 = allValuesDosS3TM2[1];
}
if (dn1000S3TM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM2 = allValuesDosS3TM2[1];
}

printf("dn1000 Scene 3 TM2: %12f\n", dn1000S3TM2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS3TM3 = dosS3TM3realSize;
numeric hDOSS3TM3 = 1;

while ( (hDOSS3TM3 * 3 + 1) < lastDOSS3TM3 - 1 )
{
    hDOSS3TM3 = 3 * hDOSS3TM3 + 1;
}

while ( hDOSS3TM3 > 0 )
{
    for i = hDOSS3TM3 - 1 to lastDOSS3TM3               # for each of the h sets of elements
    {
        numeric keyDOSS3TM3 = allValuesDosS3TM3[i];
        numeric jDOSS3TM3 = i;

        while (jDOSS3TM3 >= hDOSS3TM3 && allValuesDosS3TM3[jDOSS3TM3 - hDOSS3TM3] > keyDOSS3TM3)
        {
            allValuesDosS3TM3[jDOSS3TM3] = allValuesDosS3TM3[jDOSS3TM3 - hDOSS3TM3];
            jDOSS3TM3 = jDOSS3TM3 - hDOSS3TM3;
        }
        allValuesDosS3TM3[jDOSS3TM3] = keyDOSS3TM3;
    }
    hDOSS3TM3 = floor(hDOSS3TM3/3);
}

numeric minValueDosS3TM3 = allValuesDosS3TM3[1];
numeric dn1000S3TM3 = 10000;

numeric actualCountDosS3TM3 = 0;
for i = 1 to dosS3TM3realSize 
{
    if (allValuesDosS3TM3[i] == minValueDosS3TM3)
    {
        actualCountDosS3TM3 = actualCountDosS3TM3 + 1;
        if (actualCountDosS3TM3 >= DNminCALCS3TM3)
        {
            dn1000S3TM3 = minValueDosS3TM3;
            go to dn1000S3TM3jumper;
        }
    }
    else
    {
        minValueDosS3TM3 = allValuesDosS3TM3[i+1];
        actualCountDosS3TM3 = 0;
    }
}

dn1000S3TM3jumper:

if (dn1000S3TM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM3 = allValuesDosS3TM3[1];
}
if (dn1000S3TM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM3 = allValuesDosS3TM3[1];
}
	
printf("dn1000 Scene 3 TM3: %12f\n", dn1000S3TM3);

########################### Scene 3 - Dn1000 - Band 4

numeric lastDOSS3TM4 = dosS3TM4realSize;
numeric hDOSS3TM4 = 1;

while ( (hDOSS3TM4 * 3 + 1) < lastDOSS3TM4 - 1 )
{
    hDOSS3TM4 = 3 * hDOSS3TM4 + 1;
}

while ( hDOSS3TM4 > 0 )
{
    for i = hDOSS3TM4 - 1 to lastDOSS3TM4
    {
        numeric keyDOSS3TM4 = allValuesDosS3TM4[i];
        numeric jDOSS3TM4 = i;

        while (jDOSS3TM4 >= hDOSS3TM4 && allValuesDosS3TM4[jDOSS3TM4 - hDOSS3TM4] > keyDOSS3TM4)
        {
            allValuesDosS3TM4[jDOSS3TM4] = allValuesDosS3TM4[jDOSS3TM4 - hDOSS3TM4];
            jDOSS3TM4 = jDOSS3TM4 - hDOSS3TM4;
        }
        allValuesDosS3TM4[jDOSS3TM4] = keyDOSS3TM4;
    }
    hDOSS3TM4 = floor(hDOSS3TM4/3);
}

numeric minValueDosS3TM4 = allValuesDosS3TM4[1];
numeric dn1000S3TM4 = 10000;

numeric actualCountDosS3TM4 = 0;
for i = 1 to dosS3TM4realSize 
{
    if (allValuesDosS3TM4[i] == minValueDosS3TM4)
    {
        actualCountDosS3TM4 = actualCountDosS3TM4 + 1;
        if (actualCountDosS3TM4 >= DNminCALCS3TM4)
        {
            dn1000S3TM4 = minValueDosS3TM4;
            go to dn1000S3TM4jumper;
        }
    }
    else
    {
        minValueDosS3TM4 = allValuesDosS3TM4[i+1];
        actualCountDosS3TM4 = 0;
    }
}

dn1000S3TM4jumper:

if (dn1000S3TM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM4 = allValuesDosS3TM4[1];
}
if (dn1000S3TM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM4 = allValuesDosS3TM4[1];
}

printf("dn1000 Scene 3 TM4: %12f\n", dn1000S3TM4);

########################### Scene 3 - Dn1000 - Band 5

numeric lastDOSS3TM5 = dosS3TM5realSize;
numeric hDOSS3TM5 = 1;

while ( (hDOSS3TM5 * 3 + 1) < lastDOSS3TM5 - 1 )
{
    hDOSS3TM5 = 3 * hDOSS3TM5 + 1;
}

while ( hDOSS3TM5 > 0 )
{
    for i = hDOSS3TM5 - 1 to lastDOSS3TM5               # for each of the h sets of elements
    {
        numeric keyDOSS3TM5 = allValuesDosS3TM5[i];
        numeric jDOSS3TM5 = i;

        while (jDOSS3TM5 >= hDOSS3TM5 && allValuesDosS3TM5[jDOSS3TM5 - hDOSS3TM5] > keyDOSS3TM5)
        {
            allValuesDosS3TM5[jDOSS3TM5] = allValuesDosS3TM5[jDOSS3TM5 - hDOSS3TM5];
            jDOSS3TM5 = jDOSS3TM5 - hDOSS3TM5;
        }
        allValuesDosS3TM5[jDOSS3TM5] = keyDOSS3TM5;
    }
    hDOSS3TM5 = floor(hDOSS3TM5/3);
}

numeric minValueDosS3TM5 = allValuesDosS3TM5[1];
numeric dn1000S3TM5 = 10000;

numeric actualCountDosS3TM5 = 0;
for i = 1 to dosS3TM5realSize 
{
    if (allValuesDosS3TM5[i] == minValueDosS3TM5)
    {
        actualCountDosS3TM5 = actualCountDosS3TM5 + 1;
        if (actualCountDosS3TM5 >= DNminCALCS3TM5)
        {
            dn1000S3TM5 = minValueDosS3TM5;
            go to dn1000S3TM5jumper;
        }
    }
    else
    {
        minValueDosS3TM5 = allValuesDosS3TM5[i+1];
        actualCountDosS3TM5 = 0;
    }
}

dn1000S3TM5jumper:

if (dn1000S3TM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM5 = allValuesDosS3TM5[1];
}
if (dn1000S3TM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM5 = allValuesDosS3TM5[1];
}
	
printf("dn1000 Scene 3 TM5: %12f\n", dn1000S3TM5);

########################### Scene 3 - Dn1000 - Band 7

numeric lastDOSS3TM7 = dosS3TM7realSize;
numeric hDOSS3TM7 = 1;

while ( (hDOSS3TM7 * 3 + 1) < lastDOSS3TM7 - 1 )
{
    hDOSS3TM7 = 3 * hDOSS3TM7 + 1;
}

while ( hDOSS3TM7 > 0 )
{
    for i = hDOSS3TM7 - 1 to lastDOSS3TM7               # for each of the h sets of elements
    {
        numeric keyDOSS3TM7 = allValuesDosS3TM7[i];
        numeric jDOSS3TM7 = i;

        while (jDOSS3TM7 >= hDOSS3TM7 && allValuesDosS3TM7[jDOSS3TM7 - hDOSS3TM7] > keyDOSS3TM7)
        {
            allValuesDosS3TM7[jDOSS3TM7] = allValuesDosS3TM7[jDOSS3TM7 - hDOSS3TM7];
            jDOSS3TM7 = jDOSS3TM7 - hDOSS3TM7;
        }
        allValuesDosS3TM7[jDOSS3TM7] = keyDOSS3TM7;
    }
    hDOSS3TM7 = floor(hDOSS3TM7/3);
}

numeric minValueDosS3TM7 = allValuesDosS3TM7[1];
numeric dn1000S3TM7 = 10000;

numeric actualCountDosS3TM7 = 0;
for i = 1 to dosS3TM7realSize 
{
    if (allValuesDosS3TM7[i] == minValueDosS3TM7)
    {
        actualCountDosS3TM7 = actualCountDosS3TM7 + 1;
        if (actualCountDosS3TM7 >= DNminCALCS3TM7)
        {
            dn1000S3TM7 = minValueDosS3TM7;
            go to dn1000S3TM7jumper;
        }
    }
    else
    {
        minValueDosS3TM7 = allValuesDosS3TM7[i+1];
        actualCountDosS3TM7 = 0;
    }
}

dn1000S3TM7jumper:

if (dn1000S3TM7  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3TM7  = allValuesDosS3TM7[1];
}
if (dn1000S3TM7  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3TM7  = allValuesDosS3TM7[1];
}
	
printf("dn1000 Scene 3 TM7: %12f\n\n", dn1000S3TM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS3TM1 = dn1000S3TM1 - (0.01 * (radsurfTMS3b1 * cos(s3zenith)) / pi);
numeric pathRadS3TM2 = dn1000S3TM2 - (0.01 * (radsurfTMS3b2 * cos(s3zenith)) / pi);
numeric pathRadS3TM3 = dn1000S3TM3 - (0.01 * (radsurfTMS3b3 * cos(s3zenith)) / pi);
numeric pathRadS3TM4 = dn1000S3TM4 - (0.01 * (radsurfTMS3b4 * cos(s3zenith)) / pi);
numeric pathRadS3TM5 = dn1000S3TM5 - (0.01 * (radsurfTMS3b5 * cos(s3zenith)) / pi);
numeric pathRadS3TM7 = dn1000S3TM7 - (0.01 * (radsurfTMS3b7 * cos(s3zenith)) / pi);

printf("Pathradiance Scene 3 Band 1: %12f\n", pathRadS3TM1);
printf("Pathradiance Scene 3 Band 2: %12f\n", pathRadS3TM2);
printf("Pathradiance Scene 3 Band 3: %12f\n", pathRadS3TM3);
printf("Pathradiance Scene 3 Band 4: %12f\n", pathRadS3TM4);
printf("Pathradiance Scene 3 Band 5: %12f\n", pathRadS3TM5);
printf("Pathradiance Scene 3 Band 7: %12f\n\n", pathRadS3TM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to TMlins
	 {
	        for j = 1 to TMcols
	        {
                  valueS3TM1 = (pi * (RADS3TM1[i,j] - pathRadS3TM1)) / (radsurfTMS3b1 * cos(s3zenith));

                  if ( valueS3TM1 < 0 )
       		{
          			valueS3TM1 = 0;
				countS3REFnullTM1 = countS3REFnullTM1 + 1;
       		}

			else if ( valueS3TM1 > 1 )
			{
				valueS3TM1 = 1;
				countS3REFoneTM1 = countS3REFoneTM1 + 1;
			}
       		
			REFS3TM1[i,j] = valueS3TM1;

               valueS3TM2 = (pi * (RADS3TM2[i,j] - pathRadS3TM2)) / (radsurfTMS3b2 * cos(s3zenith));

                  if ( valueS3TM2 < 0 )
       		{
          			valueS3TM2 = 0;
				countS3REFnullTM2 = countS3REFnullTM2 + 1;
       		}

			else if ( valueS3TM2 > 1 )
			{
				valueS3TM2 = 1;
				countS3REFoneTM2 = countS3REFoneTM2 + 1;
			}
       		
			REFS3TM2[i,j] = valueS3TM2;

               valueS3TM3 = (pi * (RADS3TM3[i,j] - pathRadS3TM3)) / (radsurfTMS3b3 * cos(s3zenith));

                  if ( valueS3TM3 < 0 )
       		{
          			valueS3TM3 = 0;
				countS3REFnullTM3 = countS3REFnullTM3 + 1;
       		}

			else if ( valueS3TM3 > 1 )
			{
				valueS3TM3 = 1;
				countS3REFoneTM3 = countS3REFoneTM3 + 1;
			}
       		
			REFS3TM3[i,j] = valueS3TM3;

               valueS3TM4 = (pi * (RADS3TM4[i,j] - pathRadS3TM4)) / (radsurfTMS3b4 * cos(s3zenith));

                  if ( valueS3TM4 < 0 )
       		{
          			valueS3TM4 = 0;
				countS3REFnullTM4 = countS3REFnullTM4 + 1;
       		}

			else if ( valueS3TM4 > 1 )
			{
				valueS3TM4 = 1;
				countS3REFoneTM4 = countS3REFoneTM4 + 1;
			}
       		
			REFS3TM4[i,j] = valueS3TM4;

               valueS3TM5 = (pi * (RADS3TM5[i,j] - pathRadS3TM5)) / (radsurfTMS3b5 * cos(s3zenith));

                  if ( valueS3TM5 < 0 )
       		{
          			valueS3TM5 = 0;
				countS3REFnullTM5 = countS3REFnullTM5 + 1;
       		}

			else if ( valueS3TM5 > 1 )
			{
				valueS3TM5 = 1;
				countS3REFoneTM5 = countS3REFoneTM5 + 1;
			}
       		
			REFS3TM5[i,j] = valueS3TM5;

               valueS3TM7 = (pi * (RADS3TM7[i,j] - pathRadS3TM7)) / (radsurfTMS3b7 * cos(s3zenith));

                  if ( valueS3TM7 < 0 )
       		{
          			valueS3TM7 = 0;
				countS3REFnullTM7 = countS3REFnullTM7 + 1;
       		}

			else if ( valueS3TM7 > 1 )
			{
				valueS3TM7 = 1;
				countS3REFoneTM7 = countS3REFoneTM7 + 1;
			}
       		
			REFS3TM7[i,j] = valueS3TM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullTM1, countS3REFoneTM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullTM2, countS3REFoneTM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullTM3, countS3REFoneTM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullTM4, countS3REFoneTM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullTM5, countS3REFoneTM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS3REFnullTM7, countS3REFoneTM7);

CreatePyramid(REFS3TM1);
CreatePyramid(REFS3TM2);
CreatePyramid(REFS3TM3);
CreatePyramid(REFS3TM4);
CreatePyramid(REFS3TM5);
CreatePyramid(REFS3TM7);
        
CreateHistogram(REFS3TM1);
CreateHistogram(REFS3TM2);
CreateHistogram(REFS3TM3);
CreateHistogram(REFS3TM4);
CreateHistogram(REFS3TM5);
CreateHistogram(REFS3TM7);

CloseRaster(RADS3TM1);
CloseRaster(RADS3TM2);
CloseRaster(RADS3TM3);
CloseRaster(RADS3TM4);
CloseRaster(RADS3TM5);
CloseRaster(RADS3TM7);

printf("Reflectance calculation for Scene 3 (TM) is done...\n\n\n");
}

else if ( sensors3 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
raster REFS3MSS1, REFS3MSS2, REFS3MSS3, REFS3MSS4;
numeric radsurfMSS3b1, radsurfMSS3b2, radsurfMSS3b3, radsurfMSS3b4;
numeric valueS3MSS1, valueS3MSS2, valueS3MSS3, valueS3MSS4;

numeric dosS3MSS1nullValCount = 0;
numeric dosS3MSS2nullValCount = 0;
numeric dosS3MSS3nullValCount = 0;
numeric dosS3MSS4nullValCount = 0;

numeric dosS3MSS1realArrayCount = 1;
numeric dosS3MSS2realArrayCount = 1;
numeric dosS3MSS3realArrayCount = 1;
numeric dosS3MSS4realArrayCount = 1;

numeric countS3REFnullMSS1 = 0;
numeric countS3REFnullMSS2 = 0;
numeric countS3REFnullMSS3 = 0;
numeric countS3REFnullMSS4 = 0;

numeric countS3REFoneMSS1 = 0;
numeric countS3REFoneMSS2 = 0;
numeric countS3REFoneMSS3 = 0;
numeric countS3REFoneMSS4 = 0;

numeric countnoDOSS3REFnullMSS1 = 0;
numeric countnoDOSS3REFnullMSS2 = 0;
numeric countnoDOSS3REFnullMSS3 = 0;
numeric countnoDOSS3REFnullMSS4 = 0;

numeric countnoDOSS3REFoneMSS1 = 0;
numeric countnoDOSS3REFoneMSS2 = 0;
numeric countnoDOSS3REFoneMSS3 = 0;
numeric countnoDOSS3REFoneMSS4 = 0;
	          
radsurfMSS3b1 = ESMSS51 / (dist3^2);
radsurfMSS3b2 = ESMSS52 / (dist3^2);
radsurfMSS3b3 = ESMSS53 / (dist3^2);
radsurfMSS3b4 = ESMSS54 / (dist3^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 3): %12f\n\n", radsurfMSS3b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS3MSS1[i,j]) == 1)
		{
		dosS3MSS1nullValCount = dosS3MSS1nullValCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 1)
		{
		dosS3MSS2nullValCount = dosS3MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS3[i,j]) == 1)
		{
		dosS3MSS3nullValCount = dosS3MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS4[i,j]) == 1)
		{
		dosS3MSS4nullValCount = dosS3MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS3MSS1realSize = (MSSlins * MSScols) - dosS3MSS1nullValCount;
numeric dosS3MSS2realSize = (MSSlins * MSScols) - dosS3MSS2nullValCount;
numeric dosS3MSS3realSize = (MSSlins * MSScols) - dosS3MSS3nullValCount;
numeric dosS3MSS4realSize = (MSSlins * MSScols) - dosS3MSS4nullValCount;

array allValuesDosS3MSS1[dosS3MSS1realSize];
array allValuesDosS3MSS2[dosS3MSS2realSize];
array allValuesDosS3MSS3[dosS3MSS3realSize];
array allValuesDosS3MSS4[dosS3MSS4realSize];

numeric DNminCALCS3MSS1 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS1realSize);
numeric DNminCALCS3MSS2 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS2realSize);
numeric DNminCALCS3MSS3 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS3realSize);
numeric DNminCALCS3MSS4 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS3MSS1[i,j]) == 0)
		{
		  allValuesDosS3MSS1[dosS3MSS1realArrayCount] = RADS3MSS1[i,j];		 
		  dosS3MSS1realArrayCount = dosS3MSS1realArrayCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 0)
		{
		  allValuesDosS3MSS2[dosS3MSS2realArrayCount] = RADS3MSS2[i,j];		 
		  dosS3MSS2realArrayCount = dosS3MSS2realArrayCount + 1;
		}
		if (IsNull(RADS3MSS3[i,j]) == 0)
		{
		  allValuesDosS3MSS3[dosS3MSS3realArrayCount] = RADS3MSS3[i,j];		 
		  dosS3MSS3realArrayCount = dosS3MSS3realArrayCount + 1;
		}
		if (IsNull(RADS3MSS4[i,j]) == 0)
		{
		  allValuesDosS3MSS4[dosS3MSS4realArrayCount] = RADS3MSS4[i,j];		 
		  dosS3MSS4realArrayCount = dosS3MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 3 - Dn1000 - Band 1

numeric lastDOSS3MSS1 = dosS3MSS1realSize;
numeric hDOSS3MSS1 = 1;

while ( (hDOSS3MSS1 * 3 + 1) < lastDOSS3MSS1 - 1 )
{
    hDOSS3MSS1 = 3 * hDOSS3MSS1 + 1;
}

while ( hDOSS3MSS1 > 0 )
{
    for i = hDOSS3MSS1 - 1 to lastDOSS3MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS1 = allValuesDosS3MSS1[i];
        numeric jDOSS3MSS1 = i;

        while (jDOSS3MSS1 >= hDOSS3MSS1 && allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1] > keyDOSS3MSS1)
        {
            allValuesDosS3MSS1[jDOSS3MSS1] = allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1];
            jDOSS3MSS1 = jDOSS3MSS1 - hDOSS3MSS1;
        }
        allValuesDosS3MSS1[jDOSS3MSS1] = keyDOSS3MSS1;
    }
    hDOSS3MSS1 = floor(hDOSS3MSS1/3);
}

numeric minValueDosS3MSS1 = allValuesDosS3MSS1[1];
numeric dn1000S3MSS1 = 10000;

numeric actualCountDosS3MSS1 = 0;
for i = 1 to dosS3MSS1realSize 
{
    if (allValuesDosS3MSS1[i] == minValueDosS3MSS1)
    {
        actualCountDosS3MSS1 = actualCountDosS3MSS1 + 1;
        if (actualCountDosS3MSS1 >= DNminCALCS3MSS1)
        {
            dn1000S3MSS1 = minValueDosS3MSS1;
            go to dn1000S3MSS1jumper;
        }
    }
    else
    {
        minValueDosS3MSS1 = allValuesDosS3MSS1[i+1];
        actualCountDosS3MSS1 = 0;
    }
}

dn1000S3MSS1jumper:

if (dn1000S3MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}
if (dn1000S3MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}
	
printf("dn1000 Scene 3 MSS1: %12f\n", dn1000S3MSS1);

########################### Scene 3 - Dn1000 - Band 2

numeric lastDOSS3MSS2 = dosS3MSS1realSize;
numeric hDOSS3MSS2 = 1;

while ( (hDOSS3MSS2 * 3 + 1) < lastDOSS3MSS2 - 1 )
{
    hDOSS3MSS2 = 3 * hDOSS3MSS2 + 1;
}

while ( hDOSS3MSS2 > 0 )
{
    for i = hDOSS3MSS2 - 1 to lastDOSS3MSS2
    {
        numeric keyDOSS3MSS2 = allValuesDosS3MSS2[i];
        numeric jDOSS3MSS2 = i;

        while (jDOSS3MSS2 >= hDOSS3MSS2 && allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2] > keyDOSS3MSS2)
        {
            allValuesDosS3MSS2[jDOSS3MSS2] = allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2];
            jDOSS3MSS2 = jDOSS3MSS2 - hDOSS3MSS2;
        }
        allValuesDosS3MSS2[jDOSS3MSS2] = keyDOSS3MSS2;
    }
    hDOSS3MSS2 = floor(hDOSS3MSS2/3);
}

numeric minValueDosS3MSS2 = allValuesDosS3MSS2[1];
numeric dn1000S3MSS2 = 10000;

numeric actualCountDosS3MSS2 = 0;
for i = 1 to dosS3MSS2realSize 
{
    if (allValuesDosS3MSS2[i] == minValueDosS3MSS2)
    {
        actualCountDosS3MSS2 = actualCountDosS3MSS2 + 1;
        if (actualCountDosS3MSS2 >= DNminCALCS3MSS2)
        {
            dn1000S3MSS2 = minValueDosS3MSS2;
            go to dn1000S3MSS2jumper;
        }
    }
    else
    {
        minValueDosS3MSS2 = allValuesDosS3MSS2[i+1];
        actualCountDosS3MSS2 = 0;
    }
}

dn1000S3MSS2jumper:

if (dn1000S3MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}
if (dn1000S3MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}

printf("dn1000 Scene 3 MSS2: %12f\n", dn1000S3MSS2);

########################### Scene 3 - Dn1000 - Band 3

numeric lastDOSS3MSS3 = dosS3MSS3realSize;
numeric hDOSS3MSS3 = 1;

while ( (hDOSS3MSS3 * 3 + 1) < lastDOSS3MSS3 - 1 )
{
    hDOSS3MSS3 = 3 * hDOSS3MSS3 + 1;
}

while ( hDOSS3MSS3 > 0 )
{
    for i = hDOSS3MSS3 - 1 to lastDOSS3MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS3 = allValuesDosS3MSS3[i];
        numeric jDOSS3MSS3 = i;

        while (jDOSS3MSS3 >= hDOSS3MSS3 && allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3] > keyDOSS3MSS3)
        {
            allValuesDosS3MSS3[jDOSS3MSS3] = allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3];
            jDOSS3MSS3 = jDOSS3MSS3 - hDOSS3MSS3;
        }
        allValuesDosS3MSS3[jDOSS3MSS3] = keyDOSS3MSS3;
    }
    hDOSS3MSS3 = floor(hDOSS3MSS3/3);
}

numeric minValueDosS3MSS3 = allValuesDosS3MSS3[1];
numeric dn1000S3MSS3 = 10000;

numeric actualCountDosS3MSS3 = 0;
for i = 1 to dosS3MSS3realSize 
{
    if (allValuesDosS3MSS3[i] == minValueDosS3MSS3)
    {
        actualCountDosS3MSS3 = actualCountDosS3MSS3 + 1;
        if (actualCountDosS3MSS3 >= DNminCALCS3MSS3)
        {
            dn1000S3MSS3 = minValueDosS3MSS3;
            go to dn1000S3MSS3jumper;
        }
    }
    else
    {
        minValueDosS3MSS3 = allValuesDosS3MSS3[i+1];
        actualCountDosS3MSS3 = 0;
    }
}

dn1000S3MSS3jumper:

if (dn1000S3MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}
if (dn1000S3MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}

printf("dn1000 Scene 3 MSS3: %12f\n", dn1000S3MSS3);

########################### Scene 3 - Dn1000 - Band 4

numeric lastDOSS3MSS4 = dosS3MSS4realSize;
numeric hDOSS3MSS4 = 1;

while ( (hDOSS3MSS4 * 3 + 1) < lastDOSS3MSS4 - 1 )
{
    hDOSS3MSS4 = 3 * hDOSS3MSS4 + 1;
}

while ( hDOSS3MSS4 > 0 )
{
    for i = hDOSS3MSS4 - 1 to lastDOSS3MSS4
    {
        numeric keyDOSS3MSS4 = allValuesDosS3MSS4[i];
        numeric jDOSS3MSS4 = i;

        while (jDOSS3MSS4 >= hDOSS3MSS4 && allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4] > keyDOSS3MSS4)
        {
            allValuesDosS3MSS4[jDOSS3MSS4] = allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4];
            jDOSS3MSS4 = jDOSS3MSS4 - hDOSS3MSS4;
        }
        allValuesDosS3MSS4[jDOSS3MSS4] = keyDOSS3MSS4;
    }
    hDOSS3MSS4 = floor(hDOSS3MSS4/3);
}

numeric minValueDosS3MSS4 = allValuesDosS3MSS4[1];
numeric dn1000S3MSS4 = 10000;

numeric actualCountDosS3MSS4 = 0;
for i = 1 to dosS3MSS4realSize 
{
    if (allValuesDosS3MSS4[i] == minValueDosS3MSS4)
    {
        actualCountDosS3MSS4 = actualCountDosS3MSS4 + 1;
        if (actualCountDosS3MSS4 >= DNminCALCS3MSS1)
        {
            dn1000S3MSS4 = minValueDosS3MSS4;
            go to dn1000S3MSS4jumper;
        }
    }
    else
    {
        minValueDosS3MSS4 = allValuesDosS3MSS4[i+1];
        actualCountDosS3MSS4 = 0;
    }
}

dn1000S3MSS4jumper:

if (dn1000S3MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}
if (dn1000S3MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}
	
printf("dn1000 Scene 3 MSS4: %12f\n", dn1000S3MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS3MSS1 = dn1000S3MSS1 - (0.01 * (radsurfMSS3b1 * cos(s3zenith)) / pi);
numeric pathRadS3MSS2 = dn1000S3MSS2 - (0.01 * (radsurfMSS3b2 * cos(s3zenith)) / pi);
numeric pathRadS3MSS3 = dn1000S3MSS3 - (0.01 * (radsurfMSS3b3 * cos(s3zenith)) / pi);
numeric pathRadS3MSS4 = dn1000S3MSS4 - (0.01 * (radsurfMSS3b4 * cos(s3zenith)) / pi);

printf("Pathradiance Scene 3 Band 1: %12f\n", pathRadS3MSS1);
printf("Pathradiance Scene 3 Band 2: %12f\n", pathRadS3MSS2);
printf("Pathradiance Scene 3 Band 3: %12f\n", pathRadS3MSS3);
printf("Pathradiance Scene 3 Band 4: %12f\n\n", pathRadS3MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS3MSS1 = (pi * (RADS3MSS1[i,j] - pathRadS3MSS1)) / (radsurfMSS3b1 * cos(s3zenith));

                  if ( valueS3MSS1 < 0 )
       		{
          			valueS3MSS1 = 0;
				countS3REFnullMSS1 = countS3REFnullMSS1 + 1;
       		}

			else if ( valueS3MSS1 > 1 )
			{
				valueS3MSS1 = 1;
				countS3REFoneMSS1 = countS3REFoneMSS1 + 1;
			}
       		
			REFS3MSS1[i,j] = valueS3MSS1;

               valueS3MSS2 = (pi * (RADS3MSS2[i,j] - pathRadS3MSS2)) / (radsurfMSS3b2 * cos(s3zenith));

                  if ( valueS3MSS2 < 0 )
       		{
          			valueS3MSS2 = 0;
				countS3REFnullMSS2 = countS3REFnullMSS2 + 1;
       		}

			else if ( valueS3MSS2 > 1 )
			{
				valueS3MSS2 = 1;
				countS3REFoneMSS2 = countS3REFoneMSS2 + 1;
			}
       		
			REFS3MSS2[i,j] = valueS3MSS2;

               valueS3MSS3 = (pi * (RADS3MSS3[i,j] - pathRadS3MSS3)) / (radsurfMSS3b3 * cos(s3zenith));

                  if ( valueS3MSS3 < 0 )
       		{
          			valueS3MSS3 = 0;
				countS3REFnullMSS3 = countS3REFnullMSS3 + 1;
       		}

			else if ( valueS3MSS3 > 1 )
			{
				valueS3MSS3 = 1;
				countS3REFoneMSS3 = countS3REFoneMSS3 + 1;
			}
       		
			REFS3MSS3[i,j] = valueS3MSS3;

               valueS3MSS4 = (pi * (RADS3MSS4[i,j] - pathRadS3MSS4)) / (radsurfMSS3b4 * cos(s3zenith));

                  if ( valueS3MSS4 < 0 )
       		{
          			valueS3MSS4 = 0;
				countS3REFnullMSS4 = countS3REFnullMSS4 + 1;
       		}

			else if ( valueS3MSS4 > 1 )
			{
				valueS3MSS4 = 1;
				countS3REFoneMSS4 = countS3REFoneMSS4 + 1;
			}
       		
			REFS3MSS4[i,j] = valueS3MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS1, countS3REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS2, countS3REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS3, countS3REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS4, countS3REFoneMSS4);

CreatePyramid(REFS3MSS1);
CreatePyramid(REFS3MSS2);
CreatePyramid(REFS3MSS3);
CreatePyramid(REFS3MSS4);
        
CreateHistogram(REFS3MSS1);
CreateHistogram(REFS3MSS2);
CreateHistogram(REFS3MSS3);
CreateHistogram(REFS3MSS4);

CloseRaster(RADS3MSS1);
CloseRaster(RADS3MSS2);
CloseRaster(RADS3MSS3);
CloseRaster(RADS3MSS4);

printf("Reflectance calculation for Scene 3 (MSS) is done...\n\n\n");
}

else if ( sensors3 == 4 )                                               # MSS 4
{
raster REFS3MSS1, REFS3MSS2, REFS3MSS3, REFS3MSS4;
numeric radsurfMSS3b1, radsurfMSS3b2, radsurfMSS3b3, radsurfMSS3b4;
numeric valueS3MSS1, valueS3MSS2, valueS3MSS3, valueS3MSS4;

numeric dosS3MSS1nullValCount = 0;
numeric dosS3MSS2nullValCount = 0;
numeric dosS3MSS3nullValCount = 0;
numeric dosS3MSS4nullValCount = 0;

numeric dosS3MSS1realArrayCount = 1;
numeric dosS3MSS2realArrayCount = 1;
numeric dosS3MSS3realArrayCount = 1;
numeric dosS3MSS4realArrayCount = 1;

numeric countS3REFnullMSS1 = 0;
numeric countS3REFnullMSS2 = 0;
numeric countS3REFnullMSS3 = 0;
numeric countS3REFnullMSS4 = 0;

numeric countS3REFoneMSS1 = 0;
numeric countS3REFoneMSS2 = 0;
numeric countS3REFoneMSS3 = 0;
numeric countS3REFoneMSS4 = 0;

radsurfMSS3b1 = ESMSS41 / (dist3^2);
radsurfMSS3b2 = ESMSS42 / (dist3^2);
radsurfMSS3b3 = ESMSS43 / (dist3^2);
radsurfMSS3b4 = ESMSS44 / (dist3^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 3): %12f\n\n", radsurfMSS3b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS3MSS1[i,j]) == 1)
		{
		dosS3MSS1nullValCount = dosS3MSS1nullValCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 1)
		{
		dosS3MSS2nullValCount = dosS3MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS3[i,j]) == 1)
		{
		dosS3MSS3nullValCount = dosS3MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS4[i,j]) == 1)
		{
		dosS3MSS4nullValCount = dosS3MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS3MSS1realSize = (MSSlins * MSScols) - dosS3MSS1nullValCount;
numeric dosS3MSS2realSize = (MSSlins * MSScols) - dosS3MSS2nullValCount;
numeric dosS3MSS3realSize = (MSSlins * MSScols) - dosS3MSS3nullValCount;
numeric dosS3MSS4realSize = (MSSlins * MSScols) - dosS3MSS4nullValCount;

array allValuesDosS3MSS1[dosS3MSS1realSize];
array allValuesDosS3MSS2[dosS3MSS2realSize];
array allValuesDosS3MSS3[dosS3MSS3realSize];
array allValuesDosS3MSS4[dosS3MSS4realSize];

numeric DNminCALCS3MSS1 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS1realSize);
numeric DNminCALCS3MSS2 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS2realSize);
numeric DNminCALCS3MSS3 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS3realSize);
numeric DNminCALCS3MSS4 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS3MSS1[i,j]) == 0)
		{
		  allValuesDosS3MSS1[dosS3MSS1realArrayCount] = RADS3MSS1[i,j];		 
		  dosS3MSS1realArrayCount = dosS3MSS1realArrayCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 0)
		{
		  allValuesDosS3MSS2[dosS3MSS2realArrayCount] = RADS3MSS2[i,j];		 
		  dosS3MSS2realArrayCount = dosS3MSS2realArrayCount + 1;
		}
		if (IsNull(RADS3MSS3[i,j]) == 0)
		{
		  allValuesDosS3MSS3[dosS3MSS3realArrayCount] = RADS3MSS3[i,j];		 
		  dosS3MSS3realArrayCount = dosS3MSS3realArrayCount + 1;
		}
		if (IsNull(RADS3MSS4[i,j]) == 0)
		{
		  allValuesDosS3MSS4[dosS3MSS4realArrayCount] = RADS3MSS4[i,j];		 
		  dosS3MSS4realArrayCount = dosS3MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 3 - Dn1000 - Band 1

numeric lastDOSS3MSS1 = dosS3MSS1realSize;
numeric hDOSS3MSS1 = 1;

while ( (hDOSS3MSS1 * 3 + 1) < lastDOSS3MSS1 - 1 )
{
    hDOSS3MSS1 = 3 * hDOSS3MSS1 + 1;
}

while ( hDOSS3MSS1 > 0 )
{
    for i = hDOSS3MSS1 - 1 to lastDOSS3MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS1 = allValuesDosS3MSS1[i];
        numeric jDOSS3MSS1 = i;

        while (jDOSS3MSS1 >= hDOSS3MSS1 && allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1] > keyDOSS3MSS1)
        {
            allValuesDosS3MSS1[jDOSS3MSS1] = allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1];
            jDOSS3MSS1 = jDOSS3MSS1 - hDOSS3MSS1;
        }
        allValuesDosS3MSS1[jDOSS3MSS1] = keyDOSS3MSS1;
    }
    hDOSS3MSS1 = floor(hDOSS3MSS1/3);
}

numeric minValueDosS3MSS1 = allValuesDosS3MSS1[1];
numeric dn1000S3MSS1 = 10000;

numeric actualCountDosS3MSS1 = 0;
for i = 1 to dosS3MSS1realSize 
{
    if (allValuesDosS3MSS1[i] == minValueDosS3MSS1)
    {
        actualCountDosS3MSS1 = actualCountDosS3MSS1 + 1;
        if (actualCountDosS3MSS1 >= DNminCALCS3MSS1)
        {
            dn1000S3MSS1 = minValueDosS3MSS1;
            go to dn1000S3MSS1jumper;
        }
    }
    else
    {
        minValueDosS3MSS1 = allValuesDosS3MSS1[i+1];
        actualCountDosS3MSS1 = 0;
    }
}

dn1000S3MSS1jumper:

if (dn1000S3MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}
if (dn1000S3MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}

printf("dn1000 Scene 3 MSS1: %12f\n", dn1000S3MSS1);

########################### Scene 3 - Dn1000 - Band 2

numeric lastDOSS3MSS2 = dosS3MSS1realSize;
numeric hDOSS3MSS2 = 1;

while ( (hDOSS3MSS2 * 3 + 1) < lastDOSS3MSS2 - 1 )
{
    hDOSS3MSS2 = 3 * hDOSS3MSS2 + 1;
}

while ( hDOSS3MSS2 > 0 )
{
    for i = hDOSS3MSS2 - 1 to lastDOSS3MSS2
    {
        numeric keyDOSS3MSS2 = allValuesDosS3MSS2[i];
        numeric jDOSS3MSS2 = i;

        while (jDOSS3MSS2 >= hDOSS3MSS2 && allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2] > keyDOSS3MSS2)
        {
            allValuesDosS3MSS2[jDOSS3MSS2] = allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2];
            jDOSS3MSS2 = jDOSS3MSS2 - hDOSS3MSS2;
        }
        allValuesDosS3MSS2[jDOSS3MSS2] = keyDOSS3MSS2;
    }
    hDOSS3MSS2 = floor(hDOSS3MSS2/3);
}

numeric minValueDosS3MSS2 = allValuesDosS3MSS2[1];
numeric dn1000S3MSS2 = 10000;

numeric actualCountDosS3MSS2 = 0;
for i = 1 to dosS3MSS2realSize 
{
    if (allValuesDosS3MSS2[i] == minValueDosS3MSS2)
    {
        actualCountDosS3MSS2 = actualCountDosS3MSS2 + 1;
        if (actualCountDosS3MSS2 >= DNminCALCS3MSS2)
        {
            dn1000S3MSS2 = minValueDosS3MSS2;
            go to dn1000S3MSS2jumper;
        }
    }
    else
    {
        minValueDosS3MSS2 = allValuesDosS3MSS2[i+1];
        actualCountDosS3MSS2 = 0;
    }
}

dn1000S3MSS2jumper:

if (dn1000S3MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}
if (dn1000S3MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}

printf("dn1000 Scene 3 MSS2: %12f\n", dn1000S3MSS2);

########################### Scene 3 - Dn1000 - Band 3

numeric lastDOSS3MSS3 = dosS3MSS3realSize;
numeric hDOSS3MSS3 = 1;

while ( (hDOSS3MSS3 * 3 + 1) < lastDOSS3MSS3 - 1 )
{
    hDOSS3MSS3 = 3 * hDOSS3MSS3 + 1;
}

while ( hDOSS3MSS3 > 0 )
{
    for i = hDOSS3MSS3 - 1 to lastDOSS3MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS3 = allValuesDosS3MSS3[i];
        numeric jDOSS3MSS3 = i;

        while (jDOSS3MSS3 >= hDOSS3MSS3 && allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3] > keyDOSS3MSS3)
        {
            allValuesDosS3MSS3[jDOSS3MSS3] = allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3];
            jDOSS3MSS3 = jDOSS3MSS3 - hDOSS3MSS3;
        }
        allValuesDosS3MSS3[jDOSS3MSS3] = keyDOSS3MSS3;
    }
    hDOSS3MSS3 = floor(hDOSS3MSS3/3);
}

numeric minValueDosS3MSS3 = allValuesDosS3MSS3[1];
numeric dn1000S3MSS3 = 10000;

numeric actualCountDosS3MSS3 = 0;
for i = 1 to dosS3MSS3realSize 
{
    if (allValuesDosS3MSS3[i] == minValueDosS3MSS3)
    {
        actualCountDosS3MSS3 = actualCountDosS3MSS3 + 1;
        if (actualCountDosS3MSS3 >= DNminCALCS3MSS3)
        {
            dn1000S3MSS3 = minValueDosS3MSS3;
            go to dn1000S3MSS3jumper;
        }
    }
    else
    {
        minValueDosS3MSS3 = allValuesDosS3MSS3[i+1];
        actualCountDosS3MSS3 = 0;
    }
}

dn1000S3MSS3jumper:

if (dn1000S3MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}
if (dn1000S3MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}
	
printf("dn1000 Scene 3 MSS3: %12f\n", dn1000S3MSS3);

########################### Scene 3 - Dn1000 - Band 4

numeric lastDOSS3MSS4 = dosS3MSS4realSize;
numeric hDOSS3MSS4 = 1;

while ( (hDOSS3MSS4 * 3 + 1) < lastDOSS3MSS4 - 1 )
{
    hDOSS3MSS4 = 3 * hDOSS3MSS4 + 1;
}

while ( hDOSS3MSS4 > 0 )
{
    for i = hDOSS3MSS4 - 1 to lastDOSS3MSS4
    {
        numeric keyDOSS3MSS4 = allValuesDosS3MSS4[i];
        numeric jDOSS3MSS4 = i;

        while (jDOSS3MSS4 >= hDOSS3MSS4 && allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4] > keyDOSS3MSS4)
        {
            allValuesDosS3MSS4[jDOSS3MSS4] = allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4];
            jDOSS3MSS4 = jDOSS3MSS4 - hDOSS3MSS4;
        }
        allValuesDosS3MSS4[jDOSS3MSS4] = keyDOSS3MSS4;
    }
    hDOSS3MSS4 = floor(hDOSS3MSS4/3);
}

numeric minValueDosS3MSS4 = allValuesDosS3MSS4[1];
numeric dn1000S3MSS4 = 10000;

numeric actualCountDosS3MSS4 = 0;
for i = 1 to dosS3MSS4realSize 
{
    if (allValuesDosS3MSS4[i] == minValueDosS3MSS4)
    {
        actualCountDosS3MSS4 = actualCountDosS3MSS4 + 1;
        if (actualCountDosS3MSS4 >= DNminCALCS3MSS4)
        {
            dn1000S3MSS4 = minValueDosS3MSS4;
            go to dn1000S3MSS4jumper;
        }
    }
    else
    {
        minValueDosS3MSS4 = allValuesDosS3MSS4[i+1];
        actualCountDosS3MSS4 = 0;
    }
}

dn1000S3MSS4jumper:

if (dn1000S3MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}
if (dn1000S3MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}
	
printf("dn1000 Scene 3 MSS4: %12f\n", dn1000S3MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS3MSS1 = dn1000S3MSS1 - (0.01 * (radsurfMSS3b1 * cos(s3zenith)) / pi);
numeric pathRadS3MSS2 = dn1000S3MSS2 - (0.01 * (radsurfMSS3b2 * cos(s3zenith)) / pi);
numeric pathRadS3MSS3 = dn1000S3MSS3 - (0.01 * (radsurfMSS3b3 * cos(s3zenith)) / pi);
numeric pathRadS3MSS4 = dn1000S3MSS4 - (0.01 * (radsurfMSS3b4 * cos(s3zenith)) / pi);

printf("Pathradiance Scene 3 Band 1: %12f\n", pathRadS3MSS1);
printf("Pathradiance Scene 3 Band 2: %12f\n", pathRadS3MSS2);
printf("Pathradiance Scene 3 Band 3: %12f\n", pathRadS3MSS3);
printf("Pathradiance Scene 3 Band 4: %12f\n\n", pathRadS3MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS3MSS1 = (pi * (RADS3MSS1[i,j] - pathRadS3MSS1)) / (radsurfMSS3b1 * cos(s3zenith));

                  if ( valueS3MSS1 < 0 )
       		{
          			valueS3MSS1 = 0;
				countS3REFnullMSS1 = countS3REFnullMSS1 + 1;
       		}

			else if ( valueS3MSS1 > 1 )
			{
				valueS3MSS1 = 1;
				countS3REFoneMSS1 = countS3REFoneMSS1 + 1;
			}
       		
			REFS3MSS1[i,j] = valueS3MSS1;

               valueS3MSS2 = (pi * (RADS3MSS2[i,j] - pathRadS3MSS2)) / (radsurfMSS3b2 * cos(s3zenith));

                  if ( valueS3MSS2 < 0 )
       		{
          			valueS3MSS2 = 0;
				countS3REFnullMSS2 = countS3REFnullMSS2 + 1;
       		}

			else if ( valueS3MSS2 > 1 )
			{
				valueS3MSS2 = 1;
				countS3REFoneMSS2 = countS3REFoneMSS2 + 1;
			}
       		
			REFS3MSS2[i,j] = valueS3MSS2;

               valueS3MSS3 = (pi * (RADS3MSS3[i,j] - pathRadS3MSS3)) / (radsurfMSS3b3 * cos(s3zenith));

                  if ( valueS3MSS3 < 0 )
       		{
          			valueS3MSS3 = 0;
				countS3REFnullMSS3 = countS3REFnullMSS3 + 1;
       		}

			else if ( valueS3MSS3 > 1 )
			{
				valueS3MSS3 = 1;
				countS3REFoneMSS3 = countS3REFoneMSS3 + 1;
			}
       		
			REFS3MSS3[i,j] = valueS3MSS3;

               valueS3MSS4 = (pi * (RADS3MSS4[i,j] - pathRadS3MSS4)) / (radsurfMSS3b4 * cos(s3zenith));

                  if ( valueS3MSS4 < 0 )
       		{
          			valueS3MSS4 = 0;
				countS3REFnullMSS4 = countS3REFnullMSS4 + 1;
       		}

			else if ( valueS3MSS4 > 1 )
			{
				valueS3MSS4 = 1;
				countS3REFoneMSS4 = countS3REFoneMSS4 + 1;
			}
       		
			REFS3MSS4[i,j] = valueS3MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS1, countS3REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS2, countS3REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS3, countS3REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS4, countS3REFoneMSS4);

CreatePyramid(REFS3MSS1);
CreatePyramid(REFS3MSS2);
CreatePyramid(REFS3MSS3);
CreatePyramid(REFS3MSS4);
        
CreateHistogram(REFS3MSS1);
CreateHistogram(REFS3MSS2);
CreateHistogram(REFS3MSS3);
CreateHistogram(REFS3MSS4);

CloseRaster(RADS3MSS1);
CloseRaster(RADS3MSS2);
CloseRaster(RADS3MSS3);
CloseRaster(RADS3MSS4);

printf("Reflectance calculation for Scene 3 (MSS) is done...\n\n\n");
}

else
{
raster REFS3MSS1, REFS3MSS2, REFS3MSS3, REFS3MSS4;
numeric radsurfMSS3b1, radsurfMSS3b2, radsurfMSS3b3, radsurfMSS3b4;
numeric valueS3MSS1, valueS3MSS2, valueS3MSS3, valueS3MSS4;

numeric dosS3MSS1nullValCount = 0;
numeric dosS3MSS2nullValCount = 0;
numeric dosS3MSS3nullValCount = 0;
numeric dosS3MSS4nullValCount = 0;

numeric dosS3MSS1realArrayCount = 1;
numeric dosS3MSS2realArrayCount = 1;
numeric dosS3MSS3realArrayCount = 1;
numeric dosS3MSS4realArrayCount = 1;

numeric countS3REFnullMSS1 = 0;
numeric countS3REFnullMSS2 = 0;
numeric countS3REFnullMSS3 = 0;
numeric countS3REFnullMSS4 = 0;

numeric countS3REFoneMSS1 = 0;
numeric countS3REFoneMSS2 = 0;
numeric countS3REFoneMSS3 = 0;
numeric countS3REFoneMSS4 = 0;

radsurfMSS3b1 = ESMSS11 / (dist3^2);
radsurfMSS3b2 = ESMSS12 / (dist3^2);
radsurfMSS3b3 = ESMSS13 / (dist3^2);
radsurfMSS3b4 = ESMSS14 / (dist3^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 3): %12f\n", radsurfMSS3b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 3): %12f\n\n", radsurfMSS3b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS3MSS1[i,j]) == 1)
		{
		dosS3MSS1nullValCount = dosS3MSS1nullValCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 1)
		{
		dosS3MSS2nullValCount = dosS3MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS3[i,j]) == 1)
		{
		dosS3MSS3nullValCount = dosS3MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS3MSS4[i,j]) == 1)
		{
		dosS3MSS4nullValCount = dosS3MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS3MSS1realSize = (MSSlins * MSScols) - dosS3MSS1nullValCount;
numeric dosS3MSS2realSize = (MSSlins * MSScols) - dosS3MSS2nullValCount;
numeric dosS3MSS3realSize = (MSSlins * MSScols) - dosS3MSS3nullValCount;
numeric dosS3MSS4realSize = (MSSlins * MSScols) - dosS3MSS4nullValCount;

array allValuesDosS3MSS1[dosS3MSS1realSize];
array allValuesDosS3MSS2[dosS3MSS2realSize];
array allValuesDosS3MSS3[dosS3MSS3realSize];
array allValuesDosS3MSS4[dosS3MSS4realSize];

numeric DNminCALCS3MSS1 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS1realSize);
numeric DNminCALCS3MSS2 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS2realSize);
numeric DNminCALCS3MSS3 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS3realSize);
numeric DNminCALCS3MSS4 = DNminCALC(Scene3Lin, Scene3Col, dosS3MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS3MSS1[i,j]) == 0)
		{
		  allValuesDosS3MSS1[dosS3MSS1realArrayCount] = RADS3MSS1[i,j];		 
		  dosS3MSS1realArrayCount = dosS3MSS1realArrayCount + 1;
		}
		if (IsNull(RADS3MSS2[i,j]) == 0)
		{
		  allValuesDosS3MSS2[dosS3MSS2realArrayCount] = RADS3MSS2[i,j];		 
		  dosS3MSS2realArrayCount = dosS3MSS2realArrayCount + 1;
		}
		if (IsNull(RADS3MSS3[i,j]) == 0)
		{
		  allValuesDosS3MSS3[dosS3MSS3realArrayCount] = RADS3MSS3[i,j];		 
		  dosS3MSS3realArrayCount = dosS3MSS3realArrayCount + 1;
		}
		if (IsNull(RADS3MSS4[i,j]) == 0)
		{
		  allValuesDosS3MSS4[dosS3MSS4realArrayCount] = RADS3MSS4[i,j];		 
		  dosS3MSS4realArrayCount = dosS3MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 1 - Dn1000 - Band 1

numeric lastDOSS3MSS1 = dosS3MSS1realSize;
numeric hDOSS3MSS1 = 1;

while ( (hDOSS3MSS1 * 3 + 1) < lastDOSS3MSS1 - 1 )
{
    hDOSS3MSS1 = 3 * hDOSS3MSS1 + 1;
}

while ( hDOSS3MSS1 > 0 )
{
    for i = hDOSS3MSS1 - 1 to lastDOSS3MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS1 = allValuesDosS3MSS1[i];
        numeric jDOSS3MSS1 = i;

        while (jDOSS3MSS1 >= hDOSS3MSS1 && allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1] > keyDOSS3MSS1)
        {
            allValuesDosS3MSS1[jDOSS3MSS1] = allValuesDosS3MSS1[jDOSS3MSS1 - hDOSS3MSS1];
            jDOSS3MSS1 = jDOSS3MSS1 - hDOSS3MSS1;
        }
        allValuesDosS3MSS1[jDOSS3MSS1] = keyDOSS3MSS1;
    }
    hDOSS3MSS1 = floor(hDOSS3MSS1/3);
}

numeric minValueDosS3MSS1 = allValuesDosS3MSS1[1];
numeric dn1000S3MSS1 = 10000;

numeric actualCountDosS3MSS1 = 0;
for i = 1 to dosS3MSS1realSize 
{
    if (allValuesDosS3MSS1[i] == minValueDosS3MSS1)
    {
        actualCountDosS3MSS1 = actualCountDosS3MSS1 + 1;
        if (actualCountDosS3MSS1 >= DNminCALCS3MSS1)
        {
            dn1000S3MSS1 = minValueDosS3MSS1;
            go to dn1000S3MSS1jumper;
        }
    }
    else
    {
        minValueDosS3MSS1 = allValuesDosS3MSS1[i+1];
        actualCountDosS3MSS1 = 0;
    }
}

dn1000S3MSS1jumper:

if (dn1000S3MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}
if (dn1000S3MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS1 = allValuesDosS3MSS1[1];
}

printf("dn1000 Scene 3 MSS1: %12f\n", dn1000S3MSS1);

########################### Scene 3 - Dn1000 - Band 2

numeric lastDOSS3MSS2 = dosS3MSS1realSize;
numeric hDOSS3MSS2 = 1;

while ( (hDOSS3MSS2 * 3 + 1) < lastDOSS3MSS2 - 1 )
{
    hDOSS3MSS2 = 3 * hDOSS3MSS2 + 1;
}

while ( hDOSS3MSS2 > 0 )
{
    for i = hDOSS3MSS2 - 1 to lastDOSS3MSS2
    {
        numeric keyDOSS3MSS2 = allValuesDosS3MSS2[i];
        numeric jDOSS3MSS2 = i;

        while (jDOSS3MSS2 >= hDOSS3MSS2 && allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2] > keyDOSS3MSS2)
        {
            allValuesDosS3MSS2[jDOSS3MSS2] = allValuesDosS3MSS2[jDOSS3MSS2 - hDOSS3MSS2];
            jDOSS3MSS2 = jDOSS3MSS2 - hDOSS3MSS2;
        }
        allValuesDosS3MSS2[jDOSS3MSS2] = keyDOSS3MSS2;
    }
    hDOSS3MSS2 = floor(hDOSS3MSS2/3);
}

numeric minValueDosS3MSS2 = allValuesDosS3MSS2[1];
numeric dn1000S3MSS2 = 10000;

numeric actualCountDosS3MSS2 = 0;
for i = 1 to dosS3MSS2realSize 
{
    if (allValuesDosS3MSS2[i] == minValueDosS3MSS2)
    {
        actualCountDosS3MSS2 = actualCountDosS3MSS2 + 1;
        if (actualCountDosS3MSS2 >= DNminCALCS3MSS2)
        {
            dn1000S3MSS2 = minValueDosS3MSS2;
            go to dn1000S3MSS2jumper;
        }
    }
    else
    {
        minValueDosS3MSS2 = allValuesDosS3MSS2[i+1];
        actualCountDosS3MSS2 = 0;
    }
}

dn1000S3MSS2jumper:

if (dn1000S3MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}
if (dn1000S3MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS2 = allValuesDosS3MSS2[1];
}

printf("dn1000 Scene 3 MSS2: %12f\n", dn1000S3MSS2);

########################### Scene 1 - Dn1000 - Band 3

numeric lastDOSS3MSS3 = dosS3MSS3realSize;
numeric hDOSS3MSS3 = 1;

while ( (hDOSS3MSS3 * 3 + 1) < lastDOSS3MSS3 - 1 )
{
    hDOSS3MSS3 = 3 * hDOSS3MSS3 + 1;
}

while ( hDOSS3MSS3 > 0 )
{
    for i = hDOSS3MSS3 - 1 to lastDOSS3MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS3MSS3 = allValuesDosS3MSS3[i];
        numeric jDOSS3MSS3 = i;

        while (jDOSS3MSS3 >= hDOSS3MSS3 && allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3] > keyDOSS3MSS3)
        {
            allValuesDosS3MSS3[jDOSS3MSS3] = allValuesDosS3MSS3[jDOSS3MSS3 - hDOSS3MSS3];
            jDOSS3MSS3 = jDOSS3MSS3 - hDOSS3MSS3;
        }
        allValuesDosS3MSS3[jDOSS3MSS3] = keyDOSS3MSS3;
    }
    hDOSS3MSS3 = floor(hDOSS3MSS3/3);
}

numeric minValueDosS3MSS3 = allValuesDosS3MSS3[1];
numeric dn1000S3MSS3 = 10000;

numeric actualCountDosS3MSS3 = 0;
for i = 1 to dosS3MSS3realSize 
{
    if (allValuesDosS3MSS3[i] == minValueDosS3MSS3)
    {
        actualCountDosS3MSS3 = actualCountDosS3MSS3 + 1;
        if (actualCountDosS3MSS3 >= DNminCALCS3MSS3)
        {
            dn1000S3MSS3 = minValueDosS3MSS3;
            go to dn1000S3MSS3jumper;
        }
    }
    else
    {
        minValueDosS3MSS3 = allValuesDosS3MSS3[i+1];
        actualCountDosS3MSS3 = 0;
    }
}

dn1000S3MSS3jumper:

if (dn1000S3MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}
if (dn1000S3MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS3 = allValuesDosS3MSS3[1];
}
	
printf("dn1000 Scene 3 MSS3: %12f\n", dn1000S3MSS3);

########################### Scene 3 - Dn1000 - Band 4

numeric lastDOSS3MSS4 = dosS3MSS4realSize;
numeric hDOSS3MSS4 = 1;

while ( (hDOSS3MSS4 * 3 + 1) < lastDOSS3MSS4 - 1 )
{
    hDOSS3MSS4 = 3 * hDOSS3MSS4 + 1;
}

while ( hDOSS3MSS4 > 0 )
{
    for i = hDOSS3MSS4 - 1 to lastDOSS3MSS4
    {
        numeric keyDOSS3MSS4 = allValuesDosS3MSS4[i];
        numeric jDOSS3MSS4 = i;

        while (jDOSS3MSS4 >= hDOSS3MSS4 && allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4] > keyDOSS3MSS4)
        {
            allValuesDosS3MSS4[jDOSS3MSS4] = allValuesDosS3MSS4[jDOSS3MSS4 - hDOSS3MSS4];
            jDOSS3MSS4 = jDOSS3MSS4 - hDOSS3MSS4;
        }
        allValuesDosS3MSS4[jDOSS3MSS4] = keyDOSS3MSS4;
    }
    hDOSS3MSS4 = floor(hDOSS3MSS4/3);
}

numeric minValueDosS3MSS4 = allValuesDosS3MSS4[1];
numeric dn1000S3MSS4 = 10000;

numeric actualCountDosS3MSS4 = 0;
for i = 1 to dosS3MSS4realSize 
{
    if (allValuesDosS3MSS4[i] == minValueDosS3MSS4)
    {
        actualCountDosS3MSS4 = actualCountDosS3MSS4 + 1;
        if (actualCountDosS3MSS4 >= DNminCALCS3MSS4)
        {
            dn1000S3MSS4 = minValueDosS3MSS4;
            go to dn1000S3MSS4jumper;
        }
    }
    else
    {
        minValueDosS3MSS4 = allValuesDosS3MSS4[i+1];
        actualCountDosS3MSS4 = 0;
    }
}

dn1000S3MSS4jumper:

if (dn1000S3MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}

if (dn1000S3MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S3MSS4 = allValuesDosS3MSS4[1];
}
	
printf("dn1000 Scene 3 MSS4: %12f\n", dn1000S3MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS3MSS1 = dn1000S3MSS1 - (0.01 * (radsurfMSS3b1 * cos(s3zenith)) / pi);
numeric pathRadS3MSS2 = dn1000S3MSS2 - (0.01 * (radsurfMSS3b2 * cos(s3zenith)) / pi);
numeric pathRadS3MSS3 = dn1000S3MSS3 - (0.01 * (radsurfMSS3b3 * cos(s3zenith)) / pi);
numeric pathRadS3MSS4 = dn1000S3MSS4 - (0.01 * (radsurfMSS3b4 * cos(s3zenith)) / pi);

printf("Pathradiance Scene 3 Band 1: %12f\n", pathRadS3MSS1);
printf("Pathradiance Scene 3 Band 2: %12f\n", pathRadS3MSS2);
printf("Pathradiance Scene 3 Band 3: %12f\n", pathRadS3MSS3);
printf("Pathradiance Scene 3 Band 4: %12f\n\n", pathRadS3MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS3MSS1 = (pi * (RADS3MSS1[i,j] - pathRadS3MSS1)) / (radsurfMSS3b1 * cos(s3zenith));

                  if ( valueS3MSS1 < 0 )
       		{
          			valueS3MSS1 = 0;
				countS3REFnullMSS1 = countS3REFnullMSS1 + 1;
       		}

			else if ( valueS3MSS1 > 1 )
			{
				valueS3MSS1 = 1;
				countS3REFoneMSS1 = countS3REFoneMSS1 + 1;
			}
       		
			REFS3MSS1[i,j] = valueS3MSS1;

               valueS3MSS2 = (pi * (RADS3MSS2[i,j] - pathRadS3MSS2)) / (radsurfMSS3b2 * cos(s3zenith));

                  if ( valueS3MSS2 < 0 )
       		{
          			valueS3MSS2 = 0;
				countS3REFnullMSS2 = countS3REFnullMSS2 + 1;
       		}

			else if ( valueS3MSS2 > 1 )
			{
				valueS3MSS2 = 1;
				countS3REFoneMSS2 = countS3REFoneMSS2 + 1;
			}
       		
			REFS3MSS2[i,j] = valueS3MSS2;

               valueS3MSS3 = (pi * (RADS3MSS3[i,j] - pathRadS3MSS3)) / (radsurfMSS3b3 * cos(s3zenith));

                  if ( valueS3MSS3 < 0 )
       		{
          			valueS3MSS3 = 0;
				countS3REFnullMSS3 = countS3REFnullMSS3 + 1;
       		}

			else if ( valueS3MSS3 > 1 )
			{
				valueS3MSS3 = 1;
				countS3REFoneMSS3 = countS3REFoneMSS3 + 1;
			}
       		
			REFS3MSS3[i,j] = valueS3MSS3;

               valueS3MSS4 = (pi * (RADS3MSS4[i,j] - pathRadS3MSS4)) / (radsurfMSS3b4 * cos(s3zenith));

                  if ( valueS3MSS4 < 0 )
       		{
          			valueS3MSS4 = 0;
				countS3REFnullMSS4 = countS3REFnullMSS4 + 1;
       		}

			else if ( valueS3MSS4 > 1 )
			{
				valueS3MSS4 = 1;
				countS3REFoneMSS4 = countS3REFoneMSS4 + 1;
			}
       		
			REFS3MSS4[i,j] = valueS3MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS1, countS3REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS2, countS3REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS3, countS3REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 3 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS3REFnullMSS4, countS3REFoneMSS4);

CreatePyramid(REFS3MSS1);
CreatePyramid(REFS3MSS2);
CreatePyramid(REFS3MSS3);
CreatePyramid(REFS3MSS4);
        
CreateHistogram(REFS3MSS1);
CreateHistogram(REFS3MSS2);
CreateHistogram(REFS3MSS3);
CreateHistogram(REFS3MSS4);

CloseRaster(RADS3MSS1);
CloseRaster(RADS3MSS2);
CloseRaster(RADS3MSS3);
CloseRaster(RADS3MSS4);

printf("Reflectance calculation for Scene 3 (MSS) is done...\n\n\n");
}

#############################
#### Reflectance Scene 4
#############################

if ( sensors4 == 7 )
{
raster REFS4ETM1, REFS4ETM2, REFS4ETM3, REFS4ETM4, REFS4ETM5, REFS4ETM7;
numeric radsurfETMS4b1, radsurfETMS4b2, radsurfETMS4b3, radsurfETMS4b4, radsurfETMS4b5, radsurfETMS4b7;
numeric valueS4ETM1, valueS4ETM2, valueS4ETM3, valueS4ETM4, valueS4ETM5, valueS4ETM7;

numeric dosS4ETM1nullValCount = 0;
numeric dosS4ETM2nullValCount = 0;
numeric dosS4ETM3nullValCount = 0;
numeric dosS4ETM4nullValCount = 0;
numeric dosS4ETM5nullValCount = 0;
numeric dosS4ETM7nullValCount = 0;

numeric dosS4ETM1realArrayCount = 1;
numeric dosS4ETM2realArrayCount = 1;
numeric dosS4ETM3realArrayCount = 1;
numeric dosS4ETM4realArrayCount = 1;
numeric dosS4ETM5realArrayCount = 1;
numeric dosS4ETM7realArrayCount = 1;

numeric countS4REFnullETM1 = 0;
numeric countS4REFnullETM2 = 0;
numeric countS4REFnullETM3 = 0;
numeric countS4REFnullETM4 = 0;
numeric countS4REFnullETM5 = 0;
numeric countS4REFnullETM7 = 0;

numeric countS4REFoneETM1 = 0;
numeric countS4REFoneETM2 = 0;
numeric countS4REFoneETM3 = 0;
numeric countS4REFoneETM4 = 0;
numeric countS4REFoneETM5 = 0;
numeric countS4REFoneETM7 = 0;

radsurfETMS4b1 = ESETM1 / (dist4^2);
radsurfETMS4b2 = ESETM2 / (dist4^2);
radsurfETMS4b3 = ESETM3 / (dist4^2);
radsurfETMS4b4 = ESETM4 / (dist4^2);
radsurfETMS4b5 = ESETM5 / (dist4^2);
radsurfETMS4b7 = ESETM7 / (dist4^2);

printf("Surface Radiance ETM Band 1 in W/m^2µm (Scene 4): %12f\n", radsurfETMS4b1);
printf("Surface Radiance ETM Band 2 in W/m^2µm (Scene 4): %12f\n", radsurfETMS4b2);
printf("Surface Radiance ETM Band 3 in W/m^2µm (Scene 4): %12f\n", radsurfETMS4b3);
printf("Surface Radiance ETM Band 4 in W/m^2µm (Scene 4): %12f\n", radsurfETMS4b4);
printf("Surface Radiance ETM Band 5 in W/m^2µm (Scene 4): %12f\n", radsurfETMS4b5);
printf("Surface Radiance ETM Band 7 in W/m^2µm (Scene 4): %12f\n\n", radsurfETMS4b7);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {       
		if (IsNull(RADS4ETM1[i,j]) == 1)
		{
		dosS4ETM1nullValCount = dosS4ETM1nullValCount + 1;
		}
		if (IsNull(RADS4ETM2[i,j]) == 1)
		{
		dosS4ETM2nullValCount = dosS4ETM2nullValCount + 1;
		}		 
		if (IsNull(RADS4ETM3[i,j]) == 1)
		{
		dosS4ETM3nullValCount = dosS4ETM3nullValCount + 1;
		}		 
		if (IsNull(RADS4ETM4[i,j]) == 1)
		{
		dosS4ETM4nullValCount = dosS4ETM4nullValCount + 1;
		}		 
		if (IsNull(RADS4ETM5[i,j]) == 1)
		{
		dosS4ETM5nullValCount = dosS4ETM5nullValCount + 1;
		}		 
		if (IsNull(RADS4ETM7[i,j]) == 1)
		{
		dosS4ETM7nullValCount = dosS4ETM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS4ETM1realSize = (ETMlins * ETMcols) - dosS4ETM1nullValCount;
numeric dosS4ETM2realSize = (ETMlins * ETMcols) - dosS4ETM2nullValCount;
numeric dosS4ETM3realSize = (ETMlins * ETMcols) - dosS4ETM3nullValCount;
numeric dosS4ETM4realSize = (ETMlins * ETMcols) - dosS4ETM4nullValCount;
numeric dosS4ETM5realSize = (ETMlins * ETMcols) - dosS4ETM5nullValCount;
numeric dosS4ETM7realSize = (ETMlins * ETMcols) - dosS4ETM7nullValCount;

array allValuesDosS4ETM1[dosS4ETM1realSize];
array allValuesDosS4ETM2[dosS4ETM2realSize];
array allValuesDosS4ETM3[dosS4ETM3realSize];
array allValuesDosS4ETM4[dosS4ETM4realSize];
array allValuesDosS4ETM5[dosS4ETM5realSize];
array allValuesDosS4ETM7[dosS4ETM7realSize];

numeric DNminCALCS4ETM1 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM1realSize);
numeric DNminCALCS4ETM2 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM2realSize);
numeric DNminCALCS4ETM3 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM3realSize);
numeric DNminCALCS4ETM4 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM4realSize);
numeric DNminCALCS4ETM5 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM5realSize);
numeric DNminCALCS4ETM7 = DNminCALC(Scene4Lin, Scene4Col, dosS4ETM7realSize);

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(RADS4ETM1[i,j]) == 0)
		{
		  allValuesDosS4ETM1[dosS4ETM1realArrayCount] = RADS4ETM1[i,j];		 
		  dosS4ETM1realArrayCount = dosS4ETM1realArrayCount + 1;
		}
		if (IsNull(RADS4ETM2[i,j]) == 0)
		{
		  allValuesDosS4ETM2[dosS4ETM2realArrayCount] = RADS4ETM2[i,j];		 
		  dosS4ETM2realArrayCount = dosS4ETM2realArrayCount + 1;
		}
		if (IsNull(RADS4ETM3[i,j]) == 0)
		{
		  allValuesDosS4ETM3[dosS4ETM3realArrayCount] = RADS4ETM3[i,j];		 
		  dosS4ETM3realArrayCount = dosS4ETM3realArrayCount + 1;
		}
		if (IsNull(RADS4ETM4[i,j]) == 0)
		{
		  allValuesDosS4ETM4[dosS4ETM4realArrayCount] = RADS4ETM4[i,j];		 
		  dosS4ETM4realArrayCount = dosS4ETM4realArrayCount + 1;
		}
		if (IsNull(RADS4ETM5[i,j]) == 0)
		{
		  allValuesDosS4ETM5[dosS4ETM5realArrayCount] = RADS4ETM5[i,j];		 
		  dosS4ETM5realArrayCount = dosS4ETM5realArrayCount + 1;
		}
		if (IsNull(RADS4ETM7[i,j]) == 0)
		{
		  allValuesDosS4ETM7[dosS4ETM7realArrayCount] = RADS4ETM7[i,j];		 
		  dosS4ETM7realArrayCount = dosS4ETM7realArrayCount + 1;
		}
    }
}

########################### Scene 4 - Dn1000 - Band 1

numeric lastDOSS4ETM1 = dosS4ETM1realSize;
numeric hDOSS4ETM1 = 1;

while ( (hDOSS4ETM1 * 3 + 1) < lastDOSS4ETM1 - 1 )
{
    hDOSS4ETM1 = 3 * hDOSS4ETM1 + 1;
}

while ( hDOSS4ETM1 > 0 )
{
    for i = hDOSS4ETM1 - 1 to lastDOSS4ETM1               # for each of the h sets of elements
    {
        numeric keyDOSS4ETM1 = allValuesDosS4ETM1[i];
        numeric jDOSS4ETM1 = i;

        while (jDOSS4ETM1 >= hDOSS4ETM1 && allValuesDosS4ETM1[jDOSS4ETM1 - hDOSS4ETM1] > keyDOSS4ETM1)
        {
            allValuesDosS4ETM1[jDOSS4ETM1] = allValuesDosS4ETM1[jDOSS4ETM1 - hDOSS4ETM1];
            jDOSS4ETM1 = jDOSS4ETM1 - hDOSS4ETM1;
        }
        allValuesDosS4ETM1[jDOSS4ETM1] = keyDOSS4ETM1;
    }
    hDOSS4ETM1 = floor(hDOSS4ETM1/3);
}

numeric minValueDosS4ETM1 = allValuesDosS4ETM1[1];
numeric dn1000S4ETM1 = 10000;

numeric actualCountDosS4ETM1 = 0;
for i = 1 to dosS4ETM1realSize 
{
    if (allValuesDosS4ETM1[i] == minValueDosS4ETM1)
    {
        actualCountDosS4ETM1 = actualCountDosS4ETM1 + 1;
        if (actualCountDosS4ETM1 >= DNminCALCS4ETM1)
        {
            dn1000S4ETM1 = minValueDosS4ETM1;
            go to dn1000S4ETM1jumper;
        }
    }
    else
    {
        minValueDosS4ETM1 = allValuesDosS4ETM1[i+1];
        actualCountDosS4ETM1 = 0;
    }
}

dn1000S4ETM1jumper:

if (dn1000S4ETM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM1 = allValuesDosS4ETM1[1];
}
if (dn1000S4ETM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM1 = allValuesDosS4ETM1[1];
}

printf("dn1000 Scene 4 ETM1: %12f\n", dn1000S4ETM1);

########################### Scene 4 - Dn1000 - Band 2

numeric lastDOSS4ETM2 = dosS4ETM1realSize;
numeric hDOSS4ETM2 = 1;

while ( (hDOSS4ETM2 * 3 + 1) < lastDOSS4ETM2 - 1 )
{
    hDOSS4ETM2 = 3 * hDOSS4ETM2 + 1;
}

while ( hDOSS4ETM2 > 0 )
{
    for i = hDOSS4ETM2 - 1 to lastDOSS4ETM2
    {
        numeric keyDOSS4ETM2 = allValuesDosS4ETM2[i];
        numeric jDOSS4ETM2 = i;

        while (jDOSS4ETM2 >= hDOSS4ETM2 && allValuesDosS4ETM2[jDOSS4ETM2 - hDOSS4ETM2] > keyDOSS4ETM2)
        {
            allValuesDosS4ETM2[jDOSS4ETM2] = allValuesDosS4ETM2[jDOSS4ETM2 - hDOSS4ETM2];
            jDOSS4ETM2 = jDOSS4ETM2 - hDOSS4ETM2;
        }
        allValuesDosS4ETM2[jDOSS4ETM2] = keyDOSS4ETM2;
    }
    hDOSS4ETM2 = floor(hDOSS4ETM2/3);
}

numeric minValueDosS4ETM2 = allValuesDosS4ETM2[1];
numeric dn1000S4ETM2 = 10000;

numeric actualCountDosS4ETM2 = 0;
for i = 1 to dosS4ETM2realSize 
{
    if (allValuesDosS4ETM2[i] == minValueDosS4ETM2)
    {
        actualCountDosS4ETM2 = actualCountDosS4ETM2 + 1;
        if (actualCountDosS4ETM2 >= DNminCALCS4ETM2)
        {
            dn1000S4ETM2 = minValueDosS4ETM2;
            go to dn1000S4ETM2jumper;
        }
    }
    else
    {
        minValueDosS4ETM2 = allValuesDosS4ETM2[i+1];
        actualCountDosS4ETM2 = 0;
    }
}

dn1000S4ETM2jumper:

if (dn1000S4ETM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM2 = allValuesDosS4ETM2[1];
}
if (dn1000S4ETM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM2 = allValuesDosS4ETM2[1];
}

printf("dn1000 Scene 4 ETM2: %12f\n", dn1000S4ETM2);

########################### Scene 4 - Dn1000 - Band 3

numeric lastDOSS4ETM3 = dosS4ETM3realSize;
numeric hDOSS4ETM3 = 1;

while ( (hDOSS4ETM3 * 3 + 1) < lastDOSS4ETM3 - 1 )
{
    hDOSS4ETM3 = 3 * hDOSS4ETM3 + 1;
}

while ( hDOSS4ETM3 > 0 )
{
    for i = hDOSS4ETM3 - 1 to lastDOSS4ETM3               # for each of the h sets of elements
    {
        numeric keyDOSS4ETM3 = allValuesDosS4ETM3[i];
        numeric jDOSS4ETM3 = i;

        while (jDOSS4ETM3 >= hDOSS4ETM3 && allValuesDosS4ETM3[jDOSS4ETM3 - hDOSS4ETM3] > keyDOSS4ETM3)
        {
            allValuesDosS4ETM3[jDOSS4ETM3] = allValuesDosS4ETM3[jDOSS4ETM3 - hDOSS4ETM3];
            jDOSS4ETM3 = jDOSS4ETM3 - hDOSS4ETM3;
        }
        allValuesDosS4ETM3[jDOSS4ETM3] = keyDOSS4ETM3;
    }
    hDOSS4ETM3 = floor(hDOSS4ETM3/3);
}

numeric minValueDosS4ETM3 = allValuesDosS4ETM3[1];
numeric dn1000S4ETM3 = 10000;

numeric actualCountDosS4ETM3 = 0;
for i = 1 to dosS4ETM3realSize 
{
    if (allValuesDosS4ETM3[i] == minValueDosS4ETM3)
    {
        actualCountDosS4ETM3 = actualCountDosS4ETM3 + 1;
        if (actualCountDosS4ETM3 >= DNminCALCS4ETM3)
        {
            dn1000S4ETM3 = minValueDosS4ETM3;
            go to dn1000S4ETM3jumper;
        }
    }
    else
    {
        minValueDosS4ETM3 = allValuesDosS4ETM3[i+1];
        actualCountDosS4ETM3 = 0;
    }
}

dn1000S4ETM3jumper:

if (dn1000S4ETM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM3 = allValuesDosS4ETM3[1];
}
if (dn1000S4ETM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM3 = allValuesDosS4ETM3[1];
}
	
printf("dn1000 Scene 4 ETM3: %12f\n", dn1000S4ETM3);

########################### Scene 4 - Dn1000 - Band 4

numeric lastDOSS4ETM4 = dosS4ETM4realSize;
numeric hDOSS4ETM4 = 1;

while ( (hDOSS4ETM4 * 3 + 1) < lastDOSS4ETM4 - 1 )
{
    hDOSS4ETM4 = 3 * hDOSS4ETM4 + 1;
}

while ( hDOSS4ETM4 > 0 )
{
    for i = hDOSS4ETM4 - 1 to lastDOSS4ETM4
    {
        numeric keyDOSS4ETM4 = allValuesDosS4ETM4[i];
        numeric jDOSS4ETM4 = i;

        while (jDOSS4ETM4 >= hDOSS4ETM4 && allValuesDosS4ETM4[jDOSS4ETM4 - hDOSS4ETM4] > keyDOSS4ETM4)
        {
            allValuesDosS4ETM4[jDOSS4ETM4] = allValuesDosS4ETM4[jDOSS4ETM4 - hDOSS4ETM4];
            jDOSS4ETM4 = jDOSS4ETM4 - hDOSS4ETM4;
        }
        allValuesDosS4ETM4[jDOSS4ETM4] = keyDOSS4ETM4;
    }
    hDOSS4ETM4 = floor(hDOSS4ETM4/3);
}

numeric minValueDosS4ETM4 = allValuesDosS4ETM4[1];
numeric dn1000S4ETM4 = 10000;

numeric actualCountDosS4ETM4 = 0;
for i = 1 to dosS4ETM4realSize 
{
    if (allValuesDosS4ETM4[i] == minValueDosS4ETM4)
    {
        actualCountDosS4ETM4 = actualCountDosS4ETM4 + 1;
        if (actualCountDosS4ETM4 >= DNminCALCS4ETM4)
        {
            dn1000S4ETM4 = minValueDosS4ETM4;
            go to dn1000S4ETM4jumper;
        }
    }
    else
    {
        minValueDosS4ETM4 = allValuesDosS4ETM4[i+1];
        actualCountDosS4ETM4 = 0;
    }
}

dn1000S4ETM4jumper:

if (dn1000S4ETM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM4 = allValuesDosS4ETM4[1];
}
if (dn1000S4ETM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM4 = allValuesDosS4ETM4[1];
}

printf("dn1000 Scene 4 ETM4: %12f\n", dn1000S4ETM4);

########################### Scene 4 - Dn1000 - Band 5

numeric lastDOSS4ETM5 = dosS4ETM5realSize;
numeric hDOSS4ETM5 = 1;

while ( (hDOSS4ETM5 * 3 + 1) < lastDOSS4ETM5 - 1 )
{
    hDOSS4ETM5 = 3 * hDOSS4ETM5 + 1;
}

while ( hDOSS4ETM5 > 0 )
{
    for i = hDOSS4ETM5 - 1 to lastDOSS4ETM5               # for each of the h sets of elements
    {
        numeric keyDOSS4ETM5 = allValuesDosS4ETM5[i];
        numeric jDOSS4ETM5 = i;

        while (jDOSS4ETM5 >= hDOSS4ETM5 && allValuesDosS4ETM5[jDOSS4ETM5 - hDOSS4ETM5] > keyDOSS4ETM5)
        {
            allValuesDosS4ETM5[jDOSS4ETM5] = allValuesDosS4ETM5[jDOSS4ETM5 - hDOSS4ETM5];
            jDOSS4ETM5 = jDOSS4ETM5 - hDOSS4ETM5;
        }
        allValuesDosS4ETM5[jDOSS4ETM5] = keyDOSS4ETM5;
    }
    hDOSS4ETM5 = floor(hDOSS4ETM5/3);
}

numeric minValueDosS4ETM5 = allValuesDosS4ETM5[1];
numeric dn1000S4ETM5 = 10000;

numeric actualCountDosS4ETM5 = 0;
for i = 1 to dosS4ETM5realSize 
{
    if (allValuesDosS4ETM5[i] == minValueDosS4ETM5)
    {
        actualCountDosS4ETM5 = actualCountDosS4ETM5 + 1;
        if (actualCountDosS4ETM5 >= DNminCALCS4ETM5)
        {
            dn1000S4ETM5 = minValueDosS4ETM5;
            go to dn1000S4ETM5jumper;
        }
    }
    else
    {
        minValueDosS4ETM5 = allValuesDosS4ETM5[i+1];
        actualCountDosS4ETM5 = 0;
    }
}

dn1000S4ETM5jumper:

if (dn1000S4ETM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM5 = allValuesDosS4ETM5[1];
}
if (dn1000S4ETM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM5 = allValuesDosS4ETM5[1];
}
	
printf("dn1000 Scene 4 ETM5: %12f\n", dn1000S4ETM5);

########################### Scene 4 - Dn1000 - Band 7

numeric lastDOSS4ETM7 = dosS4ETM7realSize;
numeric hDOSS4ETM7 = 1;

while ( (hDOSS4ETM7 * 3 + 1) < lastDOSS4ETM7 - 1 )
{
    hDOSS4ETM7 = 3 * hDOSS4ETM7 + 1;
}

while ( hDOSS4ETM7 > 0 )
{
    for i = hDOSS4ETM7 - 1 to lastDOSS4ETM7               # for each of the h sets of elements
    {
        numeric keyDOSS4ETM7 = allValuesDosS4ETM7[i];
        numeric jDOSS4ETM7 = i;

        while (jDOSS4ETM7 >= hDOSS4ETM7 && allValuesDosS4ETM7[jDOSS4ETM7 - hDOSS4ETM7] > keyDOSS4ETM7)
        {
            allValuesDosS4ETM7[jDOSS4ETM7] = allValuesDosS4ETM7[jDOSS4ETM7 - hDOSS4ETM7];
            jDOSS4ETM7 = jDOSS4ETM7 - hDOSS4ETM7;
        }
        allValuesDosS4ETM7[jDOSS4ETM7] = keyDOSS4ETM7;
    }
    hDOSS4ETM7 = floor(hDOSS4ETM7/3);
}

numeric minValueDosS4ETM7 = allValuesDosS4ETM7[1];
numeric dn1000S4ETM7 = 10000;

numeric actualCountDosS4ETM7 = 0;
for i = 1 to dosS4ETM7realSize 
{
    if (allValuesDosS4ETM7[i] == minValueDosS4ETM7)
    {
        actualCountDosS4ETM7 = actualCountDosS4ETM7 + 1;
        if (actualCountDosS4ETM7 >= DNminCALCS4ETM7)
        {
            dn1000S4ETM7 = minValueDosS4ETM7;
            go to dn1000S4ETM7jumper;
        }
    }
    else
    {
        minValueDosS4ETM7 = allValuesDosS4ETM7[i+1];
        actualCountDosS4ETM7 = 0;
    }
}

dn1000S4ETM7jumper:

if (dn1000S4ETM7  < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4ETM7  = allValuesDosS4ETM7[1];
}
if (dn1000S4ETM7  == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4ETM7  = allValuesDosS4ETM7[1];
}
	
printf("dn1000 Scene 4 ETM7: %12f\n\n", dn1000S4ETM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS4ETM1 = dn1000S4ETM1 - (0.01 * (radsurfETMS4b1 * cos(s4zenith)) / pi);
numeric pathRadS4ETM2 = dn1000S4ETM2 - (0.01 * (radsurfETMS4b2 * cos(s4zenith)) / pi);
numeric pathRadS4ETM3 = dn1000S4ETM3 - (0.01 * (radsurfETMS4b3 * cos(s4zenith)) / pi);
numeric pathRadS4ETM4 = dn1000S4ETM4 - (0.01 * (radsurfETMS4b4 * cos(s4zenith)) / pi);
numeric pathRadS4ETM5 = dn1000S4ETM5 - (0.01 * (radsurfETMS4b5 * cos(s4zenith)) / pi);
numeric pathRadS4ETM7 = dn1000S4ETM7 - (0.01 * (radsurfETMS4b7 * cos(s4zenith)) / pi);

printf("Pathradiance Scene 4 Band 1: %12f\n", pathRadS4ETM1);
printf("Pathradiance Scene 4 Band 2: %12f\n", pathRadS4ETM2);
printf("Pathradiance Scene 4 Band 3: %12f\n", pathRadS4ETM3);
printf("Pathradiance Scene 4 Band 4: %12f\n", pathRadS4ETM4);
printf("Pathradiance Scene 4 Band 5: %12f\n", pathRadS4ETM5);
printf("Pathradiance Scene 4 Band 7: %12f\n\n", pathRadS4ETM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to ETMlins
	 {
	        for j = 1 to ETMcols
	        {
                  valueS4ETM1 = (pi * (RADS4ETM1[i,j] - pathRadS4ETM1)) / (radsurfETMS4b1 * cos(s4zenith));

                  if ( valueS4ETM1 < 0 )
       		{
          			valueS4ETM1 = 0;
				countS4REFnullETM1 = countS4REFnullETM1 + 1;
       		}

			else if ( valueS4ETM1 > 1 )
			{
				valueS4ETM1 = 1;
				countS4REFoneETM1 = countS4REFoneETM1 + 1;
			}
       		
			REFS4ETM1[i,j] = valueS4ETM1;

               valueS4ETM2 = (pi * (RADS4ETM2[i,j] - pathRadS4ETM2)) / (radsurfETMS4b2 * cos(s4zenith));

                  if ( valueS4ETM2 < 0 )
       		{
          			valueS4ETM2 = 0;
				countS4REFnullETM2 = countS4REFnullETM2 + 1;
       		}

			else if ( valueS4ETM2 > 1 )
			{
				valueS4ETM2 = 1;
				countS4REFoneETM2 = countS4REFoneETM2 + 1;
			}
       		
			REFS4ETM2[i,j] = valueS4ETM2;

               valueS4ETM3 = (pi * (RADS4ETM3[i,j] - pathRadS4ETM3)) / (radsurfETMS4b3 * cos(s4zenith));

                  if ( valueS4ETM3 < 0 )
       		{
          			valueS4ETM3 = 0;
				countS4REFnullETM3 = countS4REFnullETM3 + 1;
       		}

			else if ( valueS4ETM3 > 1 )
			{
				valueS4ETM3 = 1;
				countS4REFoneETM3 = countS4REFoneETM3 + 1;
			}
       		
			REFS4ETM3[i,j] = valueS4ETM3;

               valueS4ETM4 = (pi * (RADS4ETM4[i,j] - pathRadS4ETM4)) / (radsurfETMS4b4 * cos(s4zenith));

                  if ( valueS4ETM4 < 0 )
       		{
          			valueS4ETM4 = 0;
				countS4REFnullETM4 = countS4REFnullETM4 + 1;
       		}

			else if ( valueS4ETM4 > 1 )
			{
				valueS4ETM4 = 1;
				countS4REFoneETM4 = countS4REFoneETM4 + 1;
			}
       		
			REFS4ETM4[i,j] = valueS4ETM4;

               valueS4ETM5 = (pi * (RADS4ETM5[i,j] - pathRadS4ETM5)) / (radsurfETMS4b5 * cos(s4zenith));

                  if ( valueS4ETM5 < 0 )
       		{
          			valueS4ETM5 = 0;
				countS4REFnullETM5 = countS4REFnullETM5 + 1;
       		}

			else if ( valueS4ETM5 > 1 )
			{
				valueS4ETM5 = 1;
				countS4REFoneETM5 = countS4REFoneETM5 + 1;
			}
       		
			REFS4ETM5[i,j] = valueS4ETM5;

               valueS4ETM7 = (pi * (RADS4ETM7[i,j] - pathRadS4ETM7)) / (radsurfETMS4b7 * cos(s4zenith));

                  if ( valueS4ETM7 < 0 )
       		{
          			valueS4ETM7 = 0;
				countS4REFnullETM7 = countS4REFnullETM7 + 1;
       		}

			else if ( valueS4ETM7 > 1 )
			{
				valueS4ETM7 = 1;
				countS4REFoneETM7 = countS4REFoneETM7 + 1;
			}
       		
			REFS4ETM7[i,j] = valueS4ETM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullETM1, countS4REFoneETM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullETM2, countS4REFoneETM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullETM3, countS4REFoneETM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullETM4, countS4REFoneETM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullETM5, countS4REFoneETM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS4REFnullETM7, countS4REFoneETM7);

CreatePyramid(REFS4ETM1);
CreatePyramid(REFS4ETM2);
CreatePyramid(REFS4ETM3);
CreatePyramid(REFS4ETM4);
CreatePyramid(REFS4ETM5);
CreatePyramid(REFS4ETM7);
        
CreateHistogram(REFS4ETM1);
CreateHistogram(REFS4ETM2);
CreateHistogram(REFS4ETM3);
CreateHistogram(REFS4ETM4);
CreateHistogram(REFS4ETM5);
CreateHistogram(REFS4ETM7);

CloseRaster(RADS4ETM1);
CloseRaster(RADS4ETM2);
CloseRaster(RADS4ETM3);
CloseRaster(RADS4ETM4);
CloseRaster(RADS4ETM5);
CloseRaster(RADS4ETM7);

printf("Reflectance calculation for Scene 4 (ETM) is done...\n\n\n");
}

else if ( sensors4 == 6 )
{
raster REFS4TM1, REFS4TM2, REFS4TM3, REFS4TM4, REFS4TM5, REFS4TM7;
numeric radsurfTMS4b1, radsurfTMS4b2, radsurfTMS4b3, radsurfTMS4b4, radsurfTMS4b5, radsurfTMS4b7;
numeric valueS4TM1, valueS4TM2, valueS4TM3, valueS4TM4, valueS4TM5, valueS4TM7;

numeric dosS4TM1nullValCount = 0;
numeric dosS4TM2nullValCount = 0;
numeric dosS4TM3nullValCount = 0;
numeric dosS4TM4nullValCount = 0;
numeric dosS4TM5nullValCount = 0;
numeric dosS4TM7nullValCount = 0;

numeric dosS4TM1realArrayCount = 1;
numeric dosS4TM2realArrayCount = 1;
numeric dosS4TM3realArrayCount = 1;
numeric dosS4TM4realArrayCount = 1;
numeric dosS4TM5realArrayCount = 1;
numeric dosS4TM7realArrayCount = 1;

numeric countS4REFnullTM1 = 0;
numeric countS4REFnullTM2 = 0;
numeric countS4REFnullTM3 = 0;
numeric countS4REFnullTM4 = 0;
numeric countS4REFnullTM5 = 0;
numeric countS4REFnullTM7 = 0;

numeric countS4REFoneTM1 = 0;
numeric countS4REFoneTM2 = 0;
numeric countS4REFoneTM3 = 0;
numeric countS4REFoneTM4 = 0;
numeric countS4REFoneTM5 = 0;
numeric countS4REFoneTM7 = 0;

radsurfTMS4b1 = ESTM1 / (dist4^2);
radsurfTMS4b2 = ESTM2 / (dist4^2);
radsurfTMS4b3 = ESTM3 / (dist4^2);
radsurfTMS4b4 = ESTM4 / (dist4^2);
radsurfTMS4b5 = ESTM5 / (dist4^2);
radsurfTMS4b7 = ESTM7 / (dist4^2);

printf("Surface Radiance TM Band 1 in W/m^2µm (Scene 4): %12f\n", radsurfTMS4b1);
printf("Surface Radiance TM Band 2 in W/m^2µm (Scene 4): %12f\n", radsurfTMS4b2);
printf("Surface Radiance TM Band 3 in W/m^2µm (Scene 4): %12f\n", radsurfTMS4b3);
printf("Surface Radiance TM Band 4 in W/m^2µm (Scene 4): %12f\n", radsurfTMS4b4);
printf("Surface Radiance TM Band 5 in W/m^2µm (Scene 4): %12f\n", radsurfTMS4b5);
printf("Surface Radiance TM Band 7 in W/m^2µm (Scene 4): %12f\n\n", radsurfTMS4b7);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {       
		if (IsNull(RADS4TM1[i,j]) == 1)
		{
		dosS4TM1nullValCount = dosS4TM1nullValCount + 1;
		}
		if (IsNull(RADS4TM2[i,j]) == 1)
		{
		dosS4TM2nullValCount = dosS4TM2nullValCount + 1;
		}		 
		if (IsNull(RADS4TM3[i,j]) == 1)
		{
		dosS4TM3nullValCount = dosS4TM3nullValCount + 1;
		}		 
		if (IsNull(RADS4TM4[i,j]) == 1)
		{
		dosS4TM4nullValCount = dosS4TM4nullValCount + 1;
		}		 
		if (IsNull(RADS4TM5[i,j]) == 1)
		{
		dosS4TM5nullValCount = dosS4TM5nullValCount + 1;
		}		 
		if (IsNull(RADS4TM7[i,j]) == 1)
		{
		dosS4TM7nullValCount = dosS4TM7nullValCount + 1;
		}		 	 
    }
}

numeric dosS4TM1realSize = (TMlins * TMcols) - dosS4TM1nullValCount;
numeric dosS4TM2realSize = (TMlins * TMcols) - dosS4TM2nullValCount;
numeric dosS4TM3realSize = (TMlins * TMcols) - dosS4TM3nullValCount;
numeric dosS4TM4realSize = (TMlins * TMcols) - dosS4TM4nullValCount;
numeric dosS4TM5realSize = (TMlins * TMcols) - dosS4TM5nullValCount;
numeric dosS4TM7realSize = (TMlins * TMcols) - dosS4TM7nullValCount;

array allValuesDosS4TM1[dosS4TM1realSize];
array allValuesDosS4TM2[dosS4TM2realSize];
array allValuesDosS4TM3[dosS4TM3realSize];
array allValuesDosS4TM4[dosS4TM4realSize];
array allValuesDosS4TM5[dosS4TM5realSize];
array allValuesDosS4TM7[dosS4TM7realSize];

numeric DNminCALCS4TM1 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM1realSize);
numeric DNminCALCS4TM2 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM2realSize);
numeric DNminCALCS4TM3 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM3realSize);
numeric DNminCALCS4TM4 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM4realSize);
numeric DNminCALCS4TM5 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM5realSize);
numeric DNminCALCS4TM7 = DNminCALC(Scene4Lin, Scene4Col, dosS4TM7realSize);

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(RADS4TM1[i,j]) == 0)
		{
		  allValuesDosS4TM1[dosS4TM1realArrayCount] = RADS4TM1[i,j];		 
		  dosS4TM1realArrayCount = dosS4TM1realArrayCount + 1;
		}
		if (IsNull(RADS4TM2[i,j]) == 0)
		{
		  allValuesDosS4TM2[dosS4TM2realArrayCount] = RADS4TM2[i,j];		 
		  dosS4TM2realArrayCount = dosS4TM2realArrayCount + 1;
		}
		if (IsNull(RADS4TM3[i,j]) == 0)
		{
		  allValuesDosS4TM3[dosS4TM3realArrayCount] = RADS4TM3[i,j];		 
		  dosS4TM3realArrayCount = dosS4TM3realArrayCount + 1;
		}
		if (IsNull(RADS4TM4[i,j]) == 0)
		{
		  allValuesDosS4TM4[dosS4TM4realArrayCount] = RADS4TM4[i,j];		 
		  dosS4TM4realArrayCount = dosS4TM4realArrayCount + 1;
		}
		if (IsNull(RADS4TM5[i,j]) == 0)
		{
		  allValuesDosS4TM5[dosS4TM5realArrayCount] = RADS4TM5[i,j];		 
		  dosS4TM5realArrayCount = dosS4TM5realArrayCount + 1;
		}
		if (IsNull(RADS4TM7[i,j]) == 0)
		{
		  allValuesDosS4TM7[dosS4TM7realArrayCount] = RADS4TM7[i,j];		 
		  dosS4TM7realArrayCount = dosS4TM7realArrayCount + 1;
		}
    }
}

########################### Scene 4 - Dn1000 - Band 1

numeric lastDOSS4TM1 = dosS4TM1realSize;
numeric hDOSS4TM1 = 1;

while ( (hDOSS4TM1 * 3 + 1) < lastDOSS4TM1 - 1 )
{
    hDOSS4TM1 = 3 * hDOSS4TM1 + 1;
}

while ( hDOSS4TM1 > 0 )
{
    for i = hDOSS4TM1 - 1 to lastDOSS4TM1               # for each of the h sets of elements
    {
        numeric keyDOSS4TM1 = allValuesDosS4TM1[i];
        numeric jDOSS4TM1 = i;

        while (jDOSS4TM1 >= hDOSS4TM1 && allValuesDosS4TM1[jDOSS4TM1 - hDOSS4TM1] > keyDOSS4TM1)
        {
            allValuesDosS4TM1[jDOSS4TM1] = allValuesDosS4TM1[jDOSS4TM1 - hDOSS4TM1];
            jDOSS4TM1 = jDOSS4TM1 - hDOSS4TM1;
        }
        allValuesDosS4TM1[jDOSS4TM1] = keyDOSS4TM1;
    }
    hDOSS4TM1 = floor(hDOSS4TM1/3);
}

numeric minValueDosS4TM1 = allValuesDosS4TM1[1];
numeric dn1000S4TM1 = 10000;

numeric actualCountDosS4TM1 = 0;
for i = 1 to dosS4TM1realSize 
{
    if (allValuesDosS4TM1[i] == minValueDosS4TM1)
    {
        actualCountDosS4TM1 = actualCountDosS4TM1 + 1;
        if (actualCountDosS4TM1 >= DNminCALCS4TM1)
        {
            dn1000S4TM1 = minValueDosS4TM1;
            go to dn1000S4TM1jumper;
        }
    }
    else
    {
        minValueDosS4TM1 = allValuesDosS4TM1[i+1];
        actualCountDosS4TM1 = 0;
    }
}

dn1000S4TM1jumper:

if (dn1000S4TM1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM1 = allValuesDosS4TM1[1];
}
if (dn1000S4TM1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM1 = allValuesDosS4TM1[1];
}

printf("dn1000 Scene 4 TM1: %12f\n", dn1000S4TM1);

########################### Scene 4 - Dn1000 - Band 2

numeric lastDOSS4TM2 = dosS4TM1realSize;
numeric hDOSS4TM2 = 1;

while ( (hDOSS4TM2 * 3 + 1) < lastDOSS4TM2 - 1 )
{
    hDOSS4TM2 = 3 * hDOSS4TM2 + 1;
}

while ( hDOSS4TM2 > 0 )
{
    for i = hDOSS4TM2 - 1 to lastDOSS4TM2
    {
        numeric keyDOSS4TM2 = allValuesDosS4TM2[i];
        numeric jDOSS4TM2 = i;

        while (jDOSS4TM2 >= hDOSS4TM2 && allValuesDosS4TM2[jDOSS4TM2 - hDOSS4TM2] > keyDOSS4TM2)
        {
            allValuesDosS4TM2[jDOSS4TM2] = allValuesDosS4TM2[jDOSS4TM2 - hDOSS4TM2];
            jDOSS4TM2 = jDOSS4TM2 - hDOSS4TM2;
        }
        allValuesDosS4TM2[jDOSS4TM2] = keyDOSS4TM2;
    }
    hDOSS4TM2 = floor(hDOSS4TM2/3);
}

numeric minValueDosS4TM2 = allValuesDosS4TM2[1];
numeric dn1000S4TM2 = 10000;

numeric actualCountDosS4TM2 = 0;
for i = 1 to dosS4TM2realSize 
{
    if (allValuesDosS4TM2[i] == minValueDosS4TM2)
    {
        actualCountDosS4TM2 = actualCountDosS4TM2 + 1;
        if (actualCountDosS4TM2 >= DNminCALCS4TM2)
        {
            dn1000S4TM2 = minValueDosS4TM2;
            go to dn1000S4TM2jumper;
        }
    }
    else
    {
        minValueDosS4TM2 = allValuesDosS4TM2[i+1];
        actualCountDosS4TM2 = 0;
    }
}

dn1000S4TM2jumper:

if (dn1000S4TM2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM2 = allValuesDosS4TM2[1];
}
if (dn1000S4TM2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM2 = allValuesDosS4TM2[1];
}

printf("dn1000 Scene 4 TM2: %12f\n", dn1000S4TM2);

########################### Scene 4 - Dn1000 - Band 3

numeric lastDOSS4TM3 = dosS4TM3realSize;
numeric hDOSS4TM3 = 1;

while ( (hDOSS4TM3 * 3 + 1) < lastDOSS4TM3 - 1 )
{
    hDOSS4TM3 = 3 * hDOSS4TM3 + 1;
}

while ( hDOSS4TM3 > 0 )
{
    for i = hDOSS4TM3 - 1 to lastDOSS4TM3               # for each of the h sets of elements
    {
        numeric keyDOSS4TM3 = allValuesDosS4TM3[i];
        numeric jDOSS4TM3 = i;

        while (jDOSS4TM3 >= hDOSS4TM3 && allValuesDosS4TM3[jDOSS4TM3 - hDOSS4TM3] > keyDOSS4TM3)
        {
            allValuesDosS4TM3[jDOSS4TM3] = allValuesDosS4TM3[jDOSS4TM3 - hDOSS4TM3];
            jDOSS4TM3 = jDOSS4TM3 - hDOSS4TM3;
        }
        allValuesDosS4TM3[jDOSS4TM3] = keyDOSS4TM3;
    }
    hDOSS4TM3 = floor(hDOSS4TM3/3);
}

numeric minValueDosS4TM3 = allValuesDosS4TM3[1];
numeric dn1000S4TM3 = 10000;

numeric actualCountDosS4TM3 = 0;
for i = 1 to dosS4TM3realSize 
{
    if (allValuesDosS4TM3[i] == minValueDosS4TM3)
    {
        actualCountDosS4TM3 = actualCountDosS4TM3 + 1;
        if (actualCountDosS4TM3 >= DNminCALCS4TM3)
        {
            dn1000S4TM3 = minValueDosS4TM3;
            go to dn1000S4TM3jumper;
        }
    }
    else
    {
        minValueDosS4TM3 = allValuesDosS4TM3[i+1];
        actualCountDosS4TM3 = 0;
    }
}

dn1000S4TM3jumper:

if (dn1000S4TM3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM3 = allValuesDosS4TM3[1];
}
if (dn1000S4TM3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM3 = allValuesDosS4TM3[1];
}

printf("dn1000 Scene 4 TM3: %12f\n", dn1000S4TM3);

########################### Scene 4 - Dn1000 - Band 4

numeric lastDOSS4TM4 = dosS4TM4realSize;
numeric hDOSS4TM4 = 1;

while ( (hDOSS4TM4 * 3 + 1) < lastDOSS4TM4 - 1 )
{
    hDOSS4TM4 = 3 * hDOSS4TM4 + 1;
}

while ( hDOSS4TM4 > 0 )
{
    for i = hDOSS4TM4 - 1 to lastDOSS4TM4
    {
        numeric keyDOSS4TM4 = allValuesDosS4TM4[i];
        numeric jDOSS4TM4 = i;

        while (jDOSS4TM4 >= hDOSS4TM4 && allValuesDosS4TM4[jDOSS4TM4 - hDOSS4TM4] > keyDOSS4TM4)
        {
            allValuesDosS4TM4[jDOSS4TM4] = allValuesDosS4TM4[jDOSS4TM4 - hDOSS4TM4];
            jDOSS4TM4 = jDOSS4TM4 - hDOSS4TM4;
        }
        allValuesDosS4TM4[jDOSS4TM4] = keyDOSS4TM4;
    }
    hDOSS4TM4 = floor(hDOSS4TM4/3);
}

numeric minValueDosS4TM4 = allValuesDosS4TM4[1];
numeric dn1000S4TM4 = 10000;

numeric actualCountDosS4TM4 = 0;
for i = 1 to dosS4TM4realSize 
{
    if (allValuesDosS4TM4[i] == minValueDosS4TM4)
    {
        actualCountDosS4TM4 = actualCountDosS4TM4 + 1;
        if (actualCountDosS4TM4 >= DNminCALCS4TM4)
        {
            dn1000S4TM4 = minValueDosS4TM4;
            go to dn1000S4TM4jumper;
        }
    }
    else
    {
        minValueDosS4TM4 = allValuesDosS4TM4[i+1];
        actualCountDosS4TM4 = 0;
    }
}

dn1000S4TM4jumper:

if (dn1000S4TM4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM4 = allValuesDosS4TM4[1];
}
if (dn1000S4TM4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM4 = allValuesDosS4TM4[1];
}
	
printf("dn1000 Scene 4 TM4: %12f\n", dn1000S4TM4);

########################### Scene 4 - Dn1000 - Band 5

numeric lastDOSS4TM5 = dosS4TM5realSize;
numeric hDOSS4TM5 = 1;

while ( (hDOSS4TM5 * 3 + 1) < lastDOSS4TM5 - 1 )
{
    hDOSS4TM5 = 3 * hDOSS4TM5 + 1;
}

while ( hDOSS4TM5 > 0 )
{
    for i = hDOSS4TM5 - 1 to lastDOSS4TM5               # for each of the h sets of elements
    {
        numeric keyDOSS4TM5 = allValuesDosS4TM5[i];
        numeric jDOSS4TM5 = i;

        while (jDOSS4TM5 >= hDOSS4TM5 && allValuesDosS4TM5[jDOSS4TM5 - hDOSS4TM5] > keyDOSS4TM5)
        {
            allValuesDosS4TM5[jDOSS4TM5] = allValuesDosS4TM5[jDOSS4TM5 - hDOSS4TM5];
            jDOSS4TM5 = jDOSS4TM5 - hDOSS4TM5;
        }
        allValuesDosS4TM5[jDOSS4TM5] = keyDOSS4TM5;
    }
    hDOSS4TM5 = floor(hDOSS4TM5/3);
}

numeric minValueDosS4TM5 = allValuesDosS4TM5[1];
numeric dn1000S4TM5 = 10000;

numeric actualCountDosS4TM5 = 0;
for i = 1 to dosS4TM5realSize 
{
    if (allValuesDosS4TM5[i] == minValueDosS4TM5)
    {
        actualCountDosS4TM5 = actualCountDosS4TM5 + 1;
        if (actualCountDosS4TM5 >= DNminCALCS4TM5)
        {
            dn1000S4TM5 = minValueDosS4TM5;
            go to dn1000S4TM5jumper;
        }
    }
    else
    {
        minValueDosS4TM5 = allValuesDosS4TM5[i+1];
        actualCountDosS4TM5 = 0;
    }
}

dn1000S4TM5jumper:

if (dn1000S4TM5 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM5 = allValuesDosS4TM5[1];
}
if (dn1000S4TM5 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM5 = allValuesDosS4TM5[1];
}
	
printf("dn1000 Scene 4 TM5: %12f\n", dn1000S4TM5);

########################### Scene 4 - Dn1000 - Band 7

numeric lastDOSS4TM7 = dosS4TM7realSize;
numeric hDOSS4TM7 = 1;

while ( (hDOSS4TM7 * 3 + 1) < lastDOSS4TM7 - 1 )
{
    hDOSS4TM7 = 3 * hDOSS4TM7 + 1;
}

while ( hDOSS4TM7 > 0 )
{
    for i = hDOSS4TM7 - 1 to lastDOSS4TM7               # for each of the h sets of elements
    {
        numeric keyDOSS4TM7 = allValuesDosS4TM7[i];
        numeric jDOSS4TM7 = i;

        while (jDOSS4TM7 >= hDOSS4TM7 && allValuesDosS4TM7[jDOSS4TM7 - hDOSS4TM7] > keyDOSS4TM7)
        {
            allValuesDosS4TM7[jDOSS4TM7] = allValuesDosS4TM7[jDOSS4TM7 - hDOSS4TM7];
            jDOSS4TM7 = jDOSS4TM7 - hDOSS4TM7;
        }
        allValuesDosS4TM7[jDOSS4TM7] = keyDOSS4TM7;
    }
    hDOSS4TM7 = floor(hDOSS4TM7/3);
}

numeric minValueDosS4TM7 = allValuesDosS4TM7[1];
numeric dn1000S4TM7 = 10000;

numeric actualCountDosS4TM7 = 0;
for i = 1 to dosS4TM7realSize 
{
    if (allValuesDosS4TM7[i] == minValueDosS4TM7)
    {
        actualCountDosS4TM7 = actualCountDosS4TM7 + 1;
        if (actualCountDosS4TM7 >= DNminCALCS4TM7)
        {
            dn1000S4TM7 = minValueDosS4TM7;
            go to dn1000S4TM7jumper;
        }
    }
    else
    {
        minValueDosS4TM7 = allValuesDosS4TM7[i+1];
        actualCountDosS4TM7 = 0;
    }
}

dn1000S4TM7jumper:

if (dn1000S4TM7 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4TM7 = allValuesDosS4TM7[1];
}

if (dn1000S4TM7 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4TM7 = allValuesDosS4TM7[1];
}
	
printf("dn1000 Scene 4 TM7: %12f\n\n", dn1000S4TM7);


################################### Pathradiance Calculation #######################################

numeric pathRadS4TM1 = dn1000S4TM1 - (0.01 * (radsurfTMS4b1 * cos(s4zenith)) / pi);
numeric pathRadS4TM2 = dn1000S4TM2 - (0.01 * (radsurfTMS4b2 * cos(s4zenith)) / pi);
numeric pathRadS4TM3 = dn1000S4TM3 - (0.01 * (radsurfTMS4b3 * cos(s4zenith)) / pi);
numeric pathRadS4TM4 = dn1000S4TM4 - (0.01 * (radsurfTMS4b4 * cos(s4zenith)) / pi);
numeric pathRadS4TM5 = dn1000S4TM5 - (0.01 * (radsurfTMS4b5 * cos(s4zenith)) / pi);
numeric pathRadS4TM7 = dn1000S4TM7 - (0.01 * (radsurfTMS4b7 * cos(s4zenith)) / pi);

printf("Pathradiance Scene 4 Band 1: %12f\n", pathRadS4TM1);
printf("Pathradiance Scene 4 Band 2: %12f\n", pathRadS4TM2);
printf("Pathradiance Scene 4 Band 3: %12f\n", pathRadS4TM3);
printf("Pathradiance Scene 4 Band 4: %12f\n", pathRadS4TM4);
printf("Pathradiance Scene 4 Band 5: %12f\n", pathRadS4TM5);
printf("Pathradiance Scene 4 Band 7: %12f\n\n", pathRadS4TM7);

################################# Reflectance Calculation with athmo correction

	 for i = 1 to TMlins
	 {
	        for j = 1 to TMcols
	        {
                  valueS4TM1 = (pi * (RADS4TM1[i,j] - pathRadS4TM1)) / (radsurfTMS4b1 * cos(s4zenith));

                  if ( valueS4TM1 < 0 )
       		{
          			valueS4TM1 = 0;
				countS4REFnullTM1 = countS4REFnullTM1 + 1;
       		}

			else if ( valueS4TM1 > 1 )
			{
				valueS4TM1 = 1;
				countS4REFoneTM1 = countS4REFoneTM1 + 1;
			}
       		
			REFS4TM1[i,j] = valueS4TM1;

               valueS4TM2 = (pi * (RADS4TM2[i,j] - pathRadS4TM2)) / (radsurfTMS4b2 * cos(s4zenith));

                  if ( valueS4TM2 < 0 )
       		{
          			valueS4TM2 = 0;
				countS4REFnullTM2 = countS4REFnullTM2 + 1;
       		}

			else if ( valueS4TM2 > 1 )
			{
				valueS4TM2 = 1;
				countS4REFoneTM2 = countS4REFoneTM2 + 1;
			}
       		
			REFS4TM2[i,j] = valueS4TM2;

               valueS4TM3 = (pi * (RADS4TM3[i,j] - pathRadS4TM3)) / (radsurfTMS4b3 * cos(s4zenith));

                  if ( valueS4TM3 < 0 )
       		{
          			valueS4TM3 = 0;
				countS4REFnullTM3 = countS4REFnullTM3 + 1;
       		}

			else if ( valueS4TM3 > 1 )
			{
				valueS4TM3 = 1;
				countS4REFoneTM3 = countS4REFoneTM3 + 1;
			}
       		
			REFS4TM3[i,j] = valueS4TM3;

               valueS4TM4 = (pi * (RADS4TM4[i,j] - pathRadS4TM4)) / (radsurfTMS4b4 * cos(s4zenith));

                  if ( valueS4TM4 < 0 )
       		{
          			valueS4TM4 = 0;
				countS4REFnullTM4 = countS4REFnullTM4 + 1;
       		}

			else if ( valueS4TM4 > 1 )
			{
				valueS4TM4 = 1;
				countS4REFoneTM4 = countS4REFoneTM4 + 1;
			}
       		
			REFS4TM4[i,j] = valueS4TM4;

               valueS4TM5 = (pi * (RADS4TM5[i,j] - pathRadS4TM5)) / (radsurfTMS4b5 * cos(s4zenith));

                  if ( valueS4TM5 < 0 )
       		{
          			valueS4TM5 = 0;
				countS4REFnullTM5 = countS4REFnullTM5 + 1;
       		}

			else if ( valueS4TM5 > 1 )
			{
				valueS4TM5 = 1;
				countS4REFoneTM5 = countS4REFoneTM5 + 1;
			}
       		
			REFS4TM5[i,j] = valueS4TM5;

               valueS4TM7 = (pi * (RADS4TM7[i,j] - pathRadS4TM7)) / (radsurfTMS4b7 * cos(s4zenith));

                  if ( valueS4TM7 < 0 )
       		{
          			valueS4TM7 = 0;
				countS4REFnullTM7 = countS4REFnullTM7 + 1;
       		}

			else if ( valueS4TM7 > 1 )
			{
				valueS4TM7 = 1;
				countS4REFoneTM7 = countS4REFoneTM7 + 1;
			}
       		
			REFS4TM7[i,j] = valueS4TM7;
	        }
	 }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullTM1, countS4REFoneTM1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullTM2, countS4REFoneTM2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullTM3, countS4REFoneTM3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullTM4, countS4REFoneTM4);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 5: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullTM5, countS4REFoneTM5);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 7: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n\n", countS4REFnullTM7, countS4REFoneTM7);

CreatePyramid(REFS4TM1);
CreatePyramid(REFS4TM2);
CreatePyramid(REFS4TM3);
CreatePyramid(REFS4TM4);
CreatePyramid(REFS4TM5);
CreatePyramid(REFS4TM7);
        
CreateHistogram(REFS4TM1);
CreateHistogram(REFS4TM2);
CreateHistogram(REFS4TM3);
CreateHistogram(REFS4TM4);
CreateHistogram(REFS4TM5);
CreateHistogram(REFS4TM7);

CloseRaster(RADS4TM1);
CloseRaster(RADS4TM2);
CloseRaster(RADS4TM3);
CloseRaster(RADS4TM4);
CloseRaster(RADS4TM5);
CloseRaster(RADS4TM7);

printf("Reflectance calculation for Scene 4 (TM) is done...\n\n\n");
}

else if ( sensors4 == 5 )                                               # MSS 5 - Calib Werte nach Price J.C.1987
{
raster REFS4MSS1, REFS4MSS2, REFS4MSS3, REFS4MSS4;
numeric radsurfMSS4b1, radsurfMSS4b2, radsurfMSS4b3, radsurfMSS4b4;
numeric valueS4MSS1, valueS4MSS2, valueS4MSS3, valueS4MSS4;

numeric dosS4MSS1nullValCount = 0;
numeric dosS4MSS2nullValCount = 0;
numeric dosS4MSS3nullValCount = 0;
numeric dosS4MSS4nullValCount = 0;

numeric dosS4MSS1realArrayCount = 1;
numeric dosS4MSS2realArrayCount = 1;
numeric dosS4MSS3realArrayCount = 1;
numeric dosS4MSS4realArrayCount = 1;

numeric countS4REFnullMSS1 = 0;
numeric countS4REFnullMSS2 = 0;
numeric countS4REFnullMSS3 = 0;
numeric countS4REFnullMSS4 = 0;

numeric countS4REFoneMSS1 = 0;
numeric countS4REFoneMSS2 = 0;
numeric countS4REFoneMSS3 = 0;
numeric countS4REFoneMSS4 = 0;

radsurfMSS4b1 = ESMSS51 / (dist4^2);
radsurfMSS4b2 = ESMSS52 / (dist4^2);
radsurfMSS4b3 = ESMSS53 / (dist4^2);
radsurfMSS4b4 = ESMSS54 / (dist4^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 4): %12f\n\n", radsurfMSS4b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS4MSS1[i,j]) == 1)
		{
		dosS4MSS1nullValCount = dosS4MSS1nullValCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 1)
		{
		dosS4MSS2nullValCount = dosS4MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS3[i,j]) == 1)
		{
		dosS4MSS3nullValCount = dosS4MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS4[i,j]) == 1)
		{
		dosS4MSS4nullValCount = dosS4MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS4MSS1realSize = (MSSlins * MSScols) - dosS4MSS1nullValCount;
numeric dosS4MSS2realSize = (MSSlins * MSScols) - dosS4MSS2nullValCount;
numeric dosS4MSS3realSize = (MSSlins * MSScols) - dosS4MSS3nullValCount;
numeric dosS4MSS4realSize = (MSSlins * MSScols) - dosS4MSS4nullValCount;

array allValuesDosS4MSS1[dosS4MSS1realSize];
array allValuesDosS4MSS2[dosS4MSS2realSize];
array allValuesDosS4MSS3[dosS4MSS3realSize];
array allValuesDosS4MSS4[dosS4MSS4realSize];

numeric DNminCALCS4MSS1 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS1realSize);
numeric DNminCALCS4MSS2 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS2realSize);
numeric DNminCALCS4MSS3 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS3realSize);
numeric DNminCALCS4MSS4 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS4MSS1[i,j]) == 0)
		{
		  allValuesDosS4MSS1[dosS4MSS1realArrayCount] = RADS4MSS1[i,j];		 
		  dosS4MSS1realArrayCount = dosS4MSS1realArrayCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 0)
		{
		  allValuesDosS4MSS2[dosS4MSS2realArrayCount] = RADS4MSS2[i,j];		 
		  dosS4MSS2realArrayCount = dosS4MSS2realArrayCount + 1;
		}
		if (IsNull(RADS4MSS3[i,j]) == 0)
		{
		  allValuesDosS4MSS3[dosS4MSS3realArrayCount] = RADS4MSS3[i,j];		 
		  dosS4MSS3realArrayCount = dosS4MSS3realArrayCount + 1;
		}
		if (IsNull(RADS4MSS4[i,j]) == 0)
		{
		  allValuesDosS4MSS4[dosS4MSS4realArrayCount] = RADS4MSS4[i,j];		 
		  dosS4MSS4realArrayCount = dosS4MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 4 - Dn1000 - Band 1

numeric lastDOSS4MSS1 = dosS4MSS1realSize;
numeric hDOSS4MSS1 = 1;

while ( (hDOSS4MSS1 * 3 + 1) < lastDOSS4MSS1 - 1 )
{
    hDOSS4MSS1 = 3 * hDOSS4MSS1 + 1;
}

while ( hDOSS4MSS1 > 0 )
{
    for i = hDOSS4MSS1 - 1 to lastDOSS4MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS1 = allValuesDosS4MSS1[i];
        numeric jDOSS4MSS1 = i;

        while (jDOSS4MSS1 >= hDOSS4MSS1 && allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1] > keyDOSS4MSS1)
        {
            allValuesDosS4MSS1[jDOSS4MSS1] = allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1];
            jDOSS4MSS1 = jDOSS4MSS1 - hDOSS4MSS1;
        }
        allValuesDosS4MSS1[jDOSS4MSS1] = keyDOSS4MSS1;
    }
    hDOSS4MSS1 = floor(hDOSS4MSS1/3);
}

numeric minValueDosS4MSS1 = allValuesDosS4MSS1[1];
numeric dn1000S4MSS1 = 10000;

numeric actualCountDosS4MSS1 = 0;
for i = 1 to dosS4MSS1realSize 
{
    if (allValuesDosS4MSS1[i] == minValueDosS4MSS1)
    {
        actualCountDosS4MSS1 = actualCountDosS4MSS1 + 1;
        if (actualCountDosS4MSS1 >= DNminCALCS4MSS1)
        {
            dn1000S4MSS1 = minValueDosS4MSS1;
            go to dn1000S4MSS1jumper;
        }
    }
    else
    {
        minValueDosS4MSS1 = allValuesDosS4MSS1[i+1];
        actualCountDosS4MSS1 = 0;
    }
}

dn1000S4MSS1jumper:

if (dn1000S4MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
if (dn1000S4MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
	
printf("dn1000 Scene 4 MSS1: %12f\n", dn1000S4MSS1);

########################### Scene 4 - Dn1000 - Band 2

numeric lastDOSS4MSS2 = dosS4MSS1realSize;
numeric hDOSS4MSS2 = 1;

while ( (hDOSS4MSS2 * 3 + 1) < lastDOSS4MSS2 - 1 )
{
    hDOSS4MSS2 = 3 * hDOSS4MSS2 + 1;
}

while ( hDOSS4MSS2 > 0 )
{
    for i = hDOSS4MSS2 - 1 to lastDOSS4MSS2
    {
        numeric keyDOSS4MSS2 = allValuesDosS4MSS2[i];
        numeric jDOSS4MSS2 = i;

        while (jDOSS4MSS2 >= hDOSS4MSS2 && allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2] > keyDOSS4MSS2)
        {
            allValuesDosS4MSS2[jDOSS4MSS2] = allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2];
            jDOSS4MSS2 = jDOSS4MSS2 - hDOSS4MSS2;
        }
        allValuesDosS4MSS2[jDOSS4MSS2] = keyDOSS4MSS2;
    }
    hDOSS4MSS2 = floor(hDOSS4MSS2/3);
}

numeric minValueDosS4MSS2 = allValuesDosS4MSS2[1];
numeric dn1000S4MSS2 = 10000;

numeric actualCountDosS4MSS2 = 0;
for i = 1 to dosS4MSS2realSize 
{
    if (allValuesDosS4MSS2[i] == minValueDosS4MSS2)
    {
        actualCountDosS4MSS2 = actualCountDosS4MSS2 + 1;
        if (actualCountDosS4MSS2 >= DNminCALCS4MSS2)
        {
            dn1000S4MSS2 = minValueDosS4MSS2;
            go to dn1000S4MSS2jumper;
        }
    }
    else
    {
        minValueDosS4MSS2 = allValuesDosS4MSS2[i+1];
        actualCountDosS4MSS2 = 0;
    }
}

dn1000S4MSS2jumper:

if (dn1000S4MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}
if (dn1000S4MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}

printf("dn1000 Scene 4 MSS2: %12f\n", dn1000S4MSS2);

########################### Scene 4 - Dn1000 - Band 3

numeric lastDOSS4MSS3 = dosS4MSS3realSize;
numeric hDOSS4MSS3 = 1;

while ( (hDOSS4MSS3 * 3 + 1) < lastDOSS4MSS3 - 1 )
{
    hDOSS4MSS3 = 3 * hDOSS4MSS3 + 1;
}

while ( hDOSS4MSS3 > 0 )
{
    for i = hDOSS4MSS3 - 1 to lastDOSS4MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS3 = allValuesDosS4MSS3[i];
        numeric jDOSS4MSS3 = i;

        while (jDOSS4MSS3 >= hDOSS4MSS3 && allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3] > keyDOSS4MSS3)
        {
            allValuesDosS4MSS3[jDOSS4MSS3] = allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3];
            jDOSS4MSS3 = jDOSS4MSS3 - hDOSS4MSS3;
        }
        allValuesDosS4MSS3[jDOSS4MSS3] = keyDOSS4MSS3;
    }
    hDOSS4MSS3 = floor(hDOSS4MSS3/3);
}

numeric minValueDosS4MSS3 = allValuesDosS4MSS3[1];
numeric dn1000S4MSS3 = 10000;

numeric actualCountDosS4MSS3 = 0;
for i = 1 to dosS4MSS3realSize 
{
    if (allValuesDosS4MSS3[i] == minValueDosS4MSS3)
    {
        actualCountDosS4MSS3 = actualCountDosS4MSS3 + 1;
        if (actualCountDosS4MSS3 >= DNminCALCS4MSS3)
        {
            dn1000S4MSS3 = minValueDosS4MSS3;
            go to dn1000S4MSS3jumper;
        }
    }
    else
    {
        minValueDosS4MSS3 = allValuesDosS4MSS3[i+1];
        actualCountDosS4MSS3 = 0;
    }
}

dn1000S4MSS3jumper:

if (dn1000S4MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}
if (dn1000S4MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}
	
printf("dn1000 Scene 4 MSS3: %12f\n", dn1000S4MSS3);

########################### Scene 4 - Dn1000 - Band 4

numeric lastDOSS4MSS4 = dosS4MSS4realSize;
numeric hDOSS4MSS4 = 1;

while ( (hDOSS4MSS4 * 3 + 1) < lastDOSS4MSS4 - 1 )
{
    hDOSS4MSS4 = 3 * hDOSS4MSS4 + 1;
}

while ( hDOSS4MSS4 > 0 )
{
    for i = hDOSS4MSS4 - 1 to lastDOSS4MSS4
    {
        numeric keyDOSS4MSS4 = allValuesDosS4MSS4[i];
        numeric jDOSS4MSS4 = i;

        while (jDOSS4MSS4 >= hDOSS4MSS4 && allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4] > keyDOSS4MSS4)
        {
            allValuesDosS4MSS4[jDOSS4MSS4] = allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4];
            jDOSS4MSS4 = jDOSS4MSS4 - hDOSS4MSS4;
        }
        allValuesDosS4MSS4[jDOSS4MSS4] = keyDOSS4MSS4;
    }
    hDOSS4MSS4 = floor(hDOSS4MSS4/3);
}

numeric minValueDosS4MSS4 = allValuesDosS4MSS4[1];
numeric dn1000S4MSS4 = 10000;

numeric actualCountDosS4MSS4 = 0;
for i = 1 to dosS4MSS4realSize 
{
    if (allValuesDosS4MSS4[i] == minValueDosS4MSS4)
    {
        actualCountDosS4MSS4 = actualCountDosS4MSS4 + 1;
        if (actualCountDosS4MSS4 >= DNminCALCS4MSS4)
        {
            dn1000S4MSS4 = minValueDosS4MSS4;
            go to dn1000S4MSS4jumper;
        }
    }
    else
    {
        minValueDosS4MSS4 = allValuesDosS4MSS4[i+1];
        actualCountDosS4MSS4 = 0;
    }
}

dn1000S4MSS4jumper:

if (dn1000S4MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}
if (dn1000S4MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}

printf("dn1000 Scene 4 MSS4: %12f\n", dn1000S4MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS4MSS1 = dn1000S4MSS1 - (0.01 * (radsurfMSS4b1 * cos(s4zenith)) / pi);
numeric pathRadS4MSS2 = dn1000S4MSS2 - (0.01 * (radsurfMSS4b2 * cos(s4zenith)) / pi);
numeric pathRadS4MSS3 = dn1000S4MSS3 - (0.01 * (radsurfMSS4b3 * cos(s4zenith)) / pi);
numeric pathRadS4MSS4 = dn1000S4MSS4 - (0.01 * (radsurfMSS4b4 * cos(s4zenith)) / pi);

printf("Pathradiance Scene 4 Band 1: %12f\n", pathRadS4MSS1);
printf("Pathradiance Scene 4 Band 2: %12f\n", pathRadS4MSS2);
printf("Pathradiance Scene 4 Band 3: %12f\n", pathRadS4MSS3);
printf("Pathradiance Scene 4 Band 4: %12f\n\n", pathRadS4MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS4MSS1 = (pi * (RADS4MSS1[i,j] - pathRadS4MSS1)) / (radsurfMSS4b1 * cos(s4zenith));

                  if ( valueS4MSS1 < 0 )
       		{
          			valueS4MSS1 = 0;
				countS4REFnullMSS1 = countS4REFnullMSS1 + 1;
       		}

			else if ( valueS4MSS1 > 1 )
			{
				valueS4MSS1 = 1;
				countS4REFoneMSS1 = countS4REFoneMSS1 + 1;
			}
       		
			REFS4MSS1[i,j] = valueS4MSS1;

               valueS4MSS2 = (pi * (RADS4MSS2[i,j] - pathRadS4MSS2)) / (radsurfMSS4b2 * cos(s4zenith));

                  if ( valueS4MSS2 < 0 )
       		{
          			valueS4MSS2 = 0;
				countS4REFnullMSS2 = countS4REFnullMSS2 + 1;
       		}

			else if ( valueS4MSS2 > 1 )
			{
				valueS4MSS2 = 1;
				countS4REFoneMSS2 = countS4REFoneMSS2 + 1;
			}
       		
			REFS4MSS2[i,j] = valueS4MSS2;

               valueS4MSS3 = (pi * (RADS4MSS3[i,j] - pathRadS4MSS3)) / (radsurfMSS4b3 * cos(s4zenith));

                  if ( valueS4MSS3 < 0 )
       		{
          			valueS4MSS3 = 0;
				countS4REFnullMSS3 = countS4REFnullMSS3 + 1;
       		}

			else if ( valueS4MSS3 > 1 )
			{
				valueS4MSS3 = 1;
				countS4REFoneMSS3 = countS4REFoneMSS3 + 1;
			}
       		
			REFS4MSS3[i,j] = valueS4MSS3;

               valueS4MSS4 = (pi * (RADS4MSS4[i,j] - pathRadS4MSS4)) / (radsurfMSS4b4 * cos(s4zenith));

                  if ( valueS4MSS4 < 0 )
       		{
          			valueS4MSS4 = 0;
				countS4REFnullMSS4 = countS4REFnullMSS4 + 1;
       		}

			else if ( valueS4MSS4 > 1 )
			{
				valueS4MSS4 = 1;
				countS4REFoneMSS4 = countS4REFoneMSS4 + 1;
			}
       		
			REFS4MSS4[i,j] = valueS4MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS1, countS4REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS2, countS4REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS3, countS4REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS4, countS4REFoneMSS4);

CreatePyramid(REFS4MSS1);
CreatePyramid(REFS4MSS2);
CreatePyramid(REFS4MSS3);
CreatePyramid(REFS4MSS4);
        
CreateHistogram(REFS4MSS1);
CreateHistogram(REFS4MSS2);
CreateHistogram(REFS4MSS3);
CreateHistogram(REFS4MSS4);

CloseRaster(RADS4MSS1);
CloseRaster(RADS4MSS2);
CloseRaster(RADS4MSS3);
CloseRaster(RADS4MSS4);

printf("Reflectance calculation for Scene 4 (MSS) is done...\n\n\n");
}

else if ( sensors4 == 4 )                                               # MSS 4
{
raster REFS4MSS1, REFS4MSS2, REFS4MSS3, REFS4MSS4;
numeric radsurfMSS4b1, radsurfMSS4b2, radsurfMSS4b3, radsurfMSS4b4;
numeric valueS4MSS1, valueS4MSS2, valueS4MSS3, valueS4MSS4;

numeric dosS4MSS1nullValCount = 0;
numeric dosS4MSS2nullValCount = 0;
numeric dosS4MSS3nullValCount = 0;
numeric dosS4MSS4nullValCount = 0;

numeric dosS4MSS1realArrayCount = 1;
numeric dosS4MSS2realArrayCount = 1;
numeric dosS4MSS3realArrayCount = 1;
numeric dosS4MSS4realArrayCount = 1;

numeric countS4REFnullMSS1 = 0;
numeric countS4REFnullMSS2 = 0;
numeric countS4REFnullMSS3 = 0;
numeric countS4REFnullMSS4 = 0;

numeric countS4REFoneMSS1 = 0;
numeric countS4REFoneMSS2 = 0;
numeric countS4REFoneMSS3 = 0;
numeric countS4REFoneMSS4 = 0;

radsurfMSS4b1 = ESMSS41 / (dist4^2);
radsurfMSS4b2 = ESMSS42 / (dist4^2);
radsurfMSS4b3 = ESMSS43 / (dist4^2);
radsurfMSS4b4 = ESMSS44 / (dist4^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 4): %12f\n\n", radsurfMSS4b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS4MSS1[i,j]) == 1)
		{
		dosS4MSS1nullValCount = dosS4MSS1nullValCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 1)
		{
		dosS4MSS2nullValCount = dosS4MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS3[i,j]) == 1)
		{
		dosS4MSS3nullValCount = dosS4MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS4[i,j]) == 1)
		{
		dosS4MSS4nullValCount = dosS4MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS4MSS1realSize = (MSSlins * MSScols) - dosS4MSS1nullValCount;
numeric dosS4MSS2realSize = (MSSlins * MSScols) - dosS4MSS2nullValCount;
numeric dosS4MSS3realSize = (MSSlins * MSScols) - dosS4MSS3nullValCount;
numeric dosS4MSS4realSize = (MSSlins * MSScols) - dosS4MSS4nullValCount;

array allValuesDosS4MSS1[dosS4MSS1realSize];
array allValuesDosS4MSS2[dosS4MSS2realSize];
array allValuesDosS4MSS3[dosS4MSS3realSize];
array allValuesDosS4MSS4[dosS4MSS4realSize];

numeric DNminCALCS4MSS1 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS1realSize);
numeric DNminCALCS4MSS2 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS2realSize);
numeric DNminCALCS4MSS3 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS3realSize);
numeric DNminCALCS4MSS4 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS4MSS1[i,j]) == 0)
		{
		  allValuesDosS4MSS1[dosS4MSS1realArrayCount] = RADS4MSS1[i,j];		 
		  dosS4MSS1realArrayCount = dosS4MSS1realArrayCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 0)
		{
		  allValuesDosS4MSS2[dosS4MSS2realArrayCount] = RADS4MSS2[i,j];		 
		  dosS4MSS2realArrayCount = dosS4MSS2realArrayCount + 1;
		}
		if (IsNull(RADS4MSS3[i,j]) == 0)
		{
		  allValuesDosS4MSS3[dosS4MSS3realArrayCount] = RADS4MSS3[i,j];		 
		  dosS4MSS3realArrayCount = dosS4MSS3realArrayCount + 1;
		}
		if (IsNull(RADS4MSS4[i,j]) == 0)
		{
		  allValuesDosS4MSS4[dosS4MSS4realArrayCount] = RADS4MSS4[i,j];		 
		  dosS4MSS4realArrayCount = dosS4MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 4 - Dn1000 - Band 1

numeric lastDOSS4MSS1 = dosS4MSS1realSize;
numeric hDOSS4MSS1 = 1;

while ( (hDOSS4MSS1 * 3 + 1) < lastDOSS4MSS1 - 1 )
{
    hDOSS4MSS1 = 3 * hDOSS4MSS1 + 1;
}

while ( hDOSS4MSS1 > 0 )
{
    for i = hDOSS4MSS1 - 1 to lastDOSS4MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS1 = allValuesDosS4MSS1[i];
        numeric jDOSS4MSS1 = i;

        while (jDOSS4MSS1 >= hDOSS4MSS1 && allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1] > keyDOSS4MSS1)
        {
            allValuesDosS4MSS1[jDOSS4MSS1] = allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1];
            jDOSS4MSS1 = jDOSS4MSS1 - hDOSS4MSS1;
        }
        allValuesDosS4MSS1[jDOSS4MSS1] = keyDOSS4MSS1;
    }
    hDOSS4MSS1 = floor(hDOSS4MSS1/3);
}

numeric minValueDosS4MSS1 = allValuesDosS4MSS1[1];
numeric dn1000S4MSS1 = 10000;

numeric actualCountDosS4MSS1 = 0;
for i = 1 to dosS4MSS1realSize 
{
    if (allValuesDosS4MSS1[i] == minValueDosS4MSS1)
    {
        actualCountDosS4MSS1 = actualCountDosS4MSS1 + 1;
        if (actualCountDosS4MSS1 >= DNminCALCS4MSS1)
        {
            dn1000S4MSS1 = minValueDosS4MSS1;
            go to dn1000S4MSS1jumper;
        }
    }
    else
    {
        minValueDosS4MSS1 = allValuesDosS4MSS1[i+1];
        actualCountDosS4MSS1 = 0;
    }
}

dn1000S4MSS1jumper:

if (dn1000S4MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
if (dn1000S4MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
	
printf("dn1000 Scene 4 MSS1: %12f\n", dn1000S4MSS1);

########################### Scene 4 - Dn1000 - Band 2

numeric lastDOSS4MSS2 = dosS4MSS1realSize;
numeric hDOSS4MSS2 = 1;

while ( (hDOSS4MSS2 * 3 + 1) < lastDOSS4MSS2 - 1 )
{
    hDOSS4MSS2 = 3 * hDOSS4MSS2 + 1;
}

while ( hDOSS4MSS2 > 0 )
{
    for i = hDOSS4MSS2 - 1 to lastDOSS4MSS2
    {
        numeric keyDOSS4MSS2 = allValuesDosS4MSS2[i];
        numeric jDOSS4MSS2 = i;

        while (jDOSS4MSS2 >= hDOSS4MSS2 && allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2] > keyDOSS4MSS2)
        {
            allValuesDosS4MSS2[jDOSS4MSS2] = allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2];
            jDOSS4MSS2 = jDOSS4MSS2 - hDOSS4MSS2;
        }
        allValuesDosS4MSS2[jDOSS4MSS2] = keyDOSS4MSS2;
    }
    hDOSS4MSS2 = floor(hDOSS4MSS2/3);
}

numeric minValueDosS4MSS2 = allValuesDosS4MSS2[1];
numeric dn1000S4MSS2 = 10000;

numeric actualCountDosS4MSS2 = 0;
for i = 1 to dosS4MSS2realSize 
{
    if (allValuesDosS4MSS2[i] == minValueDosS4MSS2)
    {
        actualCountDosS4MSS2 = actualCountDosS4MSS2 + 1;
        if (actualCountDosS4MSS2 >= DNminCALCS4MSS2)
        {
            dn1000S4MSS2 = minValueDosS4MSS2;
            go to dn1000S4MSS2jumper;
        }
    }
    else
    {
        minValueDosS4MSS2 = allValuesDosS4MSS2[i+1];
        actualCountDosS4MSS2 = 0;
    }
}

dn1000S4MSS2jumper:

if (dn1000S4MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}
if (dn1000S4MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}

printf("dn1000 Scene 4 MSS2: %12f\n", dn1000S4MSS2);

########################### Scene 4 - Dn1000 - Band 3

numeric lastDOSS4MSS3 = dosS4MSS3realSize;
numeric hDOSS4MSS3 = 1;

while ( (hDOSS4MSS3 * 3 + 1) < lastDOSS4MSS3 - 1 )
{
    hDOSS4MSS3 = 3 * hDOSS4MSS3 + 1;
}

while ( hDOSS4MSS3 > 0 )
{
    for i = hDOSS4MSS3 - 1 to lastDOSS4MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS3 = allValuesDosS4MSS3[i];
        numeric jDOSS4MSS3 = i;

        while (jDOSS4MSS3 >= hDOSS4MSS3 && allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3] > keyDOSS4MSS3)
        {
            allValuesDosS4MSS3[jDOSS4MSS3] = allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3];
            jDOSS4MSS3 = jDOSS4MSS3 - hDOSS4MSS3;
        }
        allValuesDosS4MSS3[jDOSS4MSS3] = keyDOSS4MSS3;
    }
    hDOSS4MSS3 = floor(hDOSS4MSS3/3);
}

numeric minValueDosS4MSS3 = allValuesDosS4MSS3[1];
numeric dn1000S4MSS3 = 10000;

numeric actualCountDosS4MSS3 = 0;
for i = 1 to dosS4MSS3realSize 
{
    if (allValuesDosS4MSS3[i] == minValueDosS4MSS3)
    {
        actualCountDosS4MSS3 = actualCountDosS4MSS3 + 1;
        if (actualCountDosS4MSS3 >= DNminCALCS4MSS3)
        {
            dn1000S4MSS3 = minValueDosS4MSS3;
            go to dn1000S4MSS3jumper;
        }
    }
    else
    {
        minValueDosS4MSS3 = allValuesDosS4MSS3[i+1];
        actualCountDosS4MSS3 = 0;
    }
}

dn1000S4MSS3jumper:

if (dn1000S4MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}
if (dn1000S4MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}

printf("dn1000 Scene 4 MSS3: %12f\n", dn1000S4MSS3);

########################### Scene 4 - Dn1000 - Band 4

numeric lastDOSS4MSS4 = dosS4MSS4realSize;
numeric hDOSS4MSS4 = 1;

while ( (hDOSS4MSS4 * 3 + 1) < lastDOSS4MSS4 - 1 )
{
    hDOSS4MSS4 = 3 * hDOSS4MSS4 + 1;
}

while ( hDOSS4MSS4 > 0 )
{
    for i = hDOSS4MSS4 - 1 to lastDOSS4MSS4
    {
        numeric keyDOSS4MSS4 = allValuesDosS4MSS4[i];
        numeric jDOSS4MSS4 = i;

        while (jDOSS4MSS4 >= hDOSS4MSS4 && allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4] > keyDOSS4MSS4)
        {
            allValuesDosS4MSS4[jDOSS4MSS4] = allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4];
            jDOSS4MSS4 = jDOSS4MSS4 - hDOSS4MSS4;
        }
        allValuesDosS4MSS4[jDOSS4MSS4] = keyDOSS4MSS4;
    }
    hDOSS4MSS4 = floor(hDOSS4MSS4/3);
}

numeric minValueDosS4MSS4 = allValuesDosS4MSS4[1];
numeric dn1000S4MSS4 = 10000;

numeric actualCountDosS4MSS4 = 0;
for i = 1 to dosS4MSS4realSize 
{
    if (allValuesDosS4MSS4[i] == minValueDosS4MSS4)
    {
        actualCountDosS4MSS4 = actualCountDosS4MSS4 + 1;
        if (actualCountDosS4MSS4 >= DNminCALCS4MSS4)
        {
            dn1000S4MSS4 = minValueDosS4MSS4;
            go to dn1000S4MSS4jumper;
        }
    }
    else
    {
        minValueDosS4MSS4 = allValuesDosS4MSS4[i+1];
        actualCountDosS4MSS4 = 0;
    }
}

dn1000S4MSS4jumper:

if (dn1000S4MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}
if (dn1000S4MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}

printf("dn1000 Scene 4 MSS4: %12f\n", dn1000S4MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS4MSS1 = dn1000S4MSS1 - (0.01 * (radsurfMSS4b1 * cos(s4zenith)) / pi);
numeric pathRadS4MSS2 = dn1000S4MSS2 - (0.01 * (radsurfMSS4b2 * cos(s4zenith)) / pi);
numeric pathRadS4MSS3 = dn1000S4MSS3 - (0.01 * (radsurfMSS4b3 * cos(s4zenith)) / pi);
numeric pathRadS4MSS4 = dn1000S4MSS4 - (0.01 * (radsurfMSS4b4 * cos(s4zenith)) / pi);

printf("Pathradiance Scene 4 Band 1: %12f\n", pathRadS4MSS1);
printf("Pathradiance Scene 4 Band 2: %12f\n", pathRadS4MSS2);
printf("Pathradiance Scene 4 Band 3: %12f\n", pathRadS4MSS3);
printf("Pathradiance Scene 4 Band 4: %12f\n\n", pathRadS4MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS4MSS1 = (pi * (RADS4MSS1[i,j] - pathRadS4MSS1)) / (radsurfMSS4b1 * cos(s4zenith));

                  if ( valueS4MSS1 < 0 )
       		{
          			valueS4MSS1 = 0;
				countS4REFnullMSS1 = countS4REFnullMSS1 + 1;
       		}

			else if ( valueS4MSS1 > 1 )
			{
				valueS4MSS1 = 1;
				countS4REFoneMSS1 = countS4REFoneMSS1 + 1;
			}
       		
			REFS4MSS1[i,j] = valueS4MSS1;

               valueS4MSS2 = (pi * (RADS4MSS2[i,j] - pathRadS4MSS2)) / (radsurfMSS4b2 * cos(s4zenith));

                  if ( valueS4MSS2 < 0 )
       		{
          			valueS4MSS2 = 0;
				countS4REFnullMSS2 = countS4REFnullMSS2 + 1;
       		}

			else if ( valueS4MSS2 > 1 )
			{
				valueS4MSS2 = 1;
				countS4REFoneMSS2 = countS4REFoneMSS2 + 1;
			}
       		
			REFS4MSS2[i,j] = valueS4MSS2;

               valueS4MSS3 = (pi * (RADS4MSS3[i,j] - pathRadS4MSS3)) / (radsurfMSS4b3 * cos(s4zenith));

                  if ( valueS4MSS3 < 0 )
       		{
          			valueS4MSS3 = 0;
				countS4REFnullMSS3 = countS4REFnullMSS3 + 1;
       		}

			else if ( valueS4MSS3 > 1 )
			{
				valueS4MSS3 = 1;
				countS4REFoneMSS3 = countS4REFoneMSS3 + 1;
			}
       		
			REFS4MSS3[i,j] = valueS4MSS3;

               valueS4MSS4 = (pi * (RADS4MSS4[i,j] - pathRadS4MSS4)) / (radsurfMSS4b4 * cos(s4zenith));

                  if ( valueS4MSS4 < 0 )
       		{
          			valueS4MSS4 = 0;
				countS4REFnullMSS4 = countS4REFnullMSS4 + 1;
       		}

			else if ( valueS4MSS4 > 1 )
			{
				valueS4MSS4 = 1;
				countS4REFoneMSS4 = countS4REFoneMSS4 + 1;
			}
       		
			REFS4MSS4[i,j] = valueS4MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS1, countS4REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS2, countS4REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS3, countS4REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS4, countS4REFoneMSS4);

CreatePyramid(REFS4MSS1);
CreatePyramid(REFS4MSS2);
CreatePyramid(REFS4MSS3);
CreatePyramid(REFS4MSS4);
        
CreateHistogram(REFS4MSS1);
CreateHistogram(REFS4MSS2);
CreateHistogram(REFS4MSS3);
CreateHistogram(REFS4MSS4);

CloseRaster(RADS4MSS1);
CloseRaster(RADS4MSS2);
CloseRaster(RADS4MSS3);
CloseRaster(RADS4MSS4);

printf("Reflectance calculation for Scene 4 (MSS) is done...\n\n\n");
}

else															# MSS 1, 2 and 3
{
raster REFS4MSS1, REFS4MSS2, REFS4MSS3, REFS4MSS4;
numeric radsurfMSS4b1, radsurfMSS4b2, radsurfMSS4b3, radsurfMSS4b4;
numeric valueS4MSS1, valueS4MSS2, valueS4MSS3, valueS4MSS4;

numeric dosS4MSS1nullValCount = 0;
numeric dosS4MSS2nullValCount = 0;
numeric dosS4MSS3nullValCount = 0;
numeric dosS4MSS4nullValCount = 0;

numeric dosS4MSS1realArrayCount = 1;
numeric dosS4MSS2realArrayCount = 1;
numeric dosS4MSS3realArrayCount = 1;
numeric dosS4MSS4realArrayCount = 1;

numeric countS4REFnullMSS1 = 0;
numeric countS4REFnullMSS2 = 0;
numeric countS4REFnullMSS3 = 0;
numeric countS4REFnullMSS4 = 0;

numeric countS4REFoneMSS1 = 0;
numeric countS4REFoneMSS2 = 0;
numeric countS4REFoneMSS3 = 0;
numeric countS4REFoneMSS4 = 0;

radsurfMSS4b1 = ESMSS11 / (dist4^2);
radsurfMSS4b2 = ESMSS12 / (dist4^2);
radsurfMSS4b3 = ESMSS13 / (dist4^2);
radsurfMSS4b4 = ESMSS14 / (dist4^2);

printf("Surface Radiance MSS Band 1 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b1);
printf("Surface Radiance MSS Band 2 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b2);
printf("Surface Radiance MSS Band 3 in W/m^2µm (Scene 4): %12f\n", radsurfMSS4b3);
printf("Surface Radiance MSS Band 4 in W/m^2µm (Scene 4): %12f\n\n", radsurfMSS4b4);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {       
		if (IsNull(RADS4MSS1[i,j]) == 1)
		{
		dosS4MSS1nullValCount = dosS4MSS1nullValCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 1)
		{
		dosS4MSS2nullValCount = dosS4MSS2nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS3[i,j]) == 1)
		{
		dosS4MSS3nullValCount = dosS4MSS3nullValCount + 1;
		}		 
		if (IsNull(RADS4MSS4[i,j]) == 1)
		{
		dosS4MSS4nullValCount = dosS4MSS4nullValCount + 1;
		}		 	 	 
    }
}

numeric dosS4MSS1realSize = (MSSlins * MSScols) - dosS4MSS1nullValCount;
numeric dosS4MSS2realSize = (MSSlins * MSScols) - dosS4MSS2nullValCount;
numeric dosS4MSS3realSize = (MSSlins * MSScols) - dosS4MSS3nullValCount;
numeric dosS4MSS4realSize = (MSSlins * MSScols) - dosS4MSS4nullValCount;

array allValuesDosS4MSS1[dosS4MSS1realSize];
array allValuesDosS4MSS2[dosS4MSS2realSize];
array allValuesDosS4MSS3[dosS4MSS3realSize];
array allValuesDosS4MSS4[dosS4MSS4realSize];

numeric DNminCALCS4MSS1 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS1realSize);
numeric DNminCALCS4MSS2 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS2realSize);
numeric DNminCALCS4MSS3 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS3realSize);
numeric DNminCALCS4MSS4 = DNminCALC(Scene4Lin, Scene4Col, dosS4MSS4realSize);

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(RADS4MSS1[i,j]) == 0)
		{
		  allValuesDosS4MSS1[dosS4MSS1realArrayCount] = RADS4MSS1[i,j];		 
		  dosS4MSS1realArrayCount = dosS4MSS1realArrayCount + 1;
		}
		if (IsNull(RADS4MSS2[i,j]) == 0)
		{
		  allValuesDosS4MSS2[dosS4MSS2realArrayCount] = RADS4MSS2[i,j];		 
		  dosS4MSS2realArrayCount = dosS4MSS2realArrayCount + 1;
		}
		if (IsNull(RADS4MSS3[i,j]) == 0)
		{
		  allValuesDosS4MSS3[dosS4MSS3realArrayCount] = RADS4MSS3[i,j];		 
		  dosS4MSS3realArrayCount = dosS4MSS3realArrayCount + 1;
		}
		if (IsNull(RADS4MSS4[i,j]) == 0)
		{
		  allValuesDosS4MSS4[dosS4MSS4realArrayCount] = RADS4MSS4[i,j];		 
		  dosS4MSS4realArrayCount = dosS4MSS4realArrayCount + 1;
		}
    }
}

########################### Scene 4 - Dn1000 - Band 1

numeric lastDOSS4MSS1 = dosS4MSS1realSize;
numeric hDOSS4MSS1 = 1;

while ( (hDOSS4MSS1 * 3 + 1) < lastDOSS4MSS1 - 1 )
{
    hDOSS4MSS1 = 3 * hDOSS4MSS1 + 1;
}

while ( hDOSS4MSS1 > 0 )
{
    for i = hDOSS4MSS1 - 1 to lastDOSS4MSS1               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS1 = allValuesDosS4MSS1[i];
        numeric jDOSS4MSS1 = i;

        while (jDOSS4MSS1 >= hDOSS4MSS1 && allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1] > keyDOSS4MSS1)
        {
            allValuesDosS4MSS1[jDOSS4MSS1] = allValuesDosS4MSS1[jDOSS4MSS1 - hDOSS4MSS1];
            jDOSS4MSS1 = jDOSS4MSS1 - hDOSS4MSS1;
        }
        allValuesDosS4MSS1[jDOSS4MSS1] = keyDOSS4MSS1;
    }
    hDOSS4MSS1 = floor(hDOSS4MSS1/3);
}

numeric minValueDosS4MSS1 = allValuesDosS4MSS1[1];
numeric dn1000S4MSS1 = 10000;

numeric actualCountDosS4MSS1 = 0;
for i = 1 to dosS4MSS1realSize 
{
    if (allValuesDosS4MSS1[i] == minValueDosS4MSS1)
    {
        actualCountDosS4MSS1 = actualCountDosS4MSS1 + 1;
        if (actualCountDosS4MSS1 >= DNminCALCS4MSS1)
        {
            dn1000S4MSS1 = minValueDosS4MSS1;
            go to dn1000S4MSS1jumper;
        }
    }
    else
    {
        minValueDosS4MSS1 = allValuesDosS4MSS1[i+1];
        actualCountDosS4MSS1 = 0;
    }
}

dn1000S4MSS1jumper:

if (dn1000S4MSS1 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
if (dn1000S4MSS1 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS1 = allValuesDosS4MSS1[1];
}
	
printf("dn1000 Scene 4 MSS1: %12f\n", dn1000S4MSS1);

########################### Scene 4 - Dn1000 - Band 2

numeric lastDOSS4MSS2 = dosS4MSS1realSize;
numeric hDOSS4MSS2 = 1;

while ( (hDOSS4MSS2 * 3 + 1) < lastDOSS4MSS2 - 1 )
{
    hDOSS4MSS2 = 3 * hDOSS4MSS2 + 1;
}

while ( hDOSS4MSS2 > 0 )
{
    for i = hDOSS4MSS2 - 1 to lastDOSS4MSS2
    {
        numeric keyDOSS4MSS2 = allValuesDosS4MSS2[i];
        numeric jDOSS4MSS2 = i;

        while (jDOSS4MSS2 >= hDOSS4MSS2 && allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2] > keyDOSS4MSS2)
        {
            allValuesDosS4MSS2[jDOSS4MSS2] = allValuesDosS4MSS2[jDOSS4MSS2 - hDOSS4MSS2];
            jDOSS4MSS2 = jDOSS4MSS2 - hDOSS4MSS2;
        }
        allValuesDosS4MSS2[jDOSS4MSS2] = keyDOSS4MSS2;
    }
    hDOSS4MSS2 = floor(hDOSS4MSS2/3);
}

numeric minValueDosS4MSS2 = allValuesDosS4MSS2[1];
numeric dn1000S4MSS2 = 10000;

numeric actualCountDosS4MSS2 = 0;
for i = 1 to dosS4MSS2realSize 
{
    if (allValuesDosS4MSS2[i] == minValueDosS4MSS2)
    {
        actualCountDosS4MSS2 = actualCountDosS4MSS2 + 1;
        if (actualCountDosS4MSS2 >= DNminCALCS4MSS2)
        {
            dn1000S4MSS2 = minValueDosS4MSS2;
            go to dn1000S4MSS2jumper;
        }
    }
    else
    {
        minValueDosS4MSS2 = allValuesDosS4MSS2[i+1];
        actualCountDosS4MSS2 = 0;
    }
}

dn1000S4MSS2jumper:

if (dn1000S4MSS2 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}
if (dn1000S4MSS2 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS2 = allValuesDosS4MSS2[1];
}

printf("dn1000 Scene 4 MSS2: %12f\n", dn1000S4MSS2);

########################### Scene 4 - Dn1000 - Band 3

numeric lastDOSS4MSS3 = dosS4MSS3realSize;
numeric hDOSS4MSS3 = 1;

while ( (hDOSS4MSS3 * 3 + 1) < lastDOSS4MSS3 - 1 )
{
    hDOSS4MSS3 = 3 * hDOSS4MSS3 + 1;
}

while ( hDOSS4MSS3 > 0 )
{
    for i = hDOSS4MSS3 - 1 to lastDOSS4MSS3               # for each of the h sets of elements
    {
        numeric keyDOSS4MSS3 = allValuesDosS4MSS3[i];
        numeric jDOSS4MSS3 = i;

        while (jDOSS4MSS3 >= hDOSS4MSS3 && allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3] > keyDOSS4MSS3)
        {
            allValuesDosS4MSS3[jDOSS4MSS3] = allValuesDosS4MSS3[jDOSS4MSS3 - hDOSS4MSS3];
            jDOSS4MSS3 = jDOSS4MSS3 - hDOSS4MSS3;
        }
        allValuesDosS4MSS3[jDOSS4MSS3] = keyDOSS4MSS3;
    }
    hDOSS4MSS3 = floor(hDOSS4MSS3/3);
}

numeric minValueDosS4MSS3 = allValuesDosS4MSS3[1];
numeric dn1000S4MSS3 = 10000;

numeric actualCountDosS4MSS3 = 0;
for i = 1 to dosS4MSS3realSize 
{
    if (allValuesDosS4MSS3[i] == minValueDosS4MSS3)
    {
        actualCountDosS4MSS3 = actualCountDosS4MSS3 + 1;
        if (actualCountDosS4MSS3 >= DNminCALCS4MSS3)
        {
            dn1000S4MSS3 = minValueDosS4MSS3;
            go to dn1000S4MSS3jumper;
        }
    }
    else
    {
        minValueDosS4MSS3 = allValuesDosS4MSS3[i+1];
        actualCountDosS4MSS3 = 0;
    }
}

dn1000S4MSS3jumper:

if (dn1000S4MSS3 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}
if (dn1000S4MSS3 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS3 = allValuesDosS4MSS3[1];
}
	
printf("dn1000 Scene 4 MSS3: %12f\n", dn1000S4MSS3);

########################### Scene 4 - Dn1000 - Band 4

numeric lastDOSS4MSS4 = dosS4MSS4realSize;
numeric hDOSS4MSS4 = 1;

while ( (hDOSS4MSS4 * 3 + 1) < lastDOSS4MSS4 - 1 )
{
    hDOSS4MSS4 = 3 * hDOSS4MSS4 + 1;
}

while ( hDOSS4MSS4 > 0 )
{
    for i = hDOSS4MSS4 - 1 to lastDOSS4MSS4
    {
        numeric keyDOSS4MSS4 = allValuesDosS4MSS4[i];
        numeric jDOSS4MSS4 = i;

        while (jDOSS4MSS4 >= hDOSS4MSS4 && allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4] > keyDOSS4MSS4)
        {
            allValuesDosS4MSS4[jDOSS4MSS4] = allValuesDosS4MSS4[jDOSS4MSS4 - hDOSS4MSS4];
            jDOSS4MSS4 = jDOSS4MSS4 - hDOSS4MSS4;
        }
        allValuesDosS4MSS4[jDOSS4MSS4] = keyDOSS4MSS4;
    }
    hDOSS4MSS4 = floor(hDOSS4MSS4/3);
}

numeric minValueDosS4MSS4 = allValuesDosS4MSS4[1];
numeric dn1000S4MSS4 = 10000;

numeric actualCountDosS4MSS4 = 0;
for i = 1 to dosS4MSS4realSize 
{
    if (allValuesDosS4MSS4[i] == minValueDosS4MSS4)
    {
        actualCountDosS4MSS4 = actualCountDosS4MSS4 + 1;
        if (actualCountDosS4MSS4 >= DNminCALCS4MSS4)
        {
            dn1000S4MSS4 = minValueDosS4MSS4;
            go to dn1000S4MSS4jumper;
        }
    }
    else
    {
        minValueDosS4MSS4 = allValuesDosS4MSS4[i+1];
    }
}

dn1000S4MSS4jumper:

if (dn1000S4MSS4 < 0)
{
        printf("dn1000 is negative, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}
if (dn1000S4MSS4 == 10000)
{
        printf("no dn1000 found, setting zero as default\n");
        dn1000S4MSS4 = allValuesDosS4MSS4[1];
}
	
printf("dn1000 Scene 4 MSS4: %12f\n", dn1000S4MSS4);

################################### Pathradiance Calculation #######################################

numeric pathRadS4MSS1 = dn1000S4MSS1 - (0.01 * (radsurfMSS4b1 * cos(s4zenith)) / pi);
numeric pathRadS4MSS2 = dn1000S4MSS2 - (0.01 * (radsurfMSS4b2 * cos(s4zenith)) / pi);
numeric pathRadS4MSS3 = dn1000S4MSS3 - (0.01 * (radsurfMSS4b3 * cos(s4zenith)) / pi);
numeric pathRadS4MSS4 = dn1000S4MSS4 - (0.01 * (radsurfMSS4b4 * cos(s4zenith)) / pi);

printf("Pathradiance Scene 4 Band 1: %12f\n", pathRadS4MSS1);
printf("Pathradiance Scene 4 Band 2: %12f\n", pathRadS4MSS2);
printf("Pathradiance Scene 4 Band 3: %12f\n", pathRadS4MSS3);
printf("Pathradiance Scene 4 Band 4: %12f\n\n", pathRadS4MSS4);

################################# Reflectance Calculation with athmo correction

	  for i = 1 to MSSlins
	  {
	     for j = 1 to MSScols
	     {          
               valueS4MSS1 = (pi * (RADS4MSS1[i,j] - pathRadS4MSS1)) / (radsurfMSS4b1 * cos(s4zenith));

                  if ( valueS4MSS1 < 0 )
       		{
          			valueS4MSS1 = 0;
				countS4REFnullMSS1 = countS4REFnullMSS1 + 1;
       		}

			else if ( valueS4MSS1 > 1 )
			{
				valueS4MSS1 = 1;
				countS4REFoneMSS1 = countS4REFoneMSS1 + 1;
			}
       		
			REFS4MSS1[i,j] = valueS4MSS1;

               valueS4MSS2 = (pi * (RADS4MSS2[i,j] - pathRadS4MSS2)) / (radsurfMSS4b2 * cos(s4zenith));

                  if ( valueS4MSS2 < 0 )
       		{
          			valueS4MSS2 = 0;
				countS4REFnullMSS2 = countS4REFnullMSS2 + 1;
       		}

			else if ( valueS4MSS2 > 1 )
			{
				valueS4MSS2 = 1;
				countS4REFoneMSS2 = countS4REFoneMSS2 + 1;
			}
       		
			REFS4MSS2[i,j] = valueS4MSS2;

               valueS4MSS3 = (pi * (RADS4MSS3[i,j] - pathRadS4MSS3)) / (radsurfMSS4b3 * cos(s4zenith));

                  if ( valueS4MSS3 < 0 )
       		{
          			valueS4MSS3 = 0;
				countS4REFnullMSS3 = countS4REFnullMSS3 + 1;
       		}

			else if ( valueS4MSS3 > 1 )
			{
				valueS4MSS3 = 1;
				countS4REFoneMSS3 = countS4REFoneMSS3 + 1;
			}
       		
			REFS4MSS3[i,j] = valueS4MSS3;

               valueS4MSS4 = (pi * (RADS4MSS4[i,j] - pathRadS4MSS4)) / (radsurfMSS4b4 * cos(s4zenith));

                  if ( valueS4MSS4 < 0 )
       		{
          			valueS4MSS4 = 0;
				countS4REFnullMSS4 = countS4REFnullMSS4 + 1;
       		}

			else if ( valueS4MSS4 > 1 )
			{
				valueS4MSS4 = 1;
				countS4REFoneMSS4 = countS4REFoneMSS4 + 1;
			}
       		
			REFS4MSS4[i,j] = valueS4MSS4;

           }
	  }

printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 1: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS1, countS4REFoneMSS1);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 2: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS2, countS4REFoneMSS2);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 3: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS3, countS4REFoneMSS3);
printf("Reflectance Computation (including DOS 1 Atmosphere Correction) Scene 4 Band 4: %5i pixels smaller zero set to zero and %5i pixels larger one set to one\n", countS4REFnullMSS4, countS4REFoneMSS4);

CreatePyramid(REFS4MSS1);
CreatePyramid(REFS4MSS2);
CreatePyramid(REFS4MSS3);
CreatePyramid(REFS4MSS4);
        
CreateHistogram(REFS4MSS1);
CreateHistogram(REFS4MSS2);
CreateHistogram(REFS4MSS3);
CreateHistogram(REFS4MSS4);

CloseRaster(RADS4MSS1);
CloseRaster(RADS4MSS2);
CloseRaster(RADS4MSS3);
CloseRaster(RADS4MSS4);

printf("Reflectance calculation for Scene 4 (MSS) is done...\n\n\n");
}

######################################
###### Begin processing.
###### Calculate Pseudo Invariant Feature.
######################################

printf( "Calculation of Reflectance finished, starting off with the Pseudo Invariant Features...\n\n" );
printf( "Computing SBI-Rasters and statistics...\n\n" );

#############################
#### SBI Scene 1 raster computation and statistics
#############################

if ( sensors1 == 7 )
{
	raster SBI1ETM;
	CreateTempRaster(SBI1ETM, ETMlins, ETMcols,"32-bit float");
	for i = 1 to ETMlins
	{
         for j = 1 to ETMcols
         {
            SBI1ETM[i,j] = (0.33183 * REFS1ETM1[i,j]) + (0.33121 * REFS1ETM2[i,j]) + (0.55177 * REFS1ETM3[i,j]) + (0.42514 * REFS1ETM4[i,j]) + (0.48087 * REFS1ETM5[i,j]) + (0.25252 * REFS1ETM7[i,j]);
         }
      }
      sbi1min = GlobalMin(SBI1ETM);
      sbi1max = GlobalMax(SBI1ETM);
      sbi1mean = GlobalMean(SBI1ETM);
      sbi1sd = GlobalSD(SBI1ETM);
}

else if ( sensors1 == 6 )
{
	raster SBI1TM;
    CreateTempRaster(SBI1TM, TMlins, TMcols,"32-bit float");
	for i = 1 to TMlins
    {
        for j = 1 to TMcols
        {
            SBI1TM[i,j] = (0.33183 * REFS1TM1[i,j]) + (0.33121 * REFS1TM2[i,j]) + (0.55177 * REFS1TM3[i,j]) + (0.42514 * REFS1TM4[i,j]) + (0.48087 * REFS1TM5[i,j]) + (0.25252 * REFS1TM7[i,j]);
        }
    }
    sbi1min = GlobalMin(SBI1TM);
    sbi1max = GlobalMax(SBI1TM);
    sbi1mean = GlobalMean(SBI1TM);
    sbi1sd = GlobalSD(SBI1TM);
}
else
{
	raster SBI1MSS;
	CreateTempRaster(SBI1MSS, MSSlins, MSScols,"32-bit float");
    for i = 1 to MSSlins
    {
        for j = 1 to MSScols
        {
            SBI1MSS[i,j] = (0.332 * REFS1MSS1[i,j]) + (0.603 * REFS1MSS2[i,j]) + (0.675 * REFS1MSS3[i,j]) + (0.262 * REFS1MSS4[i,j]);
        }
    }
    sbi1min = GlobalMin(SBI1MSS);
    sbi1max = GlobalMax(SBI1MSS);
    sbi1mean = GlobalMean(SBI1MSS);
    sbi1sd = GlobalSD(SBI1MSS);
}

#############################
#### SBI Scene 2 raster computation and statistics
#############################

if ( sensors2 == 7 )
{
	raster SBI2ETM;
	CreateTempRaster(SBI2ETM, ETMlins, ETMcols,"32-bit float");
	for i = 1 to ETMlins
	{
         for j = 1 to ETMcols
         {
            SBI2ETM[i,j] = (0.33183 * REFS2ETM1[i,j]) + (0.33121 * REFS2ETM2[i,j]) + (0.55177 * REFS2ETM3[i,j]) + (0.42514 * REFS2ETM4[i,j]) + (0.48087 * REFS2ETM5[i,j]) + (0.25252 * REFS2ETM7[i,j]);
         }
      }
      sbi2min = GlobalMin(SBI2ETM);
      sbi2max = GlobalMax(SBI2ETM);
      sbi2mean = GlobalMean(SBI2ETM);
      sbi2sd = GlobalSD(SBI2ETM);
}

else if ( sensors2 == 6 )
{
	raster SBI2TM;
    CreateTempRaster(SBI2TM, TMlins, TMcols,"32-bit float");
	for i = 1 to TMlins
    {
        for j = 1 to TMcols
        {
            SBI2TM[i,j] = (0.33183 * REFS2TM1[i,j]) + (0.33121 * REFS2TM2[i,j]) + (0.55177 * REFS2TM3[i,j]) + (0.42514 * REFS2TM4[i,j]) + (0.48087 * REFS2TM5[i,j]) + (0.25252 * REFS2TM7[i,j]);
        }
	}
    sbi2min = GlobalMin(SBI2TM);
    sbi2max = GlobalMax(SBI2TM);
    sbi2mean = GlobalMean(SBI2TM);
    sbi2sd = GlobalSD(SBI2TM);
}
else
{
	raster SBI2MSS;
	CreateTempRaster(SBI2MSS, MSSlins, MSScols,"32-bit float");
    for i = 1 to MSSlins
    {
	    for j = 1 to MSScols
        {
            SBI2MSS[i,j] = (0.332 * REFS2MSS1[i,j]) + (0.603 * REFS2MSS2[i,j]) + (0.675 * REFS2MSS3[i,j]) + (0.262 * REFS2MSS4[i,j]);
        }
    }
    sbi2min = GlobalMin(SBI2MSS);
    sbi2max = GlobalMax(SBI2MSS);
    sbi2mean = GlobalMean(SBI2MSS);
    sbi2sd = GlobalSD(SBI2MSS);
}

#############################
#### SBI Scene 3 raster computation and statistics
#############################

if ( sensors3 == 7 )
{
	raster SBI3ETM;
	CreateTempRaster(SBI3ETM, ETMlins, ETMcols,"32-bit float");
	for i = 1 to ETMlins
	{
         for j = 1 to ETMcols
         {
            SBI3ETM[i,j] = (0.33183 * REFS3ETM1[i,j]) + (0.33121 * REFS3ETM2[i,j]) + (0.55177 * REFS3ETM3[i,j]) + (0.42514 * REFS3ETM4[i,j]) + (0.48087 * REFS3ETM5[i,j]) + (0.25252 * REFS3ETM7[i,j]);
         }
      }
      sbi1min = GlobalMin(SBI3ETM);
      sbi1max = GlobalMax(SBI3ETM);
      sbi1mean = GlobalMean(SBI3ETM);
      sbi1sd = GlobalSD(SBI3ETM);
}

else if ( sensors3 == 6 )
{
	raster SBI3TM;
    CreateTempRaster(SBI3TM, TMlins, TMcols,"32-bit float");
	for i = 1 to TMlins
    {
	    for j = 1 to TMcols
	    {
            SBI3TM[i,j] = (0.33183 * REFS3TM1[i,j]) + (0.33121 * REFS3TM2[i,j]) + (0.55177 * REFS3TM3[i,j]) + (0.42514 * REFS3TM4[i,j]) + (0.48087 * REFS3TM5[i,j]) + (0.25252 * REFS3TM7[i,j]);
        }
	}
    sbi3min = GlobalMin(SBI3TM);
    sbi3max = GlobalMax(SBI3TM);
    sbi3mean = GlobalMean(SBI3TM);
    sbi3sd = GlobalSD(SBI3TM);
}
else
{
	raster SBI3MSS;
	CreateTempRaster(SBI3MSS, MSSlins, MSScols,"32-bit float");
    for i = 1 to MSSlins
	{
	    for j = 1 to MSScols
        {
            SBI3MSS[i,j] = (0.332 * REFS3MSS1[i,j]) + (0.603 * REFS3MSS2[i,j]) + (0.675 * REFS3MSS3[i,j]) + (0.262 * REFS3MSS4[i,j]);
        }
    }
    sbi3min = GlobalMin(SBI3MSS);
    sbi3max = GlobalMax(SBI3MSS);
    sbi3mean = GlobalMean(SBI3MSS);
    sbi3sd = GlobalSD(SBI3MSS);
}

#############################
#### SBI Scene 4 raster computation and statistics
#############################

if ( sensors4 == 7 )
{
	raster SBI4ETM;
	CreateTempRaster(SBI4ETM, ETMlins, ETMcols,"32-bit float");
	for i = 1 to ETMlins
	{
         for j = 1 to ETMcols
         {
            SBI4ETM[i,j] = (0.33183 * REFS4ETM1[i,j]) + (0.33121 * REFS4ETM2[i,j]) + (0.55177 * REFS4ETM3[i,j]) + (0.42514 * REFS4ETM4[i,j]) + (0.48087 * REFS4ETM5[i,j]) + (0.25252 * REFS4ETM7[i,j]);
         }
      }
      sbi1min = GlobalMin(SBI4ETM);
      sbi1max = GlobalMax(SBI4ETM);
      sbi1mean = GlobalMean(SBI4ETM);
      sbi1sd = GlobalSD(SBI4ETM);
}

else if ( sensors4 == 6 )
{
	raster SBI4TM;
    CreateTempRaster(SBI4TM, TMlins, TMcols,"32-bit float");
	for i = 1 to TMlins
    {
	    for j = 1 to TMcols
	    {
            SBI4TM[i,j] = (0.33183 * REFS4TM1[i,j]) + (0.33121 * REFS4TM2[i,j]) + (0.55177 * REFS4TM3[i,j]) + (0.42514 * REFS4TM4[i,j]) + (0.48087 * REFS4TM5[i,j]) + (0.25252 * REFS4TM7[i,j]);
		}
	}
    sbi4min = GlobalMin(SBI4TM);
    sbi4max = GlobalMax(SBI4TM);
    sbi4mean = GlobalMean(SBI4TM);
    sbi4sd = GlobalSD(SBI4TM);
}
else
{
	raster SBI4MSS;
	CreateTempRaster(SBI4MSS, MSSlins, MSScols,"32-bit float");
      for i = 1 to MSSlins
	{
	     for j = 1 to MSScols
	     {
	         SBI4MSS[i,j] = (0.332 * REFS4MSS1[i,j]) + (0.603 * REFS4MSS2[i,j]) + (0.675 * REFS4MSS3[i,j]) + (0.262 * REFS4MSS4[i,j]);
	     }
    }
    sbi4min = GlobalMin(SBI4MSS);
    sbi4max = GlobalMax(SBI4MSS);
    sbi4mean = GlobalMean(SBI4MSS);
    sbi4sd = GlobalSD(SBI4MSS);
}

printf( "Computing SBI-Rasters done...\n\n" );

#######################################################################################
#### Find SBI histogram spread and find out about the masterimage
#######################################################################################

sbi1spread = sbi1max - sbi1min;
printf("Sbi1spread: %12f Sbi1max: %12f Sbit1min: %12f\n", sbi1spread, sbi1max, sbi1min);
sbi2spread = sbi2max - sbi2min;
printf("Sbi2spread: %12f Sbi2max: %12f Sbit2min: %12f\n", sbi2spread, sbi2max, sbi2min);
sbi3spread = sbi3max - sbi3min;
printf("Sbi3spread: %12f Sbi3max: %12f Sbit3min: %12f\n", sbi3spread, sbi3max, sbi3min);
sbi4spread = sbi4max - sbi4min;
printf("Sbi4spread: %12f Sbi4max: %12f Sbit4min: %12f\n\n", sbi4spread, sbi4max, sbi4min);
printf("Starting of looking for the new master...\n\n");


if ( ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 ) && ( sensors2 == 5 or sensors2 == 4 or sensors2 == 3 or sensors2 == 2 or sensors2 == 1 ) && ( sensors3 == 5 or sensors3 == 4 or sensors3 == 3 or sensors3 == 2 or sensors3 == 1 ) && ( sensors4 == 5 or sensors4 == 4 or sensors4 == 3 or sensors4 == 2 or sensors4 == 1 ) )
{
    printf("All entered scenes are MSS sensors\n");
    if ( sbi1spread > sbi2spread && sbi1spread > sbi3spread && sbi1spread > sbi4spread )
    {
        slaveMaster = 1;
        printf("Scene 1 is the new master (MSS-Scenes only)\n\n");
    }
    
    else if ( sbi2spread > sbi1spread && sbi2spread > sbi3spread && sbi2spread > sbi4spread )
    {
        slaveMaster = 2;
        printf("Scene 2 is the new master (MSS-Scenes only)\n\n");
    }
    
    else if ( sbi3spread > sbi1spread && sbi3spread > sbi2spread && sbi3spread > sbi4spread )
    {
        slaveMaster = 3;
        printf("Scene 3 is the new master (MSS-Scenes only)\n\n");
    }
    
    else if ( sbi4spread > sbi1spread && sbi4spread > sbi2spread && sbi4spread > sbi3spread )
    {
        slaveMaster = 4;
        printf("Scene 4 is the new master (MSS-Scenes only)\n\n");
    }
}

else    # a mixed setup - MSS together with TM and/or ETM scenes  
{
    printf("A mixed setup - There are MSS together with TM and/or ETM scenes - TM and ETM scenes are allowed as master only...\n");
    if ( sbi1spread > sbi2spread && sbi1spread > sbi3spread && sbi1spread > sbi4spread )
    {
        if ( sensors1 == 7 or sensors1 == 6)
        {
            slaveMaster = 1;
            printf("Scene 1 is the new master...\n\n");
        }
        
        else
        {
            printf("Scene 1 has had the widest spread, but it is a MSS sensor... Looking out for the next smaller spread...\n");
            if ( sbi2spread > sbi3spread && sbi2spread > sbi4spread )
            {
                if ( sensors2 == 7 or sensors2 == 6)
                {
                    slaveMaster = 2;
                    printf("Scene 2 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 1 has had the widest spread, but it is a MSS sensor, same like Scene 2... Looking out for the next smaller spread...\n");
                    if ( sbi3spread > sbi4spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi3spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi3spread > sbi2spread && sbi3spread > sbi4spread )
            {
                if ( sensors3 == 7 or sensors3 == 6)
                {
                    slaveMaster = 3;
                    printf("Hail to Scene 3 our new master...\n\n");
                }
                
                else
                {
                    printf("Scene 1 has had the widest spread, but it is a MSS sensor, same like Scene 3... Looking out for the next smaller spread...\n");
                    if ( sbi2spread > sbi4spread )
                    {
                        if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi2spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi4spread > sbi2spread && sbi4spread > sbi3spread )
            {
                if ( sensors4 == 7 or sensors4 == 6)
                {
                    slaveMaster = 4;
                    printf("Scene 4 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 1 has had the widest spread, but it is a MSS sensor, same like Scene 4... Looking out for the next smaller spread...\n");
                    if ( sbi2spread > sbi3spread )
                    {
                        if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi3spread > sbi2spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                }
            }
        }
    }
    
    else if ( sbi2spread > sbi1spread && sbi2spread > sbi3spread && sbi2spread > sbi4spread )
    {
        if ( sensors2 == 7 or sensors2 == 6)
        {
            slaveMaster = 2;
            printf("Scene 2 is the new master...\n\n");
        }
        
        else
        {
            printf("Scene 2 has had the widest spread, but it is a MSS sensor... Looking out for the next smaller spread...\n");
            if ( sbi1spread > sbi3spread && sbi1spread > sbi4spread )
            {
                if ( sensors1 == 7 or sensors1 == 6)
                {
                    slaveMaster = 1;
                    printf("Scene 1 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 2 has had the widest spread, but it is a MSS sensor, same like Scene 1... Looking out for the next smaller spread...\n");
                    if ( sbi3spread > sbi4spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi3spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi3spread > sbi1spread && sbi3spread > sbi4spread )
            {
                if ( sensors3 == 7 or sensors3 == 6)
                {
                    slaveMaster = 3;
                    printf("Scene 3 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 2 has had the widest spread, but it is a MSS sensor, same like Scene 3... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi4spread )
                    {
                        if ( sensors1 == 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi1spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi4spread > sbi1spread && sbi4spread > sbi3spread )
            {
                if ( sensors4 == 7 or sensors4 == 6)
                {
                    slaveMaster = 4;
                    printf("Scene 4 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 2 has had the widest spread, but it is a MSS sensor, same like Scene 4... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi3spread )
                    {
                        if ( sensors1== 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi3spread > sbi1spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
        }
    }
    
    else if ( sbi3spread > sbi1spread && sbi3spread && sbi2spread && sbi3spread > sbi4spread )
    {
        if ( sensors3 == 7 or sensors3 == 6)
        {
            slaveMaster = 3;
            printf("Scene 3 is the new master...\n\n");
        }
        
        else
        {
            printf("Scene 3 has had the widest spread, but it is a MSS sensor... Looking out for the next smaller spread...\n");
            if ( sbi1spread > sbi2spread && sbi1spread > sbi4spread )
            {
                if ( sensors1 == 7 or sensors1 == 6)
                {
                    slaveMaster = 1;
                    printf("Scene 1 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 3 has had the widest spread, but it is a MSS sensor, same like Scene 1... Looking out for the next smaller spread...\n");
                    if ( sbi2spread > sbi4spread )
                    {
                        if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi2spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi2spread > sbi1spread && sbi2spread > sbi4spread )
            {
                if ( sensors2 == 7 or sensors2 == 6)
                {
                    slaveMaster = 2;
                    printf("Scene 2 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 3 has had the widest spread, but it is a MSS sensor, same like Scene 2... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi4spread )
                    {
                        if ( sensors1 == 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi4spread > sbi1spread )
                    {
                        if ( sensors4 == 7 or sensors4 == 6)
                        {
                            slaveMaster = 4;
                            printf("Scene 4 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi4spread > sbi1spread && sbi4spread > sbi2spread )
            {
                if ( sensors4 == 7 or sensors4 == 6)
                {
                    slaveMaster = 4;
                    printf("Scene 4 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 3 has had the widest spread, but it is a MSS sensor, same like Scene 4... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi2spread )
                    {
                        if ( sensors1 == 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi2spread > sbi1spread )
                    {
      if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
        }
    }
    
    else if ( sbi4spread > sbi1spread && sbi4spread > sbi2spread && sbi4spread > sbi3spread )
    {
        if ( sensors4 == 7 or sensors4 == 6)
        {
            slaveMaster = 4;
            printf("Scene 4 is the new master...\n\n");
        }
        
        else
        {
            printf("Scene 4 has had the widest spread, but it is a MSS sensor... Looking out for the next smaller spread...\n");
            if ( sbi1spread > sbi2spread && sbi1spread > sbi3spread )
            {
                if ( sensors1 == 7 or sensors1 == 6)
                {
                    slaveMaster = 1;
                    printf("Scene 1 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 4 has had the widest spread, but it is a MSS sensor, same like Scene 1... Looking out for the next smaller spread...\n");
                    if ( sbi2spread > sbi3spread )
                    {
                        if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi3spread > sbi2spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi2spread > sbi1spread && sbi2spread > sbi3spread )
            {
                if ( sensors2 == 7 or sensors2 == 6)
                {
                    slaveMaster = 2;
                    printf("Scene 2 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 4 has had the widest spread, but it is a MSS sensor, same like Scene 2... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi3spread )
                    {
                        if ( sensors1 == 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi3spread > sbi1spread )
                    {
                        if ( sensors3 == 7 or sensors3 == 6)
                        {
                            slaveMaster = 3;
                            printf("Scene 3 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
            
            else if ( sbi3spread > sbi1spread && sbi3spread > sbi2spread )
            {
                if ( sensors3 == 7 or sensors3 == 6)
                {
                    slaveMaster = 3;
                    printf("Scene 3 is the new master...\n\n");
                }
                
                else
                {
                    printf("Scene 1 has had the widest spread, but it is a MSS sensor, same like Scene 4... Looking out for the next smaller spread...\n");
                    if ( sbi1spread > sbi2spread )
                    {
                        if ( sensors1 == 7 or sensors1 == 6)
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                    }
                    
                    else if ( sbi2spread > sbi1spread )
                    {
                        if ( sensors2 == 7 or sensors2 == 6)
                        {
                            slaveMaster = 2;
                            printf("Scene 2 is the new master...\n\n");
                        }
                        
                        else
                        {
                            slaveMaster = 1;
                            printf("Scene 1 is the new master...\n\n");
                        }
                    }
                }
            }
        }
    }
}

#######################################################################################
#### Find PIFS
#######################################################################################

######################################## Scene 1 ######################################

if ( sensors1 == 7 )
{                     
numeric ETM1realSize, ETM1realSizeROUNDED, ETM1tailDARK, ETM1tailBRIGHT, darkTAILfinalETM1, brightTAILfinalETM1, ETM1tailPosition;

numeric ETM1nullValCount = 0;                        # Null Value count for ETM1
numeric ETM1realSizeNULL = 0;                        # pixels with value 0
numeric ETM1realArrayCount = 1;

numeric countETM1dark = 0;                           # Dark Pif pixel count
numeric countETM1bright = 0;                         # Bright Pif pixel count
numeric countS1PIF = 0;                              # overall Pif count
 
for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {
		if (IsNull(SBI1ETM[i,j]) == 1)
		{
		ETM1nullValCount = ETM1nullValCount + 1;
		}		 
    }
}

ETM1realSize = (ETMlins * ETMcols) - ETM1nullValCount;

array allValuesSbiETM1[ETM1realSize];

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(SBI1ETM[i,j]) == 0)
		{
		  allValuesSbiETM1[ETM1realArrayCount] = SBI1ETM[i,j];		 
		  ETM1realArrayCount = ETM1realArrayCount + 1;
	
		  if ( SBI1ETM[i,j] == 0 )
		  {
		      ETM1realSizeNULL = ETM1realSizeNULL + 1;
		  }
		}
    }
}
	numeric lastETM1 = ETM1realSize;
	numeric hETM1 = 1;

	while ( (hETM1 * 3 + 1) < lastETM1-1 )
	{
		hETM1 = 3 * hETM1 + 1;
	}
	
	while ( hETM1 > 0 )
	{
		for i = hETM1-1 to lastETM1               # for each of the h sets of elements
		{
			numeric keyETM1 = allValuesSbiETM1[i];
			numeric jETM1 = i;
		
			while (jETM1 >= hETM1 && allValuesSbiETM1[jETM1 - hETM1] > keyETM1)
			{
				allValuesSbiETM1[jETM1] = allValuesSbiETM1[jETM1 - hETM1];
				jETM1 = jETM1 - hETM1;
			}
			allValuesSbiETM1[jETM1] = keyETM1;
		}
		hETM1 = floor(hETM1/3);
	}

ETM1tailPosition = (ETM1realSize - ETM1realSizeNULL) * (histoTailInputVAL / 100);
ETM1realSizeROUNDED = round(ETM1tailPosition);            					         # ETM1tailNUM rounded to gain a integer value
ETM1tailDARK =  ETM1realSizeNULL + ETM1realSizeROUNDED;
ETM1tailBRIGHT = ETM1realSize - ETM1realSizeROUNDED;

darkTAILfinalETM1 = allValuesSbiETM1[ETM1tailDARK];
brightTAILfinalETM1 = allValuesSbiETM1[ETM1tailBRIGHT];			                      # Final Threshold value, not position

  for i = 1 to ETMlins									                             # Generate dark PIF mask for Scene 1			
  {
	for j = 1 to ETMcols
	{
	if (SBI1ETM[i,j] > 0 && SBI1ETM[i,j] <= darkTAILfinalETM1)
	{
		S1darkMASK[i,j] = 1;
		countETM1dark = countETM1dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S1darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to ETMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to ETMcols
	{
	if (SBI1ETM[i,j] >= brightTAILfinalETM1)
	{
		S1brightMASK[i,j] = 1;
		countETM1bright = countETM1bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S1brightMASK[i,j] = 0;
	}
	}
  }

 if ( countETM1dark >= pifDarkVAL && countETM1bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
	if ( (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 1) or (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 0) or (S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 1) )
	{
	S1MASK[i,j] = 1; 
	countS1PIF = countS1PIF + 1;
	} 
	else if ( S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 0 ) 
	{
	S1MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countETM1dark < pifDarkVAL && countETM1bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n",pifDarkVAL, countETM1dark);
    Exit();
}

else if ( countETM1bright < pifBrightVAL && countETM1dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n",pifBrightVAL, countETM1bright);
	Exit();
}
else if ( countETM1dark < pifDarkVAL && countETM1bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countETM1dark, pifBrightVAL, countETM1bright);
    Exit();
}

printf("The SBI raster (Scene 1 - ETM) consists of %12i pixels\n", ETM1realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", ETM1nullValCount, ETM1realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM1tailDARK, darkTAILfinalETM1);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM1tailBRIGHT, brightTAILfinalETM1);
printf("Scene 1 has %12i Pseudo Invariant Features\n\n", countS1PIF);
}

else if ( sensors1 == 6 )
{
numeric TM1realSize, TM1realSizeROUNDED, TM1tailDARK, TM1tailBRIGHT, darkTAILfinalTM1, brightTAILfinalTM1, TM1tailPosition;

numeric TM1nullValCount = 0;                         # Null Value count for TM1
numeric TM1realSizeNULL = 0;                         # pixels with value 0
numeric TM1realArrayCount = 1;

numeric countTM1dark = 0;                           # Dark Pif pixel count
numeric countTM1bright = 0;                         # Bright Pif pixel count
numeric countS1PIF = 0;                             # overall Pif count
 
for i = 1 to TMlins
{
    for j = 1 to TMcols
    {
        
		if (IsNull(SBI1TM[i,j]) == 1)
		{
		TM1nullValCount = TM1nullValCount + 1;
		}		 
    }
}

TM1realSize = (TMlins * TMcols) - TM1nullValCount;

array allValuesSbiTM1[TM1realSize];

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(SBI1TM[i,j]) == 0)
		{
		  allValuesSbiTM1[TM1realArrayCount] = SBI1TM[i,j];		 
		  TM1realArrayCount = TM1realArrayCount + 1;
	
		  if ( SBI1TM[i,j] == 0 )
		  {
		      TM1realSizeNULL = TM1realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastTM1 = TM1realSize;
	numeric hTM1 = 1;

	while ( (hTM1 * 3 + 1) < lastTM1-1 )
	{
		hTM1 = 3 * hTM1 + 1;
	}
	
	while ( hTM1 > 0 )
	{
		for i = hTM1-1 to lastTM1               # for each of the h sets of elements
		{
			numeric keyTM1 = allValuesSbiTM1[i];
			numeric jTM1 = i;
		
			while (jTM1 >= hTM1 && allValuesSbiTM1[jTM1 - hTM1] > keyTM1)
			{
				allValuesSbiTM1[jTM1] = allValuesSbiTM1[jTM1 - hTM1];
				jTM1 = jTM1 - hTM1;
			}
			allValuesSbiTM1[jTM1] = keyTM1;
		}
		hTM1 = floor(hTM1/3);
	}

TM1tailPosition = (TM1realSize - TM1realSizeNULL)  * (histoTailInputVAL / 100);
TM1realSizeROUNDED = round(TM1tailPosition);            					               # TM1tailNUM rounded to gain a integer value
TM1tailDARK =  TM1realSizeNULL + TM1realSizeROUNDED;
TM1tailBRIGHT = TM1realSize - TM1realSizeROUNDED;

darkTAILfinalTM1 = allValuesSbiTM1[TM1tailDARK];
brightTAILfinalTM1 = allValuesSbiTM1[TM1tailBRIGHT];			                             # Final Threshold value, not position

  for i = 1 to TMlins									                                  # Generate dark PIF mask for Scene 1			
  {
	for j = 1 to TMcols
	{
	if (SBI1TM[i,j] > 0 && SBI1TM[i,j] <= darkTAILfinalTM1)
	{
		S1darkMASK[i,j] = 1;
		countTM1dark = countTM1dark + 1;					                                 # Dark PIF Pixel count
	}
	else
	{
		S1darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to TMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to TMcols
	{
	if (SBI1TM[i,j] >= brightTAILfinalTM1)
	{
		S1brightMASK[i,j] = 1;
		countTM1bright = countTM1bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S1brightMASK[i,j] = 0;
	}
	}
  }

 if ( countTM1dark >= pifDarkVAL && countTM1bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to TMlins
  {
	for j = 1 to TMcols
	{
	if ( (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 1) or (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 0) or (S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 1) )
	{
	S1MASK[i,j] = 1; 
	countS1PIF = countS1PIF + 1;
	} 
	else if ( S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 0 ) 
	{
	S1MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countTM1dark < pifDarkVAL && countTM1bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n",pifDarkVAL, countTM1dark);
    Exit();
}

else if ( countTM1bright < pifBrightVAL && countTM1dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n",pifBrightVAL, countTM1bright);
	Exit();
}
else if ( countTM1dark < pifDarkVAL && countTM1bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countTM1dark, pifBrightVAL, countTM1bright);
    Exit();
}

printf("The SBI raster (Scene 1 - TM) consists of %12i pixels\n", TM1realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", TM1nullValCount, TM1realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM1tailDARK, darkTAILfinalTM1);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM1tailBRIGHT, brightTAILfinalTM1);
printf("Scene 1 has %12i Pseudo Invariant Features\n\n", countS1PIF);
}

else
{
numeric MSS1realSize, MSS1realSizeROUNDED, MSS1tailDARK, MSS1tailBRIGHT, darkTAILfinalMSS1, brightTAILfinalMSS1, MSS1tailPosition;

numeric MSS1nullValCount = 0;                           # Null Value count for MSS1
numeric MSS1realSizeNULL = 0;                        # pixels with value 0
numeric MSS1realArrayCount = 1;

numeric countMSS1dark = 0;                           # Dark Pif pixel count
numeric countMSS1bright = 0;                         # Bright Pif pixel count
numeric countS1PIF = 0;                              # overall Pif count
 
for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {
        
		if (IsNull(SBI1MSS[i,j]) == 1)
		{
		MSS1nullValCount = MSS1nullValCount + 1;
		}		 
    }
}

MSS1realSize = (MSSlins * MSScols) - MSS1nullValCount;

array allValuesSbiMSS1[MSS1realSize];

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(SBI1MSS[i,j]) == 0)
		{
		  allValuesSbiMSS1[MSS1realArrayCount] = SBI1MSS[i,j];		 
		  MSS1realArrayCount = MSS1realArrayCount + 1;
	
		  if ( SBI1MSS[i,j] == 0 )
		  {
		      MSS1realSizeNULL = MSS1realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastMSS1 = MSS1realSize;
	numeric hMSS1 = 1;

	while ( (hMSS1 * 3 + 1) < lastMSS1 - 1 )
	{
		hMSS1 = 3 * hMSS1 + 1;
	}
	
	while ( hMSS1 > 0 )
	{
		for i = hMSS1 - 1 to lastMSS1               # for each of the h sets of elements
		{
			numeric keyMSS1 = allValuesSbiMSS1[i];
			numeric jMSS1 = i;
		
			while (jMSS1 >= hMSS1 && allValuesSbiMSS1[jMSS1 - hMSS1] > keyMSS1)
			{
				allValuesSbiMSS1[jMSS1] = allValuesSbiMSS1[jMSS1 - hMSS1];
				jMSS1 = jMSS1 - hMSS1;
			}
			allValuesSbiMSS1[jMSS1] = keyMSS1;
		}
		hMSS1 = floor(hMSS1/3);
	}

MSS1tailPosition = (MSS1realSize - MSS1realSizeNULL) * (histoTailInputVAL / 100);
MSS1realSizeROUNDED = round(MSS1tailPosition);            					             # MSS1tailNUM rounded to gain a integer value
MSS1tailDARK =  MSS1realSizeNULL + MSS1realSizeROUNDED;
MSS1tailBRIGHT = MSS1realSize - MSS1realSizeROUNDED;

darkTAILfinalMSS1 = allValuesSbiMSS1[MSS1tailDARK];
brightTAILfinalMSS1 = allValuesSbiMSS1[MSS1tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to MSSlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to MSScols
	{
	if (SBI1MSS[i,j] > 0 && SBI1MSS[i,j] <= darkTAILfinalMSS1)
	{
		S1darkMASK[i,j] = 1;
		countMSS1dark = countMSS1dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S1darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to MSSlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to MSScols
	{
	if (SBI1MSS[i,j] >= brightTAILfinalMSS1)
	{
		S1brightMASK[i,j] = 1;
		countMSS1bright = countMSS1bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S1brightMASK[i,j] = 0;
	}
	}
  }

 if ( countMSS1dark >= pifDarkVAL && countMSS1bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to MSSlins
  {
	for j = 1 to MSScols
	{
	if ( (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 1) or (S1darkMASK[i,j] == 1 && S1brightMASK[i,j] == 0) or (S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 1) )
	{
	S1MASK[i,j] = 1; 
	countS1PIF = countS1PIF + 1;
	} 
	else if ( S1darkMASK[i,j] == 0 && S1brightMASK[i,j] == 0 ) 
	{
	S1MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countMSS1dark < pifDarkVAL && countMSS1bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n", pifDarkVAL, countMSS1dark);
    Exit();
}

else if ( countMSS1bright < pifBrightVAL && countMSS1dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 1 but only %3i were found. Quitting\n\n", pifBrightVAL, countMSS1bright);
	Exit();
}
else if ( countMSS1dark < pifDarkVAL && countMSS1bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countMSS1dark, pifBrightVAL, countMSS1bright);
    Exit();
}

printf("The SBI raster (Scene 1 - MSS) consists of %12i pixels\n", MSS1realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", MSS1nullValCount, MSS1realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS1tailDARK, darkTAILfinalMSS1);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS1tailBRIGHT, brightTAILfinalMSS1);
printf("Scene 1 has %12i Pseudo Invariant Features\n\n", countS1PIF);
}

######################################## Scene 2 ########################################

if ( sensors2 == 7 )
{
numeric ETM2realSize, ETM2realSizeROUNDED, ETM2tailDARK, ETM2tailBRIGHT, darkTAILfinalETM2, brightTAILfinalETM2, ETM2tailPosition;

numeric ETM2nullValCount = 0;                           # Null Value count for ETM1
numeric ETM2realSizeNULL = 0;                        # pixels with value 0
numeric ETM2realArrayCount = 1;

numeric countETM2dark = 0;                           # Dark Pif pixel count
numeric countETM2bright = 0;                         # Bright Pif pixel count
numeric countS2PIF = 0;                              # overall Pif count
 
for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {
        
		if (IsNull(SBI2ETM[i,j]) == 1)
		{
		ETM2nullValCount = ETM2nullValCount + 1;
		}		 
    }
}

ETM2realSize = (ETMlins * ETMcols) - ETM2nullValCount;

array allValuesSbiETM2[ETM2realSize];

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(SBI2ETM[i,j]) == 0)
		{
		  allValuesSbiETM2[ETM2realArrayCount] = SBI2ETM[i,j];		 
		  ETM2realArrayCount = ETM2realArrayCount + 1;
	
		  if ( SBI2ETM[i,j] == 0 )
		  {
		      ETM2realSizeNULL = ETM2realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastETM2 = ETM2realSize;
	numeric hETM2 = 1;

	while ( (hETM2 * 3 + 1) < lastETM2 - 1 )
	{
		hETM2 = 3 * hETM2 + 1;
	}
	
	while ( hETM2 > 0 )
	{
		for i = hETM2 - 1 to lastETM2               # for each of the h sets of elements
		{
			numeric keyETM2 = allValuesSbiETM2[i];
			numeric jETM2 = i;
		
			while (jETM2 >= hETM2 && allValuesSbiETM2[jETM2 - hETM2] > keyETM2)
			{
				allValuesSbiETM2[jETM2] = allValuesSbiETM2[jETM2 - hETM2];
				jETM2 = jETM2 - hETM2;
			}
			allValuesSbiETM2[jETM2] = keyETM2;
		}
		hETM2 = floor(hETM2/3);
	}

ETM2tailPosition = (ETM2realSize - ETM2realSizeNULL) * (histoTailInputVAL / 100);
ETM2realSizeROUNDED = round(ETM2tailPosition);            				          	# ETM1tailNUM rounded to gain a integer value
ETM2tailDARK =  ETM2realSizeNULL + ETM2realSizeROUNDED;
ETM2tailBRIGHT = ETM2realSize - ETM2realSizeROUNDED;

darkTAILfinalETM2 = allValuesSbiETM2[ETM2tailDARK];
brightTAILfinalETM2 = allValuesSbiETM2[ETM2tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to ETMlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to ETMcols
	{
	if ( SBI2ETM[i,j] > 0 && SBI2ETM[i,j] <= darkTAILfinalETM2 )
	{
		S2darkMASK[i,j] = 1;
		countETM2dark = countETM2dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S2darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to ETMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to ETMcols
	{
	if ( SBI2ETM[i,j] >= brightTAILfinalETM2 )
	{
		S2brightMASK[i,j] = 1;
		countETM2bright = countETM2bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S2brightMASK[i,j] = 0;
	}
	}
  }

 if ( countETM2dark >= pifDarkVAL && countETM2bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
	if ( (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 1) or (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 0) or (S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 1) )
	{
	S2MASK[i,j] = 1; 
	countS2PIF = countS2PIF + 1;
	} 
	else if ( S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 0 ) 
	{
	S2MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countETM2dark < pifDarkVAL && countETM2bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n",pifDarkVAL, countETM2dark);
    Exit();
}

else if ( countETM2bright < pifBrightVAL && countETM2dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n",pifBrightVAL, countETM2bright);
	Exit();
}
else if ( countETM2dark < pifDarkVAL && countETM2bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countETM2dark, pifBrightVAL, countETM2bright);
    Exit();
}

printf("The SBI raster (Scene 2 - ETM) consists of %12i pixels\n", ETM2realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", ETM2nullValCount, ETM2realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM2tailDARK, darkTAILfinalETM2);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM2tailBRIGHT, brightTAILfinalETM2);
printf("Scene 2 has %12i Pseudo Invariant Features\n\n", countS2PIF);
}

else if ( sensors2 == 6 )
{
numeric TM2realSize, TM2realSizeROUNDED, TM2tailDARK, TM2tailBRIGHT, darkTAILfinalTM2, brightTAILfinalTM2, TM2tailPosition;

numeric TM2nullValCount = 0;                         # Null Value count for ETM1
numeric TM2realSizeNULL = 0;                         # pixels with value 0
numeric TM2realArrayCount = 1;

numeric countTM2dark = 0;                           # Dark Pif pixel count
numeric countTM2bright = 0;                         # Bright Pif pixel count
numeric countS2PIF = 0;                              # overall Pif count
 
for i = 1 to TMlins
{
    for j = 1 to TMcols
    {
        
		if (IsNull(SBI2TM[i,j]) == 1)
		{
		TM2nullValCount = TM2nullValCount + 1;
		}		 
    }
}

TM2realSize = (TMlins * TMcols) - TM2nullValCount;

array allValuesSbiTM2[TM2realSize];

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(SBI2TM[i,j]) == 0)
		{
		  allValuesSbiTM2[TM2realArrayCount] = SBI2TM[i,j];		 
		  TM2realArrayCount = TM2realArrayCount + 1;
	
		  if ( SBI2TM[i,j] == 0 )
		  {
		      TM2realSizeNULL = TM2realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastTM2 = TM2realSize;
	numeric hTM2 = 1;

	while ( (hTM2 * 3 + 1) < lastTM2 - 1 )
	{
		hTM2 = 3 * hTM2 + 1;
	}
	
	while ( hTM2 > 0 )
	{
		for i = hTM2 - 1 to lastTM2               # for each of the h sets of elements
		{
			numeric keyTM2 = allValuesSbiTM2[i];
			numeric jTM2 = i;
		
			while ( jTM2 >= hTM2 && allValuesSbiTM2[jTM2 - hTM2] > keyTM2 )
			{
				allValuesSbiTM2[jTM2] = allValuesSbiTM2[jTM2 - hTM2];
				jTM2 = jTM2 - hTM2;
			}
			allValuesSbiTM2[jTM2] = keyTM2;
		}
		hTM2 = floor(hTM2/3);
	}

TM2tailPosition = (TM2realSize - TM2realSizeNULL) * (histoTailInputVAL / 100);
TM2realSizeROUNDED = round(TM2tailPosition);            					       # ETM1tailNUM rounded to gain a integer value
TM2tailDARK =  TM2realSizeNULL + TM2realSizeROUNDED;
TM2tailBRIGHT = TM2realSize - TM2realSizeROUNDED;

darkTAILfinalTM2 = allValuesSbiTM2[TM2tailDARK];
brightTAILfinalTM2 = allValuesSbiTM2[TM2tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to TMlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to TMcols
	{
	if ( SBI2TM[i,j] > 0 && SBI2TM[i,j] <= darkTAILfinalTM2 )
	{
		S2darkMASK[i,j] = 1;
		countTM2dark = countTM2dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S2darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to TMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to TMcols
	{
	if ( SBI2TM[i,j] >= brightTAILfinalTM2 )
	{
		S2brightMASK[i,j] = 1;
		countTM2bright = countTM2bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S2brightMASK[i,j] = 0;
	}
	}
  }

 if ( countTM2dark >= pifDarkVAL && countTM2bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to TMlins
  {
	for j = 1 to TMcols
	{
	if ( (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 1) or (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 0) or (S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 1) )
	{
	S2MASK[i,j] = 1; 
	countS2PIF = countS2PIF + 1;
	} 
	else if ( S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 0 ) 
	{
	S2MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countTM2dark < pifDarkVAL && countTM2bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n",pifDarkVAL, countTM2dark);
    Exit();
}

else if ( countTM2bright < pifBrightVAL && countTM2dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n",pifBrightVAL, countTM2bright);
	Exit();
}
else if ( countTM2dark < pifDarkVAL && countTM2bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countTM2dark, pifBrightVAL, countTM2bright);
    Exit();
}

printf("The SBI raster (Scene 2 - TM) consists of %12i pixels\n", TM2realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", TM2nullValCount, TM2realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM2tailDARK, darkTAILfinalTM2);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM2tailBRIGHT, brightTAILfinalTM2);
printf("Scene 2 has %12i Pseudo Invariant Features\n\n", countS2PIF);
}

else
{
numeric MSS2realSize, MSS2realSizeROUNDED, MSS2tailDARK, MSS2tailBRIGHT, darkTAILfinalMSS2, brightTAILfinalMSS2, MSS2tailPosition;

numeric MSS2nullValCount = 0;                        # Null Value count for MSS2
numeric MSS2realSizeNULL = 0;                        # pixels with value 0
numeric MSS2realArrayCount = 1;

numeric countMSS2dark = 0;                           # Dark Pif pixel count
numeric countMSS2bright = 0;                         # Bright Pif pixel count
numeric countS2PIF = 0;                              # overall Pif count
 
for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {
        
		if (IsNull(SBI2MSS[i,j]) == 1)
		{
		MSS2nullValCount = MSS2nullValCount + 1;
		}		 
    }
}

MSS2realSize = (MSSlins * MSScols) - MSS2nullValCount;

array allValuesSbiMSS2[MSS2realSize];

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(SBI2MSS[i,j]) == 0)
		{
		  allValuesSbiMSS2[MSS2realArrayCount] = SBI2MSS[i,j];		 
		  MSS2realArrayCount = MSS2realArrayCount + 1;
	
		  if ( SBI2MSS[i,j] == 0 )
		  {
		      MSS2realSizeNULL = MSS2realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastMSS2 = MSS2realSize;
	numeric hMSS2 = 1;

	while ( (hMSS2 * 3 + 1) < lastMSS2 - 1 )
	{
		hMSS2 = 3 * hMSS2 + 1;
	}
	
	while ( hMSS2 > 0 )
	{
		for i = hMSS2 - 1 to lastMSS2               # for each of the h sets of elements
		{
			numeric keyMSS2 = allValuesSbiMSS2[i];
			numeric jMSS2 = i;
		
			while (jMSS2 >= hMSS2 && allValuesSbiMSS2[jMSS2 - hMSS2] > keyMSS2)
			{
				allValuesSbiMSS2[jMSS2] = allValuesSbiMSS2[jMSS2 - hMSS2];
				jMSS2 = jMSS2 - hMSS2;
			}
			allValuesSbiMSS2[jMSS2] = keyMSS2;
		}
		hMSS2 = floor(hMSS2/3);
	}

MSS2tailPosition = (MSS2realSize - MSS2realSizeNULL) * (histoTailInputVAL / 100);
MSS2realSizeROUNDED = round(MSS2tailPosition);            					                 # MSS2realSizeROUNDED rounded to gain a integer value
MSS2tailDARK =  MSS2realSizeNULL + MSS2realSizeROUNDED;
MSS2tailBRIGHT = MSS2realSize - MSS2realSizeROUNDED;

darkTAILfinalMSS2 = allValuesSbiMSS2[MSS2tailDARK];
brightTAILfinalMSS2 = allValuesSbiMSS2[MSS2tailBRIGHT];			              # Final Threshold value, not position

  for i = 1 to MSSlins									                      # Generate dark PIF mask for Scene 2			
  {
	for j = 1 to MSScols
	{
	if (SBI2MSS[i,j] > 0 && SBI2MSS[i,j] <= darkTAILfinalMSS2)
	{
		S2darkMASK[i,j] = 1;
		countMSS2dark = countMSS2dark + 1;					                   # Dark PIF Pixel count
	}
	else
	{
		S2darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to MSSlins									                     #Generate bright PIF mask for Scene 2
  {
	for j = 1 to MSScols
	{
	if (SBI2MSS[i,j] >= brightTAILfinalMSS2)
	{
		S2brightMASK[i,j] = 1;
		countMSS2bright = countMSS2bright + 1;				                # Bright PIF Pixel count
	}
	else
	{
		S2brightMASK[i,j] = 0;
	}
	}
  }

 if ( countMSS2dark >= pifDarkVAL && countMSS2bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to MSSlins
  {
	for j = 1 to MSScols
	{
	if ( (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 1) or (S2darkMASK[i,j] == 1 && S2brightMASK[i,j] == 0) or (S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 1) )
	{
	S2MASK[i,j] = 1; 
	countS2PIF = countS2PIF + 1;
	} 
	else if ( S2darkMASK[i,j] == 0 && S2brightMASK[i,j] == 0 ) 
	{
	S2MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countMSS2dark < pifDarkVAL && countMSS2bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n", pifDarkVAL, countMSS2dark);
    Exit();
}

else if ( countMSS2bright < pifBrightVAL && countMSS2dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 2 but only %3i were found. Quitting\n\n", pifBrightVAL, countMSS2bright);
	Exit();
}
else if ( countMSS2dark < pifDarkVAL && countMSS2bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countMSS2dark, pifBrightVAL, countMSS2bright);
    Exit();
}

printf("The SBI raster (Scene 2 - MSS) consists of %12i pixels\n", MSS2realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", MSS2nullValCount, MSS2realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS2tailDARK, darkTAILfinalMSS2);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS2tailBRIGHT, brightTAILfinalMSS2);
printf("Scene 2 has %12i Pseudo Invariant Features\n\n", countS2PIF);
}

######################################## Scene 3 ########################################

if ( sensors3 == 7 )
{
numeric ETM3realSize, ETM3realSizeROUNDED, ETM3tailDARK, ETM3tailBRIGHT, darkTAILfinalETM3, brightTAILfinalETM3, ETM3tailPosition;

numeric ETM3nullValCount = 0;                           # Null Value count for ETM3
numeric ETM3realSizeNULL = 0;                        # pixels with value 0
numeric ETM3realArrayCount = 1;

numeric countETM3dark = 0;                           # Dark Pif pixel count
numeric countETM3bright = 0;                         # Bright Pif pixel count
numeric countS3PIF = 0;                              # overall Pif count
 
for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {
        
		if (IsNull(SBI3ETM[i,j]) == 1)
		{
		ETM3nullValCount = ETM3nullValCount + 1;
		}		 
    }
}

ETM3realSize = (ETMlins * ETMcols) - ETM3nullValCount;

array allValuesSbiETM3[ETM3realSize];

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(SBI3ETM[i,j]) == 0)
		{
		  allValuesSbiETM3[ETM3realArrayCount] = SBI3ETM[i,j];		 
		  ETM3realArrayCount = ETM3realArrayCount + 1;
	
		  if ( SBI3ETM[i,j] == 0 )
		  {
		      ETM3realSizeNULL = ETM3realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastETM3 = ETM3realSize;
	numeric hETM3 = 1;

	while ( (hETM3 * 3 + 1) < lastETM3 - 1 )
	{
		hETM3 = 3 * hETM3 + 1;
	}
	
	while ( hETM3 > 0 )
	{
		for i = hETM3 - 1 to lastETM3               # for each of the h sets of elements
		{
			numeric keyETM3 = allValuesSbiETM3[i];
			numeric jETM3 = i;
		
			while (jETM3 >= hETM3 && allValuesSbiETM3[jETM3 - hETM3] > keyETM3)
			{
				allValuesSbiETM3[jETM3] = allValuesSbiETM3[jETM3 - hETM3];
				jETM3 = jETM3 - hETM3;
			}
			allValuesSbiETM3[jETM3] = keyETM3;
		}
		hETM3 = floor(hETM3/3);
	}

ETM3tailPosition = (ETM3realSize - ETM3realSizeNULL) * (histoTailInputVAL / 100);
ETM3realSizeROUNDED = round(ETM3tailPosition);            					             # ETM3realSizeROUNDED rounded to gain a integer value
ETM3tailDARK =  ETM3realSizeNULL + ETM3realSizeROUNDED;
ETM3tailBRIGHT = ETM3realSize - ETM3realSizeROUNDED;

darkTAILfinalETM3 = allValuesSbiETM3[ETM3tailDARK];
brightTAILfinalETM3 = allValuesSbiETM3[ETM3tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to ETMlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to ETMcols
	{
	if (SBI3ETM[i,j] > 0 && SBI3ETM[i,j] <= darkTAILfinalETM3)
	{
		S3darkMASK[i,j] = 1;
		countETM3dark = countETM3dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S3darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to ETMlins									#Generate bright PIF mask for Scene 3
  {
	for j = 1 to ETMcols
	{
	if (SBI3ETM[i,j] >= brightTAILfinalETM3)
	{
		S3brightMASK[i,j] = 1;
		countETM3bright = countETM3bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S3brightMASK[i,j] = 0;
	}
	}
  }

 if ( countETM3dark >= pifDarkVAL && countETM3bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 3
 {
  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
	if ( (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 1) or (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 0) or (S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 1) )
	{
	S3MASK[i,j] = 1; 
	countS3PIF = countS3PIF + 1;
	} 
	else if ( S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 0 ) 
	{
	S3MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countETM3dark < pifDarkVAL && countETM3bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n",pifDarkVAL, countETM3dark);
    Exit();
}

else if ( countETM3bright < pifBrightVAL && countETM3dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n",pifBrightVAL, countETM3bright);
	Exit();
}
else if ( countETM3dark < pifDarkVAL && countETM3bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countETM3dark, pifBrightVAL, countETM3bright);
    Exit();
}

printf("The SBI raster (Scene 3 - ETM) consists of %12i pixels\n", ETM3realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", ETM3nullValCount, ETM3realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM3tailDARK, darkTAILfinalETM3);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM3tailBRIGHT, brightTAILfinalETM3);
printf("Scene 3 has %12i Pseudo Invariant Features\n\n", countS3PIF);
}

else if ( sensors3 == 6 )
{
numeric TM3realSize, TM3realSizeROUNDED, TM3tailDARK, TM3tailBRIGHT, darkTAILfinalTM3, brightTAILfinalTM3, TM3tailPosition;

numeric TM3nullValCount = 0;                         # Null Value count for TM3
numeric TM3realSizeNULL = 0;                         # pixels with value 0
numeric TM3realArrayCount = 1;

numeric countTM3dark = 0;                           # Dark Pif pixel count
numeric countTM3bright = 0;                         # Bright Pif pixel count
numeric countS3PIF = 0;                              # overall Pif count
 
for i = 1 to TMlins
{
    for j = 1 to TMcols
    {
        
		if (IsNull(SBI3TM[i,j]) == 1)
		{
		TM3nullValCount = TM3nullValCount + 1;
		}		 
    }
}

TM3realSize = (TMlins * TMcols) - TM3nullValCount;

array allValuesSbiTM3[TM3realSize];

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(SBI3TM[i,j]) == 0)
		{
		  allValuesSbiTM3[TM3realArrayCount] = SBI3TM[i,j];		 
		  TM3realArrayCount = TM3realArrayCount + 1;
	
		  if ( SBI3TM[i,j] == 0 )
		  {
		      TM3realSizeNULL = TM3realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastTM3 = TM3realSize;
	numeric hTM3 = 1;

	while ( (hTM3 * 3 + 1) < lastTM3 - 1 )
	{
		hTM3 = 3 * hTM3 + 1;
	}
	
	while ( hTM3 > 0 )
	{
		for i = hTM3 - 1 to lastTM3               # for each of the h sets of elements
		{
			numeric keyTM3 = allValuesSbiTM3[i];
			numeric jTM3 = i;
		
			while (jTM3 >= hTM3 && allValuesSbiTM3[jTM3 - hTM3] > keyTM3)
			{
				allValuesSbiTM3[jTM3] = allValuesSbiTM3[jTM3 - hTM3];
				jTM3 = jTM3 - hTM3;
			}
			allValuesSbiTM3[jTM3] = keyTM3;
		}
		hTM3 = floor(hTM3/3);
	}

TM3tailPosition = (TM3realSize - TM3realSizeNULL) * (histoTailInputVAL / 100);
TM3realSizeROUNDED = round(TM3tailPosition);            					               # ETM1tailNUM rounded to gain a integer value
TM3tailDARK =  TM3realSizeNULL + TM3realSizeROUNDED;
TM3tailBRIGHT = TM3realSize - TM3realSizeROUNDED;

darkTAILfinalTM3 = allValuesSbiTM3[TM3tailDARK];
brightTAILfinalTM3 = allValuesSbiTM3[TM3tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to TMlins									# Generate dark PIF mask for Scene 3			
  {
	for j = 1 to TMcols
	{
	if (SBI3TM[i,j] > 0 && SBI3TM[i,j] <= darkTAILfinalTM3)
	{
		S3darkMASK[i,j] = 1;
		countTM3dark = countTM3dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S3darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to TMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to TMcols
	{
	if (SBI3TM[i,j] >= brightTAILfinalTM3)
	{
		S3brightMASK[i,j] = 1;
		countTM3bright = countTM3bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S3brightMASK[i,j] = 0;
	}
	}
  }

 if ( countTM3dark >= pifDarkVAL && countTM3bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to TMlins
  {
	for j = 1 to TMcols
	{
	if ( (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 1) or (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 0) or (S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 1) )
	{
	S3MASK[i,j] = 1; 
	countS3PIF = countS3PIF + 1;
	} 
	else if ( S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 0 ) 
	{
	S3MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countTM3dark < pifDarkVAL && countTM3bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n",pifDarkVAL, countTM3dark);
    Exit();
}

else if ( countTM3bright < pifBrightVAL && countTM3dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n",pifBrightVAL, countTM3bright);
	Exit();
}
else if ( countTM3dark < pifDarkVAL && countTM3bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countTM3dark, pifBrightVAL, countTM3bright);
    Exit();
}

printf("The SBI raster (Scene 3 - TM) consists of %12i pixels\n", TM3realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", TM3nullValCount, TM3realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM3tailDARK, darkTAILfinalTM3);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM3tailBRIGHT, brightTAILfinalTM3);
printf("Scene 3 has %12i Pseudo Invariant Features\n\n", countS3PIF);
}

else
{
numeric MSS3realSize, MSS3realSizeROUNDED, MSS3tailDARK, MSS3tailBRIGHT, darkTAILfinalMSS3, brightTAILfinalMSS3, MSS3tailPosition;

numeric MSS3nullValCount = 0;                           # Null Value count for MSS3
numeric MSS3realSizeNULL = 0;                        # pixels with value 0
numeric MSS3realArrayCount = 1;

numeric countMSS3dark = 0;                           # Dark Pif pixel count
numeric countMSS3bright = 0;                         # Bright Pif pixel count
numeric countS3PIF = 0;                              # overall Pif count
 
for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {
        
		if (IsNull(SBI3MSS[i,j]) == 1)
		{
		MSS3nullValCount = MSS3nullValCount + 1;
		}		 
    }
}

MSS3realSize = (MSSlins * MSScols) - MSS3nullValCount;

array allValuesSbiMSS3[MSS3realSize];

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(SBI3MSS[i,j]) == 0)
		{
		  allValuesSbiMSS3[MSS3realArrayCount] = SBI3MSS[i,j];		 
		  MSS3realArrayCount = MSS3realArrayCount + 1;
	
		  if ( SBI3MSS[i,j] == 0 )
		  {
		      MSS3realSizeNULL = MSS3realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastMSS3 = MSS3realSize;
	numeric hMSS3 = 1;

	while ( (hMSS3 * 3 + 1) < lastMSS3 - 1 )
	{
		hMSS3 = 3 * hMSS3 + 1;
	}
	
	while ( hMSS3 > 0 )
	{
		for i = hMSS3 - 1 to lastMSS3               # for each of the h sets of elements
		{
			numeric keyMSS3 = allValuesSbiMSS3[i];
			numeric jMSS3 = i;
		
			while (jMSS3 >= hMSS3 && allValuesSbiMSS3[jMSS3 - hMSS3] > keyMSS3)
			{
				allValuesSbiMSS3[jMSS3] = allValuesSbiMSS3[jMSS3 - hMSS3];
				jMSS3 = jMSS3 - hMSS3;
			}
			allValuesSbiMSS3[jMSS3] = keyMSS3;
		}
		hMSS3 = floor(hMSS3/3);
	}

MSS3tailPosition = (MSS3realSize - MSS3realSizeNULL) * (histoTailInputVAL / 100);
MSS3realSizeROUNDED = round(MSS3tailPosition);            					             # MSS2realSizeROUNDED rounded to gain a integer value
MSS3tailDARK =  MSS3realSizeNULL + MSS3realSizeROUNDED;
MSS3tailBRIGHT = MSS3realSize - MSS3realSizeROUNDED;

darkTAILfinalMSS3 = allValuesSbiMSS3[MSS3tailDARK];
brightTAILfinalMSS3 = allValuesSbiMSS3[MSS3tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to MSSlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to MSScols
	{
	if (SBI3MSS[i,j] > 0 && SBI3MSS[i,j] <= darkTAILfinalMSS3)
	{
		S3darkMASK[i,j] = 1;
		countMSS3dark = countMSS3dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S3darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to MSSlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to MSScols
	{
	if (SBI3MSS[i,j] >= brightTAILfinalMSS3)
	{
		S3brightMASK[i,j] = 1;
		countMSS3bright = countMSS3bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S3brightMASK[i,j] = 0;
	}
	}
  }

 if ( countMSS3dark >= pifDarkVAL && countMSS3bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 1
 {
  for i = 1 to MSSlins
  {
	for j = 1 to MSScols
	{
	if ( (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 1) or (S3darkMASK[i,j] == 1 && S3brightMASK[i,j] == 0) or (S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 1) )
	{
	S3MASK[i,j] = 1; 
	countS3PIF = countS3PIF + 1;
	} 
	else if ( S3darkMASK[i,j] == 0 && S3brightMASK[i,j] == 0 ) 
	{
	S3MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countMSS3dark < pifDarkVAL && countMSS3bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n", pifDarkVAL, countMSS3dark);
    Exit();
}

else if ( countMSS3bright < pifBrightVAL && countMSS3dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 3 but only %3i were found. Quitting\n\n", pifBrightVAL, countMSS3bright);
	Exit();
}
else if ( countMSS3dark < pifDarkVAL && countMSS3bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countMSS3dark, pifBrightVAL, countMSS3bright);
    Exit();
}

printf("The SBI raster (Scene 3 - MSS) consists of %12i pixels\n", MSS3realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", MSS3nullValCount, MSS3realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS3tailDARK, darkTAILfinalMSS3);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS3tailBRIGHT, brightTAILfinalMSS3);
printf("Scene 3 has %12i Pseudo Invariant Features\n\n", countS3PIF);
}

######################################## Scene 4 ########################################

if ( sensors4 == 7 )
{
numeric ETM4realSize, ETM4realSizeROUNDED, ETM4tailDARK, ETM4tailBRIGHT, darkTAILfinalETM4, brightTAILfinalETM4, ETM4tailPosition;

numeric ETM4nullValCount = 0;                           # Null Value count for ETM4
numeric ETM4realSizeNULL = 0;                        # pixels with value 0
numeric ETM4realArrayCount = 1;

numeric countETM4dark = 0;                           # Dark Pif pixel count
numeric countETM4bright = 0;                         # Bright Pif pixel count
numeric countS4PIF = 0;                              # overall Pif count
 
for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {
        
		if (IsNull(SBI4ETM[i,j]) == 1)
		{
		ETM4nullValCount = ETM4nullValCount + 1;
		}		 
    }
}

ETM4realSize = (ETMlins * ETMcols) - ETM4nullValCount;

array allValuesSbiETM4[ETM4realSize];

for i = 1 to ETMlins
{
    for j = 1 to ETMcols
    {     
		if (IsNull(SBI4ETM[i,j]) == 0)
		{
		  allValuesSbiETM4[ETM4realArrayCount] = SBI4ETM[i,j];		 
		  ETM4realArrayCount = ETM4realArrayCount + 1;
	
		  if ( SBI4ETM[i,j] == 0 )
		  {
		      ETM4realSizeNULL = ETM4realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastETM4 = ETM4realSize;
	numeric hETM4 = 1;

	while ( (hETM4 * 3 + 1) < lastETM4 - 1 )
	{
		hETM4 = 3 * hETM4 + 1;
	}
	
	while ( hETM4 > 0 )
	{
		for i = hETM4 - 1 to lastETM4               # for each of the h sets of elements
		{
			numeric keyETM4 = allValuesSbiETM4[i];
			numeric jETM4 = i;
		
			while (jETM4 >= hETM4 && allValuesSbiETM4[jETM4 - hETM4] > keyETM4)
			{
				allValuesSbiETM4[jETM4] = allValuesSbiETM4[jETM4 - hETM4];
				jETM4 = jETM4 - hETM4;
			}
			allValuesSbiETM4[jETM4] = keyETM4;
		}
		hETM4 = floor(hETM4/3);
	}

ETM4tailPosition = (ETM4realSize - ETM4realSizeNULL) * (histoTailInputVAL / 100);
ETM4realSizeROUNDED = round(ETM4tailPosition);            					             # ETM4realSizeROUNDED rounded to gain a integer value
ETM4tailDARK =  ETM4realSizeNULL + ETM4realSizeROUNDED;
ETM4tailBRIGHT = ETM4realSize - ETM4realSizeROUNDED;

darkTAILfinalETM4 = allValuesSbiETM4[ETM4tailDARK];
brightTAILfinalETM4 = allValuesSbiETM4[ETM4tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to ETMlins									# Generate dark PIF mask for Scene 1			
  {
	for j = 1 to ETMcols
	{
	if (SBI4ETM[i,j] > 0 && SBI4ETM[i,j] <= darkTAILfinalETM4)
	{
		S4darkMASK[i,j] = 1;
		countETM4dark = countETM4dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S4darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to ETMlins									#Generate bright PIF mask for Scene 4
  {
	for j = 1 to ETMcols
	{
	if (SBI4ETM[i,j] >= brightTAILfinalETM4)
	{
		S4brightMASK[i,j] = 1;
		countETM4bright = countETM4bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S4brightMASK[i,j] = 0;
	}
	}
  }

 if ( countETM4dark >= pifDarkVAL && countETM4bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 4
 {
  for i = 1 to ETMlins
  {
	for j = 1 to ETMcols
	{
	if ( (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 1) or (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 0) or (S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 1) )
	{
	S4MASK[i,j] = 1; 
	countS4PIF = countS4PIF + 1;
	} 
	else if ( S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 0 ) 
	{
	S4MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countETM4dark < pifDarkVAL && countETM4bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n",pifDarkVAL, countETM4dark);
    Exit();
}

else if ( countETM4bright < pifBrightVAL && countETM4dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n",pifBrightVAL, countETM4bright);
	Exit();
}
else if ( countETM4dark < pifDarkVAL && countETM4bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countETM4dark, pifBrightVAL, countETM4bright);
    Exit();
}

printf("The SBI raster (Scene 4 - ETM) consists of %12i pixels\n", ETM4realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", ETM4nullValCount, ETM4realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM4tailDARK, darkTAILfinalETM4);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", ETM4tailBRIGHT, brightTAILfinalETM4);
printf("Scene 4 has %12i Pseudo Invariant Features\n\n", countS4PIF);
}

else if ( sensors4 == 6 )
{
numeric TM4realSize, TM4realSizeROUNDED, TM4tailDARK, TM4tailBRIGHT, darkTAILfinalTM4, brightTAILfinalTM4, TM4tailPosition;

numeric TM4nullValCount = 0;                         # Null Value count for TM4
numeric TM4realSizeNULL = 0;                         # pixels with value 0
numeric TM4realArrayCount = 1;

numeric countTM4dark = 0;                           # Dark Pif pixel count
numeric countTM4bright = 0;                         # Bright Pif pixel count
numeric countS4PIF = 0;                              # overall Pif count
 
for i = 1 to TMlins
{
    for j = 1 to TMcols
    {
        
		if (IsNull(SBI4TM[i,j]) == 1)
		{
		TM4nullValCount = TM4nullValCount + 1;
		}		 
    }
}

TM4realSize = (TMlins * TMcols) - TM4nullValCount;

array allValuesSbiTM4[TM4realSize];

for i = 1 to TMlins
{
    for j = 1 to TMcols
    {     
		if (IsNull(SBI4TM[i,j]) == 0)
		{
		  allValuesSbiTM4[TM4realArrayCount] = SBI4TM[i,j];		 
		  TM4realArrayCount = TM4realArrayCount + 1;
	
		  if ( SBI4TM[i,j] == 0 )
		  {
		      TM4realSizeNULL = TM4realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastTM4 = TM4realSize;
	numeric hTM4 = 1;

	while ( (hTM4 * 3 + 1) < lastTM4 - 1 )
	{
		hTM4 = 3 * hTM4 + 1;
	}
	
	while ( hTM4 > 0 )
	{
		for i = hTM4 - 1 to lastTM4               # for each of the h sets of elements
		{
			numeric keyTM4 = allValuesSbiTM4[i];
			numeric jTM4 = i;
		
			while (jTM4 >= hTM4 && allValuesSbiTM4[jTM4 - hTM4] > keyTM4)
			{
				allValuesSbiTM4[jTM4] = allValuesSbiTM4[jTM4 - hTM4];
				jTM4 = jTM4 - hTM4;
			}
			allValuesSbiTM4[jTM4] = keyTM4;
		}
		hTM4 = floor(hTM4/3);
	}

TM4tailPosition = (TM4realSize - TM4realSizeNULL) * (histoTailInputVAL / 100);
TM4realSizeROUNDED = round(TM4tailPosition);            					           # TM4realSizeROUNDED rounded to gain a integer value
TM4tailDARK =  TM4realSizeNULL + TM4realSizeROUNDED;
TM4tailBRIGHT = TM4realSize - TM4realSizeROUNDED;

darkTAILfinalTM4 = allValuesSbiTM4[TM4tailDARK];
brightTAILfinalTM4 = allValuesSbiTM4[TM4tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to TMlins									# Generate dark PIF mask for Scene 4			
  {
	for j = 1 to TMcols
	{
	if (SBI4TM[i,j] > 0 && SBI4TM[i,j] <= darkTAILfinalTM4)
	{
		S4darkMASK[i,j] = 1;
		countTM4dark = countTM4dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S4darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to TMlins									#Generate bright PIF mask for Scene 1
  {
	for j = 1 to TMcols
	{
	if (SBI4TM[i,j] >= brightTAILfinalTM4)
	{
		S4brightMASK[i,j] = 1;
		countTM4bright = countTM4bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S4brightMASK[i,j] = 0;
	}
	}
  }

 if ( countTM4dark >= pifDarkVAL && countTM4bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 4
 {
  for i = 1 to TMlins
  {
	for j = 1 to TMcols
	{
	if ( (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 1) or (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 0) or (S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 1) )
	{
	S4MASK[i,j] = 1; 
	countS4PIF = countS4PIF + 1;
	} 
	else if ( S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 0 ) 
	{
	S4MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countTM4dark < pifDarkVAL && countTM4bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n",pifDarkVAL, countTM4dark);
    Exit();
}

else if ( countTM4bright < pifBrightVAL && countTM4dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n",pifBrightVAL, countTM4bright);
	Exit();
}
else if ( countTM4dark < pifDarkVAL && countTM4bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countTM4dark, pifBrightVAL, countTM4bright);
    Exit();
}

printf("The SBI raster (Scene 4 - TM) consists of %12i pixels\n", TM4realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", TM4nullValCount, TM4realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM4tailDARK, darkTAILfinalTM4);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", TM4tailBRIGHT, brightTAILfinalTM4);
printf("Scene 4 has %12i Pseudo Invariant Features\n\n", countS4PIF);
}

else
{
numeric MSS4realSize, MSS4realSizeROUNDED, MSS4tailDARK, MSS4tailBRIGHT, darkTAILfinalMSS4, brightTAILfinalMSS4, MSS4tailPosition;

numeric MSS4nullValCount = 0;                           # Null Value count for MSS4
numeric MSS4realSizeNULL = 0;                        # pixels with value 0
numeric MSS4realArrayCount = 1;

numeric countMSS4dark = 0;                           # Dark Pif pixel count
numeric countMSS4bright = 0;                         # Bright Pif pixel count
numeric countS4PIF = 0;                              # overall Pif count
 
for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {
        
		if (IsNull(SBI4MSS[i,j]) == 1)
		{
		MSS4nullValCount = MSS4nullValCount + 1;
		}		 
    }
}

MSS4realSize = (MSSlins * MSScols) - MSS4nullValCount;

array allValuesSbiMSS4[MSS4realSize];

for i = 1 to MSSlins
{
    for j = 1 to MSScols
    {     
		if (IsNull(SBI4MSS[i,j]) == 0)
		{
		  allValuesSbiMSS4[MSS4realArrayCount] = SBI4MSS[i,j];		 
		  MSS4realArrayCount = MSS4realArrayCount + 1;
	
		  if ( SBI4MSS[i,j] == 0 )
		  {
		      MSS4realSizeNULL = MSS4realSizeNULL + 1;
		  }
		}
    }
}

	numeric lastMSS4 = MSS4realSize;
	numeric hMSS4 = 1;

	while ( (hMSS4 * 3 + 1) < lastMSS4 - 1 )
	{
		hMSS4 = 3 * hMSS4 + 1;
	}
	
	while ( hMSS4 > 0 )
	{
		for i = hMSS4 - 1 to lastMSS4               # for each of the h sets of elements
		{
			numeric keyMSS4 = allValuesSbiMSS4[i];
			numeric jMSS4 = i;
		
			while (jMSS4 >= hMSS4 && allValuesSbiMSS4[jMSS4 - hMSS4] > keyMSS4)
			{
				allValuesSbiMSS4[jMSS4] = allValuesSbiMSS4[jMSS4 - hMSS4];
				jMSS4 = jMSS4 - hMSS4;
			}
			allValuesSbiMSS4[jMSS4] = keyMSS4;
		}
		hMSS4 = floor(hMSS4/3);
	}

MSS4tailPosition = (MSS4realSize - MSS4realSizeNULL) * (histoTailInputVAL / 100);
MSS4realSizeROUNDED = round(MSS4tailPosition);            				              	# MSS4realSizeROUNDED rounded to gain a integer value
MSS4tailDARK =  MSS4realSizeNULL + MSS4realSizeROUNDED;
MSS4tailBRIGHT = MSS4realSize - MSS4realSizeROUNDED;

darkTAILfinalMSS4 = allValuesSbiMSS4[MSS4tailDARK];
brightTAILfinalMSS4 = allValuesSbiMSS4[MSS4tailBRIGHT];			# Final Threshold value, not position

  for i = 1 to MSSlins									# Generate dark PIF mask for Scene 4			
  {
	for j = 1 to MSScols
	{
	if (SBI4MSS[i,j] > 0 && SBI4MSS[i,j] <= darkTAILfinalMSS4)
	{
		S4darkMASK[i,j] = 1;
		countMSS4dark = countMSS4dark + 1;					# Dark PIF Pixel count
	}
	else
	{
		S4darkMASK[i,j] = 0;
	}
	}
  }

  for i = 1 to MSSlins									#Generate bright PIF mask for Scene 4
  {
	for j = 1 to MSScols
	{
	if (SBI4MSS[i,j] >= brightTAILfinalMSS4)
	{
		S4brightMASK[i,j] = 1;
		countMSS4bright = countMSS4bright + 1;				# Bright PIF Pixel count
	}
	else
	{
		S4brightMASK[i,j] = 0;
	}
	}
  }

 if ( countMSS4dark >= pifDarkVAL && countMSS4bright >= pifBrightVAL )						#Generate whole PIF mask for Scene 4
 {
  for i = 1 to MSSlins
  {
	for j = 1 to MSScols
	{
	if ( (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 1) or (S4darkMASK[i,j] == 1 && S4brightMASK[i,j] == 0) or (S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 1) )
	{
	S4MASK[i,j] = 1; 
	countS4PIF = countS4PIF + 1;
	} 
	else if ( S4darkMASK[i,j] == 0 && S4brightMASK[i,j] == 0 ) 
	{
	S4MASK[i,j] = 0;
	}
	}
  }
 }
 
else if ( countMSS4dark < pifDarkVAL && countMSS4bright >= pifBrightVAL )
{
    printf("%3i dark PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n", pifDarkVAL, countMSS4dark);
    Exit();
}

else if ( countMSS4bright < pifBrightVAL && countMSS4dark >= pifDarkVAL ) 
{
	printf("%3i bright PIFs were needed for Scene 4 but only %3i were found. Quitting\n\n", pifBrightVAL, countMSS4bright);
	Exit();
}
else if ( countMSS4dark < pifDarkVAL && countMSS4bright < pifBrightVAL ) 
{
    printf("Both dark and bright PIFs were way too few.\n%3i dark PIFs were needed but only %3i found.\n%3i bright PIFs were needed but only %3i found.\nQuitting\n",pifDarkVAL, countMSS4dark, pifBrightVAL, countMSS4bright);
    Exit();
}

printf("The SBI raster (Scene 4 - MSS) consists of %12i pixels\n", MSS4realSize);
printf("In the raster %12i NullValues were found and %12i pixels have the value 0\n", MSS4nullValCount, MSS4realSizeNULL);
printf("Dark PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS4tailDARK, darkTAILfinalMSS4);
printf("Bright PIF threshold has the pixel position %12i and a equivalent pixel value of %12f\n", MSS4tailBRIGHT, brightTAILfinalMSS4);
printf("Scene 4 has %12i Pseudo Invariant Features\n\n", countS4PIF);
}

#######################################################################################
#### Take Samplepixels and write arrays for later selection
#######################################################################################

numeric countPIFtotal = 0;
numeric countNOPIFtotal = 0;
numeric isTherePifMask = 0;                                 # 1 = one globale mask... 0 = 3Masks

if ( sensors1 == 7 )
{
    for i = 1 to ETMlins
    {
         for j = 1 to ETMcols
         {
            PIFMASK[i,j] = S1MASK[i,j] * S2MASK[i,j] * S3MASK[i,j] * S4MASK[i,j];
            if ( PIFMASK[i,j] == 1 )
            {
                countPIFtotal = countPIFtotal + 1;
            }
            else if ( PIFMASK[i,j] == 0 )
            {
                countNOPIFtotal = countNOPIFtotal + 1;
            }
         }
    }

    if ( countPIFtotal >= minPifVAL )
    {
    isTherePifMask = 1;
    printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nOne global mask is generated\n\n", countPIFtotal, countNOPIFtotal);
    }
    
    else
    {
    isTherePifMask = 0;
    printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nThree were generated\n\n", countPIFtotal, countNOPIFtotal);
    
    if ( sensors1 == 7 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else if ( sensors1 == 6 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
    }
}

else if ( sensors1 == 6 )
{

    for i = 1 to TMlins
    {
         for j = 1 to TMcols
         {
            PIFMASK[i,j] = S1MASK[i,j] * S2MASK[i,j] * S3MASK[i,j] * S4MASK[i,j];
            if ( PIFMASK[i,j] == 1 )
            {
                countPIFtotal = countPIFtotal + 1;
            }
            else if ( PIFMASK[i,j] == 0 )
            {
                countNOPIFtotal = countNOPIFtotal + 1;
            }
         }
    }

    if ( countPIFtotal >= minPifVAL )
    {
    isTherePifMask = 1;
    printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nOne global mask is generated\n\n", countPIFtotal, countNOPIFtotal);
    }
    
    else
    {
    isTherePifMask = 0;
    printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nThree were generated\n\n", countPIFtotal, countNOPIFtotal);
    
    if ( sensors1 == 7 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else if ( sensors1 == 6 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
    }
}

else
{
        for i = 1 to MSSlins
        {
             for j = 1 to MSScols
             {
                PIFMASK[i,j] = S1MASK[i,j] * S2MASK[i,j] * S3MASK[i,j] * S4MASK[i,j];
                if ( PIFMASK[i,j] == 1 )
                {
                    countPIFtotal = countPIFtotal + 1;
                }
                else if ( PIFMASK[i,j] == 0 )
                {
                    countNOPIFtotal = countNOPIFtotal + 1;
                }
             }
        }

        if ( countPIFtotal >= minPifVAL )
        {
            isTherePifMask = 1;
            printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nOne global mask is generated\n\n", countPIFtotal, countNOPIFtotal);
        }
    
        else
        {
            isTherePifMask = 0;
            printf("%10i PIF pixels were found.\n%10i none PIF pixels were found.\nThree were generated\n\n", countPIFtotal, countNOPIFtotal);
       
            if ( sensors1 == 7 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total+ 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j]== 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1)
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else if ( sensors1 == 6 )
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
            
            else
            {
                numeric countS1S2total = 0;
                numeric countS1S3total = 0;
                numeric countS1S4total = 0;
                
                numeric countS2S1total = 0;
                numeric countS2S3total = 0;
                numeric countS2S4total = 0;
                
                numeric countS3S1total = 0;
                numeric countS3S2total = 0;
                numeric countS3S4total = 0;
                
                numeric countS4S1total = 0;
                numeric countS4S2total = 0;
                numeric countS4S3total = 0;
                
                for i = 1 to ETMlins
                {
                for j = 1 to ETMcols
                {
                if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS1S2total = countS1S2total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS1S3total = countS1S3total + 1;
                }
                
                if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS1S4total = countS1S4total + 1;
                }
                
                ###########################################
                
                if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS2S1total = countS2S1total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS2S3total = countS2S3total + 1;
                }
                
                if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS2S4total = countS2S4total + 1;
                }
                
                ###########################################
                
                if ( S3MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS3S1total = countS3S1total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS3S2total = countS3S2total + 1;
                }
                
                if ( S3MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                {
                countS3S4total = countS3S4total + 1;
                }
                
                ###########################################
                
                if ( S4MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                {
                countS4S1total = countS4S1total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                {
                countS4S2total = countS4S2total + 1;
                }
                
                if ( S4MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                {
                countS4S3total = countS4S3total + 1;
                }
                }
                }
            }
    }
}

printf("Starting off with the data transfer into arrays.\n\n");

######################################### Array computation ###########################################

if ( isTherePifMask == 1 )
{
  if ( slaveMaster == 1 )
  {
    if ( sensors1 == 7 )
    {
    numeric pifTOTALS1arrayCount = 1;
    
    array pifTOTALS1ETM1array[countPIFtotal];
    array pifTOTALS1ETM2array[countPIFtotal];
    array pifTOTALS1ETM3array[countPIFtotal];
    array pifTOTALS1ETM4array[countPIFtotal];
    array pifTOTALS1ETM5array[countPIFtotal];
    array pifTOTALS1ETM7array[countPIFtotal];
    
    array pifTOTALS1ETM1arrayRAND[minPifVAL];
    array pifTOTALS1ETM2arrayRAND[minPifVAL];
    array pifTOTALS1ETM3arrayRAND[minPifVAL];
    array pifTOTALS1ETM4arrayRAND[minPifVAL];
    array pifTOTALS1ETM5arrayRAND[minPifVAL];
    array pifTOTALS1ETM7arrayRAND[minPifVAL];
    
    array pifTOTALS1ETM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS1ETM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS1ETM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS1ETM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS1ETM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS1ETM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                     pifTOTALS1ETM1array[pifTOTALS1arrayCount] = REFS1ETM1[i,j];
                     pifTOTALS1ETM2array[pifTOTALS1arrayCount] = REFS1ETM2[i,j];
                     pifTOTALS1ETM3array[pifTOTALS1arrayCount] = REFS1ETM3[i,j];
                     pifTOTALS1ETM4array[pifTOTALS1arrayCount] = REFS1ETM4[i,j];
                       pifTOTALS1ETM5array[pifTOTALS1arrayCount] = REFS1ETM5[i,j];
			     pifTOTALS1ETM7array[pifTOTALS1arrayCount] = REFS1ETM7[i,j];
			     pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS1ETM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM1 == 0 )
            {
                numeric randomValueTOTALS1ETM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM1 = pifTOTALS1ETM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM1 == randomValueTOTALS1ETM1 )
                    {
                        validTOTALS1ETM1 = 0;
                    }
                }
            }
        pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[randomValueTOTALS1ETM1];
        pifTOTALS1ETM1arrayRANDusedInd[i] = randomValueTOTALS1ETM1;
        validTOTALS1ETM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS1ETM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM2 == 0 )
            {
                numeric randomValueTOTALS1ETM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM2 = pifTOTALS1ETM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM2 == randomValueTOTALS1ETM2 )
                    {
                        validTOTALS1ETM2 = 0;
                    }
                }
            }
        pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[randomValueTOTALS1ETM2];
        pifTOTALS1ETM2arrayRANDusedInd[i] = randomValueTOTALS1ETM2;
        validTOTALS1ETM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS1ETM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM3 == 0 )
            {
                numeric randomValueTOTALS1ETM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM3 = pifTOTALS1ETM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM3 == randomValueTOTALS1ETM3 )
                    {
                        validTOTALS1ETM3 = 0;
                    }
                }
            }
        pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[randomValueTOTALS1ETM3];
        pifTOTALS1ETM3arrayRANDusedInd[i] = randomValueTOTALS1ETM3;
        validTOTALS1ETM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS1ETM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM4 == 0 )
            {
                numeric randomValueTOTALS1ETM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM4 = pifTOTALS1ETM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM4 == randomValueTOTALS1ETM4 )
                    {
                        validTOTALS1ETM4 = 0;
                    }
                }
            }
        pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[randomValueTOTALS1ETM4];
        pifTOTALS1ETM4arrayRANDusedInd[i] = randomValueTOTALS1ETM4;
        validTOTALS1ETM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS1ETM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM5 == 0 )
            {
                numeric randomValueTOTALS1ETM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM5 = pifTOTALS1ETM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM5 == randomValueTOTALS1ETM5 )
                    {
                        validTOTALS1ETM5 = 0;
                    }
                }
            }
        pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[randomValueTOTALS1ETM5];
        pifTOTALS1ETM5arrayRANDusedInd[i] = randomValueTOTALS1ETM5;
        validTOTALS1ETM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS1ETM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1ETM7 == 0 )
            {
                numeric randomValueTOTALS1ETM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1ETM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1ETM7 = pifTOTALS1ETM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1ETM7 == randomValueTOTALS1ETM7 )
                    {
                        validTOTALS1ETM7 = 0;
                    }
                }
            }
        pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[randomValueTOTALS1ETM7];
        pifTOTALS1ETM7arrayRANDusedInd[i] = randomValueTOTALS1ETM7;
        validTOTALS1ETM7 = 0;
        }
    }
    
    else if ( sensors1 == 6 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1TM1array[countPIFtotal];
    array pifTOTALS1TM2array[countPIFtotal];
    array pifTOTALS1TM3array[countPIFtotal];
    array pifTOTALS1TM4array[countPIFtotal];
    array pifTOTALS1TM5array[countPIFtotal];
    array pifTOTALS1TM7array[countPIFtotal];
    
    array pifTOTALS1TM1arrayRAND[minPifVAL];
    array pifTOTALS1TM2arrayRAND[minPifVAL];
    array pifTOTALS1TM3arrayRAND[minPifVAL];
    array pifTOTALS1TM4arrayRAND[minPifVAL];
    array pifTOTALS1TM5arrayRAND[minPifVAL];
    array pifTOTALS1TM7arrayRAND[minPifVAL];
    
    array pifTOTALS1TM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS1TM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS1TM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS1TM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS1TM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS1TM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1TM1array[pifTOTALS1arrayCount] = REFS1TM1[i,j];
			      pifTOTALS1TM2array[pifTOTALS1arrayCount] = REFS1TM2[i,j];
			      pifTOTALS1TM3array[pifTOTALS1arrayCount] = REFS1TM3[i,j];
			      pifTOTALS1TM4array[pifTOTALS1arrayCount] = REFS1TM4[i,j];
                  pifTOTALS1TM5array[pifTOTALS1arrayCount] = REFS1TM5[i,j];
			      pifTOTALS1TM7array[pifTOTALS1arrayCount] = REFS1TM7[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
        
    ############# band 1
    
        numeric validTOTALS1TM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM1 == 0 )
            {
                numeric randomValueTOTALS1TM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM1 = pifTOTALS1TM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM1 == randomValueTOTALS1TM1 )
                    {
                        validTOTALS1TM1 = 0;
                    }
                }
            }
        pifTOTALS1TM1arrayRAND[i] = pifTOTALS1TM1array[randomValueTOTALS1TM1];
        pifTOTALS1TM1arrayRANDusedInd[i] = randomValueTOTALS1TM1;
        validTOTALS1TM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS1TM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM2 == 0 )
            {
                numeric randomValueTOTALS1TM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM2 = pifTOTALS1TM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM2 == randomValueTOTALS1TM2 )
                    {
                        validTOTALS1TM2 = 0;
                    }
                }
            }
        pifTOTALS1TM2arrayRAND[i] = pifTOTALS1TM2array[randomValueTOTALS1TM2];
        pifTOTALS1TM2arrayRANDusedInd[i] = randomValueTOTALS1TM2;
        validTOTALS1TM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS1TM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM3 == 0 )
            {
                numeric randomValueTOTALS1TM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM3 = pifTOTALS1TM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM3 == randomValueTOTALS1TM3 )
                    {
                        validTOTALS1TM3 = 0;
                    }
                }
            }
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[randomValueTOTALS1TM3];
        pifTOTALS1TM3arrayRANDusedInd[i] = randomValueTOTALS1TM3;
        validTOTALS1TM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS1TM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM4 == 0 )
            {
                numeric randomValueTOTALS1TM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM4 = pifTOTALS1TM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM4 == randomValueTOTALS1TM4 )
                    {
                        validTOTALS1TM4 = 0;
                    }
                }
            }
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[randomValueTOTALS1TM4];
        pifTOTALS1TM4arrayRANDusedInd[i] = randomValueTOTALS1TM4;
        validTOTALS1TM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS1TM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM5 == 0 )
            {
                numeric randomValueTOTALS1TM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM5 = pifTOTALS1TM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM5 == randomValueTOTALS1TM5 )
                    {
                        validTOTALS1TM5 = 0;
                    }
                }
            }
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[randomValueTOTALS1TM5];
        pifTOTALS1TM5arrayRANDusedInd[i] = randomValueTOTALS1TM5;
        validTOTALS1TM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS1TM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1TM7 == 0 )
            {
                numeric randomValueTOTALS1TM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1TM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1TM7 = pifTOTALS1TM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1TM7 == randomValueTOTALS1TM7 )
                    {
                        validTOTALS1TM7 = 0;
                    }
                }
            }
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[randomValueTOTALS1TM7];
        pifTOTALS1TM7arrayRANDusedInd[i] = randomValueTOTALS1TM7;
        validTOTALS1TM7 = 0;
        }
    }
    
    else
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1MSS1array[countPIFtotal];
    array pifTOTALS1MSS2array[countPIFtotal];
    array pifTOTALS1MSS3array[countPIFtotal];
    array pifTOTALS1MSS4array[countPIFtotal];
    
    array pifTOTALS1MSS1arrayRAND[minPifVAL];
    array pifTOTALS1MSS2arrayRAND[minPifVAL];
    array pifTOTALS1MSS3arrayRAND[minPifVAL];
    array pifTOTALS1MSS4arrayRAND[minPifVAL];
    
    array pifTOTALS1MSS1arrayRANDusedInd[minPifVAL];
    array pifTOTALS1MSS2arrayRANDusedInd[minPifVAL];
    array pifTOTALS1MSS3arrayRANDusedInd[minPifVAL];
    array pifTOTALS1MSS4arrayRANDusedInd[minPifVAL];
        
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1MSS1array[pifTOTALS1arrayCount] = REFS1MSS1[i,j];
			pifTOTALS1MSS2array[pifTOTALS1arrayCount] = REFS1MSS2[i,j];
			pifTOTALS1MSS3array[pifTOTALS1arrayCount] = REFS1MSS3[i,j];
			pifTOTALS1MSS4array[pifTOTALS1arrayCount] = REFS1MSS4[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS1MSS1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1MSS1 == 0 )
            {
                numeric randomValueTOTALS1MSS1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1MSS1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1MSS1 = pifTOTALS1MSS1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1MSS1 == randomValueTOTALS1MSS1 )
                    {
                        validTOTALS1MSS1 = 0;
                    }
                }
            }
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[randomValueTOTALS1MSS1];
        pifTOTALS1MSS1arrayRANDusedInd[i] = randomValueTOTALS1MSS1;
        validTOTALS1MSS1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS1MSS2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1MSS2 == 0 )
            {
                numeric randomValueTOTALS1MSS2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1MSS2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1MSS2 = pifTOTALS1MSS2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1MSS2 == randomValueTOTALS1MSS2 )
                    {
                        validTOTALS1MSS2 = 0;
                    }
                }
            }
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[randomValueTOTALS1MSS2];
        pifTOTALS1MSS2arrayRANDusedInd[i] = randomValueTOTALS1MSS2;
        validTOTALS1MSS2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS1MSS3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1MSS3 == 0 )
            {
                numeric randomValueTOTALS1MSS3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1MSS3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1MSS3 = pifTOTALS1MSS3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1MSS3 == randomValueTOTALS1MSS3 )
                    {
                        validTOTALS1MSS3 = 0;
                    }
                }
            }
        pifTOTALS1MSS3arrayRAND[i] = pifTOTALS1MSS3array[randomValueTOTALS1MSS3];
        pifTOTALS1MSS3arrayRANDusedInd[i] = randomValueTOTALS1MSS3;
        validTOTALS1MSS3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS1MSS4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS1MSS4 == 0 )
            {
                numeric randomValueTOTALS1MSS4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS1MSS4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS1MSS4 = pifTOTALS1MSS4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS1MSS4 == randomValueTOTALS1MSS4 )
                    {
                        validTOTALS1MSS4 = 0;
                    }
                }
            }
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[randomValueTOTALS1MSS4];
        pifTOTALS1MSS4arrayRANDusedInd[i] = randomValueTOTALS1MSS4;
        validTOTALS1MSS4 = 0;
        }
    }

    if ( sensors2 == 7 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2ETM1array[countPIFtotal];
    array pifTOTALS2ETM2array[countPIFtotal];
    array pifTOTALS2ETM3array[countPIFtotal];
    array pifTOTALS2ETM4array[countPIFtotal];
    array pifTOTALS2ETM5array[countPIFtotal];
    array pifTOTALS2ETM7array[countPIFtotal];
    
    array pifTOTALS2ETM1arrayRAND[minPifVAL];
    array pifTOTALS2ETM2arrayRAND[minPifVAL];
    array pifTOTALS2ETM3arrayRAND[minPifVAL];
    array pifTOTALS2ETM4arrayRAND[minPifVAL];
    array pifTOTALS2ETM5arrayRAND[minPifVAL];
    array pifTOTALS2ETM7arrayRAND[minPifVAL];
        
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2ETM1array[pifTOTALS2arrayCount] = REFS2ETM1[i,j];
			pifTOTALS2ETM2array[pifTOTALS2arrayCount] = REFS2ETM2[i,j];
			pifTOTALS2ETM3array[pifTOTALS2arrayCount] = REFS2ETM3[i,j];
			pifTOTALS2ETM4array[pifTOTALS2arrayCount] = REFS2ETM4[i,j];
                  pifTOTALS2ETM5array[pifTOTALS2arrayCount] = REFS2ETM5[i,j];
			pifTOTALS2ETM7array[pifTOTALS2arrayCount] = REFS2ETM7[i,j];
			pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    if ( sensors1 == 7 )
    {     
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {
	  for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }
    }
    
    ############# band 2
    if ( sensors1 == 7 )
    { 
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
    }
    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }
    }
    
    ############## band 3
    if ( sensors1 == 7 )
    {            
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
    }
    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }
    }

    ################ band 4

    if ( sensors1 == 7 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }
    }

    ############## band 5

    if ( sensors1 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        }
    }

    ################ band 7

    if ( sensors1 == 7 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }
    }

   }
    
    else if ( sensors2 == 6 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2TM1array[countPIFtotal];
    array pifTOTALS2TM2array[countPIFtotal];
    array pifTOTALS2TM3array[countPIFtotal];
    array pifTOTALS2TM4array[countPIFtotal];
    array pifTOTALS2TM5array[countPIFtotal];
    array pifTOTALS2TM7array[countPIFtotal];
    
    array pifTOTALS2TM1arrayRAND[minPifVAL];
    array pifTOTALS2TM2arrayRAND[minPifVAL];
    array pifTOTALS2TM3arrayRAND[minPifVAL];
    array pifTOTALS2TM4arrayRAND[minPifVAL];
    array pifTOTALS2TM5arrayRAND[minPifVAL];
    array pifTOTALS2TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2TM1array[pifTOTALS2arrayCount] = REFS2TM1[i,j];
			pifTOTALS2TM2array[pifTOTALS2arrayCount] = REFS2TM2[i,j];
			pifTOTALS2TM3array[pifTOTALS2arrayCount] = REFS2TM3[i,j];
			pifTOTALS2TM4array[pifTOTALS2arrayCount] = REFS2TM4[i,j];
                  pifTOTALS2TM5array[pifTOTALS2arrayCount] = REFS2TM5[i,j];
			pifTOTALS2TM7array[pifTOTALS2arrayCount] = REFS2TM7[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1

    if ( sensors1 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }
    }

    ############# band 2
    
    if ( sensors1 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
    }

    else if ( sensors1 == 6 )
    {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }  
    }

    ############## band 3
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
    
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        }   
	}

    ################ band 7
        
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }    
	  }
    }
    
    else
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2MSS1array[countPIFtotal];
    array pifTOTALS2MSS2array[countPIFtotal];
    array pifTOTALS2MSS3array[countPIFtotal];
    array pifTOTALS2MSS4array[countPIFtotal];
    
    array pifTOTALS2MSS1arrayRAND[minPifVAL];
    array pifTOTALS2MSS2arrayRAND[minPifVAL];
    array pifTOTALS2MSS3arrayRAND[minPifVAL];
    array pifTOTALS2MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2MSS1array[pifTOTALS2arrayCount] = REFS2MSS1[i,j];
			pifTOTALS2MSS2array[pifTOTALS2arrayCount] = REFS2MSS2[i,j];
			pifTOTALS2MSS3array[pifTOTALS2arrayCount] = REFS2MSS3[i,j];
			pifTOTALS2MSS4array[pifTOTALS2arrayCount] = REFS2MSS4[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS1MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS1MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
    if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
    {
	   for i = 1 to minPifVAL
        {
        pifTOTALS2MSS3arrayRAND[i] = pifTOTALS2MSS3array[pifTOTALS1MSS3arrayRANDusedInd[i]];
        }
	}
    
    ################ band 4
    
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS1MSS4arrayRANDusedInd[i]];
        }
	}    
    }

    if ( sensors3 == 7 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3ETM1array[countPIFtotal];
    array pifTOTALS3ETM2array[countPIFtotal];
    array pifTOTALS3ETM3array[countPIFtotal];
    array pifTOTALS3ETM4array[countPIFtotal];
    array pifTOTALS3ETM5array[countPIFtotal];
    array pifTOTALS3ETM7array[countPIFtotal];
    
    array pifTOTALS3ETM1arrayRAND[minPifVAL];
    array pifTOTALS3ETM2arrayRAND[minPifVAL];
    array pifTOTALS3ETM3arrayRAND[minPifVAL];
    array pifTOTALS3ETM4arrayRAND[minPifVAL];
    array pifTOTALS3ETM5arrayRAND[minPifVAL];
    array pifTOTALS3ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3ETM1array[pifTOTALS3arrayCount] = REFS3ETM1[i,j];
			pifTOTALS3ETM2array[pifTOTALS3arrayCount] = REFS3ETM2[i,j];
			pifTOTALS3ETM3array[pifTOTALS3arrayCount] = REFS3ETM3[i,j];
			pifTOTALS3ETM4array[pifTOTALS3arrayCount] = REFS3ETM4[i,j];
                  pifTOTALS3ETM5array[pifTOTALS3arrayCount] = REFS3ETM5[i,j];
			pifTOTALS3ETM7array[pifTOTALS3arrayCount] = REFS3ETM7[i,j];
			pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

	else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }   
	}
    }
    
    else if ( sensors3 == 6 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3TM1array[countPIFtotal];
    array pifTOTALS3TM2array[countPIFtotal];
    array pifTOTALS3TM3array[countPIFtotal];
    array pifTOTALS3TM4array[countPIFtotal];
    array pifTOTALS3TM5array[countPIFtotal];
    array pifTOTALS3TM7array[countPIFtotal];
    
    array pifTOTALS3TM1arrayRAND[minPifVAL];
    array pifTOTALS3TM2arrayRAND[minPifVAL];
    array pifTOTALS3TM3arrayRAND[minPifVAL];
    array pifTOTALS3TM4arrayRAND[minPifVAL];
    array pifTOTALS3TM5arrayRAND[minPifVAL];
    array pifTOTALS3TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3TM1array[pifTOTALS3arrayCount] = REFS3TM1[i,j];
			pifTOTALS3TM2array[pifTOTALS3arrayCount] = REFS3TM2[i,j];
			pifTOTALS3TM3array[pifTOTALS3arrayCount] = REFS3TM3[i,j];
			pifTOTALS3TM4array[pifTOTALS3arrayCount] = REFS3TM4[i,j];
                  pifTOTALS3TM5array[pifTOTALS3arrayCount] = REFS3TM5[i,j];
			pifTOTALS3TM7array[pifTOTALS3arrayCount] = REFS3TM7[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }   
	}

    ############## band 5
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }  
	}
    }
    
    else
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3MSS1array[countPIFtotal];
    array pifTOTALS3MSS2array[countPIFtotal];
    array pifTOTALS3MSS3array[countPIFtotal];
    array pifTOTALS3MSS4array[countPIFtotal];
    
    array pifTOTALS3MSS1arrayRAND[minPifVAL];
    array pifTOTALS3MSS2arrayRAND[minPifVAL];
    array pifTOTALS3MSS3arrayRAND[minPifVAL];
    array pifTOTALS3MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3MSS1array[pifTOTALS3arrayCount] = REFS3MSS1[i,j];
			pifTOTALS3MSS2array[pifTOTALS3arrayCount] = REFS3MSS2[i,j];
			pifTOTALS3MSS3array[pifTOTALS3arrayCount] = REFS3MSS3[i,j];
			pifTOTALS3MSS4array[pifTOTALS3arrayCount] = REFS3MSS4[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS1MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS1MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
    if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS3arrayRAND[i] = pifTOTALS3MSS3array[pifTOTALS1MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }    
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS1MSS4arrayRANDusedInd[i]];
        }
	}
    }

    if ( sensors4 == 7 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4ETM1array[countPIFtotal];
    array pifTOTALS4ETM2array[countPIFtotal];
    array pifTOTALS4ETM3array[countPIFtotal];
    array pifTOTALS4ETM4array[countPIFtotal];
    array pifTOTALS4ETM5array[countPIFtotal];
    array pifTOTALS4ETM7array[countPIFtotal];
    
    array pifTOTALS4ETM1arrayRAND[minPifVAL];
    array pifTOTALS4ETM2arrayRAND[minPifVAL];
    array pifTOTALS4ETM3arrayRAND[minPifVAL];
    array pifTOTALS4ETM4arrayRAND[minPifVAL];
    array pifTOTALS4ETM5arrayRAND[minPifVAL];
    array pifTOTALS4ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4ETM1array[pifTOTALS4arrayCount] = REFS4ETM1[i,j];
			pifTOTALS4ETM2array[pifTOTALS4arrayCount] = REFS4ETM2[i,j];
			pifTOTALS4ETM3array[pifTOTALS4arrayCount] = REFS4ETM3[i,j];
			pifTOTALS4ETM4array[pifTOTALS4arrayCount] = REFS4ETM4[i,j];
                  pifTOTALS4ETM5array[pifTOTALS4arrayCount] = REFS4ETM5[i,j];
			pifTOTALS4ETM7array[pifTOTALS4arrayCount] = REFS4ETM7[i,j];
			pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }
	}

    ############## band 3
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }  
	}

    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {   
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        } 
	}
    
    ################ band 7
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
	  }

	  else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else if ( sensors4 == 6 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4TM1array[countPIFtotal];
    array pifTOTALS4TM2array[countPIFtotal];
    array pifTOTALS4TM3array[countPIFtotal];
    array pifTOTALS4TM4array[countPIFtotal];
    array pifTOTALS4TM5array[countPIFtotal];
    array pifTOTALS4TM7array[countPIFtotal];
    
    array pifTOTALS4TM1arrayRAND[minPifVAL];
    array pifTOTALS4TM2arrayRAND[minPifVAL];
    array pifTOTALS4TM3arrayRAND[minPifVAL];
    array pifTOTALS4TM4arrayRAND[minPifVAL];
    array pifTOTALS4TM5arrayRAND[minPifVAL];
    array pifTOTALS4TM7arrayRAND[minPifVAL];
        
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4TM1array[pifTOTALS4arrayCount] = REFS4TM1[i,j];
			pifTOTALS4TM2array[pifTOTALS4arrayCount] = REFS4TM2[i,j];
			pifTOTALS4TM3array[pifTOTALS4arrayCount] = REFS4TM3[i,j];
			pifTOTALS4TM4array[pifTOTALS4arrayCount] = REFS4TM4[i,j];
                  pifTOTALS4TM5array[pifTOTALS4arrayCount] = REFS4TM5[i,j];
			pifTOTALS4TM7array[pifTOTALS4arrayCount] = REFS4TM7[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS1ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS1TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }    
	}

    ############## band 3
	
      if ( sensors1 == 7 )
      {         
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors1 == 7 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS1ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS1TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS1ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS1TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4MSS1array[countPIFtotal];
    array pifTOTALS4MSS2array[countPIFtotal];
    array pifTOTALS4MSS3array[countPIFtotal];
    array pifTOTALS4MSS4array[countPIFtotal];
    
    array pifTOTALS4MSS1arrayRAND[minPifVAL];
    array pifTOTALS4MSS2arrayRAND[minPifVAL];
    array pifTOTALS4MSS3arrayRAND[minPifVAL];
    array pifTOTALS4MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4MSS1array[pifTOTALS4arrayCount] = REFS4MSS1[i,j];
			pifTOTALS4MSS2array[pifTOTALS4arrayCount] = REFS4MSS2[i,j];
			pifTOTALS4MSS3array[pifTOTALS4arrayCount] = REFS4MSS3[i,j];
			pifTOTALS4MSS4array[pifTOTALS4arrayCount] = REFS4MSS4[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS1ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS1TM2arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS1MSS1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS1ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS1TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS1MSS2arrayRANDusedInd[i]];
        }   
	}
    
    ############## band 3
     
    if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS3arrayRAND[i] = pifTOTALS4MSS3array[pifTOTALS1MSS3arrayRANDusedInd[i]];
        }
	}
    
    ################ band 4
     
      if ( sensors1 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS1ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors1 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS1TM4arrayRANDusedInd[i]];
        }    
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS1MSS4arrayRANDusedInd[i]];
        }
	}
    }
  }

  else if ( slaveMaster == 2 )
  {
    if ( sensors2 == 7 )
    {
    numeric pifTOTALS2arrayCount = 1;
    
    array pifTOTALS2ETM1array[countPIFtotal];
    array pifTOTALS2ETM2array[countPIFtotal];
    array pifTOTALS2ETM3array[countPIFtotal];
    array pifTOTALS2ETM4array[countPIFtotal];
    array pifTOTALS2ETM5array[countPIFtotal];
    array pifTOTALS2ETM7array[countPIFtotal];
    
    array pifTOTALS2ETM1arrayRAND[minPifVAL];
    array pifTOTALS2ETM2arrayRAND[minPifVAL];
    array pifTOTALS2ETM3arrayRAND[minPifVAL];
    array pifTOTALS2ETM4arrayRAND[minPifVAL];
    array pifTOTALS2ETM5arrayRAND[minPifVAL];
    array pifTOTALS2ETM7arrayRAND[minPifVAL];
    
    array pifTOTALS2ETM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS2ETM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS2ETM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS2ETM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS2ETM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS2ETM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                       pifTOTALS2ETM1array[pifTOTALS2arrayCount] = REFS2ETM1[i,j];
			     pifTOTALS2ETM2array[pifTOTALS2arrayCount] = REFS2ETM2[i,j];
			     pifTOTALS2ETM3array[pifTOTALS2arrayCount] = REFS2ETM3[i,j];
			     pifTOTALS2ETM4array[pifTOTALS2arrayCount] = REFS2ETM4[i,j];
                       pifTOTALS2ETM5array[pifTOTALS2arrayCount] = REFS2ETM5[i,j];
			     pifTOTALS2ETM7array[pifTOTALS2arrayCount] = REFS2ETM7[i,j];
			     pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS2ETM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM1 == 0 )
            {
                numeric randomValueTOTALS2ETM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM1 = pifTOTALS2ETM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM1 == randomValueTOTALS2ETM1 )
                    {
                        validTOTALS2ETM1 = 0;
                    }
                }
            }
        pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[randomValueTOTALS2ETM1];
        pifTOTALS2ETM1arrayRANDusedInd[i] = randomValueTOTALS2ETM1;
        validTOTALS2ETM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS2ETM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM2 == 0 )
            {
                numeric randomValueTOTALS2ETM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM2 = pifTOTALS2ETM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM2 == randomValueTOTALS2ETM2 )
                    {
                        validTOTALS2ETM2 = 0;
                    }
                }
            }
        pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[randomValueTOTALS2ETM2];
        pifTOTALS2ETM2arrayRANDusedInd[i] = randomValueTOTALS2ETM2;
        validTOTALS2ETM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS2ETM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM3 == 0 )
            {
                numeric randomValueTOTALS2ETM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM3 = pifTOTALS2ETM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM3 == randomValueTOTALS2ETM3 )
                    {
                        validTOTALS2ETM3 = 0;
                    }
                }
            }
        pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[randomValueTOTALS2ETM3];
        pifTOTALS2ETM3arrayRANDusedInd[i] = randomValueTOTALS2ETM3;
        validTOTALS2ETM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS2ETM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM4 == 0 )
            {
                numeric randomValueTOTALS2ETM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM4 = pifTOTALS2ETM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM4 == randomValueTOTALS2ETM4 )
                    {
                        validTOTALS2ETM4 = 0;
                    }
                }
            }
        pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[randomValueTOTALS2ETM4];
        pifTOTALS2ETM4arrayRANDusedInd[i] = randomValueTOTALS2ETM4;
        validTOTALS2ETM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS2ETM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM5 == 0 )
            {
                numeric randomValueTOTALS2ETM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM5 = pifTOTALS2ETM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM5 == randomValueTOTALS2ETM5 )
                    {
                        validTOTALS2ETM5 = 0;
                    }
                }
            }
        pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[randomValueTOTALS2ETM5];
        pifTOTALS2ETM5arrayRANDusedInd[i] = randomValueTOTALS2ETM5;
        validTOTALS2ETM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS2ETM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2ETM7 == 0 )
            {
                numeric randomValueTOTALS2ETM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2ETM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2ETM7 = pifTOTALS2ETM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2ETM7 == randomValueTOTALS2ETM7 )
                    {
                        validTOTALS2ETM7 = 0;
                    }
                }
            }
        pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[randomValueTOTALS2ETM7];
        pifTOTALS2ETM7arrayRANDusedInd[i] = randomValueTOTALS2ETM7;
        validTOTALS2ETM7 = 0;
        }
    }
    
    else if ( sensors2 == 6 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2TM1array[countPIFtotal];
    array pifTOTALS2TM2array[countPIFtotal];
    array pifTOTALS2TM3array[countPIFtotal];
    array pifTOTALS2TM4array[countPIFtotal];
    array pifTOTALS2TM5array[countPIFtotal];
    array pifTOTALS2TM7array[countPIFtotal];
    
    array pifTOTALS2TM1arrayRAND[minPifVAL];
    array pifTOTALS2TM2arrayRAND[minPifVAL];
    array pifTOTALS2TM3arrayRAND[minPifVAL];
    array pifTOTALS2TM4arrayRAND[minPifVAL];
    array pifTOTALS2TM5arrayRAND[minPifVAL];
    array pifTOTALS2TM7arrayRAND[minPifVAL];
    
    array pifTOTALS2TM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS2TM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS2TM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS2TM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS2TM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS2TM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2TM1array[pifTOTALS2arrayCount] = REFS2TM1[i,j];
			      pifTOTALS2TM2array[pifTOTALS2arrayCount] = REFS2TM2[i,j];
			      pifTOTALS2TM3array[pifTOTALS2arrayCount] = REFS2TM3[i,j];
			      pifTOTALS2TM4array[pifTOTALS2arrayCount] = REFS2TM4[i,j];
                  pifTOTALS2TM5array[pifTOTALS2arrayCount] = REFS2TM5[i,j];
			      pifTOTALS2TM7array[pifTOTALS2arrayCount] = REFS2TM7[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
        
    ############# band 1
    
        numeric validTOTALS2TM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM1 == 0 )
            {
                numeric randomValueTOTALS2TM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM1 = pifTOTALS2TM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM1 == randomValueTOTALS2TM1 )
                    {
                        validTOTALS2TM1 = 0;
                    }
                }
            }
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[randomValueTOTALS2TM1];
        pifTOTALS2TM1arrayRANDusedInd[i] = randomValueTOTALS2TM1;
        validTOTALS2TM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS2TM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM2 == 0 )
            {
                numeric randomValueTOTALS2TM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM2 = pifTOTALS2TM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM2 == randomValueTOTALS2TM2 )
                    {
                        validTOTALS2TM2 = 0;
                    }
                }
            }
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[randomValueTOTALS2TM2];
        pifTOTALS2TM2arrayRANDusedInd[i] = randomValueTOTALS2TM2;
        validTOTALS2TM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS2TM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM3 == 0 )
            {
                numeric randomValueTOTALS2TM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM3 = pifTOTALS2TM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM3 == randomValueTOTALS2TM3 )
                    {
                        validTOTALS2TM3 = 0;
                    }
                }
            }
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[randomValueTOTALS2TM3];
        pifTOTALS2TM3arrayRANDusedInd[i] = randomValueTOTALS2TM3;
        validTOTALS2TM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS2TM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM4 == 0 )
            {
                numeric randomValueTOTALS2TM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM4 = pifTOTALS2TM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM4 == randomValueTOTALS2TM4 )
                    {
                        validTOTALS2TM4 = 0;
                    }
                }
            }
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[randomValueTOTALS2TM4];
        pifTOTALS2TM4arrayRANDusedInd[i] = randomValueTOTALS2TM4;
        validTOTALS2TM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS2TM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM5 == 0 )
            {
                numeric randomValueTOTALS2TM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM5 = pifTOTALS2TM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM5 == randomValueTOTALS2TM5 )
                    {
                        validTOTALS2TM5 = 0;
                    }
                }
            }
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[randomValueTOTALS2TM5];
        pifTOTALS2TM5arrayRANDusedInd[i] = randomValueTOTALS2TM5;
        validTOTALS2TM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS2TM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2TM7 == 0 )
            {
                numeric randomValueTOTALS2TM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2TM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2TM7 = pifTOTALS2TM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2TM7 == randomValueTOTALS2TM7 )
                    {
                        validTOTALS2TM7 = 0;
                    }
                }
            }
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[randomValueTOTALS2TM7];
        pifTOTALS2TM7arrayRANDusedInd[i] = randomValueTOTALS2TM7;
        validTOTALS2TM7 = 0;
        }
    }
    
    else
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2MSS1array[countPIFtotal];
    array pifTOTALS2MSS2array[countPIFtotal];
    array pifTOTALS2MSS3array[countPIFtotal];
    array pifTOTALS2MSS4array[countPIFtotal];
    
    array pifTOTALS2MSS1arrayRAND[minPifVAL];
    array pifTOTALS2MSS2arrayRAND[minPifVAL];
    array pifTOTALS2MSS3arrayRAND[minPifVAL];
    array pifTOTALS2MSS4arrayRAND[minPifVAL];
    
    array pifTOTALS2MSS1arrayRANDusedInd[minPifVAL];
    array pifTOTALS2MSS2arrayRANDusedInd[minPifVAL];
    array pifTOTALS2MSS3arrayRANDusedInd[minPifVAL];
    array pifTOTALS2MSS4arrayRANDusedInd[minPifVAL];
        
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2MSS1array[pifTOTALS2arrayCount] = REFS2MSS1[i,j];
			pifTOTALS2MSS2array[pifTOTALS2arrayCount] = REFS2MSS2[i,j];
			pifTOTALS2MSS3array[pifTOTALS2arrayCount] = REFS2MSS3[i,j];
			pifTOTALS2MSS4array[pifTOTALS2arrayCount] = REFS2MSS4[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS2MSS1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2MSS1 == 0 )
            {
                numeric randomValueTOTALS2MSS1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2MSS1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2MSS1 = pifTOTALS2MSS1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2MSS1 == randomValueTOTALS2MSS1 )
                    {
                        validTOTALS2MSS1 = 0;
                    }
                }
            }
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[randomValueTOTALS2MSS1];
        pifTOTALS2MSS1arrayRANDusedInd[i] = randomValueTOTALS2MSS1;
        validTOTALS2MSS1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS2MSS2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2MSS2 == 0 )
            {
                numeric randomValueTOTALS2MSS2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2MSS2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2MSS2 = pifTOTALS2MSS2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2MSS2 == randomValueTOTALS2MSS2 )
                    {
                        validTOTALS2MSS2 = 0;
                    }
                }
            }
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[randomValueTOTALS2MSS2];
        pifTOTALS2MSS2arrayRANDusedInd[i] = randomValueTOTALS2MSS2;
        validTOTALS2MSS2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS2MSS3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2MSS3 == 0 )
            {
                numeric randomValueTOTALS2MSS3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2MSS3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2MSS3 = pifTOTALS2MSS3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2MSS3 == randomValueTOTALS2MSS3 )
                    {
                        validTOTALS2MSS3 = 0;
                    }
                }
            }
        pifTOTALS2MSS3arrayRAND[i] = pifTOTALS2MSS3array[randomValueTOTALS2MSS3];
        pifTOTALS2MSS3arrayRANDusedInd[i] = randomValueTOTALS2MSS3;
        validTOTALS2MSS3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS2MSS4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS2MSS4 == 0 )
            {
                numeric randomValueTOTALS2MSS4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS2MSS4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS2MSS4 = pifTOTALS2MSS4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS2MSS4 == randomValueTOTALS2MSS4 )
                    {
                        validTOTALS2MSS4 = 0;
                    }
                }
            }
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[randomValueTOTALS2MSS4];
        pifTOTALS2MSS4arrayRANDusedInd[i] = randomValueTOTALS2MSS4;
        validTOTALS2MSS4 = 0;
        }
    }

    if ( sensors1 == 7 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1ETM1array[countPIFtotal];
    array pifTOTALS1ETM2array[countPIFtotal];
    array pifTOTALS1ETM3array[countPIFtotal];
    array pifTOTALS1ETM4array[countPIFtotal];
    array pifTOTALS1ETM5array[countPIFtotal];
    array pifTOTALS1ETM7array[countPIFtotal];
    
    array pifTOTALS1ETM1arrayRAND[minPifVAL];
    array pifTOTALS1ETM2arrayRAND[minPifVAL];
    array pifTOTALS1ETM3arrayRAND[minPifVAL];
    array pifTOTALS1ETM4arrayRAND[minPifVAL];
    array pifTOTALS1ETM5arrayRAND[minPifVAL];
    array pifTOTALS1ETM7arrayRAND[minPifVAL];
        
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1ETM1array[pifTOTALS1arrayCount] = REFS1ETM1[i,j];
			pifTOTALS1ETM2array[pifTOTALS1arrayCount] = REFS1ETM2[i,j];
			pifTOTALS1ETM3array[pifTOTALS1arrayCount] = REFS1ETM3[i,j];
			pifTOTALS1ETM4array[pifTOTALS1arrayCount] = REFS1ETM4[i,j];
                  pifTOTALS1ETM5array[pifTOTALS1arrayCount] = REFS1ETM5[i,j];
			pifTOTALS1ETM7array[pifTOTALS1arrayCount] = REFS1ETM7[i,j];
			pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    if ( sensors2 == 7 )
    {     
        for i = 1 to minPifVAL
        {
              pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {
	  for i = 1 to minPifVAL
        {
              pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }
    }
    
    ############# band 2
    if ( sensors2 == 7 )
    { 
        for i = 1 to minPifVAL
        {
              pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
    }
    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
              pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }
    }
    
    ############## band 3
    if ( sensors2 == 7 )
    {            
        for i = 1 to minPifVAL
        {
               pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
    }
    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }
    }

    ################ band 4

    if ( sensors2 == 7 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }
    }

    ############## band 5

    if ( sensors2 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        	   pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        }
    }

    ################ band 7

    if ( sensors2 == 7 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }
    }

   }
    
    else if ( sensors1 == 6 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1TM1array[countPIFtotal];
    array pifTOTALS1TM2array[countPIFtotal];
    array pifTOTALS1TM3array[countPIFtotal];
    array pifTOTALS1TM4array[countPIFtotal];
    array pifTOTALS1TM5array[countPIFtotal];
    array pifTOTALS1TM7array[countPIFtotal];
    
    array pifTOTALS1TM1arrayRAND[minPifVAL];
    array pifTOTALS1TM2arrayRAND[minPifVAL];
    array pifTOTALS1TM3arrayRAND[minPifVAL];
    array pifTOTALS1TM4arrayRAND[minPifVAL];
    array pifTOTALS1TM5arrayRAND[minPifVAL];
    array pifTOTALS1TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1TM1array[pifTOTALS1arrayCount] = REFS1TM1[i,j];
			pifTOTALS1TM2array[pifTOTALS1arrayCount] = REFS1TM2[i,j];
			pifTOTALS1TM3array[pifTOTALS1arrayCount] = REFS1TM3[i,j];
			pifTOTALS1TM4array[pifTOTALS1arrayCount] = REFS1TM4[i,j];
                  pifTOTALS1TM5array[pifTOTALS1arrayCount] = REFS1TM5[i,j];
			pifTOTALS1TM7array[pifTOTALS1arrayCount] = REFS1TM7[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1

    if ( sensors2 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }
    }

    ############# band 2
    
    if ( sensors2 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
    }

    else if ( sensors2 == 6 )
    {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }  
    }

    ############## band 3
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }  
	  }

    ############## band 5
    
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        }   
	}

    ################ band 7
        
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }    
	  }
    }
    
    else
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1MSS1array[countPIFtotal];
    array pifTOTALS1MSS2array[countPIFtotal];
    array pifTOTALS1MSS3array[countPIFtotal];
    array pifTOTALS1MSS4array[countPIFtotal];
    
    array pifTOTALS1MSS1arrayRAND[minPifVAL];
    array pifTOTALS1MSS2arrayRAND[minPifVAL];
    array pifTOTALS1MSS3arrayRAND[minPifVAL];
    array pifTOTALS1MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1MSS1array[pifTOTALS1arrayCount] = REFS1MSS1[i,j];
			pifTOTALS1MSS2array[pifTOTALS1arrayCount] = REFS1MSS2[i,j];
			pifTOTALS1MSS3array[pifTOTALS1arrayCount] = REFS1MSS3[i,j];
			pifTOTALS1MSS4array[pifTOTALS1arrayCount] = REFS1MSS4[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS2MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS2MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors2 == 5 or sensors2 == 4 or sensors2 == 3 or sensors2 == 2 or sensors2 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS3arrayRAND[i] = pifTOTALS1MSS3array[pifTOTALS2MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
    
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS2MSS4arrayRANDusedInd[i]];
        }
	}    
    }

    if ( sensors3 == 7 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3ETM1array[countPIFtotal];
    array pifTOTALS3ETM2array[countPIFtotal];
    array pifTOTALS3ETM3array[countPIFtotal];
    array pifTOTALS3ETM4array[countPIFtotal];
    array pifTOTALS3ETM5array[countPIFtotal];
    array pifTOTALS3ETM7array[countPIFtotal];
    
    array pifTOTALS3ETM1arrayRAND[minPifVAL];
    array pifTOTALS3ETM2arrayRAND[minPifVAL];
    array pifTOTALS3ETM3arrayRAND[minPifVAL];
    array pifTOTALS3ETM4arrayRAND[minPifVAL];
    array pifTOTALS3ETM5arrayRAND[minPifVAL];
    array pifTOTALS3ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3ETM1array[pifTOTALS3arrayCount] = REFS3ETM1[i,j];
			pifTOTALS3ETM2array[pifTOTALS3arrayCount] = REFS3ETM2[i,j];
			pifTOTALS3ETM3array[pifTOTALS3arrayCount] = REFS3ETM3[i,j];
			pifTOTALS3ETM4array[pifTOTALS3arrayCount] = REFS3ETM4[i,j];
                  pifTOTALS3ETM5array[pifTOTALS3arrayCount] = REFS3ETM5[i,j];
			pifTOTALS3ETM7array[pifTOTALS3arrayCount] = REFS3ETM7[i,j];
			pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

	else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2== 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }   
	}
    }
    
    else if ( sensors3 == 6 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3TM1array[countPIFtotal];
    array pifTOTALS3TM2array[countPIFtotal];
    array pifTOTALS3TM3array[countPIFtotal];
    array pifTOTALS3TM4array[countPIFtotal];
    array pifTOTALS3TM5array[countPIFtotal];
    array pifTOTALS3TM7array[countPIFtotal];
    
    array pifTOTALS3TM1arrayRAND[minPifVAL];
    array pifTOTALS3TM2arrayRAND[minPifVAL];
    array pifTOTALS3TM3arrayRAND[minPifVAL];
    array pifTOTALS3TM4arrayRAND[minPifVAL];
    array pifTOTALS3TM5arrayRAND[minPifVAL];
    array pifTOTALS3TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3TM1array[pifTOTALS3arrayCount] = REFS3TM1[i,j];
			pifTOTALS3TM2array[pifTOTALS3arrayCount] = REFS3TM2[i,j];
			pifTOTALS3TM3array[pifTOTALS3arrayCount] = REFS3TM3[i,j];
			pifTOTALS3TM4array[pifTOTALS3arrayCount] = REFS3TM4[i,j];
                  pifTOTALS3TM5array[pifTOTALS3arrayCount] = REFS3TM5[i,j];
			pifTOTALS3TM7array[pifTOTALS3arrayCount] = REFS3TM7[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }   
	}

    ############## band 5
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3MSS1array[countPIFtotal];
    array pifTOTALS3MSS2array[countPIFtotal];
    array pifTOTALS3MSS3array[countPIFtotal];
    array pifTOTALS3MSS4array[countPIFtotal];
    
    array pifTOTALS3MSS1arrayRAND[minPifVAL];
    array pifTOTALS3MSS2arrayRAND[minPifVAL];
    array pifTOTALS3MSS3arrayRAND[minPifVAL];
    array pifTOTALS3MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3MSS1array[pifTOTALS3arrayCount] = REFS3MSS1[i,j];
			pifTOTALS3MSS2array[pifTOTALS3arrayCount] = REFS3MSS2[i,j];
			pifTOTALS3MSS3array[pifTOTALS3arrayCount] = REFS3MSS3[i,j];
			pifTOTALS3MSS4array[pifTOTALS3arrayCount] = REFS3MSS4[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS2MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS2MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors2 == 5 or sensors2 == 4 or sensors2 == 3 or sensors2 == 2 or sensors2 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS3arrayRAND[i] = pifTOTALS3MSS3array[pifTOTALS2MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }    
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS2MSS4arrayRANDusedInd[i]];
        }
	}
    }

    if ( sensors4 == 7 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4ETM1array[countPIFtotal];
    array pifTOTALS4ETM2array[countPIFtotal];
    array pifTOTALS4ETM3array[countPIFtotal];
    array pifTOTALS4ETM4array[countPIFtotal];
    array pifTOTALS4ETM5array[countPIFtotal];
    array pifTOTALS4ETM7array[countPIFtotal];
    
    array pifTOTALS4ETM1arrayRAND[minPifVAL];
    array pifTOTALS4ETM2arrayRAND[minPifVAL];
    array pifTOTALS4ETM3arrayRAND[minPifVAL];
    array pifTOTALS4ETM4arrayRAND[minPifVAL];
    array pifTOTALS4ETM5arrayRAND[minPifVAL];
    array pifTOTALS4ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4ETM1array[pifTOTALS4arrayCount] = REFS4ETM1[i,j];
			pifTOTALS4ETM2array[pifTOTALS4arrayCount] = REFS4ETM2[i,j];
			pifTOTALS4ETM3array[pifTOTALS4arrayCount] = REFS4ETM3[i,j];
			pifTOTALS4ETM4array[pifTOTALS4arrayCount] = REFS4ETM4[i,j];
                  pifTOTALS4ETM5array[pifTOTALS4arrayCount] = REFS4ETM5[i,j];
			pifTOTALS4ETM7array[pifTOTALS4arrayCount] = REFS4ETM7[i,j];
			pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }
	}

    ############## band 3
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }  
	  }

    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }    
	  }

    ############## band 5
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {   
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        } 
	}
    
    ################ band 7
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
	  }

	  else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else if ( sensors4 == 6 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4TM1array[countPIFtotal];
    array pifTOTALS4TM2array[countPIFtotal];
    array pifTOTALS4TM3array[countPIFtotal];
    array pifTOTALS4TM4array[countPIFtotal];
    array pifTOTALS4TM5array[countPIFtotal];
    array pifTOTALS4TM7array[countPIFtotal];
    
    array pifTOTALS4TM1arrayRAND[minPifVAL];
    array pifTOTALS4TM2arrayRAND[minPifVAL];
    array pifTOTALS4TM3arrayRAND[minPifVAL];
    array pifTOTALS4TM4arrayRAND[minPifVAL];
    array pifTOTALS4TM5arrayRAND[minPifVAL];
    array pifTOTALS4TM7arrayRAND[minPifVAL];
        
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4TM1array[pifTOTALS4arrayCount] = REFS4TM1[i,j];
			pifTOTALS4TM2array[pifTOTALS4arrayCount] = REFS4TM2[i,j];
			pifTOTALS4TM3array[pifTOTALS4arrayCount] = REFS4TM3[i,j];
			pifTOTALS4TM4array[pifTOTALS4arrayCount] = REFS4TM4[i,j];
                  pifTOTALS4TM5array[pifTOTALS4arrayCount] = REFS4TM5[i,j];
			pifTOTALS4TM7array[pifTOTALS4arrayCount] = REFS4TM7[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS2ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS2TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }    
	}

    ############## band 3
	
      if ( sensors2 == 7 )
      {         
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors2 == 7 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }    
	  }

    ############## band 5
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS2ETM5arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS2TM5arrayRANDusedInd[i]];
        }  
	  }

    ################ band 7
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS2ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS2TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4MSS1array[countPIFtotal];
    array pifTOTALS4MSS2array[countPIFtotal];
    array pifTOTALS4MSS3array[countPIFtotal];
    array pifTOTALS4MSS4array[countPIFtotal];
    
    array pifTOTALS4MSS1arrayRAND[minPifVAL];
    array pifTOTALS4MSS2arrayRAND[minPifVAL];
    array pifTOTALS4MSS3arrayRAND[minPifVAL];
    array pifTOTALS4MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4MSS1array[pifTOTALS4arrayCount] = REFS4MSS1[i,j];
			pifTOTALS4MSS2array[pifTOTALS4arrayCount] = REFS4MSS2[i,j];
			pifTOTALS4MSS3array[pifTOTALS4arrayCount] = REFS4MSS3[i,j];
			pifTOTALS4MSS4array[pifTOTALS4arrayCount] = REFS4MSS4[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS2ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS2TM2arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS2MSS1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS2ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors2 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS2TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS2MSS2arrayRANDusedInd[i]];
        }   
	}
    
    ############## band 3
     
      if ( sensors2 == 5 or sensors2 == 4 or sensors2 == 3 or sensors2 == 2 or sensors2 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS3arrayRAND[i] = pifTOTALS4MSS3array[pifTOTALS2MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors2 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS2ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors2 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS2TM4arrayRANDusedInd[i]];
        }    
	  }

	 else
	 {
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS2MSS4arrayRANDusedInd[i]];
        }
	  }
    }
  }
  else if ( slaveMaster == 3 )
  {
    if ( sensors3 == 7 )
    {
    numeric pifTOTALS3arrayCount = 1;
    
    array pifTOTALS3ETM1array[countPIFtotal];
    array pifTOTALS3ETM2array[countPIFtotal];
    array pifTOTALS3ETM3array[countPIFtotal];
    array pifTOTALS3ETM4array[countPIFtotal];
    array pifTOTALS3ETM5array[countPIFtotal];
    array pifTOTALS3ETM7array[countPIFtotal];
    
    array pifTOTALS3ETM1arrayRAND[minPifVAL];
    array pifTOTALS3ETM2arrayRAND[minPifVAL];
    array pifTOTALS3ETM3arrayRAND[minPifVAL];
    array pifTOTALS3ETM4arrayRAND[minPifVAL];
    array pifTOTALS3ETM5arrayRAND[minPifVAL];
    array pifTOTALS3ETM7arrayRAND[minPifVAL];
    
    array pifTOTALS3ETM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS3ETM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS3ETM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS3ETM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS3ETM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS3ETM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                       pifTOTALS3ETM1array[pifTOTALS3arrayCount] = REFS3ETM1[i,j];
			     pifTOTALS3ETM2array[pifTOTALS3arrayCount] = REFS3ETM2[i,j];
			     pifTOTALS3ETM3array[pifTOTALS3arrayCount] = REFS3ETM3[i,j];
			     pifTOTALS3ETM4array[pifTOTALS3arrayCount] = REFS3ETM4[i,j];
                       pifTOTALS3ETM5array[pifTOTALS3arrayCount] = REFS3ETM5[i,j];
			     pifTOTALS3ETM7array[pifTOTALS3arrayCount] = REFS3ETM7[i,j];
			     pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS3ETM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM1 == 0 )
            {
                numeric randomValueTOTALS3ETM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM1 = pifTOTALS3ETM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM1 == randomValueTOTALS3ETM1 )
                    {
                        validTOTALS3ETM1 = 0;
                    }
                }
            }
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[randomValueTOTALS3ETM1];
        pifTOTALS3ETM1arrayRANDusedInd[i] = randomValueTOTALS3ETM1;
        validTOTALS3ETM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS3ETM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM2 == 0 )
            {
                numeric randomValueTOTALS3ETM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM2 = pifTOTALS3ETM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM2 == randomValueTOTALS3ETM2 )
                    {
                        validTOTALS3ETM2 = 0;
                    }
                }
            }
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[randomValueTOTALS3ETM2];
        pifTOTALS3ETM2arrayRANDusedInd[i] = randomValueTOTALS3ETM2;
        validTOTALS3ETM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS3ETM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM3 == 0 )
            {
                numeric randomValueTOTALS3ETM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM3 = pifTOTALS3ETM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM3 == randomValueTOTALS3ETM3 )
                    {
                        validTOTALS3ETM3 = 0;
                    }
                }
            }
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[randomValueTOTALS3ETM3];
        pifTOTALS3ETM3arrayRANDusedInd[i] = randomValueTOTALS3ETM3;
        validTOTALS3ETM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS3ETM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM4 == 0 )
            {
                numeric randomValueTOTALS3ETM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM4 = pifTOTALS3ETM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM4 == randomValueTOTALS3ETM4 )
                    {
                        validTOTALS3ETM4 = 0;
                    }
                }
            }
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[randomValueTOTALS3ETM4];
        pifTOTALS3ETM4arrayRANDusedInd[i] = randomValueTOTALS3ETM4;
        validTOTALS3ETM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS3ETM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM5 == 0 )
            {
                numeric randomValueTOTALS3ETM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM5 = pifTOTALS3ETM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM5 == randomValueTOTALS3ETM5 )
                    {
                        validTOTALS3ETM5 = 0;
                    }
                }
            }
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[randomValueTOTALS3ETM5];
        pifTOTALS3ETM5arrayRANDusedInd[i] = randomValueTOTALS3ETM5;
        validTOTALS3ETM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS3ETM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3ETM7 == 0 )
            {
                numeric randomValueTOTALS3ETM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3ETM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3ETM7 = pifTOTALS3ETM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3ETM7 == randomValueTOTALS3ETM7 )
                    {
                        validTOTALS3ETM7 = 0;
                    }
                }
            }
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[randomValueTOTALS3ETM7];
        pifTOTALS3ETM7arrayRANDusedInd[i] = randomValueTOTALS3ETM7;
        validTOTALS3ETM7 = 0;
        }
    }
    
    else if ( sensors3 == 6 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3TM1array[countPIFtotal];
    array pifTOTALS3TM2array[countPIFtotal];
    array pifTOTALS3TM3array[countPIFtotal];
    array pifTOTALS3TM4array[countPIFtotal];
    array pifTOTALS3TM5array[countPIFtotal];
    array pifTOTALS3TM7array[countPIFtotal];
    
    array pifTOTALS3TM1arrayRAND[minPifVAL];
    array pifTOTALS3TM2arrayRAND[minPifVAL];
    array pifTOTALS3TM3arrayRAND[minPifVAL];
    array pifTOTALS3TM4arrayRAND[minPifVAL];
    array pifTOTALS3TM5arrayRAND[minPifVAL];
    array pifTOTALS3TM7arrayRAND[minPifVAL];
    
    array pifTOTALS3TM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS3TM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS3TM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS3TM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS3TM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS3TM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                        pifTOTALS3TM1array[pifTOTALS3arrayCount] = REFS3TM1[i,j];
			      pifTOTALS3TM2array[pifTOTALS3arrayCount] = REFS3TM2[i,j];
			      pifTOTALS3TM3array[pifTOTALS3arrayCount] = REFS3TM3[i,j];
			      pifTOTALS3TM4array[pifTOTALS3arrayCount] = REFS3TM4[i,j];
                        pifTOTALS3TM5array[pifTOTALS3arrayCount] = REFS3TM5[i,j];
			      pifTOTALS3TM7array[pifTOTALS3arrayCount] = REFS3TM7[i,j];
                        pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
        
    ############# band 1
    
        numeric validTOTALS3TM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM1 == 0 )
            {
                numeric randomValueTOTALS3TM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM1 = pifTOTALS3TM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM1 == randomValueTOTALS3TM1 )
                    {
                        validTOTALS3TM1 = 0;
                    }
                }
            }
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[randomValueTOTALS3TM1];
        pifTOTALS3TM1arrayRANDusedInd[i] = randomValueTOTALS3TM1;
    validTOTALS3TM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS3TM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM2 == 0 )
            {
                numeric randomValueTOTALS3TM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM2 = pifTOTALS3TM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM2 == randomValueTOTALS3TM2 )
                    {
                        validTOTALS3TM2 = 0;
                    }
                }
            }
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[randomValueTOTALS3TM2];
        pifTOTALS3TM2arrayRANDusedInd[i] = randomValueTOTALS3TM2;
        validTOTALS3TM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS3TM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM3 == 0 )
            {
                numeric randomValueTOTALS3TM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM3 = pifTOTALS3TM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM3 == randomValueTOTALS3TM3 )
                    {
                        validTOTALS3TM3 = 0;
                    }
                }
            }
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[randomValueTOTALS3TM3];
        pifTOTALS3TM3arrayRANDusedInd[i] = randomValueTOTALS3TM3;
        validTOTALS3TM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS3TM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM4 == 0 )
            {
                numeric randomValueTOTALS3TM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM4 = pifTOTALS3TM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM4 == randomValueTOTALS3TM4 )
                    {
                        validTOTALS3TM4 = 0;
                    }
                }
            }
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[randomValueTOTALS3TM4];
        pifTOTALS3TM4arrayRANDusedInd[i] = randomValueTOTALS3TM4;
        validTOTALS3TM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS3TM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM5 == 0 )
            {
                numeric randomValueTOTALS3TM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM5 = pifTOTALS3TM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM5 == randomValueTOTALS3TM5 )
                    {
                        validTOTALS3TM5 = 0;
                    }
                }
            }
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[randomValueTOTALS3TM5];
        pifTOTALS3TM5arrayRANDusedInd[i] = randomValueTOTALS3TM5;
        validTOTALS3TM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS3TM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3TM7 == 0 )
            {
                numeric randomValueTOTALS3TM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3TM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3TM7 = pifTOTALS3TM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3TM7 == randomValueTOTALS3TM7 )
                    {
                        validTOTALS3TM7 = 0;
                    }
                }
            }
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[randomValueTOTALS3TM7];
        pifTOTALS3TM7arrayRANDusedInd[i] = randomValueTOTALS3TM7;
        validTOTALS3TM7 = 0;
        }
    }
    
    else
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3MSS1array[countPIFtotal];
    array pifTOTALS3MSS2array[countPIFtotal];
    array pifTOTALS3MSS3array[countPIFtotal];
    array pifTOTALS3MSS4array[countPIFtotal];
    
    array pifTOTALS3MSS1arrayRAND[minPifVAL];
    array pifTOTALS3MSS2arrayRAND[minPifVAL];
    array pifTOTALS3MSS3arrayRAND[minPifVAL];
    array pifTOTALS3MSS4arrayRAND[minPifVAL];
    
    array pifTOTALS3MSS1arrayRANDusedInd[minPifVAL];
    array pifTOTALS3MSS2arrayRANDusedInd[minPifVAL];
    array pifTOTALS3MSS3arrayRANDusedInd[minPifVAL];
    array pifTOTALS3MSS4arrayRANDusedInd[minPifVAL];
        
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3MSS1array[pifTOTALS3arrayCount] = REFS3MSS1[i,j];
			pifTOTALS3MSS2array[pifTOTALS3arrayCount] = REFS3MSS2[i,j];
			pifTOTALS3MSS3array[pifTOTALS3arrayCount] = REFS3MSS3[i,j];
			pifTOTALS3MSS4array[pifTOTALS3arrayCount] = REFS3MSS4[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS3MSS1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3MSS1 == 0 )
            {
                numeric randomValueTOTALS3MSS1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3MSS1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3MSS1 = pifTOTALS3MSS1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3MSS1 == randomValueTOTALS3MSS1 )
                    {
                        validTOTALS3MSS1 = 0;
                    }
                }
            }
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[randomValueTOTALS3MSS1];
        pifTOTALS3MSS1arrayRANDusedInd[i] = randomValueTOTALS3MSS1;
        validTOTALS3MSS1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS3MSS2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3MSS2 == 0 )
            {
                numeric randomValueTOTALS3MSS2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3MSS2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3MSS2 = pifTOTALS3MSS2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3MSS2 == randomValueTOTALS3MSS2 )
                    {
                        validTOTALS3MSS2 = 0;
                    }
                }
            }
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[randomValueTOTALS3MSS2];
        pifTOTALS3MSS2arrayRANDusedInd[i] = randomValueTOTALS3MSS2;
        validTOTALS3MSS2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS3MSS3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3MSS3 == 0 )
            {
                numeric randomValueTOTALS3MSS3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3MSS3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3MSS3 = pifTOTALS3MSS3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3MSS3 == randomValueTOTALS3MSS3 )
                    {
                        validTOTALS3MSS3 = 0;
                    }
                }
            }
        pifTOTALS3MSS3arrayRAND[i] = pifTOTALS3MSS3array[randomValueTOTALS3MSS3];
        pifTOTALS3MSS3arrayRANDusedInd[i] = randomValueTOTALS3MSS3;
        validTOTALS3MSS3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS3MSS4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS3MSS4 == 0 )
            {
                numeric randomValueTOTALS3MSS4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS3MSS4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS3MSS4 = pifTOTALS3MSS4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS3MSS4 == randomValueTOTALS3MSS4 )
                    {
                        validTOTALS3MSS4 = 0;
                    }
                }
            }
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[randomValueTOTALS3MSS4];
        pifTOTALS3MSS4arrayRANDusedInd[i] = randomValueTOTALS3MSS4;
        validTOTALS3MSS4 = 0;
        }
    }

    if ( sensors2 == 7 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2ETM1array[countPIFtotal];
    array pifTOTALS2ETM2array[countPIFtotal];
    array pifTOTALS2ETM3array[countPIFtotal];
    array pifTOTALS2ETM4array[countPIFtotal];
    array pifTOTALS2ETM5array[countPIFtotal];
    array pifTOTALS2ETM7array[countPIFtotal];
    
    array pifTOTALS2ETM1arrayRAND[minPifVAL];
    array pifTOTALS2ETM2arrayRAND[minPifVAL];
    array pifTOTALS2ETM3arrayRAND[minPifVAL];
    array pifTOTALS2ETM4arrayRAND[minPifVAL];
    array pifTOTALS2ETM5arrayRAND[minPifVAL];
    array pifTOTALS2ETM7arrayRAND[minPifVAL];
        
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2ETM1array[pifTOTALS2arrayCount] = REFS2ETM1[i,j];
			pifTOTALS2ETM2array[pifTOTALS2arrayCount] = REFS2ETM2[i,j];
			pifTOTALS2ETM3array[pifTOTALS2arrayCount] = REFS2ETM3[i,j];
			pifTOTALS2ETM4array[pifTOTALS2arrayCount] = REFS2ETM4[i,j];
                  pifTOTALS2ETM5array[pifTOTALS2arrayCount] = REFS2ETM5[i,j];
			pifTOTALS2ETM7array[pifTOTALS2arrayCount] = REFS2ETM7[i,j];
			pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    if ( sensors3 == 7 )
    {     
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {
	  for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }
    }
    
    ############# band 2
    if ( sensors3 == 7 )
    { 
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
    }
    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }
    }
    
    ############## band 3
    if ( sensors3 == 7 )
    {            
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
    }
    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }
    }

    ################ band 4

    if ( sensors3 == 7 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }
    }

    ############## band 5

    if ( sensors3 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        }
    }

    ################ band 7

    if ( sensors3 == 7 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }
    }

   }
    
    else if ( sensors2 == 6 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2TM1array[countPIFtotal];
    array pifTOTALS2TM2array[countPIFtotal];
    array pifTOTALS2TM3array[countPIFtotal];
    array pifTOTALS2TM4array[countPIFtotal];
    array pifTOTALS2TM5array[countPIFtotal];
    array pifTOTALS2TM7array[countPIFtotal];
    
    array pifTOTALS2TM1arrayRAND[minPifVAL];
    array pifTOTALS2TM2arrayRAND[minPifVAL];
    array pifTOTALS2TM3arrayRAND[minPifVAL];
    array pifTOTALS2TM4arrayRAND[minPifVAL];
    array pifTOTALS2TM5arrayRAND[minPifVAL];
    array pifTOTALS2TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2TM1array[pifTOTALS2arrayCount] = REFS2TM1[i,j];
			pifTOTALS2TM2array[pifTOTALS2arrayCount] = REFS2TM2[i,j];
			pifTOTALS2TM3array[pifTOTALS2arrayCount] = REFS2TM3[i,j];
			pifTOTALS2TM4array[pifTOTALS2arrayCount] = REFS2TM4[i,j];
                  pifTOTALS2TM5array[pifTOTALS2arrayCount] = REFS2TM5[i,j];
			pifTOTALS2TM7array[pifTOTALS2arrayCount] = REFS2TM7[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1

    if ( sensors3 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }
    }

    ############# band 2
    
    if ( sensors3 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
    }

    else if ( sensors3 == 6 )
    {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }  
    }

    ############## band 3
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
    
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        }   
	}

    ################ band 7
        
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }    
	}
    }
    
    else
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2MSS1array[countPIFtotal];
    array pifTOTALS2MSS2array[countPIFtotal];
    array pifTOTALS2MSS3array[countPIFtotal];
    array pifTOTALS2MSS4array[countPIFtotal];
    
    array pifTOTALS2MSS1arrayRAND[minPifVAL];
    array pifTOTALS2MSS2arrayRAND[minPifVAL];
    array pifTOTALS2MSS3arrayRAND[minPifVAL];
    array pifTOTALS2MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2MSS1array[pifTOTALS2arrayCount] = REFS2MSS1[i,j];
			pifTOTALS2MSS2array[pifTOTALS2arrayCount] = REFS2MSS2[i,j];
			pifTOTALS2MSS3array[pifTOTALS2arrayCount] = REFS2MSS3[i,j];
			pifTOTALS2MSS4array[pifTOTALS2arrayCount] = REFS2MSS4[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS3MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS3MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors3 == 5 or sensors3 == 4 or sensors3 == 3 or sensors3 == 2 or sensors3 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS3arrayRAND[i] = pifTOTALS2MSS3array[pifTOTALS3MSS3arrayRANDusedInd[i]];
        }
	}
    
    ################ band 4
    
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS3MSS4arrayRANDusedInd[i]];
        }
	}    
    }

    if ( sensors1 == 7 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1ETM1array[countPIFtotal];
    array pifTOTALS1ETM2array[countPIFtotal];
    array pifTOTALS1ETM3array[countPIFtotal];
    array pifTOTALS1ETM4array[countPIFtotal];
    array pifTOTALS1ETM5array[countPIFtotal];
    array pifTOTALS1ETM7array[countPIFtotal];
    
    array pifTOTALS1ETM1arrayRAND[minPifVAL];
    array pifTOTALS1ETM2arrayRAND[minPifVAL];
    array pifTOTALS1ETM3arrayRAND[minPifVAL];
    array pifTOTALS1ETM4arrayRAND[minPifVAL];
    array pifTOTALS1ETM5arrayRAND[minPifVAL];
    array pifTOTALS1ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1ETM1array[pifTOTALS1arrayCount] = REFS1ETM1[i,j];
			pifTOTALS1ETM2array[pifTOTALS1arrayCount] = REFS1ETM2[i,j];
			pifTOTALS1ETM3array[pifTOTALS1arrayCount] = REFS1ETM3[i,j];
			pifTOTALS1ETM4array[pifTOTALS1arrayCount] = REFS1ETM4[i,j];
                  pifTOTALS1ETM5array[pifTOTALS1arrayCount] = REFS1ETM5[i,j];
			pifTOTALS1ETM7array[pifTOTALS1arrayCount] = REFS1ETM7[i,j];
			pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

	else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else if ( sensors1 == 6 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1TM1array[countPIFtotal];
    array pifTOTALS1TM2array[countPIFtotal];
    array pifTOTALS1TM3array[countPIFtotal];
    array pifTOTALS1TM4array[countPIFtotal];
    array pifTOTALS1TM5array[countPIFtotal];
    array pifTOTALS1TM7array[countPIFtotal];
    
    array pifTOTALS1TM1arrayRAND[minPifVAL];
    array pifTOTALS1TM2arrayRAND[minPifVAL];
    array pifTOTALS1TM3arrayRAND[minPifVAL];
    array pifTOTALS1TM4arrayRAND[minPifVAL];
    array pifTOTALS1TM5arrayRAND[minPifVAL];
    array pifTOTALS1TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1TM1array[pifTOTALS1arrayCount] = REFS1TM1[i,j];
			pifTOTALS1TM2array[pifTOTALS1arrayCount] = REFS1TM2[i,j];
			pifTOTALS1TM3array[pifTOTALS1arrayCount] = REFS1TM3[i,j];
			pifTOTALS1TM4array[pifTOTALS1arrayCount] = REFS1TM4[i,j];
                  pifTOTALS1TM5array[pifTOTALS1arrayCount] = REFS1TM5[i,j];
			pifTOTALS1TM7array[pifTOTALS1arrayCount] = REFS1TM7[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM1arrayRAND[i] = pifTOTALS1TM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM1arrayRAND[i] = pifTOTALS1TM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM2arrayRAND[i] = pifTOTALS1TM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM2arrayRAND[i] = pifTOTALS1TM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }   
	}

    ############## band 5
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1MSS1array[countPIFtotal];
    array pifTOTALS1MSS2array[countPIFtotal];
    array pifTOTALS1MSS3array[countPIFtotal];
    array pifTOTALS1MSS4array[countPIFtotal];
    
    array pifTOTALS1MSS1arrayRAND[minPifVAL];
    array pifTOTALS1MSS2arrayRAND[minPifVAL];
    array pifTOTALS1MSS3arrayRAND[minPifVAL];
    array pifTOTALS1MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1MSS1array[pifTOTALS1arrayCount] = REFS1MSS1[i,j];
			pifTOTALS1MSS2array[pifTOTALS1arrayCount] = REFS1MSS2[i,j];
			pifTOTALS1MSS3array[pifTOTALS1arrayCount] = REFS1MSS3[i,j];
			pifTOTALS1MSS4array[pifTOTALS1arrayCount] = REFS1MSS4[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS3MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS3MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors3 == 5 or sensors3 == 4 or sensors3 == 3 or sensors3 == 2 or sensors3 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS3arrayRAND[i] = pifTOTALS1MSS3array[pifTOTALS3MSS3arrayRANDusedInd[i]];
        }
	 }
    
    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }    
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS3MSS4arrayRANDusedInd[i]];
        }
	}
    }

    if ( sensors4 == 7 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4ETM1array[countPIFtotal];
    array pifTOTALS4ETM2array[countPIFtotal];
    array pifTOTALS4ETM3array[countPIFtotal];
    array pifTOTALS4ETM4array[countPIFtotal];
    array pifTOTALS4ETM5array[countPIFtotal];
    array pifTOTALS4ETM7array[countPIFtotal];
    
    array pifTOTALS4ETM1arrayRAND[minPifVAL];
    array pifTOTALS4ETM2arrayRAND[minPifVAL];
    array pifTOTALS4ETM3arrayRAND[minPifVAL];
    array pifTOTALS4ETM4arrayRAND[minPifVAL];
    array pifTOTALS4ETM5arrayRAND[minPifVAL];
    array pifTOTALS4ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4ETM1array[pifTOTALS4arrayCount] = REFS4ETM1[i,j];
			pifTOTALS4ETM2array[pifTOTALS4arrayCount] = REFS4ETM2[i,j];
			pifTOTALS4ETM3array[pifTOTALS4arrayCount] = REFS4ETM3[i,j];
			pifTOTALS4ETM4array[pifTOTALS4arrayCount] = REFS4ETM4[i,j];
                  pifTOTALS4ETM5array[pifTOTALS4arrayCount] = REFS4ETM5[i,j];
			pifTOTALS4ETM7array[pifTOTALS4arrayCount] = REFS4ETM7[i,j];
			pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }
	}

    ############## band 3
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }  
	  }

    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      {   
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        } 
	  }
    
    ################ band 7
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
	  }

	  else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else if ( sensors4 == 6 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4TM1array[countPIFtotal];
    array pifTOTALS4TM2array[countPIFtotal];
    array pifTOTALS4TM3array[countPIFtotal];
    array pifTOTALS4TM4array[countPIFtotal];
    array pifTOTALS4TM5array[countPIFtotal];
    array pifTOTALS4TM7array[countPIFtotal];
    
    array pifTOTALS4TM1arrayRAND[minPifVAL];
    array pifTOTALS4TM2arrayRAND[minPifVAL];
    array pifTOTALS4TM3arrayRAND[minPifVAL];
    array pifTOTALS4TM4arrayRAND[minPifVAL];
    array pifTOTALS4TM5arrayRAND[minPifVAL];
    array pifTOTALS4TM7arrayRAND[minPifVAL];
        
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4TM1array[pifTOTALS4arrayCount] = REFS4TM1[i,j];
			pifTOTALS4TM2array[pifTOTALS4arrayCount] = REFS4TM2[i,j];
			pifTOTALS4TM3array[pifTOTALS4arrayCount] = REFS4TM3[i,j];
			pifTOTALS4TM4array[pifTOTALS4arrayCount] = REFS4TM4[i,j];
                  pifTOTALS4TM5array[pifTOTALS4arrayCount] = REFS4TM5[i,j];
			pifTOTALS4TM7array[pifTOTALS4arrayCount] = REFS4TM7[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS3ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[pifTOTALS3TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }    
	}

    ############## band 3
	
      if ( sensors3 == 7 )
      {         
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors3 == 7 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS3ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[pifTOTALS3TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS3ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[pifTOTALS3TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4MSS1array[countPIFtotal];
    array pifTOTALS4MSS2array[countPIFtotal];
    array pifTOTALS4MSS3array[countPIFtotal];
    array pifTOTALS4MSS4array[countPIFtotal];
    
    array pifTOTALS4MSS1arrayRAND[minPifVAL];
    array pifTOTALS4MSS2arrayRAND[minPifVAL];
    array pifTOTALS4MSS3arrayRAND[minPifVAL];
    array pifTOTALS4MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4MSS1array[pifTOTALS4arrayCount] = REFS4MSS1[i,j];
			pifTOTALS4MSS2array[pifTOTALS4arrayCount] = REFS4MSS2[i,j];
			pifTOTALS4MSS3array[pifTOTALS4arrayCount] = REFS4MSS3[i,j];
			pifTOTALS4MSS4array[pifTOTALS4arrayCount] = REFS4MSS4[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS3ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS3TM2arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS3MSS1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS3ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors3 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[pifTOTALS3TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[pifTOTALS3MSS2arrayRANDusedInd[i]];
        }   
	}
    
    ############## band 3
     
      if ( sensors3 == 5 or sensors3 == 4 or sensors3 == 3 or sensors3 == 2 or sensors3 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS3arrayRAND[i] = pifTOTALS4MSS3array[pifTOTALS3MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors3 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS3ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors3 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS3TM4arrayRANDusedInd[i]];
        }    
	  }

	 else
	 {
        for i = 1 to minPifVAL
        {
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[pifTOTALS3MSS4arrayRANDusedInd[i]];
        }
	 }
    }
  }

  else if ( slaveMaster == 4 )
  {
    if ( sensors4 == 7 )
    {
    numeric pifTOTALS4arrayCount = 1;
    
    array pifTOTALS4ETM1array[countPIFtotal];
    array pifTOTALS4ETM2array[countPIFtotal];
    array pifTOTALS4ETM3array[countPIFtotal];
    array pifTOTALS4ETM4array[countPIFtotal];
    array pifTOTALS4ETM5array[countPIFtotal];
    array pifTOTALS4ETM7array[countPIFtotal];
    
    array pifTOTALS4ETM1arrayRAND[minPifVAL];
    array pifTOTALS4ETM2arrayRAND[minPifVAL];
    array pifTOTALS4ETM3arrayRAND[minPifVAL];
    array pifTOTALS4ETM4arrayRAND[minPifVAL];
    array pifTOTALS4ETM5arrayRAND[minPifVAL];
    array pifTOTALS4ETM7arrayRAND[minPifVAL];
    
    array pifTOTALS4ETM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS4ETM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS4ETM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS4ETM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS4ETM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS4ETM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                       pifTOTALS4ETM1array[pifTOTALS4arrayCount] = REFS4ETM1[i,j];
			     pifTOTALS4ETM2array[pifTOTALS4arrayCount] = REFS4ETM2[i,j];
			     pifTOTALS4ETM3array[pifTOTALS4arrayCount] = REFS4ETM3[i,j];
			     pifTOTALS4ETM4array[pifTOTALS4arrayCount] = REFS4ETM4[i,j];
                       pifTOTALS4ETM5array[pifTOTALS4arrayCount] = REFS4ETM5[i,j];
			     pifTOTALS4ETM7array[pifTOTALS4arrayCount] = REFS4ETM7[i,j];
			     pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS4ETM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM1 == 0 )
            {
                numeric randomValueTOTALS4ETM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM1 = pifTOTALS4ETM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM1 == randomValueTOTALS4ETM1 )
                    {
                        validTOTALS4ETM1 = 0;
                    }
                }
            }
        pifTOTALS4ETM1arrayRAND[i] = pifTOTALS4ETM1array[randomValueTOTALS4ETM1];
        pifTOTALS4ETM1arrayRANDusedInd[i] = randomValueTOTALS4ETM1;
        validTOTALS4ETM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS4ETM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM2 == 0 )
            {
                numeric randomValueTOTALS4ETM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM2 = pifTOTALS4ETM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM2 == randomValueTOTALS4ETM2 )
                    {
                        validTOTALS4ETM2 = 0;
                    }
                }
            }
        pifTOTALS4ETM2arrayRAND[i] = pifTOTALS4ETM2array[randomValueTOTALS4ETM2];
        pifTOTALS4ETM2arrayRANDusedInd[i] = randomValueTOTALS4ETM2;
        validTOTALS4ETM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS4ETM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM3 == 0 )
            {
                numeric randomValueTOTALS4ETM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM3 = pifTOTALS4ETM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM3 == randomValueTOTALS4ETM3 )
                    {
                        validTOTALS4ETM3 = 0;
                    }
                }
            }
        pifTOTALS4ETM3arrayRAND[i] = pifTOTALS4ETM3array[randomValueTOTALS4ETM3];
        pifTOTALS4ETM3arrayRANDusedInd[i] = randomValueTOTALS4ETM3;
        validTOTALS4ETM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS4ETM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM4 == 0 )
            {
                numeric randomValueTOTALS4ETM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM4 = pifTOTALS4ETM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM4 == randomValueTOTALS4ETM4 )
                    {
                        validTOTALS4ETM4 = 0;
                    }
                }
            }
        pifTOTALS4ETM4arrayRAND[i] = pifTOTALS4ETM4array[randomValueTOTALS4ETM4];
        pifTOTALS4ETM4arrayRANDusedInd[i] = randomValueTOTALS4ETM4;
        validTOTALS4ETM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS4ETM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM5 == 0 )
            {
                numeric randomValueTOTALS4ETM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM5 = pifTOTALS4ETM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM5 == randomValueTOTALS4ETM5 )
                    {
                        validTOTALS4ETM5 = 0;
                    }
                }
            }
        pifTOTALS4ETM5arrayRAND[i] = pifTOTALS4ETM5array[randomValueTOTALS4ETM5];
        pifTOTALS4ETM5arrayRANDusedInd[i] = randomValueTOTALS4ETM5;
        validTOTALS4ETM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS4ETM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4ETM7 == 0 )
            {
                numeric randomValueTOTALS4ETM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4ETM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4ETM7 = pifTOTALS4ETM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4ETM7 == randomValueTOTALS4ETM7 )
                    {
                        validTOTALS4ETM7 = 0;
                    }
                }
            }
        pifTOTALS4ETM7arrayRAND[i] = pifTOTALS4ETM7array[randomValueTOTALS4ETM7];
        pifTOTALS4ETM7arrayRANDusedInd[i] = randomValueTOTALS4ETM7;
        validTOTALS4ETM7 = 0;
        }
    }
    
    else if ( sensors4 == 6 )
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4TM1array[countPIFtotal];
    array pifTOTALS4TM2array[countPIFtotal];
    array pifTOTALS4TM3array[countPIFtotal];
    array pifTOTALS4TM4array[countPIFtotal];
    array pifTOTALS4TM5array[countPIFtotal];
    array pifTOTALS4TM7array[countPIFtotal];
    
    array pifTOTALS4TM1arrayRAND[minPifVAL];
    array pifTOTALS4TM2arrayRAND[minPifVAL];
    array pifTOTALS4TM3arrayRAND[minPifVAL];
    array pifTOTALS4TM4arrayRAND[minPifVAL];
    array pifTOTALS4TM5arrayRAND[minPifVAL];
    array pifTOTALS4TM7arrayRAND[minPifVAL];
    
    array pifTOTALS4TM1arrayRANDusedInd[minPifVAL];
    array pifTOTALS4TM2arrayRANDusedInd[minPifVAL];
    array pifTOTALS4TM3arrayRANDusedInd[minPifVAL];
    array pifTOTALS4TM4arrayRANDusedInd[minPifVAL];
    array pifTOTALS4TM5arrayRANDusedInd[minPifVAL];
    array pifTOTALS4TM7arrayRANDusedInd[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4TM1array[pifTOTALS4arrayCount] = REFS4TM1[i,j];
			      pifTOTALS4TM2array[pifTOTALS4arrayCount] = REFS4TM2[i,j];
			      pifTOTALS4TM3array[pifTOTALS4arrayCount] = REFS4TM3[i,j];
			      pifTOTALS4TM4array[pifTOTALS4arrayCount] = REFS4TM4[i,j];
                  pifTOTALS4TM5array[pifTOTALS4arrayCount] = REFS4TM5[i,j];
			      pifTOTALS4TM7array[pifTOTALS4arrayCount] = REFS4TM7[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
        
    ############# band 1
    
        numeric validTOTALS4TM1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM1 == 0 )
            {
                numeric randomValueTOTALS4TM1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM1 = pifTOTALS4TM1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM1 == randomValueTOTALS4TM1 )
                    {
                        validTOTALS4TM1 = 0;
                    }
                }
            }
        pifTOTALS4TM1arrayRAND[i] = pifTOTALS4TM1array[randomValueTOTALS4TM1];
        pifTOTALS4TM1arrayRANDusedInd[i] = randomValueTOTALS4TM1;
        validTOTALS4TM1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS4TM2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM2 == 0 )
            {
                numeric randomValueTOTALS4TM2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM2 = pifTOTALS4TM2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM2 == randomValueTOTALS4TM2 )
                    {
                        validTOTALS4TM2 = 0;
                    }
                }
            }
        pifTOTALS4TM2arrayRAND[i] = pifTOTALS4TM2array[randomValueTOTALS4TM2];
        pifTOTALS4TM2arrayRANDusedInd[i] = randomValueTOTALS4TM2;
        validTOTALS4TM2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS4TM3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM3 == 0 )
            {
                numeric randomValueTOTALS4TM3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM3 = pifTOTALS4TM3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM3 == randomValueTOTALS4TM3 )
                    {
                        validTOTALS4TM3 = 0;
                    }
                }
            }
        pifTOTALS4TM3arrayRAND[i] = pifTOTALS4TM3array[randomValueTOTALS4TM3];
        pifTOTALS4TM3arrayRANDusedInd[i] = randomValueTOTALS4TM3;
        validTOTALS4TM3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS4TM4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM4 == 0 )
            {
                numeric randomValueTOTALS4TM4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM4 = pifTOTALS4TM4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM4 == randomValueTOTALS4TM4 )
                    {
                        validTOTALS4TM4 = 0;
                    }
                }
            }
        pifTOTALS4TM4arrayRAND[i] = pifTOTALS4TM4array[randomValueTOTALS4TM4];
        pifTOTALS4TM4arrayRANDusedInd[i] = randomValueTOTALS4TM4;
        validTOTALS4TM4 = 0;
        }
    
    ############## band 5
    
        numeric validTOTALS4TM5 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM5 == 0 )
            {
                numeric randomValueTOTALS4TM5 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM5 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM5 = pifTOTALS4TM5arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM5 == randomValueTOTALS4TM5 )
                    {
                        validTOTALS4TM5 = 0;
                    }
                }
            }
        pifTOTALS4TM5arrayRAND[i] = pifTOTALS4TM5array[randomValueTOTALS4TM5];
        pifTOTALS4TM5arrayRANDusedInd[i] = randomValueTOTALS4TM5;
        validTOTALS4TM5 = 0;
        }
    
    ################ band 7
        
        numeric validTOTALS4TM7 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4TM7 == 0 )
            {
                numeric randomValueTOTALS4TM7 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4TM7 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4TM7 = pifTOTALS4TM7arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4TM7 == randomValueTOTALS4TM7 )
                    {
                        validTOTALS4TM7 = 0;
                    }
                }
            }
        pifTOTALS4TM7arrayRAND[i] = pifTOTALS4TM7array[randomValueTOTALS4TM7];
        pifTOTALS4TM7arrayRANDusedInd[i] = randomValueTOTALS4TM7;
        validTOTALS4TM7 = 0;
        }
    }
    
    else
    {
    numeric pifTOTALS4arrayCount = 1;
    array pifTOTALS4MSS1array[countPIFtotal];
    array pifTOTALS4MSS2array[countPIFtotal];
    array pifTOTALS4MSS3array[countPIFtotal];
    array pifTOTALS4MSS4array[countPIFtotal];
    
    array pifTOTALS4MSS1arrayRAND[minPifVAL];
    array pifTOTALS4MSS2arrayRAND[minPifVAL];
    array pifTOTALS4MSS3arrayRAND[minPifVAL];
    array pifTOTALS4MSS4arrayRAND[minPifVAL];
    
    array pifTOTALS4MSS1arrayRANDusedInd[minPifVAL];
    array pifTOTALS4MSS2arrayRANDusedInd[minPifVAL];
    array pifTOTALS4MSS3arrayRANDusedInd[minPifVAL];
    array pifTOTALS4MSS4arrayRANDusedInd[minPifVAL];
        
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS4MSS1array[pifTOTALS4arrayCount] = REFS4MSS1[i,j];
			pifTOTALS4MSS2array[pifTOTALS4arrayCount] = REFS4MSS2[i,j];
			pifTOTALS4MSS3array[pifTOTALS4arrayCount] = REFS4MSS3[i,j];
			pifTOTALS4MSS4array[pifTOTALS4arrayCount] = REFS4MSS4[i,j];
                  pifTOTALS4arrayCount = pifTOTALS4arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
        numeric validTOTALS4MSS1 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4MSS1 == 0 )
            {
                numeric randomValueTOTALS4MSS1 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4MSS1 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4MSS1 = pifTOTALS4MSS1arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4MSS1 == randomValueTOTALS4MSS1 )
                    {
                        validTOTALS4MSS1 = 0;
                    }
                }
            }
        pifTOTALS4MSS1arrayRAND[i] = pifTOTALS4MSS1array[randomValueTOTALS4MSS1];
        pifTOTALS4MSS1arrayRANDusedInd[i] = randomValueTOTALS4MSS1;
        validTOTALS4MSS1 = 0;
        }
    
    ############# band 2
    
        numeric validTOTALS4MSS2 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4MSS2 == 0 )
            {
                numeric randomValueTOTALS4MSS2 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4MSS2 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4MSS2 = pifTOTALS4MSS2arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4MSS2 == randomValueTOTALS4MSS2 )
                    {
                        validTOTALS4MSS2 = 0;
                    }
                }
            }
        pifTOTALS4MSS2arrayRAND[i] = pifTOTALS4MSS2array[randomValueTOTALS4MSS2];
        pifTOTALS4MSS2arrayRANDusedInd[i] = randomValueTOTALS4MSS2;
        validTOTALS4MSS2 = 0;
        }
    
    ############## band 3
        
        numeric validTOTALS4MSS3 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4MSS3 == 0 )
            {
                numeric randomValueTOTALS4MSS3 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4MSS3 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4MSS3 = pifTOTALS4MSS3arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4MSS3 == randomValueTOTALS4MSS3 )
                    {
                        validTOTALS4MSS3 = 0;
                    }
                }
            }
        pifTOTALS4MSS3arrayRAND[i] = pifTOTALS4MSS3array[randomValueTOTALS4MSS3];
        pifTOTALS4MSS3arrayRANDusedInd[i] = randomValueTOTALS4MSS3;
        validTOTALS4MSS3 = 0;
        }
    
    ################ band 4
    
        numeric validTOTALS4MSS4 = 0;
     
        for i = 1 to minPifVAL
        {
            while ( validTOTALS4MSS4 == 0 )
            {
                numeric randomValueTOTALS4MSS4 = floor((rand(countPIFtotal) + 1));      
                validTOTALS4MSS4 = 1;
    
                for j = 1 to (i-1)
                {
                    local numeric testValueTOTALS4MSS4 = pifTOTALS4MSS4arrayRANDusedInd[j];
                
                    if ( testValueTOTALS4MSS4 == randomValueTOTALS4MSS4 )
                    {
                        validTOTALS4MSS4 = 0;
                    }
                }
            }
        pifTOTALS4MSS4arrayRAND[i] = pifTOTALS4MSS4array[randomValueTOTALS4MSS4];
        pifTOTALS4MSS4arrayRANDusedInd[i] = randomValueTOTALS4MSS4;
        validTOTALS4MSS4 = 0;
        }
    }

    if ( sensors2 == 7 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2ETM1array[countPIFtotal];
    array pifTOTALS2ETM2array[countPIFtotal];
    array pifTOTALS2ETM3array[countPIFtotal];
    array pifTOTALS2ETM4array[countPIFtotal];
    array pifTOTALS2ETM5array[countPIFtotal];
    array pifTOTALS2ETM7array[countPIFtotal];
    
    array pifTOTALS2ETM1arrayRAND[minPifVAL];
    array pifTOTALS2ETM2arrayRAND[minPifVAL];
    array pifTOTALS2ETM3arrayRAND[minPifVAL];
    array pifTOTALS2ETM4arrayRAND[minPifVAL];
    array pifTOTALS2ETM5arrayRAND[minPifVAL];
    array pifTOTALS2ETM7arrayRAND[minPifVAL];
        
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2ETM1array[pifTOTALS2arrayCount] = REFS2ETM1[i,j];
			pifTOTALS2ETM2array[pifTOTALS2arrayCount] = REFS2ETM2[i,j];
			pifTOTALS2ETM3array[pifTOTALS2arrayCount] = REFS2ETM3[i,j];
			pifTOTALS2ETM4array[pifTOTALS2arrayCount] = REFS2ETM4[i,j];
                  pifTOTALS2ETM5array[pifTOTALS2arrayCount] = REFS2ETM5[i,j];
			pifTOTALS2ETM7array[pifTOTALS2arrayCount] = REFS2ETM7[i,j];
			pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    if ( sensors4 == 7 )
    {     
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors4 == 6 )
    {
	  for i = 1 to minPifVAL
        {
              pifTOTALS2ETM1arrayRAND[i] = pifTOTALS2ETM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }
    }
    
    ############# band 2
    if ( sensors4 == 7 )
    { 
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
    }
    else if ( sensors4 == 6 )
    {
        for i = 1 to minPifVAL
        {
              pifTOTALS2ETM2arrayRAND[i] = pifTOTALS2ETM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }
    }

    ############## band 3
    if ( sensors4 == 7 )
    {            
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
    }
    else if ( sensors4 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM3arrayRAND[i] = pifTOTALS2ETM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }
    }

    ################ band 4

    if ( sensors4 == 7 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
    }

    else if ( sensors4 == 6 )
    {
        for i = 1 to minPifVAL
        {
               pifTOTALS2ETM4arrayRAND[i] = pifTOTALS2ETM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }
    }

    ############## band 5

    if ( sensors4 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
    }

    else if ( sensors4 == 6 )
    {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM5arrayRAND[i] = pifTOTALS2ETM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
        }
    }

    ################ band 7

     if ( sensors4 == 7 )
     {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
     }

     else if ( sensors4 == 6 )
     {
        for i = 1 to minPifVAL
        {
        	   pifTOTALS2ETM7arrayRAND[i] = pifTOTALS2ETM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }
     }

   }
    
    else if ( sensors2 == 6 )
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2TM1array[countPIFtotal];
    array pifTOTALS2TM2array[countPIFtotal];
    array pifTOTALS2TM3array[countPIFtotal];
    array pifTOTALS2TM4array[countPIFtotal];
    array pifTOTALS2TM5array[countPIFtotal];
    array pifTOTALS2TM7array[countPIFtotal];
    
    array pifTOTALS2TM1arrayRAND[minPifVAL];
    array pifTOTALS2TM2arrayRAND[minPifVAL];
    array pifTOTALS2TM3arrayRAND[minPifVAL];
    array pifTOTALS2TM4arrayRAND[minPifVAL];
    array pifTOTALS2TM5arrayRAND[minPifVAL];
    array pifTOTALS2TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2TM1array[pifTOTALS2arrayCount] = REFS2TM1[i,j];
			pifTOTALS2TM2array[pifTOTALS2arrayCount] = REFS2TM2[i,j];
			pifTOTALS2TM3array[pifTOTALS2arrayCount] = REFS2TM3[i,j];
			pifTOTALS2TM4array[pifTOTALS2arrayCount] = REFS2TM4[i,j];
                  pifTOTALS2TM5array[pifTOTALS2arrayCount] = REFS2TM5[i,j];
			pifTOTALS2TM7array[pifTOTALS2arrayCount] = REFS2TM7[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1

    if ( sensors4 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
    }

    else if ( sensors4 == 6 )
    {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM1arrayRAND[i] = pifTOTALS2TM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }
    }

    ############# band 2
    
    if ( sensors4 == 7 )
    {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
    }

    else if ( sensors4 == 6 )
    {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM2arrayRAND[i] = pifTOTALS2TM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }  
    }

    ############## band 3
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM3arrayRAND[i] = pifTOTALS2TM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }   
	  }

    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM4arrayRAND[i] = pifTOTALS2TM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }  
	  }

    ############## band 5
    
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM5arrayRAND[i] = pifTOTALS2TM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
        }   
	}

    ################ band 7
        
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2TM7arrayRAND[i] = pifTOTALS2TM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }    
	  }
    }
    
    else
    {
    numeric pifTOTALS2arrayCount = 1;
    array pifTOTALS2MSS1array[countPIFtotal];
    array pifTOTALS2MSS2array[countPIFtotal];
    array pifTOTALS2MSS3array[countPIFtotal];
    array pifTOTALS2MSS4array[countPIFtotal];
    
    array pifTOTALS2MSS1arrayRAND[minPifVAL];
    array pifTOTALS2MSS2arrayRAND[minPifVAL];
    array pifTOTALS2MSS3arrayRAND[minPifVAL];
    array pifTOTALS2MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS2MSS1array[pifTOTALS2arrayCount] = REFS2MSS1[i,j];
			pifTOTALS2MSS2array[pifTOTALS2arrayCount] = REFS2MSS2[i,j];
			pifTOTALS2MSS3array[pifTOTALS2arrayCount] = REFS2MSS3[i,j];
			pifTOTALS2MSS4array[pifTOTALS2arrayCount] = REFS2MSS4[i,j];
                  pifTOTALS2arrayCount = pifTOTALS2arrayCount + 1;
                }
            }
        }
    
    ############# band 1
    
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS1arrayRAND[i] = pifTOTALS2MSS1array[pifTOTALS4MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS2arrayRAND[i] = pifTOTALS2MSS2array[pifTOTALS4MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors4 == 5 or sensors4 == 4 or sensors4 == 3 or sensors4 == 2 or sensors4 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS3arrayRAND[i] = pifTOTALS2MSS3array[pifTOTALS4MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
    
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	  }

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS2MSS4arrayRAND[i] = pifTOTALS2MSS4array[pifTOTALS4MSS4arrayRANDusedInd[i]];
        }
	}    
    }

    if ( sensors3 == 7 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3ETM1array[countPIFtotal];
    array pifTOTALS3ETM2array[countPIFtotal];
    array pifTOTALS3ETM3array[countPIFtotal];
    array pifTOTALS3ETM4array[countPIFtotal];
    array pifTOTALS3ETM5array[countPIFtotal];
    array pifTOTALS3ETM7array[countPIFtotal];
    
    array pifTOTALS3ETM1arrayRAND[minPifVAL];
    array pifTOTALS3ETM2arrayRAND[minPifVAL];
    array pifTOTALS3ETM3arrayRAND[minPifVAL];
    array pifTOTALS3ETM4arrayRAND[minPifVAL];
    array pifTOTALS3ETM5arrayRAND[minPifVAL];
    array pifTOTALS3ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3ETM1array[pifTOTALS3arrayCount] = REFS3ETM1[i,j];
			pifTOTALS3ETM2array[pifTOTALS3arrayCount] = REFS3ETM2[i,j];
			pifTOTALS3ETM3array[pifTOTALS3arrayCount] = REFS3ETM3[i,j];
			pifTOTALS3ETM4array[pifTOTALS3arrayCount] = REFS3ETM4[i,j];
                  pifTOTALS3ETM5array[pifTOTALS3arrayCount] = REFS3ETM5[i,j];
			pifTOTALS3ETM7array[pifTOTALS3arrayCount] = REFS3ETM7[i,j];
			pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM1arrayRAND[i] = pifTOTALS3ETM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM2arrayRAND[i] = pifTOTALS3ETM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }   
	}

    ############## band 3
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

	else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM3arrayRAND[i] = pifTOTALS3ETM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM4arrayRAND[i] = pifTOTALS3ETM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }  
	}

    ############## band 5
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM5arrayRAND[i] = pifTOTALS3ETM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3ETM7arrayRAND[i] = pifTOTALS3ETM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else if ( sensors3 == 6 )
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3TM1array[countPIFtotal];
    array pifTOTALS3TM2array[countPIFtotal];
    array pifTOTALS3TM3array[countPIFtotal];
    array pifTOTALS3TM4array[countPIFtotal];
    array pifTOTALS3TM5array[countPIFtotal];
    array pifTOTALS3TM7array[countPIFtotal];
    
    array pifTOTALS3TM1arrayRAND[minPifVAL];
    array pifTOTALS3TM2arrayRAND[minPifVAL];
    array pifTOTALS3TM3arrayRAND[minPifVAL];
    array pifTOTALS3TM4arrayRAND[minPifVAL];
    array pifTOTALS3TM5arrayRAND[minPifVAL];
    array pifTOTALS3TM7arrayRAND[minPifVAL];
    
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3TM1array[pifTOTALS3arrayCount] = REFS3TM1[i,j];
			pifTOTALS3TM2array[pifTOTALS3arrayCount] = REFS3TM2[i,j];
			pifTOTALS3TM3array[pifTOTALS3arrayCount] = REFS3TM3[i,j];
			pifTOTALS3TM4array[pifTOTALS3arrayCount] = REFS3TM4[i,j];
                  pifTOTALS3TM5array[pifTOTALS3arrayCount] = REFS3TM5[i,j];
			pifTOTALS3TM7array[pifTOTALS3arrayCount] = REFS3TM7[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM1arrayRAND[i] = pifTOTALS3TM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM2arrayRAND[i] = pifTOTALS3TM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }   
	  }

    ############## band 3
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM3arrayRAND[i] = pifTOTALS3TM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }   
	}

    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM4arrayRAND[i] = pifTOTALS3TM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }   
	}

    ############## band 5
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM5arrayRAND[i] = pifTOTALS3TM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
        }  
	}

    ################ band 7
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3TM7arrayRAND[i] = pifTOTALS3TM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }  
	   }
    }
    
    else
    {
    numeric pifTOTALS3arrayCount = 1;
    array pifTOTALS3MSS1array[countPIFtotal];
    array pifTOTALS3MSS2array[countPIFtotal];
    array pifTOTALS3MSS3array[countPIFtotal];
    array pifTOTALS3MSS4array[countPIFtotal];
    
    array pifTOTALS3MSS1arrayRAND[minPifVAL];
    array pifTOTALS3MSS2arrayRAND[minPifVAL];
    array pifTOTALS3MSS3arrayRAND[minPifVAL];
    array pifTOTALS3MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS3MSS1array[pifTOTALS3arrayCount] = REFS3MSS1[i,j];
			pifTOTALS3MSS2array[pifTOTALS3arrayCount] = REFS3MSS2[i,j];
			pifTOTALS3MSS3array[pifTOTALS3arrayCount] = REFS3MSS3[i,j];
			pifTOTALS3MSS4array[pifTOTALS3arrayCount] = REFS3MSS4[i,j];
                  pifTOTALS3arrayCount = pifTOTALS3arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }  
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS1arrayRAND[i] = pifTOTALS3MSS1array[pifTOTALS4MSS1arrayRANDusedInd[i]];
        }
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS2arrayRAND[i] = pifTOTALS3MSS2array[pifTOTALS4MSS2arrayRANDusedInd[i]];
        }
	}
    
    ############## band 3
     
      if ( sensors4 == 5 or sensors4 == 4 or sensors4 == 3 or sensors4 == 2 or sensors4 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS3arrayRAND[i] = pifTOTALS3MSS3array[pifTOTALS4MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }    
	  }

	 else
	 {
        for i = 1 to minPifVAL
        {
        pifTOTALS3MSS4arrayRAND[i] = pifTOTALS3MSS4array[pifTOTALS4MSS4arrayRANDusedInd[i]];
        }
	 }
    }

    if ( sensors1 == 7 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1ETM1array[countPIFtotal];
    array pifTOTALS1ETM2array[countPIFtotal];
    array pifTOTALS1ETM3array[countPIFtotal];
    array pifTOTALS1ETM4array[countPIFtotal];
    array pifTOTALS1ETM5array[countPIFtotal];
    array pifTOTALS1ETM7array[countPIFtotal];
    
    array pifTOTALS1ETM1arrayRAND[minPifVAL];
    array pifTOTALS1ETM2arrayRAND[minPifVAL];
    array pifTOTALS1ETM3arrayRAND[minPifVAL];
    array pifTOTALS1ETM4arrayRAND[minPifVAL];
    array pifTOTALS1ETM5arrayRAND[minPifVAL];
    array pifTOTALS1ETM7arrayRAND[minPifVAL];
    
        for i = 1 to ETMlins
        {
            for j = 1 to ETMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1ETM1array[pifTOTALS1arrayCount] = REFS1ETM1[i,j];
			pifTOTALS1ETM2array[pifTOTALS1arrayCount] = REFS1ETM2[i,j];
			pifTOTALS1ETM3array[pifTOTALS1arrayCount] = REFS1ETM3[i,j];
			pifTOTALS1ETM4array[pifTOTALS1arrayCount] = REFS1ETM4[i,j];
                  pifTOTALS1ETM5array[pifTOTALS1arrayCount] = REFS1ETM5[i,j];
			pifTOTALS1ETM7array[pifTOTALS1arrayCount] = REFS1ETM7[i,j];
			pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM1arrayRAND[i] = pifTOTALS1ETM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }  
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM2arrayRAND[i] = pifTOTALS1ETM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }
	}

    ############## band 3
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM3arrayRAND[i] = pifTOTALS1ETM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }  
	}

    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM4arrayRAND[i] = pifTOTALS1ETM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {   
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM5arrayRAND[i] = pifTOTALS1ETM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
        } 
	}
    
    ################ band 7
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
	}

	  else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1ETM7arrayRAND[i] = pifTOTALS1ETM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }  
	  }
    }
    
    else if ( sensors1 == 6 )
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1TM1array[countPIFtotal];
    array pifTOTALS1TM2array[countPIFtotal];
    array pifTOTALS1TM3array[countPIFtotal];
    array pifTOTALS1TM4array[countPIFtotal];
    array pifTOTALS1TM5array[countPIFtotal];
    array pifTOTALS1TM7array[countPIFtotal];
    
    array pifTOTALS1TM1arrayRAND[minPifVAL];
    array pifTOTALS1TM2arrayRAND[minPifVAL];
    array pifTOTALS1TM3arrayRAND[minPifVAL];
    array pifTOTALS1TM4arrayRAND[minPifVAL];
    array pifTOTALS1TM5arrayRAND[minPifVAL];
    array pifTOTALS1TM7arrayRAND[minPifVAL];
        
        for i = 1 to TMlins
        {
            for j = 1 to TMcols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1TM1array[pifTOTALS1arrayCount] = REFS1TM1[i,j];
			pifTOTALS1TM2array[pifTOTALS1arrayCount] = REFS1TM2[i,j];
			pifTOTALS1TM3array[pifTOTALS1arrayCount] = REFS1TM3[i,j];
			pifTOTALS1TM4array[pifTOTALS1arrayCount] = REFS1TM4[i,j];
                  pifTOTALS1TM5array[pifTOTALS1arrayCount] = REFS1TM5[i,j];
			pifTOTALS1TM7array[pifTOTALS1arrayCount] = REFS1TM7[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM1arrayRAND[i] = pifTOTALS1TM1array[pifTOTALS4ETM1arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM1arrayRAND[i] = pifTOTALS1TM1array[pifTOTALS4TM1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM2arrayRAND[i] = pifTOTALS1TM2array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM2arrayRAND[i] = pifTOTALS1TM2array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }    
	}

    ############## band 3
	
      if ( sensors4 == 7 )
      {         
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM3arrayRAND[i] = pifTOTALS1TM3array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }    
	}

    ################ band 4
     
      if ( sensors4 == 7 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM4arrayRAND[i] = pifTOTALS1TM4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }    
	}

    ############## band 5
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS4ETM5arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {  
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM5arrayRAND[i] = pifTOTALS1TM5array[pifTOTALS4TM5arrayRANDusedInd[i]];
    }  
	}

    ################ band 7
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS4ETM7arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1TM7arrayRAND[i] = pifTOTALS1TM7array[pifTOTALS4TM7arrayRANDusedInd[i]];
        }   
	  }
    }
    
    else
    {
    numeric pifTOTALS1arrayCount = 1;
    array pifTOTALS1MSS1array[countPIFtotal];
    array pifTOTALS1MSS2array[countPIFtotal];
    array pifTOTALS1MSS3array[countPIFtotal];
    array pifTOTALS1MSS4array[countPIFtotal];
    
    array pifTOTALS1MSS1arrayRAND[minPifVAL];
    array pifTOTALS1MSS2arrayRAND[minPifVAL];
    array pifTOTALS1MSS3arrayRAND[minPifVAL];
    array pifTOTALS1MSS4arrayRAND[minPifVAL];
    
        for i = 1 to MSSlins
        {
            for j = 1 to MSScols
            {     
                if ( PIFMASK[i,j] == 1 )
                {
                  pifTOTALS1MSS1array[pifTOTALS1arrayCount] = REFS1MSS1[i,j];
			pifTOTALS1MSS2array[pifTOTALS1arrayCount] = REFS1MSS2[i,j];
			pifTOTALS1MSS3array[pifTOTALS1arrayCount] = REFS1MSS3[i,j];
			pifTOTALS1MSS4array[pifTOTALS1arrayCount] = REFS1MSS4[i,j];
                  pifTOTALS1arrayCount = pifTOTALS1arrayCount + 1;
                }
            }
        }
    
    ############# band 1
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS4ETM2arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS4TM2arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS4MSS1arrayRANDusedInd[i]];
        }   
	}
    
    ############# band 2
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS4ETM3arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      { 
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS2arrayRAND[i] = pifTOTALS1MSS2array[pifTOTALS4TM3arrayRANDusedInd[i]];
        }   
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS1arrayRAND[i] = pifTOTALS1MSS1array[pifTOTALS4MSS2arrayRANDusedInd[i]];
        }   
	}
    
    ############## band 3
     
      if ( sensors4 == 5 or sensors4 == 4 or sensors4 == 3 or sensors4 == 2 or sensors4 == 1 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS3arrayRAND[i] = pifTOTALS1MSS3array[pifTOTALS4MSS3arrayRANDusedInd[i]];
        }
	  }
    
    ################ band 4
     
      if ( sensors4 == 7 )
      {    
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS4ETM4arrayRANDusedInd[i]];
        }
	}

      else if ( sensors4 == 6 )
      {
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS4TM4arrayRANDusedInd[i]];
        }    
	}

	else
	{
        for i = 1 to minPifVAL
        {
        pifTOTALS1MSS4arrayRAND[i] = pifTOTALS1MSS4array[pifTOTALS4MSS4arrayRANDusedInd[i]];
        }
	}
    }
  }
}

########################################################### 3 Mask Part #####################################################

else if ( isTherePifMask == 0 )
{
    if ( slaveMaster == 1 )
    {
          if ( countS1S2total >= minPifVAL && countS1S3total >= minPifVAL && countS1S4total >= minPifVAL )
          {
            	if ( sensors1 == 7 )
			{
                numeric pifSEPERATS1S2arrayCount = 1;
                numeric pifSEPERATS1S3arrayCount = 1;
                numeric pifSEPERATS1S4arrayCount = 1;

            	array pifSEPERATS1S2ETM1array[countS1S2total];
            	array pifSEPERATS1S2ETM2array[countS1S2total];
            	array pifSEPERATS1S2ETM3array[countS1S2total];
            	array pifSEPERATS1S2ETM4array[countS1S2total];
            	array pifSEPERATS1S2ETM5array[countS1S2total];
            	array pifSEPERATS1S2ETM7array[countS1S2total];
            	            
                array pifSEPERATS1S2ETM1arrayRAND[minPifVAL];
                array pifSEPERATS1S2ETM2arrayRAND[minPifVAL];
                array pifSEPERATS1S2ETM3arrayRAND[minPifVAL];
                array pifSEPERATS1S2ETM4arrayRAND[minPifVAL];
                array pifSEPERATS1S2ETM5arrayRAND[minPifVAL];
                array pifSEPERATS1S2ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS1S2ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S2ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S2ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S2ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S2ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S2ETM7arrayRANDusedInd[minPifVAL];
                
                
            	array pifSEPERATS1S3ETM1array[countS1S3total];
            	array pifSEPERATS1S3ETM2array[countS1S3total];
            	array pifSEPERATS1S3ETM3array[countS1S3total];
            	array pifSEPERATS1S3ETM4array[countS1S3total];
            	array pifSEPERATS1S3ETM5array[countS1S3total];
            	array pifSEPERATS1S3ETM7array[countS1S3total];
            
            	array pifSEPERATS1S3ETM1arrayRAND[minPifVAL];
                array pifSEPERATS1S3ETM2arrayRAND[minPifVAL];
                array pifSEPERATS1S3ETM3arrayRAND[minPifVAL];
                array pifSEPERATS1S3ETM4arrayRAND[minPifVAL];
                array pifSEPERATS1S3ETM5arrayRAND[minPifVAL];
                array pifSEPERATS1S3ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS1S3ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3ETM7arrayRANDusedInd[minPifVAL];
                
                
            	array pifSEPERATS1S4ETM1array[countS1S4total];
            	array pifSEPERATS1S4ETM2array[countS1S4total];
            	array pifSEPERATS1S4ETM3array[countS1S4total];
            	array pifSEPERATS1S4ETM4array[countS1S4total];
            	array pifSEPERATS1S4ETM5array[countS1S4total];
            	array pifSEPERATS1S4ETM7array[countS1S4total];
            
            	array pifSEPERATS1S4ETM1arrayRAND[minPifVAL];
                array pifSEPERATS1S4ETM2arrayRAND[minPifVAL];
                array pifSEPERATS1S4ETM3arrayRAND[minPifVAL];
                array pifSEPERATS1S4ETM4arrayRAND[minPifVAL];
                array pifSEPERATS1S4ETM5arrayRAND[minPifVAL];
                array pifSEPERATS1S4ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS1S4ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4ETM7arrayRANDusedInd[minPifVAL];

                	  for i = 1 to ETMlins
                	  {
                    	for j = 1 to ETMcols
                    	{     
					     if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
					     {
                        	pifSEPERATS1S2ETM1array[pifSEPERATS1S2arrayCount] = REFS1ETM1[i,j];                        
                       	 	pifSEPERATS1S2ETM2array[pifSEPERATS1S2arrayCount] = REFS1ETM2[i,j];                        
                        	pifSEPERATS1S2ETM3array[pifSEPERATS1S2arrayCount] = REFS1ETM3[i,j];
					        pifSEPERATS1S2ETM4array[pifSEPERATS1S2arrayCount] = REFS1ETM4[i,j];
                        	pifSEPERATS1S2ETM5array[pifSEPERATS1S2arrayCount] = REFS1ETM5[i,j];
					        pifSEPERATS1S2ETM7array[pifSEPERATS1S2arrayCount] = REFS1ETM7[i,j];
                        	pifSEPERATS1S2arrayCount = pifSEPERATS1S2arrayCount + 1;
					     }

					     if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
					     {
					        pifSEPERATS1S3ETM1array[pifSEPERATS1S3arrayCount] = REFS1ETM1[i,j];                        
                       	 	pifSEPERATS1S3ETM2array[pifSEPERATS1S3arrayCount] = REFS1ETM2[i,j];                        
                        	pifSEPERATS1S3ETM3array[pifSEPERATS1S3arrayCount] = REFS1ETM3[i,j];
					        pifSEPERATS1S3ETM4array[pifSEPERATS1S3arrayCount] = REFS1ETM4[i,j];
                        	pifSEPERATS1S3ETM5array[pifSEPERATS1S3arrayCount] = REFS1ETM5[i,j];
					        pifSEPERATS1S3ETM7array[pifSEPERATS1S3arrayCount] = REFS1ETM7[i,j];
					        pifSEPERATS1S3arrayCount = pifSEPERATS1S3arrayCount + 1;
					     }

					     if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
					     {
					        pifSEPERATS1S4ETM1array[pifSEPERATS1S4arrayCount] = REFS1ETM1[i,j];                        
                       	 	pifSEPERATS1S4ETM2array[pifSEPERATS1S4arrayCount] = REFS1ETM2[i,j];                        
                        	pifSEPERATS1S4ETM3array[pifSEPERATS1S4arrayCount] = REFS1ETM3[i,j];
					        pifSEPERATS1S4ETM4array[pifSEPERATS1S4arrayCount] = REFS1ETM4[i,j];
                        	pifSEPERATS1S4ETM5array[pifSEPERATS1S4arrayCount] = REFS1ETM5[i,j];
					        pifSEPERATS1S4ETM7array[pifSEPERATS1S4arrayCount] = REFS1ETM7[i,j];
					        pifSEPERATS1S4arrayCount = pifSEPERATS1S4arrayCount + 1;
					     }
                    	}
                	  }
            	            
                ############# band 1
            
                numeric validSEPERATS1S2ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM1 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM1 = pifSEPERATS1S2ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM1 == randomValueSEPERATS1S2ETM1 )
                            {
                                validSEPERATS1S2ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM1arrayRAND[i] = pifSEPERATS1S2ETM1array[randomValueSEPERATS1S2ETM1];
                pifSEPERATS1S2ETM1arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM1;
                validSEPERATS1S2ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S2ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM2 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM2 = pifSEPERATS1S2ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM2 == randomValueSEPERATS1S2ETM2 )
                            {
                                validSEPERATS1S2ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM2arrayRAND[i] = pifSEPERATS1S2ETM2array[randomValueSEPERATS1S2ETM2];
                pifSEPERATS1S2ETM2arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM2;
                validSEPERATS1S2ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S2ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM3 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM3 = pifSEPERATS1S2ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM3 == randomValueSEPERATS1S2ETM3 )
                            {
                                validSEPERATS1S2ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM3arrayRAND[i] = pifSEPERATS1S2ETM3array[randomValueSEPERATS1S2ETM3];
                pifSEPERATS1S2ETM3arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM3;
                validSEPERATS1S2ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S2ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM4 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM4 = pifSEPERATS1S2ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM4 == randomValueSEPERATS1S2ETM4 )
                            {
                                validSEPERATS1S2ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM4arrayRAND[i] = pifSEPERATS1S2ETM4array[randomValueSEPERATS1S2ETM4];
                pifSEPERATS1S2ETM4arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM4;
                validSEPERATS1S2ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S2ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM5 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM5 = pifSEPERATS1S2ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM5 == randomValueSEPERATS1S2ETM5 )
                            {
                                validSEPERATS1S2ETM5 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM5arrayRAND[i] = pifSEPERATS1S2ETM5array[randomValueSEPERATS1S2ETM5];
                pifSEPERATS1S2ETM5arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM5;
                validSEPERATS1S2ETM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS1S2ETM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2ETM7 == 0 )
                    {
                        numeric randomValueSEPERATS1S2ETM7 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2ETM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2ETM7 = pifSEPERATS1S2ETM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2ETM7 == randomValueSEPERATS1S2ETM7 )
                            {
                                validSEPERATS1S2ETM7 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2ETM7arrayRAND[i] = pifSEPERATS1S2ETM7array[randomValueSEPERATS1S2ETM7];
                pifSEPERATS1S2ETM7arrayRANDusedInd[i] = randomValueSEPERATS1S2ETM7;
                validSEPERATS1S2ETM7 = 0;
                }
            
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS1S3ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM1 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM1 = pifSEPERATS1S3ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM1 == randomValueSEPERATS1S3ETM1 )
                            {
                                validSEPERATS1S3ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM1arrayRAND[i] = pifSEPERATS1S3ETM1array[randomValueSEPERATS1S3ETM1];
                pifSEPERATS1S3ETM1arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM1;
                validSEPERATS1S3ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S3ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM2 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM2 = pifSEPERATS1S3ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM2 == randomValueSEPERATS1S3ETM2 )
                            {
                                validSEPERATS1S3ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM2arrayRAND[i] = pifSEPERATS1S3ETM2array[randomValueSEPERATS1S3ETM2];
                pifSEPERATS1S3ETM2arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM2;
                validSEPERATS1S3ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S3ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM3 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM3 = pifSEPERATS1S3ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM3 == randomValueSEPERATS1S3ETM3 )
                            {
                                validSEPERATS1S3ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM3arrayRAND[i] = pifSEPERATS1S3ETM3array[randomValueSEPERATS1S3ETM3];
                pifSEPERATS1S3ETM3arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM3;
                validSEPERATS1S3ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S3ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM4 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM4 = pifSEPERATS1S3ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM4 == randomValueSEPERATS1S3ETM4 )
                            {
                                validSEPERATS1S3ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM4arrayRAND[i] = pifSEPERATS1S3ETM4array[randomValueSEPERATS1S3ETM4];
                pifSEPERATS1S3ETM4arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM4;
                validSEPERATS1S3ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S3ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM5 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM5 = pifSEPERATS1S3ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM5 == randomValueSEPERATS1S3ETM5 )
                            {
                                validSEPERATS1S3ETM5 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM5arrayRAND[i] = pifSEPERATS1S3ETM5array[randomValueSEPERATS1S3ETM5];
                pifSEPERATS1S3ETM5arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM5;
                validSEPERATS1S3ETM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS1S3ETM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3ETM7 == 0 )
                    {
                        numeric randomValueSEPERATS1S3ETM7 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3ETM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3ETM7 = pifSEPERATS1S3ETM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3ETM7 == randomValueSEPERATS1S3ETM7 )
                            {
                                validSEPERATS1S3ETM7 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3ETM7arrayRAND[i] = pifSEPERATS1S3ETM7array[randomValueSEPERATS1S3ETM7];
                pifSEPERATS1S3ETM7arrayRANDusedInd[i] = randomValueSEPERATS1S3ETM7;
                validSEPERATS1S3ETM7 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS1S4ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S4ETM1 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4ETM1 = pifSEPERATS1S4ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4ETM1 == randomValueSEPERATS1S4ETM1 )
                            {
                                validSEPERATS1S4ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4ETM1arrayRAND[i] = pifSEPERATS1S4ETM1array[randomValueSEPERATS1S4ETM1];
                pifSEPERATS1S4ETM1arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM1;
                validSEPERATS1S4ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S4ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S4ETM2 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4ETM2 = pifSEPERATS1S4ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4ETM2 == randomValueSEPERATS1S4ETM2 )
                            {
                                validSEPERATS1S4ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4ETM2arrayRAND[i] = pifSEPERATS1S4ETM2array[randomValueSEPERATS1S4ETM2];
                pifSEPERATS1S4ETM2arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM2;
                validSEPERATS1S4ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S4ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S4ETM3 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4ETM3 = pifSEPERATS1S4ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4ETM3 == randomValueSEPERATS1S4ETM3 )
                            {
                                validSEPERATS1S4ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4ETM3arrayRAND[i] = pifSEPERATS1S4ETM3array[randomValueSEPERATS1S4ETM3];
                pifSEPERATS1S4ETM3arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM3;
                validSEPERATS1S4ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S4ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S4ETM4 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4ETM4 = pifSEPERATS1S4ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4ETM4 == randomValueSEPERATS1S4ETM4 )
                            {
                                validSEPERATS1S4ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4ETM4arrayRAND[i] = pifSEPERATS1S4ETM4array[randomValueSEPERATS1S4ETM4];
                pifSEPERATS1S4ETM4arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM4;
                validSEPERATS1S4ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S4ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S4ETM5 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4ETM5 = pifSEPERATS1S4ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4ETM5 == randomValueSEPERATS1S4ETM5 )
                            {
                                validSEPERATS1S4ETM5 = 0;
                            }
                 }
                    }
                pifSEPERATS1S4ETM5arrayRAND[i] = pifSEPERATS1S4ETM5array[randomValueSEPERATS1S4ETM5];
                pifSEPERATS1S4ETM5arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM5;
                validSEPERATS1S4ETM5 = 0;
                }
            
            ################ band 7
                
                    numeric validSEPERATS1S4ETM7 = 0;
                 
                    for i = 1 to minPifVAL
                    {
                        while ( validSEPERATS1S4ETM7 == 0 )
                        {
                            numeric randomValueSEPERATS1S4ETM7 = floor((rand(countS1S4total) + 1));      
                            validSEPERATS1S4ETM7 = 1;
                
                            for j = 1 to (i-1)
                            {
                                local numeric testValueSEPERATS1S4ETM7 = pifSEPERATS1S4ETM7arrayRANDusedInd[j];
                            
                                if ( testValueSEPERATS1S4ETM7 == randomValueSEPERATS1S4ETM7 )
                                {
                                    validSEPERATS1S4ETM7 = 0;
                                }
                            }
                        }
                    pifSEPERATS1S4ETM7arrayRAND[i] = pifSEPERATS1S4ETM7array[randomValueSEPERATS1S4ETM7];
                    pifSEPERATS1S4ETM7arrayRANDusedInd[i] = randomValueSEPERATS1S4ETM7;
                    validSEPERATS1S4ETM7 = 0;
                    }            
            	}
		
			else if ( sensors1 == 6 )
			{
                numeric pifSEPERATS1S2arrayCount = 1;
                numeric pifSEPERATS1S3arrayCount = 1;
                numeric pifSEPERATS1S4arrayCount = 1;

            	array pifSEPERATS1S2TM1array[countS1S2total];
            	array pifSEPERATS1S2TM2array[countS1S2total];
            	array pifSEPERATS1S2TM3array[countS1S2total];
            	array pifSEPERATS1S2TM4array[countS1S2total];
            	array pifSEPERATS1S2TM5array[countS1S2total];
            	array pifSEPERATS1S2TM7array[countS1S2total];
            
            	array pifSEPERATS1S2TM1arrayRAND[minPifVAL];
            	array pifSEPERATS1S2TM2arrayRAND[minPifVAL];
            	array pifSEPERATS1S2TM3arrayRAND[minPifVAL];
            	array pifSEPERATS1S2TM4arrayRAND[minPifVAL];
            	array pifSEPERATS1S2TM5arrayRAND[minPifVAL];
            	array pifSEPERATS1S2TM7arrayRAND[minPifVAL];
            
            	array pifSEPERATS1S2TM1arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2TM2arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2TM3arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2TM4arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2TM5arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2TM7arrayRANDusedInd[minPifVAL];
            

            	array pifSEPERATS1S3TM1array[countS1S3total];
            	array pifSEPERATS1S3TM2array[countS1S3total];
            	array pifSEPERATS1S3TM3array[countS1S3total];
            	array pifSEPERATS1S3TM4array[countS1S3total];
            	array pifSEPERATS1S3TM5array[countS1S3total];
            	array pifSEPERATS1S3TM7array[countS1S3total];
            
            	array pifSEPERATS1S3TM1arrayRAND[minPifVAL];
                array pifSEPERATS1S3TM2arrayRAND[minPifVAL];
                array pifSEPERATS1S3TM3arrayRAND[minPifVAL];
                array pifSEPERATS1S3TM4arrayRAND[minPifVAL];
                array pifSEPERATS1S3TM5arrayRAND[minPifVAL];
                array pifSEPERATS1S3TM7arrayRAND[minPifVAL];
    
                array pifSEPERATS1S3TM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3TM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3TM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3TM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3TM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3TM7arrayRANDusedInd[minPifVAL];
                

            	array pifSEPERATS1S4TM1array[countS1S4total];
            	array pifSEPERATS1S4TM2array[countS1S4total];
            	array pifSEPERATS1S4TM3array[countS1S4total];
            	array pifSEPERATS1S4TM4array[countS1S4total];
            	array pifSEPERATS1S4TM5array[countS1S4total];
            	array pifSEPERATS1S4TM7array[countS1S4total];
            
            	array pifSEPERATS1S4TM1arrayRAND[minPifVAL];
                array pifSEPERATS1S4TM2arrayRAND[minPifVAL];
                array pifSEPERATS1S4TM3arrayRAND[minPifVAL];
                array pifSEPERATS1S4TM4arrayRAND[minPifVAL];
                array pifSEPERATS1S4TM5arrayRAND[minPifVAL];
                array pifSEPERATS1S4TM7arrayRAND[minPifVAL];
    
                array pifSEPERATS1S4TM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4TM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4TM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4TM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4TM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4TM7arrayRANDusedInd[minPifVAL];

                	  for i = 1 to TMlins
                	  {
                    	for j = 1 to TMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                            {
                                    pifSEPERATS1S2TM1array[pifSEPERATS1S2arrayCount] = REFS1TM1[i,j];                        
                                        pifSEPERATS1S2TM2array[pifSEPERATS1S2arrayCount] = REFS1TM2[i,j];                        
                                    pifSEPERATS1S2TM3array[pifSEPERATS1S2arrayCount] = REFS1TM3[i,j];
                            pifSEPERATS1S2TM4array[pifSEPERATS1S2arrayCount] = REFS1TM4[i,j];
                                    pifSEPERATS1S2TM5array[pifSEPERATS1S2arrayCount] = REFS1TM5[i,j];
                            pifSEPERATS1S2TM7array[pifSEPERATS1S2arrayCount] = REFS1TM7[i,j];
                                    pifSEPERATS1S2arrayCount = pifSEPERATS1S2arrayCount + 1;
                            }
        
                            if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                            pifSEPERATS1S3TM1array[pifSEPERATS1S3arrayCount] = REFS1TM1[i,j];                        
                                        pifSEPERATS1S3TM2array[pifSEPERATS1S3arrayCount] = REFS1TM2[i,j];                        
                                    pifSEPERATS1S3TM3array[pifSEPERATS1S3arrayCount] = REFS1TM3[i,j];
                            pifSEPERATS1S3TM4array[pifSEPERATS1S3arrayCount] = REFS1TM4[i,j];
                                    pifSEPERATS1S3TM5array[pifSEPERATS1S3arrayCount] = REFS1TM5[i,j];
                            pifSEPERATS1S3TM7array[pifSEPERATS1S3arrayCount] = REFS1TM7[i,j];
                            pifSEPERATS1S3arrayCount = pifSEPERATS1S3arrayCount + 1;
                            }
        
                            if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                            pifSEPERATS1S4TM1array[pifSEPERATS1S4arrayCount] = REFS1TM1[i,j];                        
                                        pifSEPERATS1S4TM2array[pifSEPERATS1S4arrayCount] = REFS1TM2[i,j];                        
                                    pifSEPERATS1S4TM3array[pifSEPERATS1S4arrayCount] = REFS1TM3[i,j];
                            pifSEPERATS1S4TM4array[pifSEPERATS1S4arrayCount] = REFS1TM4[i,j];
                                    pifSEPERATS1S4TM5array[pifSEPERATS1S4arrayCount] = REFS1TM5[i,j];
                            pifSEPERATS1S4TM7array[pifSEPERATS1S4arrayCount] = REFS1TM7[i,j];
                            pifSEPERATS1S4arrayCount = pifSEPERATS1S4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ############# band 1
            
                numeric validSEPERATS1S2TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM1 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM1 = pifSEPERATS1S2TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM1 == randomValueSEPERATS1S2TM1 )
                            {
                                validSEPERATS1S2TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM1arrayRAND[i] = pifSEPERATS1S2TM1array[randomValueSEPERATS1S2TM1];
                pifSEPERATS1S2TM1arrayRANDusedInd[i] = randomValueSEPERATS1S2TM1;
                validSEPERATS1S2TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S2TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM2 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM2 = pifSEPERATS1S2TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM2 == randomValueSEPERATS1S2TM2 )
                            {
                                validSEPERATS1S2TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM2arrayRAND[i] = pifSEPERATS1S2TM2array[randomValueSEPERATS1S2TM2];
                pifSEPERATS1S2TM2arrayRANDusedInd[i] = randomValueSEPERATS1S2TM2;
                validSEPERATS1S2TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S2TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM3 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM3 = pifSEPERATS1S2TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM3 == randomValueSEPERATS1S2TM3 )
                            {
                                validSEPERATS1S2TM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM3arrayRAND[i] = pifSEPERATS1S2TM3array[randomValueSEPERATS1S2TM3];
                pifSEPERATS1S2TM3arrayRANDusedInd[i] = randomValueSEPERATS1S2TM3;
                validSEPERATS1S2TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S2TM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM4 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM4 = pifSEPERATS1S2TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM4 == randomValueSEPERATS1S2TM4 )
                            {
                                validSEPERATS1S2TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM4arrayRAND[i] = pifSEPERATS1S2TM4array[randomValueSEPERATS1S2TM4];
                pifSEPERATS1S2TM4arrayRANDusedInd[i] = randomValueSEPERATS1S2TM4;
                validSEPERATS1S2TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S2TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM5 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM5 = pifSEPERATS1S2TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM5 == randomValueSEPERATS1S2TM5 )
                            {
                                validSEPERATS1S2TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM5arrayRAND[i] = pifSEPERATS1S2TM5array[randomValueSEPERATS1S2TM5];
                pifSEPERATS1S2TM5arrayRANDusedInd[i] = randomValueSEPERATS1S2TM5;
                validSEPERATS1S2TM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS1S2TM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2TM7 == 0 )
                    {
                        numeric randomValueSEPERATS1S2TM7 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2TM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2TM7 = pifSEPERATS1S2TM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2TM7 == randomValueSEPERATS1S2TM7 )
                            {
                                validSEPERATS1S2TM7 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2TM7arrayRAND[i] = pifSEPERATS1S2TM7array[randomValueSEPERATS1S2TM7];
                pifSEPERATS1S2TM7arrayRANDusedInd[i] = randomValueSEPERATS1S2TM7;
                validSEPERATS1S2TM7 = 0;
                }
            
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS1S3TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3TM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM1 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3TM1 = pifSEPERATS1S3TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM1 == randomValueSEPERATS1S3TM1 )
                            {
                                validSEPERATS1S3TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM1arrayRAND[i] = pifSEPERATS1S3TM1array[randomValueSEPERATS1S3TM1];
                pifSEPERATS1S3TM1arrayRANDusedInd[i] = randomValueSEPERATS1S3TM1;
                validSEPERATS1S3TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S3TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3TM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM2 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3TM2 = pifSEPERATS1S3TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM2 == randomValueSEPERATS1S3TM2 )
                            {
                                validSEPERATS1S3TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM2arrayRAND[i] = pifSEPERATS1S3TM2array[randomValueSEPERATS1S3TM2];
                pifSEPERATS1S3TM2arrayRANDusedInd[i] = randomValueSEPERATS1S3TM2;
                validSEPERATS1S3TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S3TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3TM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM3 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3TM3 = pifSEPERATS1S3TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM3 == randomValueSEPERATS1S3TM3 )
                            {
                                validSEPERATS1S3TM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM3arrayRAND[i] = pifSEPERATS1S3TM3array[randomValueSEPERATS1S3TM3];
                pifSEPERATS1S3TM3arrayRANDusedInd[i] = randomValueSEPERATS1S3TM3;
                validSEPERATS1S3TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S3TM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3TM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM4 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3TM4 = pifSEPERATS1S3TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM4 == randomValueSEPERATS1S3TM4 )
                            {
                                validSEPERATS1S3TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM4arrayRAND[i] = pifSEPERATS1S3TM4array[randomValueSEPERATS1S3TM4];
                pifSEPERATS1S3TM4arrayRANDusedInd[i] = randomValueSEPERATS1S3TM4;
                validSEPERATS1S3TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S3TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                  while ( validSEPERATS1S3TM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM5 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
       local numeric testValueSEPERATS1S3TM5 = pifSEPERATS1S3TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM5 == randomValueSEPERATS1S3TM5 )
                            {
                                validSEPERATS1S3TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM5arrayRAND[i] = pifSEPERATS1S3TM5array[randomValueSEPERATS1S3TM5];
                pifSEPERATS1S3TM5arrayRANDusedInd[i] = randomValueSEPERATS1S3TM5;
                validSEPERATS1S3TM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS1S3TM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3TM7 == 0 )
                    {
                        numeric randomValueSEPERATS1S3TM7 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3TM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3TM7 = pifSEPERATS1S3TM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3TM7 == randomValueSEPERATS1S3TM7 )
                            {
                                validSEPERATS1S3TM7 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3TM7arrayRAND[i] = pifSEPERATS1S3TM7array[randomValueSEPERATS1S3TM7];
                pifSEPERATS1S3TM7arrayRANDusedInd[i] = randomValueSEPERATS1S3TM7;
                validSEPERATS1S3TM7 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS1S4TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4TM1 == 0 )
                    {
                        numeric randomValueSEPERATS1S4TM1 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4TM1 = pifSEPERATS1S4TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4TM1 == randomValueSEPERATS1S4TM1 )
                            {
                                validSEPERATS1S4TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4TM1arrayRAND[i] = pifSEPERATS1S4TM1array[randomValueSEPERATS1S4TM1];
                pifSEPERATS1S4TM1arrayRANDusedInd[i] = randomValueSEPERATS1S4TM1;
                validSEPERATS1S4TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S4TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4TM2 == 0 )
                    {
                        numeric randomValueSEPERATS1S4TM2 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4TM2 = pifSEPERATS1S4TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4TM2 == randomValueSEPERATS1S4TM2 )
                            {
                                validSEPERATS1S4TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4TM2arrayRAND[i] = pifSEPERATS1S4TM2array[randomValueSEPERATS1S4TM2];
                pifSEPERATS1S4TM2arrayRANDusedInd[i] = randomValueSEPERATS1S4TM2;
                validSEPERATS1S4TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S4TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4TM3 == 0 )
                    {
                        numeric randomValueSEPERATS1S4TM3 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4TM3 = pifSEPERATS1S4TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4TM3 == randomValueSEPERATS1S4TM3 )
                            {
                                validSEPERATS1S4TM3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4TM3arrayRAND[i] = pifSEPERATS1S4TM3array[randomValueSEPERATS1S4TM3];
                pifSEPERATS1S4TM3arrayRANDusedInd[i] = randomValueSEPERATS1S4TM3;
                validSEPERATS1S4TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S4TM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4TM4 == 0 )
                    {
                        numeric randomValueSEPERATS1S4TM4 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4TM4 = pifSEPERATS1S4TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4TM4 == randomValueSEPERATS1S4TM4 )
                            {
                                validSEPERATS1S4TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4TM4arrayRAND[i] = pifSEPERATS1S4TM4array[randomValueSEPERATS1S4TM4];
                pifSEPERATS1S4TM4arrayRANDusedInd[i] = randomValueSEPERATS1S4TM4;
                validSEPERATS1S4TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS1S4TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4TM5 == 0 )
                    {
                        numeric randomValueSEPERATS1S4TM5 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4TM5 = pifSEPERATS1S4TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4TM5 == randomValueSEPERATS1S4TM5 )
                            {
                                validSEPERATS1S4TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4TM5arrayRAND[i] = pifSEPERATS1S4TM5array[randomValueSEPERATS1S4TM5];
                pifSEPERATS1S4TM5arrayRANDusedInd[i] = randomValueSEPERATS1S4TM5;
                validSEPERATS1S4TM5 = 0;
                }
            
            ################ band 7
                
                    numeric validSEPERATS1S4TM7 = 0;
                 
                    for i = 1 to minPifVAL
                    {
                        while ( validSEPERATS1S4TM7 == 0 )
                        {
                            numeric randomValueSEPERATS1S4TM7 = floor((rand(countS1S4total) + 1));      
                            validSEPERATS1S4TM7 = 1;
                
                            for j = 1 to (i-1)
                            {
                                local numeric testValueSEPERATS1S4TM7 = pifSEPERATS1S4TM7arrayRANDusedInd[j];
                            
                                if ( testValueSEPERATS1S4TM7 == randomValueSEPERATS1S4TM7 )
                                {
                                    validSEPERATS1S4TM7 = 0;
                                }
                            }
                        }
                    pifSEPERATS1S4TM7arrayRAND[i] = pifSEPERATS1S4TM7array[randomValueSEPERATS1S4TM7];
                    pifSEPERATS1S4TM7arrayRANDusedInd[i] = randomValueSEPERATS1S4TM7;
                    validSEPERATS1S4TM7 = 0;
                    }               	  
			}

			else
			{
                numeric pifSEPERATS1S2arrayCount = 1;
                numeric pifSEPERATS1S3arrayCount = 1;
                numeric pifSEPERATS1S4arrayCount = 1;

            	array pifSEPERATS1S2MSS1array[countS1S2total];
            	array pifSEPERATS1S2MSS2array[countS1S2total];
            	array pifSEPERATS1S2MSS3array[countS1S2total];
            	array pifSEPERATS1S2MSS4array[countS1S2total];
            
            	array pifSEPERATS1S2MSS1arrayRAND[minPifVAL];
            	array pifSEPERATS1S2MSS2arrayRAND[minPifVAL];
            	array pifSEPERATS1S2MSS3arrayRAND[minPifVAL];
            	array pifSEPERATS1S2MSS4arrayRAND[minPifVAL];
            	            
            	array pifSEPERATS1S2MSS1arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2MSS2arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2MSS3arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS1S2MSS4arrayRANDusedInd[minPifVAL];
            
            
            	array pifSEPERATS1S3MSS1array[countS1S3total];
            	array pifSEPERATS1S3MSS2array[countS1S3total];
            	array pifSEPERATS1S3MSS3array[countS1S3total];
            	array pifSEPERATS1S3MSS4array[countS1S3total];
            
            	array pifSEPERATS1S3MSS1arrayRAND[minPifVAL];
                array pifSEPERATS1S3MSS2arrayRAND[minPifVAL];
                array pifSEPERATS1S3MSS3arrayRAND[minPifVAL];
                array pifSEPERATS1S3MSS4arrayRAND[minPifVAL];
                   
                array pifSEPERATS1S3MSS1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3MSS2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3MSS3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S3MSS4arrayRANDusedInd[minPifVAL];
                
                            
            	array pifSEPERATS1S4MSS1array[countS1S4total];
            	array pifSEPERATS1S4MSS2array[countS1S4total];
            	array pifSEPERATS1S4MSS3array[countS1S4total];
            	array pifSEPERATS1S4MSS4array[countS1S4total];
            
            	array pifSEPERATS1S4MSS1arrayRAND[minPifVAL];
                array pifSEPERATS1S4MSS2arrayRAND[minPifVAL];
                array pifSEPERATS1S4MSS3arrayRAND[minPifVAL];
                array pifSEPERATS1S4MSS4arrayRAND[minPifVAL];
                   
                array pifSEPERATS1S4MSS1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4MSS2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4MSS3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS1S4MSS4arrayRANDusedInd[minPifVAL];
                
            	  for i = 1 to MSSlins
                	  {
                    	for j = 1 to MSScols
                    	{     
                            if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                            {
                                    pifSEPERATS1S2MSS1array[pifSEPERATS1S2arrayCount] = REFS1MSS1[i,j];                        
                                        pifSEPERATS1S2MSS2array[pifSEPERATS1S2arrayCount] = REFS1MSS2[i,j];                        
                                    pifSEPERATS1S2MSS3array[pifSEPERATS1S2arrayCount] = REFS1MSS3[i,j];
                            pifSEPERATS1S2MSS4array[pifSEPERATS1S2arrayCount] = REFS1MSS4[i,j];
                                    pifSEPERATS1S2arrayCount = pifSEPERATS1S2arrayCount + 1;
                            }
        
                            if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                            pifSEPERATS1S3MSS1array[pifSEPERATS1S3arrayCount] = REFS1MSS1[i,j];                        
                                        pifSEPERATS1S3MSS2array[pifSEPERATS1S3arrayCount] = REFS1MSS2[i,j];                        
                                    pifSEPERATS1S3MSS3array[pifSEPERATS1S3arrayCount] = REFS1MSS3[i,j];
                            pifSEPERATS1S3MSS4array[pifSEPERATS1S3arrayCount] = REFS1MSS4[i,j];
                            pifSEPERATS1S3arrayCount = pifSEPERATS1S3arrayCount + 1;
                            }
        
                            if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                            pifSEPERATS1S4MSS1array[pifSEPERATS1S4arrayCount] = REFS1MSS1[i,j];                        
                                        pifSEPERATS1S4MSS2array[pifSEPERATS1S4arrayCount] = REFS1MSS2[i,j];                        
                                    pifSEPERATS1S4MSS3array[pifSEPERATS1S4arrayCount] = REFS1MSS3[i,j];
                            pifSEPERATS1S4MSS4array[pifSEPERATS1S4arrayCount] = REFS1MSS4[i,j];
                            pifSEPERATS1S4arrayCount = pifSEPERATS1S4arrayCount + 1;
                            }
                    	}
                	  }
                	  
            ############# band 1
            
                numeric validSEPERATS1S2MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS1S2MSS1 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2MSS1 = 1;
            
                        for j = 1to (i-1)
                        {
                            local numeric testValueSEPERATS1S2MSS1 = pifSEPERATS1S2MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2MSS1 == randomValueSEPERATS1S2MSS1 )
                            {
                                validSEPERATS1S2MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2MSS1arrayRAND[i] = pifSEPERATS1S2MSS1array[randomValueSEPERATS1S2MSS1];
                pifSEPERATS1S2MSS1arrayRANDusedInd[i] = randomValueSEPERATS1S2MSS1;
                validSEPERATS1S2MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S2MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS1S2MSS2 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2MSS2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2MSS2 = pifSEPERATS1S2MSS2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2MSS2 == randomValueSEPERATS1S2MSS2 )
                            {
                                validSEPERATS1S2MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2MSS2arrayRAND[i] = pifSEPERATS1S2MSS2array[randomValueSEPERATS1S2MSS2];
                pifSEPERATS1S2MSS2arrayRANDusedInd[i] = randomValueSEPERATS1S2MSS2;
                validSEPERATS1S2MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S2MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS1S2MSS3 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2MSS3 = pifSEPERATS1S2MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2MSS3 == randomValueSEPERATS1S2MSS3 )
                            {
                                validSEPERATS1S2MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2MSS3arrayRAND[i] = pifSEPERATS1S2MSS3array[randomValueSEPERATS1S2MSS3];
                pifSEPERATS1S2MSS3arrayRANDusedInd[i] = randomValueSEPERATS1S2MSS3;
                validSEPERATS1S2MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S2MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S2MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS1S2MSS4 = floor((rand(countS1S2total) + 1));      
                        validSEPERATS1S2MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S2MSS4 = pifSEPERATS1S2MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S2MSS4 == randomValueSEPERATS1S2MSS4 )
                            {
                                validSEPERATS1S2MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S2MSS4arrayRAND[i] = pifSEPERATS1S2MSS4array[randomValueSEPERATS1S2MSS4];
                pifSEPERATS1S2MSS4arrayRANDusedInd[i] = randomValueSEPERATS1S2MSS4;
                validSEPERATS1S2MSS4 = 0;
                }
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS1S3MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS1S3MSS1 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3MSS1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3MSS1 = pifSEPERATS1S3MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3MSS1 == randomValueSEPERATS1S3MSS1 )
                            {
                                validSEPERATS1S3MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3MSS1arrayRAND[i] = pifSEPERATS1S3MSS1array[randomValueSEPERATS1S3MSS1];
                pifSEPERATS1S3MSS1arrayRANDusedInd[i] = randomValueSEPERATS1S3MSS1;
                validSEPERATS1S3MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S3MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS1S3MSS2 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3MSS2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3MSS2 = pifSEPERATS1S3MSS2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3MSS2 == randomValueSEPERATS1S3MSS2 )
                            {
                                validSEPERATS1S3MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3MSS2arrayRAND[i] = pifSEPERATS1S3MSS2array[randomValueSEPERATS1S3MSS2];
                pifSEPERATS1S3MSS2arrayRANDusedInd[i] = randomValueSEPERATS1S3MSS2;
                validSEPERATS1S3MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S3MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS1S3MSS3 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3MSS3 = pifSEPERATS1S3MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3MSS3 == randomValueSEPERATS1S3MSS3 )
                            {
                                validSEPERATS1S3MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3MSS3arrayRAND[i] = pifSEPERATS1S3MSS3array[randomValueSEPERATS1S3MSS3];
                pifSEPERATS1S3MSS3arrayRANDusedInd[i] = randomValueSEPERATS1S3MSS3;
                validSEPERATS1S3MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S3MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S3MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS1S3MSS4 = floor((rand(countS1S3total) + 1));      
                        validSEPERATS1S3MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S3MSS4 = pifSEPERATS1S3MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S3MSS4 == randomValueSEPERATS1S3MSS4 )
                            {
                                validSEPERATS1S3MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S3MSS4arrayRAND[i] = pifSEPERATS1S3MSS4array[randomValueSEPERATS1S3MSS4];
                pifSEPERATS1S3MSS4arrayRANDusedInd[i] = randomValueSEPERATS1S3MSS4;
                validSEPERATS1S3MSS4 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS1S4MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS1S4MSS1 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4MSS1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4MSS1 = pifSEPERATS1S4MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4MSS1 == randomValueSEPERATS1S4MSS1 )
                            {
                                validSEPERATS1S4MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4MSS1arrayRAND[i] = pifSEPERATS1S4MSS1array[randomValueSEPERATS1S4MSS1];
                pifSEPERATS1S4MSS1arrayRANDusedInd[i] = randomValueSEPERATS1S4MSS1;
                validSEPERATS1S4MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS1S4MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS1S4MSS2 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4MSS2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4MSS2 = pifSEPERATS1S4MSS2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4MSS2 == randomValueSEPERATS1S4MSS2 )
                            {
                                validSEPERATS1S4MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4MSS2arrayRAND[i] = pifSEPERATS1S4MSS2array[randomValueSEPERATS1S4MSS2];
                pifSEPERATS1S4MSS2arrayRANDusedInd[i] = randomValueSEPERATS1S4MSS2;
                validSEPERATS1S4MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS1S4MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS1S4MSS3 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4MSS3 = pifSEPERATS1S4MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4MSS3 == randomValueSEPERATS1S4MSS3 )
                            {
                                validSEPERATS1S4MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4MSS3arrayRAND[i] = pifSEPERATS1S4MSS3array[randomValueSEPERATS1S4MSS3];
                pifSEPERATS1S4MSS3arrayRANDusedInd[i] = randomValueSEPERATS1S4MSS3;
                validSEPERATS1S4MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS1S4MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS1S4MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS1S4MSS4 = floor((rand(countS1S4total) + 1));      
                        validSEPERATS1S4MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS1S4MSS4 = pifSEPERATS1S4MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS1S4MSS4 == randomValueSEPERATS1S4MSS4 )
                            {
                                validSEPERATS1S4MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS1S4MSS4arrayRAND[i] = pifSEPERATS1S4MSS4array[randomValueSEPERATS1S4MSS4];
                pifSEPERATS1S4MSS4arrayRANDusedInd[i] = randomValueSEPERATS1S4MSS4;
                validSEPERATS1S4MSS4 = 0;
                }
			}

			if ( sensors2 == 7 )
			{
			    numeric pifSEPERATS2arrayCount = 1;
            	array pifSEPERATS2slaveETM1array[countS1S2total];
            	array pifSEPERATS2slaveETM2array[countS1S2total];
            	array pifSEPERATS2slaveETM3array[countS1S2total];
            	array pifSEPERATS2slaveETM4array[countS1S2total];
            	array pifSEPERATS2slaveETM5array[countS1S2total];
            	array pifSEPERATS2slaveETM7array[countS1S2total];
            
            	array pifSEPERATS2slaveETM1arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveETM2arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveETM3arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveETM4arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveETM5arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveETM7arrayRAND[minPifVAL];
            
			    for i = 1 to ETMlins
                {
                    	for j = 1 to ETMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2slaveETM1array[pifSEPERATS2arrayCount] = REFS2ETM1[i,j];                        
                                    pifSEPERATS2slaveETM2array[pifSEPERATS2arrayCount] = REFS2ETM2[i,j];                        
                                    pifSEPERATS2slaveETM3array[pifSEPERATS2arrayCount] = REFS2ETM3[i,j];
                            		pifSEPERATS2slaveETM4array[pifSEPERATS2arrayCount] = REFS2ETM4[i,j];
                                    pifSEPERATS2slaveETM5array[pifSEPERATS2arrayCount] = REFS2ETM5[i,j];
                            		pifSEPERATS2slaveETM7array[pifSEPERATS2arrayCount] = REFS2ETM7[i,j];
                                    pifSEPERATS2arrayCount = pifSEPERATS2arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1
        if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM1arrayRAND[i] = pifSEPERATS2slaveETM1array[pifSEPERATS1S2ETM1arrayRANDusedInd[i]];
		    }
		}
		
		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM1arrayRAND[i] = pifSEPERATS2slaveETM1array[pifSEPERATS1S2TM1arrayRANDusedInd[i]];
		        }
		 }
                
                ################ band 2
             
            if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM2arrayRAND[i] = pifSEPERATS2slaveETM2array[pifSEPERATS1S2ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM2arrayRAND[i] = pifSEPERATS2slaveETM2array[pifSEPERATS1S2TM2arrayRANDusedInd[i]];
                }
		}

                ################ band 3

            if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM3arrayRAND[i] = pifSEPERATS2slaveETM3array[pifSEPERATS1S2ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM3arrayRAND[i] = pifSEPERATS2slaveETM3array[pifSEPERATS1S2TM3arrayRANDusedInd[i]];
                }
		}
		
                ################ band 4

            if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM4arrayRAND[i] = pifSEPERATS2slaveETM4array[pifSEPERATS1S2ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM4arrayRAND[i] = pifSEPERATS2slaveETM4array[pifSEPERATS1S2TM4arrayRANDusedInd[i]];
                }
		}

                ################ band 5

            if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM5arrayRAND[i] = pifSEPERATS2slaveETM5array[pifSEPERATS1S2ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM5arrayRAND[i] = pifSEPERATS2slaveETM5array[pifSEPERATS1S2TM5arrayRANDusedInd[i]];
                }
		}

                ################ band 7

            if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM7arrayRAND[i] = pifSEPERATS2slaveETM7array[pifSEPERATS1S2ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveETM7arrayRAND[i] = pifSEPERATS2slaveETM7array[pifSEPERATS1S2TM7arrayRANDusedInd[i]];
                }
		}
	}

			else if ( sensors2 == 6 )
			{
			    numeric pifSEPERATS2arrayCount = 1;
            	array pifSEPERATS2slaveTM1array[countS1S2total];
            	array pifSEPERATS2slaveTM2array[countS1S2total];
            	array pifSEPERATS2slaveTM3array[countS1S2total];
            	array pifSEPERATS2slaveTM4array[countS1S2total];
            	array pifSEPERATS2slaveTM5array[countS1S2total];
            	array pifSEPERATS2slaveTM7array[countS1S2total];
            
            	array pifSEPERATS2slaveTM1arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveTM2arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveTM3arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveTM4arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveTM5arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveTM7arrayRAND[minPifVAL];
            
			 for i = 1 to TMlins
                	 {
                    	for j = 1 to TMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2slaveTM1array[pifSEPERATS2arrayCount] = REFS2TM1[i,j];                        
                                    pifSEPERATS2slaveTM2array[pifSEPERATS2arrayCount] = REFS2TM2[i,j];                        
                                    pifSEPERATS2slaveTM3array[pifSEPERATS2arrayCount] = REFS2TM3[i,j];
                            		pifSEPERATS2slaveTM4array[pifSEPERATS2arrayCount] = REFS2TM4[i,j];
                                    pifSEPERATS2slaveTM5array[pifSEPERATS2arrayCount] = REFS2TM5[i,j];
                            		pifSEPERATS2slaveTM7array[pifSEPERATS2arrayCount] = REFS2TM7[i,j];
                                    pifSEPERATS2arrayCount = pifSEPERATS2arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1
        
            if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM1arrayRAND[i] = pifSEPERATS2slaveTM1array[pifSEPERATS1S2ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM1arrayRAND[i] = pifSEPERATS2slaveTM1array[pifSEPERATS1S2TM1arrayRANDusedInd[i]];
                }
		}
                
                ################ band 2

            if ( sensors1 == 7 )
		{                          
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM2arrayRAND[i] = pifSEPERATS2slaveTM2array[pifSEPERATS1S2ETM2arrayRANDusedInd[i]];
                }
		}

            else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM2arrayRAND[i] = pifSEPERATS2slaveTM2array[pifSEPERATS1S2TM2arrayRANDusedInd[i]];
                }
		}

                ################ band 3

            if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM3arrayRAND[i] = pifSEPERATS2slaveTM3array[pifSEPERATS1S2ETM3arrayRANDusedInd[i]];
                }
		}

            else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM3arrayRAND[i] = pifSEPERATS2slaveTM3array[pifSEPERATS1S2TM3arrayRANDusedInd[i]];
                }
		}

                ################ band 4

            if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM4arrayRAND[i] = pifSEPERATS2slaveTM4array[pifSEPERATS1S2ETM4arrayRANDusedInd[i]];
                }
		}

            else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM4arrayRAND[i] = pifSEPERATS2slaveTM4array[pifSEPERATS1S2TM4arrayRANDusedInd[i]];
                }
		}

                ################ band 5

            if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM5arrayRAND[i] = pifSEPERATS2slaveTM5array[pifSEPERATS1S2ETM5arrayRANDusedInd[i]];
                }
		}

            else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM5arrayRAND[i] = pifSEPERATS2slaveTM5array[pifSEPERATS1S2TM5arrayRANDusedInd[i]];
                }                                     
		}

                ################ band 7
            if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM7arrayRAND[i] = pifSEPERATS2slaveTM7array[pifSEPERATS1S2ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveTM7arrayRAND[i] = pifSEPERATS2slaveTM7array[pifSEPERATS1S2TM7arrayRANDusedInd[i]];
                }
		}
	}

			else
			{
			numeric pifSEPERATS2arrayCount = 1;
            	array pifSEPERATS2slaveMSS1array[countS1S2total];
            	array pifSEPERATS2slaveMSS2array[countS1S2total];
            	array pifSEPERATS2slaveMSS3array[countS1S2total];
            	array pifSEPERATS2slaveMSS4array[countS1S2total];
            
            	array pifSEPERATS2slaveMSS1arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveMSS2arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveMSS3arrayRAND[minPifVAL];
            	array pifSEPERATS2slaveMSS4arrayRAND[minPifVAL];
            
            	            
			  for i = 1 to MSSlins
                	  {
                    	for j = 1 to MSScols
                    	{     
                            if ( S1MASK[i,j] == 1 && S2MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2slaveMSS1array[pifSEPERATS2arrayCount] = REFS2MSS1[i,j];                        
                                    pifSEPERATS2slaveMSS2array[pifSEPERATS2arrayCount] = REFS2MSS2[i,j];                        
                                    pifSEPERATS2slaveMSS3array[pifSEPERATS2arrayCount] = REFS2MSS3[i,j];
                            		pifSEPERATS2slaveMSS4array[pifSEPERATS2arrayCount] = REFS2MSS4[i,j];
                                    pifSEPERATS2arrayCount = pifSEPERATS2arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1

		if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS1arrayRAND[i] = pifSEPERATS2slaveMSS1array[pifSEPERATS1S2ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS1arrayRAND[i] = pifSEPERATS2slaveMSS1array[pifSEPERATS1S2TM2arrayRANDusedInd[i]];
                }
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS1arrayRAND[i] = pifSEPERATS2slaveMSS1array[pifSEPERATS1S2MSS1arrayRANDusedInd[i]];
                }
		}
                
                ################ band 2

		if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS2arrayRAND[i] = pifSEPERATS2slaveMSS2array[pifSEPERATS1S2ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS2arrayRAND[i] = pifSEPERATS2slaveMSS2array[pifSEPERATS1S2TM3arrayRANDusedInd[i]];
                }
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS2arrayRAND[i] = pifSEPERATS2slaveMSS2array[pifSEPERATS1S2MSS2arrayRANDusedInd[i]];
                }
		}
                
                ################ band 3
             
        if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS3arrayRAND[i] = pifSEPERATS2slaveMSS3array[pifSEPERATS1S2MSS3arrayRANDusedInd[i]];
                }
		}          
                
                ################ band 4

		if ( sensors1 == 7 )
		{                          
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS4arrayRAND[i] = pifSEPERATS2slaveMSS4array[pifSEPERATS1S2ETM4arrayRANDusedInd[i]];
                }
        }

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS4arrayRAND[i] = pifSEPERATS2slaveMSS4array[pifSEPERATS1S2TM4arrayRANDusedInd[i]];
                }
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS2slaveMSS4arrayRAND[i] = pifSEPERATS2slaveMSS4array[pifSEPERATS1S2MSS4arrayRANDusedInd[i]];
                }
		}
	}

			if ( sensors3 == 7 )
			{
			numeric pifSEPERATS3arrayCount = 1;
            	array pifSEPERATS3slaveETM1array[countS1S3total];
            	array pifSEPERATS3slaveETM2array[countS1S3total];
            	array pifSEPERATS3slaveETM3array[countS1S3total];
            	array pifSEPERATS3slaveETM4array[countS1S3total];
            	array pifSEPERATS3slaveETM5array[countS1S3total];
            	array pifSEPERATS3slaveETM7array[countS1S3total];
            
            	array pifSEPERATS3slaveETM1arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveETM2arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveETM3arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveETM4arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveETM5arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveETM7arrayRAND[minPifVAL];
            
			  for i = 1 to ETMlins
                	  {
                    	for j = 1 to ETMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                                    pifSEPERATS3slaveETM1array[pifSEPERATS3arrayCount] = REFS3ETM1[i,j];
                                    pifSEPERATS3slaveETM2array[pifSEPERATS3arrayCount] = REFS3ETM2[i,j]; 
                                    pifSEPERATS3slaveETM3array[pifSEPERATS3arrayCount] = REFS3ETM3[i,j];
                            		pifSEPERATS3slaveETM4array[pifSEPERATS3arrayCount] = REFS3ETM4[i,j];
                                    pifSEPERATS3slaveETM5array[pifSEPERATS3arrayCount] = REFS3ETM5[i,j];
                            		pifSEPERATS3slaveETM7array[pifSEPERATS3arrayCount] = REFS3ETM7[i,j];
                                    pifSEPERATS3arrayCount = pifSEPERATS3arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1

		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM1arrayRAND[i] = pifSEPERATS3slaveETM1array[pifSEPERATS1S3ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM1arrayRAND[i] = pifSEPERATS3slaveETM1array[pifSEPERATS1S3TM1arrayRANDusedInd[i]];
                }                        
		}
                
                ################ band 2

		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM2arrayRAND[i] = pifSEPERATS3slaveETM2array[pifSEPERATS1S3ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM2arrayRAND[i] = pifSEPERATS3slaveETM2array[pifSEPERATS1S3TM2arrayRANDusedInd[i]];
                }
		}

                ################ band 3

		if ( sensors1 == 7 )
		{                                                                 
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM3arrayRAND[i] = pifSEPERATS3slaveETM3array[pifSEPERATS1S3ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM3arrayRAND[i] = pifSEPERATS3slaveETM3array[pifSEPERATS1S3TM3arrayRANDusedInd[i]];
                }                                                   
		}

                ################ band 4

		if ( sensors1 == 7 )
		{                                                                              
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM4arrayRAND[i] = pifSEPERATS3slaveETM4array[pifSEPERATS1S3ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM4arrayRAND[i] = pifSEPERATS3slaveETM4array[pifSEPERATS1S3TM4arrayRANDusedInd[i]];
                }                                                                 
		}

                ################ band 5

		if ( sensors1 == 7 )
		{                                                                                           
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM5arrayRAND[i] = pifSEPERATS3slaveETM5array[pifSEPERATS1S3ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{  
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM5arrayRAND[i] = pifSEPERATS3slaveETM5array[pifSEPERATS1S3TM5arrayRANDusedInd[i]];
                }                                                                            
		}

                ################ band 7

		if ( sensors1 == 7 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM7arrayRAND[i] = pifSEPERATS3slaveETM7array[pifSEPERATS1S3ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveETM7arrayRAND[i] = pifSEPERATS3slaveETM7array[pifSEPERATS1S3TM7arrayRANDusedInd[i]];
                }                                                                                           
		}
	}

			else if ( sensors3 == 6 )
			{
			numeric pifSEPERATS3arrayCount = 1;
            	array pifSEPERATS3slaveTM1array[countS1S3total];
            	array pifSEPERATS3slaveTM2array[countS1S3total];
            	array pifSEPERATS3slaveTM3array[countS1S3total];
            	array pifSEPERATS3slaveTM4array[countS1S3total];
            	array pifSEPERATS3slaveTM5array[countS1S3total];
            	array pifSEPERATS3slaveTM7array[countS1S3total];
            
            	array pifSEPERATS3slaveTM1arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveTM2arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveTM3arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveTM4arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveTM5arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveTM7arrayRAND[minPifVAL];
            
			for i = 1 to TMlins
        	      {
                    	for j = 1 to TMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                                    pifSEPERATS3slaveTM1array[pifSEPERATS3arrayCount] = REFS3TM1[i,j];                        
                                    pifSEPERATS3slaveTM2array[pifSEPERATS3arrayCount] = REFS3TM2[i,j];                        
                                    pifSEPERATS3slaveTM3array[pifSEPERATS3arrayCount] = REFS3TM3[i,j];
                            		pifSEPERATS3slaveTM4array[pifSEPERATS3arrayCount] = REFS3TM4[i,j];
                                    pifSEPERATS3slaveTM5array[pifSEPERATS3arrayCount] = REFS3TM5[i,j];
                            		pifSEPERATS3slaveTM7array[pifSEPERATS3arrayCount] = REFS3TM7[i,j];
                                    pifSEPERATS3arrayCount = pifSEPERATS3arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1

		if ( sensors1 == 7 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM1arrayRAND[i] = pifSEPERATS3slaveTM1array[pifSEPERATS1S3ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM1arrayRAND[i] = pifSEPERATS3slaveTM1array[pifSEPERATS1S3TM1arrayRANDusedInd[i]];
                }
		} 
               
                ################ band 2
             
		if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM2arrayRAND[i] = pifSEPERATS3slaveTM2array[pifSEPERATS1S3ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM2arrayRAND[i] = pifSEPERATS3slaveTM2array[pifSEPERATS1S3TM2arrayRANDusedInd[i]];
                }
		}

                ################ band 3
             
		if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM3arrayRAND[i] = pifSEPERATS3slaveTM3array[pifSEPERATS1S3ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM3arrayRAND[i] = pifSEPERATS3slaveTM3array[pifSEPERATS1S3TM3arrayRANDusedInd[i]];
                }
		}

                ################ band 4
             
		if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM4arrayRAND[i] = pifSEPERATS3slaveTM4array[pifSEPERATS1S3ETM4arrayRANDusedInd[i]];
                }
             }

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM4arrayRAND[i] = pifSEPERATS3slaveTM4array[pifSEPERATS1S3TM4arrayRANDusedInd[i]];
                }
		}

                ################ band 5
             
		if ( sensors1 == 7 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM5arrayRAND[i] = pifSEPERATS3slaveTM5array[pifSEPERATS1S3ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM5arrayRAND[i] = pifSEPERATS3slaveTM5array[pifSEPERATS1S3TM5arrayRANDusedInd[i]];
                }
		}

                ################ band 7

		if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM7arrayRAND[i] = pifSEPERATS3slaveTM7array[pifSEPERATS1S3ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveTM7arrayRAND[i] = pifSEPERATS3slaveTM7array[pifSEPERATS1S3TM7arrayRANDusedInd[i]];
                }
		}
	}

			else
			{
			numeric pifSEPERATS3arrayCount = 1;
            	array pifSEPERATS3slaveMSS1array[countS1S3total];
            	array pifSEPERATS3slaveMSS2array[countS1S3total];
            	array pifSEPERATS3slaveMSS3array[countS1S3total];
            	array pifSEPERATS3slaveMSS4array[countS1S3total];
            
            	array pifSEPERATS3slaveMSS1arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveMSS2arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveMSS3arrayRAND[minPifVAL];
            	array pifSEPERATS3slaveMSS4arrayRAND[minPifVAL];
            
			  for i = 1 to TMlins
                	  {
                    	for j = 1 to TMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                                    pifSEPERATS3slaveMSS1array[pifSEPERATS3arrayCount] = REFS3MSS1[i,j];                        
                                    pifSEPERATS3slaveMSS2array[pifSEPERATS3arrayCount] = REFS3MSS2[i,j];                        
                                    pifSEPERATS3slaveMSS3array[pifSEPERATS3arrayCount] = REFS3MSS3[i,j];
                            		pifSEPERATS3slaveMSS4array[pifSEPERATS3arrayCount] = REFS3MSS4[i,j];
                                    pifSEPERATS3arrayCount = pifSEPERATS3arrayCount + 1;
                            }
                    	}
                	  }
                	  
                	  ################ band 1
             
		if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS1arrayRAND[i] = pifSEPERATS3slaveMSS1array[pifSEPERATS1S3ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS1arrayRAND[i] = pifSEPERATS3slaveMSS1array[pifSEPERATS1S3TM2arrayRANDusedInd[i]];
                }           
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS1arrayRAND[i] = pifSEPERATS3slaveMSS1array[pifSEPERATS1S3MSS1arrayRANDusedInd[i]];
                }           
		}
                
                ################ band 2
             
		if ( sensors1 == 7 )
		{             
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS2arrayRAND[i] = pifSEPERATS3slaveMSS2array[pifSEPERATS1S3ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS2arrayRAND[i] = pifSEPERATS3slaveMSS2array[pifSEPERATS1S3TM3arrayRANDusedInd[i]];
                }
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS2arrayRAND[i] = pifSEPERATS3slaveMSS2array[pifSEPERATS1S3MSS2arrayRANDusedInd[i]];
                }
		}             
                
                ################ band 3

        if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
		{                          
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS3arrayRAND[i] = pifSEPERATS3slaveMSS3array[pifSEPERATS1S3MSS3arrayRANDusedInd[i]];
                }
		}
                
                ################ band 4

		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS4arrayRAND[i] = pifSEPERATS3slaveMSS4array[pifSEPERATS1S3ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS4arrayRAND[i] = pifSEPERATS3slaveMSS4array[pifSEPERATS1S3TM4arrayRANDusedInd[i]];
                }                          
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS3slaveMSS4arrayRAND[i] = pifSEPERATS3slaveMSS4array[pifSEPERATS1S3MSS4arrayRANDusedInd[i]];
                }
		}
	}

			if ( sensors4 == 7 )
			{
			numeric pifSEPERATS4arrayCount = 1;
            	array pifSEPERATS4slaveETM1array[countS1S4total];
            	array pifSEPERATS4slaveETM2array[countS1S4total];
            	array pifSEPERATS4slaveETM3array[countS1S4total];
            	array pifSEPERATS4slaveETM4array[countS1S4total];
            	array pifSEPERATS4slaveETM5array[countS1S4total];
            	array pifSEPERATS4slaveETM7array[countS1S4total];
            
            	array pifSEPERATS4slaveETM1arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveETM2arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveETM3arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveETM4arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveETM5arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveETM7arrayRAND[minPifVAL];
            
			  for i = 1 to ETMlins
                	  {
                    	for j = 1 to ETMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                                    pifSEPERATS4slaveETM1array[pifSEPERATS4arrayCount] = REFS4ETM1[i,j];
                                    pifSEPERATS4slaveETM2array[pifSEPERATS4arrayCount] = REFS4ETM2[i,j]; 
                                    pifSEPERATS4slaveETM3array[pifSEPERATS4arrayCount] = REFS4ETM3[i,j];
                            		pifSEPERATS4slaveETM4array[pifSEPERATS4arrayCount] = REFS4ETM4[i,j];
                                    pifSEPERATS4slaveETM5array[pifSEPERATS4arrayCount] = REFS4ETM5[i,j];
                            		pifSEPERATS4slaveETM7array[pifSEPERATS4arrayCount] = REFS4ETM7[i,j];
                                    pifSEPERATS4arrayCount = pifSEPERATS4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                	  ################ band 1
             
		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM1arrayRAND[i] = pifSEPERATS4slaveETM1array[pifSEPERATS1S4ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM1arrayRAND[i] = pifSEPERATS4slaveETM1array[pifSEPERATS1S4TM1arrayRANDusedInd[i]];
                }                                       
		}

                ################ band 2
             
		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM2arrayRAND[i] = pifSEPERATS4slaveETM2array[pifSEPERATS1S4ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM2arrayRAND[i] = pifSEPERATS4slaveETM2array[pifSEPERATS1S4TM2arrayRANDusedInd[i]];
                }                                       
		}

                ################ band 3
             
		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM3arrayRAND[i] = pifSEPERATS4slaveETM3array[pifSEPERATS1S4ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM3arrayRAND[i] = pifSEPERATS4slaveETM3array[pifSEPERATS1S4TM3arrayRANDusedInd[i]];
                }                                      
		}

                ################ band 4
             
		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM4arrayRAND[i] = pifSEPERATS4slaveETM4array[pifSEPERATS1S4ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{  
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM4arrayRAND[i] = pifSEPERATS4slaveETM4array[pifSEPERATS1S4TM4arrayRANDusedInd[i]];
                }                                     
		}

                ################ band 5
             
		if ( sensors1 == 7 )
		{                                       
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM5arrayRAND[i] = pifSEPERATS4slaveETM5array[pifSEPERATS1S4ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM5arrayRAND[i] = pifSEPERATS4slaveETM5array[pifSEPERATS1S4TM5arrayRANDusedInd[i]];
                }                                      
		}

                ################ band 7

		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM7arrayRAND[i] = pifSEPERATS4slaveETM7array[pifSEPERATS1S4ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{  
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveETM7arrayRAND[i] = pifSEPERATS4slaveETM7array[pifSEPERATS1S4TM7arrayRANDusedInd[i]];
                }                                     
		}
	}

			else if ( sensors4 == 6 )
			{
			numeric pifSEPERATS4arrayCount = 1;
            	array pifSEPERATS4slaveTM1array[countS1S4total];
            	array pifSEPERATS4slaveTM2array[countS1S4total];
            	array pifSEPERATS4slaveTM3array[countS1S4total];
            	array pifSEPERATS4slaveTM4array[countS1S4total];
            	array pifSEPERATS4slaveTM5array[countS1S4total];
            	array pifSEPERATS4slaveTM7array[countS1S4total];
            
            	array pifSEPERATS4slaveTM1arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveTM2arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveTM3arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveTM4arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveTM5arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveTM7arrayRAND[minPifVAL];
            
			  for i = 1 to TMlins
                	  {
                    	for j = 1 to TMcols
                    	{     
                            if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                                    pifSEPERATS4slaveTM1array[pifSEPERATS4arrayCount] = REFS4TM1[i,j];
                                    pifSEPERATS4slaveTM2array[pifSEPERATS4arrayCount] = REFS4TM2[i,j]; 
                                    pifSEPERATS4slaveTM3array[pifSEPERATS4arrayCount] = REFS4TM3[i,j];
                            		pifSEPERATS4slaveTM4array[pifSEPERATS4arrayCount] = REFS4TM4[i,j];
                                    pifSEPERATS4slaveTM5array[pifSEPERATS4arrayCount] = REFS4TM5[i,j];
                            		pifSEPERATS4slaveTM7array[pifSEPERATS4arrayCount] = REFS4TM7[i,j];
                                    pifSEPERATS4arrayCount = pifSEPERATS4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                	  ################ band 1
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM1arrayRAND[i] = pifSEPERATS4slaveTM1array[pifSEPERATS1S4ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM1arrayRAND[i] = pifSEPERATS4slaveTM1array[pifSEPERATS1S4TM1arrayRANDusedInd[i]];
                }                                                  
		}

                ################ band 2
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM2arrayRAND[i] = pifSEPERATS4slaveTM2array[pifSEPERATS1S4ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{     
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM2arrayRAND[i] = pifSEPERATS4slaveTM2array[pifSEPERATS1S4TM2arrayRANDusedInd[i]];
                }                                               
		}

                ################ band 3
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM3arrayRAND[i] = pifSEPERATS4slaveTM3array[pifSEPERATS1S4ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{   
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM3arrayRAND[i] = pifSEPERATS4slaveTM3array[pifSEPERATS1S4TM3arrayRANDusedInd[i]];
                }                                                 
		}

                ################ band 4
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM4arrayRAND[i] = pifSEPERATS4slaveTM4array[pifSEPERATS1S4ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{   
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM4arrayRAND[i] = pifSEPERATS4slaveTM4array[pifSEPERATS1S4TM4arrayRANDusedInd[i]];
                }                                                 
		}

                ################ band 5
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM5arrayRAND[i] = pifSEPERATS4slaveTM5array[pifSEPERATS1S4ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM5arrayRAND[i] = pifSEPERATS4slaveTM5array[pifSEPERATS1S4TM5arrayRANDusedInd[i]];
                }                                                    
		}

                ################ band 7
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM7arrayRAND[i] = pifSEPERATS4slaveTM7array[pifSEPERATS1S4ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{      
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveTM7arrayRAND[i] = pifSEPERATS4slaveTM7array[pifSEPERATS1S4TM7arrayRANDusedInd[i]];
                }                                              
		}
	}

			else
			{
			numeric pifSEPERATS4arrayCount = 1;
            	array pifSEPERATS4slaveMSS1array[countS1S4total];
            	array pifSEPERATS4slaveMSS2array[countS1S4total];
            	array pifSEPERATS4slaveMSS3array[countS1S4total];
            	array pifSEPERATS4slaveMSS4array[countS1S4total];
            
            	array pifSEPERATS4slaveMSS1arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveMSS2arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveMSS3arrayRAND[minPifVAL];
            	array pifSEPERATS4slaveMSS4arrayRAND[minPifVAL];
            
			  for i = 1 to MSSlins
                	  {
                    	for j = 1 to MSScols
                    	{     
                            if ( S1MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                                    pifSEPERATS4slaveMSS1array[pifSEPERATS4arrayCount] = REFS4MSS1[i,j];
                                    pifSEPERATS4slaveMSS2array[pifSEPERATS4arrayCount] = REFS4MSS2[i,j]; 
                                    pifSEPERATS4slaveMSS3array[pifSEPERATS4arrayCount] = REFS4MSS3[i,j];
                            		pifSEPERATS4slaveMSS4array[pifSEPERATS4arrayCount] = REFS4MSS4[i,j];
                                    pifSEPERATS4arrayCount = pifSEPERATS4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS1arrayRAND[i] = pifSEPERATS4slaveMSS1array[pifSEPERATS1S4ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{   
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS1arrayRAND[i] = pifSEPERATS4slaveMSS1array[pifSEPERATS1S4TM2arrayRANDusedInd[i]];
                }                                                 
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS1arrayRAND[i] = pifSEPERATS4slaveMSS1array[pifSEPERATS1S4MSS1arrayRANDusedInd[i]];
                }
		}
                
                ################ band 2
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS2arrayRAND[i] = pifSEPERATS4slaveMSS2array[pifSEPERATS1S4ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{  
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS2arrayRAND[i] = pifSEPERATS4slaveMSS2array[pifSEPERATS1S4TM3arrayRANDusedInd[i]];
                }                                                  
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS2arrayRAND[i] = pifSEPERATS4slaveMSS2array[pifSEPERATS1S4MSS2arrayRANDusedInd[i]];
                }
		}
                
                ################ band 3
             
        if ( sensors1 == 5 or sensors1 == 4 or sensors1 == 3 or sensors1 == 2 or sensors1 == 1 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS3arrayRAND[i] = pifSEPERATS4slaveMSS3array[pifSEPERATS1S4MSS3arrayRANDusedInd[i]];
                }
		}
                
                ################ band 4
             
		if ( sensors1 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS4arrayRAND[i] = pifSEPERATS4slaveMSS4array[pifSEPERATS1S4ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors1 == 6 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS4arrayRAND[i] = pifSEPERATS4slaveMSS4array[pifSEPERATS1S4TM4arrayRANDusedInd[i]];
                }                                                   
		}

		else
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS4slaveMSS4arrayRAND[i] = pifSEPERATS4slaveMSS4array[pifSEPERATS1S4MSS4arrayRANDusedInd[i]];
                }
		}
	  }
	}

            if ( pifSEPERATS1S2arrayCount < minPifVAL )
            {
               printf("For the PIF mask combination Scene 1 and Scene 2 %5i pixels were needed, but only %5i were found.\n", minPifVAL, pifSEPERATS1S2arrayCount);
            }
            
            if ( pifSEPERATS1S3arrayCount < minPifVAL )
            {
               printf("For the PIF mask combination Scene 1 and Scene 3 %5i pixels were needed, but only %5i were found.\n", minPifVAL, pifSEPERATS1S3arrayCount);
            }
            
            if ( pifSEPERATS1S4arrayCount < minPifVAL )
            {
               printf("For the PIF mask combination Scene 1 and Scene 4 %5i pixels were needed, but only %5i were found.\n", minPifVAL, pifSEPERATS1S4arrayCount);
            }
            if ( pifSEPERATS1S2arrayCount < minPifVAL && pifSEPERATS1S3arrayCount < minPifVAL && pifSEPERATS1S4arrayCount < minPifVAL )
            {
                printf("All PIF-masks have a smaller pixelcount than minPifVAL requires... %5i pixels are needed for each raster... Quitting...\n\n", minPifVAL);
                Exit();
            }
    }
    
    else if ( slaveMaster == 2 )
    {
	    if ( countS2S1total >= minPifVAL && countS2S3total >= minPifVAL && countS2S4total >= minPifVAL )
          {

			if ( sensors2 == 7 )
			{
			numeric pifSEPERATS2S1arrayCount = 1;
			numeric pifSEPERATS2S3arrayCount = 1;
			numeric pifSEPERATS2S4arrayCount = 1;

            	array pifSEPERATS2S1ETM1array[countS2S1total];
            	array pifSEPERATS2S1ETM2array[countS2S1total];
            	array pifSEPERATS2S1ETM3array[countS2S1total];
            	array pifSEPERATS2S1ETM4array[countS2S1total];
            	array pifSEPERATS2S1ETM5array[countS2S1total];
            	array pifSEPERATS2S1ETM7array[countS2S1total];
            
            	array pifSEPERATS2S1ETM1arrayRAND[minPifVAL];
                array pifSEPERATS2S1ETM2arrayRAND[minPifVAL];
                array pifSEPERATS2S1ETM3arrayRAND[minPifVAL];
                array pifSEPERATS2S1ETM4arrayRAND[minPifVAL];
                array pifSEPERATS2S1ETM5arrayRAND[minPifVAL];
                array pifSEPERATS2S1ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS2S1ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S1ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S1ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S1ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S1ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S1ETM7arrayRANDusedInd[minPifVAL];


            	array pifSEPERATS2S3ETM1array[countS2S3total];
            	array pifSEPERATS2S3ETM2array[countS2S3total];
            	array pifSEPERATS2S3ETM3array[countS2S3total];
            	array pifSEPERATS2S3ETM4array[countS2S3total];
            	array pifSEPERATS2S3ETM5array[countS2S3total];
            	array pifSEPERATS2S3ETM7array[countS2S3total];
            
            	array pifSEPERATS2S3ETM1arrayRAND[minPifVAL];
                array pifSEPERATS2S3ETM2arrayRAND[minPifVAL];
                array pifSEPERATS2S3ETM3arrayRAND[minPifVAL];
                array pifSEPERATS2S3ETM4arrayRAND[minPifVAL];
                array pifSEPERATS2S3ETM5arrayRAND[minPifVAL];
                array pifSEPERATS2S3ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS2S3ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3ETM7arrayRANDusedInd[minPifVAL];
                

            	array pifSEPERATS2S4ETM1array[countS2S4total];
            	array pifSEPERATS2S4ETM2array[countS2S4total];
            	array pifSEPERATS2S4ETM3array[countS2S4total];
            	array pifSEPERATS2S4ETM4array[countS2S4total];
            	array pifSEPERATS2S4ETM5array[countS2S4total];
            	array pifSEPERATS2S4ETM7array[countS2S4total];
            
            	array pifSEPERATS2S4ETM1arrayRAND[minPifVAL];
                array pifSEPERATS2S4ETM2arrayRAND[minPifVAL];
                array pifSEPERATS2S4ETM3arrayRAND[minPifVAL];
                array pifSEPERATS2S4ETM4arrayRAND[minPifVAL];
                array pifSEPERATS2S4ETM5arrayRAND[minPifVAL];
                array pifSEPERATS2S4ETM7arrayRAND[minPifVAL];
    
                array pifSEPERATS2S4ETM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4ETM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4ETM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4ETM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4ETM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4ETM7arrayRANDusedInd[minPifVAL];

                	  for i = 1 to ETMlins
                	  {
                    	for j = 1 to ETMcols
                    	{     
                            if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2S1ETM1array[pifSEPERATS2S1arrayCount] = REFS2ETM1[i,j];                        
                                        pifSEPERATS2S1ETM2array[pifSEPERATS2S1arrayCount] = REFS2ETM2[i,j];                        
                                    pifSEPERATS2S1ETM3array[pifSEPERATS2S1arrayCount] = REFS2ETM3[i,j];
                            pifSEPERATS2S1ETM4array[pifSEPERATS2S1arrayCount] = REFS2ETM4[i,j];
                                    pifSEPERATS2S1ETM5array[pifSEPERATS2S1arrayCount] = REFS2ETM5[i,j];
                            pifSEPERATS2S1ETM7array[pifSEPERATS2S1arrayCount] = REFS2ETM7[i,j];
                                    pifSEPERATS2S1arrayCount = pifSEPERATS2S1arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S3ETM1array[pifSEPERATS2S3arrayCount] = REFS2ETM1[i,j];                        
                                        pifSEPERATS2S3ETM2array[pifSEPERATS2S3arrayCount] = REFS2ETM2[i,j];                        
                                    pifSEPERATS2S3ETM3array[pifSEPERATS2S3arrayCount] = REFS2ETM3[i,j];
                            pifSEPERATS2S3ETM4array[pifSEPERATS2S3arrayCount] = REFS2ETM4[i,j];
                                    pifSEPERATS2S3ETM5array[pifSEPERATS2S3arrayCount] = REFS2ETM5[i,j];
                            pifSEPERATS2S3ETM7array[pifSEPERATS2S3arrayCount] = REFS2ETM7[i,j];
                            pifSEPERATS2S3arrayCount = pifSEPERATS2S3arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S4ETM1array[pifSEPERATS2S4arrayCount] = REFS2ETM1[i,j];                        
                                        pifSEPERATS2S4ETM2array[pifSEPERATS2S4arrayCount] = REFS2ETM2[i,j];                        
                                    pifSEPERATS2S4ETM3array[pifSEPERATS2S4arrayCount] = REFS2ETM3[i,j];
                            pifSEPERATS2S4ETM4array[pifSEPERATS2S4arrayCount] = REFS2ETM4[i,j];
                                    pifSEPERATS2S4ETM5array[pifSEPERATS2S4arrayCount] = REFS2ETM5[i,j];
                            pifSEPERATS2S4ETM7array[pifSEPERATS2S4arrayCount] = REFS2ETM7[i,j];
                            pifSEPERATS2S4arrayCount = pifSEPERATS2S4arrayCount + 1;
                            }
                    	}
                	  }
            
            ############# band 1
            
                numeric validSEPERATS2S1ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM1 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM1 = pifSEPERATS2S1ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM1 == randomValueSEPERATS2S1ETM1 )
                            {
                                validSEPERATS2S1ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM1arrayRAND[i] = pifSEPERATS2S1ETM1array[randomValueSEPERATS2S1ETM1];
                pifSEPERATS2S1ETM1arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM1;
                validSEPERATS2S1ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S1ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM2 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM2 = pifSEPERATS2S1ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM2 == randomValueSEPERATS2S1ETM2 )
                            {
                                validSEPERATS2S1ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM2arrayRAND[i] = pifSEPERATS2S1ETM2array[randomValueSEPERATS2S1ETM2];
                pifSEPERATS2S1ETM2arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM2;
                validSEPERATS2S1ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S1ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM3 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM3 = pifSEPERATS2S1ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM3 == randomValueSEPERATS2S1ETM3 )
                            {
                                validSEPERATS2S1ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM3arrayRAND[i] = pifSEPERATS2S1ETM3array[randomValueSEPERATS2S1ETM3];
                pifSEPERATS2S1ETM3arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM3;
                validSEPERATS2S1ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S1ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM4 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM4 = pifSEPERATS2S1ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM4 == randomValueSEPERATS2S1ETM4 )
                            {
                                validSEPERATS2S1ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM4arrayRAND[i] = pifSEPERATS2S1ETM4array[randomValueSEPERATS2S1ETM4];
                pifSEPERATS2S1ETM4arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM4;
                validSEPERATS2S1ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S1ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM5 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM5 = pifSEPERATS2S1ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM5 == randomValueSEPERATS2S1ETM5 )
                            {
                                validSEPERATS2S1ETM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM5arrayRAND[i] = pifSEPERATS2S1ETM5array[randomValueSEPERATS2S1ETM5];
                pifSEPERATS2S1ETM5arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM5;
                validSEPERATS2S1ETM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS2S1ETM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1ETM7 == 0 )
                    {
                        numeric randomValueSEPERATS2S1ETM7 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1ETM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1ETM7 = pifSEPERATS2S1ETM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1ETM7 == randomValueSEPERATS2S1ETM7 )
                            {
                                validSEPERATS2S1ETM7 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1ETM7arrayRAND[i] = pifSEPERATS2S1ETM7array[randomValueSEPERATS2S1ETM7];
                pifSEPERATS2S1ETM7arrayRANDusedInd[i] = randomValueSEPERATS2S1ETM7;
                validSEPERATS2S1ETM7 = 0;
                }
            
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS2S3ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM1 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM1 = pifSEPERATS2S3ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM1 == randomValueSEPERATS2S3ETM1 )
                            {
                                validSEPERATS2S3ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM1arrayRAND[i] = pifSEPERATS2S3ETM1array[randomValueSEPERATS2S3ETM1];
                pifSEPERATS2S3ETM1arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM1;
                validSEPERATS2S3ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S3ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM2 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM2 = pifSEPERATS2S3ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM2 == randomValueSEPERATS2S3ETM2 )
                            {
                                validSEPERATS2S3ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM2arrayRAND[i] = pifSEPERATS2S3ETM2array[randomValueSEPERATS2S3ETM2];
                pifSEPERATS2S3ETM2arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM2;
                validSEPERATS2S3ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S3ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM3 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM3 = pifSEPERATS2S3ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM3 == randomValueSEPERATS2S3ETM3 )
                            {
                                validSEPERATS2S3ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM3arrayRAND[i] = pifSEPERATS2S3ETM3array[randomValueSEPERATS2S3ETM3];
                pifSEPERATS2S3ETM3arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM3;
                validSEPERATS2S3ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S3ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM4 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM4 = pifSEPERATS2S3ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM4 == randomValueSEPERATS2S3ETM4 )
                            {
                                validSEPERATS2S3ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM4arrayRAND[i] = pifSEPERATS2S3ETM4array[randomValueSEPERATS2S3ETM4];
                pifSEPERATS2S3ETM4arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM4;
                validSEPERATS2S3ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S3ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM5 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM5 = pifSEPERATS2S3ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM5 == randomValueSEPERATS2S3ETM5 )
                            {
                                validSEPERATS2S3ETM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM5arrayRAND[i] = pifSEPERATS2S3ETM5array[randomValueSEPERATS2S3ETM5];
                pifSEPERATS2S3ETM5arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM5;
                validSEPERATS2S3ETM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS2S3ETM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3ETM7 == 0 )
                    {
                        numeric randomValueSEPERATS2S3ETM7 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3ETM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3ETM7 = pifSEPERATS2S3ETM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3ETM7 == randomValueSEPERATS2S3ETM7 )
                            {
                                validSEPERATS2S3ETM7 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3ETM7arrayRAND[i] = pifSEPERATS2S3ETM7array[randomValueSEPERATS2S3ETM7];
                pifSEPERATS2S3ETM7arrayRANDusedInd[i] = randomValueSEPERATS2S3ETM7;
                validSEPERATS2S3ETM7 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS2S4ETM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4ETM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S4ETM1 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4ETM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4ETM1 = pifSEPERATS2S4ETM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4ETM1 == randomValueSEPERATS2S4ETM1 )
                            {
                                validSEPERATS2S4ETM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4ETM1arrayRAND[i] = pifSEPERATS2S4ETM1array[randomValueSEPERATS2S4ETM1];
                pifSEPERATS2S4ETM1arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM1;
                validSEPERATS2S4ETM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S4ETM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4ETM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S4ETM2 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4ETM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4ETM2 = pifSEPERATS2S4ETM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4ETM2 == randomValueSEPERATS2S4ETM2 )
                            {
                                validSEPERATS2S4ETM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4ETM2arrayRAND[i] = pifSEPERATS2S4ETM2array[randomValueSEPERATS2S4ETM2];
                pifSEPERATS2S4ETM2arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM2;
                validSEPERATS2S4ETM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S4ETM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4ETM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S4ETM3 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4ETM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4ETM3 = pifSEPERATS2S4ETM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4ETM3 == randomValueSEPERATS2S4ETM3 )
                            {
                                validSEPERATS2S4ETM3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4ETM3arrayRAND[i] = pifSEPERATS2S4ETM3array[randomValueSEPERATS2S4ETM3];
                pifSEPERATS2S4ETM3arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM3;
                validSEPERATS2S4ETM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S4ETM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4ETM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S4ETM4 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4ETM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4ETM4 = pifSEPERATS2S4ETM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4ETM4 == randomValueSEPERATS2S4ETM4 )
                            {
                                validSEPERATS2S4ETM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4ETM4arrayRAND[i] = pifSEPERATS2S4ETM4array[randomValueSEPERATS2S4ETM4];
                pifSEPERATS2S4ETM4arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM4;
                validSEPERATS2S4ETM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S4ETM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4ETM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S4ETM5 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4ETM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4ETM5 = pifSEPERATS2S4ETM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4ETM5 == randomValueSEPERATS2S4ETM5 )
                            {
                                validSEPERATS2S4ETM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4ETM5arrayRAND[i] = pifSEPERATS2S4ETM5array[randomValueSEPERATS2S4ETM5];
                pifSEPERATS2S4ETM5arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM5;
                validSEPERATS2S4ETM5 = 0;
                }
            
            ################ band 7
                
                    numeric validSEPERATS2S4ETM7 = 0;
                 
                    for i = 1 to minPifVAL
                    {
                        while ( validSEPERATS2S4ETM7 == 0 )
                        {
                            numeric randomValueSEPERATS2S4ETM7 = floor((rand(countS2S4total) + 1));      
                            validSEPERATS2S4ETM7 = 1;
                
                            for j = 1 to (i-1)
                            {
                                local numeric testValueSEPERATS2S4ETM7 = pifSEPERATS2S4ETM7arrayRANDusedInd[j];
                            
                                if ( testValueSEPERATS2S4ETM7 == randomValueSEPERATS2S4ETM7 )
                                {
                                    validSEPERATS2S4ETM7 = 0;
                                }
                            }
                        }
                    pifSEPERATS2S4ETM7arrayRAND[i] = pifSEPERATS2S4ETM7array[randomValueSEPERATS2S4ETM7];
                    pifSEPERATS2S4ETM7arrayRANDusedInd[i] = randomValueSEPERATS2S4ETM7;
                    validSEPERATS2S4ETM7 = 0;
                    }
			}

			else if ( sensors2 == 6 )
			{
			numeric pifSEPERATS2S1arrayCount = 1;
			numeric pifSEPERATS2S3arrayCount = 1;
			numeric pifSEPERATS2S4arrayCount = 1;

            	array pifSEPERATS2S1TM1array[countS2S1total];
            	array pifSEPERATS2S1TM2array[countS2S1total];
            	array pifSEPERATS2S1TM3array[countS2S1total];
            	array pifSEPERATS2S1TM4array[countS2S1total];
            	array pifSEPERATS2S1TM5array[countS2S1total];
            	array pifSEPERATS2S1TM7array[countS2S1total];
            
            	array pifSEPERATS2S1TM1arrayRAND[minPifVAL];
            	array pifSEPERATS2S1TM2arrayRAND[minPifVAL];
            	array pifSEPERATS2S1TM3arrayRAND[minPifVAL];
            	array pifSEPERATS2S1TM4arrayRAND[minPifVAL];
            	array pifSEPERATS2S1TM5arrayRAND[minPifVAL];
            	array pifSEPERATS2S1TM7arrayRAND[minPifVAL];
            
            	array pifSEPERATS2S1TM1arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1TM2arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1TM3arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1TM4arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1TM5arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1TM7arrayRANDusedInd[minPifVAL];
            

            	array pifSEPERATS2S3TM1array[countS2S3total];
            	array pifSEPERATS2S3TM2array[countS2S3total];
            	array pifSEPERATS2S3TM3array[countS2S3total];
            	array pifSEPERATS2S3TM4array[countS2S3total];
            	array pifSEPERATS2S3TM5array[countS2S3total];
            	array pifSEPERATS2S3TM7array[countS2S3total];
            
            	array pifSEPERATS2S3TM1arrayRAND[minPifVAL];
                array pifSEPERATS2S3TM2arrayRAND[minPifVAL];
                array pifSEPERATS2S3TM3arrayRAND[minPifVAL];
                array pifSEPERATS2S3TM4arrayRAND[minPifVAL];
                array pifSEPERATS2S3TM5arrayRAND[minPifVAL];
                array pifSEPERATS2S3TM7arrayRAND[minPifVAL];
    
                array pifSEPERATS2S3TM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3TM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3TM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3TM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3TM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3TM7arrayRANDusedInd[minPifVAL];
                

            	array pifSEPERATS2S4TM1array[countS2S4total];
            	array pifSEPERATS2S4TM2array[countS2S4total];
            	array pifSEPERATS2S4TM3array[countS2S4total];
            	array pifSEPERATS2S4TM4array[countS2S4total];
            	array pifSEPERATS2S4TM5array[countS2S4total];
            	array pifSEPERATS2S4TM7array[countS2S4total];
            
            	array pifSEPERATS2S4TM1arrayRAND[minPifVAL];
                array pifSEPERATS2S4TM2arrayRAND[minPifVAL];
                array pifSEPERATS2S4TM3arrayRAND[minPifVAL];
                array pifSEPERATS2S4TM4arrayRAND[minPifVAL];
                array pifSEPERATS2S4TM5arrayRAND[minPifVAL];
                array pifSEPERATS2S4TM7arrayRAND[minPifVAL];
    
                array pifSEPERATS2S4TM1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4TM2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4TM3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4TM4arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4TM5arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4TM7arrayRANDusedInd[minPifVAL];

                	  for i = 1 to TMlins
                	  {
                    	for j = 1 to TMcols
                    	{     
                            if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2S1TM1array[pifSEPERATS2S1arrayCount] = REFS2TM1[i,j];                        
                                        pifSEPERATS2S1TM2array[pifSEPERATS2S1arrayCount] = REFS2TM2[i,j];                        
                                    pifSEPERATS2S1TM3array[pifSEPERATS2S1arrayCount] = REFS2TM3[i,j];
                            pifSEPERATS2S1TM4array[pifSEPERATS2S1arrayCount] = REFS2TM4[i,j];
                                    pifSEPERATS2S1TM5array[pifSEPERATS2S1arrayCount] = REFS2TM5[i,j];
                            pifSEPERATS2S1TM7array[pifSEPERATS2S1arrayCount] = REFS2TM7[i,j];
                                    pifSEPERATS2S1arrayCount = pifSEPERATS2S1arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S3TM1array[pifSEPERATS2S3arrayCount] = REFS2TM1[i,j];                        
                                        pifSEPERATS2S3TM2array[pifSEPERATS2S3arrayCount] = REFS2TM2[i,j];                        
                                    pifSEPERATS2S3TM3array[pifSEPERATS2S3arrayCount] = REFS2TM3[i,j];
                            pifSEPERATS2S3TM4array[pifSEPERATS2S3arrayCount] = REFS2TM4[i,j];
                                    pifSEPERATS2S3TM5array[pifSEPERATS2S3arrayCount] = REFS2TM5[i,j];
                            pifSEPERATS2S3TM7array[pifSEPERATS2S3arrayCount] = REFS2TM7[i,j];
                            pifSEPERATS2S3arrayCount = pifSEPERATS2S3arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S4TM1array[pifSEPERATS2S4arrayCount] = REFS2TM1[i,j];                        
                                        pifSEPERATS2S4TM2array[pifSEPERATS2S4arrayCount] = REFS2TM2[i,j];                        
                                    pifSEPERATS2S4TM3array[pifSEPERATS2S4arrayCount] = REFS2TM3[i,j];
                            pifSEPERATS2S4TM4array[pifSEPERATS2S4arrayCount] = REFS2TM4[i,j];
                                    pifSEPERATS2S4TM5array[pifSEPERATS2S4arrayCount] = REFS2TM5[i,j];
                            pifSEPERATS2S4TM7array[pifSEPERATS2S4arrayCount] = REFS2TM7[i,j];
                            pifSEPERATS2S4arrayCount = pifSEPERATS2S4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ############# band 1
            
                numeric validSEPERATS2S1TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM1 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM1 = pifSEPERATS2S1TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM1 == randomValueSEPERATS2S1TM1 )
                            {
                                validSEPERATS2S1TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM1arrayRAND[i] = pifSEPERATS2S1TM1array[randomValueSEPERATS2S1TM1];
                pifSEPERATS2S1TM1arrayRANDusedInd[i] = randomValueSEPERATS2S1TM1;
                validSEPERATS2S1TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S1TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM2 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM2 = pifSEPERATS2S1TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM2 == randomValueSEPERATS2S1TM2 )
                            {
                                validSEPERATS2S1TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM2arrayRAND[i] = pifSEPERATS2S1TM2array[randomValueSEPERATS2S1TM2];
                pifSEPERATS2S1TM2arrayRANDusedInd[i] = randomValueSEPERATS2S1TM2;
                validSEPERATS2S1TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S1TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM3 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM3 = pifSEPERATS2S1TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM3 == randomValueSEPERATS2S1TM3 )
                            {
                                validSEPERATS2S1TM3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM3arrayRAND[i] = pifSEPERATS2S1TM3array[randomValueSEPERATS2S1TM3];
                pifSEPERATS2S1TM3arrayRANDusedInd[i] = randomValueSEPERATS2S1TM3;
                validSEPERATS2S1TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S1TM4 = 0;
             
 		    for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM4 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM4 = pifSEPERATS2S1TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM4 == randomValueSEPERATS2S1TM4 )
                            {
                                validSEPERATS2S1TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM4arrayRAND[i] = pifSEPERATS2S1TM4array[randomValueSEPERATS2S1TM4];
                pifSEPERATS2S1TM4arrayRANDusedInd[i] = randomValueSEPERATS2S1TM4;
                validSEPERATS2S1TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S1TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM5 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM5 = pifSEPERATS2S1TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM5 == randomValueSEPERATS2S1TM5 )
                            {
                                validSEPERATS2S1TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM5arrayRAND[i] = pifSEPERATS2S1TM5array[randomValueSEPERATS2S1TM5];
                pifSEPERATS2S1TM5arrayRANDusedInd[i] = randomValueSEPERATS2S1TM5;
                validSEPERATS2S1TM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS2S1TM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1TM7 == 0 )
                    {
                        numeric randomValueSEPERATS2S1TM7 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1TM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1TM7 = pifSEPERATS2S1TM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1TM7 == randomValueSEPERATS2S1TM7 )
                            {
                                validSEPERATS2S1TM7 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1TM7arrayRAND[i] = pifSEPERATS2S1TM7array[randomValueSEPERATS2S1TM7];
                pifSEPERATS2S1TM7arrayRANDusedInd[i] = randomValueSEPERATS2S1TM7;
                validSEPERATS2S1TM7 = 0;
                }
            
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS2S3TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM1 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM1 = pifSEPERATS2S3TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM1 == randomValueSEPERATS2S3TM1 )
                            {
                                validSEPERATS2S3TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3TM1arrayRAND[i] = pifSEPERATS2S3TM1array[randomValueSEPERATS2S3TM1];
                pifSEPERATS2S3TM1arrayRANDusedInd[i] = randomValueSEPERATS2S3TM1;
                validSEPERATS2S3TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S3TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM2 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM2 = pifSEPERATS2S3TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM2 == randomValueSEPERATS2S3TM2 )
                            {
                                validSEPERATS2S3TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3TM2arrayRAND[i] = pifSEPERATS2S3TM2array[randomValueSEPERATS2S3TM2];
                pifSEPERATS2S3TM2arrayRANDusedInd[i] = randomValueSEPERATS2S3TM2;
                validSEPERATS2S3TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S3TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM3 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM3 = pifSEPERATS2S3TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM3 == randomValueSEPERATS2S3TM3 )
                            {
                                validSEPERATS2S3TM3 = 0;
                        }
                        }
                    }
                pifSEPERATS2S3TM3arrayRAND[i] = pifSEPERATS2S3TM3array[randomValueSEPERATS2S3TM3];
                pifSEPERATS2S3TM3arrayRANDusedInd[i] = randomValueSEPERATS2S3TM3;
                validSEPERATS2S3TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S3TM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM4 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM4 = pifSEPERATS2S3TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM4 == randomValueSEPERATS2S3TM4 )
                            {
                                validSEPERATS2S3TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3TM4arrayRAND[i] = pifSEPERATS2S3TM4array[randomValueSEPERATS2S3TM4];
                pifSEPERATS2S3TM4arrayRANDusedInd[i] = randomValueSEPERATS2S3TM4;
                validSEPERATS2S3TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S3TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM5 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM5 = pifSEPERATS2S3TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM5 == randomValueSEPERATS2S3TM5 )
                            {
                                validSEPERATS2S3TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3TM5arrayRAND[i] = pifSEPERATS2S3TM5array[randomValueSEPERATS2S3TM5];
                pifSEPERATS2S3TM5arrayRANDusedInd[i] = randomValueSEPERATS2S3TM5;
                validSEPERATS2S3TM5 = 0;
                }
            
            ################ band 7
                
                numeric validSEPERATS2S3TM7 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3TM7 == 0 )
                    {
                        numeric randomValueSEPERATS2S3TM7 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3TM7 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3TM7 = pifSEPERATS2S3TM7arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3TM7 == randomValueSEPERATS2S3TM7 )
                            {
                                validSEPERATS2S3TM7 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3TM7arrayRAND[i] = pifSEPERATS2S3TM7array[randomValueSEPERATS2S3TM7];
                pifSEPERATS2S3TM7arrayRANDusedInd[i] = randomValueSEPERATS2S3TM7;
                validSEPERATS2S3TM7 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS2S4TM1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4TM1 == 0 )
                    {
                        numeric randomValueSEPERATS2S4TM1 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4TM1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4TM1 = pifSEPERATS2S4TM1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4TM1 == randomValueSEPERATS2S4TM1 )
                            {
                                validSEPERATS2S4TM1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4TM1arrayRAND[i] = pifSEPERATS2S4TM1array[randomValueSEPERATS2S4TM1];
                pifSEPERATS2S4TM1arrayRANDusedInd[i] = randomValueSEPERATS2S4TM1;
                validSEPERATS2S4TM1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S4TM2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4TM2 == 0 )
                    {
                        numeric randomValueSEPERATS2S4TM2 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4TM2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4TM2 = pifSEPERATS2S4TM2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4TM2 == randomValueSEPERATS2S4TM2 )
                            {
                                validSEPERATS2S4TM2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4TM2arrayRAND[i] = pifSEPERATS2S4TM2array[randomValueSEPERATS2S4TM2];
                pifSEPERATS2S4TM2arrayRANDusedInd[i] = randomValueSEPERATS2S4TM2;
                validSEPERATS2S4TM2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S4TM3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4TM3 == 0 )
                    {
                        numeric randomValueSEPERATS2S4TM3 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4TM3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4TM3 = pifSEPERATS2S4TM3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4TM3 == randomValueSEPERATS2S4TM3 )
                            {
                                validSEPERATS2S4TM3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4TM3arrayRAND[i] = pifSEPERATS2S4TM3array[randomValueSEPERATS2S4TM3];
                pifSEPERATS2S4TM3arrayRANDusedInd[i] = randomValueSEPERATS2S4TM3;
                validSEPERATS2S4TM3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S4TM4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4TM4 == 0 )
                    {
                        numeric randomValueSEPERATS2S4TM4 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4TM4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4TM4 = pifSEPERATS2S4TM4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4TM4 == randomValueSEPERATS2S4TM4 )
                            {
                                validSEPERATS2S4TM4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4TM4arrayRAND[i] = pifSEPERATS2S4TM4array[randomValueSEPERATS2S4TM4];
                pifSEPERATS2S4TM4arrayRANDusedInd[i] = randomValueSEPERATS2S4TM4;
                validSEPERATS2S4TM4 = 0;
                }
            
            ############## band 5
            
                numeric validSEPERATS2S4TM5 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4TM5 == 0 )
                    {
                        numeric randomValueSEPERATS2S4TM5 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4TM5 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4TM5 = pifSEPERATS2S4TM5arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4TM5 == randomValueSEPERATS2S4TM5 )
                            {
                                validSEPERATS2S4TM5 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4TM5arrayRAND[i] = pifSEPERATS2S4TM5array[randomValueSEPERATS2S4TM5];
                pifSEPERATS2S4TM5arrayRANDusedInd[i] = randomValueSEPERATS2S4TM5;
                validSEPERATS2S4TM5 = 0;
                }
            
            ################ band 7
                
                    numeric validSEPERATS2S4TM7 = 0;
                 
                    for i = 1 to minPifVAL
                    {
                        while ( validSEPERATS2S4TM7 == 0 )
                        {
                            numeric randomValueSEPERATS2S4TM7 = floor((rand(countS2S4total) + 1));      
                            validSEPERATS2S4TM7 = 1;
                
                            for j = 1 to (i-1)
                            {
                                local numeric testValueSEPERATS2S4TM7 = pifSEPERATS2S4TM7arrayRANDusedInd[j];
                            
                                if ( testValueSEPERATS2S4TM7 == randomValueSEPERATS2S4TM7 )
                                {
                                    validSEPERATS2S4TM7 = 0;
                                }
                            }
                        }
                    pifSEPERATS2S4TM7arrayRAND[i] = pifSEPERATS2S4TM7array[randomValueSEPERATS2S4TM7];
                    pifSEPERATS2S4TM7arrayRANDusedInd[i] = randomValueSEPERATS2S4TM7;
                    validSEPERATS2S4TM7 = 0;
                    }
			}

			else
			{
			numeric pifSEPERATS2S1arrayCount = 1;
			numeric pifSEPERATS2S3arrayCount = 1;
			numeric pifSEPERATS2S4arrayCount = 1;

            	array pifSEPERATS2S1MSS1array[countS2S1total];
            	array pifSEPERATS2S1MSS2array[countS2S1total];
            	array pifSEPERATS2S1MSS3array[countS2S1total];
            	array pifSEPERATS2S1MSS4array[countS2S1total];
            
            	array pifSEPERATS2S1MSS1arrayRAND[minPifVAL];
            	array pifSEPERATS2S1MSS2arrayRAND[minPifVAL];
            	array pifSEPERATS2S1MSS3arrayRAND[minPifVAL];
            	array pifSEPERATS2S1MSS4arrayRAND[minPifVAL];
            	            
            	array pifSEPERATS2S1MSS1arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1MSS2arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1MSS3arrayRANDusedInd[minPifVAL];
            	array pifSEPERATS2S1MSS4arrayRANDusedInd[minPifVAL];
            

            	array pifSEPERATS2S3MSS1array[countS2S3total];
            	array pifSEPERATS2S3MSS2array[countS2S3total];
            	array pifSEPERATS2S3MSS3array[countS2S3total];
            	array pifSEPERATS2S3MSS4array[countS2S3total];
            
            	array pifSEPERATS2S3MSS1arrayRAND[minPifVAL];
                array pifSEPERATS2S3MSS2arrayRAND[minPifVAL];
                array pifSEPERATS2S3MSS3arrayRAND[minPifVAL];
                array pifSEPERATS2S3MSS4arrayRAND[minPifVAL];
                   
                array pifSEPERATS2S3MSS1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3MSS2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3MSS3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S3MSS4arrayRANDusedInd[minPifVAL];
                

            	array pifSEPERATS2S4MSS1array[countS2S4total];
            	array pifSEPERATS2S4MSS2array[countS2S4total];
            	array pifSEPERATS2S4MSS3array[countS2S4total];
            	array pifSEPERATS2S4MSS4array[countS2S4total];
            
            	array pifSEPERATS2S4MSS1arrayRAND[minPifVAL];
                array pifSEPERATS2S4MSS2arrayRAND[minPifVAL];
                array pifSEPERATS2S4MSS3arrayRAND[minPifVAL];
                array pifSEPERATS2S4MSS4arrayRAND[minPifVAL];
                   
                array pifSEPERATS2S4MSS1arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4MSS2arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4MSS3arrayRANDusedInd[minPifVAL];
                array pifSEPERATS2S4MSS4arrayRANDusedInd[minPifVAL];

                	  for i = 1 to MSSlins
                	  {
                    	for j = 1 to MSScols
                    	{     
                            if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                            {
                                    pifSEPERATS2S1MSS1array[pifSEPERATS2S1arrayCount] = REFS2MSS1[i,j];                        
                                        pifSEPERATS2S1MSS2array[pifSEPERATS2S1arrayCount] = REFS2MSS2[i,j];                        
                                    pifSEPERATS2S1MSS3array[pifSEPERATS2S1arrayCount] = REFS2MSS3[i,j];
                            pifSEPERATS2S1MSS4array[pifSEPERATS2S1arrayCount] = REFS2MSS4[i,j];
                                    pifSEPERATS2S1arrayCount = pifSEPERATS2S1arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S3MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S3MSS1array[pifSEPERATS2S3arrayCount] = REFS2MSS1[i,j];                        
                                        pifSEPERATS2S3MSS2array[pifSEPERATS2S3arrayCount] = REFS2MSS2[i,j];                        
                                    pifSEPERATS2S3MSS3array[pifSEPERATS2S3arrayCount] = REFS2MSS3[i,j];
                            pifSEPERATS2S3MSS4array[pifSEPERATS2S3arrayCount] = REFS2MSS4[i,j];
                            pifSEPERATS2S3arrayCount = pifSEPERATS2S3arrayCount + 1;
                            }
        
                            if ( S2MASK[i,j] == 1 && S4MASK[i,j] == 1 )
                            {
                            pifSEPERATS2S4MSS1array[pifSEPERATS2S4arrayCount] = REFS2MSS1[i,j];                        
                                        pifSEPERATS2S4MSS2array[pifSEPERATS2S4arrayCount] = REFS2MSS2[i,j];                        
                                    pifSEPERATS2S4MSS3array[pifSEPERATS2S4arrayCount] = REFS2MSS3[i,j];
                            pifSEPERATS2S4MSS4array[pifSEPERATS2S4arrayCount] = REFS2MSS4[i,j]; 
                            pifSEPERATS2S4arrayCount = pifSEPERATS2S4arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ############# band 1
            
                numeric validSEPERATS2S1MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS2S1MSS1 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1MSS1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1MSS1 = pifSEPERATS2S1MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1MSS1 == randomValueSEPERATS2S1MSS1 )
                            {
                                validSEPERATS2S1MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1MSS1arrayRAND[i] = pifSEPERATS2S1MSS1array[randomValueSEPERATS2S1MSS1];
                pifSEPERATS2S1MSS1arrayRANDusedInd[i] = randomValueSEPERATS2S1MSS1;
                validSEPERATS2S1MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S1MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS2S1MSS2 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1MSS2 = 1;
            
                        for j = 1 to (i-1)
                 {
                            local numeric testValueSEPERATS2S1MSS2 = pifSEPERATS2S1MSS2arrayRANDusedInd[j];
         
                            if ( testValueSEPERATS2S1MSS2 == randomValueSEPERATS2S1MSS2 )
                            {
    validSEPERATS2S1MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1MSS2arrayRAND[i] = pifSEPERATS2S1MSS2array[randomValueSEPERATS2S1MSS2];
                pifSEPERATS2S1MSS2arrayRANDusedInd[i] = randomValueSEPERATS2S1MSS2;
                validSEPERATS2S1MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S1MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS2S1MSS3 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1MSS3 = pifSEPERATS2S1MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1MSS3 == randomValueSEPERATS2S1MSS3 )
                            {
                                validSEPERATS2S1MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1MSS3arrayRAND[i] = pifSEPERATS2S1MSS3array[randomValueSEPERATS2S1MSS3];
                pifSEPERATS2S1MSS3arrayRANDusedInd[i] = randomValueSEPERATS2S1MSS3;
                validSEPERATS2S1MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S1MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S1MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS2S1MSS4 = floor((rand(countS2S1total) + 1));      
                        validSEPERATS2S1MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S1MSS4 = pifSEPERATS2S1MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S1MSS4 == randomValueSEPERATS2S1MSS4 )
                            {
                                validSEPERATS2S1MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S1MSS4arrayRAND[i] = pifSEPERATS2S1MSS4array[randomValueSEPERATS2S1MSS4];
                pifSEPERATS2S1MSS4arrayRANDusedInd[i] = randomValueSEPERATS2S1MSS4;
                validSEPERATS2S1MSS4 = 0;
                }
            
            ###################################################
            
            	############# band 1
            
                numeric validSEPERATS2S3MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS2S3MSS1 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3MSS1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3MSS1 = pifSEPERATS2S3MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3MSS1 == randomValueSEPERATS2S3MSS1 )
                            {
                                validSEPERATS2S3MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3MSS1arrayRAND[i] = pifSEPERATS2S3MSS1array[randomValueSEPERATS2S3MSS1];
                pifSEPERATS2S3MSS1arrayRANDusedInd[i] = randomValueSEPERATS2S3MSS1;
                validSEPERATS2S3MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S3MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS2S3MSS2 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3MSS2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3MSS2 = pifSEPERATS2S3MSS2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3MSS2 == randomValueSEPERATS2S3MSS2 )
                            {
                                validSEPERATS2S3MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3MSS2arrayRAND[i] = pifSEPERATS2S3MSS2array[randomValueSEPERATS2S3MSS2];
                pifSEPERATS2S3MSS2arrayRANDusedInd[i] = randomValueSEPERATS2S3MSS2;
                validSEPERATS2S3MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S3MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS2S3MSS3 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3MSS3 = pifSEPERATS2S3MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3MSS3 == randomValueSEPERATS2S3MSS3 )
                            {
                                validSEPERATS2S3MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3MSS3arrayRAND[i] = pifSEPERATS2S3MSS3array[randomValueSEPERATS2S3MSS3];
                pifSEPERATS2S3MSS3arrayRANDusedInd[i] = randomValueSEPERATS2S3MSS3;
                validSEPERATS2S3MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S3MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S3MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS2S3MSS4 = floor((rand(countS2S3total) + 1));      
                        validSEPERATS2S3MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S3MSS4 = pifSEPERATS2S3MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S3MSS4 == randomValueSEPERATS2S3MSS4 )
                            {
                                validSEPERATS2S3MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S3MSS4arrayRAND[i] = pifSEPERATS2S3MSS4array[randomValueSEPERATS2S3MSS4];
                pifSEPERATS2S3MSS4arrayRANDusedInd[i] = randomValueSEPERATS2S3MSS4;
                validSEPERATS2S3MSS4 = 0;
                }
            
            #######################################
            
            	############# band 1
            
                numeric validSEPERATS2S4MSS1 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4MSS1 == 0 )
                    {
                        numeric randomValueSEPERATS2S4MSS1 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4MSS1 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4MSS1 = pifSEPERATS2S4MSS1arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4MSS1 == randomValueSEPERATS2S4MSS1 )
                            {
                                validSEPERATS2S4MSS1 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4MSS1arrayRAND[i] = pifSEPERATS2S4MSS1array[randomValueSEPERATS2S4MSS1];
                pifSEPERATS2S4MSS1arrayRANDusedInd[i] = randomValueSEPERATS2S4MSS1;
                validSEPERATS2S4MSS1 = 0;
                }
            
            ############# band 2
            
                numeric validSEPERATS2S4MSS2 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4MSS2 == 0 )
                    {
                        numeric randomValueSEPERATS2S4MSS2 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4MSS2 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4MSS2 = pifSEPERATS2S4MSS2arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4MSS2 == randomValueSEPERATS2S4MSS2 )
                            {
                                validSEPERATS2S4MSS2 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4MSS2arrayRAND[i] = pifSEPERATS2S4MSS2array[randomValueSEPERATS2S4MSS2];
                pifSEPERATS2S4MSS2arrayRANDusedInd[i] = randomValueSEPERATS2S4MSS2;
                validSEPERATS2S4MSS2 = 0;
                }
            
            ############## band 3
                
                numeric validSEPERATS2S4MSS3 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4MSS3 == 0 )
                    {
                        numeric randomValueSEPERATS2S4MSS3 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4MSS3 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4MSS3 = pifSEPERATS2S4MSS3arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4MSS3 == randomValueSEPERATS2S4MSS3 )
                            {
                                validSEPERATS2S4MSS3 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4MSS3arrayRAND[i] = pifSEPERATS2S4MSS3array[randomValueSEPERATS2S4MSS3];
                pifSEPERATS2S4MSS3arrayRANDusedInd[i] = randomValueSEPERATS2S4MSS3;
                validSEPERATS2S4MSS3 = 0;
                }
            
            ################ band 4
            
                numeric validSEPERATS2S4MSS4 = 0;
             
                for i = 1 to minPifVAL
                {
                    while ( validSEPERATS2S4MSS4 == 0 )
                    {
                        numeric randomValueSEPERATS2S4MSS4 = floor((rand(countS2S4total) + 1));      
                        validSEPERATS2S4MSS4 = 1;
            
                        for j = 1 to (i-1)
                        {
                            local numeric testValueSEPERATS2S4MSS4 = pifSEPERATS2S4MSS4arrayRANDusedInd[j];
                        
                            if ( testValueSEPERATS2S4MSS4 == randomValueSEPERATS2S4MSS4 )
                            {
                                validSEPERATS2S4MSS4 = 0;
                            }
                        }
                    }
                pifSEPERATS2S4MSS4arrayRAND[i] = pifSEPERATS2S4MSS4array[randomValueSEPERATS2S4MSS4];
                pifSEPERATS2S4MSS4arrayRANDusedInd[i] = randomValueSEPERATS2S4MSS4;
                validSEPERATS2S4MSS4 = 0;
                }
		}

			if ( sensors1 == 7 )
	            {
            	numeric pifSEPERATS1arrayCount = 1;
            	array pifSEPERATS1slaveETM1array[countS2S1total];
            	array pifSEPERATS1slaveETM2array[countS2S1total];
            	array pifSEPERATS1slaveETM3array[countS2S1total];
            	array pifSEPERATS1slaveETM4array[countS2S1total];
            	array pifSEPERATS1slaveETM5array[countS2S1total];
            	array pifSEPERATS1slaveETM7array[countS2S1total];
            
            	array pifSEPERATS1slaveETM1arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveETM2arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveETM3arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveETM4arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveETM5arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveETM7arrayRAND[minPifVAL];
            
			  for i = 1 to ETMlins
                	  {
                    	for j = 1 to ETMcols
                    	{     
                            if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                            {
                                    pifSEPERATS1slaveETM1array[pifSEPERATS1arrayCount] = REFS1ETM1[i,j];
                                    pifSEPERATS1slaveETM2array[pifSEPERATS1arrayCount] = REFS1ETM2[i,j]; 
                                    pifSEPERATS1slaveETM3array[pifSEPERATS1arrayCount] = REFS1ETM3[i,j];
                            		pifSEPERATS1slaveETM4array[pifSEPERATS1arrayCount] = REFS1ETM4[i,j];
                                    pifSEPERATS1slaveETM5array[pifSEPERATS1arrayCount] = REFS1ETM5[i,j];
                            		pifSEPERATS1slaveETM7array[pifSEPERATS1arrayCount] = REFS1ETM7[i,j];
                                    pifSEPERATS1arrayCount = pifSEPERATS1arrayCount + 1;
                            }
                    	}
                	  }
                	  
                ################ band 1
             
		if ( sensors2 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM1arrayRAND[i] = pifSEPERATS1slaveETM1array[pifSEPERATS2S1ETM1arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{
		}                                     
                
                ################ band 2
             
		if ( sensors2 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM2arrayRAND[i] = pifSEPERATS1slaveETM2array[pifSEPERATS2S1ETM2arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM2arrayRAND[i] = pifSEPERATS1slaveETM2array[pifSEPERATS2S1TM2arrayRANDusedInd[i]];
                }                                                    
		}

                ################ band 3
             
		if ( sensors2 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM3arrayRAND[i] = pifSEPERATS1slaveETM3array[pifSEPERATS2S1ETM3arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{     
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM3arrayRAND[i] = pifSEPERATS1slaveETM3array[pifSEPERATS2S1TM3arrayRANDusedInd[i]];
                }                                               
		}

                ################ band 4
             
		if ( sensors2 == 7 )
		{           	
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM4arrayRAND[i] = pifSEPERATS1slaveETM4array[pifSEPERATS2S1ETM4arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{   
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM4arrayRAND[i] = pifSEPERATS1slaveETM4array[pifSEPERATS2S1TM4arrayRANDusedInd[i]];
                }                                                 
		}

                ################ band 5
             
		if ( sensors2 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM5arrayRAND[i] = pifSEPERATS1slaveETM5array[pifSEPERATS2S1ETM5arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{   
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM5arrayRAND[i] = pifSEPERATS1slaveETM5array[pifSEPERATS2S1TM5arrayRANDusedInd[i]];
                }                                                 
		}

                ################ band 7
             
		if ( sensors2 == 7 )
		{                                                    
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM7arrayRAND[i] = pifSEPERATS1slaveETM7array[pifSEPERATS2S1ETM7arrayRANDusedInd[i]];
                }
		}

		else if ( sensors2 == 6 )
		{ 
                for i = 1 to minPifVAL
                {
                pifSEPERATS1slaveETM7arrayRAND[i] = pifSEPERATS1slaveETM7array[pifSEPERATS2S1TM7arrayRANDusedInd[i]];
                }                                                   
		}
        }

			else if ( sensors1 == 6 )
			{
			numeric pifSEPERATS1arrayCount = 1;
            	array pifSEPERATS1slaveTM1array[countS2S1total];
            	array pifSEPERATS1slaveTM2array[countS2S1total];
            	array pifSEPERATS1slaveTM3array[countS2S1total];
            	array pifSEPERATS1slaveTM4array[countS2S1total];
            	array pifSEPERATS1slaveTM5array[countS2S1total];
            	array pifSEPERATS1slaveTM7array[countS2S1total];
            
            	array pifSEPERATS1slaveTM1arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveTM2arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveTM3arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveTM4arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveTM5arrayRAND[minPifVAL];
            	array pifSEPERATS1slaveTM7arrayRAND[minPifVAL];
            
			  for i = 1 to TMlins
                	  {
                    	for j = 1 to TMcols
                    	{     
                            if ( S2MASK[i,j] == 1 && S1MASK[i,j] == 1 )
                            {
                                    pifSEPERATS1slaveTM1array[pifSEPERATS1arrayCount] = REFS1TM1[i,j];
                                    pifSEPERATS1slaveTM2array[pifSEPERATS1arrayCount] = REFS1TM2[i,j]; 
                                    pifSEPERATS1slaveTM3array[pifSEPERATS1arrayCount] = REFS1TM3[i,j];
                                    pifSEPERATS1slaveTM4array[pifSEPERATS1arrayCount] = REFS1TM4[i,j];
                                    pifSEPERATS1slaveTM5array[pifSEPERATS1arrayCount] = REFS1TM5[i,j];
                                    pifSEPERATS1slaveTM7array[pifSEPERATS1arrayCount] = REF