def AddMagneticCoordinates(sourceFilename, resultFilename): startSecs = time.time() prev_time = current_time = None timestep = 0 linenum = 0 CSVfileOUT = open(resultFilename, 'w') CSVwriter = csv.writer(CSVfileOUT, delimiter=',') CSVwriter.writerow([ "Epoch(UTCG)", "Lat_GEOD(deg)", "Lon_GEOD(deg)", "Height_WGS84 (km)", "Daedalus.Magnetic Latitude", "Daedalus.Magnetic Longitude", "Daedalus.MLT" ]) with open(sourceFilename) as CSVfileIN: CSVreader = csv.reader(CSVfileIN) # locate the column numnbers of interest inside the csv file CSVheader = next(CSVreader) Time_idx = CSVheader.index( "Epoch(UTCG)") #CSVheader.index( "Daedalus.EpochText" ) Lat_idx = CSVheader.index( "Lat_GEOD(deg)") #CSVheader.index( "Daedalus.Latitude" ) Lon_idx = CSVheader.index( "Lon_GEOD(deg)") #CSVheader.index( "Daedalus.Longitude" ) Alt_idx = CSVheader.index( "Height_WGS84 (km)") #CSVheader.index( "Daedalus.Longitude" ) # read the orbit file for row in CSVreader: # for each satellite position if len(list(row)) == 0: break # <<<< linenum += 1 resultItems = list() # add the standard fields to the result file resultItems.append(row[Time_idx]) resultItems.append( row[Lat_idx]) # Latitude is geodetic inside the orbit file resultItems.append(row[Lon_idx]) resultItems.append(row[Alt_idx]) # Calculate the extra fields prev_time = current_time current_time = parseDaedalusDate( row[Time_idx]) # read time for the current orbit position if current_time == None: print("ERROR - Wrong time format at line", linenum, ":", row[Time_idx]) continue if prev_time is not None: if timestep == 0: timestep = calendar.timegm( current_time.utctimetuple()) - calendar.timegm( prev_time.utctimetuple()) else: if calendar.timegm( current_time.utctimetuple()) - calendar.timegm( prev_time.utctimetuple()) != timestep: print( "Time leap at line", linenum, ":", row, "(", calendar.timegm(current_time.utctimetuple()) - calendar.timegm(prev_time.utctimetuple()), "sec)", "\n", prev_time, "\n", current_time) # take care of time so that it is compatible with igrf #if current_time.year > 2024: # time_for_igrf = current_time - relativedelta(years=13) #else: # time_for_igrf = current_time time_for_igrf = current_time MagneticLatitude, MagneticLongitude, MagneticLocalTime = Conversions.getMagneticProperties( time_for_igrf, float(row[Lat_idx]), float(row[Lon_idx]), float(row[Alt_idx])) # add the extra fields to the result file resultItems.append(MagneticLatitude) resultItems.append(MagneticLongitude) resultItems.append(MagneticLocalTime) # write it CSVwriter.writerow(resultItems) # clean up CSVfileOUT.close() finishSecs = time.time() print("Processed", linenum, "lines in", finishSecs - startSecs, "sec")