void write_UV10 () { int nc_id; int dim_ids[2]; int u10_id; int v10_id; static char title[] = "example netCDF dataset"; nc_error(nc_create("out/wind/uv10.vec.nc", 0, &nc_id)); nc_error(nc_def_dim(nc_id, "south_north", nSN, &dim_ids[0])); nc_error(nc_def_dim(nc_id, "west_east", nWE, &dim_ids[1])); nc_error(nc_def_var (nc_id, "u10", NC_FLOAT, 2, dim_ids, &u10_id)); nc_error(nc_def_var (nc_id, "v10", NC_FLOAT, 2, dim_ids, &v10_id)); nc_error(nc_put_att_text (nc_id, NC_GLOBAL, "title", strlen(title), title)); nc_error(nc_enddef(nc_id)); nc_error(nc_put_var_float(nc_id, u10_id, U10)); nc_error(nc_put_var_float(nc_id, v10_id, V10)); nc_error(nc_close(nc_id)); }
void write_CLOUD () { fprintf(stdout, "Writing CLOUD\n"); nc_error(nc_put_var_float(ncout_ID, idCLOUD, wCLOUD)); nc_error(nc_put_var_float(ncout_ID, idCLOUDPCT_H, wCLOUDPCT_H)); nc_error(nc_put_var_float(ncout_ID, idCLOUDPCT_M, wCLOUDPCT_M)); nc_error(nc_put_var_float(ncout_ID, idCLOUDPCT_L, wCLOUDPCT_L)); }
int insert_place() { placecount++; if (rd_ident && rd_ident != placecount) autonumbering = 0; if (!rd_ident && autonumbering) rd_ident = placecount; if (!rd_ident) nc_error("missing place identifier"); if (rd_ident > AnzPlNamen) AnzPlNamen = rd_ident; else if (PlArray[rd_ident]) nc_error("place identifier %d used twice", rd_ident); while (AnzPlNamen >= MaxPlNamen) { int count; PlArray = MYrealloc((char*) PlArray, (MaxPlNamen += NAMES_OFFSET) * sizeof(place_t*)); for (count = MaxPlNamen - NAMES_OFFSET; count < MaxPlNamen;) PlArray[count++] = NULL; } PlArray[rd_ident] = nc_create_place(rd_net); PlArray[rd_ident]->name = rd_name? MYstrdup(rd_name) : NULL; if (rd_marked > 1) nc_error("place %s has more than one token",rd_name); PlArray[rd_ident]->marked = !!rd_marked; return 0; }
int insert_trans() { if (!transcount++) autonumbering = 1; if (rd_ident && rd_ident != transcount) autonumbering = 0; if (!rd_ident && autonumbering) rd_ident = transcount; if (!rd_ident) nc_error("missing transition identifier"); if (rd_ident > AnzTrNamen) AnzTrNamen = rd_ident; else if (TrArray[rd_ident]) nc_error("transition identifier %d used twice",rd_ident); while (AnzTrNamen >= MaxTrNamen) { int count; TrArray = MYrealloc((char*) TrArray, (MaxTrNamen += NAMES_OFFSET) * sizeof(trans_t*)); for (count = MaxTrNamen - NAMES_OFFSET; count < MaxTrNamen;) TrArray[count++] = NULL; } TrArray[rd_ident] = nc_create_transition(rd_net); TrArray[rd_ident]->name = rd_name? MYstrdup(rd_name) : NULL; return 0; }
int insert_arc() { static int tp = 0; /* tp = 1 means Place->Trans, 0 is Trans->Place */ int pl, tr; if (*blocktype) { tp = strcmp(blocktype,"PT"); *blocktype = '\0'; } pl = tp? rd_co->y : rd_co->x; tr = tp? rd_co->x : rd_co->y; if (!tr || (tr > AnzTrNamen) || !TrArray[tr]) nc_error("arc: incorrect transition identifier"); if (!pl || (pl > AnzPlNamen) || !PlArray[pl] ) nc_error("arc: incorrect place identifier"); tp? nc_create_arc(&(TrArray[tr]->postset),&(PlArray[pl]->preset), TrArray[tr],PlArray[pl]) : nc_create_arc(&(PlArray[pl]->postset),&(TrArray[tr]->preset), PlArray[pl],TrArray[tr]); return 0; }
void load_TOPO () { fprintf(stdout, "Loading TOPO\n"); int topo_id; nc_error(nc_inq_varid (wrfout_id, "HGT", &topo_id)); wTOPO = malloc (wN2D * sizeof(float)); if (wTOPO==NULL) {fprintf(stderr, "topo.c : Cannot allocate wTOPO\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, topo_id, wTOPO)); }
void load_MODEL_LEVEL () { fprintf(stdout, "Loading MODEL_LEVEL\n"); int model_level_id; nc_error(nc_inq_varid (wrfout_id, "ZNU", &model_level_id)); wMODEL_LEVEL = malloc (wNZ * sizeof(float)); if (wMODEL_LEVEL==NULL) {fprintf(stderr, "model_level.c : Cannot allocate wMODEL_LEVEL\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, model_level_id, wMODEL_LEVEL)); }
void load_UV10 () { int u10_id; int v10_id; nc_error(nc_inq_varid (wrfout_id, "U10", &u10_id)); nc_error(nc_inq_varid (wrfout_id, "V10", &v10_id)); U10 = malloc (nWE * nSN * sizeof(float)); if (U10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate U10\n"); exit(-1);} V10 = malloc (nWE * nSN * sizeof(float)); if (V10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate V10\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, u10_id, U10)); nc_error(nc_get_var_float(wrfout_id, v10_id, V10)); double deg2rad = M_PI/180; if (MAP_PROJ == 1 || MAP_PROJ == 2) { double cone; if (MAP_PROJ == 1) { // lambert conformal if ( (fabs(TRUELAT1 - TRUELAT2) > 0.1) && (fabs(TRUELAT2 - 90.) > 0.1)) { cone = ( pow(10, cos(TRUELAT1*deg2rad)) - pow(10, cos(TRUELAT2*deg2rad)) ) / ( pow(10, tan(45. -fabs(TRUELAT1/2.)*deg2rad)) - pow(10, tan(45. -fabs(TRUELAT2/2.)*deg2rad)) ); } else { cone = sin(fabs(TRUELAT1)*deg2rad); } } else if (MAP_PROJ == 2) { // polar stereographic cone = 1.; } float * umet10 = malloc (nWE * nSN * sizeof(float)); if (umet10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate UMET10\n"); exit(-1);} float * vmet10 = malloc (nWE * nSN * sizeof(float)); if (vmet10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate VMET10\n"); exit(-1);} int is_stag = 1; int is_missing_val = 0; calc_uvmet (U10, V10, umet10, vmet10, cone, is_stag, is_missing_val); U10 = umet10; V10 = vmet10; free (umet10); free (vmet10); } }
int insert_ra() { int tr = rd_co->x, pl = rd_co->y; if (!tr || (tr > AnzTrNamen) || !TrArray[tr]) nc_error("readarc: incorrect transition identifier"); if (!pl || (pl > AnzPlNamen) || !PlArray[pl] ) nc_error("readarc: incorrect place identifier"); nc_create_arc(&(TrArray[tr]->readarcs),&(PlArray[pl]->readarcs), TrArray[tr],PlArray[pl]); return 0; }
/** * Wrapper function for memory allocation. */ void* MYcalloc(size_t size) { void * m; if (!(m = calloc(size, 1))) nc_error("memory allocation rejected!"); memset(m,0,size); return m; }
struct nc * nc_open(char * address_str, short port) { struct nc * nc; struct sockaddr_in address; if (!nc_initted) { if (nc_init()) { //fprintf(stderr, "Couldn't initialize netclient library\n"); return NULL; } } nc = malloc(sizeof *nc); if (!nc) { //fprintf(stderr, "Couldn't allocate memory for struct nc\n"); return NULL; } memset(nc, 0, sizeof *nc); nc->fd = -1; memset(&address, 0, sizeof address); address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = parse_addr(address_str); if (address.sin_addr.s_addr == INADDR_NONE) { //fprintf(stderr, "Couldn't determine address for \"%s\"\n", address_str); return NULL; } nc_dump("creating socket", NULL, 0); nc->fd = socket(AF_INET, SOCK_STREAM, 0); if (nc->fd == INVALID_SOCKET) { nc_error("open_nc socket"); return NULL; } nc_dump("created", NULL, 0); nc_dump("connecting", NULL, 0); if (connect(nc->fd, (struct sockaddr *)&address, sizeof(address)) == -1) { nc_error("open_nc connect"); return NULL; } nc_dump("connected", NULL, 0); return nc; }
void write_UV10_pol () { int nc_id; int dim_ids[2]; int dir10_id; int speed10_id; static char title[] = "example netCDF dataset"; float * dir10 = malloc (nWE * nSN * sizeof(float)); if (dir10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate DIR10\n"); exit(-1);} float * speed10 = malloc (nWE * nSN * sizeof(float)); if (speed10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate SPEED10\n"); exit(-1);} int i; int nvals = nWE * nSN; for (i=0; i<nvals; i++) { dir10[i] = fmod( (atan2(V10[i], U10[i]) * 180 / M_PI + 360) , 360); speed10[i] = sqrt( pow(U10[i],2) + pow(V10[i],2) ); } nc_error(nc_create("out/wind/uv10.pol.nc", 0, &nc_id)); nc_error(nc_def_dim(nc_id, "south_north", nSN, &dim_ids[0])); nc_error(nc_def_dim(nc_id, "west_east", nWE, &dim_ids[1])); nc_error(nc_def_var (nc_id, "dir10", NC_FLOAT, 2, dim_ids, &dir10_id)); nc_error(nc_def_var (nc_id, "speed10", NC_FLOAT, 2, dim_ids, &speed10_id)); nc_error(nc_put_att_text (nc_id, NC_GLOBAL, "title", strlen(title), title)); nc_error(nc_enddef(nc_id)); nc_error(nc_put_var_float(nc_id, dir10_id, dir10)); nc_error(nc_put_var_float(nc_id, speed10_id, speed10)); free (dir10); free (speed10); nc_error(nc_close(nc_id)); }
void load_RAIN () { fprintf(stdout, "Loading RAIN\n"); int rain_id; int rainc_id; nc_error(nc_inq_varid (wrfout_id, "RAINNC", &rain_id)); nc_error(nc_inq_varid (wrfout_id, "RAINC", &rainc_id)); wRAIN = malloc (wN2D * sizeof(float)); if (wRAIN==NULL) {fprintf(stderr, "rain.c : Cannot allocate wRAIN\n"); exit(-1);} wRAINC = malloc (wN2D * sizeof(float)); if (wRAINC==NULL) {fprintf(stderr, "rain.c : Cannot allocate wRAINC\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, rain_id, wRAIN)); nc_error(nc_get_var_float(wrfout_id, rainc_id, wRAINC)); int i; if (wFRAME_N_LAST >= 0) { fprintf(stdout, "Getting accumulated rain values from previous file\n"); int rain_last_id; int rainc_last_id; nc_error(nc_inq_varid (wrfout_last_id, "RAINNC", &rain_last_id)); nc_error(nc_inq_varid (wrfout_last_id, "RAINC", &rainc_last_id)); wRAIN_LAST = malloc (wN2D * sizeof(float)); if (wRAIN_LAST==NULL) {fprintf(stderr, "rain.c : Cannot allocate wRAIN_LAST\n"); exit(-1);} wRAINC_LAST = malloc (wN2D * sizeof(float)); if (wRAINC_LAST==NULL) {fprintf(stderr, "rain.c : Cannot allocate wRAINC_LAST\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_last_id, rain_last_id, wRAIN_LAST)); nc_error(nc_get_var_float(wrfout_last_id, rainc_last_id, wRAINC_LAST)); #pragma omp parallel for private(i) for (i=0; i<wN2D; i++) { wRAIN[i] -= wRAIN_LAST[i]; wRAINC[i] -= wRAINC_LAST[i]; } free(wRAIN_LAST); free(wRAINC_LAST); } else { fprintf(stderr, "rain.c : WARNING : no previous rain. Using raw values.\n"); } for (i=0; i<wN2D; i++) { wRAIN[i] += wRAINC[i]; } }
void load_TEMP2M () { fprintf(stdout, "Loading TEMP2M\n"); int temp2m_id; nc_error(nc_inq_varid (wrfout_id, "T2", &temp2m_id)); wTEMP2M = malloc (wN2D * sizeof(float)); if (wTEMP2M==NULL) {fprintf(stderr, "temp2m.c : Cannot allocate wTEMP2M\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, temp2m_id, wTEMP2M)); int i; for (i=0; i<wN2D; i++) { wTEMP2M[i] -= 273.16; } }
void load_RH () { fprintf(stdout, "Loading RH\n"); int qvapor_id; nc_error(nc_inq_varid (wrfout_id, "QVAPOR", &qvapor_id)); wRH = malloc (wN3D * sizeof(float)); if (wRH==NULL) {fprintf(stderr, "rh.c : Cannot allocate wRH\n"); exit(-1);} wQVAPOR = malloc (wN3D * sizeof(float)); if (wQVAPOR==NULL) {fprintf(stderr, "rh.c : Cannot allocate wQVAPOR\n"); exit(-1);} nc_error(nc_get_var_float(wrfout_id, qvapor_id, wQVAPOR)); double svp1=0.6112; double svp2=17.67; double svp3=29.65; double ep_3=0.622; double es; double qvs; int i; #pragma omp parallel for private(i) for (i=0; i<wN3D; i++) { es = 10.*svp1*exp(svp2*(wTEMP[i])/(wTK[i]-svp3)); qvs = ep_3*es/ (wPRESS[i] - (1.-ep_3)*es); wRH[i] = 100.*MAX(MIN(wQVAPOR[i]/qvs,1.0),0.0); } wRH_A = malloc (wN2D * ip_nALEVELS * sizeof(float)); if (wRH_A==NULL) {fprintf(stderr, "rh.c : Cannot allocate wRH_A\n"); exit(-1);} wRH_P = malloc (wN2D * ip_nPLEVELS * sizeof(float)); if (wRH_P==NULL) {fprintf(stderr, "rh.c : Cannot allocate wRH_P\n"); exit(-1);} #pragma omp parallel for private(i) for (i=0; i<ip_nALEVELS; i++) { interpolate_3d_z (wRH, ip_ALEVELS[i], wHEIGHT, &wRH_A[wN2D*i]); } #pragma omp parallel for private(i) for (i=0; i<ip_nPLEVELS; i++) { interpolate_3d_z (wRH, ip_PLEVELS[i], wPRESS, &wRH_P[wN2D*i]); } }
int nc_write(struct nc * nc, unsigned char * buf, size_t len) { int r; nc_dump("sending...", buf, len); r = send(nc->fd, buf, len, 0); if (r < 0) nc_error("nc_write"); nc_dump("sent", NULL, 0); return r; }
static void defineVars(Context ctx,const char *var,void *value,int type,int elems){ int ret,t,varid=-1; char *pstrFt=NULL; DimRef *pdr=NULL; if(ctx.ncid<=0||!ctx.param||!var)return; pdr=(DimRef*)ctx.param; if(0==strncmp(VAR_TIME,var,strlen(VAR_TIME))){ if(NC_NOERR!=(ret=nc_def_var(ctx.ncid,VAR_TIME,NC_DOUBLE,1,pdr->dim1t,&varid))) nc_error(ret,__LINE__); }else if(0==strncmp(VAR_OBS,var,strlen(VAR_OBS))){ pstrFt=(char*)queryTableValue(ctx.L,NULL,NULL,FEATURE_TYPE); if(pstrFt){ if(0==strncmp(TIMESERIES_PROFILE,pstrFt,strlen(TIMESERIES_PROFILE))){ if(NC_NOERR!=(ret=nc_def_var(ctx.ncid,VAR_OBS,NC_DOUBLE,3,pdr->dim3,&varid))) nc_error(ret,__LINE__); } else if(0==strncmp(TIMESERIES,pstrFt,strlen(TIMESERIES))){ if(NC_NOERR!=(ret=nc_def_var(ctx.ncid,VAR_OBS,NC_DOUBLE,2,pdr->dim2,&varid))) nc_error(ret,__LINE__); } free(pstrFt); } }else if(0==strncmp(VAR_Z,var,strlen(VAR_Z))){ if(NC_NOERR!=(ret=nc_def_var(ctx.ncid,VAR_Z,NC_DOUBLE,1,pdr->dim1z,&varid))) nc_error(ret,__LINE__); }else{ t=query1DVarType(ctx.L,NULL,VALUE); if(t==LUA_TNUMBER)t=NC_DOUBLE; else if(t==LUA_TSTRING)t=NC_STRING; if(t==NC_DOUBLE||t==NC_STRING){ if(NC_NOERR!=(ret=nc_def_var(ctx.ncid,var,t,1,pdr->dim1,&varid))) nc_error(ret,__LINE__); } } if(varid!=-1){ ctx.pfunCallback=populateAttrs; ctx.attrscope=varid; loopTable(&ctx,VAR_ATTRIBUTES,0); } }
void nc_static_checks (net_t* net, char *stoptr_name) { place_t *pl; trans_t *tr; for (tr = net->transitions; tr; tr = tr->next) { if (!tr->preset || !tr->postset) nc_warning("%s is not restricted",tr->name); if (stoptr_name && !strcmp(tr->name,stoptr_name)) stoptr = tr; } if (stoptr_name && !stoptr) nc_error("Transition %s not found",stoptr_name); for (pl = net->places; pl; pl = pl->next) if (pl->marked) break; if (!pl) nc_error("no initial marking"); }
static void fill_rcv_buf(struct nc * nc) { int r; nc_dump("receiving...", NULL, 0); r = recv(nc->fd, nc->rcv_buf, sizeof(nc->rcv_buf), 0); nc->low = 0; if (r <= 0) { if (r < 0) nc_error("fill_rcv_buf recv"); /* XXX -- error or closed, return whatever we got*/ nc->high = 0; } else { nc->high = r; } nc_dump("received", nc->rcv_buf, nc->high); }
static int nc_init(void) { #ifdef _WIN32 WSADATA wd; nc_dump("initting", NULL, 0); if (WSAStartup(MAKEWORD(2,2), &wd) == -1) { nc_error("WSAStartup"); return -1; } #endif nc_initted = 1; //if (getenv("NC_DUMP")) // nc_do_dump = 1; atexit(nc_fini); return 0; }
void read_PEP_file(char *filename, char **types, t_blockinfo *blocks, t_blockdest *dest) { FILE *infile; t_lookup *tbl; t_fieldinfo *fld; t_dest *dst; t_blockdest *sdest = dest; int i, len = 0, ch, num, num2, ralloc; char *restptr, *rtmp; HLinput_file = filename; HLinput_line = 1; /* Open the file, read the header. */ if (!(infile = fopen(filename, "r"))) nc_error("could not open file for reading"); ReadCmdToken(infile); if (strcmp(sbuf, "PEP")) nc_error("keyword `PEP' expected"); /* Check if the file's type (second line of file) is one of those that are allowed. */ ReadNewline(infile); ReadCmdToken(infile); for (; *types && strcmp(sbuf,*types); types++); if (!*types) nc_error("unexpected format identifier '%s'",sbuf); filetype = MYstrdup(sbuf); ReadNewline(infile); ReadCmdToken(infile); if (strncmp(sbuf, "FORMAT_N", 8)) nc_error("keyword 'FORMAT_N' or 'FORMAT_N2' expected"); tbl = (t_lookup*) MYmalloc(sizeof(t_lookup)*(128-' ')) - ' '; ReadNewline(infile); while (!feof(infile)) { /* Read next block id. */ ReadCmdToken(infile); blocktype = MYstrdup(sbuf); /* Identify block. */ for (; blocks->name && strcmp(blocks->name,sbuf); blocks++) if (!blocks->optional) nc_error("keyword '%s' expected",blocks->name); if (!blocks->name) nc_error("unknown keyword '%s'",sbuf); for (dest = sdest; dest->name; dest++) if (!strcmp(blocks->name,dest->name)) break; /* Set up tbl. */ for (i=' '; i<128; tbl[i++].type = 0); for (i=' '; i<128; tbl[i++].ptr = NULL); for (fld = blocks->field; fld->c; fld++) tbl[(int)fld->c].type = fld->type; if (!blocks->line) ReadNewline(infile); /* Read lines until next block begins. Every line yields one new entity of the type determined by the current block. */ for(;;) { if (isupper(ch = ReadCharComment(infile))) { char ch2 = getc(infile); ungetc(ch2,infile); /* We assume that uppercase letters at the start of a line always indicates a new block. */ if (isupper((int)ch2)) break; } if (feof(infile)) break; if (ch == '\n') continue; *(rtmp = restptr = MYmalloc(ralloc = 64)) = '\0'; /* If information about this block is wanted, take the information where to store data from dest. */ if (dest->name) for (dst = dest->destarray; dst->c; dst++) { t_lookup *l = tbl + dst->c; l->ptr = dst->ptr; switch(l->type) { case FT_STRING: case FT_COORDS: *(char**)(l->ptr) = NULL; break; case FT_NUMBER: case FT_FLAG: *(int*)(l->ptr) = 0; break; default: nc_error("internal error: don't " "know the type of field '%c'",dst->c); break; } } /* Parse until end of line. We assume that all information about an entity (place, transition etc.) is stored in one single line. */ while (ch != '\n') { if (isdigit(ch) || ch == '-') { /* Numbers are treated specially. x@y gets stored in the '@' field, and plain numbers are stored in the '0' field. */ ungetc(ch,infile); ReadNumber(infile,&num); if ((ch=ReadWhiteSpace(infile)) == '@' || ch == '<' || ch == '>') { ch = '@'; ReadNumber(infile,&num2); len = 24; } else { ungetc(ch,infile); ch = '0'; len = 12; } } else { if (ch == '\'' || ch == '"') ungetc(ch,infile); switch(tbl[ch].type) { case FT_STRING: ReadEnclString(infile); len = 3+strlen(sbuf); break; case FT_NUMBER: ReadNumber(infile,&num); len = 12; break; case FT_COORDS: ReadCoordinates(infile, &num, &num2); len = 24; break; case FT_FLAG: len = 1; break; default: nc_error("unknown token '%c'",ch); break; } } /* Store data in the memory locations specified by the dest array. If no location is given for a particular field, its contents are written to a 'rest' string. */ while (!tbl[ch].ptr && rtmp-restptr+len >= ralloc) { restptr = MYrealloc(restptr, ralloc += 64); rtmp = restptr + strlen(restptr); } switch(tbl[ch].type) { case FT_STRING: if (tbl[ch].ptr) *(char**)(tbl[ch].ptr) = MYstrdup(sbuf); else if (strchr("'\"",ch)) sprintf(rtmp,"\"%s\"",sbuf); else sprintf(rtmp,"%c\"%s\"",ch,sbuf); break; case FT_NUMBER: if (tbl[ch].ptr) *(int*)(tbl[ch].ptr) = num; else if (ch == '0') sprintf(rtmp,"%d ",num); else sprintf(rtmp,"%c%d",ch,num); break; case FT_COORDS: if (tbl[ch].ptr) { *((t_coords**)(tbl[ch].ptr)) = MYmalloc(sizeof(t_coords)); (*(t_coords**)(tbl[ch].ptr))->x = num; (*(t_coords**)(tbl[ch].ptr))->y = num2; } else if (ch == '@') sprintf(rtmp,"%d@%d ",num,num2); else sprintf(rtmp,"%c%d@%d",ch,num,num2); break; case FT_FLAG: if (tbl[ch].ptr) (*(int*)(tbl[ch].ptr))++; else sprintf(rtmp,"%c",ch); break; } if (!tbl[ch].ptr) rtmp += strlen(rtmp); ch = ReadCharComment(infile); } /* end of while */ /* When the whole line has been read, see if information about the current block is wanted - if so, call the corresponding hook function. */ if (dest->name) { if (dest->restptr) *(dest->restptr) = restptr; if (dest->hookfunc()) nc_error("read aborted"); } else free(restptr); HLinput_line++; } free(blocktype); if (!feof(infile)) ungetc(ch,infile); blocks++; } /* Check for mandatory blocks that didn't occur in the file. */ for (; blocks->name; blocks++) if (!blocks->optional) nc_error("section '%s' not found",blocks->name); free(tbl+' '); fclose(infile); }
/** * Wrapper function for memory allocation. */ void* MYrealloc(void *ptr, size_t size) { void *r; if (!(r = realloc(ptr,size))) nc_error("memory allocation rejected!"); return r; }
/** * Wrapper function for memory allocation. */ char* MYstrdup(char *string) { char *str; if (!(str = strdup(string))) nc_error("memory allocation rejected!"); return str; }
/* Output the data for a single variable, in CDL syntax. */ int vardata( const ncvar_t *vp, /* variable */ size_t vdims[], /* variable dimension sizes */ int ncid, /* netcdf id */ int varid, /* variable id */ const fspec_t *fsp /* formatting specs */ ) { size_t cor[NC_MAX_DIMS]; /* corner coordinates */ size_t edg[NC_MAX_DIMS]; /* edges of hypercube */ size_t add[NC_MAX_DIMS]; /* "odometer" increment to next "row" */ size_t gulp; void *vals; int id; int ir; size_t nels; size_t ncols; size_t nrows; int vrank = vp->ndims; nels = 1; for (id = 0; id < vrank; id++) { cor[id] = 0; edg[id] = 1; nels *= vdims[id]; /* total number of values for variable */ } printf("\n"); indent_out(); /* printf(" %s = ", vp->name); */ /* or */ /* printf(" %s =\n ", vp->name); */ printf(" "); print_name(vp->name); if (vrank <= 1) { printf(" = "); set_indent ((int)strlen(vp->name) + 4 + indent_get()); } else { printf(" =\n "); set_indent (2 + indent_get()); } if (vrank < 1) { ncols = 1; } else { ncols = vdims[vrank-1]; /* size of "row" along last dimension */ edg[vrank-1] = vdims[vrank-1]; for (id = 0; id < vrank; id++) add[id] = 0; if (vrank > 1) add[vrank-2] = 1; } nrows = nels/ncols; /* number of "rows" */ gulp = ncols < VALBUFSIZ ? ncols : VALBUFSIZ; vals = emalloc(gulp * vp->tinfo->size); for (ir = 0; ir < nrows; ir++) { /* * rather than just printing a whole row at once (which might * exceed the capacity of some platforms), we break each row * into smaller chunks, if necessary. */ size_t corsav = 0; int left = (int)ncols; boolean lastrow; if (vrank > 0) { corsav = cor[vrank-1]; if (fsp->brief_data_cmnts != false && vrank > 1 && left > 0) { /* print brief comment with indices range */ /* printf("// %s(",vp->name); */ printf("// "); print_name(vp->name); printf("("); switch (fsp->data_lang) { case LANG_C: /* print brief comment with C variable indices */ for (id = 0; id < vrank-1; id++) printf("%lu,", (unsigned long)cor[id]); if (vdims[vrank-1] == 1) printf("0"); else printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1); break; case LANG_F: /* print brief comment with Fortran variable indices */ if (vdims[vrank-1] == 1) printf("1"); else printf("1-%lu ", (unsigned long)vdims[vrank-1]); for (id = vrank-2; id >=0 ; id--) { printf(",%lu", (unsigned long)(1 + cor[id])); } break; } printf(")\n"); indent_out(); printf(" "); set_indent(4 + indent_get()); } } lastrow = (boolean)(ir == nrows-1); while (left > 0) { size_t toget = left < gulp ? left : gulp; if (vrank > 0) edg[vrank-1] = toget; NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals)); /* Test if we should treat array of chars as a string */ if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) { pr_tvals(vp, toget, left > toget, lastrow, (char *) vals, fsp, cor); } else { pr_any_vals(vp, toget, left > toget, lastrow, vals, fsp, cor); } left -= toget; if (vrank > 0) cor[vrank-1] += toget; } if (vrank > 0) cor[vrank-1] = corsav; if (ir < nrows-1) if (!upcorner(vdims,vp->ndims,cor,add)) nc_error("vardata: odometer overflowed!"); set_indent(2); } free(vals); return 0; }
/* Output the data for a single variable, in NcML syntax. * TODO: currently not called, need option for NcML with values ... */ int vardatax( const ncvar_t *vp, /* variable */ size_t vdims[], /* variable dimension sizes */ int ncid, /* netcdf id */ int varid, /* variable id */ const fspec_t *fsp /* formatting specs */ ) { size_t cor[NC_MAX_DIMS]; /* corner coordinates */ size_t edg[NC_MAX_DIMS]; /* edges of hypercube */ size_t add[NC_MAX_DIMS]; /* "odometer" increment to next "row" */ size_t gulp; void *vals; int id; int ir; size_t nels; size_t ncols; size_t nrows; int vrank = vp->ndims; nels = 1; for (id = 0; id < vrank; id++) { cor[id] = 0; edg[id] = 1; nels *= vdims[id]; /* total number of values for variable */ } printf(" <values>\n "); set_indent (7); if (vrank < 1) { ncols = 1; } else { ncols = vdims[vrank-1]; /* size of "row" along last dimension */ edg[vrank-1] = vdims[vrank-1]; for (id = 0; id < vrank; id++) add[id] = 0; if (vrank > 1) add[vrank-2] = 1; } nrows = nels/ncols; /* number of "rows" */ gulp = ncols < VALBUFSIZ ? VALBUFSIZ : ncols; vals = emalloc(gulp * vp->tinfo->size); for (ir = 0; ir < nrows; ir++) { /* * rather than just printing a whole row at once (which might * exceed the capacity of some platforms), we break each row * into smaller chunks, if necessary. */ size_t corsav; int left = (int)ncols; boolean lastrow; if (vrank > 0) { corsav = cor[vrank-1]; } lastrow = (boolean)(ir == nrows-1); while (left > 0) { size_t toget = left < gulp ? left : gulp; if (vrank > 0) edg[vrank-1] = toget; NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) ); /* Test if we should treat array of chars as a string */ if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) { pr_tvalsx(vp, toget, left > toget, lastrow, (char *) vals); } else { pr_any_valsx(vp, toget, left > toget, lastrow, vals); } left -= toget; if (vrank > 0) cor[vrank-1] += toget; } if (vrank > 0) cor[vrank-1] = corsav; if (ir < nrows-1) if (!upcorner(vdims,vp->ndims,cor,add)) nc_error("vardata: odometer overflowed!"); set_indent(2); } printf(" </values>\n"); free(vals); return 0; }
void write_RAIN () { fprintf(stdout, "Writing RAIN\n"); nc_error(nc_put_var_float(ncout_ID, idRAIN, wRAIN)); nc_error(nc_put_var_float(ncout_ID, idRAINC, wRAINC)); }
void write_MODEL_LEVEL () { fprintf(stdout, "Writing MODEL_LEVEL\n"); nc_error(nc_put_var_float(ncout_ID, idMODEL_LEVEL, wMODEL_LEVEL)); }
void write_RH () { fprintf(stdout, "Writing RH\n"); nc_error(nc_put_var_float(ncout_ID, idRH, wRH)); nc_error(nc_put_var_float(ncout_ID, idRH_A, wRH_A)); nc_error(nc_put_var_float(ncout_ID, idRH_P, wRH_P)); }
void write_TEMP2M () { fprintf(stdout, "Writing TEMP2M\n"); nc_error(nc_put_var_float(ncout_ID, idTEMP2M, wTEMP2M)); }
int main() { nc_init(); file = freopen("lib.log", "w", stderr); signal(SIGINT, finish); int mice = lmice_init(LMICE_SYSTEM_DEVFS); /* Set plugintype */ if (mice < 0) { finish(0); } fprintf(stdout, "Init lib found %d mice\n", mice); pthread_t thread; pthread_create(&thread, NULL, err_log, NULL); //wrefresh(shwin); lastres = lmice_result_new(); lmice_connect(LMICE_CB_NEW_DATA, cb, NULL); lmice_connect(LMICE_CB_OVERFLOW, cb, NULL); lmice_timer_set(1000000); char ch = nc_getc(); bool ok = true; LMiceResult *result = lmice_result_new(); while (ch != 'q' && ch != 'Q') { switch (ch) { case 'c': case 'C': fprintf(stdout, "config\n"); ok = lmice_config_set(lastres); break; case 's': case 'S': fprintf(stdout, "Writing configuration\n"); ok = lmice_config_save(NULL); break; case 'l': case 'L': fprintf(stdout, "Loading configuration\n"); ok = lmice_config_load(NULL); break; case 'r': case 'R': fprintf(stdout, "Read\n"); ok = lmice_read(result, false); if (!ok) nc_error("Read error \n"); nc_result(result); break; break; case 'x': case 'X': fprintf(stdout, "Clearing\n"); lmice_clear(); break; case 'h': case 'H': nc_show_help(); break; default: fprintf(stdout, "unknown command %c\n", ch); break; } if (!ok) fprintf(stdout, "an error happend....\n"); fprintf(stdout,"\n"); ch = nc_getc(); } lmice_result_delete(result); thok = 0; void *data = NULL;; pthread_join(thread, data); lmice_uninit(true); nc_uninit(); return 0; }