Skip to content

Commit

Permalink
Fix numopy issues and grib data indexing correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Feb 6, 2021
1 parent 1a5a89f commit 8caabec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/fetch_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ec_wave_model = surfpy.wavemodel.us_east_coast_wave_model()

print('Fetching WW3 Wave Data')
wave_grib_data = ec_wave_model.fetch_grib_datas(ri_wave_location, 0, 180)
wave_grib_data = ec_wave_model.fetch_grib_datas(0, 180)
raw_wave_data = ec_wave_model.parse_grib_datas(ri_wave_location, wave_grib_data)
if raw_wave_data:
data = ec_wave_model.to_buoy_data_wave(raw_wave_data)
Expand Down
22 changes: 12 additions & 10 deletions surfpy/noaamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def location_index(self, location):
print('Not in the grid')
return -1, -1

lat_offset = location.latitude - self.bottom_left.latitude
lat_offset = self.top_right.latitude - location.latitude
lon_offset = location.absolute_longitude - self.bottom_left.absolute_longitude

lat_index = int(lat_offset / self.location_resolution)
Expand Down Expand Up @@ -77,27 +77,27 @@ def time_index(self, desired_time):
hours_res = self.time_resolution_hours
return self.hourly_cutoff_index + ((diff - self.hourly_cutoff_index) / hours_res)

def create_grib_url(self, location, time_index):
def create_grib_url(self, time_index):
return ''

def create_grib_urls(self, location, start_time_index, end_time_index):
def create_grib_urls(self, start_time_index, end_time_index):
urls = []
for i in range(start_time_index, end_time_index):
if i > self.hourly_cutoff_index and (i - self.hourly_cutoff_index) % self.time_resolution_hours != 0:
continue
urls.append(self.create_grib_url(location, i))
urls.append(self.create_grib_url(i))
return urls

def fetch_grib_data(self, location, time_index):
url = self.create_grib_url(location, time_index)
def fetch_grib_data(self, time_index):
url = self.create_grib_url(time_index)
if not len(url):
return None

return tools.download_data(url)

def fetch_grib_datas(self, location, start_time_index, end_time_index):
def fetch_grib_datas(self, start_time_index, end_time_index):
urls = self.create_grib_urls(
location, start_time_index, end_time_index)
start_time_index, end_time_index)
if not len(urls):
return None

Expand Down Expand Up @@ -161,8 +161,10 @@ def parse_grib_data(self, location, raw_data, data={}):
if message.level > 1:
var += '_' + str(message.level)

lat_index, lon_index = self.location_index(location)
value = message.values[lat_index][lon_index]
tolerence = 0.1
rawvalue, lats, lons = message.data(lat1=location.latitude-tolerence,lat2=location.latitude+tolerence,
lon1=location.absolute_longitude-tolerence,lon2=location.absolute_longitude+tolerence)
value = rawvalue.mean().item()

if data.get(var) is None:
data[var] = [value]
Expand Down
16 changes: 10 additions & 6 deletions surfpy/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ def serialize_hook(val):
if isinstance(val, datetime.datetime):
return val.timestamp()
else:
out = {
'classname__': val.__class__.__name__,
'modulename__': val.__module__,
}
out.update(val.__dict__)
return out
try:
out = {
'classname__': val.__class__.__name__,
}
if hasattr(val, '__module__'):
out['modulename__'] = val.__module__
out.update(val.__dict__)
return out
except Exception:
return None


def serialize(val):
Expand Down
11 changes: 1 addition & 10 deletions surfpy/wavemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ class WaveModel(NOAAModel):
_base_multigrid_netcdf_url = 'https://nomads.ncep.noaa.gov/dods/wave/mww3/{0}/{1}{0}_{2}'
_base_multigrid_grib_url = 'https://ftp.ncep.noaa.gov/data/nccf/com/wave/prod/multi_1.{0}/{1}.t{2}z.f{3}.grib2'

# def create_grib_url(self, location, time_index):
# model_run_time = self.latest_model_time()
# model_run_str = str(model_run_time.hour).rjust(2, '0')
# hour_str = str(int(time_index)).rjust(3, '0')
# date_str = model_run_time.strftime('%Y%m%d')
# url = self._base_multigrid_grib_url.format(self.name, model_run_str, hour_str, date_str, float(math.floor(location.longitude)), float(
# math.ceil(location.longitude)), float(math.ceil(location.latitude)), float(math.floor(location.latitude)))
# return url

def create_grib_url(self, location, time_index):
def create_grib_url(self, time_index):
model_run_time = self.latest_model_time()
model_run_str = str(model_run_time.hour).rjust(2, '0')
hour_str = str(int(time_index)).rjust(3, '0')
Expand Down
3 changes: 2 additions & 1 deletion surfpy/weathermodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class GFSModel(NOAAModel):
_base_gfs_grib_url = 'https://nomads.ncep.noaa.gov/cgi-bin/filter_{0}.pl?file=gfs.t{1}z.pgrb2.{2}.f{3}&lev_10_m_above_ground=on&var_GUST=on&var_PRES=on&var_TMP=on&var_UGRD=on&var_VGRD=on&subregion=&leftlon={4}&rightlon={5}&toplat={6}&bottomlat={7}&dir=%2Fgfs.{8}%2F{1}'
_base_gfs_netcdf_url = 'https://nomads.ncep.noaa.gov/dods/{0}/gfs{1}/{0}_{2}'

def create_grib_url(self, location, time_index):
def create_grib_url(self, time_index):
# TODO: Update this to use the new schema and match wavemodel.py
model_run_time = self.latest_model_time()
model_run_str = str(model_run_time.hour).rjust(2, '0')
hour_str = str(int(time_index)).rjust(3, '0')
Expand Down

0 comments on commit 8caabec

Please sign in to comment.