Beispiel #1
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)
    inps.work_dir = os.path.abspath(os.path.dirname(inps.ts_file))
    inps.cbar_file = os.path.join(inps.work_dir, 'google_earth_cbar.png')
    inps.star_file = os.path.join(inps.work_dir, "star.png")
    inps.dot_file = os.path.join(inps.work_dir, "shaded_dot.png")
    inps.dygraph_file = os.path.join(inps.work_dir, "dygraph-combined.js")
    inps.kml_data_dir = os.path.join(inps.work_dir, 'kml_data')

    ## Define file names
    if inps.outfile:
        inps.outfile_base = os.path.splitext(os.path.basename(inps.outfile))[0]
    else:
        inps.outfile_base = plot.auto_figure_title(inps.ts_file,
                                                   inps_dict=vars(inps))
    kml_root_file = os.path.join(inps.work_dir,
                                 '{}_root.kml'.format(inps.outfile_base))
    kmz_file = os.path.join(inps.work_dir, '{}.kmz'.format(inps.outfile_base))

    ## read data
    ts_obj = timeseries(inps.ts_file)
    ts_obj.open()
    length, width = ts_obj.length, ts_obj.width
    inps.metadata = ts_obj.metadata
    lats, lons = ut.get_lat_lon(ts_obj.metadata)
    print('input data shape in row/col: {}/{}'.format(length, width))

    vel = readfile.read(inps.vel_file, datasetName='velocity')[0] * 100.
    # Set min/max velocity for colormap
    if inps.vlim is None:
        inps.vlim = [np.nanmin(vel), np.nanmax(vel)]
    if inps.wrap:
        print('re-wrapping data to {} cm/year for color coding'.format(
            inps.vlim))

    ##--------- Create root KML file with network links to data KML files --------------##
    kml_root_doc = KML.Document()

    # 1 Create Overlay element for colorbar
    cbar_overlay = generate_cbar_element(cbar_file=inps.cbar_file,
                                         vmin=inps.vlim[0],
                                         vmax=inps.vlim[1],
                                         cmap=inps.colormap)
    kml_root_doc.append(cbar_overlay)

    # 2 Generate the placemark for the Reference Pixel
    ref_point = create_reference_point_element(inps, lats, lons, ts_obj)
    print('add reference point.')
    ref_folder = KML.Folder(KML.name("ReferencePoint"))
    ref_folder.append(ref_point)
    kml_root_doc.append(ref_folder)

    # 3 Create data folder to contain actual data elements
    data_folder = KML.Folder(KML.name("Data"))
    for i, step in enumerate(inps.steps):
        net_link = generate_network_link(inps,
                                         ts_obj,
                                         step=step,
                                         lod=(inps.lods[i], inps.lods[i + 1]))
        if net_link is not None:
            data_folder.append(net_link)
    kml_root_doc.append(data_folder)

    ##---------------------------- Write root KML file ------------------------------##
    print('-' * 30)
    print('writing ' + kml_root_file)
    kml_root = KML.kml()
    kml_root.append(kml_root_doc)
    with open(kml_root_file, 'w') as f:
        f.write(etree.tostring(kml_root, pretty_print=True).decode('utf-8'))

    ## Copy auxiliary files
    res_dir = os.path.join(os.path.dirname(mintpy.__file__), "data")
    for fname in [inps.star_file, inps.dot_file, inps.dygraph_file]:
        src_file = os.path.join(res_dir, os.path.basename(fname))
        shutil.copy2(src_file, inps.work_dir)
        print("copy {} to the local directory".format(src_file))

    ## Generate KMZ file
    # 1) go to the directory of kmz file
    run_dir = os.path.abspath(os.getcwd())
    os.chdir(inps.work_dir)
    # 2) zip all data files
    with ZipFile(kmz_file, 'w') as fz:
        kml_data_files = get_all_file_paths(inps.kml_data_dir)
        for fname in [
                kml_root_file, inps.cbar_file, inps.dygraph_file,
                inps.dot_file, inps.star_file
        ] + kml_data_files:
            fz.write(os.path.relpath(fname))
            os.remove(fname)
        shutil.rmtree(inps.kml_data_dir)
    # 3) go back to the running directory
    os.chdir(run_dir)
    print('merged all files to {}'.format(kmz_file))
    print('Done.')
    print('Open {} in Google Earth and play!'.format(kmz_file))
    return
