def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """

        if not (filename or self.filename):
            raise Exception("Cannot load data, no filename provided")

        filename = filename or self.filename  # TODO: Load data from csv file.
        with open(filename, "r") as f:
            csvfile = csv.DictReader(f, delimiter=",")
            for row in csvfile:
                orbit_path = OrbitPath(**row)
                near_earth_object = NearEarthObject(**row)
                near_earth_object.update_orbits(orbit_path)

                if not self.neo_name_map.get(row["name"], None):
                    self.neo_name_map[row["name"]] = near_earth_object

                if row["close_approach_date"] in self.NearEarthObjects:
                    self.NearEarthObjects[row["close_approach_date"]].append(
                        near_earth_object)

                else:
                    self.NearEarthObjects[row["close_approach_date"]] = [
                        near_earth_object
                    ]

        return None
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """

        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        # TODO: Load data from csv file.
        with open(filename, 'r') as f:
            csvfile = csv.DictReader(f, delimiter=",")
            # TODO: Where will the data be stored?
            for row in csvfile:
                orb_date = row['close_approach_date']
                neo_name = row['name']

                neoModel = {
                    "id":
                    row["neo_reference_id"],
                    "name":
                    row["name"],
                    "diameter_min_km":
                    float(row["estimated_diameter_min_kilometers"]),
                    "is_hazardous":
                    True if row["is_potentially_hazardous_asteroid"] == "True"
                    else False,
                    "orbits": []
                    # "orbit_dates": []
                }

                orbitPathModel = {
                    "name": row["name"],
                    "distance": float(row["miss_distance_kilometers"]),
                    "orbit_date": row["close_approach_date_full"]
                }

                orbitPath = OrbitPath(**orbitPathModel)

                self.OrbitPaths.append(orbitPath)

                neoObject = NearEarthObject(**neoModel)

                if orb_date in self.NearEarthObjects:
                    neoObject.update_orbits(orbitPath)
                    self.NearEarthObjects[orb_date] += [neoObject]
                else:
                    neoObject.update_orbits(orbitPath)
                    self.NearEarthObjects[orb_date] = [neoObject]

        return None
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances -> dict('orbitDate': List[NearEarthObject])
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject -> dict('name': NearEarthObject)

        :param filename:
        :return: None
        """

        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        # TODO: Load data from csv file.
        # TODO: Where will the data be stored?
        """
        Reading neo_data.csv as dictionary and populating NearEarthObject and OrbitPath
        and initialising our two databases declared in __init__
        """
        with open(filename, 'r') as neo_data_file:
            reader = csv.DictReader(neo_data_file)
            for entry in reader:
                #print entry
                _orbit_date = entry["close_approach_date"]
                _neo_name = entry["name"]
                _neo_object = NearEarthObject(**entry)
                _orbit_path_object = OrbitPath(**entry)

                # Update date_orbit_db (Here storing neo_objects without orbit details)
                if _orbit_date in self.date_neo_db:
                    updated_neo_list = self.date_neo_db[_orbit_date] + [
                        _neo_object
                    ]
                    self.date_neo_db[_orbit_date] = updated_neo_list
                else:
                    self.date_neo_db[_orbit_date] = [_neo_object]

                # Update neo_object_db (Here neo_objects are having orbit details)
                if _neo_name in self.neo_object_db:
                    curr_neo_obj = self.neo_object_db[_neo_name]
                    curr_neo_obj.update_orbits(_orbit_path_object, False)
                    self.neo_object_db[_neo_name] = curr_neo_obj
                else:
                    _neo_object.update_orbits(_orbit_path_object, False)
                    self.neo_object_db[_neo_name] = _neo_object

        return None
Exemple #4
0
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """

        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        datafile = pd.read_csv(filename)

        for row in datafile.itertuples():
            #Reading Rows and creating instances
            instance = None
            if row.name not in self.neos_by_name:
                instance = NearEarthObject(
                    neo_id=row.id,
                    name=row.name,
                    min_d=row.estimated_diameter_min_kilometers,
                    max_d=row.estimated_diameter_max_kilometers,
                    is_hazardous=row.is_potentially_hazardous_asteroid)
                self.neos_by_name[row.name] = instance
            else:
                instance = self.neos_by_name[row.name]

            orbit = OrbitPath(name=row.name,
                              speed=row.kilometers_per_hour,
                              miss_d=row.miss_distance_kilometers,
                              date=row.close_approach_date,
                              orbiting_body=row.orbiting_body)

            instance.update_orbits(orbit)

            if row.close_approach_date not in self.neos_by_date:
                self.neos_by_date[row.close_approach_date] = list()

            self.neos_by_date[row.close_approach_date].append(instance)

        return None
