Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Last active October 27, 2023 14:53
Show Gist options
  • Save bennyistanto/dbf1456d6b345361a8d28d33c0fecaeb to your computer and use it in GitHub Desktop.
Save bennyistanto/dbf1456d6b345361a8d28d33c0fecaeb to your computer and use it in GitHub Desktop.
Generate header file (*.hdr) for each of TIMESAT output binary files in a folder
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "51687b03",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Generating hdr files: 100%|████████████████████████████████████████| 168/168 [00:02<00:00, 64.52file/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Header files generated successfully!\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"import os\n",
"from tqdm import tqdm\n",
"\n",
"def generate_hdr(folder_path):\n",
" # Header template with placeholders\n",
" hdr_content = \"\"\"\n",
"ENVI\n",
"description = {filename}\n",
"samples = 664\n",
"lines = 728\n",
"bands = 1\n",
"header offset = 0\n",
"file type = ENVI Standard\n",
"data type = 4\n",
"interleave = bsq\n",
"byte order = 0\n",
"map info = {{Geographic Lat/Lon, 1, 1, 35.1151444562321, 34.6929362726959, 0.0022457882102988, 0.0022457882102988,WGS-84}}\n",
"coordinate system string = {{GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]}}\n",
"band names = {{\n",
"Band_1}}\n",
"data ignore value = 65535\n",
"\"\"\"\n",
"\n",
" # Get list of files in the directory\n",
" files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]\n",
"\n",
" # Iterate over files in the directory with tqdm progress bar\n",
" for filename in tqdm(files, desc=\"Generating hdr files\", unit=\"file\"):\n",
" # Generate .hdr filename\n",
" hdr_filename = os.path.join(folder_path, filename + \".hdr\")\n",
" # Replace placeholder with actual filename\n",
" content = hdr_content.replace(\"{filename}\", filename)\n",
" # Write the hdr content to the .hdr file\n",
" with open(hdr_filename, 'w') as hdr_file:\n",
" hdr_file.write(content)\n",
"\n",
" print(\"\\nHeader files generated successfully!\")\n",
"\n",
"# Specify the path to the folder containing the binary files (adjust as needed)\n",
"folder_path = \"../lbn/gee/11_timesat_raw/bil/3y\"\n",
"generate_hdr(folder_path)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e677586",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment