;Written by Xiaoming Hu, CAPS, OU, Sept. 28, 2011 
; contact yuanfangcan@gmail.com
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"   
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"   
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"

begin
   wks_type = "png"
   wks_type = "pdf"
   wks_type@wkWidth = 1088
   wks_type@wkHeight = 1088
   monthshort = (/"Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sept","Oct","Nov","Dec"/)
;   sites          = (/"Denton","Frisco","Grapevine","Eagle","Dallas_North2","Rockwall","DALLAS_HIN","FW_N","Redbird","Arlington_A", "Keller_C17","PilotP","Greenville","Kaufman","Italy","Cleburne","Granbury","Midlothian","Parker","Corsicana"/);,"Goldsby","Choctaw","Yukon","NorthOKC",     "OKC",             "Moore"/)
;   sites_filename = (/"Denton","Frisco","Grapevine","Eagle","Dallas_North2","Rockwall","DALLAS_HIN","FW_N","Redbird","Arlington_A",  "Keller_C17","PilotP","Greenville","Kaufman","Italy","Cleburne","Granbury","Midlothian","Parker","Corsicana"/);,"Goldsby","Choctaw","Yukon","OCC_OKC_North","OSDH_OKC_Central","Moore"/)
   sites          = (/"OKC","DFW_Eagle"/)
   sites_filename = (/"OCC_OKC_North","Eagle"/)
   Colors         =  (/ "green","blue","Turquoise3","red",  "green","blue","Turquoise3", "Cyan3","Magenta2","Magenta2","green","green"/)
  year_to_plot = 2021
  month_to_plot = 6 
;  year_to_plot = 2018
;  month_to_plot = 7 
  monthnext = month_to_plot + 1 
  figurename = "TS_Ozone_OKC_Dallas_0"
  wks = gsn_open_wks(wks_type ,figurename)          ; eps,pdf,x11,ncgm,eeps

 do isites= 0, dimsizes(sites)-1
; do isites= 0, 10 
; do isites= 0, 0 
  print("working on "+ "TS_O3_"+sites_filename(isites)+"_since2014.csv.txt.txt")
  data_array_chemistry = readAsciiTable("TS_O3_"+sites_filename(isites)+"_since2014.csv.txt.txt", 3, "double", 1)
  dims= dimsizes(data_array_chemistry)
  data_array_chemistry@_FillValue = -999
   Date = data_array_chemistry(:,0) 
   Hour = data_array_chemistry(:,1) 
   Ozone= data_array_chemistry(:,2)
  Month_day = mod(Date, 10000)
  Day       = mod(Date, 100)
  Year      = doubletoint(Date)/10000
  print(Month_day(dims(0)-10:dims(0)-1))
  print(Year(dims(0)-10:dims(0)-1))

  res1                       = True            ; plot mods desired


    a4_height = 29.7 ; in centimeters, if my 
    a4_width = 23.0 ; reference is correct 
    cm_per_inch = 2.54 
 
  res1@gsnFrame               = False                     ; don't draw yet
  res1@gsnDraw                = False                     ; don't advance frame
;  res1@mpShapeMode  = "FreeAspect"
  res1@vpHeightF             =0.2
  res1@vpWidthF              = 0.9 
  res1@vpXF             = 0.091
  res1@vpYF              =0.9 - isites*0.21

  res1@gsnPaperOrientation       = "portrait"
  res1@gsnMaximize       = False ; True
  res1@gsnPaperMargin    = 0.01
;  res1@tiYAxisString         = variables(ivar) + ", K"
  res1@tiYAxisString         = "Ozone, ppbv"
  res1@tiXAxisString         = "Time of Day, CST";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
;  res1@xyDashPatterns    = (/16,4,16,2,15,3/)       ; 0 is default (solid)
  res1@xyDashPatterns    = (/0,4,16,2,15,3/)       ; 0 is default (solid)
;  res1@xyMarkLineModes   = (/"Markers","MarkLines","MarkLines","MarkLines","MarkLines","MarkLines"   /)
  res1@xyMarkLineMode   = "Lines"
 res1@xyMarkers         =  (/16,16,16,16,16,16/)                      ; choose type of marker  
; res1@xyMarkerColor     = "Turquoise3"       
 res1@xyLineColors       = (/"red","Green","blue","Purple4","black","brown"/)
 res1@xyMarkerSizeF     = 0.01
  res1@xyLineThicknessF       = 3 
  res1@tiXAxisFuncCode = "~"
  res1@pmTickMarkDisplayMode = "Always"         ; turn on tickmarks
