Python netCDF5 working with Longitude and Latitude and extracting Data - python

how are you doing?
I have been working on this for quite a while now, but I am not able to proceed any further.
I was given .nc Files with Cloud Based Altitudes (CBA are measurements to indicate the height of clods for a particular area).
For now I am not even able to extract Data for a single Coordinate. This Data contains the area shown in this picture, from the top left to the bottom right:
Here is a screenshot of the data I am working with:
Sorry that I can not provide more Data than that, but I don't want to get in trouble.
The cba value is the information I want to extract. So let's say I have a set of coordinates (longitude/latitude) of an area I am interested in, how would I be able to get the appropriate cba values?
I don't need a specific solution, but a direction so I can continue on my own.
For additional information this is how the .cba and .cba.values look:
I have been working with data like that before, but not this nested, so I have absolutely no idea how I can even approach that.
Is there a module/package in python which would allow me to easily handle this, so when I put in coordinates for an area it would directly give me the corresponding cba-values?
I hope that is enough information to go on and you can help me, I would really appreciate it.
Regards

The package you are using (xarray) is already perfectly suited for this task.
dObj is an xarray.Dataset object, each contained variable (such as dObj['cba']) is an xarray.DataArray object.
You can access the data at a given set of coordinates like this:
lat = ...
lon = ...
dObj['cbd'].sel(y=lat, x=lon, method='nearest')
method='nearest' is needed as your coordinate array is unlikely to contain the requested floating point values exactly.
If you instead want to select a range of latitudes and longitudes you can do that as follows:
lat_min, lat_max = (...)
lon_min, lon_max = (...)
dObj['cbd'].sel(y=slice(lat_min, lat_max), x=slice(lon_min, lon_max))
For more information about dealing with xarray objects, have a look at the very comprehensive documentation: http://xarray.pydata.org/en/stable/index.html

Related

Keep lat long order using transformer.from_crs, always_xy=True not working?

I'm trying to update some code because lat/lon values are getting flipped (lat ends up in lon column and vice versa) when using Transformer.from_crs.
The line of code that is causing the issue is this, BUT only when f'epsg:{oldEPSG}'="epsg:4326" (so only when we are converting from AND to epsg:4326).
transformer = Transformer.from_crs(f'epsg:{oldEPSG}', "epsg:4326")
I have tried adding the 3rd argument always_xy=True, as suggested in https://github.com/pyproj4/pyproj/issues/510, but get the same result.
We have coordinates with epsg:6491, epsg:26919 and epsg:5646 in our database which don't have their lat/lon flipped after transforming.
I'm not really familiar with working with coordinate, so I'm confused if I need to be figuring out if they have default lat/lon or lon/lat orders (like will epsg:4326, epsg:6491, epsg:26919 and epsg:5646 all be different?).
I guess I'm just wondering why the lat/lon don't get flipped in all cases? If it's just a matter of checking whether the from and to projections are the same, I can do that. I just want to make sure I understand why this is happening in case other projections end up having the same issue.
Thank you!

Creating Regions in Python

I am trying to use Python to create a simple solution.
I have latitude and longitude of various areas, alongside their ID number and a code. The code is used to define regions. Say three locations (near to each other) have the same code, so they form region_1 and so on. I am trying to show this on map (basically show different regions) but don't understand how to approach the problem.
I tried using Folium Choropleth but it didn't work.
I think that is because I am not looking for how a value varies across regions. I am just interested in seeing how I can use different points to represent a region on a map.
Any help to what I can look into would be appreciated!
Edit: So turns out I have to make use of Voronoi regions.
Now I tried creating those regions in Python.
Firstly I got the area shape using:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
area = world[world.name == 'Pakistan']
area = area.to_crs(epsg=3395) # convert to World Mercator CRS
area_shape = area.iloc[0].geometry # get the Polygon
This gave the correct output. Next I had two lists, lat and long.
I combined the two lists into a 2D array and passed it to:
from geovoronoi import voronoi_regions_from_coords
region_polys, region_pts = voronoi_regions_from_coords(arr, area_shape)
This is the error I am getting now:
RuntimeError: ridge line must intersect with surrounding geometry from `geom`; this error often arises when there are points outside of the surrounding geometries; first check if all your points are inside the surrounding geometries
I googled a bit but don't know how fix the error.
The points are fine I think since I did map it in Mapinfo first.
Would appreciate any help on this!

Is there a way to extract visual data from a folium map?

