Example #1
0
 def test_doy2dateconvert(self):
     """doy2date should return a known value for known input"""
     inval = [(2000, 1),
              (2001, 34),
              (2006, 34),
              (2008, 60),
              (2008, 366),
              ([2008], [366])]
     real_ans = [(1, 1),
                 (2, 3),
                 (2, 3),
                 (2, 29),
                 (12, 31),
                 ([12], [31])]
     for i, val in enumerate(inval):
         ans = t.doy2date(*val)
         ans2 = t.doy2date(*val, dtobj=True)
         self.assertEqual(real_ans[i], ans)
         try:
             self.assertEqual(real_ans[i],
                              ([ans2[0].month], [ans2[0].day]))
         except TypeError:
             self.assertEqual(real_ans[i], (ans2.month, ans2.day))
     self.assertRaises(ValueError, t.doy2date, (2000, 2000, 2000), (5, 4))
     numpy.testing.assert_array_equal(t.doy2date((2000, 2000, 2000), (5, 4, 3.3), flAns=True),
                                      [[1, 1, 1],[5, 4, 3]])
Example #2
0
 def test_doy2dateconvert(self):
     """doy2date should return a known value for known input"""
     inval = [(2000, 1),
              (2001, 34),
              (2006, 34),
              (2008, 60),
              (2008, 366),
              ([2008], [366])]
     real_ans = [(1, 1),
                 (2, 3),
                 (2, 3),
                 (2, 29),
                 (12, 31),
                 ([12], [31])]
     for i, val in enumerate(inval):
         ans = t.doy2date(*val)
         ans2 = t.doy2date(*val, dtobj=True)
         self.assertEqual(real_ans[i], ans)
         try:
             self.assertEqual(real_ans[i],
                              ([ans2[0].month], [ans2[0].day]))
         except TypeError:
             self.assertEqual(real_ans[i], (ans2.month, ans2.day))
     self.assertRaises(ValueError, t.doy2date, (2000, 2000, 2000), (5, 4))
     numpy.testing.assert_array_equal(t.doy2date((2000, 2000, 2000), (5, 4, 3.3), flAns=True),
                                      [[1, 1, 1],[5, 4, 3]])
Example #3
0
 def test_doy2datefail(self):
     '''doy2date should fail for bad input'''
     inval = ([[2007], [0.5]],
              [2007, 0.5])
     for val in inval:
         func = lambda: t.doy2date(*val)
         self.assertRaises(ValueError, func)
Example #4
0
 def test_doy2datefloat(self):
     '''doy2date should work with floats'''
     ans = (datetime.datetime(2000, 1, 2, 2, 58, 33,
                              600000), datetime.datetime(2000, 1, 2, 0, 0))
     inval = [(2000, 2.124, True, True), (2000, 2, True, True)]
     for i, val in enumerate(ans):
         self.assertEqual(val, t.doy2date(*inval[i]))
Example #5
0
 def toDate(yr, doy, hh, mm, ss):
     MM, DD = spt.doy2date(yr, doy)
     dates = dm.dmfilled(len(yr), fillval=None, dtype=object)
     for idx, (mon, day) in enumerate(zip(MM, DD)):
         dates[idx] = dt.datetime(yr[idx], mon, day, hh[idx], mm[idx],
                                  ss[idx])
     return dates
Example #6
0
 def test_doy2datefail(self):
     '''doy2date should fail for bad input'''
     inval = ([[2007], [0.5]],
              [2007, 0.5])
     for val in inval:
         func = lambda: t.doy2date(*val)
         self.assertRaises(ValueError, func)
Example #7
0
 def test_doy2datefloat(self):
     '''doy2date should work with floats'''
     ans = (datetime.datetime(2000, 1, 2, 2, 58, 33, 600000),
            datetime.datetime(2000, 1, 2, 0, 0))
     inval = [(2000, 2.124, True, True),
              (2000, 2, True, True)]
     for i, val in enumerate(ans):
         self.assertEqual(val, t.doy2date(*inval[i]))
