Esempio n. 1
0
CMapPtr ResourceLoader::getColormap(const char *fname) {
	for (Common::List<CMap *>::const_iterator i = _colormaps.begin(); i != _colormaps.end(); ++i) {
		CMap *c = *i;
		if (strcmp(fname, c->_fname.c_str()) == 0) {
			return c;
		}
	}

	return loadColormap(fname);
}
Esempio n. 2
0
CMapPtr ResourceLoader::getColormap(const Common::String &fname) {
	Common::String filename = fname;
	filename.toLowercase();
	for (Common::List<CMap *>::const_iterator i = _colormaps.begin(); i != _colormaps.end(); ++i) {
		CMap *c = *i;
		if (filename.equals(c->_fname)) {
			return c;
		}
	}

	return loadColormap(fname);
}
void QNCLContourLine::plot(QGRIB2Message *grb2Msg, QString outputPath, QString outputFileName)
{
    /*float xbvalues1[7] = {0.,60.,120.,180.,240.,300.,360.};
    char *xblabels1[7] = {"0","60E","120E","180","120W","60W","0"};
    float xbvalues2[11] = {0.,3.,6.,9.,12.,15.,18.,21.,24.,27.,30.};
    float ylvalues[7] = {-90., -60.,-30.,0.,30.,60.,90.};
    char *xblabels2[11] = {"0","3","6","9","12","15","18","21","24","27","30"};
    char *yllabels[7] = {"90S","60S","30S","EQ","30N","60N","90N"};*/
    /*rules setting*/
    float       xbValues[7] = {0};
    char        *xbLabels[7];
    float       ybValues[7] = {0};
    char        *ybLabels[7];
    int         xLabelCount = 0, yLabelCount = 0;
    double *    dData;
    float  *    fData;
    //float       cmap[255][3] = {0};
    int         ni, nj;
    int         mi, mj;
    int         xStart, xEnd;
    int         yStart, yEnd;
    int         appid, jan, cn, mp, tx, srlist;
    ng_size_t   length[2];
    float       cmap[256][3];
    //float  *    cmap;
    size_t      nSize;
    ng_size_t   icount[2];
    int         k = 0;
    int         ps2;//1, ps2, png1;
    char        setting[256]= {0};

    /*x label setting*/
    memset(xbLabels, 0, sizeof(xbLabels));
    memset(ybLabels, 0, sizeof(ybLabels));
    xLabelCount = width/100 + 1;
    xLabelCount = (xLabelCount < 2)?2:((xLabelCount>7)?7:xLabelCount);
    for (int i = 0; i < xLabelCount; i ++) {
        xbValues[i] = leftLongitude + (rightLongitude - leftLongitude) * i / (xLabelCount - 1);
        xbLabels[i] = (char *)calloc(10, 1);
        if ((xbValues[i] == 0) || (xbValues[i] == 180) || (xbValues[i]) == 360)
            sprintf(xbLabels[i], "%d", ((int)xbValues[i])%360);
        else if ((xbValues[i] > 0) && (xbValues[i] < 180)) {
            if ((xbValues[i]-(int)xbValues[i]) == 0)
                sprintf(xbLabels[i], "%dE", ((int)xbValues[i])%360);
            else
                sprintf(xbLabels[i], "%3.4fE", xbValues[i]);
        }
        else if ((xbValues[i] > 180) && (xbValues[i] < 360)) {
            if ((xbValues[i]-(int)xbValues[i]) == 0)
                sprintf(xbLabels[i], "%dW", (360 - (int)xbValues[i]));
            else
                sprintf(xbLabels[i], "%3.4fW", 360. - xbValues[i]);
        }
    }
    yLabelCount = height/100+1;
    yLabelCount = (yLabelCount < 2)?2:((yLabelCount>7)?7:yLabelCount);
    for (int i = 0; i < yLabelCount; i++) {
        ybValues[i] = topLatitude - (topLatitude - bottomLatitude) * i / (yLabelCount - 1);
        ybLabels[i] = (char *)calloc(10, 1);
        if (ybValues[i] == 0)
            sprintf(ybLabels[i], "%d", ((int)ybValues[i])%360);
        else if ((ybValues[i] > 0) && (ybValues[i] <= 90)) {
            if ((ybValues[i]-(int)ybValues[i]) == 0)
                sprintf(ybLabels[i], "%dN", ((int)ybValues[i])%360);
            else
                sprintf(ybLabels[i], "%3.4fN", ybValues[i]);
        }
        else if ((ybValues[i] < 0) && (ybValues[i] >= -90)) {
            if ((ybValues[i]-(int)ybValues[i]) == 0)
                sprintf(ybLabels[i], "%dW", ((int)ybValues[i])%360);
            else
                sprintf(ybLabels[i], "%3.4fW", ybValues[i]);
        }
    }

    /*get data from grib2 message between in the contour setting*/
    grib_handle * hgrib = grb2Msg->getGRIBHandle();
    QString     workName;
    workName = outputFileName;
    workName = workName.left(workName.indexOf('.'));
    ni = grb2Msg->getGrid()->xp();
    nj = grb2Msg->getGrid()->yp();
    nSize = ni * nj;
    dData = (double *)calloc(ni*nj, sizeof(double));



    grib_get_double_array(hgrib, "values", dData, &nSize);
    mi = ((rightLongitude - leftLongitude) * 1000000 / grb2Msg->getGrid()->dx()) + 1;
    mj = ((topLatitude - bottomLatitude) * 1000000 / grb2Msg->getGrid()->dy()) + 1;
    if (mi > ni)
        mi = ni;
    if (mj > nj)
        mj = nj;
    fData = (float *)calloc(mi * mj, sizeof(float));
    xStart = (int)((leftLongitude * 1000000 - grb2Msg->getGrid()->lon1())/grb2Msg->getGrid()->dx());
    if ((xStart * grb2Msg->getGrid()->dx()) > (leftLongitude * 1000000))
        xStart --;
    if (xStart < 0)
        xStart = 0;
    xEnd = (int)((rightLongitude * 1000000 - grb2Msg->getGrid()->lon1())/grb2Msg->getGrid()->dx()) + 1;
    if ((xEnd * grb2Msg->getGrid()->dx()) < (rightLongitude * 1000000))
        xEnd++;
    if (xEnd > ni)
        xEnd = ni;
    yStart = (int)((grb2Msg->getGrid()->lat1() - topLatitude * 1000000)/grb2Msg->getGrid()->dy());
    if ((grb2Msg->getGrid()->lat1() - yStart * grb2Msg->getGrid()->dy()) < (topLatitude * 1000000))
        yStart--;
    if (yStart < 0)
        yStart = 0;
    yEnd = (int)((grb2Msg->getGrid()->lat1() - bottomLatitude * 1000000)/grb2Msg->getGrid()->dy()) + 1;
    if ((grb2Msg->getGrid()->lat1() - yEnd * grb2Msg->getGrid()->dy()) > (bottomLatitude * 1000000))
        yEnd++;
    if (yEnd > nj)
        yEnd = nj;
    for (int jj = yStart; jj < yEnd; jj++) {
        for (int ii = xStart; ii < xEnd; ii ++) {
            fData[k++] = dData[jj * ni + ii];
        }
    }

    NhlInitialize();
    srlist = NhlRLCreate(NhlSETRL);
    NhlRLClear(srlist);
    NhlRLSetString(srlist, "appDefaultParent", "True");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, outputPath.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, "appUsrDir", setting);
    NhlCreate(&appid, grb2Msg->getName().toStdString().c_str(), NhlappClass, 0, srlist);

    NhlRLClear(srlist);
    QString outputpsFileName = outputPath + "/" + outputFileName;
    memset(setting, 0, sizeof(setting));
    strncpy(setting, outputpsFileName.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNwkFileName, setting);
    /*NhlRLSetString(srlist, NhlNwkFormat, "ps");*/
    NhlRLSetString(srlist, NhlNwkFormat, "png");

    /*NhlRLSetString(srlist, NhlNwkOrientation, "landscape");
    NhlRLSetString(srlist, NhlNwkOrientation, "portrait");
    NhlRLSetInteger(srlist, NhlNwkDeviceLowerX, 0);
    NhlRLSetInteger(srlist, NhlNwkDeviceLowerY, 60);
    NhlRLSetInteger(srlist, NhlNwkDeviceUpperX, width);
    NhlRLSetInteger(srlist, NhlNwkDeviceUpperY, height); */
    float   fcolor[3] = {0};
    float   bcolor[3] = {0};
    int r, g, b;
    foregroundColor.getRgb(&r, &g, &b);
    fcolor[0] = ((float)r)/255.;
    fcolor[1] = ((float)g)/255.;
    fcolor[2] = ((float)b)/255.;
    length[0] = 3;
    NhlRLSetFloatArray(srlist, NhlNwkForegroundColor, fcolor, length[0]);
    backgroundColor.getRgb(&r, &g, &b);
    bcolor[0] = ((float)r)/255.;
    bcolor[1] = ((float)g)/255.;
    bcolor[2] = ((float)b)/255.;
    length[0] = 3;
    NhlRLSetFloatArray(srlist, NhlNwkBackgroundColor, bcolor, length[0]);
    length[0] = loadColormap(colormap, &cmap[0][0], 256);
    length[1] = 3;
    if (length[0] > 0) {
        NhlRLSetMDFloatArray(srlist,NhlNwkColorMap,&cmap[0][0], 2, length);
    } else {
        memset(setting, 0, sizeof(setting));
        strncpy(setting, colormap.toStdString().c_str(), sizeof(setting)-1);
        NhlRLSetString(srlist, NhlNwkColorMap, setting);
    }
    /*NhlCreate(&ps2, workName.toStdString().c_str(), NhlpsWorkstationClass, 0, srlist);*/
    NhlCreate(&ps2, workName.toStdString().c_str(), NhlcairoImageWorkstationClass, 0, srlist);


    icount[1] = xEnd - xStart;
    icount[0] = yEnd - yStart; /**/
    NhlRLClear(srlist);
    NhlRLSetMDFloatArray(srlist, NhlNsfDataArray, fData, 2, icount);
    NhlRLSetFloat(srlist, NhlNsfXCStartV, leftLongitude);
    NhlRLSetFloat(srlist, NhlNsfXCEndV, rightLongitude);
    NhlRLSetFloat(srlist, NhlNsfYCStartV, topLatitude);
    NhlRLSetFloat(srlist, NhlNsfYCEndV, bottomLatitude);
    NhlCreate(&jan, "sf", NhlscalarFieldClass, appid, srlist);
    //NhlRLSetString(srlist, NhlNwkOrientation, "landscape");
    /*NhlRLSetString(srlist, NhlNwkOrientation, "portrait");*/
    NhlRLSetString(srlist, NhlNcnLineDrawOrder, "predraw");

    /** Create a ContourPlot object.*/
    NhlRLClear(srlist);
    NhlRLSetInteger(srlist, NhlNcnScalarFieldData, jan);
    NhlRLSetFloat(srlist, NhlNvpXF, .1);
    NhlRLSetFloat(srlist, NhlNvpYF, .75);
    NhlRLSetFloat(srlist, NhlNvpWidthF, 0.8);
    NhlRLSetFloat(srlist, NhlNvpHeightF, 0.4);
    NhlRLSetString(srlist, NhlNcnInfoLabelOn, "False");
    NhlRLSetString(srlist, NhlNcnHighLabelsOn, "False");
    NhlRLSetString(srlist, NhlNcnLowLabelsOn, "False");
    //NhlRLSetIntegerArray(srlist, NhlNcnLineColors, linecolors, 15);

    NhlRLSetString(srlist, NhlNcnLineDrawOrder, "predraw");
    NhlRLSetString(srlist, NhlNcnFillDrawOrder, "predraw");
    NhlRLSetString(srlist, NhlNcnLabelDrawOrder, "predraw");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, levelSelectionMode.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNcnLevelSelectionMode, setting);
    NhlRLSetInteger(srlist, NhlNcnMaxLevelCount, maxLevelCount);
    if (levelSelectionMode == "ManualLevels") {
        NhlRLSetFloat(srlist, NhlNcnMinLevelValF, minLevel);
        NhlRLSetFloat(srlist, NhlNcnMaxLevelValF, maxLevel);
        NhlRLSetFloat(srlist, NhlNcnLevelSpacingF, levelSpacing);
    } else if (levelSelectionMode == "ExplicitLevels") {
        length[0] = maxLevelCount;
        NhlRLSetFloatArray(srlist, NhlNcnLevelSpacingF, lineLevels, length[0]);
    } else if (levelSelectionMode == "EqualSpacedLevels") {
        NhlRLSetFloat(srlist, NhlNcnLevelSpacingF, levelSpacing);
    }

    if (plotMode == 0) {
        NhlRLSetString(srlist, NhlNcnFillOn, "False");
        NhlRLSetString(srlist, NhlNcnLinesOn, "True");
        NhlRLSetString(srlist, NhlNcnLineLabelsOn, "True");
        NhlRLSetInteger(srlist, NhlNcnLineLabelInterval, 2);
        NhlRLSetString(srlist, NhlNcnLineLabelPlacementMode, "computed");
        if (monoColor == false) {
            NhlRLSetString(srlist, NhlNcnMonoLineColor, "False");
            NhlRLSetIntegerArray(srlist, NhlNcnLineColors, lineColors, maxLevelCount);
        } else {
            NhlRLSetString(srlist, NhlNcnMonoLineColor, "True");
            /*NhlRLSetString(srlist, NhlNcnLineColor, "Black");*/
            NhlRLSetInteger(srlist, NhlNcnLineColor, lineColor);
        }
    } else {
        NhlRLSetString(srlist, NhlNcnFillOn, "True");
        NhlRLSetString(srlist, NhlNcnLinesOn, "False");
        NhlRLSetString(srlist, NhlNcnFillMode, "AreaFill");
        NhlRLSetString(srlist, NhlNcnLineLabelsOn, "False");
        length[0] = maxLevelCount;
        NhlRLSetIntegerArray(srlist, NhlNcnFillColors, lineColors, length[0]);
    }
    NhlRLSetString(srlist, NhlNtmXBMode, "EXPLICIT");
    NhlRLSetFloatArray(srlist, NhlNtmXBValues, xbValues, xLabelCount);
    NhlRLSetStringArray(srlist, NhlNtmXBLabels, &xbLabels[0], xLabelCount);
    NhlRLSetString(srlist, NhlNtmYLMode, "EXPLICIT");
    NhlRLSetFloatArray(srlist, NhlNtmYLValues, ybValues, yLabelCount);
    NhlRLSetStringArray(srlist, NhlNtmYLLabels, &ybLabels[0], yLabelCount);
    NhlRLSetString(srlist, NhlNtmXTLabelsOn, "False");
    NhlRLSetString(srlist, NhlNtmYRLabelsOn, "False");
    NhlRLSetFloat(srlist, NhlNtmXBLabelFontHeightF, .010);
    NhlRLSetFloat(srlist, NhlNtmYLLabelFontHeightF, .010);
    NhlRLSetFloat(srlist, NhlNtmXBMajorOutwardLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmXBMajorLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmXTMajorLengthF, 0.);
    NhlRLSetFloat(srlist, NhlNtmXTMajorOutwardLengthF, 0.);
    NhlRLSetFloat(srlist, NhlNtmYLMajorOutwardLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmYLMajorLengthF, .006);
    NhlRLSetString(srlist, NhlNtmXBMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmXTMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmYLMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmYRMinorOn, "False");
    if (monoThickness == true) {
        NhlRLSetString(srlist, NhlNcnMonoLineThickness, "True");
        NhlRLSetFloat(srlist, NhlNcnLineThicknessF, thickness);
    } else {
        NhlRLSetString(srlist, NhlNcnMonoLineThickness, "False");
        NhlRLSetFloatArray(srlist, NhlNcnLineThicknesses, lineThicknesses, maxLevelCount);
    }
    NhlCreate(&cn, "cn", NhlcontourPlotClass, ps2, srlist);

    /* Create a MapPlot object. */
    NhlRLClear(srlist);
    NhlRLSetFloat(srlist, NhlNvpXF, .1);
    NhlRLSetFloat(srlist, NhlNvpYF, .75);
    NhlRLSetFloat(srlist, NhlNvpWidthF, 0.8);
    NhlRLSetFloat(srlist, NhlNvpHeightF, 0.4);
    /*NhlRLSetString(srlist, NhlNmpFillOn, "True");*/
    NhlRLSetString(srlist, NhlNmpFillOn, "False");
    NhlRLSetString(srlist, NhlNmpLabelsOn, "False");
    /*NhlRLSetString(srlist, NhlNmpGeophysicalLineColor, "Black");
    NhlRLSetString(srlist, NhlNmpDefaultFillColor, "DarkSalmon");
    NhlRLSetString(srlist, NhlNmpLandFillColor, "DarkSalmon");
    NhlRLSetString(srlist, NhlNmpOceanFillColor, "Blue");
    NhlRLSetString(srlist, NhlNmpInlandWaterFillColor, "Blue");*/
    NhlRLSetString(srlist, NhlNmpOutlineOn, "True");
    NhlRLSetString(srlist, NhlNgsLineColor, "Black");
    /*NhlRLSetString(srlist, NhlNmpOutlineOn, "True");
    NhlRLSetString(srlist, NhlNmpOutlineBoundarySets, "Geophysical");*/
    /*NhlRLSetString(srlist, NhlNmpOutlineDrawOrder, "predraw");*/
    NhlRLSetString(srlist, NhlNmpOutlineBoundarySets, "Geophysical");
    NhlRLSetFloat(srlist, NhlNmpGeophysicalLineThicknessF, 1);
    NhlRLSetString(srlist, NhlNmpGeophysicalLineColor, "Black");
    /*NhlRLSetString(srlist, NhlNmpPerimDrawOrder, "predraw");*/
    memset(setting, 0, sizeof(setting));
    strncpy(setting, mapProject.toStdString().c_str(), sizeof(setting)-1);
    if (mapProject == "Polar Stereographic") {
        NhlRLSetString(srlist, NhlNmpProjection, "Stereographic");
        if (polarPosition == "NH") {
            NhlRLSetFloat(srlist, NhlNmpMinLatF, 0);
            NhlRLSetFloat(srlist, NhlNmpMaxLatF, 90);
            NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
            NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
            NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
            NhlRLSetFloat(srlist, NhlNmpCenterLatF, 90);
        } else {
            NhlRLSetFloat(srlist, NhlNmpMinLatF, -90);
            NhlRLSetFloat(srlist, NhlNmpMaxLatF, 0);
            NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
            NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
            NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
            NhlRLSetFloat(srlist, NhlNmpCenterLatF, -90);
        }
    } else {
        NhlRLSetString(srlist, NhlNmpProjection, setting);
        NhlRLSetFloat(srlist, NhlNmpMinLatF, bottomLatitude);
        NhlRLSetFloat(srlist, NhlNmpMaxLatF, topLatitude);
        NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
        NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
        NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
        NhlRLSetFloat(srlist, NhlNmpCenterLatF, (topLatitude + bottomLatitude)/2);
    }

    NhlRLSetString(srlist, NhlNmpGridAndLimbOn, "False");
    NhlRLSetString(srlist, NhlNmpLimitMode, "latlon");
    NhlCreate(&mp, "mp", NhlmapPlotClass, ps2, srlist);

    /* Create a TextItem object. */
    QString title = grb2Msg->getParameter()->nclName();
    QString level;
    level.setNum(grb2Msg->getLevel()->firstLevel());
    switch (grb2Msg->getLevel()->firstLevelType()) {
    case 100:
    case 101:
        level += "Pa";
        break;
    case 0:
    case 106:
        level += "m";
        break;
    }
    title += " " + level + " " + grb2Msg->getTimeLevel()->getName() ;

    NhlRLClear(srlist);
    NhlRLSetFloat(srlist, NhlNtxPosXF, 0.5);
    NhlRLSetFloat(srlist, NhlNtxPosYF, 0.8);
    NhlRLSetString(srlist, NhlNtxJust, "CENTERCENTER");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, title.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNtxString, setting);
    NhlRLSetFloat(srlist, NhlNtxFontHeightF, .030);
    NhlRLSetInteger(srlist, NhlNtxFont, 25);
    NhlCreate(&tx, "tx", NhltextItemClass, ps2, srlist);

    NhlDraw(mp);
    NhlDraw(cn);
    NhlDraw(tx);
    NhlFrame(ps2);

    NhlDestroy(ps2);
    NhlDestroy(appid);
    for (int idx = 0; idx < xLabelCount; idx ++) {
        free(xbLabels[idx]);
    }
    for (int idx = 0; idx < yLabelCount; idx ++) {
        free(ybLabels[idx]);
    }
    free(dData);
    free(fData);
}