;  res1@tmXTOn = False            ; turn off top   labels
;  res1@tmYROn = False            ; turn off right labels
  res1@tmXTLabelsOn = False
  res1@tmYRLabelsOn = False

  res1@lgPerimOn = False
  res1@lgLabelFontHeightF = 0.018
  res1@tiXAxisFontHeightF= 0.018
  res1@tiYAxisFontHeightF= 0.018
  res1@tmXBLabelFontHeightF = 0.018
  res1@tmYLLabelFontHeightF = 0.018
  res1@tmYRMajorOutwardLengthF = 0
  res1@tmYLMajorOutwardLengthF = 0
  res1@tmXBMajorOutwardLengthF = 0
  res1@tmXBMinorOutwardLengthF = 0


  res1@pmLegendSide           = "Top"               ; Change location of
  res1@pmLegendParallelPosF   = .82                  ; move units right
  res1@pmLegendOrthogonalPosF = -.3
  res1@pmLegendWidthF         = 0.08                ; Change width and
  res1@pmLegendHeightF        = 0.18                ; height of legend.
;  res1@tiYAxisOffsetXF = 0.055
  res1@tmYLLabelDeltaF = -0.8 
  res1@tmXBMode = "Explicit"
;  res1@tmXBLabels     = "8/"+ispan (11,27,1) 
;  res1@tmXBValues     = fspan(181,213,1+(213-181)*8) 
;  res1@tmXBLabels     = (/"6/30","","6","","12","","18","","7/1","","6","","12","","18","","7/2","","6","","12","","18","","7/3","","6","","12","","18","","7/4","","6","","12","","18","","7/5","","6","","12","","18","","7/6","","6","","12","","18","","7/7","","6","","12","","18","","7/8","","6","","12","","18","","7/9","","6","","12","","18","","7/10","","6","","12","","18","","7/11","","6","","12","","18","","7/12","","6","","12","","18","","7/13","","6","","12","","18","","7/14","","6","","12","","18","","7/15","","6","","12","","18","","7/16","","6","","12","","18","","7/17","","6","","12","","18","","7/18","","6","","12","","18","","7/19","","6","","12","","18","","7/20","","6","","12","","18","","7/21","","6","","12","","18","","7/22","","6","","12","","18","","7/23","","6","","12","","18","","7/24","","6","","12","","18","","7/25","","6","","12","","18","","7/26","","6","","12","","18","","7/27","","6","","12","","18","","7/28","","6","","12","","18","","7/29","","6","","12","","18","","7/30","","6","","12","","18","","7/31","","6","","12","","18","","8/1"/) 
  res1@tmYLLabelStride = 2
  res1@tmXBLabelStride = 2 
  res1@tmYLMinorOn = False 
;  res1@tmYLValues     = ispan(330,370,5) ;(/6,12,18,24,30,36,42,42+6,42+12,18,24,30,36,42/)       ; location of explicit labels
;  res1@pmLegendDisplayMode    = "Always"       ; create a legend
  res1@tmYMajorGrid                = True          ; implement x grid 
  res1@tmYMajorGridThicknessF      = 1.0           ; 2.0 is default
  res1@tmYMajorGridLineDashPattern = 2             ; select short dash lines


  res1@gsnLeftString          = "" ;files(ifiles) 
  res1@gsnRightString          = "" ; sites(isites) 
;  res1@xyExplicitLegendLabels = sites

;  figurename = "TS_Ozone_"+sites(isites)+"_29"
  setvalues wks
;    "wkColorMap"        : "BlAqGrYeOrRevi200"
    "wkForegroundColor" : "Black";"Blue"
  end setvalues

;  res1@tiXAxisString         = "Date";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units

  index = ind(Hour.eq.0)
  res1@tmXBValues     = index 
;  res1@tmXBLabels     = Year(index) 
  res1@tmXBLabels     = Day(index) 
  delete(index) ; = ind(Hour.eq.0)
  res1@trXMinF           =  ind(Date.eq.(year_to_plot*10000+1+ month_to_plot*100).and.Hour.eq.0) 
  res1@trXMaxF           =  ind(Date.eq.(year_to_plot*10000+1+ monthnext*100).and.Hour.eq.0) 
  res1@trXMaxF           =  res1@trXMaxF -1 
  if(isites.eq.0) then 
  res1@tmXBLabelsOn = False
  res1@tiXAxisString         = ""; "Day in "+year_to_plot+" "+monthshort(month_to_plot-1);;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
  else 
  res1@tmXBLabelsOn = True 
  res1@tiXAxisString         = "Date in "+ monthshort(month_to_plot-1)+" "+year_to_plot;;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
  end if 
