from test import downloadImg, getAllFields, getTestFileName, updateData from create_instructions import isCloudyImg, getRedZones # clean zones & cloudy def clearZonesOfAllFields(uid): allFields = getAllFields(uid) # print(allFields) m = 0 for fieldID, data in allFields.items(): m += 1 if m % 100 == 0: print(fieldID, m) # if m < 100: # continue # print(fieldID) # if fieldID == '1680665632852': # continue sensed_days_obj = data["SensedDays"] for lastDate, latest_day_obj in sensed_days_obj.items(): #latest_day_obj = data["SensedDays"] # print(latest_day_obj) # latest_sar_days_obj =data.get("SARDays",{}) # print(latest_sar_days_obj) #dates = list(latest_day_obj.keys()) # sar_dates = list(latest_sar_days_obj.keys()) # print(dates, sar_dates) # lastSARDate = sar_dates[len(sar_dates) - 1] if len(sar_dates) > 0 else None #lastDate = dates[len(dates) - 1] if len(dates) > 0 else None if lastDate == None: continue # if lastSARDate == None: # continue # print("1") # get ndvi, tci; then isCloudy try: downloadImg(uid, fieldID, lastDate, "ndvi") downloadImg(uid, fieldID, lastDate, "tci") except: isCloudy = True ndviPath = getTestFileName(uid, "ndvi") tciPath = getTestFileName(uid, "tci") try: isCloudy = isCloudyImg(tciPath, ndviPath) except: isCloudy = True # print("2") # isCloudy2 = ( # None # if type(latest_day_obj[lastDate]) == str # else latest_day_obj[lastDate].get("isCloudy", None) # ) # check is isCloudy is same if True: # isCloudy2 != isCloudy: # get new zones & udpate it to DB health = data["Health"] ndviAvgVal = health["ndvi"][lastDate] ndwiAvgVal = health["ndwi"][lastDate] # print("ndvi, ndmi: ", ndviAvgVal, ndwiAvgVal) isSmallPlant = float(ndviAvgVal) < 40 and float(ndwiAvgVal) < 40 if isCloudy: if False: # get rvi, rsm cmap2 downloadImg(uid, fieldID, lastDate, "rvi_cmap2") downloadImg(uid, fieldID, lastDate, "rsm_cmap2") cropPath = getTestFileName(uid, "rvi_cmap2") irriPath = getTestFileName(uid, "rsm_cmap2") else: # get rvi, rsm # if lastSARDate == None: # print("sar date is none", fieldID) # continue try: downloadImg(uid, fieldID, lastDate, "rvi") downloadImg(uid, fieldID, lastDate, "rsm") except: print("rvi not found", fieldID, lastDate) continue cropPath = getTestFileName(uid, "rvi") irriPath = getTestFileName(uid, "rsm") else: if isSmallPlant: # get ndvi, ndwi cmap2 downloadImg(uid, fieldID, lastDate, "ndvi_cmap2") downloadImg(uid, fieldID, lastDate, "ndwi_cmap2") cropPath = getTestFileName(uid, "ndvi_cmap2") irriPath = getTestFileName(uid, "ndwi_cmap2") else: # get ndwi downloadImg(uid, fieldID, lastDate, "ndwi") ndwiPath = getTestFileName(uid, "ndwi") cropPath = ndviPath irriPath = ndwiPath # print("starting redzones cal") cropZones = getRedZones(cropPath, isCmap2=isSmallPlant and not isCloudy) irriZones = getRedZones(irriPath, isCmap2=isSmallPlant and not isCloudy) finalData = { "cropRedZones": "empty" if len(cropZones) == 0 else cropZones, "irrigationRedZones": "empty" if len(irriZones) == 0 else irriZones, "isCloudy": isCloudy, } # print(finalData, isSmallPlant) updateData(f"{uid}/{fieldID}/SensedDays/{lastDate}", finalData) uid = "M53J9SImW9My4vghlCaaWLYxsc22" uid = "sAiJIWRbNOR5grDmRabVxW5BXp03" uid = "OvNqMOrqpKQeaRsj2ByVx2AABK92" uid = "6U2L0EV2mKXXh7xmQBlbyxTgltd2" uid = "ENLgcB9pl2fH7MjsEH7oc72YBRh2" clearZonesOfAllFields(uid)