Skip to content

Cannot save to netcdf where dataset contains a structured numpy array #2564

Closed
@qAp

Description

@qAp

I have this netcdf file which I can load into a xarray.Dataset containing a single data variable. This data variable consists of a structured numpy array. Loading it with:

import xarray as xr
import pandas as pd
import numpy as np

ds = xr.open_dataset('puftrac_2018103100ChinShan_Cs.nc')

returns

<xarray.Dataset>
Dimensions:  (serial: 2657664)
Dimensions without coordinates: serial
Data variables:
    trajec   (serial) {'names':['ReleaseTime','CurrentTime','Longitude','Latitude','RelativeHeight','SigX','SigY','SigZ','AirConcentrationR','GroundConcentrationR'], 'formats':['<i8','<i8','<f4','<f4','<f4','<f4','<f4','<f4','<f4','<f4'], 'offsets':[0,8,16,20,24,28,32,36,40,44], 'itemsize':48, 'aligned':True} ...
Attributes:
    Start Time:      1540944000
    End Time:        1541635200
    Release Period:  300
    Dump Period:     300

I can't save it back to netcdf with

ds.to_netcdf('tmp.nc')

getting

~/anaconda3/lib/python3.6/site-packages/xarray/backends/netCDF4_.py in _get_datatype(var, nc_format, raise_on_invalid_encoding)
    105 def _get_datatype(var, nc_format='NETCDF4', raise_on_invalid_encoding=False):
    106     if nc_format == 'NETCDF4':
--> 107         datatype = _nc4_dtype(var)
    108     else:
    109         if 'dtype' in var.encoding:

~/anaconda3/lib/python3.6/site-packages/xarray/backends/netCDF4_.py in _nc4_dtype(var)
    128     else:
    129         raise ValueError('unsupported dtype for netCDF4 variable: {}'
--> 130                          .format(var.dtype))
    131     return dtype
    132 

ValueError: unsupported dtype for netCDF4 variable: {'names':['ReleaseTime','CurrentTime','Longitude','Latitude','RelativeHeight','SigX','SigY','SigZ','AirConcentrationR','GroundConcentrationR'], 'formats':['<i8','<i8','<f4','<f4','<f4','<f4','<f4','<f4','<f4','<f4'], 'offsets':[0,8,16,20,24,28,32,36,40,44], 'itemsize':48, 'aligned':True}

Any thing I could do here? (I actually intend to change some values before writing back to disk.) The netcdf file can be found here: https://drive.google.com/open?id=1H5M3N_lv471JyOalcA3Bw_Y2ed0EX6xt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions