gridspec_api/mosaic/tst_two_tiles_print_polytopes.c

00001 /*
00002  * Test the definition of a structured grid.
00003  *
00004  * "$Id: tst_two_tiles_print_polytopes.c 507 2011-02-14 21:27:52Z dkindig $"
00005  * */
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 
00010 #include <libcf_src.h>
00011 #include <netcdf.h>
00012 #include <nccf_grid.h>
00013 #include <nccf_coord.h>
00014 #include <nccf_contacts.h>
00015 #include <nccf_mosaic.h>
00016 #include <nccf_varObj.h>
00017 #include <nccf_handle_error.h>
00018 
00019 int main(int argc,char **argv){
00020 
00021   /* Initialize the the cflists */
00022   CFLIST_STRUCTURED_GRID = NULL;
00023   CFLIST_CONTACTS = NULL;
00024 
00025   /* Variables for the grid */
00026   // Each Id goes with a ncid
00027   int contactId, mosaicid, i, j;
00028   int status, ncid;
00029   int iGrid; // Some counters
00030   int ndims = 2;         // Number of x, y dims
00031   int ngrids = 2;
00032   int nlon = 10;         // number of x faces, gives nlon + 1 edges
00033   int dims[] = {nlon, nlon};
00034   double period[] = {0., 0.};
00035 
00036   int save = 1;
00037 
00038   nc_type nc_mode = NC_CLOBBER;
00039 
00040   char *name = "tst_two_tile";
00041   char grid_filename[STRING_SIZE] = "tst_two_tiles_contacts.nc";
00042   char mosaic_filename[STRING_SIZE] = "tst_two_tiles_mosaic.nc";
00043   char gridname[STRING_SIZE];
00044   char filename[STRING_SIZE];
00045   const char *dimnames[] = {"ni", "nj"};
00046 
00047   double *lon1, *lat1, *lon2, *lat2, dlon, dlat;
00048   const double lonMin =   0.0;
00049   const double lonMax = 360.0;
00050   const double latMin = -90.0;
00051   const double latMax =  90.0;
00052 
00053   lon1 = ( double * )malloc( sizeof( double ) * nlon * nlon );
00054   lat1 = ( double * )malloc( sizeof( double ) * nlon * nlon );
00055   lon2 = ( double * )malloc( sizeof( double ) * nlon * nlon );
00056   lat2 = ( double * )malloc( sizeof( double ) * nlon * nlon );
00057 
00058   dlat = ( latMax - latMin )/( nlon - 1 );
00059   dlon = ( lonMax - lonMin )/( nlon - 1 )/2;
00060 
00061   for( j = 0; j < nlon; j++){
00062     for( i = 0; i < nlon; i++ ){
00063       lon1[j + nlon * i] = lonMin + j * dlon;
00064       lon2[j + nlon * i] = lonMin + j * dlon + 180;
00065       lat1[j + nlon * i] = latMin + i * dlat;
00066       lat2[j + nlon * i] = latMin + i * dlat;
00067     }
00068   }
00069   /* Ccoordinate ids - written over for each tile. */
00070 //  coordIds = (int *) malloc( ndims * sizeof( int ));
00071 
00072   int coordIds[ndims];
00073 //  gridid = ( int* )malloc( ngrids * sizeof( int ));
00074   int gridid[ndims];
00075 
00076 /* Define a grid */
00077   ngrids = 2;
00078 /* Calculate the coordinates for the current tile */
00079 
00080 /* Define tile 0 */
00081   if(( status = nccf_def_lon_coord( ndims, dims, dimnames, lon1, save,
00082                                          &coordIds[0] ))) ERR;
00083   if(( status = nccf_def_lat_coord( ndims, dims, dimnames, lat1, save,
00084                                          &coordIds[1] ))) ERR;
00085 
00086   iGrid = 0;
00087   sprintf( gridname, "%s%d", name, iGrid + 1 );
00088   sprintf( filename, "%s.nc", gridname );
00089   if(( status = nccf_def_struct_grid( coordIds,
00090                gridname, &gridid[iGrid] ))) ERR;
00091 
00092 /* Write tile 0 */
00093   nc_create( filename, nc_mode, &ncid );             
00094   nccf_put_struct_grid( ncid, filename, gridid[iGrid] );
00095   nc_close( ncid );
00096 
00097 /* Define tile 1 */
00098   if(( status = nccf_def_lon_coord( ndims, dims, dimnames, lon2, save,
00099                                          &coordIds[0] ))) ERR;
00100   if(( status = nccf_def_lat_coord( ndims, dims, dimnames, lat2, save,
00101                                          &coordIds[1] ))) ERR;
00102 
00103   iGrid = 1;
00104   sprintf( gridname, "%s%d", name, iGrid + 1 );
00105   sprintf( filename, "%s.nc", gridname );
00106   if(( status = nccf_def_struct_grid(coordIds,
00107                gridname, &gridid[iGrid] ))) ERR;
00108   
00109 /* Write tile 1 */
00110   nc_create( filename, nc_mode, &ncid );             
00111   nccf_put_struct_grid( ncid, filename, gridid[iGrid] );
00112   nc_close( ncid );
00113 
00114 /* Define the contacts */
00115   if(( status = nccf_def_contacts( ngrids, gridid, gridname,
00116                                 period, &contactId ))) ERR;
00117 
00118 /* Write the file */
00119   if(( status = nc_create( grid_filename, nc_mode, &ncid ))) ERR;
00120   if(( status = nccf_put_contacts( ncid, filename, contactId ))) ERR;
00121   if(( status = nc_close( ncid ))) ERR;
00122 
00123 /* Define the mosaic */  
00124   if(( status = nccf_def_mosaic( contactId, name, &mosaicid )));
00125 
00126 /* Write the mosaic */
00127   if(( status = nc_create( mosaic_filename, nc_mode, &ncid ))) ERR;
00128   if(( status = nccf_put_mosaic( ncid, mosaicid ))) ERR;
00129   if(( status = nc_close( ncid ))) ERR;
00130 
00131   if(( status = nccf_print_contacts_as_polytopes(contactId,"polytopes" ))) ERR;
00132 
00133 /* Free the mosaic */
00134   if(( status = nccf_free_mosaic( mosaicid )));
00135 
00136 /* Free the contacts */
00137   if(( status = nccf_free_contacts( contactId ))) ERR;
00138 
00139 /* Free the grid and the coordinates */
00140   for( iGrid = 0; iGrid < ngrids; iGrid++ ){
00141     if(( status = nccf_get_struct_grid_coordIds( gridid[iGrid],
00142         coordIds))) ERR;
00143     if(( status = nccf_free_struct_grid( gridid[iGrid] ))) ERR;
00144     for (i = 0; i < ndims; ++i) {
00145       if((status = nccf_free_coord(coordIds[i]))) ERR;
00146     }
00147   }
00148 
00149 
00150   free( lon1 );
00151   free( lon2 );
00152   free( lat1 );
00153   free( lat2 );
00154 //  free(coordIds);
00155 //  free(gridid);
00156 
00157 /* Close the file*/
00158   return 0;
00159 }
 All Classes Files Functions Defines

Generated on Tue Mar 1 2011 06:36:59 for libCF. LibCF is a Unidata library.