> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-vi-scada/datran-database-import-utility/import-csv.md).

# Import CSV data from other data loggers

Import CSV files from third-party data loggers into the DATRAN database: make a map file linking CSV columns to DATRAN fields, then import.

Although many software applications can export to CSV Format, this format has no rigid structure which means that a CSV file exported from one application will, most likely, not be able to be imported in another. It is because of this an intermediate file is required. This provides the mappings between the various fields within the CSV file and the required fields in the DATRAN Database. The Import Folder feature allows all files in a directory to be imported using the same map file.

{% hint style="info" %}
**Applies to:** Database Import Utility V1.x. It is fully compatible with the CSV format from QTech's Q54 Data Logger, DATRAN RTU Enhanced Data Logging (EDL), Trutrack OmniLog compliant Data Loggers and Primayer XiLog, and has flexible CSV column configuration for compatibility with most CSV formats.
{% endhint %}

## What it handles

| Your CSV file has                                                 | How to handle it                                                                                                                         |
| ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| A header row naming the columns                                   | Tick **Column headers on row above first data row**. The column headers are then used both in the grid and the Database Mapping section. |
| The site name in the first row                                    | Tick **First row contains site name**. This creates a fictitious data column that can be used for the Site name.                         |
| Data starting further down                                        | Change the value in the **Data starts on row** edit box.                                                                                 |
| Date and time in separate columns                                 | Map both the Date and the Time fields to the **Timestamp** database field.                                                               |
| Several values per row, such as air temperature and soil moisture | Select the **Number of data columns** required. Each mapping is then represented in a new tab.                                           |
| Tab, space or semicolon separators                                | The **Field Delimiter** radio buttons allow the alternative to be specified.                                                             |
| Fixed-width columns, or information only in the file name         | Not currently supported                                                                                                                  |

## DATRAN database fields

| Field           | What it is                                                                                                                                                                                                                                                                           |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Site name       | This represents the site name or location where the data came from. Sometimes it can be extracted from the CSV file if it is present as either the first row or one of the CSV columns. If this information is not present in the CSV file then it will need to be entered manually. |
| Equipment name  | Represents the type of equipment that provided the data, e.g. Flow Meter. If this information is not present in the CSV file then it will need to be entered manually.                                                                                                               |
| Point           | The name of the record, e.g. Pump Hours. If this information is not present in the CSV file then it will need to be entered manually.                                                                                                                                                |
| Timestamp       | The date and time of the record. Note that the DATRAN database operates at a resolution of 1 second and timestamps must be unique. If the CSV file contains 2 or more identical timestamps then the import will fail due to a duplicate entry in the database.                       |
| Value           | Represents the instantaneous value of the Point at the given Timestamp. For digital points the only allowed values are 0 or 1.                                                                                                                                                       |
| Units, Min, Max | Analogue points only. Units allows the units of the Value to be specified e.g. l/s, %. Min and Max are used by DATRAN Trending to define the lowest and uppermost value for the vertical axis for this Point.                                                                        |

## Create a map file

1. The map file editor can be invoked by either clicking on the **Create/Edit** button from the main window or selecting **New Mapping** from the **File** menu. Select the **CSV** file as source file type.
2. Under **CSV Import**, load your CSV file using the **Open Sample CSV** button. The grid then shows the first few rows and columns of CSV file using the current settings.
3. Set the header, site name, start row and delimiter options. After changing any of these settings press the **Refresh** button to update the contents of the grid.
4. Under **Database Mapping**, the mapping type of either **Digital** or **Analogue** needs to be selected first as the database fields are slightly different for each of these types.
5. Links are created by clicking on the CSV Columns selector (triangle) on the left hand side and then clicking on the Database Fields selector on the right hand side. Alternatively clicking on a column in the CSV Import grid will also highlight the appropriate left hand side selector. Deleting a link follows the same procedure as for creating a link. When a link is selected a subsequent time it is toggled.
6. If any of the required fields cannot be mapped from the CSV columns then that can be assigned a fixed text value. Fields in square brackets represent references to columns within the CSV file. Multiple CSV columns can be combined into a single database field.
7. The example data shows what the data would look like for a single record in the DATRAN database. If any of the edit boxes were manually changed you will need to press the **Refresh** button to update these examples. In the case of any errors, for example the time format cannot be parsed or the digital value is not either 0 or 1, the example data column will show the error in red.
8. Click **Save Mapping**. The mapping cannot be saved if any errors are detected. In this case fix the problems and press the save button again.

![Mapping CSV columns to DATRAN database fields](https://3621580088-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5hMAUl7E1cWeN5Rghg0%2Fuploads%2FvovPl4uYMwJaqlB46g78%2Fdiu-database-mapping.png?alt=media)

## Import

1. Click the **Browse** button or click **Open mapping** from the **File** menu to load the map file created in the previous step.
2. Press the **Import File** button. Navigate to the folder containing the CSV file to import from. If multiple CSV files are to be imported use the CTRL or SHIFT keys to select one or more files. Click **Open** to commence the import process.
3. The **Status Report** area shows the progress of import along with any errors (displayed in red). This includes the names of the files being imported, how many rows have been imported and any errors that occurred during the import.

If there are any errors then the import is aborted and any database changes are rolled back. This puts the database into a known state and allows you to correct the problem and then re-import with the knowledge that duplicate database entries will not occur. All records imported from that CSV file are removed from the database. When an import is in progress the **Cancel** button is enabled. If this is pressed during an import the database is rolled back in the same way as if an error occurred.
