void bi::ParticleMCMCNetCDFBuffer::map(const long P, const long T) {
  std::string name;
  int id, i;
  VarType type;
  Var* var;
  Dim* dim;

  /* dimensions */
  BI_ERROR_MSG(hasDim("nr"), "File must have nr dimension");
  nrDim = mapDim("nr", T);
  for (i = 0; i < m.getNumDims(); ++i) {
    dim = m.getDim(i);
    BI_ERROR_MSG(hasDim(dim->getName().c_str()), "File must have " <<
        dim->getName() << " dimension");
    nDims.push_back(mapDim(dim->getName().c_str(), dim->getSize()));
  }
  BI_ERROR_MSG(hasDim("np"), "File must have np dimension");
  npDim = mapDim("np", P);

  /* time variable */
  tVar = ncFile->get_var("time");
  BI_ERROR_MSG(tVar != NULL && tVar->is_valid(),
      "File does not contain variable time");
  BI_ERROR_MSG(tVar->num_dims() == 1, "Variable time has " << tVar->num_dims() <<
      " dimensions, should have 1");
  BI_ERROR_MSG(tVar->get_dim(0) == nrDim, "Dimension 0 of variable time should be nr");

  /* other variables */
  for (i = 0; i < NUM_VAR_TYPES; ++i) {
    type = static_cast<VarType>(i);
    if (type == D_VAR || type == R_VAR || type == P_VAR) {
      vars[type].resize(m.getNumVars(type), NULL);
      for (id = 0; id < m.getNumVars(type); ++id) {
        var = m.getVar(type, id);
        if (hasVar(var->getOutputName().c_str())) {
          vars[type][id] = mapVar(m.getVar(type, id));
        }
      }
    }
  }

  llVar = ncFile->get_var("loglikelihood");
  BI_ERROR_MSG(llVar != NULL && llVar->is_valid(),
      "File does not contain variable loglikelihood");
  BI_ERROR_MSG(llVar->num_dims() == 1, "Variable loglikelihood has " <<
      llVar->num_dims() << " dimensions, should have 1");
  BI_ERROR_MSG(llVar->get_dim(0) == npDim,
      "Dimension 0 of variable loglikelihood should be np");

  lpVar = ncFile->get_var("logprior");
  BI_ERROR_MSG(lpVar != NULL && lpVar->is_valid(),
      "File does not contain variable logprior");
  BI_ERROR_MSG(lpVar->num_dims() == 1, "Variable logprior has " <<
      lpVar->num_dims() << " dimensions, should have 1");
  BI_ERROR_MSG(lpVar->get_dim(0) == npDim,
      "Dimension 0 of variable logprior should be np");

}
void DXF2BRD_CONVERTER::addPolyline(const DRW_Polyline& aData )
{
    // Currently, Pcbnew does not know polylines, for boards.
    // So we have to convert a polyline to a set of segments.
    // Obviously, the z coordinate is ignored

    wxPoint polyline_startpoint;
    wxPoint segment_startpoint;

    for( unsigned ii = 0; ii < aData.vertlist.size(); ii++ )
    {
        DRW_Vertex* vertex = aData.vertlist[ii];

        if( ii == 0 )
        {
            segment_startpoint.x = mapX( vertex->basePoint.x );
            segment_startpoint.y = mapY( vertex->basePoint.y );
            polyline_startpoint  = segment_startpoint;
            continue;
        }

        DRAWSEGMENT* segm = ( m_useModuleItems ) ?
                            static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) :
                            new DRAWSEGMENT;

        segm->SetLayer( ToLAYER_ID( m_brdLayer ) );
        segm->SetStart( segment_startpoint );
        wxPoint segment_endpoint( mapX( vertex->basePoint.x ), mapY( vertex->basePoint.y ) );
        segm->SetEnd( segment_endpoint );
        segm->SetWidth( mapDim( aData.thickness == 0 ? m_defaultThickness
                                : aData.thickness ) );
        m_newItemsList.push_back( segm );
        segment_startpoint = segment_endpoint;
    }

    // Polyline flags bit 0 indicates closed (1) or open (0) polyline
    if( aData.flags & 1 )
    {
        DRAWSEGMENT* closing_segm = ( m_useModuleItems ) ?
                                    static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) :
                                    new DRAWSEGMENT;

        closing_segm->SetLayer( ToLAYER_ID( m_brdLayer ) );
        closing_segm->SetStart( segment_startpoint );
        closing_segm->SetEnd( polyline_startpoint );
        closing_segm->SetWidth( mapDim( aData.thickness == 0 ? m_defaultThickness
                                : aData.thickness ) );
        m_newItemsList.push_back( closing_segm );
    }
}
/*
 * Import Arc entities.
 */
