Development of an Artificial Neural Network Algorithm Embedded in an On-Site Sensor for Water Level Forecasting

Extreme weather events cause stream overflow and lead to urban inundation. In this study, a decentralized flood monitoring system is proposed to provide water level predictions in streams three hours ahead. The customized sensor in the system measures the water levels and implements edge computing to produce future water levels. It is very different from traditional centralized monitoring systems and considered an innovation in the field. In edge computing, traditional physics-based algorithms are not computationally efficient if microprocessors are used in sensors. A correlation analysis was performed to identify key factors that influence the variations in the water level forecasts. For example, the second-order difference in the water level is considered to represent the acceleration or deacceleration of a water level rise. According to different input factors, three artificial neural network (ANN) models were developed. Four streams or canals were selected to test and evaluate the performance of the models. One case was used for model training and testing, and the others were used for model validation. The results demonstrated that the ANN model with the second-order water level difference as an input factor outperformed the other ANN models in terms of RMSE. The customized microprocessor-based sensor with an embedded ANN algorithm can be adopted to improve edge computing capabilities and support emergency response and decision making.


Introduction
The Emergency Event Database (EM-DAT) includes records for 432 disastrous events related to natural hazards worldwide in 2021. Floods dominated these events, with 223 occurrences, with an average of 163 annual flood occurrences recorded in the 2001-2020 period [1]. The losses of life and property caused by floods are tremendous [2]. Structural and nonstructural measures have been devised to prevent or mitigate loss of life and property [3]. The development of early warning systems, which are nonstructural measures, was cited as a critical defense against floods [4]. These systems involve on-site facilities such as bubble gauges, float gauges and pressure sensors, which are installed to observe water level changes; then, these observations are used as indicators to assess the flood potential [5]. Accurate and cost-efficient water level monitoring sensors are required and must be deployed with very high intensity for detailed flood records [6][7][8][9]. Nevertheless, these sensors are only used for water level monitoring, and this kind of application provides limited lead time for decision makers to take response measures to mitigate the impact of disasters [7][8][9][10][11].
Edge computing is a distributed computing paradigm in which computations are largely or completely performed at distributed device nodes known as smart devices or edge devices, as opposed to computations in a centralized cloud environment [12]. By implementing edge computing on sensors, issues prevalent in centralized cloud systems, such as latency and network connection dependency, can be avoided [13]. Since system efficiency of the emergency response is increased. In addition, new flood prediction models are desperately needed to be performed on the computing power-limited sensors. ANN models were developed to determine the water level on the sensor. A detailed analysis regarding the correlation between input factors and output water levels was carefully conducted to maximize the efficiency of edge computing. Furthermore, special attention was given to extreme events such as typhoons during the development of the proposed system.

Study Areas
This study focused on three rivers and one artificial canal to develop the ANN model and evaluate the performance of the proposed system. The geographical locations of these study areas are shown in Figure 1. flood-prone locations with localized information. Therefore, situational and customized awareness is maintained during flooding, even if an internet connection is unavailable. As a result, the efficiency of the emergency response is increased. In addition, new flood prediction models are desperately needed to be performed on the computing power-limited sensors. ANN models were developed to determine the water level on the sensor. A detailed analysis regarding the correlation between input factors and output water levels was carefully conducted to maximize the efficiency of edge computing. Furthermore, special attention was given to extreme events such as typhoons during the development of the proposed system.

Study Areas
This study focused on three rivers and one artificial canal to develop the ANN model and evaluate the performance of the proposed system. The geographical locations of these study areas are shown in Figure 1. The Yilan River Basin is located on northeastern Taiwan Island. Its main stream is approximately 24.4 km (km) long and covers an area of approximately 149.06 square km. The Yilan River Basin was first selected to train and test the ANN model for water level forecasting. This is because the precipitation, river stage, and flow velocity data have been carefully measured by the Water Resources Agency (WRA) and National Center for Highperformance Computing (NCHC) since 2012 [30] for the Yilan River basin. In addition, there is no human interference, such as a reservoir upstream of the Yilan River, and there are no human operation-related factors considered among the ANN input factors in this study. Figure 1 (in a clockwise direction) shows the remaining two river basins, the Beinan River and Toucian River Basins, in eastern and western Taiwan and one artificial canal, Shimen Canal, in western Taiwan. The Beinan River is approximately 84 km long and flows through Taitung County to the Pacific Ocean. The Toucian River flows through Hsinchu County for 63 km to the west. Different from the Yilan River and Beinan River situations, there are also no human-made hydraulic structures upstream along the Toucian River, but there is an off-stream reservoir upstream of the river. These two rivers The Yilan River Basin is located on northeastern Taiwan Island. Its main stream is approximately 24.4 km (km) long and covers an area of approximately 149.06 square km. The Yilan River Basin was first selected to train and test the ANN model for water level forecasting. This is because the precipitation, river stage, and flow velocity data have been carefully measured by the Water Resources Agency (WRA) and National Center for High-performance Computing (NCHC) since 2012 [30] for the Yilan River basin. In addition, there is no human interference, such as a reservoir upstream of the Yilan River, and there are no human operation-related factors considered among the ANN input factors in this study. Figure 1 (in a clockwise direction) shows the remaining two river basins, the Beinan River and Toucian River Basins, in eastern and western Taiwan and one artificial canal, Shimen Canal, in western Taiwan. The Beinan River is approximately 84 km long and flows through Taitung County to the Pacific Ocean. The Toucian River flows through Hsinchu County for 63 km to the west. Different from the Yilan River and Beinan River situations, there are also no human-made hydraulic structures upstream along the Toucian River, but there is an off-stream reservoir upstream of the river. These two rivers were selected because there was no human interference, such as reservoirs or gates, found in the rivers. For reference, the proposed system (integration of Raspberry Pi (RPi) sensors and an ANN model) was only implemented on-site in the Shimen Canal because of the limitations of devices and the need for permission to install equipment. Tests performed in the Beinan River and Toucian River were conducted offline.

