Search code examples
pythonpython-3.xfilecdf

Python: how to use glob and wildcard to open CDF files


I'm trying to open multiple .cdf files and store them in a dictonary, but when I try to use wildcard within the pycdf.CDF() command, this error is returned: spacepy.pycdf.CDFError: NO_SUCH_CDF: The specified CDF does not exist.

The .cdf files have a set initial name (instrumentfile), a date (20010101) and then a variable section (could be 1, 2, 3, or 4). This means that I can't simply write code such as:

DayCDF = pycdf.CDF('/home/location/instrumentfile'+str(dates)+'.cdf')

I also need to change the names of the variables that the .cdf data is assigned to as well, so I'm trying to import the data into a dictionary (also not sure if this is feasible).

The current code looks like this:

dictDayCDF = {}

for x in range(len(dates)):
    dictDayCDF["DayCDF"+str(x)] = pycdf.CDF('/home/location/instrumentfile'+str(dates[x])+'*.cdf')


and returns the error spacepy.pycdf.CDFError: NO_SUCH_CDF: The specified CDF does not exist.

I have also tried using glob.glob as I have seen this recommended in answers to similar questions but I have not been able to work out how to apply the command to opening .cdf files:

dictDayCDF = {}

for x in range(len(dates)):
    dictDayCDF["DayCDF"+str(x)] = pycdf.CDF(glob.glob('/home/location/instrumentfile'+str(dates[x])+'*.cdf'))


with this error being returned: ValueError: pathname must be string-like

The expected result is a dictionary of .cdf files that can be called with names DayCDF1, DayCDF2, etc that can be imported no matter the end variable section.


Solution

  • How about starting with the following code skeleton:

    import glob
    
    for file_name in glob.glob('./*.cdf'):
        print(file_name)
        #do something else with the file_name
    

    As for the root cause of the error message you're encountering: if you check the documentation of the method you're trying to use, it indicates that

    Open or create a CDF file by creating an object of this class. Parameters:

    pathname : string

    name of the file to open or create
    

    based on that, we can infer that it's expecting a single file name, not a list of file names. When you try to force a list of file names, that is, the result of using glob, it complains as you've observed.