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 import json import scipy import random import time import datetime 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 datetime import date from find_img import find_img today = date.today() d1 = today.strftime('%Y%m%d') print(d1) cred = credentials.Certificate('servicekey.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) uid_list = db.reference('PaidMonitoredFields').child('PMF').get() img_count = 1 #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) #send_moni_noti(current_uid) fields = v for (p,q) in fields.items(): fieldid = p print(fieldid) try: fie = fields[fieldid] payy = fie["Paid"] payf = 1 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 faileddays = None if payf == 1: sensed_day_flag=0 failed_day_flag = 0 big_query_performed = 0 senseddays = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').get() faileddays = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').get() latestsensedday = 0 try: senseddays = fields[fieldid]["SensedDays"] for (u,v) in senseddays.items(): sensed_day = u if int(sensed_day)>int(latestsensedday): latestsensedday = sensed_day print(latestsensedday) except: print('new field') if int(d1)-int(latestsensedday)>3: req_json = make_bigquery(current_uid,fieldid) new_string_json=req_json imagedate = str(req_json["LatestDay"]) uid = current_uid print(uid) print(req_json) big_query_performed = 1 else: imagedate = 0 #fields[fieldid]["SensedDays"]["sample"]="yes" if faileddays is None: print("No previous failed days") else: faileddays = fields[fieldid]["FailedDays"] for (u,v) in faileddays.items(): failed_day = u if failed_day==imagedate: print("alreadyy failed processing for this date") failed_day_flag = 1 break if senseddays is None: print("No previous sensed days") else: senseddays = fields[fieldid]["SensedDays"] for (u,v) in senseddays.items(): sensed_day = u if sensed_day==imagedate: print("alreadyy done processing for this date") sensed_day_flag = 1 break #db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').child(imagedate).set('yes') imageid = fieldid print("sfpf") #010 c_code = str(sensed_day_flag)+str(payf)+str(failed_day_flag)+str(big_query_performed) print(c_code) if c_code=='0101': #storage_client2 = storage.Client() #bucket2 = storage_client2.get_bucket('gs://gcp-public-data-sentinel-2') #blob2 = bucket2.blob(str(new_string_json["Band4URI"])) #blob2.download_to_fielname('trial2.jp2') fieldmaxlat = new_string_json["FieldMaxLat"] fieldminlat = new_string_json["FieldMinLat"] fieldminlong = new_string_json["FieldMinLong"] fieldmaxlong = new_string_json["FieldMaxLong"] fieldarea = new_string_json["FieldArea"] tilemaxlat = new_string_json["TileMaxLat"] tileminlat = new_string_json["TileMinLat"] tilemaxlong = new_string_json["TileMaxLong"] tileminlong = new_string_json["TileMinLong"] #gmap_image(fieldminlat,fieldmaxlat,fieldminlong,fieldmaxlong,tilemaxlat,tileminlat,tilemaxlong,tileminlong,fieldid,uid,fieldarea) result_status,ndvi_array,ndre_array,ndwi_array = octave.monitored_fields_data(new_string_json) if result_status.find('successful')>=0: try: merge_img(ndvi_array,uid,'ndvi') merge_img(ndre_array,uid,'ndre') merge_img(ndwi_array,uid,'ndwi') except: result_status = "failed0" else: result_status = "failed0" storage_client = storage.Client() bucket_name = 'farmbase-b2f7e.appspot.com' try: destination_blob_name_tci = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/tci' destination_blob_name_etci = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/etci' destination_blob_name_ndvi = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+imagedate+'/ndvi' 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' source_file_name =uid+ '.png' tci_file_name = uid+'_TCI.png' etci_file_name = uid+'_ETCI.png' ndvi_file_name = uid+'_ndvi.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' 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') #destination_blob_name_tci = 'NewsFeedImages/'+uid+'/'+imageid+uid+'/1' #cred = credentials.Certificate('servicekey.json') #firebase_admin.initialize_app(cred) 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_notification(uid,'failed0',image_id) #db.reference(uid).child('Profile2').child('TempField').child('SentinelStatus').set('failed') send_error_noti(uid) #db.reference('PaidMonitoring').child(uid).child(fieldid).child('status').set('finished') #messagetoclient = str.encode("failed0") #connectionSocket.send(messagetoclient) if result_status.find('failed1')>0: uid = result_status.replace("failed1","") db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') #send_notification(uid,'failed1',image_id) #db.reference('PaidMonitoring').child(uid).child(fieldid).child('status').set('finished') send_error_noti(uid) #messagetoclient=str.encode("failed1") #connectionSocket.send(messagetoclient) if result_status.find('failed4')>0: uid = result_status.replace("failed4","") #db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FailedDays').child(imagedate).set('yes') #send_notification(uid,'failed1',image_id) #db.reference('PaidMonitoring').child(uid).child(fieldid).child('status').set('finished') #send_error_noti(uid) else: uid = result_status.replace("successful","") #get map static image try: img_name = uid+str(fieldid)+'_static_map.png' im = Image.open(img_name) except: gmap_image(fieldminlat,fieldmaxlat,fieldminlong,fieldmaxlong,tilemaxlat,tileminlat,tilemaxlong,tileminlong,fieldid,uid,fieldarea) #a = octave.identify_field(uid,fieldid) #print(a) #xstart=a[0][0] #xend = a[0][1] #ystart = a[0][2] #yend = a[0][3] xstart,xend,ystart,yend = find_img(uid,fieldid) octave.crop_images(uid,xstart,xend,ystart,yend,new_string_json) #send_notification(uid,'successful',image_id) source_file_name = source_file_name.replace("successful","") tci_file_name = tci_file_name.replace("successful","") bg_file_name = uid+'_ETCI.png' ov_file_name = uid+'_mndvi.png' bg = Image.open(bg_file_name) widthetci,heightetci = bg.size ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndvi.png' new_img2.save(file_name,"PNG") bg_file_name = uid+'_ETCI.png' ov_file_name = uid+'_mndwi.png' bg = Image.open(bg_file_name) ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") ov = ov.resize((widthetci,heightetci),Image.NEAREST) new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) #bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndwi.png' new_img2.save(file_name,"PNG") bg_file_name = uid+'_ETCI.png' ov_file_name = uid+'_mndre.png' bg = Image.open(bg_file_name) ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") ov = ov.resize((widthetci,heightetci),Image.NEAREST) new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) #bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndre.png' new_img2.save(file_name,"PNG") bg_file_name = uid+'_ETCI_large.png' ov_file_name = uid+'_mndvi_large.png' bg = Image.open(bg_file_name) widthetci,heightetci = bg.size ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndvi_large.png' new_img2.save(file_name,"PNG") bg_file_name = uid+'_ETCI_large.png' ov_file_name = uid+'_mndwi_large.png' bg = Image.open(bg_file_name) ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") ov = ov.resize((widthetci,heightetci),Image.NEAREST) new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) #bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndwi_large.png' new_img2.save(file_name,"PNG") bg_file_name = uid+'_ETCI_large.png' ov_file_name = uid+'_mndre_large.png' bg = Image.open(bg_file_name) ov = Image.open(ov_file_name) bg = bg.convert("RGBA") ov = ov.convert("RGBA") ov = ov.resize((widthetci,heightetci),Image.NEAREST) new_img = Image.blend(bg,ov,1) new_img2 = Image.blend(bg,new_img,0.4) new_img2 = new_img2.filter(ImageFilter.SHARPEN) #bg = bg.filter(ImageFilter.SHARPEN) bg.save(bg_file_name,"PNG") file_name = uid+'_mndre_large.png' new_img2.save(file_name,"PNG") bucket = storage_client.get_bucket(bucket_name) tci_file_name = uid+'_TCI.png' etci_file_name = uid+'_ETCI.png' ndvi_file_name = uid+'_ndvi.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' blob = bucket.blob(destination_blob_name_tci) blob.upload_from_filename(tci_file_name) blob = bucket.blob(destination_blob_name_etci) blob.upload_from_filename(etci_file_name) blob = bucket.blob(destination_blob_name_ndvi) blob.upload_from_filename(ndvi_file_name) blob = bucket.blob(destination_blob_name_mndvi) blob.upload_from_filename(mndvi_file_name) blob = bucket.blob(destination_blob_name_ndwi) blob.upload_from_filename(ndwi_file_name) blob = bucket.blob(destination_blob_name_mndwi) blob.upload_from_filename(mndwi_file_name) blob = bucket.blob(destination_blob_name_ndre) blob.upload_from_filename(ndre_file_name) blob = bucket.blob(destination_blob_name_mndre) blob.upload_from_filename(mndre_file_name) 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' blob = bucket.blob(destination_blob_name_tciL) blob.upload_from_filename(tci_file_name) blob = bucket.blob(destination_blob_name_etciL) blob.upload_from_filename(etci_file_name) blob = bucket.blob(destination_blob_name_ndviL) blob.upload_from_filename(ndvi_file_name) blob = bucket.blob(destination_blob_name_mndviL) blob.upload_from_filename(mndvi_file_name) blob = bucket.blob(destination_blob_name_ndwiL) blob.upload_from_filename(ndwi_file_name) blob = bucket.blob(destination_blob_name_mndwiL) blob.upload_from_filename(mndwi_file_name) blob = bucket.blob(destination_blob_name_ndreL) blob.upload_from_filename(ndre_file_name) blob = bucket.blob(destination_blob_name_mndreL) blob.upload_from_filename(mndre_file_name) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').child(imagedate).set('yes') send_moni_noti(uid)