Ecological Landscape Modeling: Models Pages

grid_io.h File Reference

Header file for grid_io utility functions. More...

#include "stdlib.h"
#include "stdio.h"

Include dependency graph for grid_io.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GR_HEADER
struct  GR_CONFIG
struct  GRID
struct  GR_FORTRAN_FILE_STRUC

Defines

#define MAX_GRID_ROWS   66
#define GRID_TITLE_LENGTH   80
#define GRID_TAG_LENGTH   80

Typedefs

typedef struct
GR_FORTRAN_FILE_STRUC 
GR_FORTRAN_FILE

Functions

int grid_write_header (FILE *file, GRID *grid)
 UNUSED: writes the grid definition header to a file.
int grid_read_header (FILE *file, GRID *grid)
 reads grid definition information from a file
int grid_write (FILE *file, GRID *grid, char *tag, float *values)
 UNUSED writes a ``snapshot'' of areal data (defined by grid) to a binary file.
int grid_read (FILE *file, GRID *grid, char *tag, float *values)
 reads a snapshot of areal data
int grid_skip (FILE *file, GRID *grid, int num)
 This routine will move the file pointer the specified number of records.
int grid_tag_search (FILE *file, GRID *grid, char *string)
int grid_top (FILE *file, GRID *grid)
 This routine places the file pointer before the first data record in the file.
int grid_bottom (FILE *file, GRID *grid)
 UNUSED. This routine moves the file pointer to just before the final data record in the file.
int grid_count_snapshots (FILE *file, GRID *grid)
 UNUSED. This routine returns the num of snapshots in the given gridio binaryfile.
int grid_free (GRID *grid)
 UNUSED. free up memory allocated for grid header.


Detailed Description

Header file for grid_io utility functions.

This defines or declares variables & functions that are global to grid_io.c.

Note: documented with Doxygen, which expects specific syntax within special comments.

The Everglades Landscape Model (ELM).
last updated: Jan 2005

Definition in file grid_io.h.


Define Documentation

#define MAX_GRID_ROWS   66

maximum number of data array rows

Definition at line 54 of file grid_io.h.

Referenced by grid_read_header(), grid_skip(), grid_write_header(), and setgrid_().

#define GRID_TITLE_LENGTH   80

maximum string length of grid_io header title

Definition at line 57 of file grid_io.h.

Referenced by getgrid_(), grid_write_header(), and setgrid_().

#define GRID_TAG_LENGTH   80

the length of the tag identifying a grid-data snapshot

Definition at line 58 of file grid_io.h.

Referenced by formatDate(), gread_(), grid_count_snapshots(), grid_read(), grid_skip(), grid_write(), gwrite_(), and initDataStruct().


Typedef Documentation

Definition at line 88 of file grid_io.h.


Function Documentation

int grid_write_header ( FILE *  file,
GRID grid 
)

UNUSED: writes the grid definition header to a file.

Definition at line 146 of file grid_io.c.

References GRID::config, GR_CONFIG::cum_node_count, Flip_float_ip, Flip_int32, Flip_int32_ip, grid_io_fatal(), GRID_TITLE_LENGTH, GRID::header, MAX_GRID_ROWS, GR_HEADER::number_of_nodes, GR_HEADER::number_of_rows, GR_HEADER::size, GR_HEADER::title, GR_HEADER::x, GR_CONFIG::xend, GR_CONFIG::xstart, and GR_HEADER::y.

Referenced by gridwhd_().

