Ecological Landscape Modeling: Models Pages

grid_io.c

Go to the documentation of this file.
00001 
00031 static char *SCCSID = "@(#)grid_io.c    1.10 05/22/00 SFWMD Research and Planning Departments";
00032 
00033 /*
00034 
00035 Copyright, 1995, South Florida Water Management District
00036 
00037 DISCLAIMER:
00038 
00039 ANY INFORMATION, INCLUDING BUT NOT LIMITED TO SOFTWARE AND DATA,
00040 RECEIVED FROM THE SOUTH FLORIDA WATER MANAGEMENT DISTRICT ("DISTRICT")
00041 IN FULFILLMENT OF A PUBLIC RECORDS REQUEST IS PROVIDED "AS IS" WITHOUT
00042 WARRANTY OF ANY KIND, AND THE DISTRICT EXPRESSLY DISCLAIMS ALL EXPRESS
00043 AND IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED
00044 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00045 THE DISTRICT DOES NOT WARRANT, GUARANTEE, OR MAKE ANY REPRESENTATIONS
00046 REGARDING THE USE, OR THE RESULTS OF THE USE, OF THE INFORMATION
00047 PROVIDED TO YOU BY THE DISTRICT IN TERMS OF CORRECTNESS, ACCURACY,
00048 RELIABILITY, TIMELINESS OR OTHERWISE.  THE ENTIRE RISK AS TO THE
00049 RESULTS AND PERFORMANCE OF ANY INFORMATION OBTAINED FROM THE DISTRICT
00050 IS ENTIRELY ASSUMED BY THE RECIPIENT.
00051 
00052 */
00053 
00054 /* -------------------------------------------------------------
00055    grid_io
00056    This module containes routines to read and write the grid
00057    for the SFWMM.  The following routines are included:
00058 
00059    write_grid_header - writes the grid definition header 
00060        in binary format to a file
00061    read_grid_header - reads the grid definition header in
00062        binary format from a file
00063    grid_write  - writes the grid values in binary format
00064    grid_read   - reads the grid values in binary format
00065    grid_count_snapshots  -  returns the num of snapshots
00066 
00067    This module should contain all necessary routines for 
00068    handling binary grid data from the SFWMM.  The functionality
00069    should include at least the following:
00070 
00071       (1) definition of a general binary format containing at 
00072       least (a) specification of the geographical grid (b)
00073       run identification (c) snapshot identification (date) 
00074       (d) snapshot of data for the entire grid
00075 
00076       (2) reading and writing of a snapshot from and to files
00077 
00078       (3) 
00079    ------------------------------------------------------------- */
00080 #define GCC_COMPILER 1
00081 #include <stdio.h>
00082 #include <string.h>
00083 #include <ctype.h>
00084 /* ELMchange #include <varargs.h> no longer supported gcc, need stdarg.h instead */
00085 #include <stdarg.h>
00086 #include <stdlib.h>
00087 
00088 #include "grid_io.h"
00089 
00090 #define TOO_MANY_NODES 10000
00091 #ifndef TRUE
00092 #define TRUE 1
00093 #define FALSE 0
00094 #endif
00095 
00096 /* ELMchange Endian Swapping Routines */
00097 
00115 #ifdef i386
00116 #define Flip_int16(type) ((((type) >>8) & 0x00ff) | (((type) <<8) & 0xff00))
00117 #define Flip_int16_ip(type) (*(type)=(((*(type) >>8) & 0x00ff) | ((*(type) <<8) & 0xff00)))
00118 #define Flip_int32(type) ((((type) >>24) & 0x000000ff) | (((type) >> 8) & 0x0000ff00) | (((type) << 8) & 0x00ff0000) | (((type) << 24) & 0xff000000) )
00119 #define Flip_int32_ip(type) (*(type)=(((*(type) >>24) & 0x000000ff) | ((*(type) >> 8) & 0x0000ff00) | ((*(type) << 8) & 0x00ff0000) | ((*(type) << 24) & 0xff000000) ))
00120 #define Flip_float_ip(type) (Flip_int32_ip((long *)(type)))
00121 #else
00122 #define Flip_int16(type) (type)
00123 #define Flip_int16_ip(type) (*(type))
00124 #define Flip_int32(type) (type)
00125 #define Flip_int32_ip(type) (*(type))
00126 #define Flip_float_ip(type) (*(type))
00127 #endif
00128 
00129 /* internal routines */
00130 void grid_io_fatal(char *name, char *message);
00131 void F77_to_C_string(char *dest, char *src, int length);
00132 void C_to_F77_string(char *dest, char *src, int length);
00133 int openfilef77_(int *unit, char *filename, char *access, int filename_len, int access_len);
00134 int closefilef77_(int *unit);
00135 FILE *getfilep_(int *unit);
00136 /* data for fortran interface routines */
00137 static GRID fortran_grid = {0};
00138 GR_FORTRAN_FILE *gr_fortran_file_list = 0;
00139 
00140 /* ------------------------------------------------------------- 
00141    grid_write_header 
00142    writes the grid definition header to a file
00143  ------------------------------------------------------------- */
00144 
00146 int grid_write_header(FILE *file, GRID *grid)
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 }
00208 
00209 
00210 /* -------------------------------------------------------------
00211    wgridhd_
00212    Fortran interface to the grid_write_header
00213    ------------------------------------------------------------- */
00214 
00216 void gridwhd_(int *fd, int *errs)
00217 {
00218   FILE *file;
00219   file = getfilep_(fd);   
00220 
00221   *errs = grid_write_header(file, &fortran_grid);
00222 }
00223 
00224 /* -------------------------------------------------------------
00225    grid_read_header
00226    reads grid definition information from a file
00227    returns 0 on success, -1 on eof, or some positive number on
00228    error
00229    ------------------------------------------------------------- */
00230 
00235 int grid_read_header(FILE *file, GRID *grid)
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 }
00295 
00296 
00304 void gridrhd_(int *fd, int *errs)
00305 {
00306   FILE *file;
00307   file = getfilep_(fd);   
00308   *errs = grid_read_header(file, &fortran_grid);
00309 }
00310 
00311 /* -------------------------------------------------------------
00312    grid_write
00313    writes a ``snapshot'' of areal data (defined by grid) to a 
00314    binary file 
00315    ------------------------------------------------------------- */
00317 int grid_write(FILE *file, GRID *grid, char *tag, float *values)
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 }
00346 
00347 /* -------------------------------------------------------------
00348    gwrite_
00349    Fortran interface to the grid_write routine
00350    ------------------------------------------------------------- */
00352 void gwrite_(int *fd, char *tag, float *values, int *errs, int tag_len)
00353 {
00354   char buf[GRID_TAG_LENGTH];
00355   FILE *file;
00356   
00357   file = getfilep_(fd);   
00358   F77_to_C_string(buf, tag, ((tag_len < GRID_TAG_LENGTH) ? tag_len : GRID_TAG_LENGTH));
00359   *errs = grid_write(file, &fortran_grid, buf, values);
00360 }
00361 
00362 /* -------------------------------------------------------------
00363    grid_read
00364    reads a ``snapshot'' of areal data
00365    returns 0 on success, -1 on eof, and some positive number on
00366    partial read
00367    ------------------------------------------------------------- */
00374 int grid_read(FILE *file, GRID *grid, char *tag, float *values)
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 }
00401 
00402 /* -------------------------------------------------------------
00403    gread_
00404    Fortran interface to the grid_read routine
00405    ------------------------------------------------------------- */
00407 void gread_(int *fd, char *tag, float *values, int *errs, int tag_len)
00408 {
00409   char buf[GRID_TAG_LENGTH];
00410   FILE *file;
00411   
00412   file = getfilep_(fd);   
00413   *errs = grid_read(file, &fortran_grid, buf, values);
00414   C_to_F77_string(tag, buf, ((tag_len < GRID_TAG_LENGTH) ? tag_len : GRID_TAG_LENGTH));
00415 }
00416 
00417 /* -------------------------------------------------------------
00418    grid_skip
00419    This routine will move the file pointer the specified number
00420    of records.
00421    ------------------------------------------------------------- */
00427 int grid_skip(FILE *file, GRID *grid, int count)
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 }
00457 
00458 /* -------------------------------------------------------------
00459    gridskp_
00460    Fortran interface to the grid_skip routine.  Note
00461    that it changes the contents of the fortran_grid.  To get
00462    the header info a call to getgrid_ is needed (cjn 1/94)
00463    ------------------------------------------------------------- */
00465 void gridskp_(int *fd, int *cnt, int *errs)
00466 {
00467   FILE *file; 
00468   file = getfilep_(fd);   
00469   *errs = grid_skip(file, &fortran_grid, *cnt);
00470 }
00471 
00472 /* -------------------------------------------------------------
00473    grid_top
00474    This routine places the file pointer before the first data
00475    record in the file
00476    ------------------------------------------------------------- */
00481 int grid_top(FILE *file, GRID *grid)
00482 {
00483   fseek(file, 0L, 0);
00484   return(grid_read_header(file, grid));
00485 }
00486 
00487 /* -------------------------------------------------------------
00488    grid_bottom
00489    This routine moves the file pointer to just before the final
00490    data record in the file.
00491    ------------------------------------------------------------- */
00498 int grid_bottom(FILE *file,GRID *grid)
00499 {
00500   fseek(file, 0L, 2);
00501   return(grid_skip(file, grid, -1));
00502 }
00503 
00504 /* -------------------------------------------------------------
00505    grid_count_snapshots
00506    This routine returns the num of snapshots in the given gridio binary
00507    file. It assumes that the gridio binary file has already been opened
00508    and the grid_header already read. Before returning, it sets the
00509    file pointer back to the 1st record.
00510    ------------------------------------------------------------- */
00517 int grid_count_snapshots(FILE *file, GRID *grid) {
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 }
00529 
00531 char *strsed(register char *string, register char *pattern, int *range);
00532 
00533 /* ELMchange (?) this grid_tag_search function was entirely commented out (?done so in original code?) */
00534 /* -------------------------------------------------------------
00535    grid_tag_search
00536    This routine searches the grid_tags from the current position
00537    in the file for the regular expression passed.  The routine
00538    returns 1 if a matching tag was found, 0 if not.  If the
00539    the search was unsuccessfull the file is set to the bottom
00540    grid data.
00541    ------------------------------------------------------------- */
00542 /*
00543 int grid_tag_search(FILE *file, GRID *grid, char *string)
00544 {
00545   char *char_ptr, tag[GRID_TAG_LENGTH], buffer[GRID_TAG_LENGTH];
00546   int range[2];
00547   float *data = (float *)malloc(grid->header.number_of_nodes*sizeof(float));
00548   int found = FALSE;
00549 
00550   sprintf(buffer, "/%s/", string);
00551 
00552   while (!found) {
00553     if (grid_read(file, grid, tag, data) != 0) {
00554       grid_bottom(file, grid);
00555       break;
00556     } else {
00557       if (strsed(tag, buffer, range) == 0)
00558         grid_io_fatal("grid_tag_search", "Error using strsed routine");
00559       else if (range[0] != -1 && range[1] != -1) {
00560          found = TRUE;
00561          grid_skip(file, grid, -1);
00562        }
00563     }
00564   }
00565 
00566   char_ptr = (char *) data;
00567   free(char_ptr);
00568   return (found);
00569 } */
00570 
00571 
00572 /* -------------------------------------------------------------
00573    grid_node
00574    This routine returns the array index of the node cooresponding
00575    to the row and column number passed.
00576    ------------------------------------------------------------- */
00584 int grid_node(GRID *grid, int row, int column)
00585 {
00586   if (row > 0 && row <= grid->header.number_of_rows) {
00587     if (column >= grid->config.xstart[row - 1] && column <= grid->config.xend[row - 1]) 
00588       return ((grid->config.cum_node_count[row - 1] - 1) + column - grid->config.xstart[row - 1] + 1);
00589   }
00590   return -1;
00591 }
00592 
00593 /* -------------------------------------------------------------
00594    setgrid_
00595    Fortran call to set internal grid definition record 
00596    ------------------------------------------------------------- */
00598 void setgrid_(char *title, int *nrows, int *nnodes, float *xsize, float *ysize, int *xstart, int *xend, int *cum_count, int title_len)
00599 {
00600   int i, array_size;
00601 
00602   F77_to_C_string(fortran_grid.header.title, title, 
00603                   ((title_len < GRID_TITLE_LENGTH) ? title_len : GRID_TITLE_LENGTH));
00604 
00605   fortran_grid.header.number_of_rows = *nrows;
00606   fortran_grid.header.number_of_nodes = *nnodes;
00607   fortran_grid.header.size.x = *xsize;
00608   fortran_grid.header.size.y = *ysize;
00609 
00610   /* allocate space for configuration arrays */
00611   array_size = (*nrows < MAX_GRID_ROWS) ? MAX_GRID_ROWS : *nrows;
00612   fortran_grid.config.xstart = (int *) malloc(array_size * sizeof(int));
00613   fortran_grid.config.xend = (int *) malloc(array_size * sizeof(int));
00614   fortran_grid.config.cum_node_count = (int *) malloc(array_size * sizeof(int));
00615   
00616   for (i = 0; i < fortran_grid.header.number_of_rows; i++) {
00617     fortran_grid.config.xstart[i] = xstart[i];
00618     fortran_grid.config.xend[i] = xend[i];
00619     fortran_grid.config.cum_node_count[i] = cum_count[i];
00620   }
00621 }
00622 
00623 /* -------------------------------------------------------------
00624    getgrid_
00625    Fortran call to get internal grid definition record 
00626    by Karen Lythgoe and Cal Neidrauer, since Bill Perkins
00627    documented a non-existent routine.  Documentation is like sex,
00628    too much is much better than not enough!
00629    ------------------------------------------------------------- */
00631 void getgrid_(char *title, int *nrows, int *nnodes, float *xsize, float *ysize,
00632          int *xstart, int *xend, int *cum_count, int title_len)
00633 {
00634   int i;
00635 
00636   C_to_F77_string(title,fortran_grid.header.title,  
00637                   ((title_len < GRID_TITLE_LENGTH) ? title_len : GRID_TITLE_LENGTH));
00638 
00639   *nrows = fortran_grid.header.number_of_rows;
00640   *nnodes = fortran_grid.header.number_of_nodes;
00641   *xsize = fortran_grid.header.size.x;
00642   *ysize = fortran_grid.header.size.y;
00643   for (i = 0; i < fortran_grid.header.number_of_rows; i++) {
00644     xstart[i] = fortran_grid.config.xstart[i];
00645     xend[i] = fortran_grid.config.xend[i];
00646     cum_count[i] =fortran_grid.config.cum_node_count[i];
00647   }
00648 }
00649 
00650 /* -------------------------------------------------------------
00651    grid_io_fatal
00652    prints fatal error messages
00653    ------------------------------------------------------------- */
00658 void grid_io_fatal(char *name, char *message)
00659 { 
00660   (void) fprintf(stderr, "ERROR in %s: ", name);
00661   (void) fprintf(stderr, message);
00662   (void) abort();
00663 }
00664 
00665 /* -------------------------------------------------------------
00666    F77_to_C_string
00667    converts fortran style strings to C style strings
00668    ------------------------------------------------------------- */
00670 void F77_to_C_string(char *dest, char *src, int length)
00671 {
00672   int i;
00673   strncpy(dest, src, length);
00674   for (i = length - 1; isspace(dest[i]); i--)
00675     dest[i] = '\0';
00676 }
00677 
00678 
00679 /* -------------------------------------------------------------
00680    C_to_F77_string
00681    converts C style strings to Fortran style
00682    ------------------------------------------------------------- */
00684 void C_to_F77_string(char *dest, char *src, int length)
00685 {
00686   int i;
00687 
00688   strncpy(dest,src,length);
00689   for (i = strlen(dest); i < length; i++)
00690     dest[i] = ' ';
00691 }
00692 
00693 
00694 /* -------------------------------------------------------------
00695    grid_free
00696    free up memory allocated for grid header
00697    ------------------------------------------------------------- */
00703 int grid_free(GRID *grid)
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 }
00713 
00715 int openfilef77_(int *unit, char *filename, char *access, int filename_len, int access_len)
00716 {
00717   GR_FORTRAN_FILE *iFile=gr_fortran_file_list;
00718   GR_FORTRAN_FILE *prevFile=gr_fortran_file_list;
00719   char *filename_buf = (char *) malloc(sizeof(char)*(filename_len+1));
00720   char *access_buf = (char *) malloc(sizeof(char)*(access_len+1));
00721 
00722   F77_to_C_string(filename_buf, filename, filename_len);
00723   F77_to_C_string(access_buf, access, access_len);
00724 
00725   while (iFile != 0) {
00726     if(iFile->unit_number == *unit) {
00727        fprintf(stderr,"Error: unit %d already open, can not open file %s as unit %d\n", *unit, filename, *unit);
00728       return 0;
00729 
00730   }
00731     prevFile=iFile;
00732     iFile = iFile->next;
00733   }
00734 
00735   iFile = (GR_FORTRAN_FILE *) malloc(sizeof(GR_FORTRAN_FILE));
00736   if(prevFile == 0 )
00737     gr_fortran_file_list = iFile;
00738   else
00739     prevFile->next = iFile;
00740   iFile->fptr = fopen(filename_buf, access_buf);
00741   if (iFile->fptr == 0) {
00742      fprintf(stderr,"Error: can not open file \"%s\" as unit %d\n",
00743              filename_buf, *unit);
00744   }
00745   iFile->unit_number = *unit;
00746   iFile->next = 0;
00747   return 1;
00748 }
00749 
00751 int closefilef77_(int *unit)
00752 {
00753   GR_FORTRAN_FILE *iFile=gr_fortran_file_list;
00754   GR_FORTRAN_FILE *prevFile=gr_fortran_file_list;
00755   while (iFile != 0) {
00756     if(iFile->unit_number == *unit) {
00757       if(iFile == gr_fortran_file_list)
00758         gr_fortran_file_list = iFile->next;
00759       else
00760         prevFile->next = iFile->next;
00761       free(iFile);
00762       return 1;
00763     }
00764   }
00765   return 0;
00766 }
00767 
00769 FILE* getfilep_(int *unit)
00770 {
00771   GR_FORTRAN_FILE *iFile;
00772   for(iFile = gr_fortran_file_list; iFile!=0; iFile =iFile->next) {
00773     if(iFile->unit_number == *unit) return iFile->fptr;
00774   }
00775   return 0;
00776 }
00777 

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