Duplicating a wave, same name, different folder

How can I properly duplicate a wave into a new folder? i.e.,

    SetDataFolder $DataFolderName
    Duplicate root:wave0, wave0


is what I think should work, but I get an error about trying to duplicate into the same wave, even though my intent is to duplicate root:wave0 to root:DataFolderName:wave0. DataFolderName is distinct from root.
Duplicate/O ...

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
I think my error had to do with invoking Wave (i.e., wave wave0) in the procedure prior to using the duplicate command. I still get confused about when and how to properly use Wave to pass an existing wave into a procedure. It appears that I don't need to use Wave at all! Any comment on that? I always seek to better understand the wave command.

Your reminder to use /O was very helpful. Inevitably, I will forget to actually load the data. I should also mention that since the procedure is in active transition, I intend to label the subfolder "wave0" with something more sensible, like "RawData". And all of this is in a subfolder that contains date and file index (from that particular day, e.g., 20161102_04).
patarroyo wrote:
I think my error had to do with invoking Wave (i.e., wave wave0) in the procedure prior to using the duplicate command.

Yes, you didn't post your complete function, but if it was like this:
Function test()

    Wave junk
    SetDataFolder RawData
    Duplicate root:junk, junk
end

then, yes, it is the WAVE statement that was responsible.

WAVE statements do two different things, one at compile time and one at run time. At compile time, it establishes a name as an alias for a wave that doesn't necessarily exist at the time. The WAVE statement allows Igor to compile wave handling code when it sees that name later in the code.

At run time, when the WAVE statement is encountered, it causes Igor to look up a wave with the specified name and causes that name to be associated with a real wave. In this case, the real wave is the one in the root folder, so the Duplicate command is asked to duplicate root:junk onto the wave previously looked up, which is root:junk. The wave reference doesn't stop pointing to the original wave just because you changed the data folder.
Quote:
I still get confused about when and how to properly use Wave to pass an existing wave into a procedure. It appears that I don't need to use Wave at all! Any comment on that? I always seek to better understand the wave command.

In addition to my explanation above, there are some convenience behaviors that can be confusing.
The basic WAVE statement works like WAVE localname = root:RawData:globalname. That establishes "localname" as an alias at run time for "globalname" in a data folder that isn't necessarily the current data folder. But Igor also allows a shortcut: WAVE globalname. That makes a local alias "globalname" for a wave called "globalname" in the current data folder. The confusing part is that the localname is the same as the real globalname, but in fact, in the function "globalname" is a local alias that happens to have the same name as the actual wave.

Another source of confusion is that some commands that create waves can sometimes make a wave reference. In particular, Make and Duplicate (and some others) if you give it a literal name will make a wave reference for you without requiring a WAVE statement. So Make myWavewill make a wave in the current data folder called "myWave" and it also creates a wave reference with the local alias name "myWave", just as if you had used the shortcut WAVE myWave.

Completely clear now, right? :)

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Quote:
I still get confused about when and how to properly use Wave to pass an existing wave into a procedure.


The rules are somewhat complex and depend on the rtGlobals statement in your procedure file.

For the complete story, execute this:
DisplayHelpTopic "Accessing Global Variables And Waves"


For simplicity, I recommend you follow this rule:

Use a wave statement to declare all waves used in a user-defined function with the optional exception that you can omit the wave statement after a Make or Duplicate statement that uses a simple name.


"Simple name" means just a standard (not liberal) wave name (e.g., wave0) with no path (root:wave0 is not a simple name) and with no use of strings or $.

Examples:
Function Demo()
    // Create sample data using liberal name which are more exacting.
    // The single quotes used below are required only for liberal names.
    SetDataFolder root:
    Make/O root:'A wave'                    // Spaces, dots and so on make a name liberal
    NewDataFolder/O 'Data Folder'       // Spaces, dots and so on make a name liberal
    Make/O root:'Data Folder':'Another wave'

    String stringContainingNameOfWave = "A wave"
    String stringContainingNameOfDataFolder = "Data Folder"
    String stringContainingNameOfAnotherWave = "Another wave"
   
    // Wave references using a string

    WAVE w = $stringContainingNameOfWave    // References wave in current data folder
    WaveStats/Q w

    String path = "root:'Data Folder':'Another wave'"
    Wave w = $path                              // References wave in another data folder
    WaveStats/Q w
   
    path = "root:'Data Folder':" + PossiblyQuoteName(stringContainingNameOfAnotherWave)
    Wave w = $path                              // References wave in another data folder
    WaveStats/Q w
   
    Wave w = root:$(stringContainingNameOfDataFolder):'Another wave'        // References wave in another data folder
    WaveStats/Q w
   
    DFREF dfr = $path
    Wave w = dfr:$(stringContainingNameOfWave)
    WaveStats/Q w
   
    // Wave references using literal names
   
    Wave w = 'A wave'                           // References wave in current data folder
    WaveStats/Q w
   
    Wave w = :'A wave'                          // References wave in current data folder
    WaveStats/Q w
   
    Wave w = root:'Data Folder':'Another wave'      // References wave in another data folder
    WaveStats/Q w
   
    Wave w = :'Data Folder':'Another wave'          // References wave in another data folder
    WaveStats/Q w
   
    // Wave references using simple, standard wave names
    // Make and Duplicate automatically creates wave references only when
    // the destination is a simple wave name (e.g., wave0)
   
    Make/O wave0                        // Igor automatically creates a wave reference named wave0
    Wave wave0                      // This is optional and is usually omitted
    WaveStats/Q wave0
   
    Duplicate/O wave0, wave1        // Igor automatically creates a wave reference named wave0
    Wave wave1                      // This is optional and is usually omitted
    WaveStats/Q wave1
   
    KillWaves wave0, wave1
End