2

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.

Emmi Starr
  • 45
  • 1
  • 6

1 Answers1

1

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.

Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
  • I jerry-rigged a basic if elif else loop based on os.path.isfile - if file exists then import the cdf, if not keep on going. There are a limited number of file endings for now but when I have time I will try and make the code a little nicer, starting with your suggestion. – Emmi Starr Sep 10 '19 at 13:47
  • If your file name pattern (that includes wildcard) is a valid one, then `glob.glob` will return all of the file names matching that pattern. Therefore, I don't think you need to use `os.path.isfile`. If it's in the list of the values returned by `glob`, then by design, it should exist (unless some other process deleted the very same file in the meantime, that is). – Emre Sevinç Sep 10 '19 at 13:56