Skip to content

Commit

Permalink
LAI predictor added
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcYin committed May 24, 2019
1 parent e571eb2 commit 6c9b1bf
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Prosail
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// A sensor invariant Atmospheric Correction (SIAC) GEE version
// v1 -- 2019-05-06
// Author: Fengn Yin, UCL
// Email: ucfafy@ucl.ac.uk
// Github: https://github.com/MarcYin/SIAC
// DOI: https://eartharxiv.org/ps957/
// LICENSE: GNU GENERAL PUBLIC LICENSE V3

var get_2D = function(image, nb, ny){
var array = ee.List(image.reduceRegion(ee.Reducer.toList(), image.geometry()).get('b1'))
var sub_len = array.size().divide(nb)
var i;
var j;
var list_2d = []
for (j = 0; j < nb; j++){
var start = sub_len.multiply(j)
var end = sub_len.multiply(j).add(ny)
var sub = array.slice(start, end)
list_2d.push(sub)
}
return ee.List(list_2d).reverse()
}

var get_3D = function(image, nb, nx, ny){
var array = ee.List(image.reduceRegion(ee.Reducer.toList(), image.geometry()).get('b1'))
var sub_len = array.size().divide(nb)
var i;
var j;
var ret = []
for (j = 0; j < nb; j++){
var list_2d = []
for (i = 0; i < nx; i++) {
var start = sub_len.multiply(j).add(i*ny)
var end = sub_len.multiply(j).add((i+1)*ny)
var sub = array.slice(start, end)
list_2d.push(sub)
}
ret.push(list_2d)
}
return ee.List(ret).reverse()
}

var parse_out = function(image){
var dict = ee.Dictionary(image.reduceRegion(ee.Reducer.toList(), image.geometry()))
var ret = ee.List([dict.get('b1')])
return ret
}

var Prosail_H1_offset = get_2D(ee.Image('users/marcyinfeng/Prosail_H1_offset'), 1, 256).get(0)
var Prosail_H2_offset = get_2D(ee.Image('users/marcyinfeng/Prosail_H2_offset'), 1, 256).get(0)
var Prosail_Out_offset = parse_out(ee.Image('users/marcyinfeng/Prosail_Out_offset'))


var Prosail_H1_scale = get_3D(ee.Image('users/marcyinfeng/Prosail_H1_scale'), 1, 10, 256).get(0)
var Prosail_H2_scale = get_3D(ee.Image('users/marcyinfeng/Prosail_H2_scale'), 1, 256, 256).get(0)
var Prosail_Out_scale = get_3D(ee.Image('users/marcyinfeng/Prosail_Out_scale'), 1, 256, 1).get(0)

// print(Prosail_H1_scale)
// print(Prosail_H2_scale)

// print(Prosail_H1_offset)
// print(Prosail_H2_offset)

// print(Prosail_Out_offset)
// print(Prosail_Out_scale)

exports.Prosail_H1_scale = Prosail_H1_scale
exports.Prosail_H2_scale = Prosail_H2_scale
exports.Prosail_Out_scale = Prosail_Out_scale
exports.Prosail_H1_offset = Prosail_H1_offset
exports.Prosail_H2_offset = Prosail_H2_offset
exports.Prosail_Out_offset = Prosail_Out_offset
89 changes: 89 additions & 0 deletions S2_Toa2Lai
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// A sensor invariant Atmospheric Correction (SIAC) GEE version
// v1 -- 2019-05-06
// Author: Fengn Yin, UCL
// Email: ucfafy@ucl.ac.uk
// Github: https://github.com/MarcYin/SIAC
// DOI: https://eartharxiv.org/ps957/
// LICENSE: GNU GENERAL PUBLIC LICENSE V3
var tnn = require('users/marcyinfeng/SIAC:Two_NN')
var prosail = require('users/marcyinfeng/Toa2Lai:Prosail')
var siac_processor = require('users/marcyinfeng/SIAC:AC_processor')

