HOW to convert '2d' to 'GEO2D' type in xarrray? - netcdf

When I used the MODIS data,I got a problem about the Type.
I got the coord by xarray(python 3.9),but I couldn't get the same type as 'GEO2D'.
xr_tmp = xr.Dataset(
{
'Gpp_500m' : (('XDim', 'YDim','time'), Gpp_500m),
'Npp_500m' : (('XDim', 'YDim','time'), Npp_500m),
'Npp_QC_500m' : (('XDim', 'YDim','time'), Npp_QC_500m),
},
coords=dict(
lon = (['XDim', 'YDim'],lon),
lat = (['XDim', 'YDim'],lat),
time = _time_
# 'latitude': lat,
# 'longitude': lon,
# 'time': _time_,
)
)
The figures are shown bellow,Could U help me? (use xarray or pyModis if necessary)
You can see the difference from the pictures as follow.

Related

TIFFReadDirectory:Failed to read directory at offset 725954560

while Im executing this code in google colab,
def read_raster(raster_file):
"""
Function
--------
read_raster
Given a raster file, get the pixel size, pixel location, and pixel value
Parameters
----------
raster_file : string
Path to the raster file
Returns
-------
x_size : float
Pixel size
top_left_x_coords : numpy.ndarray shape: (number of columns,)
Longitude of the top-left point in each pixel
top_left_y_coords : numpy.ndarray shape: (number of rows,)
Latitude of the top-left point in each pixel
centroid_x_coords : numpy.ndarray shape: (number of columns,)
Longitude of the centroid in each pixel
centroid_y_coords : numpy.ndarray shape: (number of rows,)
Latitude of the centroid in each pixel
bands_data : numpy.ndarray shape: (number of rows, number of columns, 1)
Pixel value
"""
raster_dataset = gdal.Open(raster_file, gdal.GA_ReadOnly)
# get project coordination
proj = raster_dataset.GetProjectionRef()
bands_data = []
# Loop through all raster bands
for b in range(1, raster_dataset.RasterCount + 1):
band = raster_dataset.GetRasterBand(b)
bands_data.append(band.ReadAsArray())
no_data_value = band.GetNoDataValue()
bands_data = np.dstack(bands_data)
rows, cols, n_bands = bands_data.shape
# Get the metadata of the raster
geo_transform = raster_dataset.GetGeoTransform()
(upper_left_x, x_size, x_rotation, upper_left_y, y_rotation, y_size) = geo_transform
# Get location of each pixel
x_size = 1.0 / int(round(1 / float(x_size)))
y_size = - x_size
y_index = np.arange(bands_data.shape[0])
x_index = np.arange(bands_data.shape[1])
top_left_x_coords = upper_left_x + x_index * x_size
top_left_y_coords = upper_left_y + y_index * y_size
# Add half of the cell size to get the centroid of the cell
centroid_x_coords = top_left_x_coords + (x_size / 2)
centroid_y_coords = top_left_y_coords + (y_size / 2)
return (x_size, top_left_x_coords, top_left_y_coords, centroid_x_coords, centroid_y_coords, bands_data)
Helper function to get the pixel index of the point
def get_cell_idx(lon, lat, top_left_x_coords, top_left_y_coords):
"""
Function
--------
get_cell_idx
Given a point location and all the pixel locations of the raster file,
get the column and row index of the point in the raster
Parameters
----------
lon : float
Longitude of the point
lat : float
Latitude of the point
top_left_x_coords : numpy.ndarray shape: (number of columns,)
Longitude of the top-left point in each pixel
top_left_y_coords : numpy.ndarray shape: (number of rows,)
Latitude of the top-left point in each pixel
Returns
-------
lon_idx : int
Column index
lat_idx : int
Row index
"""
lon_idx = np.where(top_left_x_coords < lon)[0][-1]
lat_idx = np.where(top_left_y_coords > lat)[0][-1]
return lon_idx, lat_idx
raster_file = '/content/image.tif'
x_size, top_left_x_coords, top_left_y_coords, centroid_x_coords, centroid_y_coords, bands_data = read_raster(raster_file)
save the result in compressed format
np.savez('/content/nightlight.npz', top_left_x_coords=top_left_x_coords, top_left_y_coords=top_left_y_coords, bands_data=bands_data)
I got this error
RuntimeError Traceback (most recent call last)
in
2
3 raster_file = '/content/image.tif'
----> 4 x_size, top_left_x_coords, top_left_y_coords, centroid_x_coords, centroid_y_coords, bands_data = read_raster(raster_file)
5
6 # save the result in compressed format
1 frames
/usr/local/lib/python3.8/dist-packages/osgeo/gdal.py in Open(*args)
3017 def Open(*args):
3018 """Open(char const * utf8_path, GDALAccess eAccess) -> Dataset"""
-> 3019 return _gdal.Open(*args)
3020
3021 def OpenEx(*args, **kwargs):
RuntimeError: TIFFReadDirectory:Failed to read directory at offset 725954560
Can anyone help me out for solving this problem?

Generate random points on osmnx graph

I have created a road network graph using osmnx library. now I want to generate some random points on the network but I don't have any idea how to do it. need some help :(
here is my code:
import geopandas as gpd
import osmnx as ox
top= gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
top.at[0, 'geometry'] = Point(100.40823730180041,14.207021554191956)
top.at[0, 'name'] = 'tl'
top.at[1, 'geometry'] = Point(100.74774714891429, 14.196946042603166)
top.at[1, 'name'] = 'tr'
bottom= gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
bottom.at[0, 'geometry'] = Point(100.38860578002853,13.612931284522707)
bottom.at[0, 'name'] = 'bl'
bottom.at[1, 'geometry'] = Point(100.7131032869639, 13.581503263247015)
bottom.at[1, 'name'] = 'br'
combined = top.append(bottom)
convex = combined.unary_union.convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(graph_extent, network_type = "drive")
Following are the steps of what I did:
I created two geodataframes top and bottom top define the extent of my road network
Then I combined them and used ox.graph_from_polygon to create a road network.
My road network looks something like this
roadNetwork
Now I want to generate some random points that should be on the links/edges of the network created.
The sample_points function does exactly that. See the OSMnx usage examples and documentation for usage: https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.utils_geo.sample_points

