int main(int argc, char *argv[]) { NcFile at(atpath, NcFile::ReadOnly); if(!at.is_valid() || at.num_dims() != 3 || at.num_vars() != 4) { fprintf(stderr, "failed reading file: %s\n", atpath); return 1; } NcVar* data = at.get_var("rhum"); if(!data->is_valid() || data->num_dims() != 3) { fprintf(stderr, "rhum has incorrect dimensions"); return 1; } NcDim* time = data->get_dim(0); int timecnt = time->size(); float *rhumd = new float[timecnt*LATS*LONS]; data->get(rhumd, timecnt, LATS, LONS); float rhumdmon[12][LATS][LONS]; for(int i = 0; i<LATS; i++) for(int j = 0; j<LONS; j++) { float rhumdmoncnt[12]; for(int k = 0; k<12; k++) { rhumdmon[k][i][j] = 0; rhumdmoncnt[k] = 0; } for(int k = 0; k<timecnt; k++) { double v = rhumd[(k*LATS+i)*LONS+j]*.1 + 3276.5; if(v >= 0 && v <= 100) { rhumdmon[k%12][i][j] += v; rhumdmoncnt[k%12]++; } } for(int k = 0; k<12; k++) rhumdmon[k][i][j] /= rhumdmoncnt[k]; } delete [] rhumd; /* use a single byte instead of 2 to save memory, resolution of 1/5th of a mm/day resolution */ uint8_t rhumbyte[12][LATS][LONS]; for(int i = 0; i<12; i++) for(int j = 0; j<LATS; j++) for(int k = 0; k<LONS; k++) if(isnan(rhumdmon[i][j][k]) || fabs(rhumdmon[i][j][k]) > 100) rhumbyte[i][j][k] = 255; else rhumbyte[i][j][k] = rhumdmon[i][j][k]*2.0; fwrite(rhumbyte, sizeof rhumbyte, 1, stdout); return 0; }
const bool isValid( void ) const { return( m_var->is_valid() != 0 ); }
// // Creates NetCDF product // bool NetCDFProduct(MSG_header *PRO_head, MSG_data* PRO_data, int totalsegs, int *segsindexes, MSG_header *header, MSG_data *msgdat) { struct tm *tmtime; char NcName[1024]; char reftime[64]; char projname[16]; int wd, hg; int bpp; int ncal; float *cal; NcVar *ivar; NcVar *tvar; NcDim *tdim; NcDim *ldim; NcDim *cdim; NcDim *caldim; int npix = header[0].image_structure->number_of_columns; int nlin = header[0].image_structure->number_of_lines; size_t npixperseg = npix*nlin; size_t total_size = totalsegs*npixperseg; MSG_SAMPLE *pixels = new MSG_SAMPLE[total_size]; memset(pixels, 0, total_size*sizeof(MSG_SAMPLE)); size_t pos = 0; for (int i = 0; i < totalsegs; i ++) { if (segsindexes[i] >= 0) memcpy(pixels+pos, msgdat[segsindexes[i]].image->data, npixperseg*sizeof(MSG_SAMPLE)); pos += npixperseg; } nlin = nlin*totalsegs; // Manage subarea if (is_subarea) { if (AreaLinStart < 0 || AreaLinStart > nlin - AreaNlin || AreaNlin > nlin - AreaLinStart) { std::cerr << "Wrong Subarea in lines...." << std::endl; throw; } if (AreaPixStart < 0 || AreaPixStart > npix - AreaNpix || AreaNpix > npix - AreaPixStart) { std::cerr << "Wrong Subarea in Pixels...." << std::endl; throw; } size_t newsize = AreaNpix * AreaNlin; MSG_SAMPLE *newpix = new MSG_SAMPLE[newsize]; memset(newpix, 0, newsize*sizeof(MSG_SAMPLE)); for (int i = 0; i < AreaNlin; i ++) memcpy(newpix + i * AreaNpix, pixels + (AreaLinStart + i) * npix + AreaPixStart, AreaNpix * sizeof(MSG_SAMPLE)); delete [ ] pixels; pixels = newpix; total_size = newsize; } else { AreaNpix = npix; AreaNlin = nlin; } tmtime = PRO_data->prologue->image_acquisition.PlannedAquisitionTime.TrueRepeatCycleStart.get_timestruct( ); t_enum_MSG_spacecraft spc = header[0].segment_id->spacecraft_id; uint_1 chn = header[0].segment_id->spectral_channel_id; float sublon = header[0].image_navigation->subsatellite_longitude; int cfac = header[0].image_navigation->column_scaling_factor; int lfac = header[0].image_navigation->line_scaling_factor; int coff = header[0].image_navigation->column_offset; int loff = header[0].image_navigation->line_offset; float sh = header[0].image_navigation->satellite_h; char *channelstring = strdup(MSG_channel_name(spc, chn).c_str( )); char *channel = chname(channelstring, strlen(channelstring) + 1); // Build up output NetCDF file name and open it sprintf( NcName, "%s_%4d%02d%02d_%02d%02d.nc", channel, tmtime->tm_year + 1900, tmtime->tm_mon + 1, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min ); NcFile ncf ( NcName , NcFile::Replace ); if (! ncf.is_valid()) return false; // Fill arrays on creation ncf.set_fill(NcFile::Fill); // Add Global Attributes if (! ncf.add_att("Satellite", MSG_spacecraft_name(spc).c_str())) return false; sprintf(reftime, "%04d-%02d-%02d %02d:%02d:00 UTC", tmtime->tm_year + 1900, tmtime->tm_mon + 1, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min); if (! ncf.add_att("Antenna", "Fixed") ) return false; if (! ncf.add_att("Receiver", "HIMET") ) return false; if (! ncf.add_att("Time", reftime) ) return false; if (! ncf.add_att("Area_Name", "SpaceView" ) ) return false; sprintf(projname, "GEOS(%3.1f)", sublon); if (! ncf.add_att("Projection", projname) ) return false; if (! ncf.add_att("Columns", AreaNpix ) ) return false; if (! ncf.add_att("Lines", AreaNlin ) ) return false; if (! ncf.add_att("SampleX", 1.0 ) ) return false; if (! ncf.add_att("SampleY", 1.0 ) ) return false; if (! ncf.add_att("AreaStartPix", AreaPixStart ) ) return false; if (! ncf.add_att("AreaStartLin", AreaLinStart ) ) return false; if (! ncf.add_att("Column_Scale_Factor", cfac) ) return false; if (! ncf.add_att("Line_Scale_Factor", lfac) ) return false; if (! ncf.add_att("Column_Offset", coff) ) return false; if (! ncf.add_att("Line_Offset", loff) ) return false; if (! ncf.add_att("Orbit_Radius", sh) ) return false; if (! ncf.add_att("Longitude", sublon) ) return false; if (! ncf.add_att("NortPolar", 1) ) return false; if (! ncf.add_att("NorthSouth", 1) ) return false; if (! ncf.add_att("title", TITLE) ) return false; if (! ncf.add_att("Institution", INSTITUTION) ) return false; if (! ncf.add_att("Type", TYPE) ) return false; if (! ncf.add_att("Version", HIMET_VERSION) ) return false; if (! ncf.add_att("Conventions", "COARDS") ) return false; if (! ncf.add_att("history", "Created from raw data") ) return false; // Dimensions wd = AreaNpix; hg = AreaNlin; bpp = header[0].image_structure->number_of_bits_per_pixel; ncal = (int) pow(2.0, bpp); tdim = ncf.add_dim("time"); if (!tdim->is_valid()) return false; ldim = ncf.add_dim("line", hg); if (!ldim->is_valid()) return false; cdim = ncf.add_dim("column", wd); if (!cdim->is_valid()) return false; caldim = ncf.add_dim("calibration", ncal); if (!caldim->is_valid()) return false; // Get calibration values cal = PRO_data->prologue->radiometric_proc.get_calibration((int) chn, bpp); // Add Calibration values NcVar *cvar = ncf.add_var("calibration", ncFloat, caldim); if (!cvar->is_valid()) return false; cvar->add_att("long_name", "Calibration coefficients"); cvar->add_att("variable", channel); if (chn > 3 && chn < 12) cvar->add_att("units", "K"); else cvar->add_att("units", "mW m^-2 sr^-1 (cm^-1)^-1"); if (!cvar->put(cal, ncal)) return false; tvar = ncf.add_var("time", ncDouble, tdim); if (!tvar->is_valid()) return false; tvar->add_att("long_name", "Time"); tvar->add_att("units", "seconds since 2000-01-01 00:00:00 UTC"); double atime; time_t ttime; extern long timezone; ttime = mktime(tmtime); atime = ttime - 946684800 - timezone; if (!tvar->put(&atime, 1)) return false; ivar = ncf.add_var(channel, ncShort, tdim, ldim, cdim); if (!ivar->is_valid()) return false; if (!ivar->add_att("add_offset", 0.0)) return false; if (!ivar->add_att("scale_factor", 1.0)) return false; if (!ivar->add_att("chnum", chn)) return false; // Write output values if (!ivar->put((const short int *) pixels, 1, hg, wd)) return false; // Close NetCDF output (void) ncf.close( ); delete [ ] pixels; delete [ ] cal; return( true ); }