00147 {
00148   int array_size, errs;
00149   int *tmp_xstart, *tmp_xend, *tmp_cum_node_count;
00150   errs = 0;
00151   tmp_xstart = grid->config.xstart;
00152   tmp_xend = grid->config.xend;
00153   tmp_cum_node_count = grid->config.cum_node_count;
00154 
00155   {
00156     GR_HEADER tmpHeader;
00157     strncpy(tmpHeader.title, grid->header.title, GRID_TITLE_LENGTH);
00158     tmpHeader.number_of_rows = Flip_int32(grid->header.number_of_rows);
00159     tmpHeader.number_of_nodes = Flip_int32(grid->header.number_of_nodes);
00160     tmpHeader.size.x =  grid->header.size.x;
00161     Flip_float_ip(&(tmpHeader.size.x));
00162     tmpHeader.size.y =  grid->header.size.y;
00163     Flip_float_ip(&(tmpHeader.size.y));
00164     if (fwrite(&tmpHeader, sizeof(GR_HEADER), 1, file) == 0) 
00165       ++errs;
00166   }
00167 
00168   if (grid->header.number_of_rows < MAX_GRID_ROWS)
00169     array_size = MAX_GRID_ROWS;
00170   else
00171     array_size = grid->header.number_of_rows;
00172 
00173 #ifdef i386
00174   {
00175     int i;
00176     tmp_xstart =  (int *) malloc(array_size * sizeof(int));
00177     memcpy(tmp_xstart, grid->config.xstart, array_size * sizeof(int));
00178 
00179     tmp_xend =  (int *) malloc(array_size * sizeof(int));
00180     memcpy(tmp_xend, grid->config.xend, array_size * sizeof(int));
00181 
00182     tmp_cum_node_count =  (int *) malloc(array_size * sizeof(int));
00183     memcpy(tmp_cum_node_count, grid->config.cum_node_count, array_size * sizeof(int));
00184 
00185     for (i=0; i<array_size; ++i) {
00186       Flip_int32_ip(tmp_xstart+i);
00187       Flip_int32_ip(tmp_xend+i);
00188       Flip_int32_ip(tmp_cum_node_count+i);
00189     }
00190   }
00191 #endif
00192   if (fwrite(tmp_xstart, sizeof(int), array_size, file) == 0)
00193     ++errs;
00194   if (fwrite(tmp_xend, sizeof(int), array_size, file) == 0)
00195     ++errs;
00196   if (fwrite(tmp_cum_node_count, sizeof(int), array_size, file) == 0)
00197     ++errs;
00198 
00199   if (errs)
00200     grid_io_fatal("write_grid_header", "Unable to write file header\n");
00201 
00202 #ifdef i386
00203   free(tmp_xstart); free(tmp_xend); free(tmp_cum_node_count);
00204 #endif
00205   
00206   return (0);
00207 }

Here is the call graph for this function:

int grid_read_header ( FILE *  file,
GRID grid 
)

reads grid definition information from a file

Parameters:
file The data file
grid The struct containing grid_io info
Returns:
errs level of success

Definition at line 235 of file grid_io.c.

References GRID::config, GR_CONFIG::cum_node_count, Flip_float_ip, Flip_int32_ip, grid_io_fatal(), GRID::header, MAX_GRID_ROWS, GR_HEADER::number_of_nodes, GR_HEADER::number_of_rows, GR_HEADER::size, GR_HEADER::x, GR_CONFIG::xend, GR_CONFIG::xstart, and GR_HEADER::y.

Referenced by grid_top(), gridrhd_(), initDataStruct(), and processData().

00236 {
00237   int errs = 0, num_read, array_size;
00238 
00239   if ((num_read = fread(&grid->header, sizeof(GR_HEADER), 1, file)) == 0) {
00240     if (feof(file))
00241       errs = -1;
00242     else 
00243       grid_io_fatal("read_grid_header", "Unable to read header from file\n");
00244   }
00245   Flip_int32_ip(&(grid->header.number_of_rows));
00246   Flip_int32_ip(&(grid->header.number_of_nodes));
00247   Flip_float_ip(&(grid->header.size.x));
00248   Flip_float_ip(&(grid->header.size.y));
00249 
00250   if (grid->header.number_of_rows < MAX_GRID_ROWS)
00251     array_size = MAX_GRID_ROWS;
00252   else
00253     array_size = grid->header.number_of_rows;
00254 
00255   /* allocate space for configuration arrays */
00256   grid->config.xstart = (int *) malloc(array_size * sizeof(int));
00257 
00258   grid->config.xend = (int *) malloc(array_size * sizeof(int));
00259 
00260   grid->config.cum_node_count = (int *) malloc(array_size * sizeof(int));
00261 
00262   if ((num_read = fread(grid->config.xstart, sizeof(int), array_size, file)) == 0) {
00263     if (feof(file))
00264       errs = -1;
00265     else 
00266       grid_io_fatal("read_grid_header", "Unable to read header from file\n");
00267   }
00268 
00269   if ((num_read = fread(grid->config.xend, sizeof(int), array_size, file)) == 0) {
00270     if (feof(file))
00271       errs = -1;
00272     else
00273       grid_io_fatal("read_grid_header", "Unable to read header from file\n");
00274   }
00275 
00276   if ((num_read = fread(grid->config.cum_node_count, sizeof(int), array_size, file)) == 0) {
00277     if (feof(file))
00278       errs = -1;
00279     else
00280       grid_io_fatal("read_grid_header", "Unable to read header from file\n");
00281   }
00282   
00283 #ifdef i386
00284   {
00285     int i;
00286     for (i=0; i<array_size; ++i) {
00287       Flip_int32_ip(grid->config.xstart+i);
00288       Flip_int32_ip(grid->config.xend+i);
00289       Flip_int32_ip(grid->config.cum_node_count+i);
00290     }
00291   }
00292 #endif
00293   return(errs);
00294 }

