// (c) Rebecca Dell, University of Cambridge, July 2020
// This script demonstrates the pre_processing workflow, in this case as applied to training (or validation) scenes
//    that were selected by Dell et al., 2021, across a series of ice shelves

// This script takes pre-selected training (or validation) images and applys a per-pixel TOA correction and clips each image to the ice shelf region of interest. 
// The images are then assembled into a collection, before applying a rock mask and a cloud mask. 
// Images are then exported to assets for use in other scripts. 

// Call Modules (external scripts containing functions for cloud and rock masking and per-pixel TOA corrections)
var cloud_mask = require('users/beckyld/Chapter_2:Modules/L8_Cloud_Mask'); // BQA method + 4km buffer
var rock_mask = require('users/beckyld/Chapter_2:Modules/L8_Rock_Mask'); // Moussavi et al. 2020 method + 1km buffer (Halberstadt et al., 2020)
var per_pixel_TOA = require('users/beckyld/Chapter_2:Modules/L8_Per_Pixel_TOA'); //L8 TOA image to per pixel TOA image

// Set Viz Params 
var vizParamsL8 = {bands: ['B4', 'B3', 'B2'],}; 

// Extract shapefiles for each ice shelf training site from SCAR Antarctic Digital Database, Version 7.0, in Quantarctica
var GVI = (table.filter(ee.Filter.eq("system:index", '0000000000000000045e')));
var Amery = (table.filter(ee.Filter.eq("system:index", '00000000000000000460')));
var Shackleton = (table.filter(ee.Filter.eq("system:index", '000000000000000003f5')));
var Bach = (table.filter(ee.Filter.eq("system:index", '0000000000000000044b')));
var Nansen = (table.filter(ee.Filter.eq("system:index", '00000000000000000367'))); 
var Nivlisen = (table.filter(ee.Filter.eq("system:index", '0000000000000000052d'))); 
var RoiB = (table.filter(ee.Filter.eq("system:index", '00000000000000000532'))); 

var im1 = ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_219110_20160113');//GVI
var im1_TOA = per_pixel_TOA.per_pixel_TOA(im1); // apply per-pixel correction
var im1_clipped = im1_TOA.clip(GVI); // clip to AOI
print(im1);
Map.addLayer(im1_clipped, vizParamsL8);

var im2 = ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_127111_20190218');//Amery
var im2_TOA = per_pixel_TOA.per_pixel_TOA(im2);
var im2_clipped = im2_TOA.clip(Amery);
print(im2);
Map.addLayer(im2_clipped, vizParamsL8);

var im3 = ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_112106_20170203');//Shackleton
var im3_TOA = per_pixel_TOA.per_pixel_TOA(im3);
var im3_clipped = im3_TOA.clip(Shackleton);
print(im3);
Map.addLayer(im3_clipped, vizParamsL8);

var im4 = ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_061113_20141206');//Nansen
var im4_TOA = per_pixel_TOA.per_pixel_TOA(im4);
var im4_clipped = im4_TOA.clip(Nansen);
print(im4);
Map.addLayer(im4_clipped, vizParamsL8);

var im5 =  ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_167110_20180207');//Nivlisen
var im5_TOA = per_pixel_TOA.per_pixel_TOA(im5);
var im5_clipped = im5_TOA.clip(Nivlisen);
print(im5);
Map.addLayer(im5_clipped, vizParamsL8);

var im6 = ee.Image('LANDSAT/LC08/C01/T2_TOA/LC08_154110_20150103');//RoiB
var im6_TOA = per_pixel_TOA.per_pixel_TOA(im6);
var im6_clipped = im6_TOA.clip(RoiB);
print(im6);
Map.addLayer(im6_clipped, vizParamsL8);

var training_collection = ee.ImageCollection.fromImages([im1_clipped, im2_clipped, im3_clipped, im4_clipped, im5_clipped, im6_clipped]);

print(training_collection);
Map.addLayer(training_collection);

// Apply rock mask 
var training_collection_rmask = training_collection.map(rock_mask.rock_mask);
print(training_collection_rmask);
Map.addLayer(training_collection_rmask, vizParamsL8, 'rock_masked');

// Apply cloud mask 
var training_collection_cmask = training_collection_rmask.map(cloud_mask.maskClouds);
print(training_collection_cmask);
Map.addLayer(training_collection_cmask, vizParamsL8, 'cloud_masked');

// Export each image to an Earth Engine asset (for example, image LC08_154110_20150103)
var image2export = ee.Image([training_collection_cmask.toList(6).get(5)]);// Change number in .get() to export each image
print(image2export);
//Map.addLayer(image2export,vizParamsL8);

Export.image.toAsset({
  image: image2export,
  description: 'LC08_154110_20150103_RoiB',
  assetId: 'LC08_154110_20150103_RoiB',
  scale: 30,
  region: RoiB,
  maxPixels: 1e13
});

// REFERENCES

//Halberstadt, A. R. W., Gleason, C. J., Moussavi, M. S., Pope, A., Trusel, L. D., & DeConto, R. M. (2020). 
//Antarctic supraglacial lake identification using landsat-8 image classification. Remote Sensing, 12(8), 
//1–29. https://doi.org/10.3390/RS12081327

// Moussavi, M., Pope, A., Halberstadt, A. R. W., Trusel, L. D., Cioffi, L., & Abdalati, W. (2020). 
//Antarctic Supraglacial Lake Detection Using Landsat 8 and Sentinel-2 Imagery: Towards Continental
//Generation of Lake Volumes. Remote Sensing, 12(1), 134. https://doi.org/10.3390/rs12010134