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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* read_uwvis.c */
/*
* Functions for reading UW NMS model VIS files.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binio.h"
#include "file.h"
#include "grid.h"
#include "misc.h"
#include "proj.h"
#include "projlist.h"
#include "../src/v5d.h"
/**********************************************************************/
/***** VIS file I/O *****/
/**********************************************************************/
static char vcscr[64] = {
'0','1','2','3','4','5','6','7','8','9'
,'A','B','C','D','E','F','G','H','I','J'
,'K','L','M','N','O','P','Q','R','S','T'
,'U','V','W','X','Y','Z','a','b','c','d'
,'e','f','g','h','i','j','k','l','m','n'
,'o','p','q','r','s','t','u','v','w','x'
,'y','z','{','|'
};
static char inv_vcscr[256];
static void init_visreader( void )
{
int i;
/* init inv_vcscr array */
for (i=0;i<64;i++) {
inv_vcscr[ vcscr[i] ] = i;
}
}
static int *read_int_block( FILE *f, int *len )
{
int words, bits, ch, val, i, j, k, count, bytes;
float a, b;
char line[80];
int *buffer;
/* read number of words, addfac, multfac */
fscanf( f, "%d %d %f %f", &words, &bits, &a, &b);
fgetc( f ); /* skip \n */
buffer = (int *) malloc( words * sizeof(int) );
bytes = (bits+5) / 6;
count = 0;
while (count<words) {
/* read a line */
fgets( line, 80, f );
/* convert line of chars to integers */
for (i=k=0;i<78/bytes && count<words;i++) {
for (j=val=0;j<bytes;j++) {
ch = inv_vcscr[line[k++]];
val = (val << 6) | ch;
}
/* buffer[count++] = val * (int) b + (int) a;*/
buffer[count++] = val * (int) b - (int) a;
}
}
*len = words;
return buffer;
}
static float *read_float_block( FILE *f, int *len )
{
int words, bits, ch, val, i, j, k, count, bytes;
float a, b;
char line[80];
float *buffer;
/* read number of words, bits, addfac, multfac */
fscanf( f, "%d %d %f %f", &words, &bits, &a, &b);
fgetc( f ); /* skip \n */
buffer = (float *) malloc( words * sizeof(float) );
bytes = (bits+5) / 6;
count = 0;
while (count<words) {
/* read a line */
fgets( line, 80, f );
/* convert line of chars to floats */
for (i=k=0;i<78/bytes && count<words;i++) {
for (j=val=0;j<bytes;j++) {
ch = inv_vcscr[line[k++]];
val = (val << 6) | ch;
}
buffer[count++] = (float) val / b - a;
}
}
*len = words;
return buffer;
}
static void skip_float_block( FILE *f )
{
int words, bits, i, count, bytes;
float a, b;
char line[80];
/* read number of words, bits, addfac, multfac */
fscanf( f, "%d %d %f %f", &words, &bits, &a, &b);
fgetc( f ); /* skip \n */
bytes = (bits+5) / 6;
count = 0;
while (count<words) {
/* read a line */
fgets( line, 80, f );
/* convert line of chars to floats */
for (i=0;i<78/bytes && count<words;i++) {
count++;
}
}
}
/**********************************************************************/
/**********************************************************************/
/*
* Scan the named file, createing a grid_info struct for each grid. Store
* the grid_info structs in the grid data base.
* Input: name - name of UW VIS file.
* db - the grid data base
* Return: number of grids found.
*/
int get_uwvis_info( char *name, struct grid_db *db )
{
static int init_flag = 0;
FILE *f;
int grids = 0;
int var, numvars, nr, nc, nl;
float height[MAXLEVELS];
char ch;
int i, vcs;
if (init_flag==0) {
init_visreader();
init_flag = 1;
}
/* Open the file */
f = fopen( name, "r" );
if (!f) {
return 0;
}
fscanf( f, "%d", &numvars );
if (numvars>MAXVARS) {
printf("ERROR: %s contains too many variables, limit is %d\n",
name, MAXVARS );
}
/* grid size */
fscanf( f, "%d", &nc );
fscanf( f, "%d", &nr );
fscanf( f, "%d", &nl );
(void) getc(f); /* get '\n' */
/* this is tricky:
* We look at the next character:
* if it's a letter then
* we read the variable name and extract the height info from header
* else
* read the height values for each grid level (variable delta Z).
* endif
*/
ch = getc(f);
ungetc(ch, f);
if (isalpha(ch)) {
vcs = 1;
}
else {
/* Read the height (in meters) of each grid level */
for (i=0;i<nl;i++) {
fscanf( f, "%8f", &height[i] );
height[i] /= 1000.0; /* convert from meters to km */
}
(void) getc(f); /* get '\n' */
vcs = 2;
}
for (var=0;var<numvars;var++) {
int *header, header_size;
char varname[100];
struct grid_info *info;
float args[100];
/* read variable name */
fgets( varname, 40, f );
for (i=7;i>=0 && varname[i]==' ';i--) {
varname[i] = '\0';
}
varname[8] = 0;
/* read data header */
header = read_int_block( f, &header_size );
/*
* Allocate grid info struct and initialize it.
*/
info = alloc_grid_info();
info->FileName = str_dup( name );
info->Format = FILE_UWVIS;
info->Position = ftell(f); /* save position of data in file */
info->Nr = nr;
info->Nc = nc;
info->Nl = nl;
info->DateStamp = header[5];
info->TimeStamp = header[6];
info->VarName = str_dup( varname );
args[0] = (float) header[22] / 10000.0;
args[1] = (float) header[23] / 10000.0;
args[2] = (float) header[24] / 10000.0;
args[3] = (float) header[25] / 10000.0;
info->Proj = new_projection( db, PROJ_LINEAR, nr, nc, args );
if (vcs==1) {
/* equally spaced km */
float tophgt = (float) header[31] / 1000.0;
float hgtinc = (float) header[32] / 1000.0;
args[0] = tophgt - hgtinc*(nl-1);
args[1] = hgtinc;
}
else {
/* vcs==2 */
/* unequally spaced km */
memcpy( args, height, sizeof(float)*nl );
}
info->Vcs = new_vcs( db, vcs, nl, 0, args );
/*
* done with this grid
*/
append_grid( info, db );
grids++;
free( header );
#ifdef LEAVEOUT
{
float *data;
int data_size;
/* read and skip data */
data = read_float_block( f, &data_size );
free( data );
}
#else
skip_float_block( f );
#endif
}
fclose(f);
return grids;
}
/*
* Get the grid data described by g.
* Input: g - pointer to a grid_info structure. It tells us which grid
* in which file is needed.
* Return: pointer to grid data (can be free()'d when finished)
* OR return NULL if there's an error.
*/
float *get_uwvis_data( struct grid_info *g )
{
float *data;
int count;
FILE *f;
/* open file, seek to grid position */
f = fopen( g->FileName, "r" );
if (!f) return NULL;
fseek( f, g->Position, SEEK_SET );
data = read_float_block( f, &count );
fclose(f);
return data;
}
|