Here is the call graph for this function:

int grid_write ( FILE *  file,
GRID grid,
char *  tag,
float *  values 
)

UNUSED writes a ``snapshot'' of areal data (defined by grid) to a binary file.

Definition at line 317 of file grid_io.c.

References Flip_float_ip, grid_io_fatal(), GRID_TAG_LENGTH, GRID::header, and GR_HEADER::number_of_nodes.

Referenced by gwrite_().

00318 {
00319   char *char_ptr;
00320   int written, errs = 0;
00321   float *tmp_values = values;
00322 
00323   if ((written = fwrite(tag, sizeof(char), GRID_TAG_LENGTH, file)) == 0)
00324     grid_io_fatal("grid_write", "Unable to write grid tag\n");
00325   if (written < GRID_TAG_LENGTH) errs++;
00326 #ifdef i386
00327   {
00328     int i;
00329     tmp_values = (float *) malloc(grid->header.number_of_nodes * sizeof(float));
00330     memcpy(tmp_values, values, grid->header.number_of_nodes * sizeof(float));
00331     for (i=0; i<grid->header.number_of_nodes; ++i) {
00332       Flip_float_ip(tmp_values+i);
00333     }
00334   }
00335 #endif
00336     if ((written = fwrite(tmp_values, sizeof(float), grid->header.number_of_nodes, file)) == 0)
00337       grid_io_fatal("grid_write", "Unable to write grid data\n");
00338 
00339   if (written < grid->header.number_of_nodes) errs++;
00340 #ifdef i386
00341   free (tmp_values);
00342 #endif
00343   return(errs);
00344 
00345 }

Here is the call graph for this function:

int grid_read ( FILE *  file,
GRID grid,
char *  tag,
float *  values 
)

reads a snapshot of areal data

Parameters:
file The data file
grid The struct containing grid_io info
tag the special grid tag identifier
values data values
Returns:
errs level of success

Definition at line 374 of file grid_io.c.

References Flip_float_ip, grid_io_fatal(), GRID_TAG_LENGTH, GRID::header, and GR_HEADER::number_of_nodes.

Referenced by gread_(), grid_count_snapshots(), initDataStruct(), and processData().

00375 {
00376   int num_read, errs = 0;
00377 
00378   /* read the tag */
00379   if ((num_read = fread(tag, sizeof(char), GRID_TAG_LENGTH, file)) == 0) {
00380     if (feof(file))
00381       return (-1);
00382     else
00383       grid_io_fatal("grid_read", "Uable to read grid tag\n");
00384   }
00385   if (num_read != GRID_TAG_LENGTH) errs++;
00386 
00387   /* read the values */
00388   if ((num_read = fread(values, sizeof(float), grid->header.number_of_nodes, file)) == 0) 
00389     grid_io_fatal("grid_read", "Unable to read data\n");
00390 #ifdef i386
00391   {
00392     int i;
00393     for (i=0; i<grid->header.number_of_nodes; ++i) {
00394       Flip_float_ip(values+i);
00395     }
00396   }
00397 #endif
00398   if (num_read != grid->header.number_of_nodes) errs++;
00399   return (errs);
00400 }

Here is the call graph for this function:

int grid_skip ( FILE *  file,
GRID grid,
int  count 
)

This routine will move the file pointer the specified number of records.

