5

I have a MODIS data. It is a hdf file (not hdf5). I can open it using ArcGIS and QGIS with no crs. When I use the following script, I get MissingCRS: CRS not found. Please set the CRS with 'rio.write_crs()'. error. Since I cannot open the file, I cannot use rio.write_crs().

import rioxarray

modis_pre_path = "C:/Users/user/Desktop/modis.hdf"
modis_pre = rioxarray.open_rasterio(modis_pre_path)

How can I solve this issue?

The full traceback:

MissingCRS                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1520/3420315969.py in <module>
----> 1 modis_pre = rxr.open_rasterio(modis_pre_path)
      2 type(modis_pre)

C:\OSGeo4W\apps\Python39\lib\site-packages\rioxarray\_io.py in open_rasterio(filename, parse_coordinates, chunks, cache, lock, masked, mask_and_scale, variable, group, default_name, decode_times, decode_timedelta, **open_kwargs)
    926     # open the subdatasets if they exist
    927     if riods.subdatasets:
--> 928         return _load_subdatasets(
    929             riods=riods,
    930             group=group,

C:\OSGeo4W\apps\Python39\lib\site-packages\rioxarray\_io.py in _load_subdatasets(riods, group, variable, parse_coordinates, chunks, cache, lock, masked, mask_and_scale, decode_times, decode_timedelta, **open_kwargs)
    680         with rasterio.open(subdataset) as rds:
    681             shape = rds.shape
--> 682         rioda: DataArray = open_rasterio(  # type: ignore
    683             subdataset,
    684             parse_coordinates=shape not in dim_groups and parse_coordinates,

C:\OSGeo4W\apps\Python39\lib\site-packages\rioxarray\_io.py in open_rasterio(filename, parse_coordinates, chunks, cache, lock, masked, mask_and_scale, variable, group, default_name, decode_times, decode_timedelta, **open_kwargs)
   1033         result.rio.write_crs(rio_crs, inplace=True)
   1034     if has_gcps:
-> 1035         result.rio.write_gcps(*riods.gcps, inplace=True)
   1036 
   1037     if chunks is not None:

C:\OSGeo4W\apps\Python39\lib\site-packages\rioxarray\rioxarray.py in write_gcps(self, gcps, gcp_crs, grid_mapping_name, inplace)
   1226         data_obj = self._get_obj(inplace=True)
   1227 
-> 1228         data_obj = data_obj.rio.write_crs(
   1229             gcp_crs, grid_mapping_name=grid_mapping_name, inplace=inplace
   1230         )

C:\OSGeo4W\apps\Python39\lib\site-packages\rioxarray\rioxarray.py in write_crs(self, input_crs, grid_mapping_name, inplace)
    485 
    486         if data_obj.rio.crs is None:
--> 487             raise MissingCRS(
    488                 "CRS not found. Please set the CRS with 'rio.write_crs()'."
    489             )

MissingCRS: CRS not found. Please set the CRS with 'rio.write_crs()'.
1
  • Are you able to provide the file? Also, are you able to provide the full traceback?
    – snowman2
    Commented Oct 19, 2022 at 16:43

2 Answers 2

2

Maybe you can try opening it with a context manager

import xarray as xr
import rioxarray

modis_pre_path = "C:/Users/user/Desktop/modis.hdf"

# Open the data using a context manager
with xr.open_dataset(modis_pre_path) as file_hdf:
    # You can open the data using the code below, however if you use rio.write_crs
    # it will ensure that crs for your data persist throughout your analysis
    # modis_pre = file_hdf # not sure if this should or not be commented
    modis_pre = file_hdf.rio.write_crs(file_hdf.rio.crs)

# view the data
modis_pre

Another option to try is to extract the numpy.array data with a context manager...

with rio.open(modis_pre_path) as file_hdf:
    modis_pre = file_hdf.read(1)

... and then create a xarray object with it

1
  • I tried but it didn't work for my data. Commented Mar 11, 2023 at 21:20
1

I've been plagued with this problem for a few days, and realized that it's most likely an issue with some MODIS product. For example, the lat and lon stored in MYD021KM are for 5km (406 x 271) while the radiation data is for 1km (2030 x 1354), and this inconsistence would cause this 'MissingCRS error'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.