Skip to content

Instantly share code, notes, and snippets.

@scidam
Created December 16, 2021 10:32
Show Gist options
  • Select an option

  • Save scidam/fcc5bddef9bf42479a70f1384cfdca6f to your computer and use it in GitHub Desktop.

Select an option

Save scidam/fcc5bddef9bf42479a70f1384cfdca6f to your computer and use it in GitHub Desktop.
Raster calculator based on gdal's `gdal_calc` script for some environmental variables (CKI, WKI, IC, PWKI, PCKI)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
Created Date: Saturday December 11th 2021
Author: Dmitry Kislov
E-mail: [email protected]
-----
Last Modified: Wednesday, December 15th 2021, 2:32:54 pm
Modified By: Dmitry Kislov
-----
Copyright (c) 2021
"""
import subprocess
import glob
import string
import os
import sys
def locate_files(path='data', ext='*.tif'):
result = dict()
for f in glob.glob(f'{path}/**/{ext}', recursive=True):
filename = '.'.join(os.path.basename(f).split('.')[:-1])
var, month = filename.split('_')[-2:]
result[(var.lower(), month)] = f
return result
def get_wki_command(threshold='5.0'):
command = [sys.executable,
rf'{os.environ.get("CONDA_PREFIX")}\Scripts\gdal_calc.py']
filtered_files = {k: v for k, v in locate_files().items() if 'tavg' in k}
file_letters = []
for s, (_, f) in zip(string.ascii_uppercase, filtered_files.items()):
command.extend([f'-{s}', f])
file_letters.append(s)
command.append('--outfile=wki.tif')
formula = '+'.join(f'{l}*({l}>{threshold})- {threshold}*({l}>{threshold})' for l in file_letters)
command.append(f'--calc={formula}')
return command
#print(get_wki_command())
subprocess.call(get_wki_command())
def get_cki_command(threshold='5.0'):
command = [sys.executable,
rf'{os.environ.get("CONDA_PREFIX")}\Scripts\gdal_calc.py']
filtered_files = {k: v for k, v in locate_files().items() if 'tavg' in k}
file_letters = []
for s, (_, f) in zip(string.ascii_uppercase, filtered_files.items()):
command.extend([f'-{s}', f])
file_letters.append(s)
command.append('--outfile=cki.tif')
formula = '+'.join(f'numpy.abs({threshold} * ({l}<{threshold}) - {l}*({l}<{threshold}))' for l in file_letters)
command.append(f'--calc={formula}')
return command
subprocess.call(get_cki_command())
from itertools import chain
def get_pwki_command(threshold='5.0'):
command = [sys.executable,
rf'{os.environ.get("CONDA_PREFIX")}\Scripts\gdal_calc.py']
filtered_files_tavg = {k: v for k, v in locate_files().items() if 'tavg' in k}
filtered_files_prec = {k: v for k, v in locate_files().items() if 'prec' in k}
file_letters_tavg = []
file_letters_prec = []
for s, (_, f) in zip(string.ascii_uppercase, chain(filtered_files_tavg.items(), filtered_files_prec.items())):
command.extend([f'-{s}', f])
if 'tavg' in f:
file_letters_tavg.append(s)
if 'prec' in f:
file_letters_prec.append(s)
command.append('--outfile=pwki.tif')
formula = '+'.join(f'{p} * ({t}>{threshold})' for t, p in zip(file_letters_tavg, file_letters_prec))
command.append(f'--calc={formula}')
return command
subprocess.call(get_pwki_command())
def get_pcki_command(threshold='5.0'):
command = [sys.executable,
rf'{os.environ.get("CONDA_PREFIX")}\Scripts\gdal_calc.py']
filtered_files_tavg = {k: v for k, v in locate_files().items() if 'tavg' in k}
filtered_files_prec = {k: v for k, v in locate_files().items() if 'prec' in k}
file_letters_tavg = []
file_letters_prec = []
for s, (_, f) in zip(string.ascii_uppercase, chain(filtered_files_tavg.items(), filtered_files_prec.items())):
command.extend([f'-{s}', f])
if 'tavg' in f:
file_letters_tavg.append(s)
if 'prec' in f:
file_letters_prec.append(s)
command.append('--outfile=pcki.tif')
formula = '+'.join(f'{p} * ({t}<{threshold})' for t, p in zip(file_letters_tavg, file_letters_prec))
command.append(f'--calc={formula}')
return command
subprocess.call(get_pcki_command())
def get_ic_command(threshold='5.0'):
command = [sys.executable,
rf'{os.environ.get("CONDA_PREFIX")}\Scripts\gdal_calc.py']
filtered_files_tmin = {k: v for k, v in locate_files().items() if 'tmin' in k}
filtered_files_tmax = {k: v for k, v in locate_files().items() if 'tmax' in k}
file_letters_tmin = []
file_letters_tmax = []
for s, (_, f) in zip(string.ascii_uppercase, chain(filtered_files_tmin.items(), filtered_files_tmax.items())):
command.extend([f'-{s}', f])
if 'tmin' in f:
file_letters_tmin.append(s)
if 'tmax' in f:
file_letters_tmax.append(s)
command.append('--outfile=ic.tif')
formula = 'numpy.maximum.reduce([' + ','.join(file_letters_tmax) + '])' +\
'-numpy.minimum.reduce([' + ','.join(file_letters_tmin) + '])'
command.append(f'--calc={formula}')
return command
subprocess.call(get_ic_command())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment