def make_bigquery_again(uid,fieldid,latestsensedday,diff_count): 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 from scipy import ndimage from google.cloud import bigquery from get_prev_date import get_prev_date import datetime import time aa = 1 cred = credentials.Certificate('servicekey2.json') # Initialize the app with a service account, granting admin privileges #firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) client = bigquery.Client().from_service_account_json('servicekey2.json') map_fields = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() #print(map_fields) #time.sleep(4) field_latitude = map_fields["Coordinates"]["a"]["Latitude"] field_longitude = map_fields["Coordinates"]["a"]["Longitude"] field_max_lat = map_fields["FieldMaxLat"] field_min_lat = map_fields["FieldMinLat"] field_max_long = map_fields["FieldMaxLong"] field_min_long = map_fields["FieldMinLong"] field_area = map_fields["FieldArea"] if field_max_lat < field_min_lat: temp = field_max_lat field_max_lat = field_min_lat field_min_lat = temp if field_max_long < field_min_long: temp = field_max_long field_max_long = field_min_long field_min_long = temp #x = datetime.datetime.now() failedimagedate = latestsensedday[6:] failedimagedate = int(failedimagedate) x = get_prev_date(latestsensedday,diff_count) onedayless = get_prev_date(latestsensedday,1) current_year = latestsensedday[0:4] current_month = latestsensedday[4:6] current_date = latestsensedday[6:] current_date = int(current_date) current_date = current_date - 1 current_year = int(current_year) current_month = int(current_month) #current_month = x.month #current_year = x.year if current_month < 10: current_month = "0"+str(current_month) m2 = int(current_month) -1 y2 = int(current_year)-1 if int(current_month) < 13 and int(current_month) > 1 : m2 = int(current_month)-1 y2 = int(current_year) else: m2 = 12 y2 = int(current_year)-1 if m2<10: m2 = "0"+str(m2) acount = 0 latest_day = " " latest_URI = " " latest_granule = " " latest_cloud_cover = " " daycount = 0 #query = ("SELECT base_url, granule_id, north_lat, south_lat, west_lon, east_lon, cloud_cover FROM `bigquery-public-data.cloud_storage_geo_index.sentinel_2_index`"' WHERE north_lat>13.035053456051692 and south_lat<13.027290157060461 and east_lon>77.78333321213722 and west_lon<77.77400583028793 and (REGEXP_CONTAINS(sensing_time,".*2019-04.*") or REGEXP_CONTAINS(sensing_time,".*2019-03.*"))') query = ("SELECT base_url, granule_id, north_lat, south_lat, west_lon, east_lon,cloud_cover FROM `bigquery-public-data.cloud_storage_geo_index.sentinel_2_index`"' WHERE north_lat>'+str(field_max_lat)+' and south_lat<'+str(field_min_lat)+' and east_lon>'+str(field_max_long)+' and west_lon<'+str(field_min_long)+' and (REGEXP_CONTAINS(sensing_time,".*'+str(current_year)+'-'+str(current_month)+'.*") or REGEXP_CONTAINS(sensing_time,".*'+str(y2)+'-'+str(m2)+'.*"))') #query = ("SELECT base_url, granule_id, north_lat, south_lat, west_lon, east_lon, cloud_cover FROM `bigquery-public-data.cloud_storage_geo_index.sentinel_2_index`"' WHERE north_lat>'+field_max_lat+' and south_lat<'+field_min_lat+' and east_lon>'+field_max_long+' and west_lon<'+field_min_long+' and (REGEXP_CONTAINS(sensing_time,".*'+current_year+'-'+current_month+'.*") or REGEXP_CONTAINS(sensing_time,".*'+y2+'-'+m2+'.*"))') query_job = client.query(query) # API request - starts the query temp_map_obj = {} temp_map_obj["FieldMaxLat"]=field_max_lat temp_map_obj["FieldMinLat"]=field_min_lat temp_map_obj["FieldMaxLong"]=field_max_long temp_map_obj["FieldMinLong"]=field_min_long temp_map_obj["FieldArea"]=field_area print(x) print(latestsensedday) print('mm') for row in query_job: # API request - fetches results # Row values can be accessed by field name or index #assert row[0] == row.name == row["name"] #print(row) #print(row) temp_URI = row["base_url"] temp_day = temp_URI[57:65] int_temp_day = int(temp_day) temp_granule = row["granule_id"] temp_cloud_cover = row["cloud_cover"] #temp_mgrs = row["mgrs"] if daycount < 1: latest_day = temp_day latest_URI = temp_URI latest_granule = temp_granule latest_cloud_cover = temp_cloud_cover #latest_mgrs = temp_mgrs temp_map_obj["TileMaxLat"]=row["north_lat"] temp_map_obj["TileMaxLong"]=row["east_lon"] temp_map_obj["TileMinLat"]=row["south_lat"] temp_map_obj["TileMinLong"]=row["west_lon"] daycount=daycount+1 # print(latest_day) print(int_temp_day) if (int_temp_day >= int(x)) and (int_temp_day < int(latestsensedday)): #if temp_day >= latest_day : latest_day = temp_day latest_URI = temp_URI latest_granule = temp_granule latest_cloud_cover= temp_cloud_cover #latest_mgrs = temp_mgrs daycount = daycount+1 temp_map_obj["cloud_cover"]=latest_cloud_cover temp_map_obj["TileMaxLat"]=row["north_lat"] temp_map_obj["TileMaxLong"]=row["east_lon"] temp_map_obj["TileMinLat"]=row["south_lat"] temp_map_obj["TileMinLong"]=row["west_lon"] #temp_mag_obj["MGRS"] = latest_mgrs # print(latest_day) if aa==1: image_nameb4 = latest_granule[4:10]+"_"+latest_URI[57:72]+"_B04.jp2" image_nameb8 = latest_granule[4:10]+"_"+latest_URI[57:72]+"_B08.jp2" final_band4_uri = "https://storage.googleapis.com/"+latest_URI[5:]+ "/GRANULE/"+latest_granule+"/IMG_DATA/"+image_nameb4 #final_band4_uri = latest_URI+ "/GRANULE/"+latest_granule+"/IMG_DATA/"+image_nameb4 #print(final_band4_uri) final_band8_uri = "https://storage.googleapis.com/"+latest_URI[5:]+ "/GRANULE/"+latest_granule+"/IMG_DATA/"+image_nameb8 temp_map_obj["Band4URI"]=final_band4_uri temp_map_obj["LatestDay"]=latest_day #db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').child(latest_day).set('yes') temp_map_obj["UID"] =uid temp_map_obj["MGRS"]=latest_granule[5:10] print(temp_map_obj) return temp_map_obj