def test_fetch_data_many_stations(self): """ Test download nultiple agrimet station data download. This runs through a list of stations, reformats data, checks unit conversion, and Pandas.DataFrame :return: """ for site in self.outside_PnGp_sites: agrimet = Agrimet(station=site, start_date='2015-05-15', end_date='2015-05-15', interval='daily') raw = agrimet.fetch_met_data(return_raw=True) formed = agrimet.fetch_met_data() params = ['et', 'mm', 'pc', 'sr', 'wr'] for param in params: key = '{}_{}'.format(site, param) converted = formed[param.upper()].values.flatten()[0] unconverted = raw[key].values.flatten()[0] if param in ['et', 'pc']: unconverted *= 25.4 if param == 'mm': unconverted = (unconverted - 32) * 5 / 9 if param == 'sr': unconverted *= 41868. if param == 'wr': unconverted *= 1609.34 if isnan(converted): pass elif agrimet.empty_df: pass else: self.assertAlmostEqual(converted, unconverted, delta=0.01)
def test_fetch_data(self): """ Test download agrimet data within time slice. Test refomatting of data, test unit converstion to std units. Checked data is in a Pandas.DataFrame object. :return: """ agrimet = Agrimet(station=self.fetch_site, start_date='2015-01-01', end_date='2015-12-31', interval='daily') raw = agrimet.fetch_met_data(return_raw=True) formed = agrimet.fetch_met_data() a = raw.iloc[1, :].tolist() b = formed.iloc[1, :].tolist() # dates equality self.assertEqual(a[0], b[0]) self.assertEqual(a[0], '2015-01-02') # in to mm self.assertEqual(a[2], b[2] / 25.4) # deg F to deg C self.assertAlmostEqual(a[4], b[4] * 1.8 + 32, delta=0.01) # in to mm self.assertEqual(a[7], b[7] / 25.4) # Langleys to J m-2 self.assertEqual(a[9], b[9] / 41868.) # mph to m sec-1 self.assertEqual(a[12], b[12] / 0.44704)
def test_web_retrieval_all_stations_met(self): for s in self.all_stations: a = Agrimet(station=s, start_date=self.start_season, end_date=self.end_season, interval='daily') try: a.fetch_met_data() print('{} appears valid'.format(a.station)) except: print('{} appears invalid'.format(a.station))
def test_agrimet_mesonet_gridmet_etr(self): agrimet = Agrimet(station=self.fetch_site, start_date=self.start, end_date=self.end, interval='daily') formed = agrimet.fetch_met_data() agri_etr = formed['ETRS'].values s, e = datetime.strptime(self.start, '%Y-%m-%d'), \ datetime.strptime(self.end, '%Y-%m-%d') gridmet = GridMet('etr', start=s, end=e, lat=self.lat, lon=self.lon) gridmet_etr = gridmet.get_point_timeseries() gridmet_etr = gridmet_etr.values mco = Mesonet(self.covm_mco, start=self.start, end=self.end) mesonet_daily = mco.mesonet_etr(lat=46.3, elevation=1000.0) mesonet_etr = mesonet_daily['ETR'].values plt.plot(gridmet_etr[121:273], label='gridmet') plt.plot(mesonet_etr[121:273], label='mesonet') plt.plot(agri_etr[121:273], label='agrimet') plt.xlabel('GROWING SEASON DAY (MAY 01 - SEP 30)') plt.ylabel('Tall Crop Reference ET (mm) daily') plt.legend() # plt.show() saved = os.path.join(os.path.dirname(__file__), 'grid_agri_meso_fig.png') print('saved to {}'.format(saved)) plt.savefig(saved) ga_ratio = gridmet_etr[121:273].sum() / agri_etr[121:273].sum() print('gridmet - agrimet ratio: {}'.format(ga_ratio)) ma_ratio = mesonet_etr[121:273].sum() / agri_etr[121:273].sum() print('mesonet - agrimet ratio: {}'.format(ma_ratio))
def test_pacific_nw_met(self): a = Agrimet(station=self.pn_site, start_date=self.start, end_date=self.end, interval='daily') df = a.fetch_met_data() self.assertIsInstance(a, Agrimet)
def test_great_plains_met(self): a = Agrimet(station=self.gp_site, start_date=self.start, end_date=self.end, interval='daily') df = a.fetch_met_data() self.assertIsInstance(a, Agrimet)
def test_agrimet_gridmet_precip(self): agrimet = Agrimet(station=self.fetch_site, start_date=self.start, end_date=self.end, interval='daily') formed = agrimet.fetch_met_data() agri_ppt = formed['PP'].values s, e = datetime.strptime(self.start, '%Y-%m-%d'), \ datetime.strptime(self.end, '%Y-%m-%d') gridmet = GridMet('pr', start=s, end=e, lat=self.lat, lon=self.lon) gridmet_ppt = gridmet.get_point_timeseries() gridmet_ppt = gridmet_ppt.values difference = abs(gridmet_ppt - agri_ppt) self.assertLess(nanmean(difference), -100000)
def get_agrimet_etr(self, yr, first=False): agrimet = Agrimet(station=self.station, start_date=START.format(yr), end_date=END.format(yr), interval='daily') formed = agrimet.fetch_met_data() if isnull(formed['ETRS']).values.sum() == formed['ETRS'].shape[0]: agri_etr = formed['ETRS'].groupby(lambda x: x.month).sum().values else: agri_etr = formed['ETRS'].groupby(lambda x: x.month).sum().values if first: self.elev = get_elevation(agrimet.station_coords[0], agrimet.station_coords[1]) print('Station {}'.format(self.station)) if formed['YM'].values.mean() != nan: t_dew = formed['YM'].values ea = calcs._sat_vapor_pressure(t_dew)[0] tmin, tmax = formed['MN'].values, formed['MX'].values try: doy = formed.index.strftime('%j').astype(int).values except AttributeError: doy = formed.index.strftime('%j').astype(int) doy = doy.reshape((len(doy), 1))[0] lat = agrimet.station_coords[0] rs = formed['SR'].values uz = formed['UA'].values zw = 2.0 formed['calc_etr'] = Daily(tmin=tmin, tmax=tmax, ea=ea, rs=rs, uz=uz, zw=zw, elev=self.elev, lat=lat, doy=doy).etr() calc_etr = formed['calc_etr'].groupby(lambda x: x.month).sum().values else: calc_etr = empty(agri_etr.shape) calc_etr[:] = 0.0 return agri_etr, calc_etr
def test_agrimet_gridmet_etr(self): agrimet = Agrimet(station=self.fetch_site, start_date=self.start, end_date=self.end, interval='daily') formed = agrimet.fetch_met_data() agri_etr = formed['ETRS'].values s, e = datetime.strptime(self.start, '%Y-%m-%d'), \ datetime.strptime(self.end, '%Y-%m-%d') gridmet = GridMet('etr', start=s, end=e, lat=self.lat, lon=self.lon) gridmet_etr = gridmet.get_point_timeseries() gridmet_etr = gridmet_etr.values # plt.plot(gridmet_etr, label='gridmet') # plt.plot(agri_etr, label='agrimet') # plt.legend() # plt.show() # plt.close() ratio = agri_etr.sum() / gridmet_etr.sum() print('ratio: {}'.format(ratio))