Example #8
0
def load_SC_OMNI(bird, year, outdata=None, **kwargs):
    '''Load satellite specific OMNI data into dict'''
    fname = get_SC_OMNI(year=year, bird=bird, **kwargs)
    dum = np.genfromtxt(fname,
                        usecols=(0, 1, 2, 3, 15, 16, 23, 26, 28, 29, 30),
                        names=('year', 'day', 'hour', 'minute', 'By_GSM',
                               'Bz_GSM', 'Vx_GSE', 'Den_P', 'X_GSE', 'Y_GSE',
                               'Z_GSE'),
                        converters={
                            0: int,
                            1: int,
                            2: int,
                            3: int
                        })
    data = dm.fromRecArray(dum)
    dates = spt.doy2date(data['year'], data['day'], dtobj=True)
    times = [
        dt.timedelta(hours=x, minutes=y)
        for x, y in zip(data['hour'], data['minute'])
    ]
    data['DateTime'] = dates + times
    for key in ['year', 'day', 'hour', 'minute']:
        del data[key]
    data['Bz_GSM'][np.abs(data['Bz_GSM']) > 20] = np.nan
    data['By_GSM'][np.abs(data['By_GSM']) > 20] = np.nan
    data['Vx_GSE'][np.abs(data['Vx_GSE']) > 900] = np.nan
    data['X_GSE'][np.abs(data['X_GSE']) > 9000] = np.nan
    data['Y_GSE'][np.abs(data['Y_GSE']) > 9000] = np.nan
    data['Z_GSE'][np.abs(data['Z_GSE']) > 9000] = np.nan
    if outdata:
        for key in [
                'By_GSM', 'Bz_GSM', 'Vx_GSE', 'DateTime', 'Den_P', 'X_GSE',
                'Y_GSE', 'Z_GSE'
        ]:
            outdata[key] = np.concatenate([outdata[key], data[key]])
        return outdata
    return data
Example #9
0
###################

### With CRRES database, extract position and time of the satellite (root of the database)
# define coordonates of the satellite
#pycdf.gAttrList(cdf)
#print cdf['Altitude'].attrs
Altitude=cdf['Altitude'][:]         # Altitude of the satellite (km) - coherent with perigee and apogee orbit type in description
radial_distance=Altitude/Re + 1             # radial distance of the satellite (R_e)
#print cdf['Latitude'].attrs
Latitude=cdf['Latitude'][:]         # Latitude of the satellite (degree) - is it in the GEO system ? - coherent with inclination orbit type in description
#print cdf['Longitude'].attrs
Longitude=cdf['Longitude'][:]         # Longitude of the satellite (degree) - is it in the GEO system ?

### Add survey data to find orbit number
temp1 = np.array(pd.DataFrame(orbit_survey, columns=['ORBIT'])).flatten().astype(float)
temp2=[tim.doy2date(1900+np.array(pd.DataFrame(orbit_survey, columns=['YR'])).flatten()[i],np.array(pd.DataFrame(orbit_survey, columns=['DOY'])).flatten()[i],dtobj=True)
 + tim.sec2hms(np.array(pd.DataFrame(orbit_survey, columns=['START'])).flatten()[i],rounding=False,days=False,dtobj=True) for i in range(len(temp1))]