System Development
In this study, a water level forecasting model is embedded in an RPi-based monitoring device that can provide real-time water level observations and support local calculations. The system structure and data processing flowchart are illustrated in Figure 2. The monitoring devices use ultrasonic waves to measure the water levels, and the observed data and other related information are preprocessed in the RPi platform. Thereafter, the water level predictions at specific locations are performed using the proposed ANN-based water level forecasting models. The details of each component in the structure are described in the following subsections. were selected because there was no human interference, such as reservoirs or gates, found in the rivers. For reference, the proposed system (integration of Raspberry Pi (RPi) sensors and an ANN model) was only implemented on-site in the Shimen Canal because of the limitations of devices and the need for permission to install equipment. Tests performed in the Beinan River and Toucian River were conducted offline.

System Development
In this study, a water level forecasting model is embedded in an RPi-based monitoring device that can provide real-time water level observations and support local calculations. The system structure and data processing flowchart are illustrated in Figure 2. The monitoring devices use ultrasonic waves to measure the water levels, and the observed data and other related information are preprocessed in the RPi platform. Thereafter, the water level predictions at specific locations are performed using the proposed ANN-based water level forecasting models. The details of each component in the structure are described in the following subsections.

Raspberry Pi Water Level Sensor
Automatic water level sensors with wireless functions are usually costly. Therefore, low-cost, open-source, and low-energy-consumption sensors are always of interest for environmental monitoring [31]. The proposed sensor is shown in Figure 3. The RPi is a reliable, low-cost microcomputer (MCU) that was developed in 2006 by the University of Cambridge's Computer Department and has been produced by the Raspberry Pi Foundation since 2012. The RPi 3 Model B+ module, which was released in

Raspberry Pi Water Level Sensor
Automatic water level sensors with wireless functions are usually costly. Therefore, low-cost, open-source, and low-energy-consumption sensors are always of interest for environmental monitoring [31]. The proposed sensor is shown in Figure 3. were selected because there was no human interference, such as reservoirs or gates, found in the rivers. For reference, the proposed system (integration of Raspberry Pi (RPi) sensors and an ANN model) was only implemented on-site in the Shimen Canal because of the limitations of devices and the need for permission to install equipment. Tests performed in the Beinan River and Toucian River were conducted offline.

System Development
In this study, a water level forecasting model is embedded in an RPi-based monitoring device that can provide real-time water level observations and support local calculations. The system structure and data processing flowchart are illustrated in Figure 2. The monitoring devices use ultrasonic waves to measure the water levels, and the observed data and other related information are preprocessed in the RPi platform. Thereafter, the water level predictions at specific locations are performed using the proposed ANN-based water level forecasting models. The details of each component in the structure are described in the following subsections.

Raspberry Pi Water Level Sensor
Automatic water level sensors with wireless functions are usually costly. Therefore, low-cost, open-source, and low-energy-consumption sensors are always of interest for environmental monitoring [31]. The proposed sensor is shown in Figure 3.  operating system supports the implementation of a Python programming language-based ANN module to forecast water levels with lead times. Many studies have successfully applied ultrasonic waves to measure water levels under severe environmental conditions [32,33]. The ultrasonic sensor used here is the high-performance ultrasonic distance sensor MB7386 HRXL-MaxSonar-WR from MaxBotix, Brainerd, MN, USA. The ultrasonic sensor emits sound waves at a frequency of 42 kHz with a 6 Hz sampling rate and detects the sound waves that bounce back. The distance can be estimated by the elapsed time between the generated and returning sound waves. Theoretically, the sensor is effective up to a maximum range of 10 m, with functions of temperature compensation and noise cancelation. However, the efficient measuring distance varies based on the size of the target and power supply to the sensor (usually within 5 m).