void DXF2BRD_CONVERTER::addArc( const DRW_Arc& data )
{
    DRAWSEGMENT* segm = ( m_useModuleItems ) ?
                        static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) : new DRAWSEGMENT;

    segm->SetLayer( ToLAYER_ID( m_brdLayer ) );
    segm->SetShape( S_ARC );

    // Init arc centre:
    wxPoint center( mapX( data.basePoint.x ), mapY( data.basePoint.y ) );
    segm->SetCenter( center );

    // Init arc start point
    double  arcStartx   = data.radious;
    double  arcStarty   = 0;
    double  startangle = data.staangle;
    double  endangle = data.endangle;

    RotatePoint( &arcStartx, &arcStarty, -RAD2DECIDEG( startangle ) );
    wxPoint arcStart( mapX( arcStartx + data.basePoint.x ),
                      mapY( arcStarty + data.basePoint.y ) );
    segm->SetArcStart( arcStart );

    // calculate arc angle (arcs are CCW, and should be < 0 in Pcbnew)
    double angle = -RAD2DECIDEG( endangle - startangle );

    if( angle > 0.0 )
        angle -= 3600.0;

    segm->SetAngle( angle );

    segm->SetWidth( mapDim( data.thickness == 0 ? m_defaultThickness : data.thickness ) );
    m_newItemsList.push_back( segm );
}
/*
 * Import Arc entities.
 */
void DXF2BRD_CONVERTER::addArc( const DRW_Arc& data )
{
    DRAWSEGMENT* segm = new DRAWSEGMENT( m_brd );

    segm->SetLayer( m_brdLayer );
    segm->SetShape( S_ARC );

    // Init arc centre:
    wxPoint center( mapX( data.basePoint.x ), mapY( data.basePoint.y ) );
    segm->SetCenter( center );

    // Init arc start point
    double  arcStartx   = data.radious;
    double  arcStarty   = 0;
    double  startangle = data.staangle;
    double  endangle = data.endangle;

    RotatePoint( &arcStartx, &arcStarty, -RAD2DECIDEG( startangle ) );
    wxPoint arcStart( mapX( arcStartx + data.basePoint.x ),
                      mapY( arcStarty + data.basePoint.y ) );
    segm->SetArcStart( arcStart );

    // calculate arc angle (arcs are CCW, and should be < 0 in Pcbnew)
    double angle = -RAD2DECIDEG( endangle - startangle );

    if( angle > 0.0 )
        angle -= 3600.0;

    segm->SetAngle( angle );

    segm->SetWidth( mapDim( data.thickness == 0 ? m_defaultThickness
                            : data.thickness ) );
    appendToBoard( segm );
}
void DXF2BRD_CONVERTER::addLine( const DRW_Line& aData )
{
    DRAWSEGMENT* segm = ( m_useModuleItems ) ?
                        static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) : new DRAWSEGMENT;

    segm->SetLayer( ToLAYER_ID( m_brdLayer ) );
    wxPoint start( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) );
    segm->SetStart( start );
    wxPoint end( mapX( aData.secPoint.x ), mapY( aData.secPoint.y ) );
    segm->SetEnd( end );
    segm->SetWidth( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) );
    m_newItemsList.push_back( segm );
}
/*
 * Import line entities.
 */
void DXF2BRD_CONVERTER::addLine( const DRW_Line& data )
{
    DRAWSEGMENT*    segm = new DRAWSEGMENT( m_brd );

    segm->SetLayer( m_brdLayer );
    wxPoint         start( mapX( data.basePoint.x ), mapY( data.basePoint.y ) );
    segm->SetStart( start );
    wxPoint         end( mapX( data.secPoint.x ), mapY( data.secPoint.y ) );
    segm->SetEnd( end );
    segm->SetWidth( mapDim( data.thickness == 0 ? m_defaultThickness
                            : data.thickness ) );
    appendToBoard( segm );
}
void DXF2BRD_CONVERTER::addCircle( const DRW_Circle& aData )
{
    DRAWSEGMENT* segm = ( m_useModuleItems ) ?
                        static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) : new DRAWSEGMENT;

    segm->SetLayer( ToLAYER_ID( m_brdLayer ) );
    segm->SetShape( S_CIRCLE );
    wxPoint center( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) );
    segm->SetCenter( center );
    wxPoint circle_start( mapX( aData.basePoint.x + aData.radious ), mapY( aData.basePoint.y ) );
    segm->SetArcStart( circle_start );
    segm->SetWidth( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) );
    m_newItemsList.push_back( segm );
}
/*
 * Import Circle entities.
 */
