Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
Fixed bug in QC Results to make annual well working
Browse files Browse the repository at this point in the history
Consolidated query files
  • Loading branch information
heath140 committed May 3, 2023
1 parent fbc9a40 commit 726ce21
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 31 deletions.
16 changes: 8 additions & 8 deletions actions/qcResults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func QcResults(myEnv map[string]string) error {

var opts []qc.Option

aGJ, err := WellAnnGJQuestion()
if err != nil {
return err
}

if aGJ {
opts = append(opts, qc.WithWellAnnGJson())
}
//aGJ, err := WellAnnGJQuestion()
//if err != nil {
// return err
//}
//
//if aGJ {
// opts = append(opts, qc.WithWellAnnGJson())
//}

rechBalance, err := RechBalanceQuestion()
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions qc/qcRCH.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ func NewQC(v *database.Setup, fileName string, options ...Option) *QC {
option(q)
}

q.grid, _ = database.GetGrid(q.v.SlDb)
q.SYear, q.EYear, _ = database.GetStartEndYrs(q.v.SlDb)
var err error
q.grid, err = database.GetGrid(q.v.SlDb)
if err != nil {
panic(err)
}

q.SYear, q.EYear, err = database.GetStartEndYrs(q.v.SlDb)
if err != nil {
panic(err)
}

return q
}
Expand Down
3 changes: 0 additions & 3 deletions qc/sql/RchResultsData.sql

This file was deleted.

4 changes: 0 additions & 4 deletions qc/sql/SsQuery.sql

This file was deleted.

1 change: 0 additions & 1 deletion qc/sql/dataNodes.sql

This file was deleted.

1 change: 0 additions & 1 deletion qc/sql/groupResultsQry.sql

This file was deleted.

4 changes: 4 additions & 0 deletions qc/sql/welQueries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select cell_node from wel_results group by cell_node;
select cell_node, sum(result) result from wel_results WHERE strftime('%%Y', dt) = '%d' GROUP BY cell_node;
select well_id, file_type, result from wel_results where strftime('%%Y', dt) = '%d' and strftime('%%m', dt) = '%s' and file_type < 209;
select cell_node, sum(result) result from wel_results where strftime('%%Y', dt) = '%d' and strftime('%%m', dt) = '%s' and file_type > 208 GROUP BY cell_node;
19 changes: 13 additions & 6 deletions qc/wells.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ var (
wellsQuery string
//go:embed sql/nodeQuery.sql
nodeQuery string
//go:embed sql/RchResultsData.sql
rQry string
//go:embed sql/SsQuery.sql
ssQry string
)

func (q *QC) WellPumpingGJson() error {
pterm.DefaultSection.Println("Well GeoJSON Creation")
spin, _ := pterm.DefaultSpinner.Start("Getting Wells from DB")
formattedQueries := Utils.SplitQueries(welQueries) // welQueries is in wellsAnn.go

q.v.Logger.Info(fmt.Sprintf("Using Grid: %d", q.grid))
var wlls []Well
if err := q.v.PgDb.Select(&wlls, wellsQuery); err != nil {
return err
Expand All @@ -71,12 +69,16 @@ func (q *QC) WellPumpingGJson() error {
mnString = fmt.Sprintf("%d", m)
}

if err := q.v.SlDb.Select(&rResults, rQry, q.Year, mnString); err != nil {
q.v.Logger.Info(fmt.Sprintf("Qry: %s, Year: %d, mnString: %s", formattedQueries[2], q.Year, mnString))
rQuery := fmt.Sprintf(formattedQueries[2], q.Year, mnString)
if err := q.v.SlDb.Select(&rResults, rQuery); err != nil {
return err
}
if err := q.v.SlDb.Select(&ssResults, ssQry, q.Year, mnString); err != nil {
ssQuery := fmt.Sprintf(formattedQueries[3], q.Year, mnString)
if err := q.v.SlDb.Select(&ssResults, ssQuery); err != nil {
return err
}
q.v.Logger.Info(fmt.Sprintf("rResults len: %d", len(rResults)))

rResMap[m] = rResults
ssResMap[m] = ssResults
Expand Down Expand Up @@ -105,6 +107,7 @@ func (q *QC) WellPumpingGJson() error {

_, _ = w.WriteString(header)
cL := len(wlls)
q.v.Logger.Info(fmt.Sprintf("Number of Wells: %d", cL))

p, _ := pterm.DefaultProgressbar.WithTotal(cL).WithTitle("Irr Wells").WithRemoveWhenDone(true).Start()
firstWrittenRecord := true
Expand All @@ -116,11 +119,14 @@ func (q *QC) WellPumpingGJson() error {
return err
}

q.v.Logger.Info(fmt.Sprintf("%+v\n", fc))

// add property to them of the monthly result
annTotal := 0.0
ft := 0
for m := 1; m < 13; m++ {
mn := time.Month(m)
q.v.Logger.Info(fmt.Sprintf("Wells in rResMap[%d]: %d", m, len(rResMap[m])))
res, mft := findWellResult(rResMap[m], wlls[i].Wellid, wlls[i].Nrd)
if q.Monthly {
fc.Properties[mn.String()[:3]+"_AF"] = res
Expand All @@ -137,6 +143,7 @@ func (q *QC) WellPumpingGJson() error {
fc.Properties["AnTl_AF"] = annTotal
fc.Properties["AnTl_cf/d"] = annTotal * 43560

q.v.Logger.Info(fmt.Sprintf("Wellid: %d, AnnTotal: %f", wlls[i].Wellid, annTotal))
if annTotal > 0.0 {
// marshal that item back to json
d, err := fc.MarshalJSON()
Expand Down
13 changes: 7 additions & 6 deletions qc/wellsAnn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
_ "embed"
"fmt"
"github.com/Longitude103/wwum2020/Utils"
"os"
"path/filepath"

Expand All @@ -26,24 +27,24 @@ type GroupedResult struct {
}

var (
//go:embed sql/dataNodes.sql
dataNodes string
//go:embed sql/nodeQuery.sql
nodeQrySubString string
//go:embed sql/groupResultsQry.sql
groupResultsQrySubString string
//go:embed sql/welQueries.sql
welQueries string
)

func (q *QC) WellsAnnPumping() error {
// get all the nodes that there is output data for from sqlite in any year
p, _ := pterm.DefaultSpinner.Start("Getting data")
formattedQueries := Utils.SplitQueries(welQueries)

var Nodes []Node
if err := q.v.SlDb.Select(&Nodes, dataNodes); err != nil {
if err := q.v.SlDb.Select(&Nodes, formattedQueries[0]); err != nil {
return err
}

// get the centroid location of all nodes from postgis as geojson
q.v.Logger.Info(fmt.Sprintf("Using grid: %d", q.grid))
nodeQry := fmt.Sprintf(nodeQrySubString, q.grid)
var NodeLocs []NodeData
if err := q.v.PgDb.Select(&NodeLocs, nodeQry); err != nil {
Expand All @@ -54,7 +55,7 @@ func (q *QC) WellsAnnPumping() error {
// make a map here for each year
for i := q.SYear; i < q.EYear+1; i++ {
// get annual amount of pumping at each node in sqlite
groupResultsQry := fmt.Sprintf(groupResultsQrySubString, i)
groupResultsQry := fmt.Sprintf(formattedQueries[1], i)
var gResults []GroupedResult
if err := q.v.SlDb.Select(&gResults, groupResultsQry); err != nil {
return err
Expand Down

0 comments on commit 726ce21

Please sign in to comment.