def gen_report_sample(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 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 health = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').get() ndvi = health["ndvi"] ndwi = health["ndwi"] ndre = health["ndre"] vari = health["vari"] 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 your to monitor multiple farming fields using latest satellite imagery.') 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: min_temp = float(min_temp)-273 min_temp = str(min_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) 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) 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(pressure).italic = True p = document.add_paragraph('') p.add_run('Average Humidity (in %): ').bold = True p.add_run(humidity).italic = True p = document.add_paragraph('') p.add_run('Average Speed (in m/s): ').bold = True p.add_run(wind_speed).italic = True p = document.add_paragraph('') p.add_run('Wind Direction (in degrees): ').bold = True p.add_run(wind_deg).italic = True p = document.add_paragraph('') p.add_run('Average Cloud Cover (in %): ').bold = True p.add_run(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() for key in dates_array: weather_date = key date_data = weather_data[weather_date] 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"]) 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('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('Index Data for the Satellite Captured Date',level=1) cur_ndvi = float(ndvi[imagedate])/100 cur_ndwi = float(ndwi[imagedate])/100 cur_ndre = float(ndre[imagedate])/100 cur_vari = float(vari[imagedate])/100 current_index_records = ((cur_ndvi,cur_ndwi,cur_ndre,cur_vari)) table = document.add_table(rows=1,cols=4) table.style = 'TableGrid' hdr_cells = table.rows[0].cells hdr_cells[0].text = 'NDVI' hdr_cells[1].text = 'NDWI' hdr_cells[2].text = 'NDRE' hdr_cells[3].text = 'VARI' row_cells = table.add_row().cells row_cells[0].text = str(cur_ndvi) row_cells[1].text = str(cur_ndwi) row_cells[2].text = str(cur_ndre) row_cells[3].text = str(cur_vari) document.add_heading('Bar Graph of the Index Values for the Satellite Capture Date',level=1) create_bargraph(uid,cur_ndvi,cur_ndwi,cur_ndre,cur_vari,imagedate) file_name=uid+'_bargraph.png' document.add_picture(file_name,width=Inches(6.0)) 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)') document.add_page_break() document.add_heading('Normalized Difference Vegetation Index Image (NDVI) and Masked Normalized Difference Vegetation Index Image (MNDVI)',level=2) document.add_paragraph('\nNDVI image provides you a color map of the vegetation of your farming field and nearby areas. The areas shown in red are the regions where the crop growth may not be normal. Masked NDVI image puts the layer of NDVI image on the ETCI Image of the area to better identify the regionss where the crop growth may not be normal. You should refer to these images when your crop is in the early stage of growth.') document.add_picture(ndvi_image,width=Inches(4)) document.add_paragraph('Normalized Difference Vegetation Index Image (NDVI)') document.add_picture(mndvi_image,width=Inches(4)) document.add_paragraph('Masked Normalized Difference Vegetation Index Image (MNDVI)') document.add_paragraph('\nPlease check this article to read more about NDVI:\n https://farmonaut.com/blogs/remote-sensing/normalized-difference-vegetation-index-ndvi/\n') document.add_heading('Table of NDVI value change with time',level=1) ndvi_records = ndvi dates = {} ndvi_columns = len(ndvi_records)+1 table = document.add_table(rows=1,cols=ndvi_columns) table.style = 'TableGrid' hdr_cells = table.rows[0].cells i =0 for (key,val) in ndvi_records.items(): if i==0: hdr_cells[i].text = 'Date' hdr_cells[i].bold = True i = i+1 y = key[0:4] m = key[4:6] d = key[6:] kk = y+'-'+m+'-'+d hdr_cells[i].text = kk hdr_cells[i].bold = True i = i+1 row_cells = table.add_row().cells i = 0 for (key,val) in ndvi_records.items(): if i==0: row_cells[i].text = 'NDVI Value' i=i+1 val = float(val)/100 row_cells[i].text = str(val) row_cells[i].bold = True i=i+1 if len(ndvi_records)>1: document.add_heading('Graph of NDVI vs Time',level=2) create_linegraph(uid,'ndvi',ndvi) file_name = uid+'_ndvi_linegraph.png' document.add_picture(file_name,width=Inches(6.0)) document.add_page_break() document.add_heading('Normalized Difference Red Edge Image (NDRE) and Masked Normalized Difference Red Edge Image (MNDRE)',level=2) document.add_paragraph('\nNDRE image provides you a color map of the vegetation of your farming field and nearby areas. The areas shown in red are the regions where the crop growth may not be normal. Masked NDVI image puts the layer of NDVI image on the ETCI Image of the area to better identify the regionss where the crop growth may not be normal. You should refer to these images when your crop is in the later stage of growth.') document.add_picture(ndre_image,width=Inches(4)) document.add_paragraph('Normalized Difference Red Edge Image (NDRE)') document.add_picture(mndre_image,width=Inches(4)) document.add_paragraph('Masked Normalized Difference Red Edge Image (MNDRE)') document.add_paragraph('') document.add_paragraph('\nPlease check this article to understand the difference between NDVI and NDRE images and their applications in agriculture: https://farmonaut.com/blogs/remote-sensing/ndvi-vs-ndre-and-their-applications-in-agriculture/\n') document.add_heading('Table of NDRE values with Time',level=1) ndre_records = ndre ndre_columns = len(ndre_records)+1 table = document.add_table(rows=1,cols=ndre_columns) table.style = 'TableGrid' hdr_cells = table.rows[0].cells i =0 for (key,val) in ndre_records.items(): if i==0: hdr_cells[i].text = 'Date' hdr_cells[i].bold = True i = i+1 y = key[0:4] m = key[4:6] d = key[6:] kk = y+'-'+m+'-'+d hdr_cells[i].text = kk hdr_cells[i].bold = True i = i+1 row_cells = table.add_row().cells i = 0 for (key,val) in ndre_records.items(): if i==0: row_cells[i].text = 'NDRE Value' i=i+1 val = float(val)/100 row_cells[i].text = str(val) row_cells[i].bold = True i=i+1 if len(ndre_records)>1: document.add_heading('Graph of NDRE vs Time',level=2) create_linegraph(uid,'ndre',ndre) file_name = uid+'_ndre_linegraph.png' document.add_picture(file_name,width=Inches(6.0)) document.add_page_break() document.add_heading('Normalized Difference Water Index Image (NDWI) and Masked Normalized Difference RWater Index Image (MNDWI)',level=2) document.add_paragraph('\nNDWI image provides you a color map of the water level of your farming field and nearby areas. The areas shown in red are the regions where the water level may not be normal. In case of drought or less rainfall, these areas will be the most affected. Masked NDVI image puts the layer of NDVI image on the ETCI Image of the area to better identify the regionss where the crop growth may not be normal. You should refer to these images when your crop is in the later stage of growth.') document.add_picture(ndwi_image,width=Inches(4)) document.add_paragraph('Normalized Difference Water Index Image (NDWI') document.add_picture(mndwi_image,width=Inches(4)) document.add_paragraph('Masked Normalized Difference Water Index Image (MNDWI)') document.add_paragraph('') document.add_paragraph('\nPlease check this article to read more about NDWI: https://farmonaut.com/blogs/remote-sensing/normalized-difference-water-index-ndwi/\n') document.add_paragraph('') document.add_heading('Table of NDWI values with time') ndwi_records = ndwi ndwi_columns = len(ndwi_records)+1 table = document.add_table(rows=1,cols=ndwi_columns) table.style = 'TableGrid' hdr_cells = table.rows[0].cells i =0 for (key,val) in ndwi_records.items(): if i==0: hdr_cells[i].text = 'Date' hdr_cells[i].bold = True i = i+1 y = key[0:4] m = key[4:6] d = key[6:] kk = y+'-'+m+'-'+d hdr_cells[i].text = kk hdr_cells[i].bold = True i = i+1 row_cells = table.add_row().cells i = 0 for (key,val) in ndwi_records.items(): if i==0: row_cells[i].text = 'NDWI Value' i=i+1 val = float(val)/100 row_cells[i].text = str(val) row_cells[i].bold = True i=i+1 if len(ndwi_records)>1: document.add_heading('Graph of NDWI vs Time',level=2) create_linegraph(uid,'ndwi',ndwi) file_name = uid+'_ndwi_linegraph.png' document.add_picture(file_name,width=Inches(6.0)) document.add_page_break() document.add_heading('Vegetation Atmospherically Resistance Index Image (VARI) and Masked Vegetation Atmospherically Resistance Index Image (MVARI)',level=2) document.add_paragraph('\nVARI image provides you a color map of the crop health of your farming field and nearby areas. The areas shown in red are the regions where the crop growth may not be normal. Masked VARI image puts the layer of VARI image on the ETCI Image of the area to better identify the regionss where the crop growth may not be normal. You should refer to these images when there is too much humidity in the satellite captured image.') document.add_picture(vari_image,width=Inches(4)) document.add_paragraph('Visible Atmospherically Resistant Index Image (VARI)') document.add_picture(mvari_image,width=Inches(4)) document.add_paragraph('Masked Visible Atmospherically Resistant Index Image (MVARI)') document.add_paragraph('') document.add_heading('\nTable of VARI values with Time') vari_records = vari vari_columns = len(vari_records)+1 table = document.add_table(rows=1,cols=vari_columns) table.style = 'TableGrid' hdr_cells = table.rows[0].cells i =0 for (key,val) in vari_records.items(): if i==0: hdr_cells[i].text = 'Date' hdr_cells[i].bold = True i = i+1 y = key[0:4] m = key[4:6] d = key[6:] kk = y+'-'+m+'-'+d hdr_cells[i].text = kk hdr_cells[i].bold = True i = i+1 row_cells = table.add_row().cells i = 0 for (key,val) in vari_records.items(): if i==0: row_cells[i].text = 'VARI Value' i=i+1 val = float(val)/100 row_cells[i].text = str(val) row_cells[i].bold = True i=i+1 if len(vari_records)>1: document.add_heading('Graph of VARI vs Time',level=2) create_linegraph(uid,'vari',vari) file_name = uid+'_vari_linegraph.png' document.add_picture(file_name,width=Inches(6.0)) file_name = uid+'_report.docx' file_name_pdf = uid+'_report.pdf' document.save(file_name) #docx_to_pdf(file_name,file_name_pdf)