directory_image_rotated = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200525soy_nighttime\\image\\rotated_RGB_non_reflectance'
imu = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200525soy_nighttime\\raw\\imu.txt'
gps = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200525soy_nighttime\\raw\\gps.txt'
scanrate = 35
width = 1.4
row = 2

#Store all HS files to refer the timestamp for extracting GPS abd IMU data
os.chdir(directory_all_HS)
path = os.getcwd()
HS_files = os.listdir(directory_all_HS)
flen = len(HS_files)
print(flen)
initial_degree = ip.FirstDirection(
    HS_files[0], gps)  #initial degree means initial yaw using rotation
initial_yaw = ip.firstYaw(HS_files[0],
                          imu)  #This is for yaw angle from second image on
velocity = ip.speed(HS_files[0], HS_files[flen // row], gps)
print('velocity is %f' % velocity)

#Read png image
os.chdir(directory_image)
initial = 0
ndvi = cv2.imread('%s.png' % initial)
ndvi = cv2.rotate(
    ndvi, cv2.ROTATE_180
)  #I don't know why but completed images are opposite so modify here

#Give rotation angle to the png image
rotated_ndvi = ip.rotation(ndvi, initial_degree, scanrate, velocity, width)

os.chdir(directory_image_rotated)
kml_file_name = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200521wheat_daytime\\image\\rotated_RGB\\20200521_wheat_daytime_RGB.kml'
kml_name = 'wheat_daytime_RGB'
imu = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200521wheat_daytime\\raw\\imu.txt'
gps = 'D:\\Vebots\\My_master_research\\HSC\\hsc20200521wheat_daytime\\raw\\gps.txt'
offset_distance = 2
scanrate = 110
velocity = 0.36  #0.3でやったらなぜかアナがたくさんあいた.実際のスピードが違ったのか....?

#Make a list of all HS_files in the below directory
os.chdir(directory_all_HS)
HS_files = os.listdir(directory_all_HS)
ftype = type(HS_files)
flen = len(HS_files)

initial_degree = ip.FirstDirection(HS_files[0], gps)
initial_yaw = ip.firstYaw(HS_files[0], imu)

with open(kml_file_name, mode='w') as f:
    f.write(
        '<?xml version="1.0" encoding="UTF-8"?>\n<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">\n'
    )
    f.write('<Folder>\n')
    f.write('    <name>%s</name>\n' % kml_name)
    f.write('    <open>1</open>\n')
    os.chdir(directory_all_HS)
    velocity = ip.speed(HS_files[0], HS_files[1], gps)
    coordinate = ip.imageCoordinate_for_modify(HS_files[0], gps,
                                               offset_distance, initial_degree,
                                               scanrate, velocity)
    imagename = '0.png'
    f.write('    <GroundOverlay>\n')