Artificial Neural Network Water Level Forecasting Algorithm
ANNs are inspired by the human central nervous system. ANNs usually consist of layers such as an input layer, one or more hidden layers, and an output layer. A three-layer ANN data processing flowchart is shown in Figure 4. The input layer comprises a number of nodes (i = 1, 2, 3 . . . n). A node, also called an artificial neuron, connects to other nodes in the hidden layer and has an associated weight (w) and threshold (bias). When the incoming signals (X 1 , X 2, X 3 . . . X n ) are passed to the nodes (j = 1, 2, 3 . . . N) in the next layer, they are multiplied by the weight of the connection. These weights describe the importance of any incoming signal, with larger weights contributing more significantly to the output compared to other signals. The effective signal (E j ) to node j shown in Equation (1) is the weighted sum of all incoming signals. In the first phase of training, the weights (i.e., w ji ) are set to random values. February 2018 with a 1.4 GHz 64-bit quad core processor, was used as a platform embedded with an ultrasonic sensor to measure water stages at a local site. The UNIX/Debian=based Raspian operating system supports the implementation of a Python programming language-based ANN module to forecast water levels with lead times. Many studies have successfully applied ultrasonic waves to measure water levels under severe environmental conditions [32,33]. The ultrasonic sensor used here is the high-performance ultrasonic distance sensor MB7386 HRXL-MaxSonar-WR from MaxBotix, Brainerd, MN, USA. The ultrasonic sensor emits sound waves at a frequency of 42 kHz with a 6 Hz sampling rate and detects the sound waves that bounce back. The distance can be estimated by the elapsed time between the generated and returning sound waves. Theoretically, the sensor is effective up to a maximum range of 10 m, with functions of temperature compensation and noise cancelation. However, the efficient measuring distance varies based on the size of the target and power supply to the sensor (usually within 5 m).

Artificial Neural Network Water Level Forecasting Algorithm
ANNs are inspired by the human central nervous system. ANNs usually consist of layers such as an input layer, one or more hidden layers, and an output layer. A threelayer ANN data processing flowchart is shown in Figure 4. The input layer comprises a number of nodes (i = 1, 2, 3…n). A node, also called an artificial neuron, connects to other nodes in the hidden layer and has an associated weight (w) and threshold (bias). When the incoming signals (X1, X2, X3…Xn) are passed to the nodes (j = 1, 2, 3…N) in the next layer, they are multiplied by the weight of the connection. These weights describe the importance of any incoming signal, with larger weights contributing more significantly to the output compared to other signals. The effective signal (Ej) to node j shown in Equation (1) is the weighted sum of all incoming signals. In the first phase of training, the weights (i.e., wji) are set to random values. An activation function is used to transform the effective signal (Ej) into an output value to be fed to the next layer or as an output. In this study, hyperbolic tangent (tanh) An activation function is used to transform the effective signal (E j ) into an output value to be fed to the next layer or as an output. In this study, hyperbolic tangent (tanh) and exponential linear unit (ELU) functions are used as the activation functions to transfer input signals to hidden and output layers, respectively. The tanh function has an S-shape similar to that of the sigmoid activation function, with a difference in the output range of −1 to 1. The ELU function is also similar to the rectified linear unit (ReLU), with a difference in output value for negative values of input. A three-layer network structure with one input layer, one hidden layer, and one output layer is adopted because of the limited computing power of the RPi 3 Model B+ module in the sensor [27]. No general guidelines exist for specifying the optimal number of nodes required in the hidden layer [34]. The number of nodes in the hidden layers can be estimated using Equation (2), as recommended by Fletcher and Goss [35]. The formula was confirmed by Huang and Foo [36] to provide the optimal network size, resulting in minimum error and a high correlation in the validation data set. There are three output nodes representing the forecasted water levels with lead periods of 1, 2, and 3 h.
where N is the number of nodes in the hidden layer and n is the number of incoming signals. The output from the neural network is calculated by propagating an input signal through each layer until the output layer outputs its values. It is a so-called feed-forward network.
As mentioned above, the weights initially are random values and modified by reducing the differences between the output and a known output. The procedure repeatedly optimizes the weights until the value of the objective root mean square error (RMSE) function, shown in Equation (3), falls below a certain threshold. In this study, the threshold is 0.01.
where P i is the predicted water level and O i is the observed water level. K is the number of outputs and here refers to the forecasted water levels with three lead times. This learning procedure is called the backpropagation approach and was proposed by Rumelhart et al. [37]. It is a method for training the weights in a multilayer feed-forward neural network structure. A Python module Scikit-learn was applied for the ANN model computation [38].
Since the output of the developed ANN model is the water level, the selection of the input factors must be based on the characteristics and shifts of the outputs at a given location. The change in the time series water level is not only dependent on rainfall records in upstream watersheds and river inflows but also related to previous water levels based on river discharge conditions. Moreover, the length of the lag phase of each input factor is influenced by the distance between input and output locations, and it determines the length of the input sequence. In fact, the selection of input factors has a large impact on the accuracy and efficiency of ANN models. There is no global way to select the input factors for an ANN model [39]. Thus, parameter selection for edge computing is important to maximize the computing efficiency of ANNs. The efficiency of calculations must be optimized for the appropriate number of input factors. In this study, a cross-correlation analysis (cc 2 ), shown in Equation (4), one of the most widely used methods for factor selection, as discussed by Babel and Shinde [39], was carried out between the outputs (forecasted water levels at a given location) and input factors with a lag phase length.
where n is the total number of time sequences in hours and k represents the time lag value. X and Y are the water level and a possible input factor, respectively. X and Y denote average values. Only the factors with a relatively high correlation value with the output were adopted in the proposed ANN models. In addition to the original inputs, two extra input factors were included in the cross-correlation analysis: water level variation (W r ) and the frequency of water level change (W f ) for consecutive records in a time interval (e.g., 1 h). The definitions of W r and W f are shown below: Mathematically, W r and W f represent the first-and second-order differences of the water level sequence at a target location, respectively. Physically, W r and W f are the velocity and acceleration of the change water level, respectively. Zhong et al. [40] found that considering the first-and second-order differences in the water level sequence can improve the forecasting accuracy of ANN models. However, they used this information to identify the level of data fluctuations and then applied the Kalman filter algorithm for local optimization. Details of the selected factors were not discussed in their study. In contrast, this study is the first attempt to apply these variables as input factors for the proposed ANN models to perform water level forecasting. Details of the analysis are described in the Results and Discussion section.