Beispiel #2
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)
    inps.work_dir = os.path.abspath(os.path.dirname(inps.ts_file))
    inps.cbar_file = os.path.join(inps.work_dir, 'google_earth_cbar.png')
    inps.star_file = os.path.join(inps.work_dir, "star.png")
    inps.dot_file = os.path.join(inps.work_dir, "shaded_dot.png")
    inps.dygraph_file = os.path.join(inps.work_dir, "dygraph-combined.js")
    inps.kml_data_dir = os.path.join(inps.work_dir, 'kml_data')

    ## Define file names
    inps.outfile_base = plot.auto_figure_title(inps.ts_file, inps_dict=vars(inps))
    kml_master_file = os.path.join(inps.work_dir, '{}_master.kml'.format(inps.outfile_base))
    kmz_file = os.path.join(inps.work_dir, '{}.kmz'.format(inps.outfile_base))

    ## read data
    ts_obj = timeseries(inps.ts_file)
    ts_obj.open()
    length, width = ts_obj.length, ts_obj.width
    inps.metadata = ts_obj.metadata
    lats, lons = ut.get_lat_lon(ts_obj.metadata)
    print('input data shape in row/col: {}/{}'.format(length, width))

    vel = readfile.read(inps.vel_file, datasetName='velocity')[0] * 100.
    # Set min/max velocity for colormap
    if inps.vlim is None:
        inps.vlim = [np.nanmin(vel), np.nanmax(vel)]


    ##--------- Create master KML file with network links to data KML files --------------##
    kml_master_doc = KML.Document()

    # 1 Create Overlay element for colorbar
    cbar_overlay = generate_cbar_element(cbar_file=inps.cbar_file,
                                         vmin=inps.vlim[0],
                                         vmax=inps.vlim[1],
                                         cmap=inps.colormap)
    kml_master_doc.append(cbar_overlay)

    # 2 Generate the placemark for the Reference Pixel
    ref_point = create_reference_point_element(inps, lats, lons, ts_obj)
    print('add reference point.')
    ref_folder = KML.Folder(KML.name("ReferencePoint"))
    ref_folder.append(ref_point)
    kml_master_doc.append(ref_folder)

    # 3 Create data folder to contain actual data elements
    net_link1 = generate_network_link(inps, ts_obj, step=inps.steps[0], lod=(0, inps.lods[0]))
    net_link2 = generate_network_link(inps, ts_obj, step=inps.steps[1], lod=(inps.lods[0], inps.lods[1]))
    net_link3 = generate_network_link(inps, ts_obj, step=inps.steps[2], lod=(inps.lods[1], inps.lods[2]))

    # 3.3 Append network links to data folder
    data_folder = KML.Folder(KML.name("Data"))
    data_folder.append(net_link1)
    data_folder.append(net_link2)
    data_folder.append(net_link3)
    kml_master_doc.append(data_folder)


    ##---------------------------- Write master KML file ------------------------------##
    print('-'*30)
    print('writing ' + kml_master_file)
    kml_master = KML.kml()
    kml_master.append(kml_master_doc)
    with open(kml_master_file, 'w') as f:
        f.write(etree.tostring(kml_master, pretty_print=True).decode('utf-8'))

    ## Copy auxiliary files
    res_dir = os.path.join(os.path.dirname(__file__), "../docs/resources")
    for fname in [inps.star_file, inps.dot_file, inps.dygraph_file]:
        src_file = os.path.join(res_dir, os.path.basename(fname))
        shutil.copy2(src_file, inps.work_dir)
        print("copy {} to the local directory".format(src_file))

    ## Generate KMZ file
    kml_files_str = ''
    for fname in [kml_master_file, inps.kml_data_dir,
                  inps.cbar_file,inps.dygraph_file,
                  inps.dot_file, inps.star_file]:
        kml_files_str += ' {}'.format(os.path.basename(fname))
    cmd = 'cd {}; zip -r {} {}'.format(inps.work_dir, kmz_file, kml_files_str)
    print('writing {} from kml files'.format(kmz_file))
    os.system(cmd)

    ## Remove extra files from file tree after KMZ generation
    cmd = 'cd {}; rm -r {}'.format(inps.work_dir, kml_files_str)
    os.system(cmd)

    print('Done.')
    print('Open {} in Google Earth!'.format(kmz_file))
    return