' Name: Plot_Menu_Many
'
' Title: Plot water quality data time series graphs for several constituents at once (menu)
'
' Topics: Views
'
' Description: Asks user for specifications for Plot script, which plots INORGANIC.DAT time series
' Initial idea from:
' Palle Due Larsen in "Graphics the hard way: Time Series in Avenue"
' at http://www.esri.com/library/userconf/proc96/TO250/PAP246/P246.HTM
'
' Author: Michael Silberbauer 2001-05-25
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' 2001-05-25
'
' Requires: Plot_Menu_Many prompts for the input data and calls Plot several times
'
' Self:
'
' Results:
' -----------------------------------------------------------------------------

UserTypeList = {"Domestic_Health","Domestic_Salts","Irrigation","None"}
DefaultColourList = {Color.GetBlue,Color.GetCyan,Color.GetGreen,Color.GetYellow,Color.GetMagenta,Color.GetRed,Color.GetGray,Color.GetBlack}
theProject = av.GetProject
DocumentList = theProject.GetDocs
TableList = List.Make
for each anItem in DocumentList
  if (anItem.is(table)) then
    TableList.Add(anItem)
  end
end
InorganicData = MsgBox.ListAsString(TableList,"Select a file with water quality data","DATA TABLES")
if (InorganicData<>nil) then
  theVTab = InorganicData.getVTab
else
  MsgBox.Info("No tables available in this project","ERROR: NO TABLE")
  exit
end
stnlist = List.Make
Stations = List.Make
if (MsgBox.YesNo("Do you wish to select the stations from a list?","STATION SELECTION MODE",true)) then
  StationData = MsgBox.ListAsString(TableList,"Select a file with monitoring point names","STATION FILE")
  if (StationData<>nil) then
    theStnVTab = StationData.getVTab
  else
    MsgBox.Info("No tables available in this project","ERROR: NO TABLE")
    exit
  end
  FieldList = theStnVTab.GetFields
  shortlist = List.Make
  for each fieldname in FieldList
    if ((fieldname.asString.Contains("st")) or (fieldname.asString.Contains("mon"))) then
      shortlist.Add(fieldname)
      'MsgBox.Info( fieldname.AsString, "FIELD" )

    end
  end
  selfield = MsgBox.ListAsString(shortlist,"Choose a field from"++StationData.AsString,"SELECT FIELD WITH STATION CODES")
  if (selfield=nil) then
    MsgBox.Info("Stopping execution","NO STATION FIELD SELECTED")
    return nil
  else
    StationField = selfield
    'MsgBox.Info( StationField.AsString, "Station field:" )

  end
  theStnField = theStnVTab.FindField(StationField.AsString)
  for each rec in theStnVTab
    stnlist.Add(theStnVTab.ReturnValue(theStnField,rec))
  end
  Stations = MsgBox.MultiListAsString(stnlist,"Choose monitoring points (shift to select many)","SELECT STATIONS TO PLOT")
else
  Stations.Add(MsgBox.Input("Enter name of station to plot:","SINGLE STATION TO PLOT","A2H027Q01"))
end

' Funny things happen to variable names during file conversion: _ValType
' keeps track of this:

_ValType = "Val_info"
if (InorganicData.AsString.Contains(".dbf")) then
  ' text file

  _ValType = "Val_info_dbf"
end
if (InorganicData.AsString.Contains(".txt")) then
  ' text file

  _ValType = "Val_info_txt"
end
theVTab.GetSelection.ClearAll
theVTab.UpdateSelection
FieldList = theVTab.GetFields
shortlist = List.Make
for each fieldname in FieldList
  if (fieldname.asString.Contains("date").Not) then
    shortlist.Add(fieldname)
    'MsgBox.Info( fieldname.AsString, "FIELD" )

  end
end
selfields = MsgBox.MultiListAsString(shortlist,"Choose fields from"++InorganicData.AsString++"(shift to select many)","SELECT FIELDS TO PLOT")
'selfield = MsgBox.ListAsString( shortlist, "Choose a field from"++InorganicData.AsString, "SELECT FIELD TO PLOT" )

if (selfields=nil) then
  MsgBox.Info("NO FIELDS SELECTED","Stopping execution")
  return nil
