Hi,
As someone else suggested, the NetCDF operators are very useful. Another useful
way of modifying files is like this:
ncdump file.nc | awkscript.awk | ncgen -o newfile.nc
Where awkscript.awk manipulates the file in the way you want. To illustrate the
point, I am attaching a little script I have been writing to make non-CF
compliant files CF compliant. Maybe this will give you some ideas about how you
can solve your problem?
Cheers,
Tim Hume
Centre for Australian Weather and Climate Research
Melbourne
Australia
---- Example script illustrating use of ncdump, awk and ncgen ----
ncdump "${ncfile}" | awk -F= -v var_name="${var_name}" \
'BEGIN{
nlons = 0;
nlats = 0;
nprogs = 0;
}
/^dimensions:/{
print $0;
printf("\tbounds = 2 ;\n");
prev_line = "dimensions";
next;
}
/^\/\/ global attributes:/{
if ((var_name == "accum_precip")||(var_name ==
"temperature_2m")||(var_name == "dewpoint_2m")){
printf("\tdouble level ;\n");
printf("\t\tlevel:long_name = \"Height above the
surface\" ;\n");
printf("\t\tlevel:units = \"m\" ;\n");
}
print $0;
next;
}
/^data:/{
print $0;
if (var_name == "accum_precip"){
printf("\n level = 0. ;\n");
} else if ((var_name == "temperature_2m")||(var_name ==
"dewpoint_2m")){
printf("\n level = 2. ;\n");
}
next;
}
/^[[:space:]]*longitude:/{
print $0;
prev_line = "longitude_var";
next;
}
/^[[:space:]]*latitude:/{
print $0;
prev_line = "latitude_var";
next;
}
/^[[:space:]]*forecast:/{
print $0;
prev_line = "forecast_var";
next;
}
/^ longitude[[:space:]]+=/,/;$/{
print $0;
n = split($NF,lons,/[,;]/);
for (ii=1; ii<n; ++ii){
lon_vals[ii+nlons] = lons[ii];
}
nlons += (n-1);
prev_line = "longitude_data";
next;
}
/^ latitude[[:space:]]+=/,/;$/{
print $0;
n = split($NF,lats,/[,;]/);
for (ii=1; ii<n; ++ii){
lat_vals[ii+nlats] = lats[ii];
}
nlats += (n-1);
prev_line = "latitude_data";
next;
}
/^ forecast[[:space:]]+=/,/;$/{
print $0;
n = split($NF,progs,/[,;]/);
for (ii=1; ii<n; ++ii){
prog_vals[ii+nprogs] = progs[ii];
}
nprogs += (n-1);
prev_line = "forecast_data";
next;
}
{
if (prev_line == "longitude_var"){
printf("\tdouble longitude_bounds(longitude, bounds)
;\n");
printf("\t\tlongitude_bounds:long_name = \"grid cell
longitude boundaries\" ;\n");
printf("\t\tlongitude_bounds:units = \"degrees_east\"
;\n");
printf("\t\tlongitude_bounds:valid_min = -360.d ;\n");
printf("\t\tlongitude_bounds:valid_max = 360.d ;\n");
}
if (prev_line == "latitude_var"){
printf("\tdouble latitude_bounds(latitude, bounds)
;\n");
printf("\t\tlatitude_bounds:long_name = \"grid cell
latitude boundaries\" ;\n");
printf("\t\tlatitude_bounds:units = \"degrees_north\"
;\n");
printf("\t\tlatitude_bounds:valid_min = -90.d ;\n");
printf("\t\tlatitude_bounds:valid_max = 90.d ;\n");
}
if (prev_line == "forecast_var"){
printf("\tdouble forecast_bounds(forecast, bounds)
;\n");
printf("\t\tforecast_bounds:long_name = \"forecast
interval\" ;\n");
printf("\t\tforecast_bounds:units = \"hours\" ;\n");
}
if (prev_line == "longitude_data"){
printf("\n");
printf(" longitude_bounds = ");
lon1 = lon_vals[1] - (lon_vals[2] - lon_vals[1])/2;
lon2 = lon_vals[1] + (lon_vals[2] - lon_vals[1])/2;
printf("%f, %f, ",lon1, lon2);
for (ii=2; ii<nlons; ++ii){
lon1 = lon_vals[ii] - (lon_vals[ii] -
lon_vals[ii-1])/2;
lon2 = lon_vals[ii] + (lon_vals[ii+1] -
lon_vals[ii])/2;
printf("%f, %f, ",lon1, lon2);
}
lon1 = lon_vals[nlons] - (lon_vals[nlons] -
lon_vals[nlons-1])/2;
lon2 = lon_vals[nlons] + (lon_vals[nlons] -
lon_vals[nlons-1])/2;
printf("%f, %f ;\n",lon1, lon2);
}
if (prev_line == "latitude_data"){
printf("\n");
printf(" latitude_bounds = ");
lat1 = lat_vals[1] - (lat_vals[2] - lat_vals[1])/2;
lat2 = lat_vals[1] + (lat_vals[2] - lat_vals[1])/2;
lat1 = (lat1 < -90) ? -90 : lat1;
lat1 = (lat1 > 90) ? 90 : lat1;
lat2 = (lat2 < -90) ? -90 : lat2;
lat2 = (lat2 > 90) ? 90 : lat2;
printf("%f, %f, ",lat1, lat2);
for (ii=2; ii<nlats; ++ii){
lat1 = lat_vals[ii] - (lat_vals[ii] -
lat_vals[ii-1])/2;
lat2 = lat_vals[ii] + (lat_vals[ii+1] -
lat_vals[ii])/2;
lat1 = (lat1 < -90) ? -90 : lat1;
lat1 = (lat1 > 90) ? 90 : lat1;
lat2 = (lat2 < -90) ? -90 : lat2;
lat2 = (lat2 > 90) ? 90 : lat2;
printf("%f, %f, ",lat1, lat2);
}
lat1 = lat_vals[nlats] - (lat_vals[nlats] -
lat_vals[nlats-1])/2;
lat2 = lat_vals[nlats] + (lat_vals[nlats] -
lat_vals[nlats-1])/2;
lat1 = (lat1 < -90) ? -90 : lat1;
lat1 = (lat1 > 90) ? 90 : lat1;
lat2 = (lat2 < -90) ? -90 : lat2;
lat2 = (lat2 > 90) ? 90 : lat2;
printf("%f, %f ;\n",lat1, lat2);
}
if (prev_line == "forecast_data"){
printf("\n");
printf(" forecast_bounds = ");
for (ii=1; ii<nprogs; ++ii){
if (var_name == "accum_precip"){
prog1 = 0;
prog2 = prog_vals[ii];
} else {
prog1 = prog_vals[ii];
prog2 = prog_vals[ii];
}
printf("%f, %f, ",prog1, prog2);
}
if (var_name == "accum_precip"){
prog1 = 0;
prog2 = prog_vals[nprogs];
} else {
prog1 = prog_vals[nprogs];
prog2 = prog_vals[nprogs];
}
printf("%f, %f ;\n",prog1, prog2);
}
print $0;
prev_line = "other";
}' | fmt -s -w 132 | ncgen -o "${ncfile}.new"
#
# Add some extra attributes etc.
#
ncatted -O -h -a bounds,latitude,o,c,"latitude_bounds" \
-a valid_min,latitude,o,d,-90. \
-a valid_max,latitude,o,d,90. \
-a bounds,longitude,o,c,"longitude_bounds" \
-a valid_min,longitude,o,d,-360. \
-a valid_max,longitude,o,d,360. \
-a bounds,forecast,o,c,"forecast_bounds" \
-a calendar,basetime,o,c,"gregorian" \
-a Conventions,global,o,c,"CF-1.3" \
-a history,global,o,c,"File created by the Gridded OCF data ingest
system" \
-a History,global,d,, \
-a institution,global,o,c,"Australian Bureau of Meteorology" \
-a source,global,o,c,"model" \
-a standard_name,${var_name},o,c,"${std_name}" \
-a title,global,o,c,"${model_str} forecasts of ${var_name}; resolution:
${res_str}; source:${source}" \
-a nco_input_file_number,global,d,, \
-a nco_input_file_list,global,d,, \
"${ncfile}.new"
-----Original Message-----
From: netcdfgroup-bounces@xxxxxxxxxxxxxxxx
[mailto:netcdfgroup-bounces@xxxxxxxxxxxxxxxx] On Behalf Of Blanquita Oviedo
Sent: Wednesday, 26 November 2008 02:56
To: netcdfgroup@xxxxxxxxxxxxxxxx
Subject: [netcdfgroup] Modifying NEtcdf file
Hi,
I am new using netCDF and I don't know anybody in my country, Colombia,
that is currently working with netCDF files.
In order to make the investigation for my master, I need to modify an a
extensive netCDF file (105.000K). I need to modify aprox 120.000 lines of data,
but I don´t know any method to do it.
I try to convert the source netCDF file into text (using ncdump - o), then I
modified it and now I have an ASCII file separated by commas and with
characters in return in each line, as obtained using the command ncdump of the
original file.
For example:
temp =
// temp(0,0,0, 0-54)
277.4813, 276.4155, 275.3335, 274.2372, 273.4386, 273.0315, 272.6246,
272.2128, 271.7205, 271.1262, 270.5313, 269.9269, 269.3189, 268.7285,
268.1587, 267.6162, 267.5617, 268.1147, 268.6765, 269.2408, 269.4946,
269.3569, 269.1975, 269.0216, 268.8656, 268.7551, 268.6632, 268.5788,
269.1266, 270.4707, 271.8028, 273.1227, 273.7098, 273.3803, 273.0515,
272.7239, 272.7801, 273.3181, 273.8549, 274.3895, 274.682, 274.6729,
274.6642, 274.6566, 274.9037, 275.468, 276.0289, 276.5884, 276.3576,
275.124, 273.8793, 272.6223, 271.6534, 271.0549, 270.4564,
// temp(0,0,1, 0-54)
276.8679, 275.8403, 274.7976, 273.7424, 273.0192, 272.7307, 272.4441,
272.1526, 271.7513, 271.2129, 270.6729, 270.1262, 269.5375, 268.9077,
268.2888, 267.6906, 267.6292, 268.238, 268.859, 269.4889, 269.8028,
269.7146, 269.6006, 269.4674, 269.2771, 269.037, 268.8181, 268.608,
269.0854, 270.4286, 271.7578, 273.0732, 273.6958, 273.4539, 273.2136,
272.9739, 272.9522, 273.2037, 273.4539, 273.7034, 273.8842, 273.9784,
274.0724, 274.1676, 274.4979, 275.1201, 275.7378, 276.3535, 276.2311,
275.1737, 274.1059, 273.0276, 272.2213, 271.7681, 271.3176,
I have problems to return it to turn to netCDF format because the command ncgen
does not work to me. I would like to ask you your support in order to advance
and to obtain a netCDF modified file.
Thanks for the support that you can provide to me.
Blanca Elvira Oviedo T.
_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit:
http://www.unidata.ucar.edu/mailing_lists/