var inv_prosail = function(image){
image = ee.Image(image)
var geom = image.geometry()
var image_date = image.date()
var projection = image.select('B2').projection()
var crs = projection.crs()
var boa = siac_processor.get_boa(image)
var saa = ee.Image.constant(ee.Number(image.get('MEAN_SOLAR_AZIMUTH_ANGLE'))).rename('saa')
var sza = ee.Image.constant(ee.Number(image.get('MEAN_SOLAR_ZENITH_ANGLE' ))).rename('sza')
var vaa = ee.Image.constant(ee.Number(image.get('MEAN_INCIDENCE_AZIMUTH_ANGLE_B2'))).rename('vaa')
var vza = ee.Image.constant(ee.Number(image.get('MEAN_INCIDENCE_ZENITH_ANGLE_B2' ))).rename('vza')
var raa = vaa.subtract(saa)
var deg2rad = ee.Number(Math.PI).divide(ee.Number(180.0))
var cos_sza = (sza.multiply(deg2rad)).cos()
var cos_vza = (vza.multiply(deg2rad)).cos()
var cos_raa = (raa.multiply(deg2rad)).cos()
var name = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12']
var new_name = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12']
var prosail_bands = ['B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B8A']
var prosail_inputs = ee.Image.cat([boa.select(name, new_name).select(prosail_bands),
cos_sza, cos_vza, cos_raa])

var H1_scale = prosail.Prosail_H1_scale
var H2_scale = prosail.Prosail_H2_scale
var Out_scale = prosail.Prosail_Out_scale

var H1_offset = prosail.Prosail_H1_offset
var H2_offset = prosail.Prosail_H2_offset
var Out_offset = prosail.Prosail_Out_offset

var arrayImage1D = prosail_inputs.toArray()
var arrayImage2D = arrayImage1D.toArray(1);
var imageAxis = 0;
var bandAxis = 1;
var arrayLength = arrayImage2D.arrayLength(imageAxis);
arrayImage2D = arrayImage2D
var h1 = arrayImage2D.arrayTranspose().matrixMultiply(ee.Image(ee.Array(H1_scale))).add(ee.Image(ee.Array(H1_offset)).toArray(1).arrayTranspose())
var in1 = h1.max(0)
var h2 = in1.matrixMultiply(ee.Image(ee.Array(H2_scale))).add(ee.Image(ee.Array(H2_offset)).toArray(1).arrayTranspose())
var in2 = h2.max(0)
var oup = in2.matrixMultiply(ee.Image(ee.Array(Out_scale)).toArray(1))
var out = oup.add(ee.Image(ee.Array(Out_offset.get(0))).toArray(1).arrayTranspose())
.arrayProject([1])
//.arrayFlatten([['lai'], ['Cab'], ['Cbrown'], ['Ala'], ['N']])
.arrayFlatten([['Lai']])
var Lai = out.select('Lai' ).log().multiply(-2)
// Map.addLayer(Lai, {min:0, max:8})
// Map.addLayer(image.divide(10000), {bands:['B4', 'B3', 'B2'], min:0, max:0.2}, 'TOA')
// Map.addLayer(prosail_inputs)
var fname = ee.String(image.get('PRODUCT_ID')).getInfo()
Export.image.toDrive({
image: Lai.multiply(100).toInt(),
description: fname + '_lai',
scale: 10,
fileFormat: 'GeoTIFF',
folder:'S2_SUR',
region: geom,
maxPixels: 1e13
})
}

// var s2_file = '20190303T184259_20190303T184825_T10SGF'
// var s2_file = '20180115T030049_20180115T030132_T50SND'
// var image = ee.ImageCollection('COPERNICUS/S2').filterMetadata('system:index', 'equals', s2_file)
// .first()
// var id = 'S2B_MSIL1C_20190513T030549_N0207_R075_T50SLG_20190513T054821'
// var image = ee.ImageCollection('COPERNICUS/S2').filterMetadata('PRODUCT_ID', 'equals', id)
// .first()

// // var s2_file = '20190122T034049_20190122T034312_T47PPR'
// // var image = ee.ImageCollection('COPERNICUS/S2').filterMetadata('system:index', 'equals', s2_file)
// // .first()

// print(image)
// inv_prosail(image)
// Map.centerObject(image)

exports.inv_prosail = inv_prosail
Binary file modified SIAC_GEE_UI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6c9b1bf

Please sign in to comment.