def gen_report_new(uid,field_address,field_lat,field_long,imagedate,fieldid,field_area,lang): import pdfkit from create_bargraph import create_bargraph from create_linegraph import create_linegraph import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import datetime from get_weather_data import get_weather_data from datetime import date from create_temperature_bargraph import create_temperature_bargraph from create_weather_line_graph import create_weather_line_graph import cv2 from PIL import Image from create_index_piechart import create_index_piechart from get_weather_forecast import get_weather_forecast from column_img import column_img import csv import os.path from upload_file import upload_file import os from pyvirtualdisplay import Display #from docx_to_pdf import docx_to_pdf # Fetch the service account key JSON file contents cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') lang_num = 0 all_strings = [] row_num = 0 with open('report_strings.csv') as csv_file: csv_reader = csv.reader(csv_file,delimiter=',') strings_column = [] for row in csv_reader: if row_num == 0: row_num = row_num + 1 available_langs = row for singleLang in available_langs: print(('aa'+singleLang+'aa')) print(('bb'+lang+'bb')) if lang.find(singleLang)>-1: print('lang break') break else: lang_num = lang_num+1 all_strings.append(row) print('lang num') print(lang) if lang == 'ar': lang_num = 2 else: lang_num = 0 print(lang_num) today = date.today() date_d = today.strftime('%Y%m%d') year = date_d[0:4] month = date_d[4:6] day = date_d[6:] report_date = year+'-'+month+'-'+day+' (in yyyy-mm-dd)' pageno = 1 sensed_year = imagedate[0:4] sensed_month = imagedate[4:6] sensed_day = imagedate[6:] sensed_imagedate = sensed_year+'-'+sensed_month+'-'+sensed_day+' (in yyyy-mm-dd)' sensed_day = int(sensed_day) sensed_month = int(sensed_month) print('report_date') print(report_date) print(sensed_imagedate) tci_image = uid+'/TCI.png' etci_image = uid+'/ETCI.png' ndvi_image = uid+'/ndvi.png' evi_image = uid+'/evi.png' savi_image = uid+'/savi.png' ndwi_image = uid+'/ndwi.png' ndmi_image = uid+'/ndmi.png' ndre_image = uid+'/ndre.png' hybrid_image = uid+'/hybrid.png' hybrid_blind_image = uid+'/hybrid_blind.png' soc_image = uid+'/soc.png' evapo_image = uid+'/evapo.png' ndvi_image_cmap2 = uid+'/ndvi_cmap2.png' evi_image_cmap2 = uid+'/evi_cmap2.png' ndwi_image_cmap2 = uid+'/ndwi_cmap2.png' ndre_image_cmap2 = uid+'/ndre_cmap2.png' savi_image_cmap2 = uid+'/savi_cmap2.png' soc_image_cmap2 = uid+'/soc_cmap2.png' savi_image_cmap2 = uid+'/savi_cmap2.png' ndmi_image_cmap2 = uid+'/ndmi_cmap2.png' # Initialize the app with a service account, granting admin privileges whitelabel = "nothing" health = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').get() try: whitelabel = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Whitelabel').get() except: whitelabel = "nothing" whitelabel = str(whitelabel) ndvi = try_index(health, "ndvi") evi = try_index(health, "evi") ndwi = try_index(health, "ndwi") ndre = try_index(health, "ndre") ndmi = try_index(health, "ndmi") savi = try_index(health, "savi") soc = try_index(health, "soc") #if lang == 'ar': # alignmentType = WD_ALIGN_PARAGRAPH.RIGHT #else: # alignmentType = WD_ALIGN_PARAGRAPH.LEFT header_img_url = 'APIOrganizations/'+uid + '.jpeg' if os.path.isfile(header_img_url): header_img_url = 'https://farmonaut.com/Images/'+uid + '.jpeg' else: header_img_url = 'APIOrganizations/'+whitelabel + '.jpg' if os.path.isfile(header_img_url): header_img_url = 'https://farmonaut.com/Images/'+whitelabel + '.jpg' else: header_img_url ='https://farmonaut.com/Images/Farmonaut_Logo_1.jpg' f_lat = str(field_lat) f_lat = f_lat[0:6] f_long = str(field_long) f_long = f_long[0:6] cur_date = imagedate cur_weather_data = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).get() try: min_temp = cur_weather_data["min_temp"] except: min_temp = None #min_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('min_temp').get() if min_temp is None: print('Weather data not available') else: min_temp = float(min_temp)-273 min_temp = str(min_temp) min_temp = min_temp[0:5] max_temp = cur_weather_data["max_temp"] #max_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('max_temp').get() max_temp = float(max_temp)-273 max_temp = str(max_temp) max_temp = max_temp[0:5] humidity = cur_weather_data["humidity"] #humidity = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('humidity').get() pressure = cur_weather_data["pressure"] #pressure = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('pressure').get() station = cur_weather_data["station"] #station = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('station').get() wind_speed = cur_weather_data["wind_speed"] #wind_speed = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_speed').get() try: wind_deg = cur_weather_data["wind_deg"] except: wind_deg = 'Not Available' #wind_deg = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_deg').get() try: cloud_cover = cur_weather_data["cloud_cover"] except: cloud_cover = 'Not Available' weather_data = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').get() if weather_data is None: print('weather data not available yet') else: cloud_array = [] humidity_array = [] max_temp_array = [] min_temp_array = [] pressure_array = [] wind_deg_array = [] wind_speed_array = [] dates_array = [] for (key,val) in weather_data.items(): weather_date = key date_data = weather_data[weather_date] dates_array.append(weather_date) dates_array.sort() for key in dates_array: weather_date = key date_data = weather_data[weather_date] try: cloud_array.append(date_data["cloud_cover"]) except: cloud_array.append('not availabe') try: humidity_array.append(date_data["humidity"]) except: humidity_array.append('not available') try: t_max_temp = date_data["max_temp"]-273 except: t_max_temp = 'not available' try: t_min_temp = date_data["min_temp"]-273 except: t_min_temp = 'not available' max_temp_array.append(t_max_temp) min_temp_array.append(t_min_temp) try: pressure_array.append(date_data["pressure"]) except: pressure_array.append('not available') try: wind_deg_array.append(date_data["wind_deg"]) except: wind_deg_array.append('not available') try: wind_speed_array.append(date_data["wind_speed"]) except: wind_speed_array.append('not available') if len(dates_array)>1: try: line_types = ['min_temp','max_temp','cloud_cover','humidity','pressure','wind_speed'] weather_array = [min_temp_array,max_temp_array,cloud_array,humidity_array,pressure_array,wind_speed_array] create_weather_line_graph(uid,line_types,weather_array,dates_array) past_weather_url = upload_file(uid, fieldid, imagedate,(uid+'_weather_linegraph.png'),'past_weather.png') except: wwq2 = 1 if len(ndvi)>1: create_linegraph(uid,'ndvi',ndvi) create_linegraph(uid,'evi',evi) create_linegraph(uid,'savi',savi) create_linegraph(uid,'ndre',ndre) create_linegraph(uid,'ndwi',ndwi) create_linegraph(uid,'ndmi',ndmi) create_linegraph(uid,'soc',soc) hybrid_url = upload_file(uid, fieldid,imagedate,(uid + '/hybrid.png'), 'hybrid') hybrid_scale_url = 'https://farmonaut.com/Images/hybrid_scale.jpg' hybrid_blind_url = upload_file(uid, fieldid, imagedate, (uid + '/hybrid_blind.png'), 'hybrid_blind') hybri_blind_scale_url = 'https://farmonaut.com/Images/hybrid_blind_scale.jpg' ndvi_url = upload_file(uid, fieldid, imagedate, (uid + '/ndvi.png'), 'ndvi') ndvi_scale_url = 'https://farmonaut.com/Images/ndvi_scale.jpg' ndvi2_url = upload_file(uid, fieldid, imagedate, (uid + '/ndvi_cmap2.png'), 'ndvi_cmap2') ndvi_line_url = upload_file(uid, fieldid, imagedate, (uid + '/ndvi_linegraph.png'), 'ndvi_linegraph') ndvi_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/ndvi_piechart.png'), 'ndvi_piechart') evi_url = upload_file(uid, fieldid, imagedate, (uid + '/evi.png'), 'evi') evi_scale_url = 'https://farmonaut.com/Images/evi_scale.jpg' evi2_url = upload_file(uid, fieldid, imagedate, (uid + '/evi_cmap2.png'), 'evi_cmap2') evi_line_url = upload_file(uid, fieldid, imagedate, (uid + '/evi_linegraph.png'), 'evi_linegraph') evi_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/evi_piechart.png'), 'evi_piechart') savi_url = upload_file(uid, fieldid, imagedate, (uid + '/savi.png'), 'savi') savi_scale_url = 'https://farmonaut.com/Images/savi_scale.jpg' savi2_url = upload_file(uid, fieldid, imagedate, (uid + '/savi_cmap2.png'), 'savi_cmap2') savi_line_url = upload_file(uid, fieldid, imagedate, (uid + '/savi_linegraph.png'), 'savi_linegraph') savi_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/savi_piechart.png'), 'savi_piechart') ndre_url = upload_file(uid, fieldid, imagedate, (uid + '/ndre.png'), 'ndre') ndre_scale_url = 'https://farmonaut.com/Images/ndre_scale.jpg' ndre2_url = upload_file(uid, fieldid, imagedate, (uid + '/ndre_cmap2.png'), 'ndre_cmap2') ndre_line_url = upload_file(uid, fieldid, imagedate, (uid + '/ndre_linegraph.png'), 'ndre_linegraph') ndre_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/ndre_piechart.png'), 'ndre_piechart') ndwi_url = upload_file(uid, fieldid, imagedate, (uid + '/ndwi.png'), 'ndwi') ndwi_scale_url = 'https://farmonaut.com/Images/ndwi_scale.jpg' ndwi2_url = upload_file(uid, fieldid, imagedate, (uid + '/ndwi_cmap2.png'), 'ndwi_cmap2') ndwi_line_url = upload_file(uid, fieldid, imagedate, (uid + '/ndwi_linegraph.png'), 'ndwi_linegraph') ndwi_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/ndwi_piechart.png'), 'ndwi_piechart') ndmi_url = upload_file(uid, fieldid, imagedate, (uid + '/ndmi.png'), 'ndmi') ndmi_scale_url = 'https://farmonaut.com/Images/ndmi_scale.jpg' ndmi2_url = upload_file(uid, fieldid, imagedate, (uid + '/ndmi_cmap2.png'), 'ndmi_cmap2') ndmi_line_url = upload_file(uid, fieldid, imagedate, (uid + '/ndmi_linegraph.png'), 'ndmi_linegraph') ndmi_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/ndmi_piechart.png'), 'ndmi_piechart') soc_url = upload_file(uid, fieldid, imagedate, (uid + '/soc.png'), 'soc') soc_scale_url = 'https://farmonaut.com/Images/soc_scale.jpg' soc2_url = upload_file(uid, fieldid, imagedate, (uid + '/soc_cmap2.png'), 'soc_cmap2') soc_line_url = upload_file(uid, fieldid, imagedate, (uid + '/soc_linegraph.png'), 'soc_linegraph') soc_pie_url = upload_file(uid, fieldid, imagedate, (uid + '/soc_piechart.png'), 'soc_piechart') evapo_url = upload_file(uid, fieldid, imagedate, (uid + '/evapo.png'), 'evapo') evapo_scale_url = 'https://farmonaut.com/Images/evapo_scale.jpg' tci_url = upload_file(uid, fieldid, imagedate, (uid + '/TCI.png'), 'tci') etci_url = upload_file(uid, fieldid, imagedate, (uid + '/ETCI.png'), 'etci') all_weather_data = get_weather_forecast(field_lat,field_long) all_days = [] min_temps = [] max_temps = [] rain_probs = [] max_preps = [] cloud_covers = [] final_days = [] weather_infos = [] for (key,val) in all_weather_data.items(): all_days.append(key) all_days.sort() all_days = all_days[1:] for key in all_days: #print('kk') #print(key) single_w_data = all_weather_data[key] w_date = datetime.datetime.fromtimestamp(int(key)) final_days.append(str(w_date)[0:10]) weather_infos.append(single_w_data["summary"]) cloud_covers.append(str(int(float(single_w_data["cloudcover"])*100))) try: temp_min_temp = (str(round(((float(single_w_data["templow"])-32)*(5/9)),2))) temp_min_temp = temp_min_temp + ((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["templowtime"])))[11:16])) min_temps.append(temp_min_temp) temp_max_temp = (str(round(((float(single_w_data["temphigh"])-32)*(5/9)),2))) temp_max_temp = temp_max_temp +((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["temphightime"])))[11:16])) max_temps.append(temp_max_temp) except: min_temps.append('not available') max_temps.append('not available') try: rain_probs.append(str(round((float(single_w_data["preciprob"])*100),2))) temp_prep = (str((single_w_data["precimax"]))) temp_prep = temp_prep + ((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["precimaxtime"])))[11:16])) max_preps.append(temp_prep) except: rain_probs.append('not available') max_preps.append('not available') #document.add_page_break() present_ndvi = float(ndvi[imagedate])/100 present_ndwi = float(ndwi[imagedate])/100 present_ndre = float(ndre[imagedate])/100 present_savi = float(savi[imagedate])/100 present_evi = float(evi[imagedate])/100 try: present_ndmi = float(ndmi[imagedate])/100 except: present_ndmi = float(0) present_soc = float(soc[imagedate])/100 html_message = """""" html_message = html_message+ """ """ with Display(): pdfkit.from_string(html_message,(uid + '/report.pdf')) htmlfile = open((uid + '/report.html'),"w") htmlfile.write(html_message) htmlfile.close() def try_index(health, index): new_index = {} try: new_index = health[index] except: new_index = {} return new_index