示例#1
0
文件: valid.py 项目: kant/pycondor
def main(condor_path, filenames):
    basepath = os.path.dirname(__file__)
    if condor_path=="":
        condor_path = paths_default['Condor']
    d_name = paths_default.copy()
    filenames = filenames.format(**d_name)      
    valicon_dll = ValiConDLL(condor_path)
    
    filenames = glob.glob(filenames)
    N_filenames = len(filenames)
    
    df_valid = pd.DataFrame(index=np.arange(N_filenames), columns=["Filename", "Valid"])

    for i, filename in enumerate(filenames):
        valid = valicon_dll.validate(filename)
        df_valid["Filename"][i] = os.path.basename(filename)
        df_valid["Valid"][i] = valid
        
    print(df_valid)
    
    df_valid.to_excel(os.path.join(basepath, "valid.xls"))
示例#2
0
def main(debug, fpl_filename, output, outdir, condor_path, landscape, disp,
         fixencoding, encoding_in, encoding_errors):
    basepath = os.path.dirname(__file__)
    #basepath = os.path.dirname(os.path.abspath(__file__))

    if outdir == '':
        outdir = os.path.join(basepath, 'out')
    if condor_path == '':
        condor_path = paths_default['Condor']

    # Translate path like {Condor} using paths_default
    d_name = paths_default.copy()
    d_name['output'] = output
    fpl_filename = fpl_filename.format(**d_name)
    outdir = outdir.format(**d_name)

    if not os.path.exists(outdir):
        print("Create directory %s" % outdir)
        os.makedirs(outdir)

    lst_errors = []
    d_landscapes = {}
    d_landscapes_missing = {}
    filenames = glob.glob(fpl_filename)
    N_filenames = len(filenames)
    #i = -1
    navicon_dll = NaviConDLL(condor_path)

    print("")
    print("=" * 20)
    print("")

    for i, filename in enumerate(filenames):
        try:
            logging.info("Read file %03d/%03d '%s'" %
                         (i + 1, N_filenames, filename))

            filename_base, filename_ext = os.path.splitext(
                os.path.basename(filename))
            if debug:
                assert filename_ext in supported_input_extensions, \
                "File extension of '%s' is '%s' but supported extension must be in %s" \
                % (filename, filename_ext, supported_input_extensions)

            config = configparser.ConfigParser()
            #with open(filename, 'r') as fd: # fix \xef\xbb[Version]\n
            config.read(filename)
            try:
                condor_version = config.get('Version', 'Condor version')
            except:
                logging.error("Can't get 'Version/Condor version'")
                condor_version = None
            if debug:
                assert condor_version in supported_versions, \
                    "[Version] Condor version '%s' is not a supported version - must be in %s" \
                    % (condor_version, supported_versions)

            print("Condor version: %s" % condor_version)

            if landscape == '':  # =landscape_forced
                fpl_landscape = config.get('Task', 'Landscape')
                if fpl_landscape not in d_landscapes.keys():
                    d_landscapes[fpl_landscape] = set()
                d_landscapes[fpl_landscape].add(filename_base)

            df_task = create_task_dataframe(config)

            try:
                navicon_dll.init(fpl_landscape)
            except:
                if fpl_landscape not in d_landscapes_missing.keys():
                    d_landscapes_missing[fpl_landscape] = set()
                d_landscapes_missing[fpl_landscape].add(filename_base)
                raise (Exception("Can't init %s" % fpl_landscape))

            max_x, max_y = navicon_dll.xy_max()

            if disp:
                print("MaxX: %f" % max_x)
                print("MaxY: %f" % max_y)

                (x, y) = (0, 0)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                (x, y) = (max_x, 0)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                (x, y) = (max_x, max_y)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                (x, y) = (0, max_y)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                print("")

            df_task["Lat"] = 0.0
            df_task["Lon"] = 0.0

            for j, tp in df_task.iterrows():
                pos_x, pos_y = tp['PosX'], tp['PosY']
                (lat, lon) = navicon_dll.xy_to_lat_lon(pos_x, pos_y)
                df_task.loc[j, 'Lat'] = lat
                df_task.loc[j, 'Lon'] = lon

            settings_task = SettingsTask()

            #df_task["Comment"] = ""
            #df_task["Wpt_id"] = df_task.index.map(lambda i: "_" + str(i))

            df_task = add_distance_bearing(df_task)
            df_task = add_observation_zone(settings_task, df_task)

            if fixencoding:
                df_task['Name'] = df_task['Name'].map(
                    lambda s: s.decode(encoding_in, errors=encoding_errors))

            if disp:
                print(df_task)
                #print(df_task.dtypes)
            print("Waypoints: %d" % len(df_task))

            output_task_from_df(df_task, filename_base, output, outdir, disp)

            plt.show()

        except:
            logging.error(traceback.format_exc())
            lst_errors.append(filename)

        print("")
        print("=" * 20)
        print("")

    #N_filenames = i + 1
    N_error = len(lst_errors)
    print("Convert %d files - %d errors" % (N_filenames, N_error))
    for i, filename_error in enumerate(lst_errors):
        print(" * %03d / %03d: %s" % (i + 1, N_error, filename_error))
    print("Landscapes (from fpl):")

    import pprint

    p = pprint.PrettyPrinter(indent=4)
    p.pprint(d_landscapes)

    print("")
    if len(d_landscapes_missing) > 0:
        print("Missing landscapes (from fpl):")
        p.pprint(d_landscapes_missing)