void DXF2BRD_CONVERTER::addCircle( const DRW_Circle& data )
{
    DRAWSEGMENT* segm = new DRAWSEGMENT( m_brd );

    segm->SetLayer( m_brdLayer );
    segm->SetShape( S_CIRCLE );
    wxPoint center( mapX( data.basePoint.x ), mapY( data.basePoint.y ) );
    segm->SetCenter( center );
    wxPoint circle_start( mapX( data.basePoint.x + data.radious ),
                          mapY( data.basePoint.y ) );
    segm->SetArcStart( circle_start );
    segm->SetWidth( mapDim( data.thickness == 0 ? m_defaultThickness
                            : data.thickness ) );
    appendToBoard( segm );
}
void DXF2BRD_CONVERTER::addLWPolyline(const DRW_LWPolyline& aData )
{
    // Currently, Pcbnew does not know polylines, for boards.
    // So we have to convert a polyline to a set of segments.
    // The import is a simplified import: the width of segment is
    // (obviously constant and is the width of the DRW_LWPolyline.
    // the variable width of each vertex (when exists) is not used.
    wxRealPoint seg_start;
    wxRealPoint poly_start;
    double bulge = 0.0;
    int lineWidth = mapDim( aData.thickness == 0 ? m_defaultThickness
                            : aData.thickness );

    for( unsigned ii = 0; ii < aData.vertlist.size(); ii++ )
    {
        DRW_Vertex2D* vertex = aData.vertlist[ii];

        if( ii == 0 )
        {
            seg_start.x = m_xOffset + vertex->x * m_DXF2mm;
            seg_start.y = m_yOffset - vertex->y * m_DXF2mm;
            bulge = vertex->bulge;
            poly_start = seg_start;
            continue;
        }

        wxRealPoint seg_end( m_xOffset + vertex->x * m_DXF2mm, m_yOffset - vertex->y * m_DXF2mm );

        if( std::abs( bulge ) < MIN_BULGE )
            insertLine( seg_start, seg_end, lineWidth );
        else
            insertArc( seg_start, seg_end, bulge, lineWidth );

        bulge = vertex->bulge;
        seg_start = seg_end;
    }

    // LWPolyline flags bit 0 indicates closed (1) or open (0) polyline
    if( aData.flags & 1 )
    {
        if( std::abs( bulge ) < MIN_BULGE )
            insertLine( seg_start, poly_start, lineWidth );
        else
            insertArc( seg_start, poly_start, bulge, lineWidth );
    }
}
void DXF2BRD_CONVERTER::addMText( const DRW_MText& aData )
{
    wxString    text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) );
    wxString    attrib, tmp;

    /* Some texts start by '\' and have formating chars (font name, font option...)
     *  ending with ';'
     *  Here are some mtext formatting codes:
     *  Format code        Purpose
     * \0...\o            Turns overline on and off
     *  \L...\l            Turns underline on and off
     * \~                 Inserts a nonbreaking space
     \\                 Inserts a backslash
     \\\{...\}            Inserts an opening and closing brace
     \\ \File name;        Changes to the specified font file
     \\ \Hvalue;           Changes to the text height specified in drawing units
     \\ \Hvaluex;          Changes the text height to a multiple of the current text height
     \\ \S...^...;         Stacks the subsequent text at the \, #, or ^ symbol
     \\ \Tvalue;           Adjusts the space between characters, from.75 to 4 times
     \\ \Qangle;           Changes obliquing angle
     \\ \Wvalue;           Changes width factor to produce wide text
     \\ \A                 Sets the alignment value; valid values: 0, 1, 2 (bottom, center, top)    while( text.StartsWith( wxT("\\") ) )
     */
    while( text.StartsWith( wxT( "\\" ) ) )
    {
        attrib << text.BeforeFirst( ';' );
        tmp     = text.AfterFirst( ';' );
        text    = tmp;
    }

    BOARD_ITEM* brdItem;
    EDA_TEXT* textItem;

    if( m_useModuleItems )
    {
        TEXTE_MODULE* modText = new TEXTE_MODULE( NULL );
        brdItem = static_cast< BOARD_ITEM* >( modText );
        textItem = static_cast< EDA_TEXT* >( modText );
    }
    else
    {
        TEXTE_PCB* pcbText = new TEXTE_PCB( NULL );
        brdItem = static_cast< BOARD_ITEM* >( pcbText );
        textItem = static_cast< EDA_TEXT* >( pcbText );
    }

    brdItem->SetLayer( ToLAYER_ID( m_brdLayer ) );
    wxPoint     textpos( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) );

    textItem->SetTextPos( textpos );
    textItem->SetTextAngle( aData.angle * 10 );

    // The 0.8 factor gives a better height/width ratio with our font
    textItem->SetTextWidth( mapDim( aData.height * 0.8 ) );
    textItem->SetTextHeight( mapDim( aData.height ) );
    textItem->SetThickness( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) );
    textItem->SetText( text );

    // Initialize text justifications:
    if( aData.textgen <= 3 )
    {
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
    }
    else if( aData.textgen <= 6 )
    {
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER );
    }
    else
    {
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
    }

    if( aData.textgen % 3 == 1 )
    {
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
    }
    else if( aData.textgen % 3 == 2 )
    {
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
    }
    else
    {
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
    }

