Skip to content

Commit

Permalink
Flip x or y axis
Browse files Browse the repository at this point in the history
Add the capability to flip the x and y axis on the fly. this is automnatically done if x or y are not monotonically increasing
  • Loading branch information
CyprienBosserelle committed Apr 11, 2022
1 parent 7bee39c commit 1cdbf53
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class inputmap {
double ymax = 0.0;
double dx = 0.0;
double grdalpha=0.0;
bool flipxx = false;
bool flipyy = false;
std::string inputfile;
std::string extension;
std::string varname;
Expand Down
14 changes: 8 additions & 6 deletions src/ReadForcing.cu
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,11 @@ std::vector<SLTS> readNestfile(std::string ncfile,std::string varname, int hor ,
double * ttt, *zsa;
bool checkhh = false;
int iswet;
bool flipx = false;
bool flipy = false;

// Read NC info
readgridncsize(ncfile,varname, nnx, nny, nt, dx, xxo, yyo, to, xmax, ymax, tmax);
readgridncsize(ncfile,varname, nnx, nny, nt, dx, xxo, yyo, to, xmax, ymax, tmax, flipx, flipy);

if (hor == 0)
{
Expand Down Expand Up @@ -1155,7 +1157,7 @@ void readforcingdata(int step,T forcing)
}
if (fileext.compare("nc") == 0)
{
readvardata(forcing.inputfile, forcing.varname,step, forcing.val);
readvardata(forcing.inputfile, forcing.varname,step, forcing.val, forcing.flipxx, forcing.flipyy);
}
if (fileext.compare("bot") == 0 || fileext.compare("dep") == 0)
{
Expand All @@ -1181,8 +1183,8 @@ template void readforcingdata<StaticForcingP<int>>(int step, StaticForcingP<int>
void readforcingdata(double totaltime, DynForcingP<float>& forcing)
{
int step = utils::min(utils::max((int)floor((totaltime - forcing.to) / forcing.dt), 0), forcing.nt - 2);
readvardata(forcing.inputfile, forcing.varname, step, forcing.before);
readvardata(forcing.inputfile, forcing.varname, step+1, forcing.after);
readvardata(forcing.inputfile, forcing.varname, step, forcing.before, forcing.flipxx, forcing.flipyy);
readvardata(forcing.inputfile, forcing.varname, step+1, forcing.after, forcing.flipxx, forcing.flipyy);
clampedges(forcing.nx, forcing.ny, forcing.clampedge, forcing.before);
clampedges(forcing.nx, forcing.ny, forcing.clampedge, forcing.after);

Expand All @@ -1205,7 +1207,7 @@ DynForcingP<float> readforcinghead(DynForcingP<float> Fmap)
if (fileext.compare("nc") == 0)
{
log("Reading Forcing file as netcdf file");
readgridncsize(Fmap.inputfile,Fmap.varname, Fmap.nx, Fmap.ny, Fmap.nt, Fmap.dx, Fmap.xo, Fmap.yo, Fmap.to, Fmap.xmax, Fmap.ymax, Fmap.tmax);
readgridncsize(Fmap.inputfile,Fmap.varname, Fmap.nx, Fmap.ny, Fmap.nt, Fmap.dx, Fmap.xo, Fmap.yo, Fmap.to, Fmap.xmax, Fmap.ymax, Fmap.tmax, Fmap.flipxx, Fmap.flipyy);

if (Fmap.nt > 1)
{
Expand Down Expand Up @@ -1262,7 +1264,7 @@ template<class T> T readforcinghead(T ForcingParam)
int dummy;
double dummyb, dummyc;
//log("netcdf file");
readgridncsize(ForcingParam.inputfile, ForcingParam.varname, ForcingParam.nx, ForcingParam.ny, dummy, ForcingParam.dx, ForcingParam.xo, ForcingParam.yo, dummyb, ForcingParam.xmax, ForcingParam.ymax, dummyc);
readgridncsize(ForcingParam.inputfile, ForcingParam.varname, ForcingParam.nx, ForcingParam.ny, dummy, ForcingParam.dx, ForcingParam.xo, ForcingParam.yo, dummyb, ForcingParam.xmax, ForcingParam.ymax, dummyc, ForcingParam.flipxx, ForcingParam.flipyy);
//log("For nc of bathy file please specify grdalpha in the BG_param.txt (default 0)");


Expand Down
68 changes: 63 additions & 5 deletions src/Read_netcdf.cu
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ inline int nc_get_var1_T(int ncid, int varid, const size_t* startp, double * zsa



void readgridncsize(const std::string ncfilestr, const std::string varstr, int &nx, int &ny, int &nt, double &dx, double &xo, double &yo, double &to, double &xmax, double &ymax, double &tmax)
void readgridncsize(const std::string ncfilestr, const std::string varstr, int &nx, int &ny, int &nt, double &dx, double &xo, double &yo, double &to, double &xmax, double &ymax, double &tmax, bool & flipx, bool & flipy)
{
//read the dimentions of grid, levels and time
int status;
Expand Down Expand Up @@ -283,6 +283,12 @@ void readgridncsize(const std::string ncfilestr, const std::string varstr, int &
ymax = utils::max(ycoord[(ny - 1)*nx], ycoord[0]);


if (xcoord[0] > xcoord[nx - 1])
flipx = true;

if (ycoord[0] > ycoord[ny - 1])
flipy = true;


status = nc_close(ncid);

Expand Down Expand Up @@ -543,7 +549,7 @@ template int readncslev1<double>(std::string filename, std::string varstr, size_


template <class T>
int readvardata(std::string filename, std::string Varname, int step, T * &vardata)
int readvardata(std::string filename, std::string Varname, int step, T * &vardata, bool flipx, bool flipy)
{
// function to standardise the way to read netCDF data off a file
// The role of this function is to offload and simplify the rest of the code
Expand All @@ -553,6 +559,8 @@ int readvardata(std::string filename, std::string Varname, int step, T * &vardat
size_t * start, * count, *ddim;
double scalefac, offset, missing;





ndims = readvarinfo(filename, Varname, ddim);
Expand Down Expand Up @@ -588,6 +596,8 @@ int readvardata(std::string filename, std::string Varname, int step, T * &vardat
count[0] = ny;
count[1] = nx;




}
else //(ndim>2)
Expand All @@ -606,6 +616,10 @@ int readvardata(std::string filename, std::string Varname, int step, T * &vardat


}

//double* xo,xnd;


status = nc_get_vara_T(ncid, varid, start, count, vardata);
if (status != NC_NOERR) handle_ncerror(status);

Expand Down Expand Up @@ -642,6 +656,50 @@ int readvardata(std::string filename, std::string Varname, int step, T * &vardat
//printf("maxval = %f\n", float(maxval));
}

if (flipx)
{
T* xdata;
xdata=(T*)malloc(nx * sizeof(T));
for (int j = 0; j < ny; j++)
{
for (int i = 0; i < nx; i++)
{
xdata[i] = vardata[i + j * nx];
//unpacked_value = packed_value * scale_factor + add_offset

}
for (int i = 0; i < nx; i++)
{
vardata[i + j * nx] = xdata[nx - 1 - i];
//unpacked_value = packed_value * scale_factor + add_offset

}
}
free(xdata);
}

if (flipy)
{
T* ydata;
ydata = (T*)malloc(ny * sizeof(T));
for (int i = 0; i < nx; i++)
{
for (int j = 0; j < ny; j++)
{
ydata[j] = vardata[i + j * nx];
//unpacked_value = packed_value * scale_factor + add_offset

}
for (int j = 0; j < ny; j++)
{
vardata[i + j * nx] = ydata[ny - 1 - j];
//unpacked_value = packed_value * scale_factor + add_offset

}
}
free(ydata);
}



// apply scale and offset
Expand Down Expand Up @@ -670,9 +728,9 @@ int readvardata(std::string filename, std::string Varname, int step, T * &vardat
return status;

}
template int readvardata<int>(std::string filename, std::string Varname, int step, int*& vardata);
template int readvardata<float>(std::string filename, std::string Varname, int step, float * &vardata);
template int readvardata<double>(std::string filename, std::string Varname, int step, double * &vardata);
template int readvardata<int>(std::string filename, std::string Varname, int step, int*& vardata, bool flipx, bool flipy);
template int readvardata<float>(std::string filename, std::string Varname, int step, float * &vardata, bool flipx, bool flipy);
template int readvardata<double>(std::string filename, std::string Varname, int step, double * &vardata, bool flipx, bool flipy);



Expand Down
4 changes: 2 additions & 2 deletions src/Read_netcdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ inline int nc_get_var1_T(int ncid, int varid, const size_t* startp, double * zsa
//int readnczb(int nx, int ny, const std::string ncfile, double*& zb);
std::string checkncvarname(int ncid, std::string stringA, std::string stringB, std::string stringC, std::string stringD, std::string stringE);

void readgridncsize(const std::string ncfilestr, const std::string varstr, int &nx, int &ny, int &nt, double &dx, double &xo, double &yo, double &to, double &xmax, double &ymax, double &tmax);
void readgridncsize(const std::string ncfilestr, const std::string varstr, int& nx, int& ny, int& nt, double& dx, double& xo, double& yo, double& to, double& xmax, double& ymax, double& tmax, bool& flipx, bool& flipy);
int readvarinfo(std::string filename, std::string Varname, size_t *&ddimU);
int readnctime(std::string filename, double * &time);
template <class T> int readncslev1(std::string filename, std::string varstr, size_t indx, size_t indy, size_t indt, bool checkhh, double eps, T * &zsa);
template <class T> int readvardata(std::string filename, std::string Varname, int step, T*& vardata);
template <class T> int readvardata(std::string filename, std::string Varname, int step, T*& vardata, bool flipx, bool flipy);
//template <class T> int readhotstartfile(Param XParam, int * leftblk, int *rightblk, int * topblk, int* botblk, double * blockxo, double * blockyo, T * &zs, T * &zb, T * &hh, T *&uu, T * &vv);

void readWNDstep(forcingmap WNDUmap, forcingmap WNDVmap, int steptoread, float *&Uo, float *&Vo);
Expand Down
2 changes: 1 addition & 1 deletion src/Updateforcing.cu
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template <class T> void Forcingthisstep(Param XParam, Loop<T> XLoop, DynForcingP

// Read the actual file data

readvardata(XDynForcing.inputfile, XDynForcing.varname, readfirststep + 1, XDynForcing.after);
readvardata(XDynForcing.inputfile, XDynForcing.varname, readfirststep + 1, XDynForcing.after, XDynForcing.flipxx, XDynForcing.flipyy);
if (XParam.GPUDEVICE >= 0)
{
CUDA_CHECK(cudaMemcpy(XDynForcing.after_g, XDynForcing.after, XDynForcing.nx * XDynForcing.ny * sizeof(float), cudaMemcpyHostToDevice));
Expand Down

0 comments on commit 1cdbf53

Please sign in to comment.