# ------------------------------------------------------------------------------------# # WSSI_web_images.py # # Description: Winter Storm Severity Index (WSSI) - Image generator for webmaps # # Authors: Josh Kastman # # Contact: joshua.kastman@noaa.gov # # Established - Kastman 2019-09-26 # # ------------------------------------------------------------------------------------# import subprocess import arcpy, os from arcpy import mapping as m import logging import traceback from datetime import datetime, timedelta from dateutil import tz from_zone = tz.tzutc() to_zone = tz.tzlocal() from time import gmtime, strftime import glob import wssi_config as cfg start_time = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) print ("WSSI Web Image Scripts Start Time "+start_time) arcpy.env.overwriteOutput = True ##home_dir = os.getcwd() ##datadir = home_dir+"\\data\\grb\\" ##refdir = home_dir+"\\data\\reference\\" ##workdir = home_dir+"\\data\\working\\" ##webmaps = home_dir+"\\images\\web_map_images\\" ##static = home_dir+"\\images\\static\\" xmldir = cfg.xmls ##mxd_dir = home_dir+"\\mxds\\" ##comp_dir = home_dir+"\\images\\components_output\\" ##log_dir = home_dir+"\\data\\logs\\" key = cfg.key plink = cfg.plink pscp = cfg.pscp ### set up log file log_stamp = strftime("%Y%m%d%H", gmtime()) logging.basicConfig(filename=cfg.log_dir+'/wssi_log_'+str(log_stamp)+'_web_imgs.log',level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') #webmaps ="C://Users//joshua.kastman//gis/wsii//operational/images//web_map_images//" #static ="C://Users//joshua.kastman//gis//wsii//operational//images//static//" #xmldir = 'C://Users//joshua.kastman//gis//wsii//operational//images//xmls//' #----------------------------------------------------# # clean() - Cleans all data in the working directory # #----------------------------------------------------# def clean(**kwargs): for key, value in kwargs.items(): print (value) #logging.debug('Cleaning out old files') try: for root, dirs, files in os.walk(value): for f in files: os.unlink(os.path.join(root, f)) for d in dirs: shutil.rmtree(os.path.join(root, d)) except Exception as e: print(traceback.format_exc()) #logging.error(traceback.format_exc()) def make_geopngs(img_dir): #-------------------------------------------------------------------------------------------------# # Switching to a single mxd model for webmap production. Will be using replace data source method # #-------------------------------------------------------------------------------------------------# os.chdir(img_dir) web_list = [] #logging.debug ("Available images for Web:") for file in glob.glob("*.tif"): print file logging.debug (file) web_list.append(file) #print web_list #full_web_data = ";".join(web_list) #print full_web_data for f in web_list: print (cfg.web_imgs+"web_CONUS_"+f[:-4]+".png") mapName = cfg.mxd_dir+"WSSI_web_data_master.mxd" print mapName mxd = m.MapDocument(mapName) for f in web_list: print f arcpy.RefreshActiveView() arcpy.RefreshTOC() df = m.ListDataFrames(mxd, "WSSI_ALL_DATA_WEB")[0] layer = arcpy.mapping.ListLayers(mxd, "*", df)[0] layer.replaceDataSource(img_dir, "RASTER_WORKSPACE", f) print "active layer "+layer.name print "Layer: " + layer.name + " Source: " + layer.dataSource #print "print image" ##m.ExportToPNG(mxd, web_dir+"\\ssef_fv3_"+init+"f037.png", df_export_width=5000, df_export_height=3719, resolution=600, transparent_color="255, 255, 255") print cfg.web_imgs+f[:-4]+".png" m.ExportToPNG(mxd, cfg.web_imgs+"web_CONUS_"+f[:-4]+".png", df_export_width=5000, df_export_height=3719, resolution=600, transparent_color="255, 255, 255") del mxd #------------------------# # Generate xmls function # #------------------------# def xml_gen(): workdir = cfg.work_dir xmldir = cfg.xmls wsiiXmlLastMod='Last Updated: ' + time.strftime("%A %B %d, %Y %I:%M %p ", time.localtime()) + 'ET' out=open(xmldir+'xmlswsiiXmlLastMod.txt', 'wb') out.write(wsiiXmlLastMod) out.close() f = open(workdir+"time_vars.txt", "r") lines = f.readlines() d1_it = lines[0][:-1] d1_et = lines[1][:-1] d2_et = lines[2][:-1] d3_et = lines[3][:-1] d3t_et = lines[4][:-1] d4_et = lines[5][:-1] f.close() wssi_day1_xml = 'Winter Storm Severity Index - Effective From ' + time.strftime("%a, %b %d, %Y %I %p ", time.localtime()) + ' ET Through ' + d1_et+' ET' out=open(xmldir+'wsii_day1_xml.txt', 'wb') out.write(wssi_day1_xml) out.close() wssi_day2_xml = 'Winter Storm Severity Index - Effective From ' + d1_et + ' ET Through ' + d2_et+' ET' out=open(xmldir+'wsii_day2_xml.txt', 'wb') out.write(wssi_day2_xml) out.close() wssi_day3_xml = 'Winter Storm Severity Index - Effective From ' + d2_et + ' ET Through ' + d3_et+' ET' out=open(xmldir+'wsii_day3_xml.txt', 'wb') out.write(wssi_day3_xml) out.close() wssi_day4_xml = 'Winter Storm Severity Index - Effective From ' + d3t_et + ' ET Through ' + d4_et+' ET' out=open(xmldir+'wsii_day4_xml.txt', 'wb') out.write(wssi_day4_xml) out.close() wssi_fp_xml = 'Winter Storm Severity Index - Effective From ' + time.strftime("%a, %b %d, %Y %I %p ", time.localtime()) + ' ET Through ' + d3_et+' ET' out=open(xmldir+'wsii_fp_xml.txt', 'wb') out.write(wssi_fp_xml) out.close() wssi_4fp_xml = 'Winter Storm Severity Index - Effective From ' + time.strftime("%a, %b %d, %Y %I %p ", time.localtime()) + ' ET Through ' + d4_et+' ET' out=open(xmldir+'wsii_d4fp_xml.txt', 'wb') out.write(wssi_4fp_xml) out.close() #-------------------------------------------------# # date_time_mgt: # # Create initial and valid Times from date_string # # variable created from percentile_status.txt. # #-------------------------------------------------# ##f = open(workdir+"gis_time_vars.txt", "r") ## lines = f.readlines() ## d1_et = lines[1][:-1] ## f.close() with open(cfg.work_dir+"gis_time_vars.txt", "r") as f: lines = f.readlines() d1_et = lines[1][:-1] print d1_et #date_cyc = f.read() #print ("DateCyc: "+date_cyc) #date_string = str(date_cyc[:-2]) from datetime import datetime def date_time_mgt(): #ds = datetime.strptime(str(date_string), '%Y%m%d%H') ds = datetime.strptime(str(d1_et), '%HZ %m/%d/%y') ds_00 = ds.strftime("%HZ %m/%d/%Y") ds_pinit = ds + timedelta(hours=-24) ds = ds_pinit ds_init = ds_pinit.strftime("%HZ %m/%d/%Y") ds_p06 = ds + timedelta(hours=6) ds_06 = ds_p06.strftime("%HZ %m/%d/%Y") ds_p12 = ds + timedelta(hours=12) ds_12 = ds_p12.strftime("%HZ %m/%d/%Y") ds_p18 = ds + timedelta(hours=18) ds_18 = ds_p18.strftime("%HZ %m/%d/%Y") ds_p24 = ds + timedelta(hours=24) ds_24 = ds_p24.strftime("%HZ %m/%d/%Y") ds_p30 = ds + timedelta(hours=30) ds_30 = ds_p30.strftime("%HZ %m/%d/%Y") ds_p36 = ds + timedelta(hours=36) ds_36 = ds_p36.strftime("%HZ %m/%d/%Y") ds_p42 = ds + timedelta(hours=42) ds_42 = ds_p42.strftime("%HZ %m/%d/%Y") ds_p48 = ds + timedelta(hours=48) ds_48 = ds_p48.strftime("%HZ %m/%d/%Y") ds_p54 = ds + timedelta(hours=54) ds_54 = ds_p54.strftime("%HZ %m/%d/%Y") ds_p60 = ds + timedelta(hours=60) ds_60 = ds_p60.strftime("%HZ %m/%d/%Y") ds_p66 = ds + timedelta(hours=66) ds_66 = ds_p66.strftime("%HZ %m/%d/%Y") ds_p72 = ds + timedelta(hours=72) ds_72 = ds_p72.strftime("%HZ %m/%d/%Y") ds_p78 = ds + timedelta(hours=78) ds_78 = ds_p78.strftime("%HZ %m/%d/%Y") ds_p84 = ds + timedelta(hours=84) ds_84 = ds_p84.strftime("%HZ %m/%d/%Y") #----------------------------# # Make date txts for web # # text form : %Hz %a %b, %Y # # e.g.: 12z Fri Feb 13, 2020 # #----------------------------# # Forecast Initial: fhr00 = ds_pinit.strftime("%HZ %a %d %b, %Y") out=open(cfg.work_dir+"init.txt", 'wb') out.write(fhr00) out.close() # Forecast FHR Loop fint = [] for f in cfg.fcst_hr_list: fint.append(int(f)) for h in fint: #fcstI = ds.strftime("%HZ %a %b, %Y") fcstTD = ds + timedelta(hours=h) fcstHR = fcstTD.strftime("%HZ %a %d %b, %Y") out=open(cfg.work_dir+"fhr_"+str(h)+".txt", 'wb') out.write(fcstHR) out.close() # Forecast Last Update #update_time = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) update_time = strftime("%H:%MZ %a, %d %b %Y", gmtime()) LastUpdate='WSSI-PWPF Last Updated: '+ str(update_time) out=open(cfg.work_dir+"last_update.txt", 'wb') out.write(LastUpdate) out.close() #--------------------------------# # Export geopngs & xmls function # #--------------------------------# def export_to_web(): linux_user = "hpc" #temp block --> linux_cpu = "hpcrzdm:" linux_cpu = "vm-lnx-rzdm01:" #pwd = "Rotten_Peaches4U" web_img_dir = "//home/www//hpc//htdocs//wwd//wssi//images//" web_img_rdir = "//home/www//hpc//htdocs//wwd//wssi//images//wssi_rolling//" #target_dir2 = "//home/www//hpc//htdocs//wwd//internal//wssi//images//" geopngs = cfg.web_imgs+"\\*.png" rgeopngs = cfg.web_imgs+"\\*.png" xmls = cfg.xmls+"\\*.txt" rfhrs = cfg.work_dir+"*fhr*"+"*.txt" fint = cfg.work_dir+"init.txt" try: #print pscp+" -pw "+pwd+" "+geopngs+" "+linux_user+"@"+linux_cpu+web_img_dir print (pscp+" -i "+key+" "+geopngs+" "+linux_user+"@"+linux_cpu+web_img_dir) subprocess.call(pscp+" -i "+key+" "+geopngs+" "+linux_user+"@"+linux_cpu+web_img_dir) subprocess.call(pscp+" -i "+key+" "+xmls+" "+linux_user+"@"+linux_cpu+web_img_dir) subprocess.call(pscp+" -i "+key+" "+rfhrs+" "+linux_user+"@"+linux_cpu+web_img_rdir) subprocess.call(pscp+" -i "+key+" "+rgeopngs+" "+linux_user+"@"+linux_cpu+web_img_rdir) subprocess.call(pscp+" -i "+key+" "+fint+" "+linux_user+"@"+linux_cpu+web_img_rdir) subprocess.call('aws s3 cp O:\wssi\operational\images\web_map_images s3://s3-east-www.wpc.woc.noaa.gov/public/wwd/wssi/images/ --recursive --exclude "*" --include "*.png"') subprocess.call('aws s3 cp O:\wssi\operational\images\\xmls s3://s3-east-www.wpc.woc.noaa.gov/public/wwd/wssi/images/ --recursive --exclude "*" --include "*.txt"') #logging.debug('geopngs exported to web') except Exception as e: print(traceback.format_exc()) #logging.error(traceback.format_exc()) ##################### # Execute Functions # ##################### clean(webmaps_dir =cfg.web_imgs, static_dir =cfg.static, xml = cfg.xmls) make_geopngs(img_dir = cfg.comp_out) make_geopngs(img_dir = cfg.roll_out) xml_gen() date_time_mgt() export_to_web() finish_time = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) print ("WSSI Web Image Scripts Start Time "+start_time) print ("WSSI Web Image Scripts Finished "+finish_time) logging.debug ("WSSI Web Image Scripts Start Time "+start_time) logging.debug ("WSSI Web Image Script Finished "+finish_time) logging.shutdown()