# #5: create a cloud free composite

A modular land cover system part 5: topographic correction

Step 1: create a new script and call it topographic_module

Step 2: copy the code below and store it in the script

```var scale = 300;
var toaOrSR = 'SR';

// get terrain layers
var dem = ee.Image("USGS/SRTMGL1_003");

exports.topoCorrection = function(collection) {

collection = collection.map(illuminationCondition);
collection = collection.map(illuminationCorrection);

//collection = correction.merge(notcorrection).sort("system:time_start");

return(collection);

////////////////////////////////////////////////////////////////////////////////
// Function to calculate illumination condition (IC). Function by Patrick Burns
// (pb463@nau.edu) and Matt Macander
// (mmacander@abrinc.com)
function illuminationCondition(img){

// Creat terrain layers
var slp = ee.Terrain.slope(dem).clip(img.geometry().buffer(10000));

// Calculate the Illumination Condition (IC)
// slope part of the illumination condition
var slope_illumination = cosS.expression("cosZ * cosS",
{'cosZ': cosZ,
'cosS': cosS.select('slope')});
// aspect part of the illumination condition
var aspect_illumination = sinZ.expression("sinZ * sinS * cosAziDiff",
{'sinZ': sinZ,
'sinS': sinS,
'cosAziDiff': cosAziDiff});
// full illumination condition (IC)

// Add IC to original image
return img_plus_ic;
}
////////////////////////////////////////////////////////////////////////////////
// Function to apply the Sun-Canopy-Sensor + C (SCSc) correction method to each
// image. Function by Patrick Burns (pb463@nau.edu) and Matt Macander
// (mmacander@abrinc.com)
function illuminationCorrection(img){
var props = img.toDictionary();
var st = img.get('system:time_start');

var img_plus_ic = img;
.and(img_plus_ic.select('IC').gte(0))
.and(img_plus_ic.select('nir').gt(-0.1));

// Specify Bands to topographically correct
var bandList = ['blue','green','red','nir','swir1','swir2'];
var compositeBands = img.bandNames();
var nonCorrectBands = img.select(compositeBands.removeAll(bandList));

var geom = ee.Geometry(img.get('system:footprint')).bounds().buffer(10000);

function apply_SCSccorr(band){
var method = 'SCSc';
.reduceRegion({reducer: ee.Reducer.linearRegression(2,1),
geometry: ee.Geometry(img.geometry()),
scale: scale,
bestEffort :true,
maxPixels:1e10});

var fit = out.combine({"coefficients": ee.Array([[1],[1]])}, false);

//Get the coefficients as a nested list,
// ast it to an array, and get just the selected column
var out_a = (ee.Array(fit.get('coefficients')).get([0,0]));
var out_b = (ee.Array(fit.get('coefficients')).get([1,0]));
var out_c = out_a.divide(out_b);

// Apply the SCSc correction
"((image * (cosB * cosZ + cvalue)) / (ic + cvalue))", {
'cvalue': out_c
});

return SCSc_output;

}

var bandList_IC = ee.List([bandList, 'IC']).flatten();

.setMulti(props)
.set('system:time_start',st);
}

};

```

Step 4: Open the main script and add the lines below

```var topography = require("users/../mrc:topographic_module");
print("applying illumination correction");
s2 =  topography.topoCorrection(s2);