def test_decrease_lat(self): """ increasing lat input should raise ValueError. """ lat_increase = np.arange(0.25, 5, 0.5) lon = np.arange(0.25, 5, 0.5) gnc.find_index_by_point(lat_increase, lon, (2, 2))
def test_decrease_lat(self): """ increasing lat input should raise ValueError. """ lat_increase = np.arange(0.25,5,0.5) lon = np.arange(0.25,5,0.5) gnc.find_index_by_point(lat_increase,lon,(2,2))
def test_return_correct_value(self): ntools.assert_tuple_equal( (0, 0), gnc.find_index_by_point(self.lat, self.lon, (5, 0))) ntools.assert_tuple_equal( (9, 0), gnc.find_index_by_point(self.lat, self.lon, (0, 0))) ntools.assert_tuple_equal( (9, 9), gnc.find_index_by_point(self.lat, self.lon, (0, 5))) ntools.assert_tuple_equal( (0, 9), gnc.find_index_by_point(self.lat, self.lon, (5, 5))) ntools.assert_tuple_equal( (2, 7), gnc.find_index_by_point(self.lat, self.lon, (3.99, 3.99)))
def dataframe_build_geoindex_from_lat_lon(df,lat_name='lat', lon_name='lon', lat=None,lon=None): """ Build a geoindex column for the dataframe "df", by check each latitude/longitude pairs (lat_name/lon_name) falling in which grid cell of the grid as specified by the vectors of lat/lon. The latitude/longitude pairs falling outside the grid will have geoindex values as np.nan. Returns: -------- dataframe Parameters: ----------- df: input dataframe. lat_name/lon_name: the latitude/longitude field name of the dataframe. lat/lon: the latitude/longitude vectors used to compose the grid. """ df['geoindex'] = [(None,None)]*len(df.index) for i in df.index: vlat = df[lat_name][i] vlon = df[lon_name][i] try: df['geoindex'][i] = gnc.find_index_by_point(lat,lon,(vlat,vlon)) except ValueError: df['geoindex'][i] = np.nan return df
def dataframe_build_geoindex_from_lat_lon(df,lat_name='lat', lon_name='lon', lat=None,lon=None): """ Build a geoindex column for the dataframe "df", by check each latitude/longitude pairs (lat_name/lon_name) falling in which grid cell of the grid as specified by the vectors of lat/lon. The latitude/longitude pairs falling outside the grid will have geoindex values as np.nan. Returns: -------- A copy of input dataframe with in geoindex being added. Parameters: ----------- df: input dataframe. lat_name/lon_name: the latitude/longitude field name of the dataframe. lat/lon: the latitude/longitude vectors used to compose the grid. """ dft = df.copy() dft['geoindex'] = [(None,None)]*len(dft.index) for i in dft.index: vlat = dft[lat_name][i] vlon = dft[lon_name][i] try: dft['geoindex'][i] = gnc.find_index_by_point(lat,lon,(vlat,vlon)) except ValueError: dft['geoindex'][i] = np.nan return dft
def latlon_to_land_index(vlat, vlon, globlat=None, globlon=None): """ This function returns the land index for the point of (lat,lon) for 0.5-degree CRUNCEP data. Notes: ------ it return the land value of the vlat,vlon, to get the index needed, one needs to use np.nonzero(grp.variables['land']==land_value) """ if globlat is None: globlat = np.arange(89.75, -90, -0.5) if globlon is None: globlon = np.arange(-179.75, 180, 0.5) (index_lat, index_lon) = gnc.find_index_by_point(globlat, globlon, (vlat, vlon)) return index_lat * len(globlon) + index_lon + 1
def test_small_value_lon(self): """ vlat/vlon smaller than range should raise ValueError. """ gnc.find_index_by_point(self.lat, self.lon, (2, -0.01))
def test_big_value_lon(self): """ vlat/vlon bigger than range should raise ValueError. """ gnc.find_index_by_point(self.lat, self.lon, (2, 5.01))
def test_return_correct_value(self): ntools.assert_tuple_equal((0,0),gnc.find_index_by_point(self.lat,self.lon,(5,0))) ntools.assert_tuple_equal((9,0),gnc.find_index_by_point(self.lat,self.lon,(0,0))) ntools.assert_tuple_equal((9,9),gnc.find_index_by_point(self.lat,self.lon,(0,5))) ntools.assert_tuple_equal((0,9),gnc.find_index_by_point(self.lat,self.lon,(5,5))) ntools.assert_tuple_equal((2,7),gnc.find_index_by_point(self.lat,self.lon,(3.99,3.99)))
def test_small_value_lon(self): """ vlat/vlon smaller than range should raise ValueError. """ gnc.find_index_by_point(self.lat,self.lon,(2,-0.01))
def test_big_value_lon(self): """ vlat/vlon bigger than range should raise ValueError. """ gnc.find_index_by_point(self.lat,self.lon,(2,5.01))