You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cf writer works well for a DataArray with crs and geotransform written by rioxarray.
But, if we remove these spatial attrs and add the area attrs, cf writer failed.
To Reproduce
importnumpyasnpfrompyprojimportCRSimportxarrayasxrimportrioxarrayfromsatpyimportScenefromaffineimportAffinefrompyresample.geometryimportAreaDefinitiondeffake_data():
test=xr.DataArray(data=np.random.rand(1157, 1235), dims=('y', 'x'))
test.rio.write_transform(Affine(30.0, 0.0, 262056.03103060447, 0.0, -30.0, 2143592.6343544293), inplace=True)
test.rio.write_crs(CRS.from_epsg(32643), inplace=True)
returntest# write data using netcdf directly (version1)test=fake_data()
test.to_netcdf('test.nc')
# add to Scene (version2)scn=Scene()
scn['test'] =testscn.save_datasets(writer="cf", filename='test.nc')
# remove spatial info and write area (version3)scn=Scene()
# add areaarea_def=AreaDefinition('id', 'description', 'proj_id',
CRS.from_epsg(32643), 1235, 1157, (-15.0, 1141.0, 1249.0, 15.0))
test.attrs['area'] =area_deftest=test.drop_vars(['spatial_ref'])
# add to Scenescn['test'] =testscn.save_datasets(writer="cf", filename='test.nc')
Expected behavior
cf writer works with deleted rioxarray info and manually added area attrs.
Actual results
Error output of version3 in code
File [~/miniconda3/envs/hyperch4/lib/python3.11/site-packages/xarray/coding/variables.py:181](https://file+.vscode-resource.vscode-cdn.net/Users/xinz/Documents/github/HyperCH4/notebooks/~/miniconda3/envs/hyperch4/lib/python3.11/site-packages/xarray/coding/variables.py:181), in safe_setitem(dest, key, value, name)
179 if key in dest:
180 var_str = f" on variable {name!r}" if name else ""
--> 181 raise ValueError(
182 f"failed to prevent overwriting existing key {key} in attrs{var_str}. "
183 "This is probably an encoding field used by xarray to describe "
184 "how a variable is serialized. To proceed, remove this key from "
185 "the variable's attributes manually."
186 )
187 dest[key] = value
ValueError: failed to prevent overwriting existing key grid_mapping in attrs. This is probably an encoding field used by xarray to describe how a variable is serialized. To proceed, remove this key from the variable's attributes manually.
Actually, I don't see the grid_mapping attrs in scn['test']:
And, I suppose the output netcdf file from version2 has wrong grid_mapping name:
Environment Info:
Satpy Version: 0.14.3.dev7585+gf018f1276
PyResample Version:1.27.1
The text was updated successfully, but these errors were encountered:
Oh, I mixed rioxarray with pyresample in this case. Because pyresample AreDefinition can handle similar things and is more powerful, we can just use AreaDefinition:
import numpy as np
import xarray as xr
import rioxarray
from satpy import Scene
from pyresample.geometry import AreaDefinition
test = xr.DataArray(data=np.random.rand(1157, 1235), dims=('y', 'x'), name='test')
area_def = AreaDefinition.from_ul_corner(area_id='area_id', projection='EPSG:32643',
shape=(1157, 1235),
upper_left_extent=(262056.03103060447, 2143592.6343544293),
resolution=(30, 30))
test.attrs['area'] = area_def
scn = Scene()
scn['test'] = test
scn.save_datasets(writer="cf", filename='test.nc')
Describe the bug
cf writer works well for a DataArray with crs and geotransform written by rioxarray.
But, if we remove these spatial attrs and add the
area
attrs, cf writer failed.To Reproduce
Expected behavior
cf writer works with deleted rioxarray info and manually added area attrs.
Actual results
Error output of version3 in code
Actually, I don't see the
![image](https://private-user-images.githubusercontent.com/30388627/276551008-cd2accf6-374d-43b8-b1fe-256f5d980a2b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA0NzUzODUsIm5iZiI6MTcyMDQ3NTA4NSwicGF0aCI6Ii8zMDM4ODYyNy8yNzY1NTEwMDgtY2QyYWNjZjYtMzc0ZC00M2I4LWIxZmUtMjU2ZjVkOTgwYTJiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA4VDIxNDQ0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWFkOTFiZmFmNzcwZDRkM2EzNWZiNTVjNDI1ZTQ2Y2U4MmZhZDMzNDU2ZDVkZjYxMzQxYTE5YmJiZDQ0N2MyZTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.xYAvsLxrLP39RqzrmZs5X12f7PmkT6ne5idNEcZ7RRU)
grid_mapping
attrs inscn['test']
:And, I suppose the output netcdf file from version2 has wrong grid_mapping name:
![image](https://private-user-images.githubusercontent.com/30388627/276551932-3933cefa-5f7d-4d4b-a70f-ba9eaf214375.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA0NzUzODUsIm5iZiI6MTcyMDQ3NTA4NSwicGF0aCI6Ii8zMDM4ODYyNy8yNzY1NTE5MzItMzkzM2NlZmEtNWY3ZC00ZDRiLWE3MGYtYmE5ZWFmMjE0Mzc1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA4VDIxNDQ0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTY5NDY1NzEwMDhhODY2MDZjNmMyOWQ2ZTY0ZDI2YjMyNTU2M2VmMGY0YWYwMmY4ZDU2MTBiMzQ3ZTZiNjI3NDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.9QeUk6R_kIEPEf29Q697uSZ_6sB2ZRB60NimxKXyQFE)
Environment Info:
The text was updated successfully, but these errors were encountered: