<?php
require_once './connection.php';

function metersToFeet($meters) {
    $feet = $meters * 3.28084; // Convert meters to feet
    return $feet;
}

if (!isset($_POST['flowMeterId']) == "") {
    session_start();
    $result_data = array();
    $canal_id = $_POST['flowMeterId'];
    $start_date123 = $_POST['startDate'];
    $to_date = $_POST['endDate'];

    $date = date_create($start_date123);
    $date_start_date = date_format($date, "d/m/Y");
    $en_date1 = date_create($to_date);
    $date_end_date = date_format($en_date1, "d/m/Y");
//    echo '$canal_id ' . $canal_id;
    ?>
    <?php
    $i = 1;
    $st_dt = $start_date123;
    $en_dt = $to_date;
//            echo '$st_dt ' . $st_dt;
//            echo '$en_dt ' . $en_dt;          
    $startDate = new DateTime($st_dt); // Start date                                    
//            $startDate = $start_date; // Start date
    $endDate = new DateTime($en_dt);   // End date
//            $endDate = $to_date;   // End date
// Create an array to store the dates between start and end date
    $dateRange = [];
//            echo '$startDate ' . $startDate;
//            echo '$endDate ' . $endDate;
// Loop to add each date between the start and end date
    while ($startDate <= $endDate) {
        // Add the current date to the array
        $dateRange[] = $startDate->format('Y-m-d'); // Format the date as 'YYYY-MM-DD'
        // Move to the next date
        $startDate->modify('+1 day');
    }
    $arr_cnt = count($dateRange);

    if ($arr_cnt != 1) {

        $id = $canal_id;
//            echo '<br>$id ' . $id;
//            print_r($result);
        foreach ($dateRange as $date) {
            $start_date = $date . " 00:00:02";
            $end_date = $date . " 23:59:59";
            $total_cusec = 0;

//            echo '$start_date '.$start_date;
//            echo '$end_date '.$end_date;
//            $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y %H:%i:%s") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.user_id=tfmd.user_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.user_id=' . $canal_id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
            $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width,tfm.latitude,tfm.longitude FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.flow_meter_id=' . $id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
//            echo '<br>$query1 ' . $query1;
            mysqli_set_charset($con, 'utf8');
            $result1 = mysqli_query($con, $query1);
            if (mysqli_num_rows($result1) > 0) {
                $total_cusec_1 = 0;
                while ($row1 = $result1->fetch_assoc()) {

                    $now = time(); // or your date as well
                    $your_date = strtotime($row1["days_date"]);
                    $datediff = $now - $your_date;

                    $Days = round($datediff / (60 * 60 * 24));
                    $check_on_off = $row1["canal_height"] - $row1["water_level"];

                    $bottom_width = $row1["canal_bottom_width"];
                    $side_slop = $row1["side_slop"];
                    $channel_slop = $row1["channel_slop"];
                    $m_r_coefficient = $row1["m_r_coefficient"];
                    $canal_top_width = $row1["canal_top_width"];
                    $water_level = $row1["water_level"];

                    $a1 = $water_level * $bottom_width;
                    $flow_level_squre = $water_level * $water_level;
                    $a2 = $side_slop * $flow_level_squre;
                    $Area = $a1 + $a2;

                    $z12 = $water_level * $side_slop;
                    $z3 = $bottom_width + $z12;
                    $Area = $z3 * $water_level;

                    $pw0 = $bottom_width;
                    $pw1 = 2 * $water_level;
                    $pw_side_slop = $side_slop * $side_slop;

                    $pw2 = 1 + $pw_side_slop;

                    $pw2_squre = number_format(sqrt($pw2), 4);
//                                                                                            
                    $pw1_squre = number_format($pw1 * $pw2_squre, 4);
//                                                                                            
                    $wetted_perimeter = $pw0 + $pw1_squre;

                    $hydraulic_radius = $Area / $wetted_perimeter;
                    $R = number_format($hydraulic_radius, 3);
//                                      
                    $S = $channel_slop; // slope
                    $V = (1 / $m_r_coefficient) * pow($R, 2 / 3) * sqrt($S);
                    $Q = $Area * $V;

                    $Q_Flow = number_format($Q, 4);
////                                                                                            
                    $Velocity = number_format($V, 2);

                    $Cusec_1_unit = 35.3147;
                    $liter_1_unit = 1000;

                    $meters = $water_level; // Example value in meters
                    $feet = metersToFeet($meters);

                    $water_level_check = $feet;

                    $cusec = 0;

                    $location_id = $row1["flow_meter_id"];
//                                                                                print_r($result);

                    $query21 = 'SELECT * FROM `tbl_canal_measure_data` WHERE location=' . $location_id . ' and flag=1 ORDER by id DESC LIMIT 0,1';
//                    echo '$query1 ' . $query1;
                    mysqli_set_charset($con, 'utf8');
                    $result21 = mysqli_query($con, $query21);
                    if (mysqli_num_rows($result21) > 0) {
                        while ($row21 = mysqli_fetch_assoc($result21)) {

                            if ($water_level_check >= 0.15 && $water_level_check <= 0.25) {
                                $cusec = $row21["0_1"];
                            } else if ($water_level_check >= 0.25 && $water_level_check <= 0.35) {
                                $cusec = $row21["0_2"];
                            } else if ($water_level_check >= 0.35 && $water_level_check <= 0.45) {
                                $cusec = $row21["0_3"];
                            } else if ($water_level_check >= 0.45 && $water_level_check <= 0.55) {
                                $cusec = $row21["0_4"];
                            } else if ($water_level_check >= 0.55 && $water_level_check <= 0.65) {
                                $cusec = $row21["0_5"];
                            } else if ($water_level_check >= 0.65 && $water_level_check <= 0.75) {
                                $cusec = $row21["0_6"];
                            } else if ($water_level_check >= 0.75 && $water_level_check <= 0.85) {
                                $cusec = $row21["0_7"];
                            } else if ($water_level_check >= 0.85 && $water_level_check <= 0.95) {
                                $cusec = $row21["0_8"];
                            } else if ($water_level_check >= 0.95 && $water_level_check <= 1.05) {
                                $cusec = $row21["0_9"];
                            } else if ($water_level_check >= 1.05 && $water_level_check <= 1.15) {
                                $cusec = $row21["1_0"];
                            } else if ($water_level_check >= 1.15 && $water_level_check <= 1.25) {
                                $cusec = $row21["1_1"];
                            } else if ($water_level_check >= 1.25 && $water_level_check <= 1.35) {
                                $cusec = $row21["1_2"];
                            } else if ($water_level_check > 1.35 && $water_level_check <= 1.45) {
                                $cusec = $row21["1_3"] + 3;
                            } else if ($water_level_check > 1.45 && $water_level_check <= 1.55) {
                                $cusec = $row21["1_4"] + 3;
                            } else if ($water_level_check > 1.55 && $water_level_check <= 1.65) {
                                $cusec = $row21["1_5"] + 3;
                            } else if ($water_level_check > 1.65 && $water_level_check <= 1.75) {
                                $cusec = $row21["1_6"] + 3;
                            } else if ($water_level_check > 1.75 && $water_level_check <= 1.85) {
                                $cusec = $row21["1_7"] + 3;
                            } else if ($water_level_check > 1.85 && $water_level_check <= 1.95) {
                                $cusec = $row21["1_8"] + 3;
                            } else if ($water_level_check > 1.95 && $water_level_check <= 2.05) {
                                $cusec = $row21["1_9"] + 3;
                            } else if ($water_level_check > 2.05 && $water_level_check <= 2.15) {
                                $cusec = $row21["2_0"] + 3;
                            } else if ($water_level_check > 2.15 && $water_level_check <= 2.25) {
                                $cusec = $row21["2_1"] + 3;
                            } else if ($water_level_check > 2.25 && $water_level_check <= 2.35) {
                                $cusec = $row21["2_2"] + 3;
                            } else if ($water_level_check > 2.35 && $water_level_check <= 2.45) {
                                $cusec = $row21["2_3"] + 3;
                            } else if ($water_level_check > 2.45 && $water_level_check <= 2.55) {
                                $cusec = $row21["2_4"] + 3;
                            } else if ($water_level_check > 2.55 && $water_level_check <= 2.65) {
                                $cusec = $row21["2_5"] + 3;
                            } else if ($water_level_check > 2.65 && $water_level_check <= 2.75) {
                                $cusec = $row21["2_6"] + 3;
                            } else if ($water_level_check > 2.75 && $water_level_check <= 2.85) {
                                $cusec = $row21["2_7"] + 3;
                            } else if ($water_level_check > 2.85 && $water_level_check <= 2.95) {
                                $cusec = $row21["2_8"] + 3;
                            } else if ($water_level_check > 2.95 && $water_level_check <= 3.05) {
                                $cusec = $row21["2_9"] + 3;
                            } else if ($water_level_check > 3.05 && $water_level_check <= 3.15) {
                                $cusec = $row21["3_0"] + 3;
                            } else if ($water_level_check > 3.15 && $water_level_check <= 3.25) {
                                $cusec = $row21["3_1"] + 3;
                            } else if ($water_level_check > 3.25 && $water_level_check <= 3.35) {
                                $cusec = $row21["3_2"] + 3;
                            } else if ($water_level_check > 3.35 && $water_level_check <= 3.45) {
                                $cusec = $row21["3_3"] + 3;
                            } else if ($water_level_check > 3.45 && $water_level_check <= 3.55) {
                                $cusec = $row21["3_4"] + 3;
                            } else if ($water_level_check > 3.55 && $water_level_check <= 3.65) {
                                $cusec = $row21["3_5"] + 3;
                            } else if ($water_level_check > 3.65 && $water_level_check <= 3.75) {
                                $cusec = $row21["3_6"] + 3;
                            } else if ($water_level_check > 3.75 && $water_level_check <= 3.85) {
                                $cusec = $row21["3_7"] + 3;
                            } else if ($water_level_check > 3.85 && $water_level_check <= 3.95) {
                                $cusec = $row21["3_8"] + 3;
                            } else if ($water_level_check > 3.95 && $water_level_check <= 4.05) {
                                $cusec = $row21["3_9"] + 3;
                            } else if ($water_level_check > 4.05 && $water_level_check <= 4.15) {
                                $cusec = $row21["4_0"] + 3;
                            } else if ($water_level_check > 4.15 && $water_level_check <= 4.25) {
                                $cusec = $row21["4_1"] + 3;
                            } else if ($water_level_check > 4.25 && $water_level_check <= 4.35) {
                                $cusec = $row21["4_2"] + 3;
                            } else if ($water_level_check > 4.35 && $water_level_check <= 4.45) {
                                $cusec = $row21["4_3"] + 3;
                            } else if ($water_level_check > 4.45 && $water_level_check <= 4.55) {
                                $cusec = $row21["4_4"] + 3;
                            } else if ($water_level_check > 4.55 && $water_level_check <= 4.65) {
                                $cusec = $row21["4_5"] + 3;
                            } else if ($water_level_check > 4.65 && $water_level_check <= 4.75) {
                                $cusec = $row21["4_6"] + 3;
                            } else if ($water_level_check > 4.72 && $water_level_check <= 4.85) {
                                $cusec = $row21["4_7"] + 3;
                            } else if ($water_level_check > 4.82 && $water_level_check <= 4.95) {
                                $cusec = $row21["4_8"] + 3;
                            } else if ($water_level_check > 4.92 && $water_level_check <= 5.05) {
                                $cusec = $row21["4_9"] + 3;
                            } else if ($water_level_check > 5.02 && $water_level_check <= 5.15) {
                                $cusec = $row21["5_0"] + 3;
                            } else if ($water_level_check > 5.15 && $water_level_check <= 5.25) {
                                $cusec = $row21["5_1"] + 3;
                            } else if ($water_level_check > 5.25 && $water_level_check <= 5.35) {
                                $cusec = $row21["5_2"] + 3;
                            } else if ($water_level_check > 5.35 && $water_level_check <= 5.45) {
                                $cusec = $row21["5_3"] + 3;
                            } else if ($water_level_check > 5.45 && $water_level_check <= 5.55) {
                                $cusec = $row21["5_4"] + 3;
                            } else if ($water_level_check > 5.55 && $water_level_check <= 5.65) {
                                $cusec = $row21["5_5"] + 3;
                            } else if ($water_level_check > 5.65 && $water_level_check <= 5.75) {
                                $cusec = $row21["5_6"] + 3;
                            } else if ($water_level_check > 5.75 && $water_level_check <= 5.85) {
                                $cusec = $row21["5_7"] + 3;
                            } else if ($water_level_check > 5.85 && $water_level_check <= 5.95) {
                                $cusec = $row21["5_8"] + 3;
                            } else if ($water_level_check > 5.95 && $water_level_check <= 6.05) {
                                $cusec = $row21["5_9"] + 3;
                            } else if ($water_level_check > 6.05 && $water_level_check <= 6.15) {
                                $cusec = $row21["6_0"] + 3;
                            } else if ($water_level_check > 6.15 && $water_level_check <= 6.25) {
                                $cusec = $row21["6_1"] + 3;
                            } else if ($water_level_check > 6.25 && $water_level_check <= 6.35) {
                                $cusec = $row21["6_2"] + 3;
                            } else if ($water_level_check > 6.35 && $water_level_check <= 6.45) {
                                $cusec = $row21["6_3"] + 3;
                            } else if ($water_level_check > 6.45 && $water_level_check <= 6.55) {
                                $cusec = $row21["6_4"] + 3;
                            } else if ($water_level_check > 6.55 && $water_level_check <= 6.65) {
                                $cusec = $row21["6_5"] + 3;
                            } else if ($water_level_check > 6.65 && $water_level_check <= 6.75) {
                                $cusec = $row21["6_6"] + 3;
                            } else if ($water_level_check > 6.75 && $water_level_check <= 6.85) {
                                $cusec = $row21["6_7"] + 3;
                            } else if ($water_level_check > 6.85) {
                                $cusec = $row21["6_8"] + 3;
                            }
                        }
                    } else {
//                                echo '<br>$Q_Flow '.$Q_Flow;
                        $cusec = number_format($Q_Flow * $Cusec_1_unit, 2);
//                        echo '<br>$cusec ' . $cusec;
                    }

                    $water_level = $row1["water_level"];

                    $meter_name = $row1["meter_name"];
                    $address = $row1["address"];
                    $water_level = $water_level;
//                            $Q_Flow = $Q_Flow;
//                            $Velocity = $Velocity;
//                            $cusec = $cusec;
//                    echo '<br>$cusec '.$cusec;
                    
                    $cusec = (float) str_replace(',', '', $cusec);
                    
                    $minit5_cusec = $cusec * 300;
//                    $total_cusec = $total_cusec + $minit5_cusec;
                    $total_cusec_1 = $total_cusec_1 + $minit5_cusec;
//                    echo '<br>$total_cusec_1 '.$total_cusec_1;
//                    echo '<br>$minit5_cusec ' . $minit5_cusec;
//                    echo '<br>$water_level ' . $water_level;
//                    echo '<br>$cusec ' . $cusec;
//                    echo '<br>$total_cusec_1 ' . $total_cusec_1;

                    $created_date = $row1["created_date_new"];
                }

//                $total_cusec = $total_cusec + $total_cusec_1;
//                echo '$total_cusec ' . $total_cusec;
//                echo '$total_cusec_1 ' . $total_cusec_1;

                if ($canal_id == 3) {    // Vyara Main Canal Measurement
                    $a = $Velocity / 10;
                    $Velocity = $a + 1 - 0.025;
                } else {
                }


                $index["id"] = "";
                $index["flow_meter_id"] = "";
                $index["user_id"] = "";
                $index["meter_name"] = $meter_name;
                $index["address"] = $address;
                $index["water_level"] = $water_level;
                $index["Q_Flow"] = $Q_Flow;
                $index["Velocity"] = $Velocity;
                $index["cusec"] = $cusec;
                $index["latitude"] = "";
                $index["longitude"] = "";
                $index["created_date"] = $created_date;
                $index["Day"] = "ONE";
                $index["Total_cusec"] = $total_cusec_1;

                array_push($result_data, $index);
            }
        }
        echo json_encode($result_data);
    } else {
        $id = $canal_id;
//            echo '<br>$id ' . $id;
//            print_r($result);
        foreach ($dateRange as $date) {
            $start_date = $date . " 00:00:02";
            $end_date = $date . " 23:59:59";
//            $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y %H:%i:%s") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.user_id=tfmd.user_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.user_id=' . $canal_id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
            $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y %H:%i:%s") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width,tfm.latitude,tfm.longitude FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.flow_meter_id=' . $id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
//            echo '$query1 ' . $query1;
            mysqli_set_charset($con, 'utf8');
            $result1 = mysqli_query($con, $query1);
            if (mysqli_num_rows($result1) > 0) {

                while ($row1 = $result1->fetch_assoc()) {

                    $now = time(); // or your date as well
                    $your_date = strtotime($row1["days_date"]);
                    $datediff = $now - $your_date;

                    $Days = round($datediff / (60 * 60 * 24));
                    $check_on_off = $row1["canal_height"] - $row1["water_level"];

                    $bottom_width = $row1["canal_bottom_width"];
                    $side_slop = $row1["side_slop"];
                    $channel_slop = $row1["channel_slop"];
                    $m_r_coefficient = $row1["m_r_coefficient"];
                    $canal_top_width = $row1["canal_top_width"];
                    $water_level = $row1["water_level"];

                    $a1 = $water_level * $bottom_width;
                    $flow_level_squre = $water_level * $water_level;
                    $a2 = $side_slop * $flow_level_squre;
                    $Area = $a1 + $a2;

                    $z12 = $water_level * $side_slop;
                    $z3 = $bottom_width + $z12;
                    $Area = $z3 * $water_level;

                    $pw0 = $bottom_width;
                    $pw1 = 2 * $water_level;
                    $pw_side_slop = $side_slop * $side_slop;

                    $pw2 = 1 + $pw_side_slop;

                    $pw2_squre = number_format(sqrt($pw2), 4);
//                                                                                            
                    $pw1_squre = number_format($pw1 * $pw2_squre, 4);
//                                                                                            
                    $wetted_perimeter = $pw0 + $pw1_squre;

                    $hydraulic_radius = $Area / $wetted_perimeter;
                    $R = number_format($hydraulic_radius, 3);
//                                      
                    $S = $channel_slop; // slope
                    $V = (1 / $m_r_coefficient) * pow($R, 2 / 3) * sqrt($S);
                    $Q = $Area * $V;

                    $Q_Flow = number_format($Q, 4);
////                                                                                            
                    $Velocity = number_format($V, 2);

                    $Cusec_1_unit = 35.3147;
                    $liter_1_unit = 1000;

                    $meters = $water_level; // Example value in meters
                    $feet = metersToFeet($meters);

                    $water_level_check = $feet;

                    $cusec = 0;

                    $location_id = $row1["flow_meter_id"];
//                                                                                print_r($result);

                    $query21 = 'SELECT * FROM `tbl_canal_measure_data` WHERE location=' . $location_id . ' and flag=1 ORDER by id DESC LIMIT 0,1';
//                    echo '$query1 ' . $query1;
                    mysqli_set_charset($con, 'utf8');
                    $result21 = mysqli_query($con, $query21);
                    if (mysqli_num_rows($result21) > 0) {
                        while ($row21 = mysqli_fetch_assoc($result21)) {

                            if ($water_level_check >= 0.15 && $water_level_check <= 0.25) {
                                $cusec = $row21["0_1"];
                            } else if ($water_level_check >= 0.25 && $water_level_check <= 0.35) {
                                $cusec = $row21["0_2"];
                            } else if ($water_level_check >= 0.35 && $water_level_check <= 0.45) {
                                $cusec = $row21["0_3"];
                            } else if ($water_level_check >= 0.45 && $water_level_check <= 0.55) {
                                $cusec = $row21["0_4"];
                            } else if ($water_level_check >= 0.55 && $water_level_check <= 0.65) {
                                $cusec = $row21["0_5"];
                            } else if ($water_level_check >= 0.65 && $water_level_check <= 0.75) {
                                $cusec = $row21["0_6"];
                            } else if ($water_level_check >= 0.75 && $water_level_check <= 0.85) {
                                $cusec = $row21["0_7"];
                            } else if ($water_level_check >= 0.85 && $water_level_check <= 0.95) {
                                $cusec = $row21["0_8"];
                            } else if ($water_level_check >= 0.95 && $water_level_check <= 1.05) {
                                $cusec = $row21["0_9"];
                            } else if ($water_level_check >= 1.05 && $water_level_check <= 1.15) {
                                $cusec = $row21["1_0"];
                            } else if ($water_level_check >= 1.15 && $water_level_check <= 1.25) {
                                $cusec = $row21["1_1"];
                            } else if ($water_level_check >= 1.25 && $water_level_check <= 1.35) {
                                $cusec = $row21["1_2"];
                            } else if ($water_level_check > 1.35 && $water_level_check <= 1.45) {
                                $cusec = $row21["1_3"] + 3;
                            } else if ($water_level_check > 1.45 && $water_level_check <= 1.55) {
                                $cusec = $row21["1_4"] + 3;
                            } else if ($water_level_check > 1.55 && $water_level_check <= 1.65) {
                                $cusec = $row21["1_5"] + 3;
                            } else if ($water_level_check > 1.65 && $water_level_check <= 1.75) {
                                $cusec = $row21["1_6"] + 3;
                            } else if ($water_level_check > 1.75 && $water_level_check <= 1.85) {
                                $cusec = $row21["1_7"] + 3;
                            } else if ($water_level_check > 1.85 && $water_level_check <= 1.95) {
                                $cusec = $row21["1_8"] + 3;
                            } else if ($water_level_check > 1.95 && $water_level_check <= 2.05) {
                                $cusec = $row21["1_9"] + 3;
                            } else if ($water_level_check > 2.05 && $water_level_check <= 2.15) {
                                $cusec = $row21["2_0"] + 3;
                            } else if ($water_level_check > 2.15 && $water_level_check <= 2.25) {
                                $cusec = $row21["2_1"] + 3;
                            } else if ($water_level_check > 2.25 && $water_level_check <= 2.35) {
                                $cusec = $row21["2_2"] + 3;
                            } else if ($water_level_check > 2.35 && $water_level_check <= 2.45) {
                                $cusec = $row21["2_3"] + 3;
                            } else if ($water_level_check > 2.45 && $water_level_check <= 2.55) {
                                $cusec = $row21["2_4"] + 3;
                            } else if ($water_level_check > 2.55 && $water_level_check <= 2.65) {
                                $cusec = $row21["2_5"] + 3;
                            } else if ($water_level_check > 2.65 && $water_level_check <= 2.75) {
                                $cusec = $row21["2_6"] + 3;
                            } else if ($water_level_check > 2.75 && $water_level_check <= 2.85) {
                                $cusec = $row21["2_7"] + 3;
                            } else if ($water_level_check > 2.85 && $water_level_check <= 2.95) {
                                $cusec = $row21["2_8"] + 3;
                            } else if ($water_level_check > 2.95 && $water_level_check <= 3.05) {
                                $cusec = $row21["2_9"] + 3;
                            } else if ($water_level_check > 3.05 && $water_level_check <= 3.15) {
                                $cusec = $row21["3_0"] + 3;
                            } else if ($water_level_check > 3.15 && $water_level_check <= 3.25) {
                                $cusec = $row21["3_1"] + 3;
                            } else if ($water_level_check > 3.25 && $water_level_check <= 3.35) {
                                $cusec = $row21["3_2"] + 3;
                            } else if ($water_level_check > 3.35 && $water_level_check <= 3.45) {
                                $cusec = $row21["3_3"] + 3;
                            } else if ($water_level_check > 3.45 && $water_level_check <= 3.55) {
                                $cusec = $row21["3_4"] + 3;
                            } else if ($water_level_check > 3.55 && $water_level_check <= 3.65) {
                                $cusec = $row21["3_5"] + 3;
                            } else if ($water_level_check > 3.65 && $water_level_check <= 3.75) {
                                $cusec = $row21["3_6"] + 3;
                            } else if ($water_level_check > 3.75 && $water_level_check <= 3.85) {
                                $cusec = $row21["3_7"] + 3;
                            } else if ($water_level_check > 3.85 && $water_level_check <= 3.95) {
                                $cusec = $row21["3_8"] + 3;
                            } else if ($water_level_check > 3.95 && $water_level_check <= 4.05) {
                                $cusec = $row21["3_9"] + 3;
                            } else if ($water_level_check > 4.05 && $water_level_check <= 4.15) {
                                $cusec = $row21["4_0"] + 3;
                            } else if ($water_level_check > 4.15 && $water_level_check <= 4.25) {
                                $cusec = $row21["4_1"] + 3;
                            } else if ($water_level_check > 4.25 && $water_level_check <= 4.35) {
                                $cusec = $row21["4_2"] + 3;
                            } else if ($water_level_check > 4.35 && $water_level_check <= 4.45) {
                                $cusec = $row21["4_3"] + 3;
                            } else if ($water_level_check > 4.45 && $water_level_check <= 4.55) {
                                $cusec = $row21["4_4"] + 3;
                            } else if ($water_level_check > 4.55 && $water_level_check <= 4.65) {
                                $cusec = $row21["4_5"] + 3;
                            } else if ($water_level_check > 4.65 && $water_level_check <= 4.75) {
                                $cusec = $row21["4_6"] + 3;
                            } else if ($water_level_check > 4.72 && $water_level_check <= 4.85) {
                                $cusec = $row21["4_7"] + 3;
                            } else if ($water_level_check > 4.82 && $water_level_check <= 4.95) {
                                $cusec = $row21["4_8"] + 3;
                            } else if ($water_level_check > 4.92 && $water_level_check <= 5.05) {
                                $cusec = $row21["4_9"] + 3;
                            } else if ($water_level_check > 5.02 && $water_level_check <= 5.15) {
                                $cusec = $row21["5_0"] + 3;
                            } else if ($water_level_check > 5.15 && $water_level_check <= 5.25) {
                                $cusec = $row21["5_1"] + 3;
                            } else if ($water_level_check > 5.25 && $water_level_check <= 5.35) {
                                $cusec = $row21["5_2"] + 3;
                            } else if ($water_level_check > 5.35 && $water_level_check <= 5.45) {
                                $cusec = $row21["5_3"] + 3;
                            } else if ($water_level_check > 5.45 && $water_level_check <= 5.55) {
                                $cusec = $row21["5_4"] + 3;
                            } else if ($water_level_check > 5.55 && $water_level_check <= 5.65) {
                                $cusec = $row21["5_5"] + 3;
                            } else if ($water_level_check > 5.65 && $water_level_check <= 5.75) {
                                $cusec = $row21["5_6"] + 3;
                            } else if ($water_level_check > 5.75 && $water_level_check <= 5.85) {
                                $cusec = $row21["5_7"] + 3;
                            } else if ($water_level_check > 5.85 && $water_level_check <= 5.95) {
                                $cusec = $row21["5_8"] + 3;
                            } else if ($water_level_check > 5.95 && $water_level_check <= 6.05) {
                                $cusec = $row21["5_9"] + 3;
                            } else if ($water_level_check > 6.05 && $water_level_check <= 6.15) {
                                $cusec = $row21["6_0"] + 3;
                            } else if ($water_level_check > 6.15 && $water_level_check <= 6.25) {
                                $cusec = $row21["6_1"] + 3;
                            } else if ($water_level_check > 6.25 && $water_level_check <= 6.35) {
                                $cusec = $row21["6_2"] + 3;
                            } else if ($water_level_check > 6.35 && $water_level_check <= 6.45) {
                                $cusec = $row21["6_3"] + 3;
                            } else if ($water_level_check > 6.45 && $water_level_check <= 6.55) {
                                $cusec = $row21["6_4"] + 3;
                            } else if ($water_level_check > 6.55 && $water_level_check <= 6.65) {
                                $cusec = $row21["6_5"] + 3;
                            } else if ($water_level_check > 6.65 && $water_level_check <= 6.75) {
                                $cusec = $row21["6_6"] + 3;
                            } else if ($water_level_check > 6.75) {
                                $cusec = $row21["6_7"] + 3;
                            }
                        }
                    } else {
//                                echo '<br>$Q_Flow '.$Q_Flow;
                        $cusec = number_format($Q_Flow * $Cusec_1_unit, 2);
//                                echo '<br>$cusec '.$cusec;
                    }

                    $water_level = $row1["water_level"];

                    $meter_name = $row1["meter_name"];
                    $address = $row1["address"];
                    $water_level = $water_level;
//                            $Q_Flow = $Q_Flow;
//                            $Velocity = $Velocity;
//                            $cusec = $cusec;

                    $created_date = $row1["created_date_new"];

                    $index["id"] = $row1["id"];
                    $index["flow_meter_id"] = $row1["flow_meter_id"];
                    $index["user_id"] = $row1["user_id"];
                    $index["meter_name"] = $meter_name;
                    $index["address"] = $address;
                    $index["water_level"] = $water_level;
                    $index["Q_Flow"] = $Q_Flow;
                    $index["Velocity"] = $Velocity;
                    $index["cusec"] = $cusec;
                    $index["latitude"] = $row1["latitude"];
                    $index["longitude"] = $row1["longitude"];
                    $index["created_date"] = $created_date;
                    $index["Day"] = "ZERO";
                    $index["Total_cusec"] = 0;

//                    print_r($index);
                    array_push($result_data, $index);
                }
            }
            echo json_encode($result_data);
        }
    }
}
?>