orbit_dates = pd.Series(temp1, index=temp2)
orbit_dates = orbit_dates[~orbit_dates.index.duplicated(keep='first')]
orbit_epoch = pd.Series(np.nan, index=Epoch)
orbit_epoch = orbit_epoch[~orbit_epoch.index.duplicated(keep='first')]
# interpolate the orbits according to these two vectors
all_orbits=pd.concat([orbit_dates, orbit_epoch]).sort_index()
all_orbits = all_orbits[~all_orbits.index.duplicated(keep='first')]
all_orbits=all_orbits.interpolate(method='time')[Epoch]
orbit_number=np.array(pd.DataFrame(all_orbits, columns=['0'])).flatten()
#plt.plot(Epoch,orbit_number)
del temp1
del temp2
del orbit_dates
del orbit_epoch
Example #10
0
def findTLEinfiles(args, **kwargs):
    """ Finds the TLE's from given files.

    Parameters
    ==========
    args : list
        List of all files (str's) for processing multiple files.
    kwargs : dict
        Key word arguments. Used for setting optional params.
        Defaults used if missing.
        Defaults:(CommonName='', SatelliteNumber='', IntDesignator='',
                  ParseMethod=ParseMethods['None'], OutputFile='',
                  DumpToFile=False, PurgeDuplicates=False, Verbose=None)
    
    Returns
    =======
    t[2] : str
        1st Line from the first TLE in the sorted list.
    t[0] : str
        2nd Line from the first TLE in the sorted list.
    t[1] : str
        0th Line (Sat name and info) from the first TLE in the sorted list.
    """
    defaults = dict(CommonName='', SatelliteNumber='', IntDesignator='',
                    ParseMethod=ParseMethods['None'], OutputFile='',
                    DumpToFile=False, PurgeDuplicates=False)
    # replace missing kwargs with defaults
    for dkey in defaults:
        if dkey not in kwargs:
            kwargs[dkey] = defaults[dkey]

    ParseMethod = kwargs['ParseMethod']

    # make a list of all the lines that we want to scan through This supports
    # globbing (i.e. if user enters multiple files or *.txt or some such thing,
    # we will read all the files before processing them).
    lines = []
    if args.__len__() > 0:
        # If there are args provided, assume they are the names of files
        # we want to scan through. The shell will already have expanded
        # wildcards to multiple entries here -- we just have to process
        # them all.
        for filename in sorted(args):
            try:
                f = open(filename, 'r')
                lines += f.readlines()
                f.close()
            except BaseException:
                print 'Unable to read file: ', filename
    else:
        # If there are no args provided, assume input is coming from the stdin
        # (e.g. via a pipe like: cat *.txt | <program> )
        lines = sys.stdin.readlines()

    # Select output stream to dump to
    # Default is stdout, but if an OutputFile is specified, try to open it for
    # writing.
    if kwargs['DumpToFile']:
        try:
            f = open(kwargs['OutputFile'], "w")
            sys.stdout = f
        except BaseException:
            raise IOError('Unable to open output file: %s' % OutputFile)

    # Scan off sets of 3 lines and test to see if the TLE is one we are looking
    # for.
    TLEs = []
    e = enumerate(lines)
    for i, line in e:
        Line0 = line            # Line 0 of a "three-line" element set
        Line1 = e.next()[1]     # Line 1 of a "three-line" element set
        Line2 = e.next()[1]     # Line 2 of a "three-line" element set

        # Add it to the list of TLEs if its one we are interested in For the
        # purposes of sorting later, we will place the lines slightly out of
        # order. Instead of putting Line0 as the first element, lets put it
        # in as the last.
        if (  ((ParseMethod == ParseMethods['UseCommonName'])
              and (CommonName.upper() in Line0.upper()))
            or
              ((ParseMethod == ParseMethods['UseSatelliteNumber'])
              and (kwargs['SatelliteNumber'] in Line1[2:8]))
            or
              ((ParseMethod == ParseMethods['UseIntDesignator'])
              and (IntDesignator in Line1[9:17]))
           ):
            # In some cases, there appears to be a formatting problem in
            # the epoch field. For example, one of the TLEs for GOES13 has
            # "07 95.94735520" for the epoch when it should be
            # "07095.94735520". I.e. it seems that the ddd fields may have
            # been printed without leading zeros. We need to fix this now
            # or later sorting may get screwed up.
            if ' ' in Line1[18:32]:
                Line1 = Line1[0:18] + \
                    Line1[18:32].replace(' ', '0') + Line1[32:69]
            TLE = [Line1.strip(), Line2.strip(), Line0.strip()]
            TLEs.append(TLE)

    # Remove duplicates entries. If the list elements were hashable, we could
    # just do TLEs = list( set(TLEs) ), but the entries are themselves list
    # which apparently arent hashable. So we do it in a more brute force way.
    # Also, the sort order and the order we scan through (end to start) should
    # ensure that of duplicates, the ones that are retained have the most info
    # in Line 0.  E.g., if we had:
    #
    #   GOES 13 (MORE INFO)
    #   1 29155U 06018A   10297.41394480 -.00000241  00000-0  10000-3 0  3276
    #   2 29155 000.1844 075.0961 0003327 026.7415 005.1518 01.00278452 16218
    #   GOES 13
    #   1 29155U 06018A   10297.41394480 -.00000241  00000-0  10000-3 0  3276
    #   2 29155 000.1844 075.0961 0003327 026.7415 005.1518 01.00278452 16218
    #
    # the entry with "MORE INFO" would be the one that gets retained.
    # Since all the TLEs should be for the same object at this point,
    # sorting on Line1
    if kwargs['PurgeDuplicates']:
        if TLEs:
            TLEs = tlesort(TLEs)  # expands the date before sorting
            t = TLEs[-1]
            for i in range(len(TLEs) - 2, -1, -1):
                if t[0:2] == TLEs[i][0:2]:
                    # remove if Line1 and Line2 are the same (i.e. Line0 can
                    # differ) recall that here the lines are stored as
                    # [line1, line2, line0]
                    del TLEs[i]
                else:
                    t = TLEs[i]

    # Now, scan through list and find the nearest match. Set t to the first
    # one and use that as a default in the event that all of the TLEs have
    # epoch times greater than our target time.
    t = TLEs[0]  # default TLE to return  is the first one in the sorted list
    for i in range(len(TLEs) - 1, -1, -1):
        t1 = TLEs[i][0]        # Line1 (its in the zeroeth slot)
        regex = re.compile('\d{7}\.\d+')
        e = float(regex.search(t1).group())  # epoch in yyyyddd.ffffff format

        if e <= kwargs['TargetEpoch']:
            t = TLEs[i]
            mystr = regex.search(t[0]).group()
            # put the two digit year back
            t[0] = t[0].replace(mystr, mystr[2:])
            targetdate = spt.doy2date(int(mystr[:4]), float(mystr[4:]))
            edate = spt.doy2date(int(str(e)[:4]), float(str(e)[4:]))
            tdt = datetime.datetime(
                int(mystr[:4]), targetdate[0], targetdate[1])
            edt = datetime.datetime(int(str(e)[:4]), edate[0], edate[1])
            if tdt - edt > datetime.timedelta(days=30):
                print(('WARNING: TLE found \n[{0}]\n'
                       'is more than 30 days old').format(t[0]))
            break

    if kwargs['Verbose']:
        print t[2]
        print t[0]
        print t[1]

    # close OutputFile if we open one.
    if kwargs['DumpToFile']:
        f.close()

    return t[2], t[0], t[1]