!pip install arcgis arcgis-mapping rasterio earthaccess
Requirement already satisfied: arcgis in c:\users\jmartine\appdata\local\esri\conda\envs\agsdev\lib\site-packages (2.2.0)
Jacom Orser
A regional view of the past with applications to the present. Python spatial and tabular analysis using multiple variables to look at a single time slice. This code can be a combination of cloud and local based analysis. A purpose for this exercise is to use use public research data for derived conclusions view the impacts climate change has on agriculture a look at what farmers can do and why we need to support them.
NASA Acres has defined the Essential Agricultural Variables or EAVs. These were designed by NASA Acres to define the capabilities of the top satellite data scientists and practitioners who make up NASA Acres Research, Development, and Extension partners, and the needs of decision-making-collaborators already in our network, to identify an initial set of focus.
Cropland and Crop Type Mapping
Crop and Crop Type Area Estimation
Derived results will be the county level statistics of impacts of harvestable acreage. To achieve this, a basic workflow we can
Then
Access the Sea level rise (elevation dataset) to identify new areas of potential areas that are at risk of future flooding. Clip to county of interest
Compare the CDL with the SLR mask and with out to identify the NOAA estimated loss of land.
Then using MODIS create a time series for NDVI of the masked crop land, derive insights about trends in NDVI
This all together would allow us to make a predictive analysis for Maryland in the future under the projections of sea level rise. Given the current conditions, subtracting the sea level rise inundated areas. Then given the remaining areas, making predictions about the trends of NDVI given the trends in historical areas that are near impacted areas. We cannot say that “sea level rise is driving the decrease” because there are many other factor and decisions that farmers make around how much to plant, how much to harvest, chemicals applied, growing degree days, soil and soil moisture conditions. But this module should provide the ability to make insights about potential causes and impacts.
The data story that we have derived is about sea level rise in Maryland and the impact that it has on the production levels with in the state. From this module, we can provide students with the ability to draw from multiple data sources, as well as derive insights using historical and future viewing data sets.
The U.S. Mid-Atlantic has seen higher rates of sea level rise, marshes may be especially vulnerable. “In the Chesapeake Bay, sea level rise has already contributed to the degradation of over 80,000 ha (70%) of tidal marsh” (Taylor et al. 2020). This view can help us to understand the impacts that small levels of sea level rise have on land.
We can prompt the user to think about future impacts outside the direct sea level rise projections, allowing them to include a more full picture, and finally using that picture to identify economic impacts that action or inaction causes. This begs the question, what can the public do to enact changes, rather than putting the pressure on farmers to change?
Requirement already satisfied: arcgis in c:\users\jmartine\appdata\local\esri\conda\envs\agsdev\lib\site-packages (2.2.0)
#import the required packages
import geopandas as gpd
import rasterio
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import box
import numpy as np
import requests
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
from io import BytesIO
import os
from rasterio.warp import calculate_default_transform, reproject, Resampling
from datetime import datetime, timedelta
#define fuctions
def reproject_raster(src_dataset, src_crs, src_transform, dst_crs='EPSG:4326'):
dst_crs = rasterio.crs.CRS.from_string(dst_crs)
dst_transform, dst_width, dst_height = calculate_default_transform(
src_crs, dst_crs,
src_dataset.width, src_dataset.height,
*src_dataset.bounds)
dst_kwargs = src_dataset.meta.copy()
dst_kwargs.update({
'crs': dst_crs,
'transform': dst_transform,
'width': dst_width,
'height': dst_height})
dst_data = np.zeros((src_dataset.count, dst_height, dst_width), dtype=src_dataset.dtypes[0])
for i in range(1, src_dataset.count + 1):
reproject(
source=rasterio.band(src_dataset, i),
destination=dst_data[i-1],
src_transform=src_transform,
src_crs=src_crs,
dst_transform=dst_transform,
dst_crs=dst_crs,
resampling=Resampling.nearest)
return dst_data, dst_kwargs
def format_cdl_url(fips,year):
base_url = "https://nassgeodata.gmu.edu/axis2/services/CDLService/GetCDLFile"
url = f"{base_url}?year={year}&fips={fips}"
return url
def get_srl_raster():
slr_path = 'data/MD_East_slr_depth_3_5ft.tif'
with rasterio.open(slr_path) as slr_raster:
slr_meta = slr_raster.meta
slr_meta['nodata'] = 0
slr_reprojected, slr_meta_reprojected = reproject_raster(src_dataset=slr_raster, src_crs=slr_meta['crs'], src_transform=slr_meta['transform'])
return slr_reprojected, slr_meta_reprojected
To understand the nature of Maryland agriculture we can begin by using the CDL. This raster dataset comes at a 30 meter spatial resolution. We can access this data through their api. The current configuration allows data to be pulled at the county level.
COUNTY LEVEL CDL FOR FIPS https://nassgeodata.gmu.edu/axis2/services/CDLService/GetCDLFile
This view provides insight into the common commodities that are grown in Maryland in the desired year.
crop_mapping = {
0: "Background", 1: "Corn", 2: "Cotton", 3: "Rice",
4: "Sorghum", 5: "Soybeans", 6: "Sunflower", 10: "Peanuts",
11: "Tobacco", 12: "Sweet Corn", 13: "Pop or Orn Corn",
14: "Mint", 21: "Barley", 22: "Durum Wheat",23: "Spring Wheat",
24: "Winter Wheat", 25: "Other Small Grains", 26: "Dbl Crop WinWht/Soybeans",
27: "Rye", 28: "Oats", 29: "Millet", 30: "Speltz", 31: "Canola",
32: "Flaxseed", 33: "Safflower", 34: "Rape Seed",35: "Mustard",
36: "Alfalfa", 37: "Other Hay/Non Alfalfa", 38: "Camelina", 39: "Buckwheat",
41: "Sugarbeets", 42: "Dry Beans", 43: "Potatoes", 44: "Other Crops",
45: "Sugarcane", 46: "Sweet Potatoes", 47: "Misc Vegs & Fruits",
48: "Watermelons", 49: "Onions", 50: "Cucumbers", 51: "Chick Peas",
52: "Lentils", 53: "Peas", 54: "Tomatoes", 55: "Caneberries", 56: "Hops",
57: "Herbs", 58: "Clover/Wildflowers", 59: "Sod/Grass Seed", 60: "Switchgrass",
61: "Fallow/Idle Cropland", 63: "Forest", 64: "Shrubland", 65: "Barren",
66: "Cherries", 67: "Peaches", 68: "Apples", 69: "Grapes",
70: "Christmas Trees", 71: "Other Tree Crops", 72: "Citrus", 74: "Pecans",
75: "Almonds", 76: "Walnuts", 77: "Pears", 81: "Clouds/No Data",
82: "Developed", 83: "Water", 87: "Wetlands", 88: "Nonag/Undefined",
92: "Aquaculture", 111: "Open Water", 112: "Perennial Ice/Snow",
121: "Developed/Open Space",122: "Developed/Low Intensity",
123: "Developed/Med Intensity", 124: "Developed/High Intensity",
131: "Barren", 141: "Deciduous Forest", 142: "Evergreen Forest",
143: "Mixed Forest", 152: "Shrubland", 176: "Grass/Pasture",
190: "Woody Wetlands", 195: "Herbaceous Wetlands", 204: "Pistachios",
205: "Triticale", 206: "Carrots", 207: "Asparagus", 208: "Garlic",
209: "Cantaloupes", 210: "Prunes", 211: "Olives", 212: "Oranges",
213: "Honeydew Melons", 214: "Broccoli", 215: "Avocados", 216: "Peppers",
217: "Pomegranates", 218: "Nectarines", 219: "Greens", 220: "Plums",
221: "Strawberries", 222: "Squash", 223: "Apricots", 224: "Vetch",
225: "Dbl Crop WinWht/Corn", 226: "Dbl Crop Oats/Corn", 227: "Lettuce",
228: "Dbl Crop Triticale/Corn", 229: "Pumpkins",
230: "Dbl Crop Lettuce/Durum Wht", 231: "Dbl Crop Lettuce/Cantaloupe",
232: "Dbl Crop Lettuce/Cotton",233: "Dbl Crop Lettuce/Barley",
234: "Dbl Crop Durum Wht/Sorghum", 235: "Dbl Crop Barley/Sorghum",
236: "Dbl Crop WinWht/Sorghum", 237: "Dbl Crop Barley/Corn",
238: "Dbl Crop WinWht/Cotton",239: "Dbl Crop Soybeans/Cotton",
240: "Dbl Crop Soybeans/Oats", 241: "Dbl Crop Corn/Soybeans",
242: "Blueberries", 243: "Cabbage", 244: "Cauliflower", 245: "Celery",
246: "Radishes", 247: "Turnips", 248: "Eggplants",249: "Gourds",
250: "Cranberries", 254: "Dbl Crop Barley/Soybeans", 255: ""
}
service_url = format_cdl_url(code, year)
response = requests.get(service_url)
root = ET.fromstring(response.content)
tif_url = root.find('.//returnURL').text
cdl_data = rasterio.open(tif_url)
cdl_meta = cdl_data.meta
cdl_reprojected, cdl_meta_reprojected = reproject_raster(src_dataset=cdl_data, src_crs=cdl_meta['crs'], src_transform=cdl_meta['transform'])
The eastern seaboard has seen changes in the sea level along with increase flooding and the decrease in predictability of water flow from rivers. This daa comes from NOAA (learn more here https://coast.noaa.gov/data/digitalcoast/pdf/slr-inundation-methods.pdf)
This raster is the projection of inudated areas by the year of 2050. Data access can be found here https://coast.noaa.gov/slrdata/Depth_Rasters/MD/index.html
def get_appeears_token(username, password):
response = requests.post('https://appeears.earthdatacloud.nasa.gov/api/login',auth=(username, password))
return response.json()['token']
def submit_appeears_task(token, task):
response = requests.post('https://appeears.earthdatacloud.nasa.gov/api/task',json=task,headers={'Authorization': f'Bearer {token}'})
return response.json()['task_id']
def get_appeears_bundle(token, task_id):
response = requests.get(f'https://appeears.earthdatacloud.nasa.gov/api/bundle/{task_id}',headers={'Authorization': f'Bearer {token}'})
return response.json()
def load_county_data():
sea_level_path = 'data/Talbot_County.geojson'
sea_level_gdf = gpd.read_file(sea_level_path)
return sea_level_gdf
def day_of_year_to_date2(day_of_year_str):
year = int(day_of_year_str[:4])
day_of_year = int(day_of_year_str[4:])
start_date = datetime(year, 1, 1)
target_date = start_date + timedelta(days=day_of_year - 1)
return target_date.strftime('%m-%d-%Y')
def resample_using_cdl(data, data_meta, cdl_meta_reprojected):
resampled_data = np.zeros(
(data.shape[0], cdl_meta_reprojected['height'],
cdl_meta_reprojected['width']),
dtype=data.dtype
)
for i in range(data.shape[0]):
reproject(
source=data[i],
destination=resampled_data[i],
src_transform=data_meta['transform'],
src_crs=data_meta['crs'],
dst_transform=cdl_meta_reprojected['transform'],
dst_crs=cdl_meta_reprojected['crs'],
resampling=Resampling.bilinear
)
updated_meta = data_meta.copy()
updated_meta.update({
'transform': cdl_meta_reprojected['transform'],
'width': cdl_meta_reprojected['width'],
'height': cdl_meta_reprojected['height'],
'crs': cdl_meta_reprojected['crs']
})
return resampled_data, updated_meta
task = {
'task_type': 'area',
'task_name': 'MODIS',
'params': {
"geo": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[bbox]]},
"properties": {}}],
"fileName": "Polygon"},
'dates': [{
'startDate': start, 'endDate': end}],
'layers': products,
'coordinates': bbox,
"output": {
"format": {
"type": "geotiff"},
"projection": "native"}
}}
{'files': [{'sha256': 'f426fecf94dbabac4ba39d29517b21bf1dfc6b11f8c9fdea8f8962a473c80e5c',
'file_id': '06026329-340d-427b-9e0a-f205d1942115',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018145_aid0001.tif',
'file_size': 54192,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018145_aid0001.tif'},
{'sha256': 'a451f917fb7ceb0cf832db9bb93a4505bb8a93d0db35527c89d4c00d41fefbd6',
'file_id': 'c6a323c9-8ddc-47e8-a66a-c5b346f7f4ea',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018161_aid0001.tif',
'file_size': 56241,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018161_aid0001.tif'},
{'sha256': '1bdac97bfb07ba3786a2a0b7a4f96800cb3fe4e25720b0c83183e5236ec003a5',
'file_id': '80b6f11d-8403-49a5-865e-7b96c5cb5c28',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018177_aid0001.tif',
'file_size': 57505,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018177_aid0001.tif'},
{'sha256': '15e9e131f646e83c4554c591d62446ea5602cea90de10a827ba888e1f41d0605',
'file_id': '912b3149-9c36-4430-8b2a-377dd0e9abfe',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018193_aid0001.tif',
'file_size': 57165,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018193_aid0001.tif'},
{'sha256': 'bb9ddb3c1ab2f6eda472074c8ff47d7a97cacb1944fe6663e9dd4193048d138a',
'file_id': '897f9eb3-2fc0-43c3-8737-494220cdb2e6',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018209_aid0001.tif',
'file_size': 54054,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018209_aid0001.tif'},
{'sha256': '6804fa2f8b2ee6fabd37376209642dd5368bfba8bea81dc16450274e1827992d',
'file_id': '40d7acb5-7cdf-49b9-b92b-2ef89e280534',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018225_aid0001.tif',
'file_size': 52560,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018225_aid0001.tif'},
{'sha256': 'e321957172e1eeb0bfe6f67326b82a537e43852ff8641487e95e5d4c6d2e4338',
'file_id': 'cf9f7bf2-fbb1-4755-bd4b-eeb08238231d',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018241_aid0001.tif',
'file_size': 50933,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018241_aid0001.tif'},
{'sha256': '0ab4e649f7c72dd09bae207995dd6b1aa9342f57e180b230b7bf790075ae9b1c',
'file_id': '860308e7-dc79-4da4-b6da-fbbb8f10e809',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018257_aid0001.tif',
'file_size': 55061,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018257_aid0001.tif'},
{'sha256': 'c338089ea6fb0b1ed4caf0a6743109c18728cb4a3d036accf913a463954559bc',
'file_id': '99825a11-cb86-463b-94be-324adeb81883',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018273_aid0001.tif',
'file_size': 53441,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018273_aid0001.tif'},
{'sha256': '77708f2f869c61da2a03d2bf864dbced4e825f19d00089ffdda865a319fb1572',
'file_id': '34268edc-e179-4339-a4f2-563352625a3f',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018145_aid0001.tif',
'file_size': 11001,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018145_aid0001.tif'},
{'sha256': '1855c0dbc0cf9e7fd815770f3a307c6e95090b75e28a1da80cb88ecd42863523',
'file_id': '6943e283-dea6-48f1-be3b-29bb16e42325',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018161_aid0001.tif',
'file_size': 8297,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018161_aid0001.tif'},
{'sha256': '49025fd4b203356549cb892781d5e2f447cf81e10e6fd6a9ca1eecdba82d407f',
'file_id': '47b7ff8c-dea5-46a7-9015-fca83e7029c5',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018177_aid0001.tif',
'file_size': 8634,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018177_aid0001.tif'},
{'sha256': '33dc10c5a7c4d992bf65678135e9fe596284cff5669b0949b356f49de6a75f57',
'file_id': '0a993029-e0e9-479d-b2df-7870d9153619',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018193_aid0001.tif',
'file_size': 8810,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018193_aid0001.tif'},
{'sha256': '22b530f8723255bc8b02be26e0a589764b37e2864194e1890ce74a40969e16cb',
'file_id': '79975fbd-7c0b-4dd6-b6c4-61f9f544c396',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018209_aid0001.tif',
'file_size': 9678,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018209_aid0001.tif'},
{'sha256': '9fd45b7aca302516cba8f3fda1084371fc9b106d1b553d2fb0e669fefc2d2954',
'file_id': '26188217-c64e-445d-8921-da20d2b43ece',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018225_aid0001.tif',
'file_size': 9868,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018225_aid0001.tif'},
{'sha256': '284bafd6bdf4790adb077b1fa330131de892d18bc4df3910a9fd750689836458',
'file_id': '08d4f42d-772e-4e1d-97cc-ec9da8f3d283',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018241_aid0001.tif',
'file_size': 10668,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018241_aid0001.tif'},
{'sha256': '84189b152d0de6adbb6c612b44258cffb20ceae80fe5b2bb0c8ea70943fb9d36',
'file_id': '32214eaf-24a2-40b3-aea0-76e67a7420a6',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018257_aid0001.tif',
'file_size': 14500,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018257_aid0001.tif'},
{'sha256': 'eee9e3c355e57918b7a64ce9ad7132fdc1f89dd989744af40b5d671c668f9b90',
'file_id': '21b381ab-0be3-4e11-8ff1-a8e0e358c486',
'file_name': 'MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018273_aid0001.tif',
'file_size': 10573,
'file_type': 'tif',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_VI_Quality_doy2018273_aid0001.tif'},
{'sha256': '179ace9770f31d37844bf3862837cd10a92c909678f16548679bb478f7643251',
'file_id': '58cc9715-48ec-4ce3-b530-0d78ad1ec3e6',
'file_name': 'MOD13Q1-061-250m-16-days-VI-Quality-lookup.csv',
'file_size': 11223,
'file_type': 'csv',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1-061-250m-16-days-VI-Quality-lookup.csv'},
{'sha256': '3c76047dcf790c4a7cc19e9eec45f5a6289bd1c2d68486a5d67bf66d0ee12b6f',
'file_id': '7229d738-b271-4dea-8a68-efb5182cedfa',
'file_name': 'MOD13Q1-061-250m-16-days-VI-Quality-Statistics-QA.csv',
'file_size': 3036,
'file_type': 'csv',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1-061-250m-16-days-VI-Quality-Statistics-QA.csv'},
{'sha256': 'c0f592fc8dfec7640ca7d2ef6450668db9817310c654bb2242775f761d44b3ff',
'file_id': 'd8b696bf-8d58-4aed-9959-292c8a0bf917',
'file_name': 'MOD13Q1-061-Statistics.csv',
'file_size': 1881,
'file_type': 'csv',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MOD13Q1-061-Statistics.csv'},
{'sha256': 'f1d26f1db61b8ec427d6fe09def21125b8946a6fe5f69fd048191234b0b2e150',
'file_id': '56910ee2-7d34-4bb7-9d05-5177664d599f',
'file_name': 'MODIS-granule-list.txt',
'file_size': 1458,
'file_type': 'txt',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MODIS-granule-list.txt'},
{'sha256': 'f409fdceacdd67014debc6e6aa123220e7fe0eea4061e1bd44a61721a6ebfdb3',
'file_id': 'ff9671d6-76fc-4453-bf1e-a11feb46dcde',
'file_name': 'MODIS-request.json',
'file_size': 1384,
'file_type': 'json',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MODIS-request.json'},
{'sha256': '4d4d9d7655c0b8ae855cf1777b30effd2451abb8a47924ad2335fc65ef967c53',
'file_id': '2d20cfa6-8980-437e-968c-52cf9645bf31',
'file_name': 'MODIS-MOD13Q1-061-metadata.xml',
'file_size': 22077,
'file_type': 'xml',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/MODIS-MOD13Q1-061-metadata.xml'},
{'sha256': '4eb00787c767afe38cfc5f2a0174f5e25f2440df3024cdf118d7199cdf9c3bde',
'file_id': 'efa079e4-3a64-4b22-ae53-986f7daa037d',
'file_name': 'README.md',
'file_size': 37507,
'file_type': 'txt',
's3_url': 's3://appeears-output/d09681c4-2da0-4513-a721-a041285438bf/README.md'}],
'created': '2024-12-18T21:32:26.298902',
'task_id': 'd09681c4-2da0-4513-a721-a041285438bf',
'updated': '2024-12-18T21:32:40.123095',
'bundle_type': 'area'}
data = {}
for file in bundle['files']:
file_id = file['file_id']
if 'NDVI' in file['file_name']:
if '_doy' in file['file_name']:
datesy = file['file_name'].split('_doy')[1][:7]
doy = day_of_year_to_date2(datesy)
print(file['file_name'])
else:
continue
file_download = requests.get(
'https://appeears.earthdatacloud.nasa.gov/api/bundle/{0}/{1}'.format(task_id, file_id),
headers={'Authorization': 'Bearer {0}'.format(token)},
allow_redirects=True,
stream=True)
file_download.raise_for_status()
if not file_download.content:
print(f"Warning: Empty file downloaded for {file['file_name']}")
continue
file_content = BytesIO(file_download.content)
with rasterio.open(file_content) as src_initial:
src = src_initial.read(1, masked=True)
src_meta = src_initial.meta
dst_crs = cdl_data.crs
transform, width, height = calculate_default_transform(
src_meta['crs'], dst_crs, src.shape[1], src.shape[0], *src_initial.bounds)
kwargs = src_meta.copy()
kwargs.update({
'crs': dst_crs,
'transform': transform,
'width': width,
'height': height
})
dst = np.zeros((height, width), dtype=src_meta['dtype'])
reproject(
source=src,
destination=dst,
src_transform=src_meta['transform'],
src_crs=src_meta['crs'],
dst_transform=transform,
dst_crs=dst_crs,
resampling=Resampling.nearest)
kwargs.update({'nodata': 0})
key = 'MODIS_NDVI'
if key not in data:
data[key] = {'data': [], 'meta': kwargs, 'doy': []}
data[key]['data'].append(dst)
data[key]['doy'].append(doy)
else:
continue
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018145_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018161_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018177_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018193_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018209_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018225_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018241_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018257_aid0001.tif
MOD13Q1.061_2018137_to_2018273/MOD13Q1.061__250m_16_days_NDVI_doy2018273_aid0001.tif
ndvi_resampled, ndvi_resampled_meta = resample_using_cdl(ndvi, meta, cdl_meta_reprojected)
slr_resampled, slr_resampled_meta = resample_using_cdl(slr_reprojected, slr_meta_reprojected, cdl_meta_reprojected)
print("CDL shape:", cdl_reprojected.shape)
print("Original NDVI shape:", ndvi.shape)
print("Resampled NDVI shape:", ndvi_resampled.shape)
print("Original SLR shape:", slr_reprojected.shape)
print("Resampled SLR shape:", slr_resampled.shape)
CDL shape: (1, 1681, 2153)
Original NDVI shape: (9, 166, 402)
Resampled NDVI shape: (9, 1681, 2153)
Original SLR shape: (1, 44790, 28460)
Resampled SLR shape: (1, 1681, 2153)
def mask_rasters(cdl_raster, slr_raster, ndvi_rasters):
corn_mask = cdl_raster == 1
ABOVE_water_mask = slr_raster <= 1
corn_above_water_mask = corn_mask & ~ABOVE_water_mask
corn_below_water_mask = corn_mask & ABOVE_water_mask
corn_above_water_mask = corn_above_water_mask[0,:,:]
corn_below_water_mask = corn_below_water_mask[0,:,:]
masked_ndvi_below_water = ndvi_rasters[:, corn_below_water_mask]
masked_ndvi_above_water = ndvi_rasters[:, corn_above_water_mask]
return {
'corn_below_water': {
'mask': corn_below_water_mask,
'ndvi': masked_ndvi_below_water,
'mean': np.mean(masked_ndvi_below_water, axis=1) if masked_ndvi_below_water.size > 0 else None,
'min': np.min(masked_ndvi_below_water, axis=1) if masked_ndvi_below_water.size > 0 else None,
'max': np.max(masked_ndvi_below_water, axis=1) if masked_ndvi_below_water.size > 0 else None
},
'corn_above_water': {
'mask': corn_above_water_mask,
'ndvi': masked_ndvi_above_water,
'mean': np.mean(masked_ndvi_above_water, axis=1) if masked_ndvi_above_water.size > 0 else None,
'min': np.min(masked_ndvi_above_water, axis=1) if masked_ndvi_above_water.size > 0 else None,
'max': np.max(masked_ndvi_above_water, axis=1) if masked_ndvi_above_water.size > 0 else None
}
}
def visualize_ndvi_analysis(ndvi_analysis, days):
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(days, ndvi_analysis['corn_above_water']['mean'], label='Corn Above Water', color='green')
plt.plot(days, ndvi_analysis['corn_below_water']['mean'], label='Corn Below Water', color='blue')
plt.title('Mean NDVI')
plt.xlabel('Days')
plt.xticks(rotation=90)
plt.ylabel('NDVI')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(days, ndvi_analysis['corn_above_water']['max'], label='Corn Above Water', color='green')
plt.plot(days, ndvi_analysis['corn_below_water']['max'], label='Corn Below Water', color='blue')
plt.title('Maximum NDVI')
plt.xlabel('Days')
plt.xticks(rotation=90)
plt.ylabel('NDVI')
plt.legend()
plt.tight_layout()
plt.show()
ndvi_analysis = mask_rasters(cdl_reprojected, slr_resampled, ndvi_resampled)
visualize_ndvi_analysis(ndvi_analysis, day)
for condition in ['corn_below_water', 'corn_above_water']:
print(f"\n{condition.replace('_', ' ').title()} Corn Analysis per MODIS:")
for stat in ['mean', 'max']:
print(f"{stat.capitalize()} NDVI: {ndvi_analysis[condition][stat]}")
Corn Below Water Corn Analysis per MODIS:
Mean NDVI: [21.55594865 20.51568452 7.4782058 26.24400004 26.60275338 23.33767715
13.16068297 11.80940537 12.10314245]
Max NDVI: [81.11699055 76.91399092 80.58758897 82.75593908 89.70690955 91.41303851
88.49125289 85.55169137 90.92957006]
Corn Above Water Corn Analysis per MODIS:
Mean NDVI: [54.61076598 59.69546623 65.22443398 65.90975493 78.52357012 76.01061572
73.06511618 54.97502392 54.67252283]
Max NDVI: [95.95130762 89.55483178 90.90481424 95.45510286 96.85160514 99.7260847
93.69302615 98.97050876 96.44831104]
def visualize_ndvi_histogram(ndvi_analysis):
plt.figure(figsize=(12, 6))
plt.hist(ndvi_analysis['corn_below_water']['ndvi'].flatten(), bins=30, alpha=0.5, label='Corn Below Water', color='blue')
plt.hist(ndvi_analysis['corn_above_water']['ndvi'].flatten(), bins=30, alpha=0.5, label='Corn Above Water', color='green')
plt.xlabel("NDVI values")
plt.ylabel("Frequency")
plt.title("Histogram of NDVI values for corn")
plt.legend(loc='upper right')
visualize_ndvi_histogram(ndvi_analysis)
What are the insights that we can derive from this module?
What are the impacts of land being close to the water currently?
What is the potential loss of productive land?
Where could there be errors given the sampling methods?
This will tell us the impacted area that could be effected by the floods
As we look into the future we should see this change become more drastic
NASA Acres is NASA’s U.S.-focused agriculture and food security Consortium. NASA Acres is commissioned under NASA’s Applied Sciences Program and led by the University of Maryland, with over 30 partner institutions and growing. The consortium approach brings together public and private sectors, and allows for flexible partnerships and rapid action in delivering the benefits of NASA data and tools to U.S. agriculture decision makers. We work with people across the agriculture sector to develop Earth observation (EO)-based data and tools that strengthen U.S. agriculture.
Climate Resilience Network has the goal of unifying the University of Maryland’s groundbreaking research with the immediate requirements of Maryland’s decision-makers to address the complexities of climate change as it affects our home. GEOG’s Grand Challenges project is aimed at promoting climate-resilient agriculture in Maryland, creating interactive tools designed to support research based decision-making.italicized text
COUNTY LEVEL SHAPEFILE https://resilience.climate.gov/datasets/nationalclimate::u-s-climate-and-coastal-inundation-projections-by-geography/explore?layer=0&location=2.619718%2C0.314282%2C1.74