Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine how to handle "write_var" with incorrect dimension sizes #291

Open
bryan-harter opened this issue Jan 15, 2025 · 0 comments
Open

Comments

@bryan-harter
Copy link
Member

The following is a minimal code example of how write_var could cause the input and output to look different.

import os
import numpy as np
import xarray as xr
import urllib
from cdflib.xarray import cdf_to_xarray, xarray_to_cdf
import cdflib 
PATH='hello.cdf'
with cdflib.cdfwrite.CDF(PATH, delete=True, cdf_spec={'Compressed': 0}) as cdf:
    NA_CHAR = np.array([['11', '12'], ['21', '22'], ['31', '32']])
    cdflib_data = NA_CHAR.tolist()     # Converting to LIST.
    print(f'NA_CHAR.shape = {NA_CHAR.shape}')
    print(f'cdflib_data = """"{cdflib_data}""""')
    cdf.write_var(
        {
            'Variable': 'ZV_CHAR',
            'Data_Type': cdf.CDF_CHAR,
            'Num_Elements': 3,
            'Dim_Sizes': (3,),
            'Rec_Vary': (True, True),  # Req., but docs says only for rVars?!
            'Var_Type': 'zVariable',
            'Compress': 0,
        },
        var_data=NA_CHAR,
    )

cdf = cdflib.cdfread.CDF(PATH)
act_na = cdf.varget('ZV_CHAR')
act_xna = cdf_to_xarray(PATH).get('ZV_CHAR').data

print(f'act_na.shape  = """"{act_na.shape}""""')
print(f'act_xna.shape = """"{act_xna.shape}""""')
print(f'act_na  = """"{act_na}""""')
print(f'act_xna = """"{act_xna}""""')

Questions:

  1. How do other libraries handle this, such as the main C library and IDL?
  2. Should this throw an error, print out a warning, or should we allow the data to be reformatted to look differently from how it is inserted?
  3. Does this behavior continue for numerical inputs, or just character?
  4. What if rec_vary is set to false, does it allow a corrupted CDF file to be created?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant