#####################################################################################
#Run this script first to run mode and create needed output files for other scripts.
#
#-Run runMODE.py first to create the netCDF files (modify mode_config for MODE sensitivity studies)
#-Run createCSV.py to create a csv of paired objects
#-Run createCSV_simpleobj.py to create a csv of simple objects
#-Run plotting.py to create non-grid plots for paired objects
#-Run plotting_grid.py to create grid plots for paired objects
#-Run plotting_grid_simpleobj.py to create grid plots for simple objects (needs work)
#
#Created in the summer/fall of 2020 by CLC, editted by MJE on 20210601.
#Additional modifications by AJ throughout summer of 2021.
#Further documented by MJE. 20210921.
#
######################################################################################

#!/usr/bin/python
import os
#from netCDF4 import Dataset
import numpy as np
import datetime
import csv
import math
import haversine
import glob
import pygrib

#######################LIST OF VARIABLES########################################################
MET_PATH     = '/opt/MET/METPlus4_0/bin/'                                   #Directory of MET bins
#DATA_PATH    = '/export/hpc-lw-dtbdev5/wpc_cpgffh/gribs/ERO_verif/'         #If working on hpc-lw-dtbdev5
DATA_PATH    = '/export/hpc-lw-dtbdev5/merickson/code/python/lapenta/data/' #If not working on hpc-lw-dtbdev5 (must copy data over)
working_dir  = '/export/hpc-lw-dtbdev5/merickson/code/python/lapenta/work/' #Working directory for saving data
validday     = 1                                                            #Valid day 
ero_category = 'SLGT'                                                       #ERO category to be considered
beg_date     = datetime.datetime(2016,6,1,12,0,0)                           #Start date       
end_date     = datetime.datetime(2016,6,15,12,0,0)                           #End date
################################################################################################

os.environ["LD_LIBRARY_PATH"] ='/opt/MET/METPlus4_0/external_libs/lib'
os.environ["LIBS"] = '/opt/MET/METPlus4_0/external_libs'

#Define the output file
latlon = working_dir+'latlon_vday'+str(validday)+'_ERO'+ero_category+'.csv'

mode_stuff_dir = working_dir+'MODEobjects/validday'+str(validday)+'_'+ero_category+'/'

#Set proper directories and files
DATA_PATH_EX = DATA_PATH+'ERO_verif_day'+str(validday)+'_ALL_noMRGL//'
config_file  = working_dir+'mode_config_'+str(ero_category)

#Remove any old files in mode_stuff_dir
os.system('rm -rf '+mode_stuff_dir+'mode*')

#Make necessary directories
try:
	os.mkdir(working_dir)
except:
	pass
try:
	os.mkdir(working_dir+'MODEobjects')
except:
	pass
try:
        os.mkdir(working_dir+'figures')
except:
        pass
try:
        os.mkdir(mode_stuff_dir)
except:
        pass

#Convert datetime to julian dates
beg_date_jul = pygrib.datetime_to_julian(beg_date)
end_date_jul = pygrib.datetime_to_julian(end_date)

for dates in range(int(round(beg_date_jul)),int(round(end_date_jul))+1): #through the dates

	print(dates)
	#Create datetime element for day being loaded
	curdate      = pygrib.julian_to_datetime(dates)
	tomdate      = pygrib.julian_to_datetime(dates+1)
	yrmonday_cur = '{:04d}'.format(curdate.year)+'{:02d}'.format(curdate.month)+'{:02d}'.format(curdate.day)
	yrmonday_tom = '{:04d}'.format(tomdate.year)+'{:02d}'.format(tomdate.month)+'{:02d}'.format(tomdate.day)

	print(DATA_PATH_EX+'grid_stat_PP_ALL_ERO_s'+yrmonday_cur+'*.nc.gz')	
	for filename in glob.glob(DATA_PATH_EX+'grid_stat_PP_ALL_ERO_s'+yrmonday_cur+'*.nc.gz'):
		if ('vhr09' in filename): #Only grab 09 UTC for validday === 1
	
			#Define hour string	
			valid_hr = filename[124:126]
	 
		        #Unzip file, run mode, and rezip file
			os.system('gunzip '+filename)
			os.system(MET_PATH+'/mode '+filename[0:-3]+' '+filename[0:-3]+' '+config_file+' -outdir '+mode_stuff_dir)
			#os.system('scp '+mode_stuff_dir+'mode_240000L_'+yrmonday_tom+'_120000V_240000A.ps hpc@vm-lnx-rzdm01:/home/people/hpc/ftp/erickson/lapenta')
  	  	       #os.system('scp /export/hpc-lw-dtbdev5/merickson/code/python/lapenta/work/MODEobjects/mode_240000L_20200804_120000V_240000A.ps hpc@vm-lnx-rzdm01:/home/people/hpc/ftp/erickson/lapenta')
			os.system('gzip '+filename[0:-3])


###############EXTRA CODE########################################################
##This is an extra snippit of code that can be used to read in a netCDF file
#from netCDF4 import Dataset
#f        = Dataset(working_dir+'MODEobjects/mode_240000L_'+yrmonday_tom+'_120000V_240000A_obj.nc', "a", format="NETCDF4")
#temp     = f.variables['fcst_obj_id'][:]
#print(np.unique(temp))
#temp     = f.variables['fcst_clus_id'][:]
#print(np.unique(temp))
#f.close()

##This is an extra snippit that pushes the postscript to the web for viewing at: https://ftp.wpc.ncep.noaa.gov/erickson/lapenta/
#os.system('scp '+working_dir+'MODEobjects/mode_240000L_'+yrmonday_tom+'_120000V_240000A.ps hpc@vm-lnx-rzdm01:/home/people/hpc/ftp/erickson/lapenta')

##This is an extra snippit to calculate a rose plot for a sample and port it to the web for viewing
#from windrose import WindroseAxes
#import matplotlib.pyplot as plt
#ax = WindroseAxes.from_ax()
#ax.bar(np.array([angle]), np.array([magnitude]), bins=[0,10,20,50,75,100,125,150,200,400,500],blowto=True)
#ax.set_legend()
#plt.savefig(working_dir+'figures/test.png')
#plt.close()
#os.system('scp '+working_dir+'figures/test.png hpc@vm-lnx-rzdm01:/home/people/hpc/ftp/erickson/lapenta')