Exemple #5
0
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """
        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        # TODO: Load data from csv file.
        # TODO: Where will the data be stored?
        with open(filename) as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                # List version of the row
                attributes_list = list(row.items())
                # Dict version of the row
                attributes_dict = dict(attributes_list)
                NEO = NearEarthObject(**attributes_dict)
                orbit = OrbitPath(**attributes_dict)
                NEO.update_orbits(orbit)
                # Getting the close approach date of this orbit
                orbit_date = orbit.close_approach_date
                if self.date_to_NEOs.get(orbit_date) is None:
                    self.date_to_NEOs[f'{orbit_date}'] = [NEO]
                else:
                    self.date_to_NEOs[f'{orbit_date}'].append(NEO)

                # Making sure only unique objects are contained
                if self.NEOs.get(NEO.name) is None:
                    self.NEOs[NEO.name] = NEO
                else:
                    pass

        return None
Exemple #6
0
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """
        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename
        self.neo_date_dict.clear()
        self.neo_name_dict.clear()
        self.orbit_date_dict.clear()
        # TODO: Load data from csv file.
        # TODO: Where will the data be stored?

        with open(filename, newline='') as csvfile:
            csvreader = csv.reader(csvfile)
            c=-1
            for row in csvreader:
                c=c+1
                if(c!=0):
                    neo=self.neo_name_dict.get(row[2])
                    if(neo is None):
                        neo=NearEarthObject(id=row[0],name=row[2],absolute_magnitude_h=row[4],estimated_diameter_min_kilometers=row[5],estimated_diameter_max_kilometers=row[6],is_potentially_hazardous_asteroid=row[13])
                        self.neo_name_dict[row[2]]=neo
                    orbit=OrbitPath(name=row[2],neo_reference_id=row[1],kilometers_per_hour=row[15],close_approach_date=row[17],miss_distance_kilometers=row[21],orbiting_body=row[23])
                    neo.update_orbits(orbit)
                    if(self.orbit_date_dict.get(row[17]) is None):
                        self.orbit_date_dict[row[17]]=list()
                    self.orbit_date_dict[row[17]].append(orbit)
                    if(self.neo_date_dict.get(row[17]) is None):
                        self.neo_date_dict[row[17]]=list()
                    if(neo not in self.neo_date_dict.values()):
                        self.neo_date_dict[row[17]].append(neo)
        return None
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename: str representing the pathway of the filename containing the Near Earth Object data
        :return: None
        """
        # TODO: Load data from csv file.
        # TODO: Where will the data be stored?

        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        # Load data from csv file
        with open(filename) as csvfile:
            neo_orbit_paths = csv.DictReader(csvfile)
            for neo_orbit_path in neo_orbit_paths:
                # Instantiate new Near Earth Object if not already exist
                if neo_orbit_path['name'] in self.neo_name_to_instance:
                    neo = self.neo_name_to_instance[neo_orbit_path['name']]
                else:
                    neo = NearEarthObject(**neo_orbit_path)
                    self.neo_name_to_instance[neo_orbit_path['name']] = neo

                # Instantiate new orbit path
                orbit = OrbitPath(neo.name, **neo_orbit_path)
                if neo_orbit_path['close_approach_date'] in self.neo_orbit_paths_date_to_neo:
                    self.neo_orbit_paths_date_to_neo[neo_orbit_path['close_approach_date']].append(neo)
                else:
                    self.neo_orbit_paths_date_to_neo[neo_orbit_path['close_approach_date']] = [neo]

                # Add an orbit path information to a Near Earth Object list of orbits
                neo.update_orbits(orbit)

        return None
    def load_data(self, filename=None):
        """
        Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by:
           - Storing a dict of orbit date to list of NearEarthObject instances
           - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject

        :param filename:
        :return:
        """

        if not (filename or self.filename):
            raise Exception('Cannot load data, no filename provided')

        filename = filename or self.filename

        # TODO: Load data from csv file.
        # TODO: Where will the data be stored?
        with open(filename) as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                neo_attributes_list = list(row.items())[:14]
                neo_attributes_dict = dict(neo_attributes_list)
                orbit_attributes_list = list(row.items())[14:]
                orbit_attributes_dict = dict(orbit_attributes_list)
                neo = NearEarthObject(**neo_attributes_dict)
                orbit = OrbitPath(**orbit_attributes_dict)
                neo.update_orbits(orbit)

                orbit_date_key = orbit.close_approach_date
                if self.orbit_date_to_neos.get(orbit_date_key) is None:
                    self.orbit_date_to_neos[orbit_date_key] = [neo]
                else:
                    self.orbit_date_to_neos[orbit_date_key].append(neo)

                self.neos[neo.name] = neo

        return None