示例#3
0
def main(debug, fpl_filename, output, outdir, condor_path,
        landscape, disp, fixencoding, encoding_in, encoding_errors):
    basepath = os.path.dirname(__file__)
    #basepath = os.path.dirname(os.path.abspath(__file__))
    
    if outdir=='':
        outdir = os.path.join(basepath, 'out')
    if condor_path=='':
        condor_path = paths_default['Condor']
    
    # Translate path like {Condor} using paths_default
    d_name = paths_default.copy()
    d_name['output'] = output
    fpl_filename = fpl_filename.format(**d_name)
    outdir = outdir.format(**d_name)
    
    if not os.path.exists(outdir):
        print("Create directory %s" % outdir)
        os.makedirs(outdir)
    
    lst_errors = []
    d_landscapes = {}
    d_landscapes_missing = {}
    filenames = glob.glob(fpl_filename)
    N_filenames = len(filenames)
    #i = -1
    navicon_dll = NaviConDLL(condor_path)

    print("")
    print("="*20)
    print("")    
    
    for i, filename in enumerate(filenames):
        try:
            logging.info("Read file %03d/%03d '%s'" % (i+1, N_filenames, filename))

            filename_base, filename_ext = os.path.splitext(os.path.basename(filename))
            if debug:        
                assert filename_ext in supported_input_extensions, \
                "File extension of '%s' is '%s' but supported extension must be in %s" \
                % (filename, filename_ext, supported_input_extensions)

            config = configparser.ConfigParser()
            #with open(filename, 'r') as fd: # fix \xef\xbb[Version]\n
            config.read(filename)
            try:
                condor_version = config.get('Version', 'Condor version')
            except:
                logging.error("Can't get 'Version/Condor version'")
                condor_version = None
            if debug:
                assert condor_version in supported_versions, \
                    "[Version] Condor version '%s' is not a supported version - must be in %s" \
                    % (condor_version, supported_versions)

            print("Condor version: %s" % condor_version)
        
            if landscape=='':  # =landscape_forced
                fpl_landscape = config.get('Task', 'Landscape')
                if fpl_landscape not in d_landscapes.keys():
                    d_landscapes[fpl_landscape] = set()
                d_landscapes[fpl_landscape].add(filename_base)

            df_task = create_task_dataframe(config)
        
            try:
                navicon_dll.init(fpl_landscape)
            except:
                if fpl_landscape not in d_landscapes_missing.keys():
                    d_landscapes_missing[fpl_landscape] = set()
                d_landscapes_missing[fpl_landscape].add(filename_base)
                raise(Exception("Can't init %s" % fpl_landscape))

            max_x, max_y = navicon_dll.xy_max()
        
            if disp:
                print("MaxX: %f" % max_x)
                print("MaxY: %f" % max_y)

                (x, y) = (0, 0)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))
            
                (x, y) = (max_x, 0)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                (x, y) = (max_x, max_y)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                (x, y) = (0, max_y)
                (lat, lon) = navicon_dll.xy_to_lat_lon(x, y)
                print("XYToLat(%f,%f): %f" % (x, y, lat))
                print("XYToLon(%f,%f): %f" % (x, y, lon))

                print("")
        
            df_task["Lat"] = 0.0
            df_task["Lon"] = 0.0
        
            for j, tp in df_task.iterrows():
                pos_x, pos_y = tp['PosX'], tp['PosY']
                (lat, lon) = navicon_dll.xy_to_lat_lon(pos_x, pos_y)
                df_task.loc[j,'Lat'] = lat
                df_task.loc[j,'Lon'] = lon
            
            settings_task = SettingsTask()

            #df_task["Comment"] = ""
            #df_task["Wpt_id"] = df_task.index.map(lambda i: "_" + str(i))

            df_task = add_distance_bearing(df_task)
            df_task = add_observation_zone(settings_task, df_task)
            
            if fixencoding:
                df_task['Name'] = df_task['Name'].map(lambda s: s.decode(encoding_in, errors=encoding_errors))
            
            if disp:
                print(df_task)
                #print(df_task.dtypes)
            print("Waypoints: %d" % len(df_task))
        
            output_task_from_df(df_task, filename_base, output, outdir, disp)

            plt.show()

        except:
            logging.error(traceback.format_exc())
            lst_errors.append(filename)

        print("")
        print("="*20)
        print("")
        
    #N_filenames = i + 1
    N_error = len(lst_errors)
    print("Convert %d files - %d errors" % (N_filenames, N_error))
    for i, filename_error in enumerate(lst_errors):
        print(" * %03d / %03d: %s" % (i+1, N_error, filename_error))
    print("Landscapes (from fpl):")

    import pprint
    
    p = pprint.PrettyPrinter(indent=4)
    p.pprint(d_landscapes)

    print("")
    if len(d_landscapes_missing) > 0:
        print("Missing landscapes (from fpl):")
        p.pprint(d_landscapes_missing)
