Coder Social home page Coder Social logo

mrtomeq / dfqtojson-converter Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 6.0 53 KB

DFQ to JSON converter is a free, open source and community-focused parser of Q-DAS ASCII transfer format.

License: MIT License

C# 100.00%
dfq json metrology q-das qdas dotnet dotnet-core csharp csharp-library

dfqtojson-converter's Introduction

DFQ to JSON Converter

DFQ to JSON Converter converts Q-DAS ASCII transfer format to JSON.

Features

  • Reads part, characteristics and measurement data according to requirements and rules stated in QDAS technical document.
  • Supports all part, characteristic and measurement K-fields
  • Supports inline and K-field notations

Technology

Entire solution is written in .NET Core. Json.NET is used for data serialization and tests are done using xUnit.

Requirements

.NET Core 1.1 SDK

License

MIT

dfqtojson-converter's People

Contributors

mrtomeq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dfqtojson-converter's Issues

Multiple smaller Issues at the conversion

Multiple smaller issues were found at the conversion of *.dfq-files:

  1. Sometimes there are double empty lines in the dfq files.
  2. At the end of file, there has to be two empty lines to cover the measurement data
  3. In all my dfq-files, there were no separation between the K10*-addresses and the K20*-addresses. The separation of line blocks has to be made by comparing the previous value with a string match.

I fixed the issues in the code below. You might integrate this solution if you want to.

public void Convert(string dfqFilePath)
{
if (string.IsNullOrEmpty(dfqFilePath))
throw new ArgumentException("dfqFilePath");

		Parts = new List<Part>();
		Characteristics = new List<Characteristic>();
		_currentPart = null;

		var dfqFile = File.ReadAllLines(dfqFilePath);
        var sectionBlock = new List<List<string>>();
		var lineBlock = new List<string>();
        

        Boolean addToLineblock = false;
        int counter = 1;

        //Create Blocks from file, then process the blocks
        for (int i=0; i<dfqFile.Length; i++)
        {
            String previousLine = "";
            String currentLine = dfqFile[i];
            String nextLine = "";

            if (i>0)
            {
                previousLine = dfqFile[i - 1];
            } 

            if (i<dfqFile.Length-1)
            {
                nextLine = dfqFile[i + 1];
            }


            //Set the separator
            //1. Empty lines
            //2. K10* to K20 if there is no line break

            //Create lineblock
            //Console.WriteLine("Create text blocks.");
            //=== Special text splitter ===//
            //Check if there is a special splitter
            Boolean textSplitActiveStart = false;
            if (previousLine.StartsWith("K10") && currentLine.StartsWith("K20"))
            {
                textSplitActiveStart = true;
                Console.WriteLine("Special text split start K10*->K20*");
            }

            //====================================//

            //Stop line block
            if (string.IsNullOrEmpty(currentLine) || textSplitActiveStart == true || string.IsNullOrEmpty(nextLine))
            {
                sectionBlock.Add(new List<String>(lineBlock));
                lineBlock.Clear();
                addToLineblock = false;
                Console.WriteLine("End line block at line: " + counter + ">" + currentLine);
            }


            //Start line block
            //Startpart is at start/run, previousline was empty or split K1*/K2 was active
            if (string.IsNullOrEmpty(currentLine)==false && (counter==1 || string.IsNullOrEmpty(previousLine)==true || textSplitActiveStart==true))
            {
                addToLineblock = true;
                Console.WriteLine("Start new lineblock at line: " + counter + ">" + currentLine);
            }

            //Add line if block is open
            if (addToLineblock==true)
            {
                lineBlock.Add(currentLine);
                Console.WriteLine("Added line to block. Line: " + counter + ">" + currentLine);
            }

            counter++;
        }

        Console.WriteLine("Created " + sectionBlock.Count() + " section blocks.");
        Console.WriteLine("Process each section block.");

        foreach (var block in sectionBlock)
        {
            if (block.Count()>0)
            {
                ProcessBlock(block);
            }
            
        }

        Console.WriteLine("Conversion finished");
	}

Enhancement of the directory structure

In order to prevent to put 100 dfq-files in the execution folder and get the json results in the same folder, it might be an idea to use a separate source and result folder. If you would like to do that, you might want to look at the method below. The files are put in the [execution folder]/dfq_source and the results are written into [execution folder]/json_result.

static void Main(string[] args)
{
var converter = new DfqConverter();

		var location = System.Reflection.Assembly.GetEntryAssembly().Location;
		var directory = Path.GetDirectoryName(location);

        var directorySource = directory + "\\dfq_source";
        var directoryResult = directory + "\\json_result";

		var files = Directory.EnumerateFiles(directorySource, "*.dfq");

		foreach (var file in files)
		{
            
            converter.Convert(file);
            
            string newPath = directoryResult + "\\" + Path.GetFileNameWithoutExtension(file) + ".json";
            

            File.WriteAllText(newPath, converter.GetJson());
		}
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.