/* Copyright 2011 NOAA Geophysical Fluid Dynamics Lab, Princeton, NJ This program is distributed under the terms of the GNU General Public License. See the file COPYING contained in this directory */ #include #include #include #include #include #include "mpp.h" #include "mosaic_util.h" #include "tool_util.h" #include "constant.h" #include "mpp_io.h" #include "create_hgrid.h" #define D2R (M_PI/180.) #define R2D (180./M_PI) void create_grid_from_text_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ); void get_grid_v1(int fid, double *xt, double *yt, double *xc, double *yc); void get_grid_v2(int fid, int nlon, int nlat, double *xt, double *yt, double *xc, double *yc); void get_grid_v3(int fid, int nlon, int nlat, double *xt, double *yt, double *xc, double *yc); void get_grid_v4(int fid, int nlon, int nlat, double *xt, double *yt, double *xc, double *yc); /*********************************************************************** void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) the grid location is defined through ascii file. calculate cell length, cell area and rotation angle ************************************************************************/ void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx, int use_great_circle_algorithm ) { double *xt, *yt, *xc, *yc; double p1[4], p2[4], cart_x[4], cart_y[4], cart_z[4]; int nx, ny, nxp, nyp, ni, nj, i, j, n; int is_uniform; char mesg[256], txt[128]; /************************************************************ identify the grid_file is ascii file or netcdf file, if the file name contains ".nc", it is a netcdf file, otherwise it is ascii file. *********************************************************/ nx = *nlon; ny = *nlat; nxp = nx + 1; nyp = ny + 1; if(strstr(file, ".nc") ) { int fid, vid; ni = *nlon/2; nj = *nlat/2; xc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); yc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); xt = (double *)malloc( ni * nj *sizeof(double)); yt = (double *)malloc( ni * nj *sizeof(double)); fid = mpp_open(file, MPP_READ); if(mpp_dim_exist(fid, "grid_xt") ) get_grid_v1(fid, xt, yt, xc, yc); else if(mpp_dim_exist(fid, "rlon") || mpp_dim_exist(fid, "i") ) get_grid_v2(fid, ni, nj, xt, yt, xc, yc); else if(mpp_dim_exist(fid, "lon") ) get_grid_v3(fid, ni, nj, xt, yt, xc, yc); else if(mpp_dim_exist(fid, "x") ) get_grid_v4(fid, ni, nj, xt, yt, xc, yc); mpp_close(fid); for(j=0; j 0) lon[i] -= 360; xt[i] = lon[i]; yt[i] = lat[i]; lon[i] *= D2R; lat[i] *= D2R; } n = log(cos(phi1)/cos(phi2))/log(tan(0.25*M_PI+0.5*phi2)/tan(0.25*M_PI+0.5*phi1)); F = cos(phi1)*pow(tan(0.25*M_PI+0.5*phi1),n); rho0 = F*pow(1/tan(0.25*M_PI+0.5*central_lat),n); for(i=0; i 1.e-6 ) { printf(" i = %d, j = %d, x1 = %g, x2 = %g \n", i, j, x1[j*nlon+i], x1[i]); mpp_error(" x is not uniform \n"); } } } printf(" x is uniform \n \n \n"); for(j=0; i 1.e-6 ) { printf(" i = %d, j = %d, x1 = %g, x2 = %g \n", i, j, y1[j*nlon+i], y1[j*nlon]); mpp_error(" y is not uniform \n"); } } } printf(" y is uniform \n \n \n"); /* get the x and y value at corner */ x2 = (double *)malloc((nlon+1)*(nlat+1)*sizeof(double)); y2 = (double *)malloc((nlon+1)*(nlat+1)*sizeof(double)); for(i=1; i