// firstly import the shapefile of your desired region which in our case is Punjab
// you can upload your own shapefile as an asset in GEE and can be imported from assets taskbar on the left side of GEE
var mask = pb.geometry(); // here pb is the shapefile needed to import from assets menu bar on the left side of GEE

var night = ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG").select("avg_rad");


var startyear = 2014; 
var endyear = 2022;
var startmonth = 1; 
var endmonth = 12; 


var startdate = ee.Date.fromYMD(startyear, startmonth, 1);
var enddate = ee.Date.fromYMD(endyear , endmonth, 30);
var years = ee.List.sequence(startyear, endyear);
var months = ee.List.sequence(startmonth,endmonth);

var nightcol = night
    .filterDate(startdate, enddate);
print(nightcol);

var yearlylight =  ee.ImageCollection.fromImages(
    years.map(function (y) { 
    var yearly = nightcol
        .filter(ee.Filter.calendarRange(y, y, "year"))
        .mean(); 
    return yearly
        .set("year", y) })
        //.set("system:time_start", ee.Date.fromYMD)})
    .flatten());
    
// Define RGB visualization parameters.
var visParams = {min:0,max:10,palette:['000000','700000','808080',
'FFFF00','ffffff','ffffff','ffffff']};

// Create RGB visualization images for use as animation frames.
var rgbVis = yearlylight.map(function(img) {
    return img.visualize(visParams).clip(mask);
});

// Define GIF visualization parameters.
var gifParams = {
    'region': region,
    'dimensions': 600,
    'crs': 'EPSG:3857',
    'framesPerSecond': 3
};

// Print the GIF URL to the console.
print(rgbVis.getVideoThumbURL(gifParams));

// Render the GIF animation in the console.
//print(ui.Thumbnail(rgbVis, gifParams));