Commit 421062ef authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

genbil: generate weight file also for num_links=0

parent 318a1ad0
......@@ -3,6 +3,10 @@
* using CDI library version 1.7.1
* Version 1.7.1 released
2016-01-06 Uwe Schulzweida
* genbil: generate weight file also for num_links=0
2016-01-05 Uwe Schulzweida
* Select: added search key steptype
......
......@@ -23,10 +23,7 @@ void remapgrid_alloc(int map_type, remapgrid_t *grid);
static
void nce(int istat)
{
/*
This routine provides a simple interface to netCDF error message routine.
*/
// This routine provides a simple interface to netCDF error message routine.
if ( istat != NC_NOERR ) cdoAbort(nc_strerror(istat));
}
#endif
......@@ -35,18 +32,15 @@ void nce(int istat)
void write_remap_scrip(const char *interp_file, int map_type, int submap_type, int num_neighbors,
int remap_order, remapgrid_t src_grid, remapgrid_t tgt_grid, remapvars_t rv)
{
/*
Writes remap data to a netCDF file using SCRIP conventions
*/
// Writes remap data to a netCDF file using SCRIP conventions
/*
Input variables:
interp_file ! filename for remap data
*/
#if defined(HAVE_LIBNETCDF)
/* Local variables */
// Local variables
int nc_file_id; /* id for netCDF file */
int nc_srcgrdsize_id; /* id for source grid size */
......@@ -87,7 +81,6 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
char tgt_grid_name[64] = "dest grid";
const char *src_grid_units = "radians";
const char *tgt_grid_units = "radians";
long i;
int lgridarea = FALSE;
int writemode = NC_CLOBBER;
......@@ -146,9 +139,10 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
break;
}
/*
if ( rv.num_links == 0 )
cdoAbort("Number of remap links is 0, no remap weights found!");
*/
{
size_t nele1 = 4*8 + 4;
size_t nele2 = 4*8 + 4;
......@@ -161,7 +155,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
if ( cdoVerbose )
cdoPrint("Filesize for remap weights: ~%lu", (unsigned long) filesize);
if ( filesize > 0x7FFFFC00 ) /* 2**31 - 1024 (<2GB) */
if ( filesize > 0x7FFFFC00 ) // 2**31 - 1024 (<2GB)
{
#if defined(NC_64BIT_OFFSET)
writemode = NC_CLOBBER | NC_64BIT_OFFSET;
......@@ -171,34 +165,34 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
}
}
/* Create netCDF file for mapping and define some global attributes */
// Create netCDF file for mapping and define some global attributes
nce(nc_create(interp_file, writemode, &nc_file_id));
/* Map name */
// Map name
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "title", strlen(map_name), map_name));
/* Normalization option */
// Normalization option
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "normalization", strlen(normalize_opt), normalize_opt));
/* Map method */
// Map method
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "map_method", strlen(map_method), map_method));
/* Remap order */
// Remap order
if ( map_type == MAP_TYPE_CONSERV && submap_type == SUBMAP_TYPE_NONE )
nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "remap_order", NC_INT, 1L, &remap_order));
/* File convention */
// File convention
strcpy(tmp_string, "SCRIP");
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "conventions", strlen(tmp_string), tmp_string));
/* Source and destination grid names */
// Source and destination grid names
gridName(gridInqType(src_grid.gridID), src_grid_name);
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "source_grid", strlen(src_grid_name), src_grid_name));
gridName(gridInqType(tgt_grid.gridID), tgt_grid_name);
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "dest_grid", strlen(tgt_grid_name), tgt_grid_name));
/* History */
// History
time_t date_and_time_in_sec = time(NULL);
if ( date_and_time_in_sec != -1 )
{
......@@ -212,41 +206,41 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
if ( CDO_Version_Info )
nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "CDO", (int)strlen(cdoComment())+1, cdoComment()));
/* Prepare netCDF dimension info */
// Prepare netCDF dimension info
/* Define grid size dimensions */
// Define grid size dimensions
nce(nc_def_dim(nc_file_id, "src_grid_size", src_grid.size, &nc_srcgrdsize_id));
nce(nc_def_dim(nc_file_id, "dst_grid_size", tgt_grid.size, &nc_dstgrdsize_id));
/* Define grid corner dimension */
// Define grid corner dimension
if ( src_grid.lneed_cell_corners )
nce(nc_def_dim(nc_file_id, "src_grid_corners", src_grid.num_cell_corners, &nc_srcgrdcorn_id));
if ( tgt_grid.lneed_cell_corners )
nce(nc_def_dim(nc_file_id, "dst_grid_corners", tgt_grid.num_cell_corners, &nc_dstgrdcorn_id));
/* Define grid rank dimension */
// Define grid rank dimension
nce(nc_def_dim(nc_file_id, "src_grid_rank", src_grid.rank, &nc_srcgrdrank_id));
nce(nc_def_dim(nc_file_id, "dst_grid_rank", tgt_grid.rank, &nc_dstgrdrank_id));
/* Define map size dimensions */
// Define map size dimensions
nce(nc_def_dim(nc_file_id, "num_links", rv.num_links, &nc_numlinks_id));
nce(nc_def_dim(nc_file_id, "num_wgts", rv.num_wts, &nc_numwgts_id));
/* Define grid dimensions */
// Define grid dimensions
nce(nc_def_var(nc_file_id, "src_grid_dims", NC_INT, 1, &nc_srcgrdrank_id, &nc_srcgrddims_id));
nce(nc_def_var(nc_file_id, "dst_grid_dims", NC_INT, 1, &nc_dstgrdrank_id, &nc_dstgrddims_id));
/* Define all arrays for netCDF descriptors */
// Define all arrays for netCDF descriptors
/* Define grid center latitude array */
// Define grid center latitude array
nce(nc_def_var(nc_file_id, "src_grid_center_lat", NC_DOUBLE, 1, &nc_srcgrdsize_id, &nc_srcgrdcntrlat_id));
nce(nc_def_var(nc_file_id, "dst_grid_center_lat", NC_DOUBLE, 1, &nc_dstgrdsize_id, &nc_dstgrdcntrlat_id));
/* Define grid center longitude array */
// Define grid center longitude array
nce(nc_def_var(nc_file_id, "src_grid_center_lon", NC_DOUBLE, 1, &nc_srcgrdsize_id, &nc_srcgrdcntrlon_id));
nce(nc_def_var(nc_file_id, "dst_grid_center_lon", NC_DOUBLE, 1, &nc_dstgrdsize_id, &nc_dstgrdcntrlon_id));
/* Define grid corner lat/lon arrays */
// Define grid corner lat/lon arrays
nc_dims2_id[0] = nc_srcgrdsize_id;
nc_dims2_id[1] = nc_srcgrdcorn_id;
......@@ -266,7 +260,8 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_def_var(nc_file_id, "dst_grid_corner_lon", NC_DOUBLE, 2, nc_dims2_id, &nc_dstgrdcrnrlon_id));
}
/* Define units for all coordinate arrays */
// Define units for all coordinate arrays
nce(nc_put_att_text(nc_file_id, nc_srcgrdcntrlat_id, "units", strlen(src_grid_units), src_grid_units));
nce(nc_put_att_text(nc_file_id, nc_dstgrdcntrlat_id, "units", strlen(tgt_grid_units), tgt_grid_units));
nce(nc_put_att_text(nc_file_id, nc_srcgrdcntrlon_id, "units", strlen(src_grid_units), src_grid_units));
......@@ -282,7 +277,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_put_att_text(nc_file_id, nc_dstgrdcrnrlon_id, "units", strlen(tgt_grid_units), tgt_grid_units));
}
/* Define grid mask */
// Define grid mask
nce(nc_def_var(nc_file_id, "src_grid_imask", NC_INT, 1, &nc_srcgrdsize_id, &nc_srcgrdimask_id));
nce(nc_put_att_text(nc_file_id, nc_srcgrdimask_id, "units", 8, "unitless"));
......@@ -290,7 +285,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_def_var(nc_file_id, "dst_grid_imask", NC_INT, 1, &nc_dstgrdsize_id, &nc_dstgrdimask_id));
nce(nc_put_att_text(nc_file_id, nc_dstgrdimask_id, "units", 8, "unitless"));
/* Define grid area arrays */
// Define grid area arrays
if ( lgridarea )
{
......@@ -301,7 +296,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_put_att_text(nc_file_id, nc_dstgrdarea_id, "units", 14, "square radians"));
}
/* Define grid fraction arrays */
// Define grid fraction arrays
nce(nc_def_var(nc_file_id, "src_grid_frac", NC_DOUBLE, 1, &nc_srcgrdsize_id, &nc_srcgrdfrac_id));
nce(nc_put_att_text(nc_file_id, nc_srcgrdfrac_id, "units", 8, "unitless"));
......@@ -309,7 +304,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_def_var(nc_file_id, "dst_grid_frac", NC_DOUBLE, 1, &nc_dstgrdsize_id, &nc_dstgrdfrac_id));
nce(nc_put_att_text(nc_file_id, nc_dstgrdfrac_id, "units", 8, "unitless"));
/* Define mapping arrays */
// Define mapping arrays
nce(nc_def_var(nc_file_id, "src_address", NC_INT, 1, &nc_numlinks_id, &nc_srcadd_id));
nce(nc_def_var(nc_file_id, "dst_address", NC_INT, 1, &nc_numlinks_id, &nc_dstadd_id));
......@@ -319,12 +314,12 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_def_var(nc_file_id, "remap_matrix", NC_DOUBLE, 2, nc_dims2_id, &nc_rmpmatrix_id));
/* End definition stage */
// End definition stage
nce(nc_enddef(nc_file_id));
/* Write mapping data */
// Write mapping data
nce(nc_put_var_int(nc_file_id, nc_srcgrddims_id, src_grid.dims));
nce(nc_put_var_int(nc_file_id, nc_dstgrddims_id, tgt_grid.dims));
......@@ -365,7 +360,7 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
nce(nc_put_var_double(nc_file_id, nc_dstgrdfrac_id, tgt_grid.cell_frac));
for ( i = 0; i < rv.num_links; i++ )
for ( long i = 0; i < rv.num_links; i++ )
{
rv.src_cell_add[i]++;
rv.tgt_cell_add[i]++;
......@@ -382,16 +377,14 @@ void write_remap_scrip(const char *interp_file, int map_type, int submap_type, i
cdoAbort("netCDF support not compiled in!");
#endif
} /* write_remap_scrip */
} // write_remap_scrip
/*****************************************************************************/
void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *map_type, int *submap_type, int *num_neighbors,
int *remap_order, remapgrid_t *src_grid, remapgrid_t *tgt_grid, remapvars_t *rv)
{
/*
The routine reads a netCDF file to extract remapping info in SCRIP format
*/
// The routine reads a netCDF file to extract remapping info in SCRIP format
/*
Input variables
......@@ -399,7 +392,7 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
*/
#if defined(HAVE_LIBNETCDF)
/* Local variables */
// Local variables
int lgridarea = FALSE;
int status;
......@@ -432,8 +425,6 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
int nc_dstadd_id; /* id for map destination address */
int nc_rmpmatrix_id; /* id for remapping matrix */
long i; /* dummy index */
char map_name[1024];
char map_method[64]; /* character string for map_type */
char normalize_opt[64]; /* character string for normalization option */
......@@ -446,13 +437,12 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
int gridID1_gme_c = -1;
// Open file and read some global information
/* Open file and read some global information */
/* nce(nc_open(interp_file, NC_NOWRITE, &nc_file_id)); */
// nce(nc_open(interp_file, NC_NOWRITE, &nc_file_id));
nc_file_id = cdf_openread(interp_file);
/* Map name */
// Map name
nce(nc_get_att_text(nc_file_id, NC_GLOBAL, "title", map_name));
nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "title", &attlen));
......@@ -464,8 +454,7 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
cdoPrint("From file: %s", interp_file);
}
/* Normalization option */
// Normalization option
nce(nc_get_att_text(nc_file_id, NC_GLOBAL, "normalization", normalize_opt));
nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "normalization", &attlen));
normalize_opt[attlen] = 0;
......@@ -487,8 +476,7 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
if ( cdoVerbose )
cdoPrint("normalize_opt = %s", normalize_opt);
/* Map method */
// Map method
nce(nc_get_att_text (nc_file_id, NC_GLOBAL, "map_method", map_method));
nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "map_method", &attlen));
map_method[attlen] = 0;
......@@ -537,8 +525,7 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
*map_type = rv->map_type;
/* File convention */
// File convention
nce(nc_get_att_text (nc_file_id, NC_GLOBAL, "conventions", convention));
nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "conventions", &attlen));
convention[attlen] = 0;
......@@ -552,9 +539,9 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
cdoAbort("Unknown file convention!");
}
/* Read some additional global attributes */
// Read some additional global attributes
/* Source and destination grid names */
// Source and destination grid names
nce(nc_get_att_text (nc_file_id, NC_GLOBAL, "source_grid", src_grid_name));
nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "source_grid", &attlen));
......@@ -567,11 +554,11 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
if ( cdoVerbose )
cdoPrint("Remapping between: %s and %s", src_grid_name, tgt_grid_name);
/* Initialize remapgrid structure */
// Initialize remapgrid structure
remapgrid_init(src_grid);
remapgrid_init(tgt_grid);
/* Read dimension information */
// Read dimension information
nce(nc_inq_dimid(nc_file_id, "src_grid_size", &nc_srcgrdsize_id));
nce(nc_inq_dimlen(nc_file_id, nc_srcgrdsize_id, &dimlen));
......@@ -616,10 +603,10 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
nce(nc_inq_dimid(nc_file_id, "num_links", &nc_numlinks_id));
nce(nc_inq_dimlen(nc_file_id, nc_numlinks_id, &dimlen));
rv->num_links = dimlen;
/*
if ( rv->num_links == 0 )
cdoAbort("Number of remap links is 0, no remap weights found!");
*/
nce(nc_inq_dimid(nc_file_id, "num_wgts", &nc_numwgts_id));
nce(nc_inq_dimlen(nc_file_id, nc_numwgts_id, &dimlen));
rv->num_wts = dimlen;
......@@ -645,14 +632,20 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
rv->resize_increment = (int) (0.1 * MAX(src_grid->size, tgt_grid->size));
/* Allocate address and weight arrays for mapping 1 */
// Allocate address and weight arrays for mapping 1
rv->src_cell_add = (int*) Malloc(rv->num_links*sizeof(int));
rv->tgt_cell_add = (int*) Malloc(rv->num_links*sizeof(int));
rv->src_cell_add = NULL;
rv->tgt_cell_add = NULL;
rv->wts = NULL;
if ( rv->num_links > 0 )
{
rv->src_cell_add = (int*) Malloc(rv->num_links*sizeof(int));
rv->tgt_cell_add = (int*) Malloc(rv->num_links*sizeof(int));
rv->wts = (double*) Malloc(rv->num_wts*rv->num_links*sizeof(double));
rv->wts = (double*) Malloc(rv->num_wts*rv->num_links*sizeof(double));
}
/* Get variable ids */
// Get variable ids
nce(nc_inq_varid(nc_file_id, "src_grid_dims", &nc_srcgrddims_id));
nce(nc_inq_varid(nc_file_id, "src_grid_imask", &nc_srcgrdimask_id));
......@@ -688,7 +681,7 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
nce(nc_inq_varid(nc_file_id, "dst_address", &nc_dstadd_id));
nce(nc_inq_varid(nc_file_id, "remap_matrix", &nc_rmpmatrix_id));
/* Read all variables */
// Read all variables
nce(nc_get_var_int(nc_file_id, nc_srcgrddims_id, src_grid->dims));
......@@ -754,18 +747,21 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
nce(nc_get_var_double(nc_file_id, nc_dstgrdfrac_id, tgt_grid->cell_frac));
nce(nc_get_var_int(nc_file_id, nc_srcadd_id, rv->src_cell_add));
nce(nc_get_var_int(nc_file_id, nc_dstadd_id, rv->tgt_cell_add));
for ( i = 0; i < rv->num_links; i++ )
if ( rv->num_links > 0 )
{
rv->src_cell_add[i]--;
rv->tgt_cell_add[i]--;
}
nce(nc_get_var_int(nc_file_id, nc_srcadd_id, rv->src_cell_add));
nce(nc_get_var_int(nc_file_id, nc_dstadd_id, rv->tgt_cell_add));
for ( long i = 0; i < rv->num_links; i++ )
{
rv->src_cell_add[i]--;
rv->tgt_cell_add[i]--;
}
nce(nc_get_var_double(nc_file_id, nc_rmpmatrix_id, rv->wts));
nce(nc_get_var_double(nc_file_id, nc_rmpmatrix_id, rv->wts));
}
/* Close input file */
// Close input file
nce(nc_close(nc_file_id));
......@@ -780,4 +776,4 @@ void read_remap_scrip(const char *interp_file, int gridID1, int gridID2, int *ma
rv->links.src_add = NULL;
rv->links.dst_add = NULL;
rv->links.w_index = NULL;
} /* read_remap_scrip */
} // read_remap_scrip
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment