/* *---------------------------------------------------------------------- * Begin file /home/sting/foster/uars/hrdi/C/rdcdf.c *---------------------------------------------------------------------- */ #include #include "netcdf.h" #include "timesgcm.h" char* cdfname; int cdfid = -1; static char *fieldlab_short[] = { "TN", "UN", "VN", "O2", "OX", "N4S","NOZ","CO", "CO2","H2O", "H2", "HOX","O+", "CH4","O21D","NO2","NO", "O3", "O1", "OH", "HO2","H", "N2D","TI", "TE", "NE", "O2+","W", "Z", "N2", NULL}; float pnt[3][JMX][KMX][IMX]; /* gcm un, vn, ht */ float gcmlat[JMX]; float gcmlon[IMX]; float gcmzp[KMX]; Gcmhdr_struct *tgcmhdr; /* *************************************************************** * Read from netCDF file (has already been opened with cdfid): *************************************************************** */ int rdcdf(cdfid) int cdfid; { int i, ii,iii; int latdim,londim,izpdim; /* dimension id's */ int latid,lonid,izpid; /* dimension var id's */ int fieldids[NFCDF+1]; /* field var id's */ static int idim3[3] = {JMX, KMX, IMX}; static int istart3[3] = {0, 0, 0}; static int start[] = {0}; static int latcount[] = {JMX}; static int loncount[] = {IMX}; static int zpcount[] = {KMX}; nc_type hdrfield_type; int hdrfield_len; /* * Get dimension id's: */ latdim = ncdimid(cdfid, "lat"); londim = ncdimid(cdfid, "lon"); izpdim = ncdimid(cdfid, "izp"); if (latdim < 0 || londim < 0 || izpdim < 0) { fprintf(stderr,"rdcdf: error getting dimension id's\n"); return(-1); } /* * Get dimension variable id's: */ latid = ncvarid(cdfid, "lat"); lonid = ncvarid(cdfid, "lon"); izpid = ncvarid(cdfid, "zp"); if (latid < 0 || lonid < 0 || izpid < 0) { fprintf(stderr,"rdcdf: error getting dimension var id's\n"); return(-1); } /* * Get values of dimension variables (defines tigcm grid): */ i = ncvarget(cdfid, latid, start, latcount, (void *)gcmlat); i = ncvarget(cdfid, lonid, start, loncount, (void *)gcmlon); i = ncvarget(cdfid, izpid, start, zpcount, (void *)gcmzp); if (i < 0) { fprintf(stderr,"rdcdf: error getting dimension variables\n"); return(-1); } /* * Get global attributes and put them in gcmhdr structure: * (most of these were from the original tigcm history header) */ tgcmhdr = (Gcmhdr_struct *)malloc(sizeof(Gcmhdr_struct)); /* * History volume name global attribute: * (ncattinq returns type and length for malloc) */ ncattinq(cdfid, NC_GLOBAL, "histvol", &hdrfield_type, &hdrfield_len); tgcmhdr->histvol = (char *) malloc(hdrfield_len * nctypelen(hdrfield_type)); if ((i = ncattget(cdfid, NC_GLOBAL, "histvol", (void *)tgcmhdr->histvol)) < 0) { fprintf(stderr,">>> rdcdf warning: error getting histvol\n"); } /* * Label global attribute: */ ncattinq(cdfid, NC_GLOBAL, "label", &hdrfield_type, &hdrfield_len); tgcmhdr->label = (char *) malloc(hdrfield_len * nctypelen(hdrfield_type)); if ((i = ncattget(cdfid, NC_GLOBAL, "label", (void *)tgcmhdr->label)) < 0) { fprintf(stderr,">>> rdcdf warning: error getting hdr label\n"); } /* * Model day,hour,minute global attribute: * (ut = model hour + model minute / 60) */ if ((i = ncattget(cdfid, NC_GLOBAL, "md:mh:mm", (void *)tgcmhdr->mdmhmm)) < 0) { fprintf(stderr,">>> rdcdf warning: error getting hdr model time\n"); } tgcmhdr->ut = tgcmhdr->mdmhmm[1] + tgcmhdr->mdmhmm[2] / 60.; /* * Hp,cp,by global attribute: */ if ((i = ncattget(cdfid, NC_GLOBAL, "hp,cp,by", (void *)tgcmhdr->hpcpby)) < 0) { fprintf(stderr,">>> rdcdf warning: error getting hdr hp,cp,by\n"); } /* * Get field var id's: * 8/92: For this code (uars/hrdi) we need only neutral winds and heights: */ ii = -1; for (i=0; i < NFCDF; i++) { if ((i >= 1 && i <= 2) || i == 28) { /* get un,vn, and ht only */ ii++; if ((fieldids[i] = ncvarid(cdfid, fieldlab_short[i])) < 0) { fprintf(stderr,"Error getting field id's: i=%d\n",i); return(-1); } else { /* * Get long field name (variable attribute): ncattinq(cdfid, fieldids[i], "long_name", &hdrfield_type, &hdrfield_len); fieldlab[i] = (char*)malloc(hdrfield_len * nctypelen(hdrfield_type)); if ((iii = ncattget(cdfid, fieldids[i], "long_name", (void*)fieldlab[i])) < 0) { fprintf(stderr, ">>> rdcdf warning: error getting long field name\n"); fprintf(stderr," i = %d fieldlab=%s\n",i,fieldlab[i]); } */ /* * Get var values (defines pnt): */ if ((iii = ncvarget(cdfid, fieldids[i], istart3, idim3, &pnt[ii][0][0][0])) < 0) { fprintf(stderr,"Error getting pnt at i=%d\n",i); return(-1); } } } } return(0); }