i am receiving Error 'groups' using Foursquare

Hi there
i have reset my credentials as suggested in similar question previously, however i am
still receiving the same error 'groups'.....
def getNearbyVenues(names, latitudes, longitudes):
radius=500
LIMIT=100
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
print(name)
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v=
{}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
LIMIT)
results = requests.get(url).json()["response"]['groups'][0]['items']
venues_list.append([(
name,
lat,
lng,
v['venue']['name'],
v['venue']['location']['lat'],
v['venue']['location']['lng'],
v['venue']['categories'][0]['name']) for v in results])
nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['Neighborhood',
'Neighborhood Latitude',
'Neighborhood Longitude',
'Venue',
'Venue Latitude',
'Venue Longitude',
'Venue Category']
return(nearby_venues)
illembe_venues = getNearbyVenues(names=df_illembe['Neighborhood'],
latitudes=df_illembe['Latitude'],
longitudes=df_illembe['Longitude']
)
i have also written the code in a new notebook and receiving the same error

How to convert Kelvin to Celsius in MODIS LST product in google earth engine?

I'm trying to convert Kelvin to Celsius in MODIS LST product in Google Earth Engine. But there is an error in my code.
The code is:
var LST_K = ee.ImageCollection('MODIS/006/MOD11A2')
.filterBounds(AOI)
.filterDate('2021-02-20','2021-05-31')
.select("LST_Day_1km","LST_Night_1km")
.map(function(img){
return img.multiply(0.02)
.copyProperties(img,['system:time_start','system:time_end']);
});
print(LST_K);
// convert LST to celcius
var toCelsius = function(img){
var time = img.get('system:time_start')
var celsius = img.multiply(0.02) // scale factor
.subtract(273.15) // from kelvin to C
.rename('Celcius')
.set('system:time_start',time)
return celsius;
};
var LST = LST_K.map(toCelsius)
print(LST);
print(ui.Chart.image.series(LST, AOI, ee.Reducer.median(), 1000, 'system:time_start'));
and the link code is below:
https://code.earthengine.google.com/61b7668525bd38cd543f72c0ad201886
Many thanks in advance
The error you're getting is about renaming the bands of the imageCollection "LST_K". This imageCollection has 2 bands, the LST_daytime band and the LST_nighttime band, so in your rename statement you should add two names instead of one.
Adding both names will solve this issue:
.rename(['LST_Day_1km_Celcius', 'LST_Night_1km_Celcius'])

Issue with the robin projection and contourf data with Basemap

I'm using the basemap library to display spatial information from Copernicus program.
The issue is i can not figure out how to project the data on the robin projection, but I do it correctly with the orthogonal projection.
So currently, I tried this :
plt.ioff()
# adapt for location of datasources
filePath = '../data/grib/download.grib'
# load data
grbs = grb.open(filePath)
grbs.seek(0)
data, lats, lons = (None, None, None)
dataUnit = None
title = None
for g in grbs:
data, lats, lons = g.data()
name = g.name
level = g.level
pressureUnit = g.pressureUnits
date = g.validDate
dataUnit = g.units
title = name + ' at ' + str(level) + ' ' + str(pressureUnit) + ' [' + str(date) + ']'
print(title)
break
# mapPlot = Basemap(projection='ortho', lat_0=0, lon_0=0)
mapPlot = Basemap(projection='robin', lat_0=0, lon_0=0, resolution='l')
mapPlot.drawcoastlines(linewidth=0.25)
x, y = mapPlot(lons, lats)
mapPlot.contourf(x, y, data)
mapPlot.colorbar(location='bottom', format='%.1f', label=dataUnit)
plt.title(title)
plt.show()
The orthogonal projection works correctly. But for the robin projection, I have an ... interesting pattern.
What I'm doing wrong ?
So i figure out how to do. I was misled but the first examples I saw.
Here is a my code:
import matplotlib
from mpl_toolkits.basemap import Basemap, shiftgrid
import matplotlib.pyplot as plt
import numpy as np
import pygrib as grb
# Get data
data = g['values']
lats = g['distinctLatitudes'] # 1D vector
lons = g['distinctLongitudes'] # 1D vector
# Useful information for late
name = g.name
level = str(g.level) + g.pressureUnits
date = g.validDate
dataUnit = g.units
# Parse the data
# Shit the data to start à -180. This is important to mark the data to start at -180°
data, lons = shiftgrid(180., data, lons, start=False) # shiftgrid
# Choose a representation (works with both)
# mapPlot = Basemap(projection='ortho', lat_0=0, lon_0=0)
mapPlot = Basemap(projection='robin', lat_0=0, lon_0=0)
mapPlot.drawcoastlines(linewidth=0.25)
# Convert the coordinates into the map projection
x, y = mapPlot(*np.meshgrid(lons, lats))
# Display data
map = mapPlot.contourf(x, y, data, levels=boundaries, cmap=plt.get_cmap('coolwarm'))
# Add what ever you want to your map.
mapPlot.nightshade(date, alpha=0.1)
# Legend
mapPlot.colorbar(map, label=dataUnit)
# Title
plt.title(name + ' at ' + str(level) + ' [' + str(date) + ']')
plt.show()
So it returns what I'm expecting.

Resources