Read CSV file and output to another CSV or text file

Jun 3, 2011 at 8:05 PM

Hi,

Now that I have the CSV files, I need to get some of the parameters and output them into another file (CSV or TXT).  For example, how do I complete the following code snippet so I can extract these data from the CSV:

private void ExportRestrictedParams(string csvfile)
{
    //Read restricted parameters from CSV file
    var dataset = sds.Dataset.Open(csvfile);                          

    var y = dataset.GetData<Single>("hrap_yor");                      //Column A
    var x = dataset.GetData<Single>("hrap_xor");                      //Column B
    var lon = dataset.GetData<Single[]>("lon");                         //Column G   
    var lat = dataset.GetData<Single[]>("lat");                           //Column H
    var precip = dataset.GetData<Int16[, ]>("amountofprecip");  //Columns GV:ON

    //Write output to csv or text file....
}

Again, I appreciate any help.

Marilyn

Developer
Jun 3, 2011 at 8:33 PM

To do that you should create an output DataSet instance, and then copy all required variables from the source DataSet. It is convenient to use DataSet.AddVariableByValue method, which creates new Variable in the target DataSet with the same rank and data type as the source variable and copies all the data and metadata from the variable to the created variable. So we can complete your sample:

private void ExportRestrictedParams(string csvfile)
{
    //Read restricted parameters from CSV file
    var dataset = sds.Dataset.Open(csvfile);                          

    var y = dataset["hrap_yor"];                      //Column A
    var x = dataset["hrap_xor"];                      //Column B
    var lon = dataset["lon"];                         //Column G    
    var lat = dataset["lat"];                           //Column H
    var precip = dataset["amountofprecip"];  //Columns GV:ON

    //Write output to csv or text file....
    using(var dst = sds.DataSet.Open("another.csv?openMode=create"))
    {
        dst.AddVariableByValue(y);
        dst.AddVariableByValue(x);
        dst.AddVariableByValue(lon);
        dst.AddVariableByValue(lat);
        dst.AddVariableByValue(precip);
    }
}

By default, output CSV file will be appended with an extra information, containing dimensions names, metadata etc. 

If you need just plain csv, you should use "appendMetadata=false" parameter when opening a DataSet:

var dst = sds.DataSet.Open("another.csv?openMode=create&appendMetadata=false")

Full list of possible parameters is given in the Scientific DataSet Reference in the section "Microsoft.Research.Science.Data.CSV Namespace", in  the summary for CsvDataSet class. The reference is available in the Scientific DataSet 1.2 folder in the Start/Programs.

 

Regards,

Dmitry.

 

 

 

Jun 3, 2011 at 9:05 PM

Hi,

God..you're WONDERFUL!!! THANK YOU.  THANK YOU.  THANK YOU!

Marilyn