Monday, March 6, 2017

Python Advanced #12 - Searching for a value in a text file

This post builds off of the coding that was developed in Python Advanced #10 and #11.  In this post, we want to ask the user to input a value that they are looking for in a text file.  In the code, we want to track the line where the value appears in the text file (line_count) and track the number of times the value appears in the file (vals_count).

Below is an image of the text file that we will be searching.




The coding to search the text file and to track the number of occurrences and the line(s) where this value appears is shown below.



Several simulations of this program are shown below.

If we search for "500", we expect that the program will indicate that the value is not found.



If we search for "43", we expect the program will indicate that the value is found once and is found in line 2.



If we search for "517", we expect the program will indicate that the value is found twice and is found in lines 1 and 4.



Thursday, March 2, 2017

Naming of local inflows at junctions in HEC-ResSim

My co-workers recently encountered a problem with HEC-ResSim that I had not experienced.  The name of their local inflow happened to be the same as the name of the junction that the local inflow was being applied to.  We found out from HEC that ResSim doesn't like this so you want to make sure that the name given to the local inflow is different than the name of the junction.  This is illustrated in the two figures below.

In the figure below, I have selected the junction named "us model end of main".  This junction is highlighted in yellow.





I then selected "Edit Junction Properties" and went to the "Local Flow" tab.  I have given the local flow the name of "dummy main".  Had I given it the name "us model end of main", this could potentially cause issues with HEC-ResSim.




Friday, February 10, 2017

Python Advanced #11 - Reading Two Columns from a Non-Justified Text File and Saving to a Justified Text File

In Python Advanced #10, we read two columns from a non-justified text file and saved those values into lists.  Recall that the non-justified text file was as follows:



Using the code developed in Python Advanced #10 and adding code to write the results of the lists named vals1 and vals2 to a text file, the code is as follows:



However, when we look at the resulting text file, we can see that the values are not justified.



To solve this issue, I use rjust in the write statement to right justify the values.  Note that the number in the parentheses specifies the desired width of the column.  The new write statement now looks like this:



The output from changing this write statement is shown below:




Wednesday, February 1, 2017

Python Advanced #10 - Reading Two Columns from a Non-Justified Text File and Saving into Lists

In this post, we start with a non-justified text file as shown below.  This file is titled nonjust.txt.




Our objective is to extract the first and last columns and then save those into lists.  I will be exporting them to a different text file in a future post.  We do this by using the code below.

We first open the text file that contains the text.  We create empty lists to store the first column (vals1) and to store the last column (vals2).  An additional empty list (templist) is used to store the values found on each line.  In the while loop, we first use the split command on the current line to create three separate value to populate templist.  Without using the split command, we would have a single string.  We then append the first value (position 0 in templist) to vals1 and append the third value (position 2 in templist) to vals2.  We then clear templist of its contents and read the next line in the text file.  After the while loop concludes, we then print vals1 and vals2 to check the contents of the list.



The results from this program are shown below.




Tuesday, January 31, 2017

Python Advanced #9 - Reading Two Columns from a Justified Text File and Writing to a New File

In this post, we start with a justified text file as shown below.  This file is titled sampletext.txt.


Our objective is to extract the date along with the values in the last column and to write those values to a different text file.  We do this by using the code below.

We first open the file and then read each line.  As each line is read, we extract the date and the values in the last column and append them to lists named vals1 and vals2.  Once that is completed, we create a new text file titled twocoloutput.txt and write each pair of values in the lists to this file with each pair of values appearing on a separate line.



The results of this program are shown below.


Python Advanced #8 - Reading One Column from a Justified Text File and Writing to a New File

In this post, we start with a justified text file as shown below.  This file is titled justified.txt.



Our objective is to extract the middle value and write those values to a different text file.  We do this by using the code below.

We first open the file and then read each line.  As each line is read, we extract the middle value and append it to the list named vals.  Once that is completed, we create a new text file titled justextract.txt and write each value in the list to this file with each value appearing on a separate line.



The results of this program are shown below.

Monday, January 9, 2017

Overtopping of Dam in HEC-ResSim

This post will demonstrate the computation in HEC-ResSim that occurs when the dam is overtopped.  For this post, I have developed a simple model with two reservoirs on a single stream.  The schematic is shown below.




The upstream reservoir is the reservoir that will be overtopped.  The elevation of the top of dam and the length at the top of the dam are used in the overtopping computation.  For this example, the top of dam elevation is 100 ft and the length at top of dam is 1000 ft.  The 1000 ft will be the overflow length.  The upstream reservoir has a controlled outlet with a physical capacity of 500 cfs.  These parameters are shown below.



The inflow into the upstream reservoir is a constant 1,000 cfs.  This means that the physical capacity of the controlled outlet is less than the inflow for this example.  The inflow is shown below.




To make sure the dam overtops in this example, I code a maximum release rule of 10 cfs for the flood control and conservation zones.  Once the pool is above the top of the flood control zone (100 ft), the full physical capacity of the controlled outlet can be used.  This rule is shown below.





In the figure below, the pool elevation rises from 90 ft to above the top of the flood control zone due to the inflow being above the maximum release of 10 cfs.  Once the top of the flood control zone is reached, the release increases.



In the release decision report shown below, we can see that on 09Jan2016 at 02:00, the pool elevation is 100.06 ft (0.06 ft above the top of the dam).  The controlled outlet flow (last column) is at its maximum of 500 cfs.  The flow going over the dam is computed to be 44.46 cfs (second to last column).  This continues for the subsequent time steps with 500 cfs going through the controlled outlet and the overtopping flow increasing with increasing pool elevation.



The pool elevation crests at 100.30 ft (shown below) once the weir flow and controlled outlet flow match (or nearly match) the inflow.





Note in the release decision report shown above that the overtopping flow seems to vary even though the elevation appears constant.  Taking the values to additional decimal places does show slight differences.  I have been unable to verify the equation used for overtopping.  The following from Open Channel Flow by M. Hanif Chaudhry provides an answer that is close to that of ResSim, but not exactly the same.