Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Created April 22, 2020 06:08
Show Gist options
  • Save bennyistanto/a61d632651418da8445e6a01f7969732 to your computer and use it in GitHub Desktop.
Save bennyistanto/a61d632651418da8445e6a01f7969732 to your computer and use it in GitHub Desktop.
Get maximum and date value from time-series rainfall data
Source: https://community.esri.com/thread/218763-max-value-of-time-series-raster-
and-date-information#comment-788295
To do (before running script)
1. Line 17-18 - data directory
2. line 40 to 43 - {lower_left_corner},{x_cell_size},{y_cell_size},{value_to_nodata}
3. line 47 to 50 - {lower_left_corner},{x_cell_size},{y_cell_size},{value_to_nodata}
import sys
import numpy as np
import arcpy
arcpy.env.overwriteOutput = True
ft = {'bool': lambda x: repr(x.astype(np.int32)),
'float_kind': '{: 0.3f}'.format}
np.set_printoptions(edgeitems=5, linewidth=120, precision=2, suppress=True,
threshold=100, formatter=ft)
np.ma.masked_print_option.set_display('-') # change to a single -
script = sys.argv[0] # print this should you need to locate the script
# ---- You need to specify the folder where the tif files reside ------------
#
src_flder = r"Y:\3days\2017" # just change the year
out_flder = r"Y:\3days\2017_output" # make a result folder to put stuff
out_year = src_flder.split("\\")[-1]
max_name = "{}\\idn_{}_max.tif".format(out_flder, out_year)
date_name = "{}\\idn_{}_day.tif".format(out_flder, out_year)
# ---- Process time ----
arcpy.env.workspace = src_flder
rasters = arcpy.ListRasters()
arrs = []
for i in rasters:
r = arcpy.RasterToNumPyArray(i, nodata_to_value=0.0)
arrs.append(r)
# ---- Convert to an integer dtype since the float precision is a bit much
a = np.array(arrs)
m = np.where(a > 0., 1, 0)
a_m = a * m
a_m = np.ndarray.astype(a_m, np.int32)
a_max = np.max(a_m, axis=0)
a_w = np.argmax(a_m, axis=0)
a_when = np.ndarray.astype(a_w, np.int32)
out_max = arcpy.NumPyArrayToRaster(a_max,
arcpy.Point(94.9500041,-11.050001),
x_cell_size=0.05,
y_cell_size=0.05,
value_to_nodata=0)
out_max.save(max_name)
out_when = arcpy.NumPyArrayToRaster(a_when,
arcpy.Point(94.9500041,-11.050001),
x_cell_size=0.05,
y_cell_size=0.05,
value_to_nodata=0)
out_when.save(date_name)
del a, m, a_m, a_max, a_w, a_when, max_name, date_name, out_max, out_when
# ---- repeat ad nauseum since this is free ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment