Skip to content

Commit

Permalink
Merge pull request #123 from US-GHG-Center/staging
Browse files Browse the repository at this point in the history
Release to Production
  • Loading branch information
paridhi-parajuli authored Sep 17, 2024
2 parents f410bc0 + 53f739c commit 3c3d313
Show file tree
Hide file tree
Showing 58 changed files with 50,694 additions and 4,313 deletions.
29 changes: 25 additions & 4 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ website:
- user_data_notebooks/oco2-mip-co2budget-yeargrid-v1_User_Notebook.ipynb
- user_data_notebooks/odiac-ffco2-monthgrid-v2023_User_Notebook.ipynb
- user_data_notebooks/tm54dvar-ch4flux-monthgrid-v1_User_Notebook.ipynb
- user_data_notebooks/epa-ch4emission-grid-v2express_User_Notebook.ipynb
- user_data_notebooks/epa-ch4emission-grid-v2express_User_Notebook.ipynb
- user_data_notebooks/vulcan-ffco2-yeargrid-v4_User_Notebook.ipynb
- section: Natural Greenhouse Gas Sources Emissions and Sinks
contents:
- user_data_notebooks/eccodarwin-co2flux-monthgrid-v5_User_Notebook.ipynb
Expand All @@ -65,13 +66,17 @@ website:
- user_data_notebooks/oco2-mip-co2budget-yeargrid-v1_User_Notebook.ipynb
- user_data_notebooks/tm54dvar-ch4flux-monthgrid-v1_User_Notebook.ipynb
- user_data_notebooks/lpjeosim-wetlandch4-grid-v2_User_Notebook.ipynb
- user_data_notebooks/gra2pes-ghg-monthgrid-v1_User_Notebook.ipynb
- section: Large Emissions Events
contents:
- user_data_notebooks/emit-ch4plume-v1_User_Notebook.ipynb
- section: Greenhouse Gas Concentrations
contents:
- user_data_notebooks/noaa-insitu_User_Notebook.ipynb
- user_data_notebooks/oco2geos-co2-daygrid-v10r_User_Notebook.ipynb
- user_data_notebooks/influx-testbed-ghg-concentrations_User_Notebook.ipynb
- user_data_notebooks/lam-testbed-ghg-concentrations_User_Notebook.ipynb
- user_data_notebooks/nec-testbed-ghg-concentrations_User_Notebook.ipynb
- section: Socioeconomic
contents:
- user_data_notebooks/sedac-popdensity-yeargrid5yr-v4.11_User_Notebook.ipynb
Expand All @@ -83,20 +88,26 @@ website:
- cog_transformation/oco2-mip-co2budget-yeargrid-v1.ipynb
- cog_transformation/odiac-ffco2-monthgrid-v2023.ipynb
- cog_transformation/tm54dvar-ch4flux-monthgrid-v1.ipynb
- cog_transformation/epa-ch4emission-grid-v2express.ipynb
- cog_transformation/epa-ch4emission-grid-v2express.ipynb
- cog_transformation/vulcan-ffco2-yeargrid-v4.ipynb
- section: Natural Greenhouse Gas Sources Emissions and Sinks
contents:
- cog_transformation/eccodarwin-co2flux-monthgrid-v5.ipynb
- cog_transformation/gosat-based-ch4budget-yeargrid-v1.ipynb
- cog_transformation/oco2-mip-co2budget-yeargrid-v1.ipynb
- cog_transformation/tm54dvar-ch4flux-monthgrid-v1.ipynb
- cog_transformation/gra2pes-ghg-monthgrid-v1.ipynb
- section: Large Emissions Events
contents:
- cog_transformation/emit-ch4plume-v1.ipynb
- section: Greenhouse Gas Concentrations
contents:
- cog_transformation/noaa-gggrn-concentrations.ipynb
- cog_transformation/oco2geos-co2-daygrid-v10r.ipynb
- cog_transformation/influx-testbed-ghg-concentrations.ipynb
- cog_transformation/lam-testbed-ghg-concentrations.ipynb
- cog_transformation/nec-testbed-ghg-concentrations.ipynb

- section: Socioeconomic
contents:
- cog_transformation/sedac-popdensity-yeargrid5yr-v4.11.ipynb
Expand All @@ -108,7 +119,8 @@ website:
- processing_and_verification_reports/oco2-mip-co2budget-yeargrid-v1_Processing and Verification Report.qmd
- processing_and_verification_reports/odiac-ffco2-monthgrid-v2023_Processing and Verification Report.qmd
- processing_and_verification_reports/tm54dvar-ch4flux-monthgrid-v1_Processing and Verification Report.qmd
- processing_and_verification_reports/epa-ch4emission-grid-v2express_Processing and Verification Report.qmd
- processing_and_verification_reports/epa-ch4emission-grid-v2express_Processing and Verification Report.qmd
- processing_and_verification_reports/vulcan-ffco2-yeargrid-v4_Processing and Verification Report.qmd
- section: Natural Greenhouse Gas Sources Emissions and Sinks
contents:
- processing_and_verification_reports/eccodarwin-co2flux-monthgrid-v5_Processing and Verification Report.qmd
Expand All @@ -117,6 +129,7 @@ website:
- processing_and_verification_reports/oco2-mip-co2budget-yeargrid-v1_Processing and Verification Report.qmd
- processing_and_verification_reports/tm54dvar-ch4flux-monthgrid-v1_Processing and Verification Report.qmd
- processing_and_verification_reports/lpjeosim-wetlandch4-grid-v2_Processing and Verification Report.qmd
- processing_and_verification_reports/gra2pes-ghg-monthgrid-v1_Processing and Verification Report.qmd
- section: Large Emissions Events
contents:
- processing_and_verification_reports/emit-ch4plume-v1_Processing and Verification Report.qmd
Expand All @@ -125,6 +138,9 @@ website:
- processing_and_verification_reports/noaa-gggrn-co2-concentrations_Processing and Verification Report.qmd
- processing_and_verification_reports/noaa-gggrn-ch4-concentrations_Processing and Verification Report.qmd
- processing_and_verification_reports/oco2geos-co2-daygrid-v10r_Processing and Verification Report.qmd
- processing_and_verification_reports/influx-testbed-ghg-concentrations_Processing and Verification Report.qmd
- processing_and_verification_reports/lam-testbed-ghg-concentrations_Processing and Verification Report.qmd
- processing_and_verification_reports/nec-testbed-ghg-concentrations_Processing and Verification Report.qmd
- section: Socioeconomic
contents:
- processing_and_verification_reports/sedac-popdensity-yeargrid5yr-v4.11_Processing and Verification Report.qmd
Expand All @@ -136,7 +152,8 @@ website:
- data_workflow/oco2-mip-co2budget-yeargrid-v1_Data_Flow.qmd
- data_workflow/odiac-ffco2-monthgrid-v2023_Data_Flow.qmd
- data_workflow/tm54dvar-ch4flux-monthgrid-v1_Data_Flow.qmd
- data_workflow/epa-ch4emission-grid-v2express_Data_Flow.qmd
- data_workflow/epa-ch4emission-grid-v2express_Data_Flow.qmd
- data_workflow/vulcan-ffco2-yeargrid-v4_Data_Flow.qmd
- section: Natural Greenhouse Gas Sources Emissions and Sinks
contents:
- data_workflow/eccodarwin-co2flux-monthgrid-v5_Data_Flow.qmd
Expand All @@ -145,6 +162,7 @@ website:
- data_workflow/oco2-mip-co2budget-yeargrid-v1_Data_Flow.qmd
- data_workflow/tm54dvar-ch4flux-monthgrid-v1_Data_Flow.qmd
- data_workflow/lpjeosim-wetlandch4-grid-v2_Data_Flow.qmd
- data_workflow/gra2pes-ghg-monthgrid-v1_Data_Flow.qmd
- section: Large Emissions Events
contents:
- data_workflow/emit-ch4plume-v1_Data_Flow.qmd
Expand All @@ -153,6 +171,9 @@ website:
- data_workflow/noaa-gggrn-co2-concentrations_Data_Flow.qmd
- data_workflow/noaa-gggrn-ch4-concentrations_Data_Flow.qmd
- data_workflow/oco2geos-co2-daygrid-v10r_Data_Flow.qmd
- data_workflow/influx-testbed-ghg-concentrations_Data_Flow.qmd
- data_workflow/lam-testbed-ghg-concentrations_Data_Flow.qmd
- data_workflow/nec-testbed-ghg-concentrations_Data_Flow.qmd
- section: Socioeconomic
contents:
- data_workflow/sedac-popdensity-yeargrid5yr-v4.11_Data_Flow.qmd
Expand Down
221 changes: 221 additions & 0 deletions cog_transformation/gra2pes-ghg-monthgrid-v1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
{
"cells": [
{
"cell_type": "raw",
"id": "0190df52-e0dc-4c6e-9a94-c290c7b86ef9",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"title: GRA²PES Greenhouse Gas and Air Quality Species\n",
"description: Monthly, 0.036 degree resolution emissions of fossil fuel carbon dioxide (ffCO₂), carbon monoxide (CO), nitrogen oxide (NOₓ), sulfur dioxide (SO₂), and particulate matter (PM₂.₅) emissions for the year 2021 over the Contiguous United States from the Greenhouse gas And Air Pollutants Emissions System (GRA²PES).\n",
"author: Siddharth Chaudhary, Paridhi Parajuli\n",
"date: 30 August 2024\n",
"execute:\n",
" freeze: true\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "6477da5d-92e7-4d79-b49c-2c29a11ec1a1",
"metadata": {},
"source": [
"This script was used to transform the GRA2PES dataset to Cloud Optimized GeoTIFF (COG) format for display in the Greenhouse Gas (GHG) Center."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca65368b-5280-4685-948e-d92fb20c9318",
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import os\n",
"import glob\n",
"from datetime import datetime\n",
"import boto3\n",
"import s3fs\n",
"import tempfile\n",
"import numpy as np\n",
"\n",
"import rasterio\n",
"from rasterio.enums import Resampling\n",
"from rio_cogeo.cogeo import cog_translate\n",
"from rio_cogeo.profiles import cog_profiles\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d8ad9e2-e18b-4ca8-b68e-fadbe14d112a",
"metadata": {},
"outputs": [],
"source": [
"config = {\n",
" \"data_acquisition_method\": \"s3\",\n",
" \"raw_data_bucket\" : \"gsfc-ghg-store\",\n",
" \"raw_data_prefix\": \"GRA2PES/monthly_subset_regrid/2021\", \n",
" \"cog_data_bucket\": \"ghgc-data-store-develop\",\n",
" \"cog_data_prefix\": \"transformed_cogs/GRAAPES\",\n",
" \"date_fmt\" :\"%Y%m\",\n",
" \"transformation\": {}\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7a394ef-d598-4769-aeb5-d22447b2d5f0",
"metadata": {},
"outputs": [],
"source": [
"session = boto3.session.Session()\n",
"s3_client = session.client(\"s3\")\n",
"\n",
"raw_data_bucket = config[\"raw_data_bucket\"]\n",
"raw_data_prefix= config[\"raw_data_prefix\"]\n",
"\n",
"cog_data_bucket = config['cog_data_bucket']\n",
"cog_data_prefix= config[\"cog_data_prefix\"]\n",
"\n",
"date_fmt=config['date_fmt']\n",
"\n",
"fs = s3fs.S3FileSystem()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04d90bf6-cd92-466e-9b23-23b927810de6",
"metadata": {},
"outputs": [],
"source": [
"def get_all_s3_keys(bucket, model_name, ext):\n",
" \"\"\"Get a list of all keys in an S3 bucket.\"\"\"\n",
" keys = []\n",
"\n",
" kwargs = {\"Bucket\": bucket, \"Prefix\": f\"{model_name}/\"}\n",
" while True:\n",
" resp = s3_client.list_objects_v2(**kwargs)\n",
" for obj in resp[\"Contents\"]:\n",
" if obj[\"Key\"].endswith(ext) and \"historical\" not in obj[\"Key\"]:\n",
" keys.append(obj[\"Key\"])\n",
"\n",
" try:\n",
" kwargs[\"ContinuationToken\"] = resp[\"NextContinuationToken\"]\n",
" except KeyError:\n",
" break\n",
"\n",
" return keys\n",
"\n",
"keys = get_all_s3_keys(raw_data_bucket, raw_data_prefix, \".nc4\")\n",
"\n",
"def download_s3_objects(bucket, keys, download_dir):\n",
" \"\"\"Download all S3 objects listed in keys to the specified local directory.\"\"\"\n",
" if not os.path.exists(download_dir):\n",
" os.makedirs(download_dir)\n",
"\n",
" for key in keys:\n",
" local_filename = os.path.join(download_dir, os.path.basename(key))\n",
" try:\n",
" s3_client.download_file(bucket, key, local_filename)\n",
" print(f\"Downloaded {key} to {local_filename}\")\n",
" except (NoCredentialsError, PartialCredentialsError) as e:\n",
" print(f\"Credentials error: {e}\")\n",
" except Exception as e:\n",
" print(f\"Failed to download {key}: {e}\")\n",
"\n",
"download_s3_objects(raw_data_bucket, keys, \"data\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b74c98b4-7059-4f47-b6b2-ae9be7bc62dc",
"metadata": {},
"outputs": [],
"source": [
"def extract_date_from_key(key):\n",
" # Split the key to isolate the part that contains the date\n",
" parts = key.split('_')\n",
" for part in parts:\n",
" # Check if the part is numeric and has the length of 6 (YYYYMM format)\n",
" if part.isdigit() and len(part) == 6:\n",
" return part\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c273250-65f3-4e28-905c-d27ef4e373e3",
"metadata": {},
"outputs": [],
"source": [
"COG_PROFILE = {\"driver\": \"COG\", \"compress\": \"DEFLATE\"}\n",
"OVERVIEW_LEVELS = 4 \n",
"OVERVIEW_RESAMPLING = 'average'\n",
"\n",
"for key in glob.glob(\"data/*.nc4\"):\n",
" xds= xr.open_dataset(key)\n",
" xds = xds.assign_coords(lon=(((xds.lon + 180) % 360) - 180)).sortby(\"lon\")\n",
" \n",
" for var in [\"PM25-PRI\",\"CO2\",\"CO\",\"NOX\",\"SOX\"]:\n",
" yearmonth = extract_date_from_key(key)\n",
" filename = f\"output/GRA2PESv1.0_total_{(\"-\").join(var.split('_'))}_{yearmonth}.tif\"\n",
" data = getattr(xds,var)\n",
" data.rio.set_spatial_dims(\"lon\", \"lat\", inplace=True)\n",
" data.rio.write_crs(\"epsg:4326\", inplace=True)\n",
" \n",
" # Create a temporary file to hold the COG\n",
" with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as temp_file:\n",
" data.rio.to_raster(f\"temp_{yearmonth}_{var}.tif\", **COG_PROFILE, nodata=-9999)\n",
" # Create COG with overviews and nodata value\n",
" cog_translate(\n",
" f\"temp_{yearmonth}_{var}.tif\",\n",
" temp_file.name,\n",
" cog_profiles.get(\"deflate\"),\n",
" overview_level=OVERVIEW_LEVELS,\n",
" overview_resampling=OVERVIEW_RESAMPLING,\n",
" nodata=-9999\n",
" )\n",
" \n",
" # Move the temporary file to the desired local path\n",
" os.rename(temp_file.name, filename)\n",
" \n",
" if os.path.exists(f\"temp_{yearmonth}_{var}.tif\"):\n",
" os.remove(f\"temp_{yearmonth}_{var}.tif\")\n",
" del data\n",
" print(f\"Done for: {filename}\")\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 3c3d313

Please sign in to comment.