Step 1: import sentinel 2 data using modules
It is generally a good practice to use a modular code structure.
To make a function or object available to other scripts, you add it to a special object called exports. To use the code in another script, use the require function to load the exports from another script:
1. Create a new repository:
2. Create a new script and save it asĀ importS2_module in your new repository with the script below.
var inBands = ee.List(['QA60','B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']); var outBands = ee.List(['QA60','cb','blue','green','red','re1','re2','re3','nir','re4','waterVapor','cirrus','swir1','swir2']); var CloudCoverMax = 30; exports.importData = function(studyArea,startDate,endDate,startJulian,endJulian) { // Get Sentinel-2 data var s2s = ee.ImageCollection('COPERNICUS/S2') .filterDate(startDate,endDate) //.filter(ee.Filter.calendarRange(startJulian,endJulian)) .filterBounds(studyArea) .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',CloudCoverMax)) .filter(ee.Filter.lt('CLOUD_COVERAGE_ASSESSMENT',CloudCoverMax)); function scaleBands(img){ var prop = img.toDictionary() var t = img.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']).divide(10000); t = t.addBands(img.select(['QA60'])).set(prop).copyProperties(img,['system:time_start','system:footprint']) return ee.Image(t); } s2s = s2s.map(scaleBands); s2s = s2s.select(inBands,outBands) ; return s2s; };
link
3. Create a new script called main and modify the code below to import your sentinel-2 data
// import required modules var importS2 = require("users/username/landcoverS2:importS2_module"); // set variables var vietnam = ee.FeatureCollection("users/servirmekong/countries/VNM_adm1"); var NgheAn = vietnam.filter(ee.Filter.eq("ID_1",41)); var region = NgheAn.geometry(); var startDate = "2017-01-01"; var endDate = "2018-01-01"; print("getting images"); var s2 = importS2.importData(region,startDate,endDate); print("found ",s2.size(),"images"); Map.addLayer(ee.Image(s2.first()),{min:0,max:0.3,bands:"swir2,nir,red"},"first image"); print(ee.Image(s2.first()));