

import pandas as pd 
import shutil

df = pd.read_csv('Information of the labeled SAR images, including file name, labeling, swath, capture time, and centre latitude and longitude.txt',header=None, delimiter=r"\s+")

df.columns = ['fileName', 'label', 'swath', 'captureTime', 'lat', 'long']

# how many per class
print(df.label.value_counts())

df2 = df.groupby('label', group_keys=False).apply(lambda x: x.sample(min(len(x), 100)))

print(df2.label.value_counts())

# For each...
for index, row in df2.iterrows():
#     print(str(index)+"-> "+row['fileName'], row['label'])
    png = row['fileName']
    tiff = png.replace("png", "tiff")
    src = "GeoTIFF/"+tiff
    dst = "Subset/GeoTIFF/"+tiff
    shutil.copyfile(src, dst)
    src = "PNG/"+png
    dst = "Subset/PNG/"+png
    shutil.copyfile(src, dst)

print(df2)
df2.to_csv('Subset/Subset.csv',index=False)
