def getHistorical(years, stationID='46011', workingdirectory=None): # # Get historical data from ndbc # import numpy as np import spectral # start = True for year in years: # url = 'http://www.ndbc.noaa.gov/data/historical/swden/' + stationID + \ 'w' + '{:4d}'.format(year) + '.txt.gz' E, f, date = getSpectralData(url, workingdirectory=None, kind=0, historical=True) if start: start = False spec = E freq = f dates = date else: spec = np.concatenate((spec, E), axis=0) dates = np.concatenate((dates, date), axis=0) # # spec = spectral.spectrum1d({'E': spec, 'f': freq, 'loc': dates}) return (spec)
def getFakeData(self, spotterID, num): # import time import spectral data = dict() datab = dict() datab['significantWaveHeight'] = 0. datab['peakPeriod'] = 0. datab['meanPeriod'] = 0. datab['direction'] = 0. datab['peakDirection'] = 0. datab['peakDirectionalSpread'] = 0. datab['directionalSpread'] = 0. data['bulk'] = datab # if not (self._xy) == None: # data['lat'] = self._xy[num, 1] data['lon'] = self._xy[num, 0] # else: # data['lat'] = 0. data['lon'] = 0. # #endif # data['spotterId'] = spotterID data['name'] = 'fake' timestamp = time.strftime("%Y-%m-%dT%H:%M:00.000Z", time.gmtime()) data['timestampStart'] = timestamp data['timestampEnd'] = timestamp spotter = spotres(data) spotter.fullMessage = False # if not (self.fakeData == None): # spotter.fullMessage = True spotter.spec = spectral.spectrum1d({ 'f': self.fakeData.ff(), 'E': self.fakeData.E[num, :], 'a1': self.fakeData.a1[num, :], 'b1': self.fakeData.b1[num, :], 'a2': self.fakeData.a2[num, :], 'b2': self.fakeData.b2[num, :] }) # #endif # return (spotter)
def getSpec(self): # # get all the spectra from the spotters as one # convinient spectral object. Note that for # spotters without full message nans are returned # import numpy as np import spectral ind = self.fullSpecIndices[0] nf = self.spotters[ind].spec.nf f = self.spotters[ind].spec.f nloc = len(self.spotters) # E = np.zeros((self.numSpot, nf)) + np.nan a1 = np.zeros((self.numSpot, nf)) + np.nan b1 = np.zeros((self.numSpot, nf)) + np.nan a2 = np.zeros((self.numSpot, nf)) + np.nan b2 = np.zeros((self.numSpot, nf)) + np.nan j = 0 # for index, spotter in enumerate(self.spotters): # if index in self.fullSpecIndices: # E[index, :] = spotter.spec.E a1[index, :] = spotter.spec.a1 b1[index, :] = spotter.spec.b1 a2[index, :] = spotter.spec.a2 b2[index, :] = spotter.spec.b2 # #endif # #endfor # spec = spectral.spectrum1d({ 'E': E, 'f': f, 'a1': a1, 'b1': b1, 'a2': a2, 'b2': b2 }) return (spec)
def getData(self, spotterID='SPOT-ST0009', endpoint='https://wavefleet.herokuapp.com/api/latestData', num=0): # import urllib.request import json import ndbc import numpy as np import h5py import spectral #If this is a fake, or virtual spotter, do update from fake if self.fake: spot = self.getFakeData(spotterID, num) return (spot) if spotterID[0:4].lower() == 'ndbc': # # ndbcID = spotterID[4:] spec, lat, lon, epochtime = ndbc.getLatestSpec(stationID=ndbcID) data = { 'spec': spec, 'lat': lat, 'lon': lon, 'epochtime': epochtime, 'name': spotterID, 'spotterId': spotterID } spot = spotres(data) # elif self.matlabSpotter: # # This is a matlab spotter, read from dir # fileName = self.matlabSpotterDir + '/' + spotterID + '.mat' dataFile = h5py.File(fileName, 'r') time = np.array(dataFile.get('time'))[0, :] #Convert from matlab to Unix epoch, 719529 is the number of days #at 1/1/1970 according to matlab datenum( 1970 , 1 , 1) time = (time - 719529) * 3600. * 24 #Find minimum index in arrays minimumIndex = np.argmin(np.abs(self.targetTime - time)) epochtime = time[minimumIndex] # E = np.array(dataFile.get('E'))[:, minimumIndex] f = np.array(dataFile.get('f')) a1 = np.array(dataFile.get('a1'))[:, minimumIndex] b1 = np.array(dataFile.get('b1'))[:, minimumIndex] a2 = np.array(dataFile.get('a2'))[:, minimumIndex] b2 = np.array(dataFile.get('b2'))[:, minimumIndex] lat = dataFile.get('latitude')[0][0] lon = dataFile.get('longitude')[0][0] spec = spectral.spectrum1d({ 'f': f, 'E': E, 'a1': a1, 'b1': b1, 'a2': a2, 'b2': b2 }) data = { 'spec': spec, 'lat': lat, 'lon': lon, 'epochtime': epochtime, 'name': spotterID, 'spotterId': spotterID } spot = spotres(data) # else: # req = urllib.request.Request(endpoint + '?spotterId=' + spotterID) if self.token is not None: # req.add_header('token', self.token) # #endif # #...decode json... response = urllib.request.urlopen(req) data = json.loads(response.read().decode('utf-8')) # #... and return a spot datatype print(data) spot = spotres(data['data']) # #endif return (spot)
def __init__(self, data): # import time import calendar import spectral import numpy as np if not 'spec' in data: # self.Hsig = data['waves'][0]['significantWaveHeight'] self.Tp = data['bulk']['peakPeriod'] self.Tm01 = data['bulk']['meanPeriod'] self.peakDir = data['bulk']['peakDirection'] self.peakSprd = data['bulk']['peakDirectionalSpread'] self.Dir = data['bulk']['direction'] self.dirSprd = data['bulk']['directionalSpread'] self.lat = data['lat'] self.lon = data['lon'] self.id = data['spotterId'] self.name = data['name'] self.updatetime = [data['timestampStart'], data['timestampEnd']] self.epochtimes = [ calendar.timegm(time.strptime(x, '%Y-%m-%dT%H:%M:%S.%fZ')) for x in self.updatetime ] self.epochtime = int( (self.epochtimes[0] + self.epochtimes[1]) // 2) self.fullMessage = False self.spec = None if 'frequencyData' in data: # data = data['frequencyData'] # if 'f' in data: # self.fullMessage = True # adhoc conversion to densities based on full messsage format # hardcoded now - should disappear once full message returns # densities f = data['f'] df = np.ones(39) df[0:33] = 0.009765625 df[33:38] = 3 * 0.009765625 df[38] = 0.751953125 - 0.517578125 E = data['Szz'] / df / 1000000 var = { 'f': data['f'], 'E': E, 'a1': data['a1'], 'a2': data['a2'], 'b1': data['b1'], 'b2': data['b2'] } self.spec = spectral.spectrum1d(var) # #end if # #end if # else: # self.Hsig = data['spec'].Hm0() self.Tp = data['spec'].Tp() self.Tm01 = data['spec'].Tm0n() self.peakDir = 270. - data['spec'].peakDir() * 180. / np.pi if self.peakDir < 0.: self.peakDir = self.peakDir + 360. if self.peakDir > 360.: self.peakDir = self.peakDir - 360. self.peakSprd = data['spec'].peakSprd() * 180. / np.pi self.Dir = 270. - data['spec'].bulkDir() * 180. / np.pi # if self.Dir < 0.: # self.Dir = self.Dir + 360. # # if self.Dir > 360.: # self.Dir = self.Dir - 360. # # self.dirSprd = data['spec'].bulkSprd() * 180. / np.pi self.lat = data['lat'] self.lon = data['lon'] self.id = data['spotterId'] self.name = data['name'] self.updatetime = ['0', '0'] self.epochtimes = [data['epochtime'], data['epochtime']] self.epochtime = data['epochtime'] if not np.any(np.isnan(data['spec'].E)): self.fullMessage = True else: self.fullMessage = False self.spec = data['spec']
def getSpec(stationID='46011', workingdirectory=None, epochtime=None, historical=False): import code import numpy as np import urllib.request import pandas as pd import calendar import time import os from datetime import datetime from pytz import timezone import spectral global baseurl files = ['.data_spec', '.swdir', '.swdir2', '.swr1', '.swr2'] kind = [0, 1, 1, 1, 1] data = [] for index, file in enumerate(files): # url = baseurl + stationID + file [tmp, freq, dates] = getSpectralData(url, workingdirectory=workingdirectory, kind=kind[index]) data.append(tmp) # # # Convert to lhs moments - see http://www.ndbc.noaa.gov/measdes.shtml # angle1 = (270 - data[1]) * np.pi / 180. angle2 = (540 - 2 * data[2]) * np.pi / 180. msk = data[3] == 999.0 msk = data[4] == 999.0 data[3][msk] = 0. data[4][msk] = 0. #D(f,A) = (1/PI)*(0.5+R1*COS(A-ALPHA1)+R2*COS(2*(A-ALPHA2))). R1 = data[3] R2 = data[4] E = data[0] a1 = R1 * np.cos(angle1) b1 = R1 * np.sin(angle1) a2 = R2 * np.cos(angle2) b2 = R2 * np.sin(angle2) spec = spectral.spectrum1d({ 'E': E, 'f': freq, 'loc': dates, 'a1': a1, 'b1': b1, 'a2': a2, 'b2': b2 }) if epochtime is not None: # if epochtime < 0: # epochtime = dates[0] # # spec = spec.interpLoc(epochtime) else: # epochtime = dates # # lat, lon = getStationInfo(stationID, workingDirectory=workingdirectory) return (spec, lat, lon, epochtime)