def read_table_rg(filename): # astropy.io.ascii.convert_numpy converters = { 'col1': [asciitable.convert_list(str)], 'col2': [asciitable.convert_list(float)], 'col3': [asciitable.convert_list(float)], 'col4': [asciitable.convert_list(float)], 'col5': [asciitable.convert_list(float)], 'col6': [asciitable.convert_list(str)], 'col7': [asciitable.convert_list(str)], 'col8': [asciitable.convert_list(str)], 'col9': [asciitable.convert_list(str)], 'col10': [asciitable.convert_list(float)]} names = ('POINT_NAME', 'RA_PNT', 'DEC_PNT', 'BACK','NH', 'MOS1FILTER', 'MOS2FILTER', 'PNFILTER', 'EXPOTIME') data = asciitable.read(filename, delimiter='|', converters=converters) npoint = data.size name = data['col1'].tolist() back = data['col4'] nh = data['col5'] m1filt = data['col6'].tolist() m2filt = data['col7'].tolist() pnfilt = data['col8'].tolist() timestamp = data['col9'] area = data['col10'] return (name, back, nh, m1filt, m2filt, pnfilt, timestamp, area, npoint)
def test_types_from_dat(numpy): if numpy: converters = {'a': [asciitable.convert_numpy(np.float)], 'e': [asciitable.convert_numpy(np.str)]} else: converters = {'a': [asciitable.convert_list(float)], 'e': [asciitable.convert_list(str)]} dat = asciitable.read(['a b c d e', '1 1 cat 2.1 4.2'], Reader=asciitable.Basic, converters=converters, numpy=numpy) reader = asciitable.get_reader(Reader=asciitable.Memory, numpy=numpy) reader.read(dat) print('numpy=%s' % numpy) print('dat=%s' % repr(dat)) print('reader.table=%s' % repr(reader.table)) print('types=%s' % repr([x.type for x in reader.cols])) assert_true(issubclass(reader.cols[0].type, asciitable.FloatType)) assert_true(issubclass(reader.cols[1].type, asciitable.IntType)) assert_true(issubclass(reader.cols[2].type, asciitable.StrType)) assert_true(issubclass(reader.cols[3].type, asciitable.FloatType)) assert_true(issubclass(reader.cols[4].type, asciitable.StrType))
def read_NX2(self, filename, date, corr_bsp=1., origin=None, timeoffset=2, verbose=True): '''read in csv data and initialize table Parameters ---------- filename : string filename as string or other input compatible with asciitable date: tuple of integers ``(day, month, year)`` date of measurement corr_bsp: float multiplictive correction factor for BSP origin: tuple (lat, lon) in deg of x,y origin default: lat, lon at first datapoint timeoffset: float hours to be added to convert UT to local ''' include_names = ['TIME', 'LAT', 'LON', 'AWA', 'AWS', 'BSP', 'COG', 'DFT', 'HDC', 'SET', 'SOG', 'TWA', 'TWS'] converters = {'TIME': asciitable.convert_list(float)} try: atpy.Table.__init__(self, filename, type='ascii', delimiter=',', fill_values=( '', 'nan'), data_start = 5, include_names = include_names, guess = False) if verbose: print 'Reading new format NX2 table - Export with 1.08 or later' except InconsistentTableError: if verbose: print 'Reading NX2 table, which was exported with 1.05' # 30 header values, but only 29 table entries, manually delete the last header value names = [ 'DATE', 'TIME', 'LAT', 'LON', 'AWA', 'AWS', 'BOD', 'BSP', 'BTW', 'CMG', 'COG', 'CTS', 'DEP', 'DFT', 'DMG', 'DST', 'DTW', 'HDC', 'LOG', 'RDR', 'SET', 'SOG', 'TBS', 'TEMP', 'TWA', 'TWS', 'VAR', 'VMG', 'WCV'] atpy.Table.__init__(self, filename, type='ascii', delimiter=',', names=names, fill_values=( '', 'nan'), data_start = 5, include_names = include_names) self.read_date = date self.filename = filename if origin: self.origin = origin else: self.origin = (self.LAT[0], self.LON[0]) # remove all columns which contain only NaNs # interpolate nans in those columns with only a few nans for name in self.names: valid = np.isfinite(self[name]) if valid.all(): pass elif (~valid).all(): self.remove_columns(name) elif (np.sum(valid, dtype=np.float) / len(valid)) >= 0.98: self.fill_nans(name) elif name == 'TIME': warn('TIME contains > 2 % nans. Interpolating ...', NX2InterpolationWarning) self.fill_nans(name) else: warn(NX2InterpolationWarning(name)) self.add_empty_column('year', np.int_) self.add_empty_column('month', np.int_) self.add_empty_column('day', np.int_) self.add_empty_column('hour', np.int_) self.add_empty_column('minute', np.int_) self.add_empty_column('sec', np.int_) self.year[:] = date[2] self.month[:] = date[1] self.day[:] = date[0] # TBD: self.TIME has non-unique entries # change [1,1,1] -> [1,1.33,1.66] ? Needs sub-s times then TIME = timeoffset * 3600 + self.TIME h, m, s = sec2hms(TIME) self.hour[:] = h self.minute[:] = m self.sec[:] = s r_earth = 6300e3 # in Si unit - meter self.add_column('y', 2.*np.pi*r_earth/360.*(self.LAT-self.origin[0])) self.add_column('x', 2.*np.pi*r_earth*np.cos( self.LAT/180.*np.pi)/360.*(self.LON-self.origin[1])) # 2012 Haltern data was taken / exported without all sensers available if 'BSP' in self.columns: self.BSP = self.BSP * corr_bsp if 'TWS' in self.columns: self.TWS = self.TWS / mps2knots if 'AWS' in self.columns: self.AWS = self.AWS / mps2knots