-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added notebook to calculate the monthly albedo.
- Loading branch information
1 parent
9a059c2
commit 195c833
Showing
1 changed file
with
1 addition
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[{"file_id":"128HdbDQU3SeqLY4vGuNlGyCgQK0HHeVg","timestamp":1733314635898},{"file_id":"1wKE0zD6TouDnQRMqMDzDN-Iqq0AuFZ0k","timestamp":1733090444603},{"file_id":"1UBvx2U-UBkAla9Y0KlXpX6U9meM3H7pQ","timestamp":1733089025224},{"file_id":"1ctpkuKPZaVRpX53ck4r3niSCnwZM4kn3","timestamp":1733070485060}],"authorship_tag":"ABX9TyNo8g5cXTeez428yBMbF0yw"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Import Python Modules\n","\n","Import the Python modules needed for this notebook."],"metadata":{"id":"JuoEAHbQ9vgo"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"Om_7sTY0BZbl"},"outputs":[],"source":["# The Google Earth Engine module\n","import ee\n","# Import the geemap (https://geemap.org/) module which\n","# has a visualisation tool\n","import geemap\n","# Geopandas allows us to read the shapefile used to\n","# define the region of interest (ROI)\n","import geopandas\n","# The datetime module is used to specify the dates\n","# to search for imagery\n","import datetime\n","# The colab module to access data from your google drive\n","from google.colab import drive"]},{"cell_type":"markdown","source":["## Install pb_gee_tools\n","\n","The pb_gee_tools module (https://github.com/remotesensinginfo/pb_gee_tools) is a collection of tools used to make it easier to access datasets on Google Earth Engine.\n","\n","The code below tests whether the module is available to import and if not will install the module from code repository."],"metadata":{"id":"RR4I6HrX-60i"}},{"cell_type":"code","source":["try:\n"," import pb_gee_tools\n"," import pb_gee_tools.datasets\n"," import pb_gee_tools.utils\n","except:\n"," !git --version\n"," !git clone https://github.com/remotesensinginfo/pb_gee_tools.git\n"," !pip --version\n"," !pip install ./pb_gee_tools/.\n"," import pb_gee_tools\n"," import pb_gee_tools.datasets\n"," import pb_gee_tools.utils"],"metadata":{"id":"UHfwtGnbB0Fh"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Autheticate with Google Earth Engine\n","\n","The next stage is to specify the project being used for your analysis and to autheticate with Google Earth Engine.\n","\n","The first time you run the following cell it will come up with a prompt asking your permission to access your GEE account and project."],"metadata":{"id":"5nhzy6cK_Wj8"}},{"cell_type":"code","source":["ee.Authenticate()\n","ee.Initialize(project='ee-pb-dev') # <==== Replace this with your own EE project string"],"metadata":{"id":"jCylV9bZ7uKy"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Mount Google Drive\n","\n","To access files within your Google Drive we need to 'mount it' to this colab environment.\n","\n","Again, it will come up with a prompt asking your permission."],"metadata":{"id":"GXovxDg6ACKq"}},{"cell_type":"code","source":["drive.mount('/content/drive')"],"metadata":{"id":"OwHDRgmqDwu1"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Customise Inputs\n","\n","This next section is where you need to specify input variables to control the MODIS Albedo which is read.\n","\n","You need to specify:\n","\n","* The path (on your google drive) to the vector layer specifying your region of interest. The vector layer can be any format that Geopandas is able to read (e.g., Shapefile, GeoJSON, GPKG).\n","* The start date (i.e., earlier date) for the image search\n","* The end date (i.e., later date) for the image search\n","* The output no date value used - should always be 0\n","* The output image file name (Note, do not specify the file extention - it will have .tif added when created)\n","* The output directory on your google drive where the output image will be saved.\n","\n"],"metadata":{"id":"vcJpQiNWALAU"}},{"cell_type":"code","source":["vec_file = '/content/drive/MyDrive/test_shp/test_roi_poly.shp'\n","start_year = 2010\n","start_month = 1\n","n_months = 11 # In addition to start month so this will output a total of 12 months.\n","out_no_data_val = 0.0\n","modis_band_name = \"Albedo_BSA_vis\" # This is the band which will be outputted.\n","out_file_name = \"modis_albedo_month_2010\"\n","out_gdrive_dir = \"test_gee_outs\""],"metadata":{"id":"1cRLfqJ58ph8"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Processing Stages"],"metadata":{"id":"skUwEf6FBa66"}},{"cell_type":"code","source":["# Read the vector layer and make sure it is project using WGS84 (EPSG:4326)\n","vec_gdf = geopandas.read_file(vec_file).to_crs(4326)\n","\n","# Get layer bbox: minx, miny, maxx, maxy\n","gp_bbox = vec_gdf.total_bounds\n","\n","# Create the GEE geometry from the bbox.\n","roi_west = gp_bbox[0]\n","roi_east = gp_bbox[2]\n","roi_north = gp_bbox[3]\n","roi_south = gp_bbox[1]\n","tile_aoi = ee.Geometry.BBox(roi_west, roi_south, roi_east, roi_north)"],"metadata":{"id":"rPBIl53-6w3J"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Get the list of years and months\n","month_years = pb_gee_tools.utils.create_year_month_n_months_lst(start_year, start_month, n_months)\n","\n","# Iterate through the months\n","img_lst = list()\n","for year, month in month_years:\n"," print(f\"{month} / {year}\")\n"," # Find the date of the last day of the month.\n"," end_day = pb_gee_tools.utils.find_month_end_date(year, month)\n","\n"," # Specify the start and end years to search by\n"," start_date = datetime.datetime(year=year, month=month, day=1)\n"," end_date = datetime.datetime(year=year, month=month, day=end_day)\n","\n"," # Find the MODIS albedo scenes\n"," modis_albedo_col = pb_gee_tools.datasets.get_modis_albedo_collection(\n"," aoi = tile_aoi,\n"," start_date = start_date,\n"," end_date = end_date,\n"," )\n","\n"," # Check how many scenes there are in the returned image collection\n"," n_imgs = modis_albedo_col.size().toInt().getInfo()\n"," print(f\"\\tNumber of images: {n_imgs}\")\n","\n"," if n_imgs > 0:\n"," # If there are more than 0 scenes then calculate the median\n"," # and select the band which is to be outputted and rename it\n"," # to include the month/year.\n"," modis_albedo_med_img = modis_albedo_col.select([modis_band_name], [f\"{modis_band_name}_{year}_{month}\"]).median()\n","\n"," # Add median image to output list.\n"," img_lst.append(modis_albedo_med_img)\n"," else:\n"," print(f\"\\tNo images found for {month} / {year}\")\n","\n","# Create an image collection from the list of monthly medians.\n","monthly_img_col = ee.ImageCollection.fromImages(ee.List(img_lst))"],"metadata":{"id":"aB4L8haiM3gh"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["\n","# Export the median image to your Google Drive\n","task = ee.batch.Export.image.toDrive(\n"," image=monthly_img_col.toBands().int16(),\n"," description=out_file_name,\n"," folder=out_gdrive_dir,\n"," region=tile_aoi,\n"," scale=500,\n"," fileFormat=\"GeoTIFF\",\n"," formatOptions={\"cloudOptimized\": True, 'noData': out_no_data_val},\n",")\n","task.start()"],"metadata":{"id":"XmRznIrU9pFJ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"cLSiHF5RQBGu"},"execution_count":null,"outputs":[]}]} |