예제 #1
0
BOOL PathHelper::GetDirFile(
    const std::wstring &strDir,
    CStdStringVector &vecFile,
    const std::wstring &strFileMask
)
{
    BOOL bResult = FALSE;
    vecFile.clear();
    /*
    下面的代码为访问目录,和操作系统相关,目前为windows操作系统的相关代码
    */

    std::wstring strFileName;
    //存放查找的当前目录
    std::wstring strCurDirName;
    //存放查找中碰到的中间目录
    std::wstring strSubDirName;
    //用来模拟递归用的存储中间遇到的子目录
    std::queue<std::wstring> recursiveDirQueue;

    strCurDirName = strDir;
    strFileName = strDir;
    strFileName += L"\\";
    strFileName += strFileMask;

    HANDLE hFindFile;
    WIN32_FIND_DATAW fileinfo;
    hFindFile = FindFirstFileW(strFileName.c_str(), &fileinfo);

    while (INVALID_HANDLE_VALUE != hFindFile)
    {
        if ((fileinfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FALSE)
        {
            std::wstring strTmpFile = strCurDirName;
            strTmpFile += L"\\";
            strTmpFile += fileinfo.cFileName;
            //文件不为子目录
            vecFile.push_back(strTmpFile);
            /**
            push(fileinfo.cFileName);
            printf ("The  file found is %s\n", fileinfo.cFileName);
            */
        }
        else
        {
#if 0
            //将查找中间碰到的子目录全部存入dirQueue队列中,以后还要对该目录进行查找
            bResult = DirFilter(fileinfo.cFileName);
            if (true != bResult) //不为要过滤掉的.和..目录则压入目录队列中
            {
                strSubDirName += strCurDirName;
                strSubDirName += "\\";
                strSubDirName += fileinfo.cFileName;
                ///sprintf(strSubDirName,"%s\\%s" , strCurDirName, fileinfo.cFileName);
                recursiveDirQueue.push(strSubDirName);
                vecSubDir.push_back(strSubDirName);
                printf ("The  subdir found is %s\n", strSubDirName.c_str());
            }
#endif
        }

        bResult = FindNextFileW(hFindFile, &fileinfo);

        if (FALSE == bResult)
        {
            /*
            if (GetLastError() == ERROR_NO_MORE_FILES)
            {
            printf("No more %s files.", m_StrFileName);
            }
            else
            {
            printf("Couldn't find next file.");
            }
            */

            FindClose(hFindFile);
            break;
        }
    }

    bResult = (vecFile.size() > 0) ;
    return bResult;
}
ERMsg C20thReanalysisProject::CreateDailyGrid( CString filePathIn, CString varName, const CString& filePathOut, CSCCallBack& callback)
{
	ERMsg msg;


	int sizeX=0;
	int sizeY=0;
	int nbBand=0;
	CGeoRectWP rect;
	float noData=0;
	double cellSizeX=0;
	double cellSizeY=0;
	//CString filePathIn = m_path + "gauss\\hgt.gauss.nc";
	CString filePath = filePathIn+".1871.nc";
	msg = GetGridInfo(filePath, sizeX, sizeY, nbBand, rect, noData, cellSizeX, cellSizeY);
	if( !msg)
		return msg;

	int nbDayTotal=0;
	for(int yr=0; yr<138&&msg; yr++)
		nbDayTotal+=CFL::GetNbDay(1871+yr);

//	if( !msg)
	//	return msg;

//	callback.SetCurrentDescription("Init "+GetFileTitle(filePathOut));
	//callback.SetCurrentStepRange(0, sizeX*sizeY*138, 1);
	

	//init with no data
	//for(int z=0; z<138&&msg; z++)
	//{
	//	for(int x=0; x<sizeX&&msg; x++)
	//	{
	//		for(int y=0; y<sizeY&&msg; y++)
	//		{
	//			msg += grid.WriteCell(noData,x,y,z);
	//			msg += callback.StepIt();
	//		}
	//	}
	//}

	//take all cell
	//CRect rect(0,0,180,172);


	callback.SetCurrentDescription("Create "+GetFileTitle(filePathOut));
	callback.SetCurrentStepRange(0, nbDayTotal, 1);
	callback.SetStartNewStep();

	int jdy=0;
	for(int yr=0; yr<1/*38*/&&msg; yr++)
	{
		int year = 1871 + yr;
		int nbDays = CFL::GetNbDay(year);

		//CMapBilFile grid;
		//grid.SetCellType(CMapBilFile::FLOAT);
		//grid.SetProjection( CProjection(CProjection::GEO) );//GetDataGrid().GetPrj() );
		//grid.SetNbCols(sizeX);
		//grid.SetNbRows(sizeY);
		//grid.SetNbBand(nbDays);//40 year * ~365days
		//grid.SetBoundingBox(rect);
		//grid.SetNoData(noData);
		//grid.SetCellSizeX( cellSizeX );
		//grid.SetCellSizeY( cellSizeY );

		CString filePathOut = filePathIn+"."+ToString(year)+".tif";
		CStdStringVector createOptions;
		createOptions.push_back("COMPRESS=LZW");

		
		CGeoRect2 boundingBox;
		boundingBox.m_xMin = rect.m_xMin; 
		boundingBox.m_yMin =  rect.m_yMin;
		boundingBox.m_xMax = rect.m_xMax+cellSizeX; //add one cell
		boundingBox.m_yMax =  rect.m_yMax;
		boundingBox.SetProjectionRef("+proj=latlon +datum=NAD83");
		

		CGDALDatasetEx dataset;
		
		msg += dataset.CreateImage((LPCTSTR)filePathOut, sizeX+1, sizeY, boundingBox, "GTIFF", GDT_Float32, nbDays, -9999, createOptions, true);

		
		
		//read data
		CData3Array var(boost::extents[nbDays][sizeY][sizeX+1]);
		CString filePath = filePathIn+"."+ToString(year)+".nc";
		msg += ReadData( filePath, varName, var);

		//copy data to weather station
		for(int jd=0; jd<nbDays; jd++)
		{
			//boost::multi_array<float, 2>  varXY(boost::extents[sizeX+1][sizeY]);
			for(int y=0; y<sizeY; y++)
			{
				for(int xx=0; xx<(sizeX+1)/2; xx++)
				{
					//work only if the number of cell is even
					int x=((sizeX+1)/2+xx)%sizeX;
					
					//varXY[xx][y] = var[jd][y][x];
					float a = var[jd][y][xx];
					var[jd][y][xx] = var[jd][y][x];
					var[jd][y][x]=a;
				}
				//the last cell is a copy of the first cell
				var[jd][y][sizeX]=var[jd][y][0];
				//varXY[sizeX][y]=varXY[0][y];
			}

			GDALRasterBand* pBand = dataset->GetRasterBand(jd+1);
			pBand->RasterIO(GF_Write,0,0,sizeX+1, sizeY, &(var[jd][0][0]),sizeX+1, sizeY, GDT_Float32,0,0);//, 1, NULL,0,0,0);
			msg += callback.StepIt();
		}
		
		
		
		//dataset->GDALDataset::RasterIO(GF_Write,0,0,sizeX, sizeY, &(var[0][0][0]),sizeX, sizeY, GDT_Float32, nbDays, NULL,0,0,0);

			

		////copy data to weather station
		//for(int y=0; y<sizeY&&msg; y++)
		//{
		//	for(int xx=0; xx<sizeX&&msg; xx++)
		//	{
		//		int x=((sizeX+1)/2+xx)%sizeX;
		//		for(int jd=0; jd<nbDays&&msg; jd++)
		//		{
		//			//float value = (float)MMG[x][sizeY-y-1][z];
		//			//int bandNo = bandBase+z;
		//			msg += grid.WriteCell(var[jd][x][y],xx,y,jdy);
		//			jdy++;
		//		}
		//		
		//		msg += callback.StepIt();
		//	}
		//}
		//	
		dataset.Close();
	}




	return msg;
}