def test_sunang_array(self): """ Sunang as a numpy array """ date_time = pd.to_datetime('2/15/1990 20:30') date_time = date_time.tz_localize('UTC') topo_config = { 'filename': os.path.join(self.basin_dir, 'topo/topo.nc'), 'northern_hemisphere': True, 'gradient_method': 'gradient_d8', 'sky_view_factor_angles': 72 } topo = Topo(topo_config) # convert from UTM to lat/long lat, lon = utm.to_latlon(topo.X[0, 0], topo.Y[0, 0], 11, 'N') lat = np.nan * np.zeros_like(topo.X) lon = np.nan * np.zeros_like(topo.X) for idx, x in np.ndenumerate(topo.X): lat[idx], lon[idx] = utm.to_latlon(topo.X[idx], topo.Y[idx], 11, 'N') self.assertFalse(np.any(np.isnan(lat))) self.assertFalse(np.any(np.isnan(lon))) cosz, azimuth, rad_vec = sunang.sunang(date_time, lat, lon) self.assertTrue(isinstance(cosz, np.ndarray)) self.assertTrue(isinstance(azimuth, np.ndarray)) self.assertTrue(isinstance(rad_vec, float))
def test_sunang(self): """ Sunang calculation """ # replicate the sun angle calculation from the sunang man page # for Santa Barbara on Feb 15, 1990 at 20:30 UTC # # | IPW # zenith | 47.122 # cosz | 0.680436 # azimuth | -5.413 # # The differences between the IPW version and python version # are insignificant and are only different because of the # values are pulled from stdout for IPW which uses printf date_time = pd.to_datetime('2/15/1990 20:30') date_time = date_time.tz_localize('UTC') lat = 34.4166667 # 35d, 25m, 0s lon = -119.9 ipw_cosz = 0.680436 ipw_azimuth = -5.413 ipw_rad_vector = 0.98787 # try out the python version result = sunang.sunang(date_time, lat, lon) self.assertTrue(result[0], ipw_cosz) self.assertTrue(result[1], ipw_azimuth) self.assertTrue(result[2], ipw_rad_vector)
def model_solar(dt, lat, lon, tau=0.2, tzone=0): """ Model solar radiation at a point Combines sun angle, solar and two stream Args: dt - datetime object lat - latitude lon - longitude tau - optical depth tzone - time zone Returns: corrected solar radiation """ # determine the sun angle cosz, az, rad_vec = sunang.sunang(dt, lat, lon) # calculate the solar irradiance sol = direct_solar_irradiance(dt, [0.28, 2.8]) # calculate the two stream model value R = twostream(cosz, sol, tau=tau) return R['irradiance_at_bottom']
def setUpClass(cls): super().setUpClass() date_time = pd.to_datetime('2/15/1990 20:30') cls.date_time = date_time.tz_localize('UTC') cls.tau_elevation = 100.0 cls.tau = 0.2 cls.omega = 0.85 cls.scattering_factor = 0.3 cls.surface_albedo = 0.5 cls.solar_irradiance = irradiance.direct_solar_irradiance( cls.date_time, w=[0.28, 2.8]) topo_config = { 'filename': os.path.join(cls.basin_dir, 'topo/topo.nc'), 'northern_hemisphere': True, 'gradient_method': 'gradient_d8', 'sky_view_factor_angles': 72 } cls.topo = Topo(topo_config) cls.dem = cls.topo.dem # inputs for toporad and stoporad cls.cosz, cls.azimuth, rad_vec = sunang( cls.date_time, cls.topo.basin_lat, cls.topo.basin_long) cls.elevrad = toporad.Elevrad( cls.dem, cls.solar_irradiance, cls.cosz) cls.illum_ang = shade( cls.topo.sin_slope, cls.topo.aspect, cls.azimuth, cls.cosz)
def distribute_single_timestep(self, t): self._logger.info('Distributing time step {}'.format(t)) if self.hrrr_data_timestep: self.data.load_class.load_timestep(t) self.data.set_variables() # 0.1 sun angle for time step cosz, azimuth, rad_vec = sunang.sunang(t.astimezone(pytz.utc), self.topo.basin_lat, self.topo.basin_long) # 0.2 illumination angle illum_ang = None if cosz > 0: illum_ang = shade(self.topo.sin_slope, self.topo.aspect, azimuth, cosz) # 1. Air temperature self.distribute['air_temp'].distribute(self.data.air_temp.loc[t]) # 2. Vapor pressure self.distribute['vapor_pressure'].distribute( self.data.vapor_pressure.loc[t], self.distribute['air_temp'].air_temp) # 3. Wind_speed and wind_direction self.distribute['wind'].distribute(self.data.wind_speed.loc[t], self.data.wind_direction.loc[t], t) # 4. Precipitation self.distribute['precipitation'].distribute( self.data.precip.loc[t], self.distribute['vapor_pressure'].dew_point, self.distribute['vapor_pressure'].precip_temp, self.distribute['air_temp'].air_temp, t, self.data.wind_speed.loc[t], self.data.air_temp.loc[t], self.distribute['wind'].wind_direction, self.distribute['wind'].wind_model.dir_round_cell, self.distribute['wind'].wind_speed, self.distribute['wind'].wind_model.cellmaxus) # 5. Albedo self.distribute['albedo'].distribute( t, illum_ang, self.distribute['precipitation'].storm_days) # 6. cloud_factor self.distribute['cloud_factor'].distribute( self.data.cloud_factor.loc[t]) # 7. Solar self.distribute['solar'].distribute( t, self.distribute["cloud_factor"].cloud_factor, illum_ang, cosz, azimuth, self.distribute['albedo'].albedo_vis, self.distribute['albedo'].albedo_ir) # 8. thermal radiation self.distribute['thermal'].distribute( t, self.distribute['air_temp'].air_temp, self.distribute['vapor_pressure'].vapor_pressure, self.distribute['vapor_pressure'].dew_point, self.distribute['cloud_factor'].cloud_factor) # 9. Soil temperature self.distribute['soil_temp'].distribute()