#if 0   // These setting have no mening in Pcbnew
    if( data.alignH == 1 )
    {
        // Text is left to right;
    }
    else if( data.alignH == 3 )
    {
        // Text is top to bottom;
    }
    else
    {
        // use ByStyle;
    }

    if( aData.alignV == 1 )
    {
        // use AtLeast;
    }
    else
    {
        // useExact;
    }
#endif

    m_newItemsList.push_back( static_cast< BOARD_ITEM* >( brdItem ) );
}
void DXF2BRD_CONVERTER::addText( const DRW_Text& aData )
{
    BOARD_ITEM* brdItem;
    EDA_TEXT* textItem;

    if( m_useModuleItems )
    {
        TEXTE_MODULE* modText = new TEXTE_MODULE( NULL );
        brdItem = static_cast< BOARD_ITEM* >( modText );
        textItem = static_cast< EDA_TEXT* >( modText );
    }
    else
    {
        TEXTE_PCB* pcbText = new TEXTE_PCB( NULL );
        brdItem = static_cast< BOARD_ITEM* >( pcbText );
        textItem = static_cast< EDA_TEXT* >( pcbText );
    }

    brdItem->SetLayer( ToLAYER_ID( m_brdLayer ) );

    wxPoint refPoint( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) );
    wxPoint secPoint( mapX( aData.secPoint.x ), mapY( aData.secPoint.y ) );

    if( aData.alignV != 0 || aData.alignH != 0 || aData.alignH == DRW_Text::HMiddle )
    {
        if( aData.alignH != DRW_Text::HAligned && aData.alignH != DRW_Text::HFit )
        {
            wxPoint tmp = secPoint;
            secPoint = refPoint;
            refPoint = tmp;
        }
    }

    switch( aData.alignV )
    {
    case DRW_Text::VBaseLine:
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
        break;

    case DRW_Text::VBottom:
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
        break;

    case DRW_Text::VMiddle:
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER );
        break;

    case DRW_Text::VTop:
        textItem->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
        break;
    }

    switch( aData.alignH )
    {
    case DRW_Text::HLeft:
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
        break;

    case DRW_Text::HCenter:
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
        break;

    case DRW_Text::HRight:
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
        break;

    case DRW_Text::HAligned:
        // no equivalent options in text pcb.
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
        break;

    case DRW_Text::HMiddle:
        // no equivalent options in text pcb.
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
        break;

    case DRW_Text::HFit:
        // no equivalent options in text pcb.
        textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
        break;
    }

#if 0
    wxString sty = wxString::FromUTF8( aData.style.c_str() );
    sty = sty.ToLower();

    if( aData.textgen == 2 )
    {
        // Text dir = left to right;
    } else if( aData.textgen == 4 )
    {
        // Text dir = top to bottom;
    } else
    {
    }
#endif

    wxString text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) );

    textItem->SetTextPos( refPoint );
    textItem->SetTextAngle( aData.angle * 10 );

    // The 0.8 factor gives a better height/width ratio with our font
    textItem->SetTextWidth( mapDim( aData.height * 0.8 ) );
    textItem->SetTextHeight( mapDim( aData.height ) );
    textItem->SetThickness( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) );
    textItem->SetText( text );

    m_newItemsList.push_back( static_cast< BOARD_ITEM* >( brdItem ) );
}
    {
        // Text dir = left to right;
    } else if (data.textgen==4)
    {
        / Text dir = top to bottom;
    } else
    {
    }
#endif

    wxString    text = toNativeString( wxString::FromUTF8( data.text.c_str() ) );

    pcb_text->SetTextPosition( refPoint );
    pcb_text->SetOrientation( data.angle * 10 );
    // The 0.8 factor gives a better height/width ratio with our font
    pcb_text->SetWidth( mapDim( data.height * 0.8 ) );
    pcb_text->SetHeight( mapDim( data.height ) );
    pcb_text->SetThickness( mapDim( data.thickness == 0 ? m_defaultThickness
                                    : data.thickness ) );
    pcb_text->SetText( text );

    appendToBoard( pcb_text );
}


/**
 * Import multi line texts (MTEXT).
 */
void DXF2BRD_CONVERTER::addMText( const DRW_MText& data )
{
    wxString    text = toNativeString( wxString::FromUTF8( data.text.c_str() ) );