2

In my script, I saved an ImageDocument to my local drive as a Gatan file. I've used the command ImageDocumentGetCurrentFile() to get that file's path. Now I'm trying to load that file back into my DM workspace using OpenFileForReadAndWrite() with the path I just retrieved, and I'm told "The system cannot find the file specified." (I've also tried OpenImage() and landed at the same issue).

I was able to pass the path into my command prompt, and it was able to recognize the path output by ImageDocumentGetCurrentFile() as valid and open the Gatan file. So, the path seems valid and it doesn't seem to be a problem with my system.

I was curious if anyone else has tips for resolving this issue so that I can open a locally saved file.

Ultimately, my goal is to temporarily remove an image document from my workspace so I can run the menu command "Spectrum > Assign ROI as > Background", which for whatever reason breaks down when images of this type are present (parent images). I've tried calling ImageDocumentHide() on the document before executing the menu command and then ImageDocumentShow() afterwards. This successfully allows the menu command to be executed; however, I learned that when the ImageDocument is shown, the image that was previously displayed is now just a blank white page. I'm not sure what's causing this either.

BmyGuest
  • 6,331
  • 1
  • 21
  • 35
  • 1
    I can't comment on your actual use-case without some example code and better explanation of what fails. But you might want to ask that as a separate question, if it is still of interest to you. – BmyGuest Jul 18 '23 at 21:28
  • A little more detail about your overall goal would also be helpful. You seem mainly to be running into problems while trying to operate DM the way a user would, but doing so via scripts. In most cases, using scripts to trigger menu commands is not the most efficient and flexible approach. Please spell out your use case in more detail. For example, I don't understand what you mean by "parent images" and how they are involved in what you are trying to do. – Mike Kundmann Jul 18 '23 at 23:36
  • 1
    Right. So my goal is to take a LinePlotImageDisplay that has upwards of 5 slices, all corresponding to separate spectra, and then be able to use script to batch perform background subtraction and signal extraction on all of them so that the user ends up with a clean LinePlotImageDisplay containing all of the slices' extracted signals for comparison. I still don't know any way to call background subtraction without using the menu command (I found on Dave Mitchell's script library a background subtraction function, but that one led to slightly different results). – Michael Ngo Jul 19 '23 at 13:45
  • 1
    What I mean by "parent image" is the STEM SI ImageDocument which I draw ROIs on to generate the individual spectra, i.e., slices. If it's loaded in the current workspace, when you call Spectrum > Extract > Signal, a popup appears saying "The selected spectrum has a parent spectrum-image. Apply to the single spectrum or parent spectrum-image?" This normally isn't a problem if the user is manually performing this, but when my script runs the command and the popup appears, after the user selects "apply to single spectrum," an error message is displayed saying "Please select a valid spectrum." – Michael Ngo Jul 19 '23 at 13:53
  • 1
    @MichaelNgo Your further details suggest that your question seeks a work-around for something tangential to your actual goal. I would recommend that you post a new question that is focussed on your main goal, e.g. something like "How to extract and compare signals from multiple spectra?" As you have already found, this is surprisingly non-trivial in DM. You were on the right track with your previous question about "Separating and combining ImageDisplay layers". Check out the EELSSubtractPowerLawBackground function, covered in the on-line help at EELS > EELS Analysis > Script Interface. – Mike Kundmann Jul 19 '23 at 17:50
  • Like Mike said: Open a new question. But also: Depending on the exact use-case, you might consider placing all spectra into a single 2D "LineScan SI" like image to then harness GMS internal tools for processing all spectra of an SI. When you say "5+ spectra in one LPID" where do they come from? Individually acquired and added together? Are they all of same size and calibration or truly independent? – BmyGuest Jul 20 '23 at 17:45

1 Answers1

1

Neither OpenFileForReadAndWrite nor OpenImage is the correct command to open an ImageDocument. The first opens a generic raw-file, f.e. to be used as a stream, while the latter is an obsolete wrapper from early days which has problems. It does not load the whole file, but only the (primary) image data from it.

You want to use NewImageDocumentFromFile like in:

string path = "C:/temp/"
image test:=Realimage("MyImg",4,100,100)
test.ShowImage()
imagedocument doc = test.ImageGetOrCreateImageDocument()
doc.ImageDocumentSetCurrentFile("C:/temp/MyImg")
doc.ImageDocumentSetCurrentFileSaveFormat("Gatan Format")
doc.ImageDocumentSave(0)
string savedAs = doc.ImageDocumentGetCurrentFile()
doc.ImageDocumentClose(0)
OKDialog("Do something")
doc = NewImageDocumentFromFile(savedAs)
doc.ImageDocumentShow()

Also: The examples section in the F1 help might be a useful starting point to learn more about files as well: F1 help

BmyGuest
  • 6,331
  • 1
  • 21
  • 35
  • I see, it makes sense that NewImageDocumentFromFile() would be the proper command to use, but I'm still getting the same error message: "The system cannot find the file specified." Something that might be important to note is that the file location I'm trying to open has about 6 ancestor directories, i.e., the path to the file contains 6 directories before the file itself. I also noticed that when I call "DoesFileExist()" on a first portion of the path, it returns true with the first 2 directories as input, but false with 3 or more. Is there an upper limit in # of directories that DM can open? – Michael Ngo Jul 19 '23 at 14:02
  • No there isn't. If "DoesFileExist(path)" does not return true, the command *will* fail. Be aware that file-paths in GMS either have to use the / like in `C:/temp/myfile.dm4` or they must *escape* the slash as in `C:\\temp\\myfiledm4` Also, some commands require the files suffix, some do not, and some mustn't include them. – BmyGuest Jul 20 '23 at 15:42
  • @MichaelNgo There *might* be a maximum length of a file-path, but I never ran into such a limitation myself. You should definitely be fine up to 256 characters. – BmyGuest Jul 20 '23 at 15:43