end
_UseType = MsgBox.ListAsString(UserTypeList,"Choose a user type for guidelines","SELECT USER TYPE")
av.Run("SetConstants",{})
y_max_default = List.Make
for each selfield in selfields
  y_max_default.Add(MsgBox.Input("Enter y-maximum for"++selfield.AsString,"SET Y MAXIMUM","42"))
end
TypeList = {"Stick","Area","Box"}
GraphType = MsgBox.ChoiceAsString(TypeList,"Please select a graph type:","GRAPH TYPES AVAILABLE")
nSelField = 0
for each selfield in selfields
PlotItem = selfields.Get(nSelField)
  Ymax_set = y_max_default.Get(nSelField)
  nSelField = nSelField+1
  CutList = _UseVariables.Get(PlotItem.AsString)
  'MsgBox.ListAsString( CutList, "CutList", "DEBUG" )

  RGBList = _theColourDict.Get(PlotItem.AsString)
  'MsgBox.ListAsString( RGBList, "RGBList", "DEBUG" )
  

  ColList = List.Make
  labels = {"Start year","Start month","Start day","End year","End month","End day","Y Max"}
  default = {"1996","1","1","2000","12","31","Auto"}
  if (CutList<>nil) then
    vType = CutList.Get(1)
    nCuts = CutList.Get(2)
    Guideline1 = CutList.Get(3)
    for each nCut in 3..(nCuts+2)
      labels.Add("Cutpoint"++(nCut-2).AsString)
      default.Add(CutList.Get(nCut).AsString)
    end
  else
    vType = 1
    nCuts = 1
    labels.Add("Cutpoint default")
    default.Add("42")
    ColList.Add(DefaultColourList.Get(0))
    ColList.Add(DefaultColourList.Get(4))
  end
  
  'selection = MsgBox.MultiInput( "Choose dates and graph limits for"++PlotItem.AsString++"in"++InorganicData.AsString+":", "SELECTION PARAMETERS", labels, default )

  selection = default
  if (selection=nil) then
    MsgBox.Info("Nothing to plot","PLOT STOPPING")
    exit
  else
    StartYear = selection.Get(0)
    StartMonth = selection.Get(1)
  end
  if (StartMonth.Count=1) then
    StartMonth = "0"+StartMonth
  end
  StartDay = selection.Get(2)
  if (StartDay.Count=1) then
    StartDay = "0"+StartDay
  end
  StartDate = StartYear+StartMonth+StartDay
  Date1 = Date.Make(StartDate,"yyyyMMdd")
  EndYear = selection.Get(3)
  EndMonth = selection.Get(4)
  if (EndMonth.Count=1) then
    EndMonth = "0"+EndMonth
  end
  EndDay = selection.Get(5)
  if (EndDay.Count=1) then
    EndDay = "0"+EndDay
  end
  EndDate = EndYear+EndMonth+EndDay
  Date2 = Date.Make(EndDate,"yyyyMMdd")
  if ((Date1.AsString="nil") or (Date2.AsString="nil")) then
    MsgBox.Info("Invalid date range"++StartDate++EndDate,"Stopping execution")
    return nil
  end
  
  'Ymax_set   = selection.Get( 6 )

  Guidelines = List.Make
  for each nCol in 7..(nCuts+6)
    Guidelines.Add(selection.Get(nCol).AsNumber)
  end
  for each nCol in 7..(nCuts+7)
    if (RGBList<>nil) then
      ColIndx1 = (nCol-6)*3
      ColIndx2 = ColIndx1+1
      ColIndx3 = ColIndx1+2
      R = RGBList.Get(ColIndx1)
      G = RGBList.Get(ColIndx2)
      B = RGBList.Get(ColIndx3)
      aCol = Color.Make
      aCol.SetRGBList({R,G,B})
      ColList.Add(aCol)
    else
      ColList.Add(DefaultColourList.Get((nCol-7)))
    end
  end
  if (Date2<Date1) then
    Date0 = Date2
    Date2 = Date1
    Date1 = Date0
  end
  av.ShowStopButton
  av.ShowMsg("Seeking graph limits for"++PlotItem.AsString++"...")
  nStn = 0
  for each station in stations
    nStn = nStn+1
    av.SetStatus(100*(nStn/stations.Count))
    av.Run("Plot",{theVTab,Station,PlotItem,Date1,Date2,TypeList,GraphType,vType,Ymax_set,ColList,Guidelines})
  end
end