;  res1@trXMinF           =  ind(Date.eq.20110701.and.Hour.eq.0) 
;  res1@trXMaxF           =  ind(Date.eq.20110801.and.Hour.eq.0) 
;  res1@tiXAxisString         = "Day in 2011 July";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
;  res1@trXMinF           =  ind(Date.eq.20110701.and.Hour.eq.0) 
;  res1@trXMaxF           =  ind(Date.eq.20110801.and.Hour.eq.0) 
;  res1@tiXAxisString         = "Day in 2011 July";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
;  res1@trXMinF           =  ind(Date.eq.20110801.and.Hour.eq.0) 
;  res1@trXMaxF           =  ind(Date.eq.20110901.and.Hour.eq.0) 
;  res1@tiXAxisString         = "Day in 2011 Aug";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
;  res1@trXMinF           =  ind(Date.eq.20110901.and.Hour.eq.0) 
;  res1@trXMaxF           =  ind(Date.eq.20111001.and.Hour.eq.0) 
;  res1@tiXAxisString         = "Day in 2011 Sept";;"DOY"; "PM~B~2.5~N~, ~F33~m~F~g m~S~-3"      ; label bottom axis with units
  res1@trYMaxF           =  130. 
  res1@trYMinF           =  0. 
  res1@tiYAxisOffsetXF = 0.01955
  res1@tmYLLabelDeltaF = -1.098
  res1@tiXAxisOffsetYF = 0.010955
  res1@tmXBLabelDeltaF = -.5098

    plot  = gsn_csm_y (wks,Ozone,res1) ; create plot
  abc = (/"a","b","c","d","e","f","g","h","i"/)
  tres       =  True
  tres@txFontHeightF = 0.013825
  gsn_text(wks,plot,abc(isites)+")"+sites(isites), res1@trXMaxF-60, 110,tres)

  delete(res1@tmXBValues)
  delete(res1@tmXBLabels)
  draw(plot)

  yearly = False
 if(yearly) then 
  do iyear = 2000, 2012 
   
  iyear_number = iday-2000+1
 figurename = "TS_o3_"+iyear_number
  wks = gsn_open_wks("eps" ,figurename)          ; eps,pdf,x11,ncgm,eeps
  setvalues wks
;    "wkColorMap"        : "BlAqGrYeOrRevi200"
    "wkForegroundColor" : "Black";"Blue"
;    "wkBackgroundColor" : (/1.,1.,1./)
  end setvalues
    plot  = gsn_csm_xy (wks,Time_1d,Data_combine*1000,res1) ; create plot

  draw(plot)
  frame(wks)

  system("convert -trim -resize 100% "+figurename+".eps /nsftor/xhu/public_html/HurricaneImpactonO3/observations/"+figurename+".png")
  system("rm "+figurename+".eps")
  print("finish plotting "+figurename)

  end do ; iyear 
  end if ; yearly
  delete(data_array_chemistry) ; = readAsciiTable("TS_O3_"+sites(isites)+".csv.txt", 3, "double", 1)
   delete(Date) ; = data_array_chemistry(:,0) 
   delete(Hour) ; = data_array_chemistry(:,1) 
   delete(Ozone) ;= data_array_chemistry(:,2)
  delete(Month_day) ; = mod(Date, 10000)
  delete(Day) ;       = mod(Date, 100)
  delete(Year) ;      = doubletoint(Date)/10000

 end do  ; isites 
  frame(wks)
  if(wks_type.eq."png")
;  system("mkdir                           /var/www/html/micronet/ARM/Ozone/"+year_to_plot+"0"+month_to_plot+"")
  system("mv  "+figurename+"."+wks_type+" /var/www/html/micronet/ARM/Ozone/"+year_to_plot+"0"+month_to_plot+"/"+figurename+"."+wks_type+"")
  else  
;  system("convert -trim -density 300 -resize 1000x400 "+figurename+".eps /nsftor/xhu/public_html/HurricaneImpactonO3/observations/"+figurename+".png")
  print("manually mv  "+figurename+"."+wks_type+" /var/www/html/micronet/ARM/Ozone/"+year_to_plot+"0"+month_to_plot+"/"+figurename+"."+wks_type+"")
;  system("convert -trim -density 300 -resize 1000x400 "+figurename+".eps /var/www/html/micronet/ARM/Ozone/"+year_to_plot+"06/"+figurename+".png")
;  system("convert -trim -density 300 -resize 1000x400 "+figurename+".eps /var/www/html/micronet/ARM/Ozone/201107/"+figurename+".png")
;  system("convert -trim -density 300 -resize 1000x400 "+figurename+".eps /var/www/html/micronet/ARM/Ozone/201108/"+figurename+".png")
;  system("convert -trim -density 300 -resize 1000x400 "+figurename+".eps /var/www/html/micronet/ARM/Ozone/201109/"+figurename+".png")
  system("rm "+figurename+".eps")
  end if 
 end 

