def generate_data(uid, fieldid): #from socket import * from oct2py import octave from io import BytesIO import base64 from google.cloud import storage from oauth2client.service_account import ServiceAccountCredentials import os import firebase_admin from firebase_admin import credentials from firebase_admin import db from PIL import Image, ImageFilter from send_notification import send_notification from sendemail import sendemail from sen_start_noti import sen_start_noti from send_sar_email import send_sar_email import json import scipy import random import time import datetime from merge_sar import merge_sar from find_sar import find_sar from merge_dem import merge_dem from find_dem import find_dem from scipy import ndimage from make_bigquery import make_bigquery from send_moni_noti import send_moni_noti from send_error_noti import send_error_noti from gmap_image import gmap_image from gmap_image_large import gmap_image_large from datetime import date from find_img import find_img from find_img_large import find_img_large from merge_img import merge_img from all_proc import all_proc from contour_images import contour_images from send_expiring_noti import send_expiring_noti from send_expired_noti import send_expired_noti from make_trial_bigquery import make_trial_bigquery from gen_geotiff import gen_geotiff from sendgeotifs import sendgeotifs from gen_report import gen_report from get_weather_data import get_weather_data from sendonlyreport import sendonlyreport from gen_failed_report import gen_failed_report from sendfailedreport import sendfailedreport #from search_sentinel import search_sentinel from map_coords import map_coords from search_new_sentinel import search_new_sentinel from convert_to_pdf import convert_to_pdf from latlon_jp2_to_pixel import latlon_jp2_to_pixel from gen_geotiff2 import gen_geotiff2 from search_sentinel_again import search_sentinel_again from get_prev_date import get_prev_date from make_bigquery_again import make_bigquery_again today = date.today() d1 = today.strftime('%Y%m%d') new_field = 0 print(d1) aqw2 = 1 cred = credentials.Certificate('servicekey.json') os.system("rm -rf AwsData") # Initialize the app with a service account, granting admin privileges #firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) uid_data = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() uid_list = {} field_list = {} field_list[fieldid] = uid_data uid_list[uid] = field_list img_count = 1 lets_monitor=1 latest_rvi_day = 0 last_latest_rvi_day = 0 #print(uid_list) #for tt in uid_list: #print(tt) for (k,v) in uid_list.items(): current_uid = k uid = current_uid print(uid) if uid != 'CeMGYvLXrGR5ZThxZ46iV7vY8sa2' and uid != 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2': #send_moni_noti(current_uid) fields = v for (p,q) in fields.items(): imagedate = None fieldid = p print(fieldid) fie = fields[fieldid] try: fieldDescription = fie["FieldDescription"] except: fieldDescription = "not available" print('fdd: ' +fieldDescription) try: payy = fie["Paid"] if payy=="yes" or payy=="Yes": payf = 1 else: payf = 0 sensed_day_flag=0 failed_day_flag = 0 senseddays = None big_query_performed = 0 paused = 0 expired = 0 faileddays = None print("Made the payment") except: print("payment not done") payf = 0 sensed_day_flag=0 failed_day_flag = 0 senseddays = None big_query_performed = 0 paused = 0 expired = 0 faileddays = None #try: # trialprocessed = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('TrialProcessed').get() #except: trialprocessed = 1 if payf == 1: sensed_day_flag=0 failed_day_flag = 0 big_query_performed = 0 paused = 0 expired = 0 totalpaidmonths = 0 totalsenseddays = 0 try: senseddays = fie["SensedDays"] except: senseddays = None try: faileddays = fie["FailedDays"] except: faileddays = None coordinates = fie["Coordinates"] fieldarea = fie["FieldArea"] latestsensedday = 0 latestfailedday = 0 latest_rvi_day = 0 latest_dem_day = 0 last_latest_rvi_day = 0 last_latest_dem_day = 0 try: pp = fie["Paused"] #pp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Paused').get() if pp=='yes': paused = 1 else: paused = 0 except: paused = 0 try: ee = fie["Expired"] #ee = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Expired').get() if int(ee)==0: expired = 0 else: expired = 1 except: expired = 0 try: tmp = fie["TotalPaidMonths"] totalpaidmonths = int(tpm) except: try: tpm = fie["PaymentType"] totalpaidmonths = float(tpm) except: tpm = 0 totalpaidmonths = 0 try: senseddays = fields[fieldid]["SensedDays"] for (u,v) in senseddays.items(): sensed_day = u totalsenseddays = totalsenseddays+1 if int(sensed_day)>int(latestsensedday): latestsensedday = sensed_day print('LSD') print(latestsensedday) new_field=0 except: print('new field') new_field=1 totalsardays = 0 try: sar_days = fields[fieldid]["SARDays"] for (u,v) in senseddays.items(): sensed_day = u totalsardays = totalsardays+1 if int(sensed_day)>int(latest_rvi_day): latest_rvi_day = sensed_day last_latest_rvi_day = latest_rvi_day print('LSDR') print(latest_rvi_day) new_field=0 except: print('new field') new_field=1 totaldemdays = 0 try: dem_days = fields[fieldid]["DEMDays"] for (u,v) in senseddays.items(): sensed_day = u totaldemdays = totaldemdays+1 if int(sensed_day)>int(latest_dem_day): latest_dem_day = sensed_day last_latest_dem_day = latest_dem_day print('LSDD') print(latest_dem_day) new_field=0 except: print('new field') new_field=1 totalfaileddays = 0 try: faileddays = fields[fieldid]["FailedDays"] for (u,v) in faileddays.items(): failed_day = u totalfaileddays = totalfaileddays+1 if int(failed_day)>int(latestfailedday): latestfailedday = failed_day print('LFD') print(latestfailedday) except: print('no failed day') print('tpm') print(totalpaidmonths) #if totalpaidmonths>0: trialenabled = 'no' if totalsenseddays > 0: trialenabled = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('trialEnabled').get() if trialenabled == 'yes': totalpaidmonths = 1 intfieldid = float(fieldid) intfieldid = intfieldid/1000 orderdate = datetime.datetime.fromtimestamp(intfieldid).strftime('%Y-%m-%d %H:%M:%S.%f') orderdate = orderdate[:10] orderyear = orderdate[:4] ordermonth = orderdate[5:7] orderday = orderdate[8:10] todayyear = d1[:4] todaymonth = d1[4:6] todayday = d1[6:] f_date = date(int(orderyear),int(ordermonth),int(orderday)) l_date = date(int(todayyear),int(todaymonth), int(todayday)) delta = l_date - f_date ndays = delta.days print('ndays') print(ndays) if (totalsenseddays>=6*totalpaidmonths+1) and ndays > 30*totalpaidmonths: expired = 1 db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Expired').set('1') send_expired_noti(uid) print('expired') else: db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Expired').delete() expired = 0 if (6*totalpaidmonths-totalsenseddays<3) and (30*totalpaidmonths - ndays < 10): db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Expiring').set('yes') else: db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Expiring').set('no') #if fieldDescription=='Bamboo': if (int(d1)-int(latestsensedday)>4) and (int(d1)-int(latestfailedday)): if new_field==1 and payf==1: # req_json = make_bigquery(current_uid,fieldid) req_json_sar = find_sar(uid,fieldid,latest_rvi_day, expired,paused) latest_rvi_day = req_json_sar["LatestDay"] req_json_dem = find_dem(uid,fieldid,latest_dem_day, expired,paused) latest_dem_day = req_json_dem["LatestDay"] req_json = search_new_sentinel(uid,fieldid,latestsensedday,latestfailedday,expired,paused) elif new_field==0 and payf==1 and expired==0: # req_json = make_bigquery(current_uid,fieldid) req_json_sar = find_sar(uid,fieldid,latest_rvi_day, expired, paused) latest_rvi_day = req_json_sar["LatestDay"] req_json_dem = find_dem(uid,fieldid,latest_dem_day, expired,paused) latest_dem_day = req_dem_sar["LatestDay"] req_json = search_new_sentinel(uid,fieldid,latestsensedday, latestfailedday,expired,paused) else: print('notpaid') req_json = {} req_json["MGRS"]="NotA" req_json["LatestDay"]=d1 new_string_json=req_json imagedate = str(req_json["LatestDay"]) print('im_date') print(imagedate) uid = current_uid print(uid) print(req_json) big_query_performed = 1 mgrs = req_json["MGRS"] print('mgrs') print(mgrs) try: previous_day = db.reference('LatestTileDates').child(mgrs).get() except: db.reference('LatestTileDates').child(str(mgrs)).set(imagedate) if previous_day is None: previous_day = '11' print('p_date') print(previous_day) if int(previous_day)=0: result_status = uid+"failed4" else: #try: #result_status = octave.download_tiles(new_string_json) result_status = "successful" if result_status.find("failed") < 0: up_left_x,up_left_y = latlon_jp2_to_pixel(fieldmaxlat,fieldminlong,imagedate,mgrs) up_right_x,up_right_y = latlon_jp2_to_pixel(fieldmaxlat,fieldmaxlong,imagedate,mgrs) down_right_x,down_right_y = latlon_jp2_to_pixel(fieldminlat,fieldmaxlong,imagedate,mgrs) down_left_x,down_left_y = latlon_jp2_to_pixel(fieldminlat,fieldminlong,imagedate,mgrs) new_string_json["StartPixelLat"] = min([up_left_x,up_right_x,down_left_x,down_right_x]) new_string_json["EndPixelLat"] = max([up_left_x,up_right_x,down_left_x,down_right_x]) new_string_json["StartPixelLong"] = min([up_left_y,up_right_y,down_left_y,down_right_y]) new_string_json["EndPixelLong"] = max([up_left_y,up_right_y,down_left_y,down_right_y]) new_string_json["FieldID"] = fieldid result_status= octave.monitored_fields_data_22_april(new_string_json) #except: # result_status = uid+"failed4" try: aqw2 = octave.check_clouds(uid) except: aqw2 = 2 diff_count = 0 else: result_status = "failed0" storage_client = storage.Client() bucket_name = 'farmbase-b2f7e.appspot.com' try: destination_blob_name_mask = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mask' destination_blob_name_tci = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/tci' destination_blob_name_etci = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/etci' destination_blob_name_hybrid = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/hybrid' destination_blob_name_ndvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi' destination_blob_name_evi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/evi' destination_blob_name_rvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/rvi' destination_blob_name_mndvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndvi' destination_blob_name_ndwi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndwi' destination_blob_name_mndwi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndwi' destination_blob_name_ndre = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndre' destination_blob_name_mndre = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndre' destination_blob_name_vari = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/vari' destination_blob_name_soc = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/soc' destination_blob_name_savi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/savi' destination_blob_name_evapo = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/evapo' destination_blob_name_avi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/avi' destination_blob_name_bsi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/bsi' destination_blob_name_si = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/si' destination_blob_name_soc_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/soc_cmap2' destination_blob_name_mvari = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mvari' destination_blob_name_ndre_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndre_cmap2' destination_blob_name_ndvi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi_cmap2' destination_blob_name_evi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/evi_cmap2' destination_blob_name_ndwi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndwi_cmap2' destination_blob_name_vari_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/vari_cmap2' destination_blob_name_savi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/savi_cmap2' destination_blob_name_avi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/avi_cmap2' destination_blob_name_bsi_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/bsi_cmap2' destination_blob_name_si_cmap2 = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/si_cmap2' #tif destination blob names destination_blob_name_tci_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/tci.tif' destination_blob_name_hybrid_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/hybrid.tif' destination_blob_name_etci_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/etci.tif' destination_blob_name_ndvi_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi.tif' destination_blob_name_evi_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/evi.tif' destination_blob_name_mndvi_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndvi.tif' destination_blob_name_soc_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/soc.tif' destination_blob_name_ndwi_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndwi.tif' destination_blob_name_mndwi_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndwi.tif' destination_blob_name_ndre_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndre.tif' destination_blob_name_mndre_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndre.tif' destination_blob_name_vari_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/vari.tif' destination_blob_name_mvari_tif = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mvari.tif' source_file_name =uid+ '.png' mask_file_name = uid + '/mask_img.png' tci_file_name = uid+'/TCI.png' hybrid_file_name = uid+'/hybrid.png' etci_file_name = uid+'/ETCI.png' ndvi_file_name = uid+'/ndvi.png' evi_file_name = uid+'/evi.png' mndvi_file_name = uid+'/mndvi.png' ndwi_file_name = uid+'/ndwi.png' mndwi_file_name = uid+'/mndwi.png' ndre_file_name = uid+'/ndre.png' mndre_file_name = uid+'/mndre.png' vari_file_name = uid+'/vari.png' mvari_file_name = uid+'/mvari.png' soc_file_name = uid+'/soc.png' savi_file_name = uid+'/savi.png' evapo_file_name = uid+'/evapo.png' avi_file_name = uid+'/avi.png' bsi_file_name = uid+'/bsi.png' si_file_name = uid+'/si.png' rvi_file_name = uid+'/rvi.png' #tif source file names soc_file_name_tif = uid+'/soc.tif' tci_file_name_tif = uid+'/TCI.tif' hybrid_file_name_tif = uid+'/hybrid.tif' etci_file_name_tif = uid+'/ETCI.tif' ndvi_file_name_tif = uid+'/ndvi.tif' evi_file_name_tif = uid+'/evi.tif' mndvi_file_name_tif = uid+'/mndvi.tif' ndwi_file_name_tif = uid+'/ndwi.tif' mndwi_file_name_tif = uid+'/mndwi.tif' ndre_file_name_tif = uid+'/ndre.tif' mndre_file_name_tif = uid+'/mndre.tif' vari_file_name_tif = uid+'/vari.tif' mvari_file_name_tif = uid+'/mvari.tif' destination_blob_name_report = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/report.docx' destination_blob_name_ndvi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi_pie' destination_blob_name_evi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/evi_pie' destination_blob_name_ndwi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndwi_pie' destination_blob_name_ndre_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndre_pie' destination_blob_name_vari_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/vari_pie' destination_blob_name_savi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/savi_pie' destination_blob_name_avi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/avi_pie' destination_blob_name_bsi_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/bsi_pie' destination_blob_name_si_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/si_pie' destination_blob_name_soc_pie = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/soc_pie' destination_blob_name_tciL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/tci_large' destination_blob_name_etciL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/etci_large' destination_blob_name_ndviL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi_large' destination_blob_name_mndviL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndvi_large' destination_blob_name_ndwiL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndwi_large' destination_blob_name_mndwiL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndwi_large' destination_blob_name_ndreL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndre_large' destination_blob_name_mndreL = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/mndre_large' tci_file_name = uid+'/TCI_large.png' etci_file_name = uid+'/ETCI_large.png' ndvi_file_name = uid+'/ndvi_large.png' mndvi_file_name = uid+'/mndvi_large.png' ndwi_file_name = uid+'/ndwi_large.png' mndwi_file_name = uid+'/mndwi_large.png' ndre_file_name = uid+'/ndre_large.png' mndre_file_name = uid+'/mndre_large.png' except: print('problem in assigning storage address') if int(latest_rvi_day) > int(last_latest_rvi_day) and expired == 0 and paused == 0: try: fieldmaxlat = new_string_json["FieldMaxLat"] fieldminlat = new_string_json["FieldMinLat"] fieldminlong = new_string_json["FieldMinLong"] fieldmaxlong = new_string_json["FieldMaxLong"] aqpp = merge_dem(uid,fieldid,coordinates,latest_dem_day,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) destination_blob_name_rvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+latest_rvi_day+'/dem' rvi_file_name = uid+'/dem.png' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name) except: print('rvi error') if int(latest_rvi_day) > int(last_latest_rvi_day) and expired == 0 and paused == 0: db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SARDays').child(str(latest_rvi_day)).set('yes') try: fieldmaxlat = new_string_json["FieldMaxLat"] fieldminlat = new_string_json["FieldMinLat"] fieldminlong = new_string_json["FieldMinLong"] fieldmaxlong = new_string_json["FieldMaxLong"] aqpp = merge_sar(uid,fieldid,coordinates,latest_rvi_day,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) destination_blob_name_rvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+latest_rvi_day+'/rvi' rvi_file_name = uid+'/rvi.png' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_rvi) blob.upload_from_filename(rvi_file_name) aqpp = merge_dem(uid,fieldid,coordinates,latest_dem_day,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) destination_blob_name_rvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+latest_dem_day+'/dem' rvi_file_name = uid+'/dem.png' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name) attachments = set() attachments.add(rvi_file_name) useremail = fie["Email"] f_address = fie["FieldAddress"] send_sar_email(attachments,useremail,f_address,imagedate) except: print('rvi error') if result_status.find('successful') < 0 : if result_status.find('failed0')>0: uid = result_status.replace("failed0","") print(uid) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') send_error_noti(uid) if result_status.find('failed1')>0: uid = result_status.replace("failed1","") if generate_tifs==1: bucket = storage_client.get_bucket(bucket_name) useremail = fie["Email"] #useremail = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Email').get() f_address = fie["FieldAddress"] #f_address = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldAddress').get() gen_failed_report(uid,f_address,centerlat,centerlong,imagedate,fieldid,fieldarea) doc_name=uid+'/report.docx' attachments.add(doc_name) blob = bucket.blob(destination_blob_name_report) blob.upload_from_filename(doc_name) sendfailedreport(attachments,useremail,f_address,imagedate) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') send_error_noti(uid) if result_status.find('failed2')>0: uid = result_status.replace("failed2","") db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') send_error_noti(uid) if result_status.find('failed4')>0: uid = result_status.replace("failed4","") else: uid = result_status.replace("successful","") aqw2 = octave.check_clouds(uid) p_date =latestsensedday c_lat = None p_coords = 0 if aqw2<2: fieldlatlen = abs(float(fieldmaxlat)-float(fieldminlat)) fieldlonglen = abs(float(fieldmaxlong)-float(fieldminlong)) field_area = map_coords(uid,fieldid,coordinates,imagedate,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong,cropType) source_file_name = source_file_name.replace("successful","") tci_file_name = tci_file_name.replace("successful","") bucket = storage_client.get_bucket(bucket_name) tci_file_name = uid+'/TCI.png' hybrid_file_name = uid+'/hybrid.png' etci_file_name = uid+'/ETCI.png' ndvi_file_name = uid+'/ndvi.png' evi_file_name = uid+'/evi.png' mndvi_file_name = uid+'/mndvi.png' ndwi_file_name = uid+'/ndwi.png' mndwi_file_name = uid+'/mndwi.png' ndre_file_name = uid+'/ndre.png' mndre_file_name = uid+'/mndre.png' vari_file_name = uid+'/vari.png' mvari_file_name = uid+'/mvari.png' soc_file_name = uid+'/soc.png' savi_file_name = uid+'/savi.png' evapo_file_name = uid+'/evapo.png' avi_file_name = uid+'/avi.png' bsi_file_name = uid+'/bsi.png' si_file_name = uid+'/si.png' rvi_file_name = uid+'/rvi.png' soc_cmap2_file_name = uid+'/soc_cmap2.png' ndvi_cmap2_file_name = uid+'/ndvi_cmap2.png' savi_cmap2_file_name = uid+'/savi_cmap2.png' avi_cmap2_file_name = uid+'/avi_cmap2.png' bsi_cmap2_file_name = uid+'/bsi_cmap2.png' si_cmap2_file_name = uid+'/si_cmap2.png' evi_cmap2_file_name = uid+'/evi_cmap2.png' ndwi_cmap2_file_name = uid+'/ndwi_cmap2.png' ndre_cmap2_file_name = uid+'/ndre_cmap2.png' vari_cmap2_file_name = uid+'/vari_cmap2.png' soc_file_name_pie = uid+'/soc'+'_piechart.png' ndvi_file_name_pie = uid+'/ndvi'+'_piechart.png' evi_file_name_pie = uid+'/evi'+'_piechart.png' ndwi_file_name_pie = uid+'/ndwi'+'_piechart.png' ndre_file_name_pie = uid+'/ndre'+'_piechart.png' vari_file_name_pie = uid+'/vari'+'_piechart.png' savi_file_name_pie = uid+'/savi'+'_piechart.png' avi_file_name_pie = uid+'/avi'+'_piechart.png' bsi_file_name_pie = uid+'/bsi'+'_piechart.png' si_file_name_pie = uid+'/si'+'_piechart.png' #tif source file names tci_file_name_tif = uid+'/TCI.tif' hybrid_file_name_tif = uid+'/hybrid.tif' etci_file_name_tif = uid+'/ETCI.tif' ndvi_file_name_tif = uid+'/ndvi.tif' evi_file_name_tif = uid+'/evi.tif' mndvi_file_name_tif = uid+'/mndvi.tif' ndwi_file_name_tif = uid+'/ndwi.tif' mndwi_file_name_tif = uid+'/mndwi.tif' ndre_file_name_tif = uid+'/ndre.tif' mndre_file_name_tif = uid+'/mndre.tif' vari_file_name_tif = uid+'/vari.tif' mvari_file_name_tif = uid+'/mvari.tif' soc_file_name_tif = uid+'/soc.tif' ndre_cmap2_file_name_tif = uid+'/ndre_cmap2.tif' ndvi_cmap2_file_name_tif = uid+'/ndvi_cmap2.tif' evi_cmap2_file_name_tif = uid+'/evi_cmap2.tif' vari_cmap2_file_name_tif = uid+'/vari_cmap2.tif' ndwi_cmap2_file_name_tif = uid+'/ndwi_cmap2.tif' soc_cmap2_file_name_tif = uid+'/soc_cmap2.tif' blob = bucket.blob(destination_blob_name_tci) blob.upload_from_filename(tci_file_name) blob = bucket.blob(destination_blob_name_hybrid) blob.upload_from_filename(hybrid_file_name) blob = bucket.blob(destination_blob_name_etci) blob.upload_from_filename(etci_file_name) try: destination_blob_name_rvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+latest_rvi_day+'/rvi' rvi_file_name = uid+'/rvi.png' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_rvi) blob.upload_from_filename(rvi_file_name) except: print('rvi error') blob = bucket.blob(destination_blob_name_mask) blob.upload_from_filename(mask_file_name) blob = bucket.blob(destination_blob_name_ndvi_cmap2) blob.upload_from_filename(ndvi_cmap2_file_name) blob = bucket.blob(destination_blob_name_evi_cmap2) blob.upload_from_filename(evi_cmap2_file_name) blob = bucket.blob(destination_blob_name_ndwi_cmap2) blob.upload_from_filename(ndwi_cmap2_file_name) blob = bucket.blob(destination_blob_name_savi_cmap2) blob.upload_from_filename(savi_cmap2_file_name) blob = bucket.blob(destination_blob_name_avi_cmap2) blob.upload_from_filename(avi_cmap2_file_name) blob = bucket.blob(destination_blob_name_bsi_cmap2) blob.upload_from_filename(bsi_cmap2_file_name) blob = bucket.blob(destination_blob_name_si_cmap2) blob.upload_from_filename(si_cmap2_file_name) blob = bucket.blob(destination_blob_name_ndre_cmap2) blob.upload_from_filename(ndre_cmap2_file_name) blob = bucket.blob(destination_blob_name_vari_cmap2) blob.upload_from_filename(vari_cmap2_file_name) blob = bucket.blob(destination_blob_name_soc_cmap2) blob.upload_from_filename(soc_cmap2_file_name) blob = bucket.blob(destination_blob_name_soc) blob.upload_from_filename(soc_file_name) blob = bucket.blob(destination_blob_name_ndvi) blob.upload_from_filename(ndvi_file_name) blob = bucket.blob(destination_blob_name_savi) blob.upload_from_filename(savi_file_name) blob = bucket.blob(destination_blob_name_evapo) blob.upload_from_filename(evapo_file_name) blob = bucket.blob(destination_blob_name_avi) blob.upload_from_filename(avi_file_name) blob = bucket.blob(destination_blob_name_bsi) blob.upload_from_filename(bsi_file_name) blob = bucket.blob(destination_blob_name_si) blob.upload_from_filename(si_file_name) blob = bucket.blob(destination_blob_name_evi) blob.upload_from_filename(evi_file_name) blob = bucket.blob(destination_blob_name_vari) blob.upload_from_filename(vari_file_name) blob = bucket.blob(destination_blob_name_ndwi) blob.upload_from_filename(ndwi_file_name) blob = bucket.blob(destination_blob_name_ndre) blob.upload_from_filename(ndre_file_name) f_address = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldAddress').get() centerlat = centerlat_large centerlong = centerlong_large if f_address is None: f_address = 'not defined' if generate_tifs==1: if f_address.find("India")<0 or gen_tif == 'abs_yes': gen_geotiff2(new_string_json,uid,'ndvi','ndvi.tif') gen_geotiff2(new_string_json,uid,'evi','evi.tif') gen_geotiff2(new_string_json,uid,'ndre','ndre.tif') gen_geotiff2(new_string_json,uid,'ndwi','ndwi.tif') gen_geotiff2(new_string_json,uid,'vari','vari.tif') gen_geotiff2(new_string_json,uid,'TCI','TCI.tif') gen_geotiff2(new_string_json,uid,'hybrid','hybrid.tif') gen_geotiff2(new_string_json,uid,'ETCI','ETCI.tif') gen_geotiff2(new_string_json,uid,'ndvi_cmap2','ndvi_cmap2.tif') gen_geotiff2(new_string_json,uid,'ndre_cmap2','ndre_cmap2.tif') gen_geotiff2(new_string_json,uid,'ndwi_cmap2','ndwi_cmap2.tif') gen_geotiff2(new_string_json,uid,'vari_cmap2','vari_cmap2.tif') gen_geotiff2(new_string_json,uid,'soc','soc.tif') gen_geotiff2(new_string_json,uid,'soc_cmap2','soc_cmap2.tif') try: #upload tif files blob = bucket.blob(destination_blob_name_tci_tif) blob.upload_from_filename(tci_file_name_tif) blob = bucket.blob(destination_blob_name_hybrid_tif) blob.upload_from_filename(hybrid_file_name_tif) blob = bucket.blob(destination_blob_name_etci_tif) blob.upload_from_filename(etci_file_name_tif) blob = bucket.blob(destination_blob_name_ndvi_tif) blob.upload_from_filename(ndvi_file_name_tif) blob = bucket.blob(destination_blob_name_evi_tif) blob.upload_from_filename(evi_file_name_tif) blob = bucket.blob(destination_blob_name_vari_tif) blob.upload_from_filename(vari_file_name_tif) blob = bucket.blob(destination_blob_name_ndwi_tif) blob.upload_from_filename(ndwi_file_name_tif) blob = bucket.blob(destination_blob_name_ndre_tif) blob.upload_from_filename(ndre_file_name_tif) blob = bucket.blob(destination_blob_name_soc_tif) blob.upload_from_filename(soc_file_name_tif) attachments.add(tci_file_name_tif) attachments.add(hybrid_file_name_tif) attachments.add(etci_file_name_tif) attachments.add(ndvi_file_name_tif) attachments.add(evi_file_name_tif) attachments.add(ndwi_file_name_tif) attachments.add(ndre_file_name_tif) attachments.add(vari_file_name_tif) attachments.add(soc_file_name_tif) attachments.add(ndwi_cmap2_file_name_tif) attachments.add(ndvi_cmap2_file_name_tif) attachments.add(evi_cmap2_file_name_tif) attachments.add(ndre_cmap2_file_name_tif) attachments.add(vari_cmap2_file_name_tif) attachments.add(soc_cmap2_file_name_tif) except: print('tif error') useremail = fie["Email"] #useremail = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Email').get() gen_report(uid,f_address,centerlat,centerlong,imagedate,fieldid,field_area) blob = bucket.blob(destination_blob_name_ndvi_pie) blob.upload_from_filename(ndvi_file_name_pie) blob = bucket.blob(destination_blob_name_evi_pie) blob.upload_from_filename(evi_file_name_pie) blob = bucket.blob(destination_blob_name_ndwi_pie) blob.upload_from_filename(ndwi_file_name_pie) blob = bucket.blob(destination_blob_name_ndre_pie) blob.upload_from_filename(ndre_file_name_pie) blob = bucket.blob(destination_blob_name_vari_pie) blob.upload_from_filename(vari_file_name_pie) blob = bucket.blob(destination_blob_name_soc_pie) blob.upload_from_filename(soc_file_name_pie) blob = bucket.blob(destination_blob_name_savi_pie) blob.upload_from_filename(savi_file_name_pie) blob = bucket.blob(destination_blob_name_avi_pie) blob.upload_from_filename(avi_file_name_pie) blob = bucket.blob(destination_blob_name_bsi_pie) blob.upload_from_filename(bsi_file_name_pie) blob = bucket.blob(destination_blob_name_si_pie) blob.upload_from_filename(si_file_name_pie) ff = f_address.replace(" ","_") ff = ff.replace(",","_") ff = ff.replace("/","_") convert_to_pdf(uid,imagedate,ff) doc_name=uid+'/Farmonaut_report_'+str(imagedate)+'_'+ff+'.pdf' doc_name_docx = uid+'/report.docx' # try: blob = bucket.blob(destination_blob_name_report) blob.upload_from_filename(doc_name) attachments.add(doc_name) # except: # print('report error') sendgeotifs(attachments,useremail,f_address,imagedate) else: useremail = fie["Email"] #useremail = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Email').get() gen_report(uid,f_address,centerlat,centerlong,imagedate,fieldid,field_area) blob = bucket.blob(destination_blob_name_ndvi_pie) blob.upload_from_filename(ndvi_file_name_pie) blob = bucket.blob(destination_blob_name_evi_pie) blob.upload_from_filename(evi_file_name_pie) blob = bucket.blob(destination_blob_name_ndwi_pie) blob.upload_from_filename(ndwi_file_name_pie) blob = bucket.blob(destination_blob_name_ndre_pie) blob.upload_from_filename(ndre_file_name_pie) blob = bucket.blob(destination_blob_name_vari_pie) blob.upload_from_filename(vari_file_name_pie) blob = bucket.blob(destination_blob_name_soc_pie) blob.upload_from_filename(soc_file_name_pie) blob = bucket.blob(destination_blob_name_savi_pie) blob.upload_from_filename(savi_file_name_pie) blob = bucket.blob(destination_blob_name_avi_pie) blob.upload_from_filename(avi_file_name_pie) blob = bucket.blob(destination_blob_name_bsi_pie) blob.upload_from_filename(bsi_file_name_pie) blob = bucket.blob(destination_blob_name_si_pie) blob.upload_from_filename(si_file_name_pie) ff = f_address.replace(" ","_") ff = ff.replace(",","_") ff = ff.replace("/","_") ff = ff.replace("'\'","_") convert_to_pdf(uid,imagedate,ff) doc_name=uid+'/Farmonaut_report_'+str(imagedate)+'_'+ff+'.pdf' blob = bucket.blob(destination_blob_name_report) try: blob.upload_from_filename(doc_name) attachments.add(doc_name) except: print('report_erro') sendonlyreport(attachments,useremail,f_address,imagedate) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').child(imagedate).set('yes') send_moni_noti(uid) else: if generate_tifs==1: useremail = fie["Email"] f_address = fie["FieldAddress"] #useremail = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Email').get() #f_address = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldAddress').get() gen_failed_report(uid,f_address,centerlat,centerlong,imagedate,fieldid,fieldarea) ff = f_address.replace(" ","_") ff = ff.replace(",","_") ff = ff.replace("'","_") convert_to_pdf(uid,imagedate,ff) doc_name=uid+'/Farmonaut_report_'+str(imagedate)+'_'+ff+'.pdf' doc_name_docx = uid+'/report.docx' bucket = storage_client.get_bucket(bucket_name) try: blob = bucket.blob(destination_blob_name_report) blob.upload_from_filename(doc_name) if uid.find('MYM46idThYa8ddEtNl9a0o0Rn9A3')>=0: attachments.add(doc_name_docx) else: attachments.add(doc_name) sendfailedreport(attachments,useremail,f_address,imagedate) except: print('report_err') db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') send_error_noti(uid) os.system("rm -rf AwsData")