def search_sentinel_again(uid, fieldid, latestsensedday, diff_count, latestfailedday): from sentinelhub import WebFeatureService, BBox, CRS, DataSource from sentinelhub import get_area_info from sentinelhub import AwsTile 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 from sentinelhub import AwsTileRequest import numpy as np from get_prev_date import get_prev_date aa = 1 cred = credentials.Certificate("servicekey2.json") 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 # Initialize the app with a service account, granting admin privileges # firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) min_lat = field_min_lat max_lat = field_max_lat min_long = field_min_long max_long = field_max_long # access_key = 'AKIAIIS7SFB6UY7NYPUQ' # secret_key = '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' access_key = "AKIAQZCF7Q5CIN2VIHVL" secret_key = "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" INSTANCE_ID = "e2e8adb2-a193-4774-af3a-062f53fdba15" # case you put instance ID into cofniguration file you can leave this unchanged search_bbox = BBox(bbox=[min_long, min_lat, max_long, max_lat], crs=CRS.WGS84) # search_bbox = BBox(bbox=[32.64358939614938,35.23309160023928,32.65433485229119,35.2399842068553], crs=CRS.WGS84) x = get_prev_date(latestsensedday, diff_count) current_year = x[0:4] current_month = x[4:6] current_date = x[6:] current_date = int(current_date) current_year = int(current_year) current_month = int(current_month) if current_date < 5 and current_month > 1: start_date = 31 - abs(current_date) start_month = current_month - 1 start_year = current_year elif current_date < 5 and current_month == 1: start_date = 31 - abs(current_date) start_month = 12 start_year = start_year - 1 else: start_date = abs(current_date) - 4 start_month = current_month start_year = current_year if current_month < 10: current_month = "0" + str(current_month) else: current_month = str(current_month) if start_month < 10: start_month = "0" + str(start_month) else: start_month = str(start_month) if current_date < 10: current_date = "0" + str(current_date) else: current_date = str(current_date) if start_date < 10: start_date = "0" + str(start_date) else: start_date = str(start_date) start_year = str(start_year) from_date = start_year + "-" + start_month + "-" + str(start_date) to_date = str(current_year) + "-" + current_month + "-" + str(current_date) l_date = str(current_year) + current_month + str(current_date) from_date = from_date + "T00:00:00" to_date = to_date + "T23:59:59" print(from_date) print(to_date) search_time_interval = (from_date, to_date) print(search_time_interval) tile_num = 0 for tile_info in get_area_info(search_bbox, search_time_interval, maxcc=10.0): print(tile_info) temp_tile = tile_info geom = temp_tile["geometry"] coords = geom["coordinates"] new_coords = coords[0][0] coord_num = 0 print("coo") print(new_coords) coord_len = len(new_coords) print(coord_len) coord_iter = 0 west_lons = [] east_lons = [] north_lats = [] south_lats = [] all_lons = [] for kk in new_coords: latlng = new_coords[coord_iter] longitude = latlng[0] all_lons.append(longitude) if coord_iter < 2: longitude = latlng[0] west_lons.append(longitude) elif coord_iter > 1 and coord_iter < 4: longitude = latlng[0] east_lons.append(longitude) if coord_iter == 0 or coord_iter == 3: latitude = latlng[1] north_lats.append(latitude) elif coord_iter == 1 or coord_iter == 2: latitude = latlng[1] south_lats.append(latitude) coord_iter = coord_iter + 1 all_lons.sort() all_lons = np.unique(all_lons) print("all_lons") print(all_lons) west_lons = [] east_lons = [] west_lons.append(all_lons[0]) west_lons.append(all_lons[1]) east_lons.append(all_lons[(len(all_lons) - 2)]) east_lons.append(all_lons[(len(all_lons) - 1)]) print(west_lons) print(east_lons) print(north_lats) print(south_lats) west_lon = min(west_lons) east_lon = max(east_lons) min_east_lon = min(east_lons) north_lat = max(north_lats) south_lat = min(south_lats) if min_east_lon > max_long: tile_id = tile_info["properties"]["title"] temp_map_obj = {} A = new_coords[0] A_lat = A[1] A_long = A[0] B = new_coords[1] B_lat = B[1] B_long = B[0] C = new_coords[2] C_lat = C[1] C_long = C[0] D = new_coords[3] D_lat = D[1] D_long = D[0] temp_map_obj["A_lat"] = A_lat temp_map_obj["A_long"] = A_long temp_map_obj["B_lat"] = B_lat temp_map_obj["B_long"] = B_long temp_map_obj["C_lat"] = C_lat temp_map_obj["C_long"] = C_long temp_map_obj["D_lat"] = D_lat temp_map_obj["D_long"] = D_long temp_map_obj["TileMaxLat"] = north_lat temp_map_obj["TileMinLat"] = south_lat temp_map_obj["TileMaxLong"] = west_lon temp_map_obj["TileMinLong"] = east_lon 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 temp_map_obj["UID"] = uid temp_map_obj["cloud_cover"] = tile_info["properties"]["cloudCover"] # coord_iter = coord_iter+1 print("tileID") print(tile_id) # tile_id = tile_info["title"] tile_name, time, aws_index = AwsTile.tile_id_to_tile(tile_id) temp_map_obj["MGRS"] = tile_name new_time = time time = time.replace("-", "") latest_date = tile_info["properties"]["startDate"] latest_date = latest_date[0:10] latest_date = latest_date.replace("-", "") temp_map_obj["LatestDay"] = latest_date bands = ["B02", "B03", "B04", "B05", "B08", "B12"] metafiles = ["tileInfo", "preview", "qi/MSK_CLOUDS_B00", "TCI"] data_folder = "./AwsData" # if int(latest_date)>int(latestsensedday): request = AwsTileRequest( tile=tile_name, time=new_time, aws_index=aws_index, bands=bands, metafiles=metafiles, data_folder=data_folder, data_source=DataSource.SENTINEL2_L1C, ) request.save_data() print("save data") return temp_map_obj temp_map_obj = {} temp_map_obj["TileMaxLat"] = "NA" temp_map_obj["TileMinLat"] = "NA" temp_map_obj["TileMaxLong"] = "NA" temp_map_obj["TileMinLong"] = "NA" 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 temp_map_obj["UID"] = uid temp_map_obj["LatestDay"] = l_date temp_map_obj["MGRS"] = "NotA" temp_map_obj["cloud_cover"] = "NA" return temp_map_obj