Results and Discussion
The ANN models were integrated into sensors, and their performance was evaluated in real cases. The discussion of the results is divided into three parts: (1) data preparation, (2) development, and (3) application. The research flowchart of this process is shown in Figure 5. were adopted in the proposed ANN models. In addition to the original inputs, two extra input factors were included in the cross-correlation analysis: water level variation (Wr) and the frequency of water level change (Wf) for consecutive records in a time interval (e.g., 1 h). The definitions of Wr and Wf are shown below: Mathematically, Wr and Wf represent the first-and second-order differences of the water level sequence at a target location, respectively. Physically, Wr and Wf are the velocity and acceleration of the change water level, respectively. Zhong et al. [40] found that considering the first-and second-order differences in the water level sequence can improve the forecasting accuracy of ANN models. However, they used this information to identify the level of data fluctuations and then applied the Kalman filter algorithm for local optimization. Details of the selected factors were not discussed in their study. In contrast, this study is the first attempt to apply these variables as input factors for the proposed ANN models to perform water level forecasting. Details of the analysis are described in the Results and Discussion section.

Results and Discussion
The ANN models were integrated into sensors, and their performance was evaluated in real cases. The discussion of the results is divided into three parts: (1) data preparation, (2) development, and (3) application. The research flowchart of this process is shown in Figure 5.    Data quality and quantity are important for the accuracy of data-driven models (e.g., ANN models). The well-calibrated data from 2012 to 2017, including rainfall, flow discharge, and water stage, from the experimental watershed in the Yilan River Basin (Figure 1), were adopted. Following the research flowchart shown in Figure 5, a frequency analysis using rainfall data from five rainfall stations was conducted, and the magnitude of extreme events was related to the corresponding frequency of occurrence through the use of a probability distribution. To find the magnitude associated with a certain return period, the standard frequency factor method [41] is used, as follows: where X T is the calculated rainfall value in a certain period, X is the mean rainfall value from historical data, K is the frequency factor, and s is the standard deviation of the historical data. K was selected for 2-, 5-, 10-, 25-, 50-, and 100-year return period events based on the Pearson III distribution [42]. The calculated cumulative rainfall for a 24-h rainfall duration is shown in Table   Hydrographs specify the precipitation depth in 24 successive time intervals of 1 h duration over a total of 24 h. Such hydrographs are necessary inputs to hydrological and hydraulic models to generate flow discharge and water level data in the Yilan River Basin for different return periods. The details associated with hydrological and hydraulic models are discussed in the next section. The annual 24-h maximum rainfall value at each station from 2012 to 2017 was selected, and the average contribution (%) of each hour to the 24-h duration was calculated. These contributions were reordered in a time sequence with the maximum contribution occurring at the center of the 24-h duration and the remaining contributions arranged in descending order alternatively to the right and to the left of the center value to form a hyetrograph. The results are shown in Figure 6. 1), were adopted. Following the research flowchart shown in Figure 5, a frequency analysis using rainfall data from five rainfall stations was conducted, and the magnitude of extreme events was related to the corresponding frequency of occurrence through the use of a probability distribution. To find the magnitude associated with a certain return period, the standard frequency factor method [41] is used, as follows: where is the calculated rainfall value in a certain period, is the mean rainfall value from historical data, is the frequency factor, and is the standard deviation of the historical data.
was selected for 2-, 5-, 10-, 25-, 50-, and 100-year return period events based on the Pearson III distribution [42]. The calculated cumulative rainfall for a 24-h rainfall duration is shown in Table   Hydrographs specify the precipitation depth in 24 successive time intervals of 1 h duration over a total of 24 h. Such hydrographs are necessary inputs to hydrological and hydraulic models to generate flow discharge and water level data in the Yilan River Basin for different return periods. The details associated with hydrological and hydraulic models are discussed in the next section. The annual 24-h maximum rainfall value at each station from 2012 to 2017 was selected, and the average contribution (%) of each hour to the 24-h duration was calculated. These contributions were reordered in a time sequence with the maximum contribution occurring at the center of the 24-h duration and the remaining contributions arranged in descending order alternatively to the right and to the left of the center value to form a hyetrograph. The results are shown in Figure 6.