示例#4
0
def main(debug, waypoints_filename, output, outdir, disp, lat, lon, dist, dist_unit):
    basepath = os.path.dirname(__file__)
    
    if outdir=='':
        outdir = os.path.join(basepath, 'out')
    
    # Translate path like {Condor} using paths_default
    d_name = paths_default.copy()
    d_name['output'] = output
    waypoints_filename = waypoints_filename.format(**d_name)
    outdir = outdir.format(**d_name)
    
    if not os.path.exists(outdir):
        print("Create directory %s" % outdir)
        os.makedirs(outdir)
    
    lst_errors = []
    filenames = glob.glob(waypoints_filename)
    N_filenames = len(filenames)

    print("")
    print("="*20)
    print("")    
    
    for i, filename in enumerate(filenames):
        try:
            logging.info("Read file %03d/%03d '%s'" % (i+1, N_filenames, filename))

            filename_base, filename_ext = os.path.splitext(os.path.basename(filename))
            if debug:        
                assert filename_ext in supported_input_extensions, \
                "File extension of '%s' is '%s' but supported extension must be in %s" \
                % (filename, filename_ext, supported_input_extensions)

            cols = ['Name', 'Code', 'Country', 'Latitude', 'Longitude', 'Elevation',
                    'Waypoint style', 'Runway direction', 'Runway length',
                    'Airport Frequency', 'Description']
            df_waypoints = pd.read_csv(filename, names=cols)

            df_waypoints = df_waypoints.rename(columns={
                'Latitude': 'Lat',
                'Longitude': 'Lon',
                'Elevation': 'Altitude',
            })
            #print(df_waypoints)
            # ToDo: rename columns
            # ToDo: map columns (at least Lat, Lon)
            # ToDo: create kml file
            df_waypoints['Waypoint style'] = df_waypoints['Waypoint style'].map(WaypointStyle)
            df_waypoints['Lat'] = df_waypoints['Lat'].map(latlon2decimal)
            df_waypoints['Lon'] = df_waypoints['Lon'].map(latlon2decimal)
            df_waypoints['Altitude'] = df_waypoints['Altitude'].map(dist2quantity)
            df_waypoints['Runway length'] = df_waypoints['Runway length'].map(dist2quantity)

            df_waypoints['Distance'] = df_waypoints.apply(lambda wpt: haversine_distance(lat, lon, wpt['Lat'], wpt['Lon']), axis=1)
            df_waypoints['Distance'] = df_waypoints['Distance'].map(lambda d: Q_(d, "km"))
            df_waypoints = df_waypoints.sort(columns=['Distance'])

            df_waypoints = df_waypoints[df_waypoints['Distance'] <= Q_(dist, dist_unit)]

            print(df_waypoints)


            print("Creating KML file (please wait)")
            task_to_kml_with_yattag(df_waypoints, outdir, filename_base)
            # Too many markers!!!

            # Sol: filter with distance from a given point

            # Sol: Use Google Maps instead
            # see https://developers.google.com/maps/articles/toomanymarkers


        except:
            logging.error(traceback.format_exc())
            lst_errors.append(filename)

        print("")
        print("="*20)
        print("")
        
    N_error = len(lst_errors)
    print("Convert %d files - %d errors" % (N_filenames, N_error))
    for i, filename_error in enumerate(lst_errors):
        print(" * %03d / %03d: %s" % (i+1, N_error, filename_error))
