QuantumATK Forum

QuantumATK => General Questions and Answers => Topic started by: jdgayles16 on August 12, 2010, 02:30

Title: Vnl 10.8
Post by: jdgayles16 on August 12, 2010, 02:30
How does one get the labels/names to show on the new atk?

Thanks
Jacob
Title: Re: Vnl 10.8
Post by: Nordland on August 12, 2010, 11:31
Are you talking about the ID or the labels?
Title: Re: Vnl 10.8
Post by: Anders Blom on August 12, 2010, 11:35
Labels cannot be read (yet), so for now we recommend using IDs only. They can be formatted as labels anyway, as long as you remember to make them unique all the time!
Title: Re: Vnl 10.8
Post by: Nordland on August 12, 2010, 11:56
The following code should do the trick
Code: python
def nllabel(filename):
    """
    Function for getting a dictionary with the labels associate to each id.
    @param filename The filename to be inspect.
    """
    # Import needed modules.
    from NL.IO.NetCDFFile import NetCDFFile
    from NL.IO.NLSaveUtilities import dataDictFromKeys
    
    # Setup return object.
    result = {}
    # Setup the NetCDF file for reading.
    nc = NetCDFFile(filename, 'r', mmap=False)
    # Find the ids.
    ids = numpy.unique( [ key.split('_')[1] for key in nc.variables.keys() ] )
    # Loop over all the ids.
    for id in ids:
        # Needed keys for object
        needed_key = [ key for key in nc.variables.keys() if id == key.split('_')[1] ][0]
        if hasattr(nc.variables[needed_key], 'user_label'):
            result[id] = nc.variables[needed_key].user_label
    # Return the complete list.
    return result

print nllabel('analysis.nc')