Generation of Synthetic Water Level and Flow Discharge Data
The abovementioned synthetic rainfall data were used with the hydrological model Hydrologic Modeling System (HEC-HMS) and the hydraulic model River Analysis System (HEC-RAS) to generate discharge and water level data for five return periods. HEC-HMS simulates rainfall runoff processes at the watershed level and includes different components, such as the runoff volume, baseflow, and channel flow. For more details, please refer to the Technical Reference Manual [43] and User's Manual [44]. In this study, an initial loss and a constant loss rate were subtracted from the precipitation depth, and the remaining depth was referred to as precipitation excess. Thereafter, excess precipitation was transformed to direct surface runoff through the SCS unit hydrograph (SCS-UH) method.
The total flow at YR_Q1 in the Yilan River Basin (Figure 1) was the sum of the direct runoff plus the base flow, and the base flow was obtained from an initial value multiplied by an exponential decay constant. The calculated total flow from the HEC-HMS model was then used as the upstream boundary condition for the downstream HEC-RAS model to calculate the variation in the water level downstream. To validate the performance of the HEC-HMS model, Typhoons Soulik (2012), Dujuan (2015), and Megi (2016) were considered. Table 2 shows the comparisons between the observations and simulations. Differences in peak discharge and time to peak discharge were below 15% and less than 2 h, respectively. Since the results met the relevant performance requirements, the calibrated model was then used to generate synthetic discharge values at YR_Q1 for further analysis. The inflow results associated with different return periods are shown in Figure 7.

Generation of Synthetic Water Level and Flow Discharge Data
The abovementioned synthetic rainfall data were used with the hydrological model Hydrologic Modeling System (HEC-HMS) and the hydraulic model River Analysis System (HEC-RAS) to generate discharge and water level data for five return periods. HEC-HMS simulates rainfall runoff processes at the watershed level and includes different components, such as the runoff volume, baseflow, and channel flow. For more details, please refer to the Technical Reference Manual [43] and User's Manual [44]. In this study, an initial loss and a constant loss rate were subtracted from the precipitation depth, and the remaining depth was referred to as precipitation excess. Thereafter, excess precipitation was transformed to direct surface runoff through the SCS unit hydrograph (SCS-UH) method.
The total flow at YR_Q1 in the Yilan River Basin (Figure 1) was the sum of the direct runoff plus the base flow, and the base flow was obtained from an initial value multiplied by an exponential decay constant. The calculated total flow from the HEC-HMS model was then used as the upstream boundary condition for the downstream HEC-RAS model to calculate the variation in the water level downstream. To validate the performance of the HEC-HMS model, Typhoons Soulik (2012), Dujuan (2015), and Megi (2016) were considered. Table 2 shows the comparisons between the observations and simulations. Differences in peak discharge and time to peak discharge were below 15% and less than 2 h, respectively. Since the results met the relevant performance requirements, the calibrated model was then used to generate synthetic discharge values at YR_Q1 for further analysis. The inflow results associated with different return periods are shown in Figure 7.   There are four water level stations downstream of YR_Q1 ( Figure 1). Among them, data from YR_S1, YR_S2, and YR_S3 were used as input data to train the ANN model. The HEC-RAS model was used to estimate water level variations at the abovementioned locations, and YR_S4 was used as the downstream boundary. HEC-RAS is a 1D river hydraulic model based on the Saint-Venant equations. These equations are approximated with the implicit Preissmann scheme and solved numerically using the Newton-Raphson iteration approach [45]. The downstream boundary condition was assumed to be the observed water levels during Typhoon Dujuan (2015). In comparison with observations, the performance was evaluated at YR_S2 in terms of temporal variations in water level, and the results are shown in Figure 8. The results demonstrate that differences in peak water level and time to peak water level were below 10% and zero hours, respectively. In conclusion, both the hydrological and hydraulic results confirm that the model parameters were well-tuned to generate the data needed for the development of the ANN model in the next section. There are four water level stations downstream of YR_Q1 (Figure 1). Among them, data from YR_S1, YR_S2, and YR_S3 were used as input data to train the ANN model. The HEC-RAS model was used to estimate water level variations at the abovementioned locations, and YR_S4 was used as the downstream boundary. HEC-RAS is a 1D river hydraulic model based on the Saint-Venant equations. These equations are approximated with the implicit Preissmann scheme and solved numerically using the Newton-Raphson iteration approach [45]. The downstream boundary condition was assumed to be the observed water levels during Typhoon Dujuan (2015). In comparison with observations, the performance was evaluated at YR_S2 in terms of temporal variations in water level, and the results are shown in Figure 8. The results demonstrate that differences in peak water level and time to peak water level were below 10% and zero hours, respectively. In conclusion, both the hydrological and hydraulic results confirm that the model parameters were well-tuned to generate the data needed for the development of the ANN model in the next section.

