00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "omgclimateimporter.h"
00022 #include "omgclimatefilereader.h"
00023 #include "omgmeridianswitcher.h"
00024
00025
00026 #include <QString>
00027 #include <QStringList>
00028 #include <QFile>
00029 #include <QFileInfo>
00030 #include <QTextStream>
00031
00032 OmgClimateImporter::OmgClimateImporter() : QObject()
00033 {}
00034
00035 OmgClimateImporter::~OmgClimateImporter()
00036 {}
00037
00038 QStringList OmgClimateImporter::import(QString theInputFile, QString theOutputDir, QString theFileType)
00039 {
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 float myInputCellSizeX = 0;
00050 int myXMultiplier = 0;
00051 int myYMultiplier = 0;
00052 int myEndRowMultiplier = 0;
00053 bool myDoMeridianShiftFlag = false;
00054
00055 OmgClimateFileReader myReader;
00056 if (theFileType=="CRU")
00057 {
00058 myReader.initialise(theInputFile,OmgClimateFileReader::CRU_CL1_MONTHLY);
00059
00060 myInputCellSizeX = 0.5;
00061 myXMultiplier = 2;
00062 myYMultiplier = 2;
00063 myEndRowMultiplier = 2;
00064 myDoMeridianShiftFlag = true;
00065 }
00066 else if (theFileType=="Hadley")
00067 {
00068 myReader.initialise(theInputFile,OmgClimateFileReader::HADLEY_SRES_MEAN);
00069 myInputCellSizeX = 3.75;
00070 myXMultiplier = 15;
00071 myYMultiplier = 10;
00072 myEndRowMultiplier = 5;
00073 myDoMeridianShiftFlag = true;
00074 }
00075 else
00076 {
00077 emit error("Invalid file type");
00078 return QStringList ();
00079 }
00080 connect(&myReader, SIGNAL(error(QString)), this, SLOT(propogateError(QString)));
00081 connect(&myReader, SIGNAL(message(QString)), this, SLOT(propogateMessage(QString)));
00082
00083
00084 QFileInfo myFileInfo(theInputFile);
00085 QString myBaseName = myFileInfo.baseName();
00086
00087 int myXDim = myReader.xDim();
00088 int myYDim = myReader.yDim();
00089 int myBlockCount = myReader.blockCount();
00090
00091 QStringList myOutputFileList;
00092
00093
00094 QString myHeader;
00095 myHeader += "ncols " + QString::number(myXDim*myXMultiplier ) + "\r\n";
00096 myHeader += "nrows " + QString::number(((myYDim-2)*(myYMultiplier))+(myEndRowMultiplier*2) ) + "\r\n";
00097 myHeader += "xllcorner -180\r\n";
00098 myHeader += "yllcorner -90\r\n";
00099 myHeader += "cellsize " + QString::number(1.0/(static_cast<float>(myXDim*myXMultiplier)/360) ) + "\r\n";
00100
00101 myHeader += "NODATA_value -9999\r\n";
00102
00103
00104 OmgMeridianSwitcher mySwitcher;
00105 connect(&mySwitcher, SIGNAL(error(QString)), this, SLOT(propogateError(QString)));
00106 connect(&mySwitcher, SIGNAL(message(QString)), this, SLOT(propogateMessage(QString)));
00107
00108
00109
00110
00111
00112
00113 for (int i=0; i<myBlockCount;i++)
00114 {
00115 if (!myReader.setActiveBlock(i))
00116 {
00117 break;
00118 }
00119
00120 emit message("Processing block " + QString::number(i+1));
00121 QString myOutputFileName;
00122
00123 int myMonthNo = i+1;
00124 if (myMonthNo<10)
00125 {
00126 myOutputFileName = theOutputDir + "/" + myBaseName + "_0" + QString::number(myMonthNo).toLocal8Bit() + ".asc";
00127 }
00128 else
00129 {
00130 myOutputFileName = theOutputDir + "/" + myBaseName + "_" + QString::number(myMonthNo).toLocal8Bit() + ".asc";
00131 }
00132 QFile myOutputFile (myOutputFileName);
00133
00134 if ( myOutputFile.open( QIODevice::WriteOnly ) )
00135 {
00136 myOutputFileList.append(myOutputFileName);
00137 QTextStream myOutputTextStream( &myOutputFile );
00138 myOutputTextStream << myHeader.toLocal8Bit();
00139 int myCurrentCount=0;
00140 QString myCurrentLine;
00141 while (!myReader.isAtMatrixEnd())
00142 {
00143 float myFloat = myReader.getElement();
00144
00145 for (int x=1 ; x <= myXMultiplier; x++)
00146 {
00147 myCurrentLine += QString::number(myFloat);
00148 if (x < myXMultiplier)
00149 {
00150 myCurrentLine += " ";
00151 }
00152 }
00153
00154
00155 if (myReader.currentCol()==myXDim)
00156 {
00157 myCurrentLine += "\r\n";
00158 int myCurrentRowNo = myReader.currentRow();
00159 if (myCurrentRowNo==1 || myCurrentRowNo == myYDim)
00160 {
00161
00162 for (int y=0; y < myEndRowMultiplier; y++)
00163 {
00164 myOutputTextStream << myCurrentLine.toLocal8Bit();
00165 }
00166 }
00167 else
00168 {
00169 for (int y=0; y < myYMultiplier; y++)
00170 {
00171 myOutputTextStream << myCurrentLine.toLocal8Bit();
00172 }
00173 }
00174 myCurrentLine="";
00175 }
00176 else
00177 {
00178 myCurrentLine += " ";
00179 }
00180 myCurrentCount++;
00181 }
00182 myOutputFile.close();
00183 }
00184 else
00185 {
00186 emit error ("Could not open output file " + myOutputFileName.toLocal8Bit() + " for writing.");
00187 break;
00188 }
00189
00190 if (myDoMeridianShiftFlag)
00191 {
00192 QString myMeridianShiftOutputFileName;
00193 if (myMonthNo<10)
00194 {
00195 myMeridianShiftOutputFileName = theOutputDir + "/" + myBaseName + "_shift_0" + QString::number(myMonthNo).toLocal8Bit() + ".asc";
00196 }
00197 else
00198 {
00199 myMeridianShiftOutputFileName = theOutputDir + "/" + myBaseName + "_shift_" + QString::number(myMonthNo).toLocal8Bit() + ".asc";
00200 }
00201 emit message("Shifting meridian");
00202 mySwitcher.doSwitch(myOutputFileName, myMeridianShiftOutputFileName);
00203 }
00204 emit updateProgress (i,myBlockCount);
00205
00206
00207 }
00208 emit message("Conversion Complete, " + QString::number(myBlockCount) + " blocks processed succssfully.");
00209 return myOutputFileList;
00210 }
00211
00212
00213 void OmgClimateImporter::propogateError (QString theError)
00214 {
00215
00216 emit error(theError);
00217 }
00218
00219 void OmgClimateImporter::propogateMessage (QString theMessage)
00220 {
00221
00222 emit message(theMessage);
00223 }