Skip to content

Commit

Permalink
data preparation: use np logic instead of loops (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
pesekon2 authored Nov 15, 2023
1 parent 958e465 commit 4abfa63
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions smoderp2d/providers/base/data_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import math
import numpy as np
import numpy.ma as ma
from abc import ABC, abstractmethod

from smoderp2d.processes import rainfall
Expand Down Expand Up @@ -780,22 +781,22 @@ def _convert_slope_units(self):
"""
Converts slope units from % to 0-1 range in the mask.
"""
# TODO convert to NumPy logic!!!
for i in range(self.data['mat_slope'].shape[0]):
for j in range(self.data['mat_slope'].shape[1]):
nv = GridGlobals.NoDataValue
if self.data['mat_slope'][i][j] != nv:
self.data['mat_slope'][i][j] /= 100.
self.data['mat_slope'] = np.where(
self.data['mat_slope'] != GridGlobals.NoDataValue,
self.data['mat_slope'] / 100.,
self.data['mat_slope']
)

@staticmethod
def _get_mat_stream_seg(mat_stream_seg):
# each element of stream has a number assigned from 0 to
# no. of stream parts
for i in range(GridGlobals.r):
for j in range(GridGlobals.c):
if mat_stream_seg[i][j] > 0: # FID starts at 1
# state 0|1|2 (> Globals.streams_flow_inc -> stream flow)
mat_stream_seg[i][j] += Globals.streams_flow_inc
mat_stream_seg += ma.where(
mat_stream_seg > 0, # FID starts at 1
# state 0|1|2 (> Globals.streams_flow_inc -> stream flow)
Globals.streams_flow_inc,
0
)

def _check_soilveg_dim(self, field):
if self.soilveg_fields[field].shape[0] != GridGlobals.r or \
Expand Down

0 comments on commit 4abfa63

Please sign in to comment.