Correlation Analysis and Input Factor Selection
As described in Section 3.2, precipitation from YR_R1 to YR_R5, flow discharge at YR_Q1, and water levels from YR_S1 to YR_S3 were assumed to be correlated with the water level output at YR_S2. A correlation analysis between the targeted water level at the present time and the abovementioned variables from previous periods was performed using Equation (4). This model was considered the ANN_0 model. To include physical features such as the velocity and acceleration of water level variations, a correlation analysis between the first-order and second-order differences of the output and its values from previous periods was conducted. In this way, two more models were developed, named the ANN_1 and ANN_2 models. All of the correlation results are shown in Table 3. The variables with the highest correlation results with those in previous periods were selected as the model inputs. For example, for the ANN_0 model, the highest correlation results are 0.795 and 0.917 for YR_R1 at t−4 h and YR_S1 at t−1 h, respectively. As a result, four variables of YR_R1 and one variable of YR_S1 were selected for the ANN_0 model. However, in some cases, such as YR_R1 in the ANN_1 model, the correlation results 7 and 8 h earlier were 0.601 and 0.608, respectively. These values were different at three decimal digits. Other rainfall-related input factors, such as YR_R2, YR_R3, YR_R4, and YR_R5, were the variables 7 h earlier. To be consistent with other rainfall input factors and to increase computational efficiency, the variables 7 h earlier were selected for YR_R1. In addition, few variables were included, so the efficiency was increased considering the limited computing resources of the sensors. As a result, there were a total of 26, 46, and 46 input factors for the ANN_0, ANN_1, and ANN_2 models, respectively, and details of the input factor selection are listed in Table 4.

Correlation Analysis and Input Factor Selection
As described in Section 3.2, precipitation from YR_R1 to YR_R5, flow discharge at YR_Q1, and water levels from YR_S1 to YR_S3 were assumed to be correlated with the water level output at YR_S2. A correlation analysis between the targeted water level at the present time and the abovementioned variables from previous periods was performed   Table 4. ANN models and their input factors.

Model Training and Testing
Random sampling was employed to split the input data from Section 4.1 into training and testing sets at an 80-20% ratio. For more details in terms of random sampling, a dataset included the input factors listed in Table 4. For example, there were 46 data needed in a dataset to train the model and produce water levels at t + 1, t + 2, and t + 3. Shown in Table 4, all data in the dataset were in a sequential order. The amount of dataset was depended on the data availability during the training process. For example, if a 24-h was available, there were 15 datasets available for training process. At each training, observed or synthetic water levels at t + 1, t + 2, t + 3 were used for performance evaluation. Two extra experiments were conducted. One involved using data from 2-, 5-, 10-, 25-, and 50-yr return periods for training and data from the maximum 100-yr return period for testing. Another involved using data from 5-, 10-, 25-, 50-, and 100-yr return periods for training and data from the minimum 2-yr return period for testing. The number of training data was fully prepared and the number of the data was constant, therefore, there was only one epoch done during this training process. The purpose of these experiments was to assess the ANN models and their forecasting capability beyond the training data range. The RMSE index (Equation (3)) was used to evaluate the model performance. The forecasting results with 1-, 2-, and 3-h lead times at location YR_R2 are shown in Table 5. The results demonstrated that the worst performance among the three models for all three lead times was from the ANN_0 model; its RMSE results were 0.3421 m and 0.7743 m for 1-and 3-h lead times, respectively. The performance of the other two tests for all three models was comparable to that in the cross-validation test. This finding confirmed that the proposed ANN models have the capability to forecast data beyond the testing data range. In comparison, the ANN_1 and ANN_2 models yielded RMSEs that were all below 0.2 m. The ANN_2 model displayed better performance for the results with 2-and 3-h lead times than ANN_1. However, the performance of these two models deteriorated when the forecasting lead time was increased. According to the abovementioned results, the following tests were continuously implemented using the ANN_1 and ANN_2 models.
To test the performance of the proposed ANN models for operational purposes, three historical typhoon events, namely, Yutu (2018), Mangkhut (2018), and Maria (2018), were considered. Three typhoons were split into two typhoons for training and one typhoon for testing. The results are demonstrated in Table 6, and the naming convention is based on the names used in the test case. The performance of both models was fairly consistent. All RMSEs were below or close to 0.1 m regardless of the lead time. According to the individual results, the ANN_2 model performed slightly better than the ANN_1 model. A temporal comparison between observations and the simulations of both models is shown in Figure 9. The results demonstrated that both models agree fairly well with the observations for all three typhoons. It was interesting to find that performance did not deteriorate when the lead time was increased. In contrast, both models produced worse results with a 1-h lead time when the peak water level occurred during Typhoon Yutu in comparison with those results for 2-and 3-h lead times. deteriorate when the lead time was increased. In contrast, both models produced worse results with a 1-h lead time when the peak water level occurred during Typhoon Yutu in comparison with those results for 2-and 3-h lead times. Additionally, the calculation times of the models were compared for different hardware configurations. The comparison was conducted with a hardware configuration that included an AMD Ryzen 94900 central processing unit, an NVIDIA GeForce RTX 2060 (PC) and an RPi 3B+ (the sensor mentioned in Section 3.1), among other components. Using Typhoon Yutu with the ANN_2 model as an example, the calculation time was 5 min if the model was run on the PC and 30 min if it was run on the sensor. The results for the PC and the sensor were consistent, but the calculation time when the model ran on the sensor was 6 times slower than when it ran on the PC. Therefore, calculation time is an issue that must be addressed in future investigations if the sensor is installed on site for real operation. In conclusion, all the results above confirmed the capability of the proposed ANN models to forecast water levels with up to a 3-h lead time. According to the comparison of model performance, the ANN_2 model can be continually applied for further applications and will be discussed in the next section.  Additionally, the calculation times of the models were compared for different hardware configurations. The comparison was conducted with a hardware configuration that included an AMD Ryzen 94900 central processing unit, an NVIDIA GeForce RTX 2060 (PC) and an RPi 3B+ (the sensor mentioned in Section 3.1), among other components. Using Typhoon Yutu with the ANN_2 model as an example, the calculation time was 5 min if the model was run on the PC and 30 min if it was run on the sensor. The results for the PC and the sensor were consistent, but the calculation time when the model ran on the sensor was 6 times slower than when it ran on the PC. Therefore, calculation time is an issue that must be addressed in future investigations if the sensor is installed on site for real operation. In conclusion, all the results above confirmed the capability of the proposed ANN models to forecast water levels with up to a 3-h lead time. According to the comparison of model performance, the ANN_2 model can be continually applied for further applications and will be discussed in the next section.

Applications
The proposed ANN_2 model and integrated sensor were then applied to three other canals and rivers for real-world tests. The details of the geographic locations of these three study areas can be found in Figure 1. The same input factors as in Table 4 were selected, but the number of gages varied according to the number of gages installed in the study areas. For example, there is only one rainfall station near the Shimen Canal; therefore, the number of input factors decreased from 46 to 17. The list of the input factors for these three study areas is shown in Table 7. In addition to the RMSE shown in Equation (3), the coefficient of determination (R 2 ) described below was used to evaluate system performance in these real-world cases.
where O i and O are the hourly observations and mean of the observations, respectively, and P i is the prediction. If the predictions exactly match the observations, SS res = 0 and R 2 = 1. A detailed discussion for each case is given below. In the applications, the sensor was continually receiving data. If the sensor collected new data, the model was retraining with newly collected data. Therefore, a new epoch was completed. This process was repeated until the end of the experiment. Table 7. ANN models and their input factors for various applications. The distance between SC_R1 and SC_R3 is approximately 650 m and is shown in Figure 1. Three sensors were installed at SC_R1, SC_R2, and SC_R3. The experiment was performed on site during 24 June 2021, and 25 June 2021. There were a total of 200 observations collected by each sensor for an hour, and the average value was used for model input. Other data, such as discharge and precipitation data, were retrieved from local stations. The system started to produce water levels at SC_R2 with 1-, 2-, and 3-h lead times after the fifth hour of installation. The overall testing time was 35 h. The comparison between observations and simulations is shown in Figure 10. The errors, which were defined as the difference between observation and prediction, were within 0.075 m. The largest error of 0.075 m was found in Figure 10a with a 1-h lead time. One possible reason for the error was human interference. The canal is operated at a certain water level for the purpose of irrigation. This study did not include the factors of human operations, and the performance deteriorated once human inference was carried out. The RMSEs were between 0.02 and 0.03 m. According to the performance, the system was able to produce the variation in the water levels. However, because of manual operations such as gate control upstream of the canal, R 2 was only in the range of 0.3 to 0.45. The total computation time needed to train the proposed ANN_2 model in the sensor and produce water level forecasts using the observations was 8 min.

Toucian and Beinan Rivers
To avoid the impact of manual operations on system performance, the Toucian and Beinan Rivers were selected to test system performance. Unfortunately, the distance between the observed water surface and the installation of the sensor was beyond the range of the maximum measurement distance. Therefore, the following experiments were conducted by using the data retrieved from the Water Resources Agency directly. The evaluation period was from 3 June 2021, to 31 December 2021, for the Toucian River. Typhoon In-fa on July 24 and Typhoon Lupit on August 6 influenced this study area during this period. Figure 11 shows that the largest difference between observations and predictions at TR_S2 (see the location in Figure 1) was 1.3 m among all forecasts. This was the time when Typhoon In-fa had an impact on this area (solid circle in Figure 11). Similar to previous cases, the system started to produce forecasts the 5th hour after the experiment started. It was confirmed that having more data in the training process increased the accuracy of the ANN_2 model. The errors between observations and simulations were decreased to below 0.5 m for the second typhoon event (dashed circle in Figure 11) and thereafter. Finally, the overall R 2 and RMSEs were approximately 0.98 and 0.045 m, respectively. For the Beinan River, the evaluation period was from 1 July 2021, to 31 December 2021. Typhoon Lupit on August 6 and Typhoon Kompasu on October 14 influenced this study area during the period. Figure 12 shows that the greatest difference between the observations and predictions at BR_S2 (see the location in Figure 1) was below 1 m. The largest errors were found when the highest and lowest water levels were observed (solid and dashed circles in Figure 12), and extreme values such as these were not included in the training data. The overall R 2 and RMSEs were approximately 0.98 and 0.08 m, respectively. In conclusion, based on the above experiments, the proposed ANN_2 model and integrated sensor show excellent potential to perform edge computing locally and generate water level forecasts for real-time operation. The forecasting accuracy was influenced if the water levels were beyond the values in the training data set. Furthermore, the performance of the proposed system decreased if human interference occurred in the study area.
when Typhoon In-fa had an impact on this area (solid circle in Figure 11). Similar to previous cases, the system started to produce forecasts the 5th hour after the experiment started. It was confirmed that having more data in the training process increased the accuracy of the ANN_2 model. The errors between observations and simulations were decreased to below 0.5 m for the second typhoon event (dashed circle in Figure 11) and thereafter. Finally, the overall R 2 and RMSEs were approximately 0.98 and 0.045 m, respectively. (c) Figure 11. (a-c) Comparison of simulation and observed water levels at TR_S2 of the Touciaan River for lead times t + 1 h, t + 2, and t + 3 h, respectively.
For the Beinan River, the evaluation period was from 1 July 2021, to 31 December 2021. Typhoon Lupit on August 6 and Typhoon Kompasu on October 14 influenced this study area during the period. Figure 12 shows that the greatest difference between the observations and predictions at BR_S2 (see the location in Figure 1) was below 1 m. The largest errors were found when the highest and lowest water levels were observed (solid Figure 11. (a-c) Comparison of simulation and observed water levels at TR_S2 of the Touciaan River for lead times t + 1 h, t + 2, and t + 3 h, respectively. the training data. The overall R and RMSEs were approximately 0.98 and 0.08 m, respec-tively. In conclusion, based on the above experiments, the proposed ANN_2 model and integrated sensor show excellent potential to perform edge computing locally and generate water level forecasts for real-time operation. The forecasting accuracy was influenced if the water levels were beyond the values in the training data set. Furthermore, the performance of the proposed system decreased if human interference occurred in the study area. (c) Figure 12. (a-c) Comparison of simulation and observed water levels at BR_S2 of the Beinan River for lead times t + 1 h, t + 2, and t + 3 h, respectively.

Conclusions
In this study, an ANN-based model was integrated into a Raspberry Pi-based sensor to implement edge computing for hourly river water level forecasting. The ANN model is capable of forecasting the river level with a high level of precision by only using previ- Figure 12. (a-c) Comparison of simulation and observed water levels at BR_S2 of the Beinan River for lead times t + 1 h, t + 2, and t + 3 h, respectively.

Conclusions
In this study, an ANN-based model was integrated into a Raspberry Pi-based sensor to implement edge computing for hourly river water level forecasting. The ANN model is capable of forecasting the river level with a high level of precision by only using previously observed water level, rainfall information, and flow discharge as inputs without the need for other hydrological and meteorological parameters. Edge computing is a form of computing that is conducted on site or near a targeted location, thus minimizing the need for data to be processed at a remote data center and increasing the efficiency of the emergency response. This study is a first attempt to combine real-time customized sensors and ANN algorithms in practice. Based on historical measured data from the Yilan River in Yilan County, synthetic upstream rainfall and discharge data were generated for six different return periods using the Pearson III probabilistic model and the HEC-HMS hydrological model with synthetic inflow data. The downstream water level data were obtained with the aid of the HEC-RAS hydraulic model. Different input combinations, including first-order difference and second-order difference water levels, were investigated to enhance the precision of the ANN model. The results demonstrated that the proposed ANN model has the capability to precisely forecast the future fluctuations in the water level of rivers in a short time and with a small number of inputs. The model was then embedded into the customized sensor to forecast the water level over different time horizons up to 3 h in advance. Finally, a comprehensive comparison between forecasts and observations was performed for three other rivers and canals. The findings revealed that the proposed water level sensor with the ANN model exhibited a high level of performance when it was applied to real events. Therefore, the integration proposed in this study is very promising and could be incorporated into a new generation of flood warning systems to prevent and mitigate the impacts of floods in downstream areas. However, the time required to train the ANN model and the system to produce results is over 30 min. Due to the limited computing power of the sensor, the amount of data required to train the model during real-time operation while maintaining high forecasting accuracy to speed up the computing process needs to be investigated further. In addition, different options of microcontroller units with more computing power will also be considered in future studies. Finally, in the current study, only an on-site experiment was performed because of the limitation of devices. More on-site experiments must be performed, and input factors of the ANN model regarding human interference should be included in future studies to extend the system application scope.  Informed Consent Statement: Informed consent was obtained from all subjects involved in the study.

Data Availability Statement:
The experimental watershed data can be accessed in [23], and the data that support the findings of this study are available from the corresponding author, T.-H.Y., upon reasonable request.