Trying to find out why downloads are failing for band maths

This commit is contained in:
heyarne 2021-02-17 15:14:30 +00:00
commit fcada57719
8 changed files with 3029 additions and 866 deletions

View file

@ -559,7 +559,9 @@
"! mkdir -p {dst_path}\n",
"\n",
"product_ids = gdf.loc[:idx, 'uuid'].values\n",
"downloads, _m _ = api.download_all(subset, dst_path)"
"\n",
"api._tqdm = tqdm # monkey-patch for nicer progress bars\n",
"downloads, _, _ = api.download_all(subset, dst_path)"
]
},
{
@ -815,7 +817,9 @@
" 'height': height,\n",
" 'transform': masked_transform\n",
" })\n",
" with r.open(out_name, 'w', **kwargs) as dst:\n",
" \n",
" ! mkdir -p output\n",
" with r.open(Path('output') / out_name, 'w', **kwargs) as dst:\n",
" dst.write(masked)\n",
" print(f'Wrote resulting raster file to {out_name}')"
]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,209 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from multiprocessing import Pool\n",
"import geopandas as gpd\n",
"from sentinel_helpers import scihub_band_paths"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"base_path = Path('input/tempelhofer_feld/')\n",
"input_files = list(base_path.glob('*.zip'))\n",
"area_of_interest = gpd.read_file(base_path / 'tempelhofer_feld.geojson')\n",
"ndvi_path = base_path / 'ndvi'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import rasterio as r\n",
"import rasterio.mask\n",
"import rasterio.plot as rplt\n",
"import numpy as np\n",
"from sentinel_helpers import scihub_normalize_range\n",
"from zipfile import BadZipFile"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def calculate_ndvi(raster_file):\n",
" global area_of_interest\n",
" \n",
" try:\n",
" b04_path, b08_path = scihub_band_paths(raster_file, ['B04', 'B08'], '10m')\n",
" except BadZipFile:\n",
" print(f'Problem reading {raster_file}, skipping it')\n",
" return\n",
"\n",
" with r.open(b04_path, 'r') as b04, r.open(b08_path, 'r') as b08:\n",
" # we want to only write the bare minimum data necessary to disk\n",
" out_meta = b04.meta.copy()\n",
"\n",
" # we reproject the geojson file we fetched above and convert it so that rasterio\n",
" # can use it as a mask\n",
" mask = area_of_interest.to_crs(out_meta['crs']).iloc[0].geometry\n",
" miny, minx, maxy, maxx = mask.bounds\n",
"\n",
" # update the dimensions and save as geotiff, not jp2\n",
" out_meta.update({\n",
" 'width': maxx - minx,\n",
" 'height': maxy - miny,\n",
" 'driver': 'GTiff',\n",
" 'dtype': 'float32'\n",
" }) \n",
" out_name = Path(b04_path).name.replace('B04', 'NDVI').replace('.jp2', '.tif')\n",
"\n",
" ! mkdir -p {ndvi_path}\n",
" with r.open(ndvi_path / out_name, 'w+', **out_meta) as dst:\n",
" # we take only the part out of our source raster that we actually need\n",
" # crop=True clips off the borders\n",
" b04, transform_b04 = rasterio.mask.mask(b04, shapes=[mask], crop=True)\n",
" b08, _ = rasterio.mask.mask(b08, shapes=[mask], crop=True) # we ignore the returned transform because it's identical to the previous one\n",
"\n",
" b04 = scihub_normalize_range(b04).astype('f4') # <- f4 = float32\n",
" b08 = scihub_normalize_range(b08).astype('f4')\n",
"\n",
" # uncomment the following line to see if your masked shape looks correct\n",
" #rplt.show(b04, transform=transform_b04)\n",
" #rplt.show(b08, transform=transform_b04)\n",
"\n",
" # we want to be able to ignore divide by zero errors so the formula is nicer to write\n",
" np.seterr(divide='ignore', invalid='ignore')\n",
" ndvi = (b08 - b04) / (b08 + b04)\n",
"\n",
" # uncomment the following line to see if we calculated the index correctly\n",
" # rplt.show(ndvi, transform=transform_b04)\n",
"\n",
" dst.write(ndvi)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from tqdm.notebook import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d6bb63ba04f94679ab26c02235782b3c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=40.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190603T101031_N0212_R022_T33UUU_20190603T114652.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190404T101031_N0211_R022_T32UQD_20190404T174806.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190216T102111_N0211_R065_T33UUU_20190216T130428.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2B_MSIL2A_20190419T101029_N0211_R022_T33UUU_20190419T132322.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190407T102021_N0211_R065_T33UUU_20190407T134109.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2B_MSIL2A_20190512T102029_N0212_R065_T33UUU_20190512T134103.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190613T101031_N0212_R022_T33UUU_20190614T125329.zip, skipping itProblem reading input/tempelhofer_feld/S2A_MSIL2A_20190424T101031_N0211_R022_T32UQD_20190424T162325.zip, skipping it\n",
"\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190822T101031_N0213_R022_T32UQD_20190822T143621.zip, skipping it\n",
"Problem reading input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip, skipping it\n",
"\n",
"CPU times: user 179 ms, sys: 154 ms, total: 333 ms\n",
"Wall time: 23.7 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"pool = Pool()\n",
"\n",
"for _ in tqdm(pool.imap_unordered(calculate_ndvi, input_files), total=len(input_files)):\n",
" # this loop is only here so we can get the progress bar\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How many files could we process?"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"31\n"
]
}
],
"source": [
"! ls -l {ndvi_path} | wc -l"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View file

@ -0,0 +1,245 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problems with Zipfiles\n",
"\n",
"Some of the downloaded zip files are suspiciously small:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.2G input/tempelhofer_feld_next_try/S2B_MSIL2A_20190601T102029_N0212_R065_T33UUU_20190601T135040.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20190825T102031_N0213_R065_T33UUU_20190825T134836.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20191014T102031_N0213_R065_T32UQD_20191014T130941.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20190726T102031_N0213_R065_T33UUU_20190726T125507.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20190626T102031_N0212_R065_T33UUU_20190626T125319.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20190417T102031_N0211_R065_T33UUU_20190417T130913.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2B_MSIL2A_20190711T102029_N0213_R065_T33UUU_20190711T135545.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2B_MSIL2A_20190402T102029_N0211_R065_T33UUU_20190402T135010.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2B_MSIL2A_20191029T102039_N0213_R065_T32UQD_20191029T134629.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2B_MSIL2A_20190422T102029_N0211_R065_T32UQD_20190422T133643.zip\n",
"1.1G input/tempelhofer_feld_next_try/S2A_MSIL2A_20191213T102421_N0213_R065_T33UUU_20191213T120011.zip\n",
"845M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190723T101031_N0213_R022_T33UUU_20190723T125722.zip\n",
"829M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190911T101021_N0213_R022_T33UUU_20190911T143947.zip\n",
"823M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190713T101031_N0213_R022_T33UUU_20190713T135651.zip\n",
"823M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190718T101039_N0213_R022_T33UUU_20190718T131731.zip\n",
"819M input/tempelhofer_feld_next_try/S2A_MSIL2A_20191210T101411_N0213_R022_T33UUU_20191210T114322.zip\n",
"813M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190921T101031_N0213_R022_T33UUU_20190921T130515.zip\n",
"809M input/tempelhofer_feld_next_try/S2A_MSIL2A_20191220T101431_N0213_R022_T33UUU_20191220T115219.zip\n",
"802M input/tempelhofer_feld_next_try/S2B_MSIL2A_20191205T101309_N0213_R022_T33UUU_20191205T122401.zip\n",
"802M input/tempelhofer_feld_next_try/S2A_MSIL2A_20191130T101401_N0213_R022_T33UUU_20191130T115440.zip\n",
"789M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190827T101029_N0213_R022_T32UQD_20190827T134854.zip\n",
"789M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190519T101039_N0212_R022_T33UUU_20190519T132053.zip\n",
"774M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190728T101029_N0213_R022_T32UQD_20190728T134658.zip\n",
"771M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190906T101029_N0213_R022_T32UQD_20190906T133832.zip\n",
"768M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190529T101039_N0212_R022_T32UQD_20190529T130331.zip\n",
"766M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190330T101029_N0211_R022_T33UUU_20190330T144328.zip\n",
"764M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190218T101059_N0211_R022_T32UQD_20190218T161620.zip\n",
"761M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190320T101029_N0211_R022_T33UUU_20190320T195148.zip\n",
"753M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190409T101029_N0211_R022_T32UQD_20190409T134504.zip\n",
"723M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190114T101351_N0211_R022_T32UQD_20190114T113404.zip\n",
" 43M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190407T102021_N0211_R065_T33UUU_20190407T134109.zip\n",
" 42M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190603T101031_N0212_R022_T33UUU_20190603T114652.zip\n",
" 38M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190822T101031_N0213_R022_T32UQD_20190822T143621.zip\n",
" 35M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190613T101031_N0212_R022_T33UUU_20190614T125329.zip\n",
" 31M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190419T101029_N0211_R022_T33UUU_20190419T132322.zip\n",
" 30M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190404T101031_N0211_R022_T32UQD_20190404T174806.zip\n",
" 30M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190424T101031_N0211_R022_T32UQD_20190424T162325.zip\n",
" 29M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190216T102111_N0211_R065_T33UUU_20190216T130428.zip\n",
" 29M input/tempelhofer_feld_next_try/S2B_MSIL2A_20190512T102029_N0212_R065_T33UUU_20190512T134103.zip\n",
" 25M input/tempelhofer_feld_next_try/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip\n"
]
}
],
"source": [
"! ls -Ssh input/tempelhofer_feld_next_try/*.zip "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Trying to extract them causes an error:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Archive: input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip\n",
" End-of-central-directory signature not found. Either this file is not\n",
" a zipfile, or it constitutes one disk of a multi-part archive. In the\n",
" latter case the central directory and zipfile comment will be found on\n",
" the last disk(s) of this archive.\n",
"unzip: cannot find zipfile directory in one of input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip or\n",
" input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip.zip, and cannot find input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip.ZIP, period.\n"
]
}
],
"source": [
"! ls -S input/tempelhofer_feld/*.zip | tail -n1 | xargs unzip"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sentinelsat"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"api = sentinelsat.SentinelAPI(os.getenv('SCIHUB_USERNAME'), os.getenv('SCIHUB_PASSWORD'))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.8/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
" return _prepare_from_string(\" \".join(pjargs))\n"
]
}
],
"source": [
"res = api.to_geodataframe(api.query(raw='S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509'))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bedec483-5ee1-4264-8dfa-a3b53ce364f7 816.67 MB\n",
"Name: size, dtype: object"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# we can see that the size given by the scihub api is way larger\n",
"res['size']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do the downloads fail repeatedly?"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input/tempelhofer_feld/S2A_MSIL2A_20190603T101031_N0212_R022_T33UUU_20190603T114652.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190404T101031_N0211_R022_T32UQD_20190404T174806.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190216T102111_N0211_R065_T33UUU_20190216T130428.zip\n",
"input/tempelhofer_feld/S2B_MSIL2A_20190419T101029_N0211_R022_T33UUU_20190419T132322.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190407T102021_N0211_R065_T33UUU_20190407T134109.zip\n",
"input/tempelhofer_feld/S2B_MSIL2A_20190512T102029_N0212_R065_T33UUU_20190512T134103.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190613T101031_N0212_R022_T33UUU_20190614T125329.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190424T101031_N0211_R022_T32UQD_20190424T162325.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190822T101031_N0213_R022_T32UQD_20190822T143621.zip\n",
"input/tempelhofer_feld/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip\n"
]
}
],
"source": [
"! find input/tempelhofer_feld -type f -size -500M -name '*.zip'"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input/tempelhofer_feld_test/S2A_MSIL2A_20190603T101031_N0212_R022_T33UUU_20190603T114652.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190404T101031_N0211_R022_T32UQD_20190404T174806.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190216T102111_N0211_R065_T33UUU_20190216T130428.zip\n",
"input/tempelhofer_feld_test/S2B_MSIL2A_20190419T101029_N0211_R022_T33UUU_20190419T132322.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190407T102021_N0211_R065_T33UUU_20190407T134109.zip\n",
"input/tempelhofer_feld_test/S2B_MSIL2A_20190512T102029_N0212_R065_T33UUU_20190512T134103.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190613T101031_N0212_R022_T33UUU_20190614T125329.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190424T101031_N0211_R022_T32UQD_20190424T162325.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190822T101031_N0213_R022_T32UQD_20190822T143621.zip\n",
"input/tempelhofer_feld_test/S2A_MSIL2A_20190623T101031_N0212_R022_T33UUU_20190623T132509.zip\n"
]
}
],
"source": [
"! find input/tempelhofer_feld_test -type f -size -500M -name '*.zip'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -53,15 +53,6 @@ def plot_all(items, extra_kwargs=[]):
item.plot(ax=ax, **kwargs)
def scihub_product_ids(geodataframe):
'''
Returns the product ids of items in a GeoDataFrame returned by
`sentinelsat.to_geodataframe` as expected by `sentinelsat.download` and
`sentinelsat.download_all`.
'''
return [link.split('Products(\'')[-1].split('\')/$value')[0] for link in geodataframe['link'].values]
def scihub_band_paths(p, bands, resolution=None):
'''
Given a zip file or folder at `p`, returns the paths inside p to the raster files containing