Parameters:
file The data file
grid The struct containing grid_io info
count count
Returns:
errs level of success

Definition at line 427 of file grid_io.c.

References grid_bottom(), GRID_TAG_LENGTH, grid_top(), GRID::header, MAX_GRID_ROWS, GR_HEADER::number_of_nodes, and GR_HEADER::number_of_rows.

Referenced by grid_bottom(), gridskp_(), initDataStruct(), and processData().

00428 {
00429   int header_size, array_size;
00430   int errs = 0;
00431   long int rec_len =  GRID_TAG_LENGTH*sizeof(char) +
00432     grid->header.number_of_nodes*sizeof(float);
00433   long int end, current;
00434 
00435   if (count < 0) {
00436     if (grid->header.number_of_rows < MAX_GRID_ROWS)
00437       array_size = MAX_GRID_ROWS;
00438     else
00439       array_size = grid->header.number_of_rows;
00440     header_size = sizeof(GRID) + 3 * sizeof(int) * array_size;
00441     if (ftell(file) <  header_size + abs(count)*rec_len)
00442       errs = grid_top(file, grid);
00443     else
00444       fseek(file, count*rec_len, 1);
00445   } else if (count > 0) {
00446     current = ftell(file);
00447     fseek(file, 0, 2);
00448     end = ftell(file);
00449     if ((end - current)/rec_len < count)
00450       errs = grid_bottom(file, grid);
00451     else 
00452       errs = fseek(file, (long)(current + count*rec_len), 0);
00453   }
00454   
00455   return(errs);
00456 }

Here is the call graph for this function:

int grid_tag_search ( FILE *  file,
GRID grid,
char *  string 
)

int grid_top ( FILE *  file,
GRID grid 
)

This routine places the file pointer before the first data record in the file.

Parameters:
file The data file
grid The struct containing grid_io info
Returns:
errs level of success

Definition at line 481 of file grid_io.c.

References grid_read_header().

Referenced by grid_count_snapshots(), and grid_skip().

00482 {
00483   fseek(file, 0L, 0);
00484   return(grid_read_header(file, grid));
00485 }

Here is the call graph for this function:

int grid_bottom ( FILE *  file,
GRID grid 
)

UNUSED. This routine moves the file pointer to just before the final data record in the file.

While this is currently UNUSED, it could be useful in future.

Parameters:
file The data file
grid The struct containing grid_io info
Returns:
errs level of success

Definition at line 498 of file grid_io.c.

References grid_skip().

Referenced by grid_skip().

00499 {
00500   fseek(file, 0L, 2);
00501   return(grid_skip(file, grid, -1));
00502 }

Here is the call graph for this function:

int grid_count_snapshots ( FILE *  file,
GRID grid 
)

UNUSED. This routine returns the num of snapshots in the given gridio binaryfile.

While this is currently UNUSED, it could be useful in future.

Parameters:
file The data file
grid The struct containing grid_io info
Returns:
i count

Definition at line 517 of file grid_io.c.

References grid_read(), GRID_TAG_LENGTH, grid_top(), GRID::header, and GR_HEADER::number_of_nodes.

00517                                                  {
00518   int i = 0;
00519   char tag[GRID_TAG_LENGTH];
00520   float *data = (float *)malloc(grid->header.number_of_nodes*sizeof(float));
00521 
00522   grid_top(file, grid);
00523   while (grid_read(file, grid, tag, data) == 0)
00524     i++;
00525   free(data);
00526   grid_top(file, grid);
00527   return(i);
00528 }

Here is the call graph for this function:

int grid_free ( GRID grid  ) 

UNUSED. free up memory allocated for grid header.

While this is currently UNUSED, it could be useful in future.

Parameters:
grid The struct containing grid_io info
Returns:
errs level of success

Definition at line 703 of file grid_io.c.

References GRID::config, GR_CONFIG::cum_node_count, GR_CONFIG::xend, and GR_CONFIG::xstart.

00704 {
00705   int errs = 0;
00706 
00707   free(grid->config.xstart);
00708   free(grid->config.xend);
00709   free(grid->config.cum_node_count);
00710 
00711   return(errs);
00712 }


Generated on Sat Jan 7 14:04:33 2012 for ELM source code by  doxygen 1.5.6