I am constructing a map with meaningful data using Folium, on Python. But, I need to extract the information(for example an image which is bounded by max-min lat-long values). I tried several different ways. However, I don't get the data I desired.
A sample map, constructed using Folium, in an html file.
I need to use this as an RGB image rather than an interactive map. As much as I see, there is no such functionality. At least, I could not find. Is there a way?
Assuming that there is no such way, and I decided to crop this image using selenium browser method. So, I firstly had to indicate the boundaries in order to capture the image with corresponding latitude, longitude values. I applied fit_bounds(), but it is not bounded by given max/min lat-long values. There is padding-like expansion outside of the boundaries. Therefore, this way also failed. Could you please let me know if there is a solution for this purpose? Simply, briefly, I need to have the data that includes the RGB image, lat-long values(at least the boundaries) and these are retrieved directly from a folium map if possible.
Thank you in advance for any support.

Read a vtr file transform it and write it again creates no file

I am completely new to dealing with vtk(/vtr/vtu/..) outside of paraview but I want to make my workflow a bit easier so I want to do some data transformation outside of paraview.
Basically I have two simulations but the origin and the axis are different. The origin difference changes every time step so I want to transform my files such that they are aligned before opening them in paraview such that I not constantly have to change the values in the transform filter when I want to look at a different time step. I was first trying to achieve this by just rotating and transforming one file.
My approach is as such (based on a lot of other webpages so unfortunately I cannot track down anymore what came form where):
import vtk
reader = vtk.vtkXMLRectilinearGridReader()
reader.SetFileName(file_name)
reader.Update()
data = reader.GetOutput()
transform = vtk.vtkTransform()
transform.RotateZ(90)
transform.Translate(2.34375, 4.6875, 2.34375)
transformFilter=vtk.vtkTransformFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputData(data)
transformFilter.Update()
writer = vtk.vtkXMLRectilinearGridWriter()
writer.SetInputData(transformFilter.GetOutput())
writer.SetFileName("Output.vtr")
writer.Update()
Now I don't get any errors but also there is no file created and I don't know where I go wrong. Any help is highly appreciated.
(btw I tried this answer and that actually does create a file)
EDIT; Maybe I found why it goes wrong but still I don't know how to fix it. If I print data it says vtkRectilinearGrid while if I print transformFilter.GetOutput() it says vtkStructuredGrid. I thought the transform filter would keep the same grid type but apparantly not. Somebody any idea to let it keep the same grid type?
A vtkRectilinearGrid is oriented along the main axis. It allows some optimizations, like having implicit coordinates.
The output of the Transform filter cannot be converted to a vtkRectlinearGrid, mainly because you cannot assume its orientation. Points cannot be implicit as with the RectilinearGrid, the object store each of them.
As you said, your solution is to change how you write your data. You can write a .vts file with a vtkXMLStructuredGridWriter.

Best way to save a 2D Pygame platformer level tile map with extra data

I'm making a 2D platformer using my own engine in Python/Pygame and have made a good start. I've also made a level designer that exports the level tile map and the game imports it, but I need to associate different things, like switches that open specific doors (or to be more precise, pressure plates that hold a specific door open) but my tile map array currently only holds the tile image index number. What's the best way to include associated tiles (like which switch opens which door etc)?
Do I make an extra file with that data? Or do I have 2 values for each tile? I've tried Googling, but it's not really covered anywhere. I'm sure there's someone with this kind of experience out there... I don't really want to hard-code it in as I want the game to be as versatile as possible.
I would change your file format from storing one tile index per 2D cell to storing some more complex data object. My first thought would be a dictionary per cell for maximum flexibility moving forward, but serializing that and storing it will be quite large. There's a trade-off here between flexibility and storage size.
Another option would be using NamedTuples to store a fixed number of parameters per cell, while preserving a concise serialization. NamedTuples are nice because they let you very concisely represent a data object in a way that both serializes well and can be queried into using named fields.
The questions you need to ask yourself are "what metadata do I need to know about each cell on the map" and "how much do I care about concise file size to represent them".
The answer to my question was posted by #BowlingHawk95 as using NamedTuples for the data object which enabled me to add multiple fields for each cell. I wanted to post a sample to show the resulting code, and a snap shot of how I've implemented it to help anybody else looking for the same thing.
# Initialise the level data array with NamedTuples;
# 'linked_point' is a tuple (x, y) for an associated cell - e.g. switch associated with a door:
Cell = namedtuple('Cell', ['image_id', 'linked_point'])
level_data = [[Cell(image_id=0, linked_point=(0, 0)) for _ in range(grid_width)] for _ in range(grid_height)]
And now that I am able to add coordinates (as the linked_point) I can now reference another cell from the one I'm on. The following image shows a shot of my level designer, with the coords in the title bar, and also showing the image_id name and coords of the linked cell.
Massive thanks to #BowlingHawk95 for the assistance!

Categories