[Python] Python - Shift Longitude Coordinates While Converting a .gfc to a Zarr

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a .gfc file that I would like to convert to Zarr format. The conversion works, but the longitude coordinates go from 0° to 360°. I want them to go from -180° to 180°. I posted a method in the code below, but this way I lose all types of georeferencing.

    Is there a better way to do it?

    import argparse
    import numpy as np
    import pyshtools as pysh
    from pyshtools import shio
    import xarray
    import zarr
    from osgeo import osr
    import rioxarray
    import rasterio
    from affine import Affine

    clm: pysh.SHGravCoeffs = pysh.SHGravCoeffs.from_file(
    "xxxx.gfc",
    format="icgem"
    )

    ds = clm.expand().to_xarray()

    # problematic part
    new_lon = ((ds.lon + 180) % 360) - 180
    new_lon.attrs = ds.lon.attrs
    ds = ds.assign_coords(lon=new_lon)

    # other tested ways:

    # ds['lon'] = ((ds['lon'] + 180) % 360) - 180

    # ds = ds.shift(lon=-180)


    store = zarr.storage.LocalStore("out.zarr")
    root = zarr.group(store=store, overwrite=True, zarr_format=2)

    DsMes = ds[["potential"]]
    DsQlt = ds[["total"]]
    DsCnd = ds[["radial","theta","phi"]]

    DsMes.to_zarr(store=store, group="measurements", mode='w', zarr_format=2)
    DsQlt.to_zarr(store=store, group="quality", mode='a', zarr_format=2)
    DsCnd.to_zarr(store=store, group="conditions", mode='a', zarr_format=2)


    Before:

    Driver: Zarr/Zarr
    Files: out.zarr/measurements/potential/.zarray
    Size is 165, 83
    Origin = (-1.097560975609756,91.097560975609753)
    Pixel Size = (2.195121951219512,-2.195121951219512)
    Metadata:
    actual_range={62427044.0291816369,62529437.6430128664}
    comment=pyshtools grid
    extend=True
    grid=DH
    kind=real
    lmax=40
    long_name=potential
    nlat=83
    nlon=165
    sampling=2
    title=gravitational potential
    Corner Coordinates:
    Upper Left ( -1.098, 91.098)
    Lower Left ( -1.098, -91.098)
    Upper Right ( 361.098, 91.098)
    Lower Right ( 361.098, -91.098)
    Center ( 180.0000000, -0.0000000)
    Band 1 Block=165x83 Type=Float64, ColorInterp=Undefined
    NoData Value=nan
    Unit Type: $m^2 s^{-2}$


    After:

    Driver: Zarr/Zarr
    Files: out.zarr/measurements/potential/.zarray
    Size is 165, 83
    Metadata:
    actual_range={62427044.0291816369,62529437.6430128664}
    comment=pyshtools grid
    extend=True
    grid=DH
    kind=real
    lmax=40
    long_name=potential
    nlat=83
    nlon=165
    sampling=2
    title=gravitational potential
    Corner Coordinates:
    Upper Left ( 0.0, 0.0)
    Lower Left ( 0.0, 83.0)
    Upper Right ( 165.0, 0.0)
    Lower Right ( 165.0, 83.0)
    Center ( 82.5, 41.5)
    Band 1 Block=165x83 Type=Float64, ColorInterp=Undefined
    NoData Value=nan
    Unit Type: $m^2 s^{-2}$

    Continue reading...

Compartilhe esta Página