print('entered') import os import time import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd from fastkml import kml from socket import * from oct2py import octave from io import BytesIO import base64 import xlrd from statistics import median import math import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import threading import xlsxwriter import numpy as np import datetime import os from numba import jit, cuda from firebase_admin import firestore from make_on_the_go_farm_image import make_on_the_go_farm_image from sentinelhub import ( WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest, DataCollection, ) from get_polygon_mask import get_polygon_mask import traceback from google.cloud import storage import os from google.oauth2 import service_account import datetime from datetime import date import traceback from gen_report_new2 import gen_report_new 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 import datetime storage_client = storage.Client.from_service_account_json("servicekey.json") bucket_name = "farmbase-b2f7e.appspot.com" cred = service_account.Credentials.from_service_account_file("servicekey.json") # cred = credentials.Certificate('servicekey.json') bucket = storage_client.bucket(bucket_name) try: firebase_admin.initialize_app( credentials.Certificate("servicekey.json"), {"databaseURL": "https://farmbase-b2f7e-31c0c.firebaseio.com/"}, ) except: print("fire running") print('step 1') db_firestore = firestore.client() sentinelSettings = db.reference("SentinelSettings4").get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] images_arr = ["TCI", "NDVI", "NDWI", "NDRE", "RECL", "RVI", "RSM"] day_gap = 5 today = date.today() ndvi_val, ndwi_val = 0, 0 imageDate = today.strftime("%Y%m%d") print('step 2') kkk = 0 uid = 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2' if not os.path.exists(uid): os.makedirs(uid) all_polygons_arr = [] n_tmux = 15 print('step 3') def send_keys_tmux_session(session_num, uid, file_name): #tmux_cmd = 'tmux send-keys -t s' + str(session_num) + ' "python3.7 ' + ' satellite_polygon_server_2023.py \'' + '\'" Enter' tmux_cmd = 'tmux send-keys -t gavl' + str(session_num) + ' "python3.7 ' + ' init_sat_poly_tmux.py \'' + file_name + '\'" Enter' #tmux_cmd = 'tmux send-keys -t s' + str(session_num) + ' "python3.7 ' + ' init_sat_tmux.py ' + str(fields_obj) + '" Enter' print(tmux_cmd) os.system(tmux_cmd) print('step 4') n_dates = ['2022-07-01', '2022-07-31'] target_yyyymm_array = ['202207'] total_num = 0 while kkk == 0: temp_list = db.reference("PaidMonitoredFields").child("PMF").child(uid).get(False, True) for fieldid, tmp in temp_list.items(): polygons_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).child('Polygons').get(False, True) try: for polygonid, temp_p in polygons_obj.items(): # str_polygon_id = str(polygonid) # sensed_days_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).child('Polygons').child(str_polygon_id).child('SensedDays').get() # sensed_days_arr = [] # try: # temp_days_num = 0 # for temp_key, val in sensed_days_obj.items(): # sensed_days_arr.append(int(temp_key)) # temp_days_num = temp_days_num + 1 # except: # temp_days_num = 0 # matched_dates = {target: [] for target in target_yyyymm_array} # # Check each sensed day for every target_yyyymm # for target in target_yyyymm_array: # matched_dates[target] = [day for day in sensed_days_arr if str(day).startswith(target)] # # Output the results # for target, dates in matched_dates.items(): # if dates: # #continue # #is_data_already_processed = 1 # print(f"Sensed days containing {target}: {dates}") # #break # else: # print(f"No sensed days contain {target}") # # Flatten all matched dates and convert to 'YYYY-MM-DD' format for removal from n_dates # # Find which target_yyyymm keys had matches # keys_with_matches = [key for key, dates in matched_dates.items() if dates] # # Create a new array without the dates matching the matched YYYY-MM # temp_n_dates = [date for date in n_dates if not any(date.startswith(key[:4] + '-' + key[4:6]) for key in keys_with_matches)] # print(f"temp_n_dates: {temp_n_dates}") # if len(temp_n_dates)>0: # total_num = total_num + 1 # print(total_num) all_polygons_arr.append((fieldid, polygonid)) except: awa = 1 # with open('missing_polygons.txt') as f: # lines = f.readlines() # for line in lines: # split_line = line.split(",") # all_polygons_arr.append([str(int(split_line[0])), str(int(split_line[1]))]) interval = round(len(all_polygons_arr)/n_tmux) print([len(all_polygons_arr), interval]) print('done len') for i in range(1, (n_tmux + 1)): print('came in for') temp_polygons_obj = {} if i == n_tmux: temp_polygons_arr = all_polygons_arr[((i-1)*interval): (len(all_polygons_arr)-1)] else: temp_polygons_arr = all_polygons_arr[((i-1)*interval): (i*interval)] for j in temp_polygons_arr: temp_polygons_obj[j[1]] = j[0] file_name = "s" + str(i) + ".json" with open(file_name, 'w') as json_file: json.dump(temp_polygons_obj, json_file) send_keys_tmux_session(i,uid, file_name) print('came here1') time.sleep(60) #process_tmux_polygons(uid, temp_polygons_arr) time.sleep(864000) # for i in range(1,29): # send_keys_tmux_session(i) # time.sleep(120)