def test_heat_index_kelvin(): """Test heat_index when given Kelvin temperatures.""" temp = 308.15 * units.degK rh = 0.7 hi = heat_index(temp, rh) # NB rounded up test value here vs the above two tests assert_almost_equal(hi.to('degC'), 50.3406 * units.degC, 4)
def test_heat_index_undefined_flag(): """Test whether masking values can be disabled for heat index.""" temp = units.Quantity(np.ma.array([80, 88, 92, 79, 30, 81]), units.degF) rh = np.ma.array([40, 39, 2, 70, 50, 39]) * units.percent hi = heat_index(temp, rh, mask_undefined=False) mask = np.array([False] * 6) assert_array_equal(hi.mask, mask)
def test_heat_index_invalid(): """Test heat index for values that should be masked.""" temp = np.array([80, 88, 92, 79, 30, 81]) * units.degF rh = np.array([40, 39, 2, 70, 50, 39]) * units.percent hi = heat_index(temp, rh) mask = np.array([False, True, True, True, True, True]) assert_array_equal(hi.mask, mask)
def test_heat_index_basic(): """Test the basic heat index calculation.""" temp = np.array([80, 88, 92, 110]) * units.degF rh = np.array([40, 100, 70, 40]) * units.percent hi = heat_index(temp, rh) values = np.array([80, 121, 112, 136]) * units.degF assert_array_almost_equal(hi, values, 0)
def mcalc_feelslike(tmpf, dwpf, smps): """Compute a feels like temperature Args: temperature (temperature): The dry bulb temperature dewpoint (temperature): The dew point temperature speed (speed): the wind speed Returns: temperature (temperature): The feels like temperature """ rh = mcalc.relative_humidity_from_dewpoint(tmpf, dwpf) hidx = mcalc.heat_index(tmpf, rh, mask_undefined=True) wcht = mcalc.windchill(tmpf, smps, mask_undefined=True) # Where heat index is masked, replace with temperature hidx[hidx.mask] = tmpf[hidx.mask] # where ever wcht is not masked, replace with wind chill hidx[~ wcht.mask] = wcht[~ wcht.mask] return hidx
def test_heat_index_ratio(): """Test giving humidity as number [0, 1] to heat index.""" temp = units.Quantity([35., 20.], units.degC) rh = 0.7 hi = heat_index(temp, rh) assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
def test_heat_index_units(): """Test units coming out of heat index.""" temp = units.Quantity([35., 20.], units.degC) rh = 70 * units.percent hi = heat_index(temp, rh) assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
def test_heat_index_scalar(): """Test heat index using scalars.""" hi = heat_index(96 * units.degF, 65 * units.percent) assert_almost_equal(hi, 121 * units.degF, 0)
def GFS_fn(in_file_path, out_file_path, slice_hr): os.mkdir(out_file_path) start_time = time.time() GFS_globe = xr.open_mfdataset(in_file_path + "*.grib", engine='cfgrib', compat='override', combine='by_coords') GFS_grib = GFS_globe.sel(longitude=slice(65, 100), latitude=slice(40, 5)) ref_date = str(GFS_grib.time.values) #'2020-02-26 00:00:00' GFS_grib.to_netcdf(in_file_path + 'GFS_INDIA_' + ref_date[:13] + '.nc') GFS = xr.open_dataset(in_file_path + 'GFS_INDIA_' + ref_date[:13] + '.nc') t_bins = GFS.step[::2] GFS.tp.groupby_bins('step', bins=t_bins, include_lowest=True, right=False).groups tp = GFS.tp.groupby_bins('step', bins=t_bins, include_lowest=True, right=False).sum( dim=('step')) #GFS.tp[:,1440,0].values tmax = GFS.tmax.groupby_bins('step', bins=t_bins, include_lowest=True, right=False).max(dim=('step')) #max r2 = GFS.r2.groupby_bins('step', bins=t_bins, include_lowest=True, right=False).max(dim=('step')) #max W_SPD = np.sqrt(GFS.u10**2 + GFS.v10**2) W_speed = W_SPD.groupby_bins('step', bins=t_bins, include_lowest=True, right=False).max(dim=('step')) W_speed_index = W_SPD.groupby_bins( 'step', bins=t_bins, include_lowest=True, right=False).map(lambda x: np.argmax(x, axis=0)) HI_tmax = mpcalc.heat_index(GFS.tmax.values * units.degK, GFS.r2.values / 100 * units.dimensionless, mask_undefined=False).to(units.degC) HI_t2 = mpcalc.heat_index(GFS.t2m.values * units.degK, GFS.r2.values / 100 * units.dimensionless, mask_undefined=False).to(units.degC) Heatindex_tmax_hr = GFS.tmax.copy() * 0 + HI_tmax.magnitude Heatindex_t2_hr = GFS.t2m.copy() * 0 + HI_t2.magnitude Heat_Index_tmax = Heatindex_tmax_hr.groupby_bins( 'step', bins=t_bins, include_lowest=True, right=False).max(dim=('step')) #max of Heat Index Heat_Index_t2 = Heatindex_t2_hr.groupby_bins( 'step', bins=t_bins, include_lowest=True, right=False).max(dim=('step')) #max of Heat Index print(time.time() - start_time) U10 = W_speed.values.copy() * 0 V10 = U10.copy() * 0 for tm in range(W_speed.shape[0]): print(tm) a = W_speed_index[tm, :, :].values + (tm * 2) m, n = a.shape I, J = np.ogrid[:m, :n] U10[tm, :, :] = GFS.u10.values[a, I, J] V10[tm, :, :] = GFS.v10.values[a, I, J] ds = xr.Dataset( { 'Total_precipitation': (['time', 'lat', 'lon'], tp.values), 'T_max': (['time', 'lat', 'lon'], tmax.values - 273.15), 'Relative_Humidity_2m': (['time', 'lat', 'lon'], r2.values), 'U10': (['time', 'lat', 'lon'], U10), 'V10': (['time', 'lat', 'lon'], V10), 'wind_speed': (['time', 'lat', 'lon'], W_speed.values), 'Heat_Index_tmax': (['time', 'lat', 'lon'], Heat_Index_tmax.values), 'Heat_Index_t2': (['time', 'lat', 'lon'], Heat_Index_t2.values) }, coords={ 'lon': (['lon'], GFS.longitude.values), 'lat': (['lat'], GFS.latitude.values), 'time': pd.date_range(ref_date, periods=W_speed.shape[0] + 1, freq='6H')[1:], 'reference_time': pd.Timestamp(ref_date) }) os.chdir(out_file_path) ds.Total_precipitation.to_netcdf("GFS_Total_precipitationto.nc") ds.T_max.to_netcdf("GFS_T_max.nc") ds.Relative_Humidity_2m.to_netcdf("GFS_Relative_Humidity_2m.nc") ds.U10.to_netcdf("GFS_U10.nc") ds.V10.to_netcdf("GFS_V10.nc") ds.wind_speed.to_netcdf("GFS_wind_speed.nc") ds.Heat_Index_tmax.to_netcdf("GFS_Heat_Index_tmax.nc") ds.Heat_Index_t2.to_netcdf("GFS_Heat_Index_t2.nc") lat_new = np.linspace( ds['lat'].values.min(), ds['lat'].values.max(), num=int((ds['lat'].values.max() - ds['lat'].values.min()) / 0.25) + 1, endpoint=True) lon_new = np.linspace( ds['lon'].values.min(), ds['lon'].values.max(), num=int((ds['lon'].values.max() - ds['lon'].values.min()) / 0.25) + 1, endpoint=True) ds_out = xr.Dataset({ 'lat': (['lat'], lat_new), 'lon': (['lon'], lon_new), }) regridder = xe.Regridder(ds, ds_out, 'bilinear', reuse_weights=True) regridder # print basic regridder information. U10p25 = regridder(ds.U10) V10p25 = regridder(ds.V10) U10p25.to_netcdf("GFS0p25_U10m.nc") V10p25.to_netcdf("GFS0p25_V10m.nc") print(time.time() - start_time) data_dir_raw = Path("/home/vassar/Documents/forecast_vassarlabs/data") brk_rh = pd.read_csv(data_dir_raw / "brk_rh.csv")["x"] col_rh = pd.read_csv(data_dir_raw / "col_rh.csv")["x"] brk_tem = pd.read_csv(data_dir_raw / "brk_tem.csv")["x"] col_tem = pd.read_csv(data_dir_raw / "col_tem.csv")["x"] brk_ws = pd.read_csv(data_dir_raw / "brk_ws.csv")["x"] col_ws = pd.read_csv(data_dir_raw / "col_ws.csv")["x"] brk_rf = [0, .1, 2.5, 15.6, 64.5, 115.6, 204.5, 8000] col_rf = [ "#FFFFFF", "#C3FDCA", "#01FF04", "#048500", "#FDC0CB", "#FC0300", "#610301" ] mask_table = pd.read_csv( "/home/vassar/Documents/Rahul/Block&&SubBasin/Block_UUID (1)/Block_uuid.csv" ) mask_table = mask_table[[ 'OBJECTID_1', "State_Name", 'district', 'block', 'UUID' ]] mask_table.columns = ['Mask_id', "State_Name", 'district', 'block', 'UUID'] mask = xr.open_dataset( '/home/vassar/Documents/Rahul/Block&&SubBasin/Block_UUID (1)/Block.nc') nc2table_max(ds.T_max, mask.Band1, mask_table, data_dir_raw, "bilinear_281x281_3032x2923.nc") nc2table_max(ds.Relative_Humidity_2m, mask.Band1, mask_table, data_dir_raw, "bilinear_281x281_3032x2923.nc") nc2table(ds.Total_precipitation, mask.Band1, mask_table, data_dir_raw, "bilinear_281x281_3032x2923.nc") nc2table_max(ds.wind_speed, mask.Band1, mask_table, data_dir_raw, "bilinear_281x281_3032x2923.nc") nc2table_max(ds.Heat_Index_t2, mask.Band1, mask_table, data_dir_raw, "bilinear_281x281_3032x2923.nc") Process(target=heatmap, args=(ds.T_max, col_tem, brk_tem)).start() Process(target=heatmap, args=(ds.Heat_Index_t2, col_tem, brk_tem)).start() Process(target=heatmap, args=(ds.Relative_Humidity_2m, col_rh, brk_rh)).start() Process(target=heatmap, args=(ds.wind_speed, col_ws, brk_ws)).start() Process(target=heatmap, args=(ds.Total_precipitation, col_rf, brk_rf)).start()