Skip to content
Snippets Groups Projects
Select Git revision
  • 68a0d717783e6167efb3bc2f6e8a1ced5dd46322
  • main default protected
2 results

add-horizons.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    add-horizons.py 1.87 KiB
    import csv
    import datetime
    import sys
    
    from horizons import retrieve_horizon_moon
    
    
    def add_diameter_to_file(matchfile):
        with open(matchfile, "r") as f:
            reader = csv.reader(f)
            matches = list(reader)
            header = matches[0]
            matches = matches[1:]
            horizon_values = ["diameter", "sub-lon", "sub-lat", "phaseangle", "sundistance"]
            headerexist = {}
            for value in horizon_values:
                try:
                    header.index(value)
                    headerexist[value] = True
                except ValueError:
                    header.append(value)
                    headerexist[value] = False
    
            for match in matches:
                year = int(match[header.index("year")])
                doy = int(match[header.index("day")])
                hour = int(match[header.index("hour")])
                minute = int(match[header.index("minute")])
                hour = int(match[header.index("hour")])
                lon = float(match[header.index("lon")])
                lat = float(match[header.index("lat")])
                height = float(match[header.index("height")])
                date = datetime.datetime(year, 1, 1, hour, minute)
                date = (date + datetime.timedelta(days=doy - 1)).strftime("%Y-%m-%d %H:%M")
                horizon = retrieve_horizon_moon(date, f"{lon},{lat},{height}")
    
                for value in horizon_values:
                    if headerexist[value]:
                        match[header.index(value)] = horizon[value]
                    else:
                        match.append(horizon[value])
    
    
        with open(matchfile, "w") as f:
            writer = csv.writer(f)
            writer.writerow(header)
            writer.writerows(matches)
    
    
    def main():
        if len(sys.argv) < 2:
            print("Usage: python add-diameter.py CSVFILE [CSVFILE ...]")
            sys.exit(1)
    
        for matchfile in sys.argv[1:]:
            add_diameter_to_file(matchfile)
    
    
    if __name__ == "__main__":
        main()