google / Xee

An Xarray extension for Google Earth Engine
Apache License 2.0
243 stars 28 forks source link

incorrect coordinate when export ERA5L #171

Open kongdd opened 1 week ago

kongdd commented 1 week ago

I faced with a strange issue. It seems that lon and lat is confused (lon becomes lat, lat becomes lon).

import ee
import xee
import xarray as xr
import xarray

# ee.Initialize()
ee.Initialize(opt_url='https://earthengine-highvolume.googleapis.com')

def ee_col_download_year(region, col_id='ECMWF/ERA5_LAND/HOURLY',
                         year=2022, fout="a.nc"):
    # fout = prefix + col_id.replace("/", "_") + "_" + str(year) + ".nc"

    ic = (ee.ImageCollection(col_id)
          .filter(ee.Filter.calendarRange(year, year, 'year'))
          .select(['surface_net_solar_radiation'])
          .limit(10)
          )
    print(fout)
    print(ic.size().getInfo())

    ds = xarray.open_dataset(
        ic,
        engine='ee',
        projection=ic.first().select(0).projection(),
        geometry=region
    )
    ds.to_netcdf(fout)
    ds

region = ee.Geometry.Rectangle(108.5, 28.75, 116.25, 33.5)
ee_col_download_year(region, year=2024, fout="test.nc")

Inspect in R:

library(terra)
ra = rast("./test.nc")
plot(ra[[1]])

image

netCDF4 is installed (v1.7.1).

kongdd commented 1 week ago

According to the comments of Alexander-Barth, (the developer of NCDatasets.jl). The netcdf that xee exported has the following format errors:

  1. coordinate variable must not have the _FillValue or missing_value attributes: https://github.com/cf-convention/cf-conventions/blob/main/conformance.adoc#5-coordinate-systems-and-domain
        double lon(lon) ;
                lon:_FillValue = NaN ;
        double lat(lat) ;
                lat:_FillValue = NaN ;
  1. the bounds attribute is expected to be a string (a containing a variable name with related metadata): https://cfconventions.org/Data/cf-conventions/cf-conventions-1.11/cf-conventions.html#cell-boundaries
netcdf xee-test {
dimensions:
        time = 10 ;
        lon = 78 ;
        lat = 48 ;
variables:
        float surface_net_solar_radiation(time, lon, lat) ;
                surface_net_solar_radiation:_FillValue = NaNf ;
                surface_net_solar_radiation:id = "surface_net_solar_radiation" ;
                surface_net_solar_radiation:data_type = "{\'type\': \'PixelType\', \'precision\': \'double\'}" ;
                surface_net_solar_radiation:dimensions = 3601LL, 1801LL ;
                surface_net_solar_radiation:crs = "EPSG:4326" ;
                surface_net_solar_radiation:crs_transform = 0.1, 0., -180.05, 0., -0.1, 90.05 ;
                surface_net_solar_radiation:scale_factor = 0.1 ;
                surface_net_solar_radiation:bounds = 108.5, 28.75, 116.25, 33.5603826424351 ;
[...]

see details at https://github.com/Alexander-Barth/NCDatasets.jl/issues/265