1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/* grid.c */
/*
* Functions for manipulating grids and lists of grids.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grid.h"
#include "memory.h"
#include "projlist.h"
/*
* Allocate a grid_info struct and initialize it.
*/
struct grid_info *alloc_grid_info( void )
{
struct grid_info *g;
g = (struct grid_info *) calloc( 1, sizeof(struct grid_info) );
if (g) {
/* all fields will be 0 thanks to calloc but here's some special cases */
g->TimeStep = g->VarNum = g->Position = -1;
g->Proj = NULL;
g->Vcs = NULL;
g->SelectBits = ALL_BITS;
}
return g;
}
/*
* Deallocate a grid_info struct.
*/
void free_grid_info( struct grid_info *info )
{
if (info->FileName) {
FREE( info->FileName, 1000 );
}
if (info->VarName) {
FREE( info->VarName, 1001 );
}
if (info->Units) {
FREE( info->Units, 1002 );
}
if (info->Data) {
FREE( info->Data, 1003 );
}
/* "erase" data to help with debugging */
/*memset( info, 0xff, sizeof(struct grid_info) );*/
FREE( info, 1004 );
}
/*
* Allocate a grid_list struct and initialize it.
*/
struct grid_db *alloc_grid_db()
{
struct grid_db *gl;
/* Allocate w/ every field initialized to zero */
gl = (struct grid_db *) calloc( sizeof(struct grid_db), 1 );
return gl;
}
/*
* Deallocate a grid_db struct.
*/
void free_grid_db( struct grid_db *db )
{
struct grid_info *g, *next;
int i;
/* free grid_info list */
for (g=db->FirstGrid; g; g=next) {
next = g->Next;
free_grid_info( g );
}
/* free varname and units strings */
for (i=0;i<db->NumVars;i++) {
FREE( db->VarNames[i], 1005 );
if (db->Units[i]) {
FREE( db->Units[i], 1005 );
}
}
/* free projections */
for (i=0;i<db->NumProj;i++) {
FREE( db->ProjList[i], 1006 );
}
/* free VCSs */
for (i=0;i<db->NumVcs;i++) {
FREE( db->VcsList[i]->Args, 1007 );
FREE( db->VcsList[i], 1008 );
}
FREE( db, 1008 );
}
/*
* Append a grid_info struct onto the tail of the list.
*/
void append_grid( struct grid_info *grid, struct grid_db *db )
{
int i, inlist;
if (db->LastGrid) {
db->LastGrid->Next = grid;
db->LastGrid = grid;
}
else {
db->FirstGrid = db->LastGrid = grid;
}
grid->Next = NULL;
db->NumGrids++;
db->Sorted = 0;
}
/*
* Remove a grid_info struct from a list.
* Return: 1 = success, 0 = error
*/
int remove_grid( struct grid_info *grid, struct grid_db *db )
{
struct grid_info *pred;
int found;
/* Find predecessor to the grid */
if (db->FirstGrid==grid) {
/* grid to remove is first in list */
db->FirstGrid = grid->Next;
pred = NULL;
found = 1;
}
else {
for (pred=db->FirstGrid; pred; pred=pred->Next) {
if (pred->Next==grid) {
/* found predecessor! */
pred->Next = grid->Next;
found = 1;
break;
}
}
}
if (db->LastGrid==grid) {
db->LastGrid = pred;
}
db->NumGrids--;
return found;
}
/*
* Free all the grids and associated data in a grid_db.
*/
void free_all_grids( struct grid_db *db )
{
struct grid_info *g, *nextg;
int i, j;
/* free grids */
for (g=db->FirstGrid; g; g=nextg) {
nextg = g->Next;
free_grid_info( g );
}
db->FirstGrid = db->LastGrid = NULL;
db->NumGrids = 0;
/* Free map projections */
for (i=0;i<db->NumProj;i++) {
FREE( db->ProjList[i]->Args, 1100 );
FREE( db->ProjList[i], 1101 );
db->ProjList[i] = NULL;
}
db->NumProj = 0;
/* Free VCSs */
for (i=0;i<db->NumVcs;i++) {
FREE( db->VcsList[i]->Args, 1102 );
FREE( db->VcsList[i], 1103 );
db->VcsList[i] = NULL;
}
db->NumVcs = 0;
/* Clear pointers in grid matrix */
for (i=0;i<db->NumTimes;i++) {
for (j=0;j<db->NumVars;j++) {
db->Matrix[i][j] = NULL;
}
}
db->NumVars = 0;
db->NumTimes = 0;
db->Sorted = 0;
}
/*
* Print a grid list to stdout. Just used for debugging.
*/
void print_grid_list( struct grid_db *db )
{
struct grid_info *g;
int i = 1;
printf(" Grid Date Time Variable Nr Nc Nl Proj# Vcs# Filename\n");
for (g=db->FirstGrid; g; g=g->Next) {
int projnum = lookup_proj( db, g->Proj );
int vcsnum = lookup_vcs( db, g->Vcs );
printf("%c %4d %05d %06d %-10s %3d %3d %3d %3d %3d %s\n",
g->SelectBits==ALL_BITS ? '*': ' ',
i, g->DateStamp, g->TimeStamp, g->VarName, g->Nr, g->Nc, g->Nl,
projnum, vcsnum,
g->FileName );
i++;
}
printf("*=include grid in output file\n");
}
/*
* "Print" the grid list to generate an array of strings.
* Return null if no grids in list.
*/
char **sprint_grid_list( struct grid_db *db )
{
struct grid_info *g;
int i = 0;
char **vector;
if (db->NumGrids==0) {
return NULL;
}
vector = (char **) MALLOC( db->NumGrids * sizeof(char *) );
/* See gui.h for column headings */
for (g=db->FirstGrid; g; g=g->Next) {
int projnum = lookup_proj( db, g->Proj );
int vcsnum = lookup_vcs( db, g->Vcs );
vector[i] = (char *) MALLOC( 1000 );
sprintf( vector[i],
"%4d %05d %06d %-10s%3d %3d %3d %2d %2d %s%c",
i+1,
g->DateStamp, g->TimeStamp,
g->VarName,
g->Nr, g->Nc, g->Nl,
projnum, vcsnum,
g->FileName,
g->Sibling ? ',' : ' ' );
i++;
}
return vector;
}
/*
* Find the maximum number of grid levels of all grids in the list.
* Input: db - the grid data base
* Return: max number of grid levels in list or -1 if empty list
*/
int find_max_levels( struct grid_db *db )
{
struct grid_info *g;
int maxnl = -1;
for (g=db->FirstGrid; g; g=g->Next) {
if (g->Nl>maxnl) {
maxnl = g->Nl;
}
}
return maxnl;
}
|