示例#5
0
def main(debug, waypoints_filename, output, outdir, disp, lat, lon, dist,
         dist_unit):
    basepath = os.path.dirname(__file__)

    if outdir == '':
        outdir = os.path.join(basepath, 'out')

    # Translate path like {Condor} using paths_default
    d_name = paths_default.copy()
    d_name['output'] = output
    waypoints_filename = waypoints_filename.format(**d_name)
    outdir = outdir.format(**d_name)

    if not os.path.exists(outdir):
        print("Create directory %s" % outdir)
        os.makedirs(outdir)

    lst_errors = []
    filenames = glob.glob(waypoints_filename)
    N_filenames = len(filenames)

    print("")
    print("=" * 20)
    print("")

    for i, filename in enumerate(filenames):
        try:
            logging.info("Read file %03d/%03d '%s'" %
                         (i + 1, N_filenames, filename))

            filename_base, filename_ext = os.path.splitext(
                os.path.basename(filename))
            if debug:
                assert filename_ext in supported_input_extensions, \
                "File extension of '%s' is '%s' but supported extension must be in %s" \
                % (filename, filename_ext, supported_input_extensions)

            cols = [
                'Name', 'Code', 'Country', 'Latitude', 'Longitude',
                'Elevation', 'Waypoint style', 'Runway direction',
                'Runway length', 'Airport Frequency', 'Description'
            ]
            df_waypoints = pd.read_csv(filename, names=cols)

            df_waypoints = df_waypoints.rename(columns={
                'Latitude': 'Lat',
                'Longitude': 'Lon',
                'Elevation': 'Altitude',
            })
            #print(df_waypoints)
            # ToDo: rename columns
            # ToDo: map columns (at least Lat, Lon)
            # ToDo: create kml file
            df_waypoints['Waypoint style'] = df_waypoints[
                'Waypoint style'].map(WaypointStyle)
            df_waypoints['Lat'] = df_waypoints['Lat'].map(latlon2decimal)
            df_waypoints['Lon'] = df_waypoints['Lon'].map(latlon2decimal)
            df_waypoints['Altitude'] = df_waypoints['Altitude'].map(
                dist2quantity)
            df_waypoints['Runway length'] = df_waypoints['Runway length'].map(
                dist2quantity)

            df_waypoints['Distance'] = df_waypoints.apply(
                lambda wpt: haversine_distance(lat, lon, wpt['Lat'], wpt['Lon']
                                               ),
                axis=1)
            df_waypoints['Distance'] = df_waypoints['Distance'].map(
                lambda d: Q_(d, "km"))
            df_waypoints = df_waypoints.sort(columns=['Distance'])

            df_waypoints = df_waypoints[
                df_waypoints['Distance'] <= Q_(dist, dist_unit)]

            print(df_waypoints)

            print("Creating KML file (please wait)")
            task_to_kml_with_yattag(df_waypoints, outdir, filename_base)
            # Too many markers!!!

            # Sol: filter with distance from a given point

            # Sol: Use Google Maps instead
            # see https://developers.google.com/maps/articles/toomanymarkers

        except:
            logging.error(traceback.format_exc())
            lst_errors.append(filename)

        print("")
        print("=" * 20)
        print("")

    N_error = len(lst_errors)
    print("Convert %d files - %d errors" % (N_filenames, N_error))
    for i, filename_error in enumerate(lst_errors):
        print(" * %03d / %03d: %s" % (i + 1, N_error, filename_error))