def plot_one_radar(inFname, radarInfo, outDir=None, axExtent=[-180, 180, 30, 90]): """ plot F region scatter on a map - does a whole day's worth of files """ radarCode = inFname.split('.')[1] # data = nc_utils.ncread_vars(inFname) # go to this once the filtering is in the netCDFs print('Filtering %s' % inFname) data = filter_radar_data.filter_sd_file(inFname) day = jd.from_jd(data["mjd"][0], fmt="mjd") if outDir: os.makedirs(outDir, exist_ok=True) uniqueTimes = np.unique(data["mjd"]) for mjdTime in uniqueTimes: print(mjdTime) time = jd.from_jd(mjdTime, fmt="mjd") radarInfo_t = id_hdw_params_t(time, radarInfo[radarCode]) # plot_vels_at_time(data, mjdTime, radarCode, radarInfo_t, axExtent) plot_radar(data, radarInfo_t['lat'], radarInfo_t['lon'], time) clb = plt.colorbar() clb.ax.set_title("Velocity") clb.set_label("m/s", rotation=270) timeStr = time.strftime('%Y%b%d_%H%M') plt.suptitle("%s - F Scatter\n%s" % (radarCode, timeStr)) if outDir: outFname = os.path.join(outDir, "%s.png" % timeStr) print('Writing to %s' % outFname) plt.savefig(outFname, dpi=300) plt.close() else: plt.show()
def magfield_lookup(self, r_ECI, order): """ Function: magfield_lookup Gets magnetic field vector using IGRF-12 model. Must have https://pypi.org/project/pyIGRF/ library installed. Inputs: year altitude (km) glat: geodetic latitude glon: geodetic longitude Ouputs: B_NED: magnetic field vector in North/East/Down in units of Teslas """ GMST = conv.mjd_2_GMST(self.mjd) r_ECEF = conv.ECI_to_ECEF(r_ECI, GMST) glat, glon, alt = conv.ECEF_to_LLA(r_ECEF, self.earth.radius) lat = np.rad2deg(glat) lon = np.rad2deg(glon) % 360.0 # pyIGRF requires East Longitude (0-360 deg) year = julian.from_jd(self.mjd, fmt='mjd').year # field = pyIGRF.igrf_value(lat, lon, alt, year) # pyIGRF uses degrees!!!! B_NED = mfcpp.get_magnetic_field(lat, lon, alt, year, order)/1e9 # B_NED = np.array([field[3], field[4], field[5]]) B_ECI = conv.NED_to_ECI(B_NED, glat, glon, GMST) return B_ECI
def read_given_txt(self): facts = [] self.years_tc = [] path = "./lab01_data/" for i in os.listdir(path): with open(path + i) as f: file = f.read().splitlines() for k in range(2, len(file) - 1): temp_file = file[k].split() facts.append(temp_file) if i == 'moon.txt': temp_years = ju.from_jd(float(temp_file[0]), fmt='mjd') self.years_tc.append(temp_years.year) if i[:-4] == "potentialCoefficientsAOHIS": self.pc_aohis_ = np.asarray(facts).astype(float) if i[:-4] == 'potentialCoefficientsTides': self.pc_tides_ = np.asarray(facts).astype(float) if i[:-4] == 'earthRotationVector': self.earth_rotation_ = np.asarray(facts).astype(float) if i[:-4] == 'moon': self.moon_ = np.asarray(facts).astype(float) if i[:-4] == 'sun': self.sun_ = np.asarray(facts).astype(float) facts.clear()
def jd2datetime(jd): # convert Julian Day to Date(str) and Time(str) dt = julian.from_jd(jd, fmt='jd') d_and_t = dt.isoformat(sep='T').split('T') if dt.microsecond == 0: d_and_t[1] = d_and_t[1] + '.000000' return d_and_t[0], d_and_t[1][:-2]
def sgpPropagate(self, jd=0): """Propogate the State Variables through SGP4 model, and returns the propagated R and V state vectors. Parameters: ----------- jd: floating-point number, optional Takes in the time period over which the state vectors have to be propagated, in terms of delta - time, in julian format. Note: 1 Julian Time = 24 Hrs. Calling the default value, gives the initial state vectors of the satelite, i.e, at t = 0. Default = 0 """ if jd == 0: jddate = twoline2rv(self.line1, self.line2, wgs72).jdsatepoch else: jddate = jd a = julian.from_jd(jddate) y = a.year mont = a.month d = a.day h = a.hour m = a.minute s = a.second + a.microsecond * 10**-6 position, velocity = twoline2rv(self.line1, self.line2, wgs72).propagate(y, mont, d, h, m, s) return np.array(position, np.float64), np.array(velocity, np.float64)
def graph_daily_user_count(): c.execute('SELECT day_number, user_count FROM daily_user_count') data = c.fetchall() dates = [] values = [] for row in data: dates.append(julian.from_jd(row[0], fmt='jd')) values.append(row[1]) fig, ax = plt.subplots() ax.plot(dates, values) plt.axvline(x=get_feature_change_date(), label='Feature Change', color='r', linestyle='--') ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m')) ax.set_xlim(dates[0], dates[-1]) plt.ylim(0, max(values)) fig.autofmt_xdate() plt.xlabel('Year') plt.ylabel('Daily User Count') plt.legend() plt.show()
def jd2isot(jd): # convert Julian Day to ISOT (str) dt = julian.from_jd(jd, fmt='jd') isot = dt.isoformat(sep='T') if dt.microsecond == 0: isot = isot + '.000000' return isot[:-2]
def loadBeam(inFname, bm): data = nc_utils.ncread_vars(inFname) hdr = nc_utils.load_nc(inFname) rsep = hdr.rsep_km rmax = hdr.maxrangegate radarLat = hdr.lat radarLon = hdr.lon bmInd = data['beam'] == bm bmdata = {} flds = 'p_l', 'v', 'mjd', 'lat', 'lon', 'range', 'gflg', 'tfreq' for fld in flds: bmdata[fld] = data[fld][bmInd] rg_idx = np.arange(rmax) rg = (np.arange(rmax + 1) * rsep + data['range'].min()).astype('int') mjds = np.unique(bmdata["mjd"]) pwr = np.zeros((len(mjds), len(rg))) * np.nan vel = np.zeros((len(mjds), len(rg))) * np.nan gflg = np.zeros((len(mjds), len(rg))) * np.nan tfreq = np.zeros((len(mjds), 1)) * np.nan for t, mjd in enumerate(mjds): ti = bmdata['mjd'] == mjd rgi = np.searchsorted(rg, bmdata['range'][ti]) pwr[t, rgi] = bmdata['p_l'][ti] vel[t, rgi] = bmdata['v'][ti] gflg[t, rgi] = bmdata['gflg'][ti] tfreq[t] = bmdata['tfreq'][ti][0] times = np.array([jd.from_jd(mjd, fmt="mjd") for mjd in mjds]) return times, rg, pwr, vel, tfreq, rsep, gflg
def julian2dt(jd): # see: https://en.wikipedia.org/wiki/Julian_day # Julian Date 12h Jan 1, 4713 BC # Modified JD 0h Nov 17, 1858 JD − 2400000.5 # CNES JD 0h Jan 1, 1950 JD − 2433282.5 jd = jd + JULIAN dt = julian.from_jd(jd, fmt='mjd') return dt
def get_feature_change_date(): c.execute(''' SELECT MIN(round(julianday(datetime(timestamp, 'unixepoch', 'localtime')) - 0.5)) AS feature_change_day_number FROM card_change_history; ''') data = c.fetchall()[0][0] return julian.from_jd(data, fmt='jd')
def loadArgoLocationsByJulian(hurrJson, offset=0): yearsmonths = {} for loc in hurrJson["locations"]: dt = julian.from_jd(loc["time"] + offset) if dt.year not in yearsmonths: yearsmonths[dt.year] = set() yearsmonths[dt.year].add(dt.month) print(yearsmonths) return loadArgoLocationsByMonth(yearsmonths)
def julian_to_seconds(jul): date=[] for i in range(len(jul)): time=julian.from_jd(jul[i],fmt='mjd') total_sec = time.hour*3600 + time.minute*60 + time.second date.append(total_sec) for i in range(len(date)): date[i]=date[i]-date[0] return date
def test_simple_from_mjd(self): """ Tests a simple conversion from mjd to datetime """ input_mjd = 53005.1 dt = julian.from_jd(input_mjd, 'mjd') self.assertEqual(dt.month, 1) self.assertEqual(dt.year, 2004) self.assertEqual(dt.day, 1)
def temporal_dist(ft, regname): """ Plot temporal distribution on the maps ft: feature train regname: region name """ # encoded julian days encoded_juld = [julian.from_jd(round(x), fmt='mjd') for x in ft[:, 2]] months = np.asarray([x.month for x in encoded_juld]) years = np.asarray([x.year + cnes_julian for x in encoded_juld]) years_ = np.linspace(min(years), max(years), max(years) - min(years) + 1, dtype=int) months_ = np.linspace(1, 12, 12, dtype=int) count_months = np.zeros(months_.shape[0], dtype=int) count_years = np.zeros(years_.shape[0] * months_.shape[0], dtype=int) for m in list(months_): count = size(np.where(months == m)) count_months[m - 1] = count x_labels = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] plt.figure(figsize=(9, 6)) plt.bar(x_labels, count_months) plt.title("Monthly Data Distribution in " + regname) plt.ylabel("Nb of profiles") i = 0 for y in list(years_): for m in list(months_): count = size(np.where((years == y) * (months == m))) count_years[i] = count i = i + 1 u_year = np.unique(years_) spots = np.asarray([12 * i for i in range(len(u_year))]) plt.figure(figsize=(12, 6)) plt.plot(count_years) plt.grid() plt.xticks(spots, u_year) plt.title('Monthly Data Distribution in ' + regname) plt.ylabel('Nb of profiles')
def julian_to_stdtime(self): """ Change the julian date variable of the HDU tables to standard time representations. """ ref_time = [ julian.from_jd(date, fmt="mjd") for date in self.raw_table["DATEBARTT"].to_list() ] date_frame = pd.DataFrame(data=ref_time, columns=["DATE"]) self.raw_table = pd.concat([self.raw_table, date_frame], axis=1, sort=False) self.feature_list = self.raw_table.keys()
def test_datetime(self): """Test `Time.datetime`.""" calc_datetime = Time(julian=self.julData).datetime() for i in range(calc_datetime.size): with self.subTest(i=i): dt = jd.from_jd(self.julData[i]) self.assertEqual(calc_datetime[i].year, dt.year) self.assertEqual(calc_datetime[i].month, dt.month) self.assertEqual(calc_datetime[i].day, dt.day) self.assertEqual(calc_datetime[i].second, dt.second) self.assertAlmostEqual(calc_datetime[i].microsecond, dt.microsecond, delta=1)
def convert_to_DatetimeIndex(df: pd.DataFrame) -> None: """ 入力された pandas.DataFrame の index を mjd から DatetimeIndex に変換する。 Parameters ---------- df : pandas.DataFrame of shape (n_mjds, n_objects) m_ap30 の表。 """ idx_datetime = np.empty_like(df.index, dtype=datetime.datetime) for i in range(len(idx_datetime)): idx_datetime[i] = \ julian.from_jd(df.index[i], fmt='mjd').replace(second=0, microsecond=0) df.index = idx_datetime
def test_mjd_full(self): """ Ensures that to_jd and from_jd are inverses for a large number of mjds. """ start_mjd = 54372 mjd_delta = 0.017 end_mjd = 55372 mjd = start_mjd while mjd < end_mjd: temp_dt = julian.from_jd(mjd, 'mjd') new_mjd = julian.to_jd(temp_dt, 'mjd') self.assertAlmostEqual(mjd, new_mjd) mjd += mjd_delta
def test_jd_full(self): """ Ensures that to_jd and from_jd are inverses for a large number of jd's """ start_jd = 2457471.93681 - 500 jd_delta = 0.017 end_jd = 2457471.93681 jd = start_jd while jd < end_jd: temp_dt = julian.from_jd(jd) new_jd = julian.to_jd(temp_dt) self.assertAlmostEqual(jd, new_jd) jd += jd_delta
def sea_temp(indexs, gt_temps, est_temps, gt_dates): """ Calulate the seasonal temperature variation in a small specific zone Args: - indexs: indexs of related points - gt_temps: the ground-truth temperature - est_temps: the estimated temperature """ gt = gt_temps[indexs] est = est_temps[indexs] residus = np.abs(gt - est) dates = gt_dates[indexs] sorted_date_indexes = np.argsort(dates) sorted_dates = dates[sorted_date_indexes] sorted_gt = gt[sorted_date_indexes] sorted_est = est[sorted_date_indexes] sorted_residus = residus[sorted_date_indexes] jdays = [julian.from_jd(x, fmt='mjd') for x in sorted_dates] days = np.array([x.day + (x.month - 1) * 30 for x in jdays]) _, trend_gt = sm.tsa.filters.hpfilter(sorted_gt, 1000) _, trend_est = sm.tsa.filters.hpfilter(sorted_est, 1000) _, trend_residus = sm.tsa.filters.hpfilter(sorted_residus, 500) temps_in_year_gt = np.zeros(365) temps_in_year_est = np.zeros(365) temps_in_year_residus = np.zeros(365) for d in range(0, 365): temps_in_year_gt[d] = np.mean(trend_gt[np.where(days == d + 1)]) temps_in_year_est[d] = np.mean(trend_est[np.where(days == d + 1)]) temps_in_year_residus[d] = np.std(trend_residus[np.where(days == d + 1)]) if np.isnan(temps_in_year_gt[d]): temps_in_year_gt[d] = temps_in_year_gt[d - 1] if np.isnan(temps_in_year_est[d]): temps_in_year_est[d] = temps_in_year_est[d - 1] if np.isnan(temps_in_year_residus[d]): temps_in_year_residus[d] = temps_in_year_residus[d - 1] return temps_in_year_gt, temps_in_year_est, temps_in_year_residus
def addMonthYearInformation(filename): #Retroactively add year, month data to hurricanes for easier searching hurricane_data = loadJson(filename) for hurricaneName in hurricane_data.keys(): year = set() month = set() for location in hurricane_data[hurricaneName]: print(location["time"]) dt = julian.from_jd(location["time"]) year.add(dt.year) month.add(dt.month) locations = hurricane_data[hurricaneName] hurricane_data[hurricaneName] = {} hurricane_data[hurricaneName]["locations"] = locations hurricane_data[hurricaneName]["years"] = list(year) hurricane_data[hurricaneName]["months"] = list(month) writeDictToJson(hurricane_data,"hurricaneWithYear.json")
def sgp4_step(line1, line2, mjd, model=wgs84): """ Function: sgp4_step Returns position and velocity in ECI from SGP4 propagation. Inputs: line1: first line of TLE (string) line2: second line of TLE (string) mjd time to propagate to (mjd) model: Earth gravity model to use (default=wgs84) Outputs: rECI: position in ECI vECI: velocity in ECI """ sgp4 = twoline2rv(line1, line2, model) dt = julian.from_jd(mjd, fmt='mjd') sec = dt.second + dt.microsecond / 1e6 return sgp4.propagate(dt.year, dt.month, dt.day, dt.hour, dt.minute, sec)
def read_date(self, jd_temp): """Returns the Gregorian Date and Time in a string format. Parameters: ----------- jd_temp: floating-point number Takes in the Julian Date as a floating point number. """ a = julian.from_jd(jd_temp) y = a.year mont = a.month d = a.day h = a.hour m = a.minute s = a.second + a.microsecond * 10**-6 temp_time = str(h) + ':' + str(m) + ':' + str(s) temp_date = str(d) + '/' + str(mont) + '/' + str(y) return temp_date, temp_time
def interp_model_to_obs(modData, radarData, time): #determine model time index hour = time.hour + time.minute / 60 tdiffs = np.abs(modData["time"]["vals"] - hour) hrind = tdiffs == np.min(tdiffs) if len(hrind) == 2: hrind == hrind[0] #determine radar time index for key, value in radarData.items(): radarData[key] = np.array(value) timeInts = [] uniqueTimes = np.unique(radarData["mjd"]) for element in uniqueTimes: readableRadarTime = jd.from_jd(element, fmt="mjd") timeInts.append([ int(readableRadarTime.strftime('%H')), int(readableRadarTime.strftime('%M')) ]) index = timeInts.index([time.hour, time.minute]) timeIndex = radarData["mjd"] == uniqueTimes[index] print(timeInts) lats = modData["lat0"]["vals"].flatten() lons = modData["lon0"]["vals"].flatten() lons[lons > 180] = lons[lons > 180] - 360 nvals = modData["utheta"]["vals"][hrind, :, :].flatten() evals = modData["uphi"]["vals"][hrind, :, :].flatten() radarLats = radarData["geolat"][timeIndex] radarLons = radarData["geolon"][timeIndex] #interpolate data based on radar coordinates nvel = griddata(np.array([lons, lats]).T, nvals, (radarLons, radarLats)) evel = griddata(np.array([lons, lats]).T, evals, (radarLons, radarLats)) return nvel, evel, index, hrind
def grab_measurements(): if request.method == 'PUT': inputs = request.json forceModel = ForceModel( [globals()[force] for force in inputs['force_model']]) globals()['STATIONS'] = { station: Station(station, inputs['stations'][station]['latitude'], inputs['stations'][station]['longitude'], inputs['stations'][station]['height']) for station in inputs['stations'] } apriori_state = np.array(inputs['apriori_state']) apriori_cov = np.diag(inputs['apriori_cov']) apriori_pert = np.array(inputs['apriori_pert']) t0 = inputs['init_time'] globals()['KALMANFILTER'] = CKFilter(apriori_state, apriori_cov, apriori_pert, t0, forceModel) globals()['DBIO'] = DataBaseIO() return 'ridiculous' elif request.method == 'POST': inputs = request.json value = inputs['value'] time = inputs['time'] mission_id = inputs['mission_id'] sigma = inputs['sigma'] station = globals()['STATIONS'][inputs['station_id']] measure = globals()[inputs['type']](value, time, mission_id, station, sigma) state_est, cov_est = globals()['KALMANFILTER'].run(measure) time = time / 86400 dt = julian.from_jd(time, fmt='mjd') time = dt.strftime('%Y-%m-%dT%H:%M:%SZ') globals()['DBIO'].push(state_est, cov_est, time) return 'something stupid' elif request.method == 'DELETE': print('Delete is running') globals()['DBIO'].client.drop_database('influx') globals()['DBIO'].client.create_database('influx') else: return "Send Me Your Measurements!!!"
def density_lookup(self, r_ECI, model="exponential_2"): """ Function: density_lookup Gets atmospheric density using various models. For MSISE-00 atmospheric model, must have https://pypi.org/project/msise00/ library installed. Inputs: r_ECI: position vector in ECI frame model: parameter to select type of model (default="exponential") Outputs: rho: atmospheric density (kg/m^3) """ if model == "exponential": rho_0 = 1.225e9 h = np.linalg.norm(r_ECI) H = 10.0 rho = rho_0 * np.exp(-(h-self.earth.radius)/H) elif model == "exponential_2": #drag equation fit coefficients a = 4.436e-09 b = -0.01895 c = 4.895e-12 d = -0.008471 # R = np.linalg.norm(r_ECI) - self.earth.radius R = conv.norm2(r_ECI) - self.earth.radius rho = a*np.exp(b*R) + c*np.exp(d*R) elif model == "msise00": GMST = conv.mjd_2_GMST(self.mjd) r_ECEF = conv.ECI_to_ECEF(r_ECI, GMST) glat, glon, alt = conv.ECEF_to_LLA(r_ECEF, self.earth.radius) datetime = julian.from_jd(self.mjd, fmt='mjd') atmos = msise00.run(time=datetime, altkm=alt, glat=glat, glon=glon) rho = atmos.Total.values[0].item() return rho
def readIMU(self): """Returns the values the IMU would read. Assumptions: accelerations aren't used and can be set to zero Currently no noise Source: N/A Inputs: None Output: 3 3-element tuples in order of accelerations, rotation velocities, magnetic field readings in the body frame Properties Used: None """ rv_eci = self.state[0:6] # Initial Orbit State Vector omq = self.state[6:] # Initial Angular Velocity (rad/s) and Quaternion # Calculate Earth's Magnetic Field in ECI t0 = julian.from_jd(self.time, fmt='mjd') # Convert mjd into seconds B_eci = igrffx(rv_eci[0:3],t0) std_om = 8.75*10**-3 # std dev in deg/s std_b = 0.14*10**-3 # std dev in guass om = omq[:3]*180.0/np.pi + np.random.normal(0,std_om) # converting to deg/s and adding noise B_eci = B_eci*10**4 +np.random.normal(0,std_b) # converting to gauss and adding noise q = omq[3:7] # Rotate B_eci into Satellite Body Frame B_body = np.dot(q2rot(q),B_eci) return ([0, 0, 0], om, B_body)
def transit_forecast(data, name, start, end): """ Forecast transits visible from somewhere on Earth within the given window for a given target :param data: Data from read_data, to be stored in Transit2 object :param name: Name of target :param start: Start of window: datetime :param end: End of window: datetime :return: List of Transit2 objects for transits visible from somewhere on Earth """ from datetime import timedelta import julian # extract data current_dt = start target_dt = end current_ephemeris = julian.from_jd(data[0]+2400000, fmt='jd') # convert dt to JD duration = timedelta(minutes=data[1]) period = timedelta(days=data[2]) ra = data[3] dec = data[4] epoch = data[5] expiry = data[6] transits = [] # loop for all transits by the target in the window while current_ephemeris < target_dt: current_ephemeris += period # increment date epoch += 1 # increment epochs if current_ephemeris > current_dt: # create new Transit2 object candidate = Transit() candidate.gen_for_forecast(current_ephemeris, duration, ra, dec, period, name, epoch, expiry) #print(julian.to_jd(current_ephemeris, fmt='jd') - 2400000 > data[6]) if candidate.check_visibility_general(): # check visibility if julian.to_jd(candidate.egress, fmt='jd') - 2400000 > expiry: transits.append(candidate) transits.sort(key=lambda x: x.expiry) return transits
def open(self): """ Open a binary file :return: if binary file was opened successfully :rtype: boolean """ if self.handle is None: self.handle = output.init() if not self.loaded: self.loaded = True output.open(self.handle, self.binfile) self.start = from_jd( output.get_start_date(self.handle) + 2415018.5) self.start = self.start.replace(microsecond=0) self.report = output.get_times(self.handle, shared_enum.Time.REPORT_STEP) self.period = output.get_times(self.handle, shared_enum.Time.NUM_PERIODS) self.end = self.start + timedelta(seconds=self.period * self.report) return True
def test_from_julian(self): """Test `AstronomicalQuantities.from_julian`. Notes ----- Test cases are generated from the `Julian` Python library. """ import julian as jd julian = np.array([2436116.31, 2445246.65, 2456124.09]) year, month, day = from_julian(julian) for i in range(julian.size): with self.subTest(i=i): dt = jd.from_jd(julian[i]) true_day = dt.day + (dt.hour + (dt.minute + (dt.second + dt.microsecond / 100000) / 60) / 60) / 24 true_month = dt.month true_year = dt.year self.assertAlmostEqual(day[i], true_day, places=3) self.assertEqual(month[i], true_month) self.assertEqual(year[i], true_year)