#include #include #include #include #include #include "netcdf.h" #define START_YEAR (1980) #define END_YEAR (1994) #define MAX_STORE_YEARS (END_YEAR - START_YEAR + 1) #define NROWS (360) #define NCOLS (720) #define MISSING_VALUE (-9999.) #define GRIDSIZE (NROWS * NCOLS) /*#define SR_UNITS "Tg C mo-1" */ /*#define PATH "/disk10/RAICH/monthly/" */ /*#define SR_FILE_NAME "srtotal" */ /*#define OUT_FILE "/disk10/RAICH/sr_total_0.5degree.nc" */ #define SR_UNITS "g C m-2 d-1" #define PATH "/disk10/RAICH/daily/" #define SR_FILE_NAME "srdaily" #define OUT_FILE "/disk10/RAICH/sr_daily_0.5degree.nc" void check_err(const int stat, const int line, const char *file) { if (stat != NC_NOERR) { (void) fprintf(stderr, "line %d of %s: %s\n", line, file, nc_strerror(stat)); exit(1); } } void main () { static size_t start[3]; static size_t count[3]; time_t current_time; int stat, i; int sr_ncid; int year, month; int lat_dim, lon_dim, time_dim, var_dim[3]; int sr_id, lat_id, lon_id, time_id; float fill_val; char header [25]; char description[80]; char sr_file_name [100]; int time_data[MAX_STORE_YEARS * 12] ; float lat_lon[720]; float *sr; FILE *sr_file; void check_err( const int, const int, const char *); /* enter define mode */ stat = nc_create(OUT_FILE, NC_CLOBBER, &sr_ncid); check_err(stat,__LINE__,__FILE__); /* define dimensions */ stat = nc_def_dim(sr_ncid, "time", 12 * MAX_STORE_YEARS , &time_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(sr_ncid, "lat", NROWS, &lat_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(sr_ncid, "lon", NCOLS, &lon_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ var_dim[0] = time_dim; var_dim[1] = lat_dim; var_dim[2] = lon_dim; stat = nc_def_var(sr_ncid, "time", NC_FLOAT, 1, &time_dim, &time_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(sr_ncid, "lat", NC_FLOAT, 1, &lat_dim, &lat_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(sr_ncid, "lon", NC_FLOAT, 1, &lon_dim, &lon_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(sr_ncid, "soil_respiration", NC_FLOAT, 3, var_dim, &sr_id); check_err(stat,__LINE__,__FILE__); /* assign attributes */ stat = nc_put_att_text(sr_ncid, lat_id, "long_name", 8, "Latitude"); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(sr_ncid, lat_id, "units", 13, "degrees_north"); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(sr_ncid, lon_id, "long_name", 9, "Longitude"); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(sr_ncid, lon_id, "units", 12, "degrees_east"); check_err(stat,__LINE__,__FILE__); sprintf(description, "days since %4d-01-01 00:00:00", START_YEAR); stat = nc_put_att_text(sr_ncid, time_id, "units", strlen(description), description); check_err(stat,__LINE__,__FILE__); fill_val = (float)MISSING_VALUE; stat = nc_put_att_float(sr_ncid, sr_id, "missing_value", NC_FLOAT, 1, &fill_val ); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(sr_ncid, sr_id, "long_name",16, "Soil Respiration"); check_err(stat,__LINE__,__FILE__); sprintf(description, "%s", SR_UNITS); stat = nc_put_att_text(sr_ncid, sr_id, "units", strlen(description), description); check_err(stat,__LINE__,__FILE__); current_time = time(NULL); strcpy(description,ctime(¤t_time)); strlen(description); description[strlen(description)-1] = '\0'; stat = nc_put_att_text(sr_ncid, NC_GLOBAL, "Created", strlen(description)+1, description ); stat = nc_put_att_text(sr_ncid, NC_GLOBAL, "Source", 138, "Raich, J.W., C.S. Potter, D. Bhagawati 2002. Interannual variability in global soil respiration, 1980-94. Global Change Biology 8:800-812."); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(sr_ncid, NC_GLOBAL, "Conventions", 11, "COARDS, GDV"); check_err(stat,__LINE__,__FILE__); /* leave define mode */ stat = nc_enddef (sr_ncid); check_err(stat,__LINE__,__FILE__); lat_lon[0] = 90.; for ( i = 1; i < 360; i++ ) { lat_lon[i] = lat_lon[i-1] - 0.5; fprintf(stderr,"lat = %d %f\n", i, lat_lon[i]); } stat = nc_put_var_float(sr_ncid, lat_id, lat_lon); check_err(stat,__LINE__,__FILE__); lat_lon[0] = -180.; for ( i = 1; i < 720; i++ ) { lat_lon[i] = lat_lon[i-1] + 0.5; fprintf(stderr,"lon = %d %f\n", i, lat_lon[i]); } stat = nc_put_var_float(sr_ncid, lon_id, lat_lon); check_err(stat,__LINE__,__FILE__); for ( i = 0; i < 12 * MAX_STORE_YEARS; i++ ) time_data[i] = (float)i*(365.0/12.0)+15.0; stat = nc_put_var_int(sr_ncid, time_id, time_data); check_err(stat,__LINE__,__FILE__); /* allocate memory for sr values */ sr = (float *)malloc(NROWS * NCOLS * sizeof(float)); count[0] = 1; start[1] = 0; count[1] = NROWS; start[2] = 0; count[2] = NCOLS; for( year = START_YEAR; year <= END_YEAR; year++) { for (month = 1; month <= 12; month++) { if(month < 10) sprintf(sr_file_name, "%s%d%s0%db.dat", PATH, year-1900, SR_FILE_NAME, month); else sprintf(sr_file_name, "%s%d%s%db.dat", PATH, year-1900, SR_FILE_NAME, month); fprintf(stderr, "reading file %s\n", sr_file_name); if (!(sr_file = fopen(sr_file_name, "r"))) { fprintf(stderr, "\nfailure opening soil respiration file\n"); exit(1); } /* read 6 header lines */ for (i = 1; i <= 6; i++) fgets(header, 20, sr_file); for (i = 0; i < GRIDSIZE; i++) fscanf(sr_file, "%f", &sr[i]); fclose (sr_file); /* store soil respiration values */ start[0] = (year - START_YEAR) * 12 + (month - 1); stat = nc_put_vara_float(sr_ncid, sr_id, start, count, (void *)sr); check_err(stat,__LINE__,__FILE__); } } /* close nc file before leaving */ stat = nc_close(sr_ncid); check_err(stat,__LINE__,__FILE__); free (sr); }