def gen_failed_report(uid,field_address,field_lat,field_long,imagedate,fieldid,field_area): from docx import Document from docx.shared import Inches 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 docx.shared import Pt from create_temperature_bargraph import create_temperature_bargraph from create_weather_line_graph import create_weather_line_graph from get_weather_forecast import get_weather_forecast #from docx_to_pdf import docx_to_pdf # Fetch the service account key JSON file contents cred = credentials.Certificate('servicekey.json') #firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) 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)' 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) tci_image = uid+'/TCI.png' etci_image = uid+'/ETCI.png' ndvi_image = uid+'/ndvi.png' mndvi_image = uid+'/mndvi.png' ndwi_image = uid+'/ndwi.png' mndwi_image = uid+'/mndwi.png' ndre_image = uid+'/ndre.png' mndre_image = uid+'/mndre.png' vari_image = uid+'/vari.png' mvari_image= uid+'/mvari.png' # Initialize the app with a service account, granting admin privileges document = Document() header_img = document.add_picture('doc_header.png',width=Inches(6.0)) document.add_heading('Field Report',0) section = document.sections[0] footer = section.footer para = footer.paragraphs[0] para.text = "Address:Evoma Business Center, Office No: 310-A, #88 Borewell Road, Opposite Whitefield Post Office, Whitefield, Bengaluru, Karnataka, Pin: 560066" para.style = document.styles["Footer"] document.add_paragraph('This report consists of satellite results of one of your fields. Automated Satellite Monitoring Service allows you to monitor multiple farming fields using latest satellite imagery.') document.add_heading('Important',level=1) document.add_paragraph('There was too much cloud cover over your field for this satellite imagery capture date, hence we could not generate results and processed images for your field for this date. This satellite imagery date will not be counted in your subscription package and your package will be increased by one more satellite visit') p = document.add_paragraph('') p.add_run('Note: ').bold = True p.add_run('This report is auto-generated by Farmonaut systems. If you have any queries/questions regarding this report then contact us on: support@farmonaut.com').italic = True p = document.add_paragraph('') p.add_run('Report Generation Date: ').bold = True p.add_run(report_date).italic = True document.add_heading('Field Details',level=1) p = document.add_paragraph('') p.add_run('Field Address: ').bold = True p.add_run(field_address).italic = True p = document.add_paragraph('') p.add_run('Field Area: ').bold = True p.add_run(str(field_area)).italic = True p.add_run(' square meters (approx.)').italic = True p = document.add_paragraph('') f_lat = str(field_lat) f_lat = f_lat[0:6] f_long = str(field_long) f_long = f_long[0:6] p.add_run('Field Location: ').bold = True p.add_run('Latitude: ').italic = True p.add_run(f_lat).italic = True p.add_run(', Longitude: ').italic = True p.add_run(f_long).italic = True p = document.add_paragraph('') p.add_run('Satellite Imagery Capture Date: ').bold = True p.add_run(sensed_imagedate).italic = True cur_date = imagedate 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') p = document.add_paragraph('') p.add_run('Weather data will be available from next report').bold = True else: document.add_page_break() min_temp = float(min_temp)-273 min_temp = str(min_temp) min_temp = min_temp[0:5] 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 = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('humidity').get() pressure = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('pressure').get() station = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('station').get() wind_speed = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_speed').get() wind_deg = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_deg').get() cloud_cover = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('cloud_cover').get() document.add_heading('Weather Statistics on the Imagery Capture Date ',level=1) print(pressure) p = document.add_paragraph('') p.add_run('Weather Station: ').bold = True p.add_run(station).italic = True p = document.add_paragraph('') p.add_run('Minimum Temperature (°C): ').bold = True p.add_run(min_temp).italic = True p = document.add_paragraph('') p.add_run('Maximum Temperature (°C): ').bold = True p.add_run(max_temp).italic = True p = document.add_paragraph('') p.add_run('Average Pressure (hPa): ').bold = True p.add_run(str(pressure)).italic = True p = document.add_paragraph('') p.add_run('Average Humidity (in %): ').bold = True p.add_run(str(humidity)).italic = True p = document.add_paragraph('') p.add_run('Average Speed (in m/s): ').bold = True p.add_run(str(wind_speed)).italic = True p = document.add_paragraph('') p.add_run('Wind Direction (in degrees): ').bold = True p.add_run(str(wind_deg)).italic = True p = document.add_paragraph('') p.add_run('Average Cloud Cover (in %): ').bold = True p.add_run(str(cloud_cover)).italic = True document.add_page_break() 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() new_dates = [] for key in dates_array: weather_date = key date_data = weather_data[weather_date] try: cloud_array.append(date_data["cloud_cover"]) humidity_array.append(date_data["humidity"]) t_max_temp = date_data["max_temp"]-273 t_min_temp = date_data["min_temp"]-273 max_temp_array.append(t_max_temp) min_temp_array.append(t_min_temp) pressure_array.append(date_data["pressure"]) wind_deg_array.append(date_data["wind_deg"]) wind_speed_array.append(date_data["wind_speed"]) new_dates.append[key] except: print('skip this date') dates_array = new_dates if len(dates_array)>1: create_weather_line_graph(uid,'cloud_cover',cloud_array,dates_array) create_weather_line_graph(uid,'humidity',humidity_array,dates_array) create_weather_line_graph(uid,'pressure',pressure_array,dates_array) create_weather_line_graph(uid,'wind_speed',wind_speed_array,dates_array) create_weather_line_graph(uid,'wind_deg',wind_deg_array,dates_array) create_temperature_bargraph(uid,min_temp_array,max_temp_array,dates_array) document.add_heading('Weather Graphs',level=1) document.add_paragraph('') document.add_heading('Temperature Vs Time',level=2) file_name = uid+'_temp_bargraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_paragraph('') document.add_heading('Cloud Cover Vs Time',level=2) file_name = uid+'_cloud_cover_linegraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_page_break() document.add_paragraph('') document.add_heading('Humidity Vs Time',level=2) file_name = uid+'_humidity_linegraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_paragraph('') document.add_heading('Pressure Vs Time',level=2) file_name = uid+'_pressure_linegraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_page_break() document.add_paragraph('') document.add_heading('Wind Speed Vs Time',level=2) file_name = uid+'_wind_speed_linegraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_paragraph('') document.add_heading('Wind Direction Vs Time',level=2) file_name = uid+'_wind_deg_linegraph.png' document.add_picture(file_name,width=Inches(4.0)) document.add_page_break() document.add_heading('Weather Forecast (Next 7 Days)') all_weather_data = get_weather_forecast(field_lat,field_long) all_days = [] for (key,val) in all_weather_data.items(): all_days.append(key) all_days.sort() for key in all_days: #print('kk') #print(key) single_w_data = all_weather_data[key] w_date = datetime.datetime.fromtimestamp(int(key)) document.add_heading(str(w_date)[0:10],level=2) p = document.add_paragraph('') try: p.add_run(single_w_data["summary"]).bold = True except: wqwq=1 try: p.add_run(' Cloud Cover (in%): ').italic = True p.add_run(str(int(float(single_w_data["cloudcover"])*100))).italic = True p = document.add_paragraph('') except: wqwq=1 try: p.add_run('Min. Temp (°C): ').bold = True p.add_run(str(round(((float(single_w_data["templow"])-32)*(5/9)),2))).italic = True p.add_run((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["templowtime"])))[11:16])).italic = True p.add_run(', Max. Temp (°C): ').bold = True p.add_run(str(round(((float(single_w_data["temphigh"])-32)*(5/9)),2))).italic = True p.add_run((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["temphightime"])))[11:16])).italic = True p = document.add_paragraph('') except: wqwq=1 try: p.add_run('Rain Probability in(%): ').bold = True p.add_run(str(round((float(single_w_data["preciprob"])*100),2))).italic = True p.add_run(', Max. Precipitation (in mm. per hour): ').bold = True p.add_run(str((single_w_data["precimax"]))).italic = True p.add_run((' at '+str(datetime.datetime.fromtimestamp(int(single_w_data["precimaxtime"])))[11:16]+'\n')).italic = True except: wqwq=1 document.add_page_break() document.add_heading('Field Region as Visible On Google Map ',level=1) document.add_paragraph('The two images below are the images of your field region as visible on Google Maps. These images are not the latest images captured by the satellite.') document.add_heading('Field and nearby region ',level=2) gmap_static_large = uid+'/'+fieldid+'_static_map_large.png' p = document.add_picture(gmap_static_large,width=Inches(3)) document.add_heading('Field in Focus ',level=2) gmap_static = uid+'/'+fieldid+'_static_map.png' document.add_picture(gmap_static,width=Inches(3)) document.add_paragraph('These two images can be used with the latest satellite images provided by Farmonaut.') document.add_page_break() document.add_heading('Satellite Captured Images',level=0) document.add_heading('True Color Image (TCI) and Enhanced True Color Image (ETCI)',level=2) document.add_paragraph('\nTrue color image is the unaltered raw satellite image retrieved for your area, whereas Enhanced True Color Image is the Farmonaut processed satellite image of your area with enhanced land features. Using these two imagees you can see any observable land changes around your field which may be crucial for your farming practices.') document.add_picture(tci_image,width=Inches(5)) document.add_paragraph('True Color Image (TCI)') document.add_picture(etci_image,width=Inches(5)) document.add_paragraph('Enhanced True Color Image (ETCI)') file_name = uid+'/report.docx' file_name_pdf = uid+'/report.pdf' document.save(file_name) #docx_to_pdf(file_name,file_name_pdf)