def find_img_value(fieldid, satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num, max_dim, day_gap): from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection from sentinelhub import SHConfig from firebase_admin import credentials from firebase_admin import db from statistics import mean from PIL import Image, ImageFilter import os.path import PIL import numpy as np import time import traceback #from scipy import ndimag config = SHConfig() print(imageType) img_values_arr = [] cred = credentials.Certificate('servicekey2.json') access_key = 'AKIAIPCM5ZR7FRHMY3MA' secret_key = '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' if CLIENT_ID and CLIENT_SECRET: config.sh_client_id = CLIENT_ID config.sh_client_secret = CLIENT_SECRET config.instance_id = INSTANCE_ID #img_width = 30 search_bbox = BBox(bbox=bounds, crs=CRS.WGS84) search_time_interval = (from_date, to_date) avg_im = 0 i = 0 i_num =0 prev_date = '90101010' iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=1.0,config=config) for tile_info in iterator_wms: if i_num %day_gap == 0: print(tile_info["properties"]["date"]) try: s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],width=max_dim,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() except: s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],height=max_dim,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() im = s1_data[-1] z_num = 0 #time.sleep(900000) for p in im: for q in p: if q == 0: z_num = z_num + 1 w,h = len(im[0]), len(im) new_date = tile_info["properties"]["date"] prev_day = prev_date.replace("-","") new_day = new_date.replace("-","") #print([prev_day, new_day]) multiplier = 1 if fieldid == 'study': multiplier = 1 if z_num <= int(multiplier*w*h): if int(prev_day) - int(new_day)>1: #print(new_day) prev_date = new_date pixel_values = [] z_num = 0 for p in range(0,h-1): for q in range(0,w-1): pixel_value = im[p,q] pixel_values.append(pixel_value) avg_im = round(100*np.mean(pixel_values))/100 img_values_arr.append(avg_im) i = i+1 i_num = i_num + 1 adda = 1 #print(avg_im) time.sleep(0.25) #print('im') #print(img_values_arr) # try: # min_p, max_p = min(img_values_arr), max(img_values_arr) # for pixel_val in img_values_arr: # if max_p > 0 and min_p ==0: # if pixel_val == 0: # if len(img_values_arr)>max_num: # try: # img_values_arr = img_values_arr.tolist() # img_values_arr.remove(pixel_val) # img_values_arr = np.ndarray(img_values_arr) # except Exception as e: # print(e) # except Exception as e: # print(e) if len(img_values_arr) > max_num: img_values_arr = img_values_arr[0:max_num] #print(img_values_arr) if len(img_values_arr) < max_num: print(img_values_arr) return None else: if fieldid == 'study': return pixel_values else: return img_values_arr