#!/usr/bin/python

"""
overwrite_kml_latlonbox.py

Writes new geographic bounds to a KML file's Ground Overlay LatLonBox

This was needed because a change in CW-utilities 3.3.2.2299 caused
the script nrt_daily_google_earth.sh to write the LatLonBox coords
incorrectly, so PNG images wouldn't overlay on the Google Map display.

The coords for each Google Map region are hardcoded below.

Ron Vogel
Dec 11, 2017
"""


open_tag = [ "<north>", "<south>", "<east>", "<west>" ]
close_tag = [ "</north>", "</south>", "</east>", "</west>" ]

regions = [ "cd", "cl", "fg", "gm", "mr", "nl" ]

coords_by_region = [ 
  [ "39.8169", "37.1469", "32.4028", "46.1967", "43.4833", "41.6635" ],
  [ "36.5431", "30.5994", "24.9278", "41.4087", "40.1083", "38.6955" ],
  [ "-74.6496", "-74.3084", "-76.3578", "-63.3775", "-68.6736", "-71.2987" ],
  [ "-77.3496", "-81.5647", "-81.6878", "-71.0725", "-72.1736", "-75.0087" ] ]

ecn_disk34 = '/disks/data034/data'
ecn_disk35 = '/disks/data035/data'

directories = [ '/disks/data034/data/modis/chl-swir/ge_kml/', 
                '/disks/data034/data/modis/k490noaa/ge_kml/',
                '/disks/data034/data/modis/true250/ge_kml/',
                '/disks/data034/data/modis/tsm/ge_kml/',
                '/disks/data035/data/viirs/true750/ge_kml/',
                '/disks/data035/data/viirs/rrs672/ge_kml/',
                '/disks/data035/data/avhrr/sst/ge_kml/' ]

# different regions for each product, so using a for loop for each product
beg = [ 0, 0, 0, 0, 1, 1, 0 ]
end = [ 5, 5, 0, 0, 5, 5, 5 ]

for prod in range(0,5+1):

    for reg in range(beg[prod],end[prod]):

        region = regions[reg]

        directory = directories[prod] + region

        files = os.listdir(directory)

        for file in files:

            # example file: fg_tur_20170904.kml

            year = file[7:11]   ???? indexing correct?
            mon = file[11:13]
            day = file[13:15]

            if year == '2017':
               if mon == '11' and day >= '16' or mon == '12' and day <= '2':

                 ????? how to find correct line in file to overwrite?

                      method acting on file to write mulitple lines
                      ----------

                 for compass in range[0,4]:
                                                     index row:col or col:row?
                                                     -------------------------
                     new_n_coord = open_tag[compass]+coords_by_region[compass,region]+close_tag[compass]
                     new_s_coord =
                     new_e_coord =
                     new_w_coord =

                     file.writelines([new_n_coord, new_s_coord, new_e_coord, new_w_coord])

                     ???? how to _overwrite_ lines? need to _delete_ old linesa

?
