def make_trial_bigquery(uid,fieldid): 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 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() 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>'+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,".*'+'2019'+'-'+'09'+'.*")') 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,".*'+'2019'+'-'+'09'+'-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>'+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 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] temp_granule = row["granule_id"] temp_cloud_cover = row["cloud_cover"] float_cloud = float(temp_cloud_cover) if daycount < 1 and (float_cloud > float('-1.0') and float_cloud= latest_day and (float_cloud > float('-1.0') and float_cloud