6.4.7 Importing User Defined Data FilesImpWiz-UserDefined-File
If the data files you wish to import cannot be imported via the Import Filters ASCII or Binary files options, you can write your own Python or Origin C function to import the files.
Python Code for Data Import
Python code can be used to import a user-defined data file. A rough example of such code is presented below. The most important point about this code is that the target worksheet will be activated prior to the code being called by Origin. Additionally, the fname$ LabTalk variable will be populated with the full path and file name of the current file to import. Please read the code comments to better understand what needs to be done in the Python code.
import originpro as op
import pandas as pd
# Function reads file into a pandas DataFrame.
def read_file(file):
df = pd.DataFrame()
with open(file, 'r') as f:
line = f.readline()
while line:
# Put your code here to parse the line in the file
# and then append it to the DataFrame.
return df
# The file chosen by Origin import filter is placed into the fname$
# LabTalk variable. Must bring it into Python. Don't specify $ in name!
fname = op.get_lt_str('fname')
# Read the file into a pandas DataFrame.
data = read_file(fname)
# Returns the active sheet.
wks = op.find_sheet()
# Add DataFrame to sheet starting at first column.
# DataFrame column labels will become worksheet column long names.
# Origin automagically sets column format based on DataFrame data types.
wks.from_df(data)
After creating the code to handle the importing, you can use it as follows:
- Open the Import Wizard with the Import Wizard button or selecting Data: Import from File: Import Wizard from the Origin menu.
- On the first page (the Source page) of the Wizard, select the User Defined radio button in the Data Type group.
- Specify the path for the imported files, if you wish to import one or more files; or, select the Clipboard radio button, if you wish to import from clipboard.
- Select a target window by selecting one of the radio buttons in the Target Window group. Choose a template * and an import mode, if desired. Then click Next to go to the User Defined Filters page.
- In the User Defined Filters page, paste your code into the Python Code text area. Alternately, you can save your Python code to a file and use Python File control to specify a path to the Python file. The path can be one of:
- An absolute file path.
- A file path relative to your User Files folder.
- Just a file name if the file is in your User Files folder.
- When you finish all the settings, click Finish. Your Python code will be used to import the files.
*The target window template named on the Source page of the Import Wizard is only used when new windows are created (as would happen under some conditions during drag-and-drop importing). If choosing Data: Import from File from the menu and your active window is consistent with your import filter's Target Window specification, no new window is created and a reference to the page object for the active window is passed to your function. If the active window is of a different type, a new window will be created using the specified template, and the page reference to this new window is passed.
|
Origin C Functions for Data Import
You can develop your own Origin C function to handle the importing user-defined data files by creating an Origin C function in an Origin C file.
The prototype of Origin C the function must be either one of the following:
- int YourFunctionName(Page& pgTarget, TreeNode& trFilter, LPCSTR lpcszFile, int nFile)
- int YourFunctionName(Layer& lyTarget, TreeNode& trFilter, LPCSTR lpcszFile, int nFile)
Note: More details on how to write such Origin C functions can be found in the Importing Data chapter in Origin C Guide.
|
After writing a function to handle the importing and saving it to an Origin C file, you can use it as follows:
- Open the Import Wizard with the Import Wizard button or selecting Data: Import from File: Import Wizard from the Origin menu.
- On the first page (the Source page) of the Wizard, select the User Defined radio button in the Data Type group.
- Specify the path for the imported files, if you wish to import one or more files; or, select the Clipboard radio button, if you wish to import from clipboard.
- Select a target window by selecting one of the radio buttons in the Target Window group. Choose a template * and an import mode, if desired. Then click Next to go to the User Defined Filters page.
- In the User Defined Filters page, click the Browse button to the right of Origin C Source File. Navigate to the folder where your Origin C file is. Select the file and click Open to return to the Import Wizard. Enter the name of the function in the Origin C Function edit box. Then click Next to customize settings in other pages.
- When you finish all the settings, click Finish. Your Origin C function will be used to import the files.
*The target window template named on the Source page of the Import Wizard is only used when new windows are created (as would happen under some conditions during drag-and-drop importing). If choosing Data: Import from File from the menu and your active window is consistent with your import filter's Target Window specification, no new window is created and a reference to the page object for the active window is passed to your function. If the active window is of a different type, a new window will be created using the specified template, and the page reference to this new window is passed.
|
|