def find_study_area_values3(field_name,satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num): from sentinelhub import WebFeatureService, BBox, CRS, DataSource, 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 from sentinelhub import BBoxSplitter from pathlib import Path from osgeo import gdal import os from osgeo import gdal from osgeo import osr import numpy as np import os,sys import cv2 import csv import json from make_geotiff import make_geotiff 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 lat_len = (bounds[3]-bounds[1]) lng_len = bounds[2] - bounds[0] #if width > height img_width = 500 search_bbox = BBox(bbox=bounds, crs=CRS.WGS84) search_time_interval = (from_date, to_date) avg_im = 0 i = 0 i_num = 0 arr_init = 0 iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=1.0,config=config) images_arr = [] prev_date = '90101010' for tile_info in iterator_wms: try: width = 500 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],width = width, image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() except: height = 500 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],height = height,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() file_name= str(tile_info["properties"]["date"]) + str(i_num)+'.tiff' i_num = i_num + 1 #im = np.array(im) im = s1_data[-1] z_num = 0 for p in im: for q in p: if q[1] == 0: z_num = z_num + 1 #im = PIL.Image.open((field_name + '/' +file_name)) #im = np.array(im) w,h = len(im[0]), len(im) new_date = tile_info["properties"]["date"] prev_day = prev_date.replace("-","") new_day = new_date.replace("-","") file_date = new_date if z_num < int(0.5*w*h): if int(prev_day) != int(new_day): print((prev_day + ', ' + new_day)) prev_date = new_date images_arr.append(im[:,:,0]) im = PIL.Image.fromarray(s1_data[-1]) if imageType == "RVI": im.save("area_estimate_rvi.tiff") else: im.save(file_name) #print(len(images_arr), len(images_arr[0]), len(images_arr[0][0])) new_arr = [] try: for i in range(0, len(images_arr[0])): for j in range(0, len(images_arr[0][0])): temp_arr = [] for k in range(0,max_num): #print([k,i,j]) temp_arr.append(round(100*images_arr[k][i][j])/100) new_arr.append(temp_arr) except Exception as e: print(traceback.format_exc()) aaa = 1 #print(new_arr) #np.savetxt('new_arr.csv',new_arr, delimiter=",") # i_num,j_num = 0,0 # for i in new_arr: # max_i, min_i = max(i), min(i) # if max_i > 0 and min_i ==0: # j_num = 0 # for j in i: # if j == 0: # if len(i)>max_num: # i = i.tolist() # i.remove(j) # i = np.ndarray(i) # #print(i) # #print(('removing' + str(j_num))) # j_num = j_num+1 # if len(i)> max_num: # i = i[0:(max_num-1)] # new_arr[i_num] = i # i_num = i_num + 1 time.sleep(0.25) return new_arr,w,h