from tempfile import tempdir from unittest.mock import sentinel import firebase_admin from firebase_admin import credentials from firebase_admin import db from find_s1_image2 import find_img_value import pandas as pd import statsmodels.api as sm import seaborn as sns sns.set() import time import json import numpy as np from sklearn.cluster import KMeans from find_study_area_values import find_study_area_values from make_area_estimate_image import make_area_estimate_image import json from sentinelhub import WebFeatureService, BBox, CRS, DataSource, MimeType, CRS, BBox, WmsRequest,DataCollection cred = credentials.Certificate('servicekey.json') x = 0.0003 y = 0.0003 fromdate = '20220701' todate = '20220715' s1_images = ['IW-VH-DB'] s1_images = ['B01'] s2_images = [] # s1_images = ["B02", "B03", "B04", "B05"] #s2_images = ["RVI-NEW"] try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') study_uid = "vPM6EKWHfHRxmRfc0YJ7PZ8J9tR2" gt_uid = ['u5ZgAskSltX7dQdNFUlHsgVhAlf2', 'b2j92LjidjeGt2b60QHhSj4GeS63', 'hSoBF0u61YfFeBFzcjhUFU3dh0e2','0AC0f8JWjUdg9fCm7f8QbXZTOp23'] different_gt_uid = 'hxA3VYEFpaOluurIbHxdzd4gPlw1' study_field_id = '1662365839021' gt_fields = {} for temp_id in gt_uid: temp_gt_fields = db.reference('PaidMonitoredFields').child('PMF').child(temp_id).get() for(p,q) in temp_gt_fields.items(): gt_fields[p] = q #gt_fields = db.reference('PaidMonitoredFields').child('PMF').child(gt_uid).get() different_gt_fields = db.reference('PaidMonitoredFields').child('PMF').child(different_gt_uid).get() study_fields = db.reference('PaidMonitoredFields').child('PMF').child(study_uid).get() for (p,q) in study_fields.items(): study_field = q x = 0.0003 y = 0.0003 sentinelSettings = db.reference('SentinelSettings').get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] modisID = sentinelSettings["MODISID"] field_num = 0 from_date = '2022-06-25T00:00:00' to_date = '2022-09-17T23:59:59' max_num = 7 def append_img_vals(temp_arr, img_values): if temp_arr is not None: for k in temp_arr: img_values.append(k) return img_values all_fields_img_values = [] gt_fields_img_values = [] i = 0 i = 0 labels = [] for (fieldid, fieldobj) in gt_fields.items(): if i < 150: img_values = [] midlat = (float(fieldobj["FieldMinLat"]) + float(fieldobj["FieldMaxLat"]))/2 midlng = (float(fieldobj["FieldMinLong"]) + float(fieldobj["FieldMaxLong"]))/2 bounds = [(midlng-y),(midlat-x),(midlng+y),(midlat+x)] print(bounds) image_num = 0 for image in s2_images: temp_arr = find_img_value(fieldid,DataCollection.SENTINEL2_L1C,image, bounds, from_date, to_date, clientID, clientSecret, wmsID, max_num) img_values = append_img_vals(temp_arr, img_values) for image in s1_images: temp_arr = find_img_value(fieldid,DataCollection.MODIS, image, bounds, from_date, to_date, clientID, clientSecret, modisID, max_num) img_values = append_img_vals(temp_arr, img_values) if len(img_values) > 0: print(i) all_fields_img_values.append(img_values) gt_fields_img_values.append(img_values) labels.append(10) i = i+1 else: break i = 0 for (fieldid, fieldobj) in different_gt_fields.items(): if i < 40: img_values = [] midlat = (float(fieldobj["FieldMinLat"]) + float(fieldobj["FieldMaxLat"]))/2 midlng = (float(fieldobj["FieldMinLong"]) + float(fieldobj["FieldMaxLong"]))/2 bounds = [(midlng-y),(midlat-x),(midlng+y),(midlat+x)] print(bounds) image_num = 0 for image in s2_images: temp_arr = find_img_value(fieldid,DataCollection.SENTINEL2_L1C,image, bounds, from_date, to_date, clientID, clientSecret, wmsID, max_num) img_values = append_img_vals(temp_arr, img_values) for image in s1_images: temp_arr = find_img_value(fieldid, DataCollection.MODIS, image, bounds, from_date, to_date, clientID, clientSecret, modisID, max_num) img_values = append_img_vals(temp_arr, img_values) if len(img_values) > 0: print(i) all_fields_img_values.append(img_values) labels.append(-1) i = i+1 else: break print(all_fields_img_values) print(gt_fields_img_values) np.savetxt("modis_paddy_all_field_img_values.csv", all_fields_img_values, delimiter = ",") np.savetxt("modis_paddy_gt_field_img_values.csv", gt_fields_img_values, delimiter = ",") np.savetxt("modis_paddy_all_field_labels.csv", labels, delimiter = ",")