def read_pixel_data(field_no):
    startDate, endDate = "2017-05-01", "2017-09-30"
    
    arrays = reader.parseKML("2017_polygons.kml")
    array = arrays[field_no]
    data, dates  = getSAR.arrayToSAR(array, startDate, endDate, returnDates=True)
    
    res_date, res_px = ([], [])
    for i in range(0, len(data), 2):
        a1 = data[i][0][data[i][0] != 0]
        a2 = data[i+1][0][data[i+1][0] != 0]
            
        tmp = np.array([[np.mean(a1), np.mean(a2)]])
        res_px.append(tmp)
        res_date.append(f'{(dates[i//2]):%B %d, %Y}')

    return res_date, res_px
def getDataset():
    Xs = []
    ys = []
    for year in ["2017", "2018", "2019"]:
        counter = 0
        fields = parseKML(year + "_polygons.kml")
        total = str(len(fields))
        for array in fields:
            try:
                X, y = pixelPairs(array, year + "-05-01", year + "-09-30", CLOUDY_PIXEL_PERCENTAGE=100)
                Xs.append(X)
                ys.append(y)
                # print([np.concatenate(Xs), np.concatenate(ys)])
            except ee.ee_exception.EEException as e:
                # TODO: fix exception (exception is related to some empty ee.Array)
                print(e)
                print("Exception occurred, skipping")
            print(year + ": " + str(counter) + "/" + total)
            counter += 1

    return np.concatenate(Xs), np.concatenate(ys)
def generateFieldImages(field_no, regr_model, year="2017"):
    try:
        startDate, endDate = f"{year}-05-01", f"{year}-09-30"
        arrays = reader.parseKML(f"{year}_polygons.kml")
        array = arrays[field_no]
        data, mask = getPair.arrayToPairs(array, startDate, endDate, False, 0.5)
        ndvi_images = [data[i][0] for i in range(0, len(data), 3)]
        vv_images = [data[i+1][0] for i in range(0, len(data), 3)]
        vh_images = [data[i+2][0] for i in range(0, len(data), 3)]
        
        for i in range(len(ndvi_images)):
            print(f"Processing image #{i+1}...")
            y = imagePredict(regr, vv_images[i], vh_images[i], mask)
    
            ndvi_mes, ndvi_pred = ndvi_images[i], y
            
            img_mes, img_pred = fieldToImages(ndvi_mes, ndvi_pred)
            #img_mes.show()
            #img_pred.show()
            img_mes.save(f"images/field_NDVI/{year}/measured/field{field_no+1}_{i+1}.jpg", "JPEG")
            img_pred.save(f"images/field_NDVI/{year}/predicted/field{field_no+1}_{i+1}.jpg", "JPEG")
    except ValueError:
        return
import ee

import model
from pixelPairs import pixelPairs
from reader import parseKML

if __name__ == "__main__":
    # Trigger the authentication flow.
    # ee.Authenticate()

    # Initialize the library.
    ee.Initialize()
    for array in parseKML("2017_polygons.kml"):
        X, y = pixelPairs(array, "2017-05-01", "2017-09-30")
        if len(X):
            model.train_and_test(X, y)
        [common.fastLatLonImg(ee.Image(l_NDVI.get(NDVI)), area) for NDVI in sorted_pairs])
    LatLonImgsSAR = ee.List([LatLonImgVHVV(ee.Image(l_SAR.get(SAR)), area) for SAR in range(SAR_size)])
    both_lists = ee.List([LatLonImgsNDVI, LatLonImgsSAR]).getInfo()
    LatLonImgsNDVI = both_lists[0]
    LatLonImgsSAR = both_lists[1]

    for NDVI in range(len(LatLonImgsNDVI)):
        i = sorted_pairs[NDVI]
        for SAR in pairs_i[i]:
            ndvi_temp = (LatLonImgsNDVI[NDVI][0], LatLonImgsNDVI[NDVI][1], LatLonImgsNDVI[NDVI][2])
            if SAR not in precomputed_SAR:
                lats = LatLonImgsSAR[SAR][0]
                lons = LatLonImgsSAR[SAR][1]
                vh = LatLonImgsSAR[SAR][2]
                vv = LatLonImgsSAR[SAR][3]
                precomputed_SAR[SAR] = []
                precomputed_SAR[SAR].append((lats, lons, vh) + (f'SAR (VH) {l_SAR_dates[SAR]:%B %d, %Y}',))
                precomputed_SAR[SAR].append((lats, lons, vv) + (f'SAR (VV) {l_SAR_dates[SAR]:%B %d, %Y}',))
            arr.append(ndvi_temp + (f'NDVI {l_NDVI_dates[NDVI]:%B %d, %Y}',))
            arr.extend(precomputed_SAR[SAR])

    return rasteriser.rasteriseImages(arr)


if __name__ == "__main__":
    import reader

    for array in reader.parseKML("2017_polygons.kml"):
        p = arrayToPairs(array, "2017-05-01", "2017-09-30")
        common.plot(p[0])