[GRASS-SVN] r62212 - grass/trunk/raster/r.in.bin

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 8 02:13:41 PDT 2014


Author: mmetz
Date: 2014-10-08 02:13:40 -0700 (Wed, 08 Oct 2014)
New Revision: 62212

Modified:
   grass/trunk/raster/r.in.bin/main.c
   grass/trunk/raster/r.in.bin/r.in.bin.html
Log:
r.in.bin: +header +bands

Modified: grass/trunk/raster/r.in.bin/main.c
===================================================================
--- grass/trunk/raster/r.in.bin/main.c	2014-10-08 00:22:20 UTC (rev 62211)
+++ grass/trunk/raster/r.in.bin/main.c	2014-10-08 09:13:40 UTC (rev 62212)
@@ -1,17 +1,23 @@
-/*
- *   r.in.bin
+
+/****************************************************************************
  *
- *   Copyright (C) 2000 by the GRASS Development Team
- *   Author: Bob Covill <bcovill tekmap.ns.ca>
+ * MODULE:       r.in.bin
+ * AUTHOR(S):    Jacques Bouchard, France (bouchard at onera.fr)
+ *               Bob Covill <bcovill tekmap.ns.ca>
+ *               Markus Metz
+ * PURPOSE:      Import binary files
+ * COPYRIGHT:    (C) 2000 - 2014 by the GRASS Development Team
  *
- *   This program is free software under the GPL (>=v2)
- *   Read the file COPYING coming with GRASS for details.
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the file COPYING that comes with GRASS
+ *               for details.
  *
- */
+ *****************************************************************************/
 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <math.h>
 #include <sys/stat.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
@@ -197,6 +203,8 @@
 	struct Option *output;
 	struct Option *null;
 	struct Option *bytes;
+	struct Option *hbytes;
+	struct Option *bands;
 	struct Option *order;
 	struct Option *title;
 	struct Option *north;
@@ -217,12 +225,14 @@
     } flag;
     char *desc = NULL;
     const char *input;
-    const char *output;
+    const char *outpre;
+    char output[GNAME_MAX];
     const char *title;
     double null_val = 0;
     int is_fp;
     int is_signed;
-    int bytes;
+    int bytes, hbytes;
+    int band, nbands, bsize;
     int order;
     int swap_flag;
     int i, flip;
@@ -234,7 +244,7 @@
     RASTER_MAP_TYPE map_type;
     int fd;
     FILE *fp;
-    off_t file_size;
+    off_t file_size, band_off;
     struct GRD_HEADER header;
     int row;
     struct History history;
@@ -280,6 +290,7 @@
     parm.input->gisprompt = "old,bin,file";
 
     parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
+    parm.output->description = _("Output name or prefix if several bands are imported");
 
     parm.title = G_define_option();
     parm.title->key = "title";
@@ -296,6 +307,23 @@
     parm.bytes->description = _("Number of bytes per cell");
     parm.bytes->guisection = _("Settings");
 
+    parm.hbytes = G_define_option();
+    parm.hbytes->key = "header";
+    parm.hbytes->type = TYPE_INTEGER;
+    parm.hbytes->required = NO;
+    parm.hbytes->answer = "0";
+    parm.hbytes->description = _("Header size in bytes");
+    parm.hbytes->guisection = _("Settings");
+
+    parm.bands = G_define_option();
+    parm.bands->key = "bands";
+    parm.bands->type = TYPE_INTEGER;
+    parm.bands->required = NO;
+    parm.bands->answer = "1";
+    parm.bands->label = _("Number of bands in input file");
+    parm.bands->description = _("Bands must be in band-sequential order");
+    parm.bands->guisection = _("Settings");
+
     parm.order = G_define_option();
     parm.order->key = "order";
     parm.order->type = TYPE_STRING;
@@ -376,9 +404,16 @@
 	exit(EXIT_FAILURE);
 
     input = parm.input->answer;
-    output = parm.output->answer;
+    outpre = parm.output->answer;
     title = parm.title->answer;
 
+    nbands = atoi(parm.bands->answer);
+    if (nbands < 1)
+	G_fatal_error(_("Option %s must be > 0"), parm.bands->key);
+    hbytes = atoi(parm.hbytes->answer);
+    if (hbytes < 0)
+	G_fatal_error(_("Option %s must be >= 0"), parm.hbytes->key);
+
     if (G_strcasecmp(parm.order->answer, "big") == 0)
 	order = 0;
     else if (G_strcasecmp(parm.order->answer, "little") == 0)
@@ -398,6 +433,12 @@
     if (flag.gmt_hd->answer && parm.flip->answer)
 	G_fatal_error(_("-%c and %s= are mutually exclusive"),
 		      flag.gmt_hd->key, parm.flip->key);
+    if (flag.gmt_hd->answer && hbytes > 0)
+	G_warning(_("Option %s= is ignored if -%c is set"),
+		    parm.hbytes->key, flag.gmt_hd->key);
+    if (flag.gmt_hd->answer && nbands > 1)
+	G_warning(_("Option %s= is ignored if -%c is set"),
+		    parm.bands->key, flag.gmt_hd->key);
 
     swap_flag = order == (G_is_little_endian() ? 0 : 1);
 
@@ -502,6 +543,8 @@
     if (flag.gmt_hd->answer) {
 	read_gmt_header(&header, swap_flag, fp);
 	get_gmt_header(&header, &cellhd);
+	hbytes = 892;
+	nbands = 1;
     }
 
     /* Adjust Cell Header to New Values */
@@ -528,9 +571,7 @@
 	G_fatal_error("cols changed from %d to %d",
 		      grass_ncols, Rast_window_cols());
 
-    expected = (off_t) ncols * nrows * bytes;
-    if (flag.gmt_hd->answer)
-	expected += 892;
+    expected = (off_t) ncols * nrows * bytes * nbands + hbytes;
 
     if (file_size != expected) {
 	G_warning(_("File Size %"PRI_OFF_T" ... Total Bytes %"PRI_OFF_T),
@@ -546,38 +587,55 @@
     in_buf = G_malloc(ncols * bytes);
     out_buf = Rast_allocate_d_buf();
 
-    fd = Rast_open_new(output, map_type);
+    bsize = log10(nbands) + 1;
+    if (!flag.gmt_hd->answer && hbytes > 0)
+	G_fseek(fp, hbytes, SEEK_SET);
 
-    for (row = 0; row < grass_nrows; row++) {
-	G_percent(row, nrows, 2);
-
-	if (flip & FLIP_V) {
-	    G_fseek(fp, (off_t) (grass_nrows - row - 1) * ncols * bytes,
-	            SEEK_SET);
+    for (band = 1; band <= nbands; band++) {
+	
+	if (nbands > 1) {
+	    G_message(_("Importing band %d..."), band);
+	    sprintf(output, "%s%0*d", outpre, bsize, band);
 	}
+	else
+	    sprintf(output, "%s", outpre);
 
-	if (fread(in_buf, bytes, ncols, fp) != ncols)
-	    G_fatal_error(_("Error reading data"));
+	fd = Rast_open_new(output, map_type);
+	
+	band_off = (off_t)nrows * ncols * bytes * (band - 1) + hbytes;
 
-	convert_row(out_buf, in_buf, ncols, is_fp, is_signed,
-		    bytes, swap_flag, null_val, flip);
+	for (row = 0; row < grass_nrows; row++) {
+	    G_percent(row, nrows, 2);
 
-	Rast_put_d_row(fd, out_buf);
-    }
+	    if (flip & FLIP_V) {
+		G_fseek(fp, (off_t) (grass_nrows - row - 1) * ncols * bytes + band_off,
+			SEEK_SET);
+	    }
 
-    G_percent(row, nrows, 2);	/* finish it off */
+	    if (fread(in_buf, bytes, ncols, fp) != ncols)
+		G_fatal_error(_("Error reading data"));
 
-    Rast_close(fd);
-    fclose(fp);
+	    convert_row(out_buf, in_buf, ncols, is_fp, is_signed,
+			bytes, swap_flag, null_val, flip);
 
-    G_debug(1, "Creating support files for %s", output);
+	    Rast_put_d_row(fd, out_buf);
+	}
 
-    if (title)
-	Rast_put_cell_title(output, title);
+	G_percent(row, nrows, 2);	/* finish it off */
 
-    Rast_short_history(output, "raster", &history);
-    Rast_command_history(&history);
-    Rast_write_history(output, &history);
+	Rast_close(fd);
 
+	G_debug(1, "Creating support files for %s", output);
+
+	if (title)
+	    Rast_put_cell_title(output, title);
+
+	Rast_short_history(output, "raster", &history);
+	Rast_command_history(&history);
+	Rast_write_history(output, &history);
+    }
+
+    fclose(fp);
+
     return EXIT_SUCCESS;
 }

Modified: grass/trunk/raster/r.in.bin/r.in.bin.html
===================================================================
--- grass/trunk/raster/r.in.bin/r.in.bin.html	2014-10-08 00:22:20 UTC (rev 62211)
+++ grass/trunk/raster/r.in.bin/r.in.bin.html	2014-10-08 09:13:40 UTC (rev 62212)
@@ -95,7 +95,28 @@
 r.colors e020n40_topex rules=etopo2
 </pre></div>
 
+<h3>GPCP</h3>
+The following is a sample call of <em>r.in.bin</em> to import GPCP 1DD v1.2 data:
+<p> 
+<div class="code"><pre>
+YEAR="2000"
+MONTH="01"
+# number of days of this month
+MDAYS=`date -d"${YEAR}-${MONTH}-01 + 1 month - 1 day" +%d`
+r.in.bin in=gpcp_1dd_v1.2_p1d.${YEAR}${MONTH} out=gpcp_${YEAR}.${MONTH}. \
+         order=big bytes=4 -f header=1440 anull=-99999 \
+         n=90 s=-90 w=0 e=360 rows=180 cols=360 bands=$MDAYS
+</pre></div>
+<p>
+The following is a sample call of <em>r.in.bin</em> to import GPCP v2.2 data:
+<p> 
+<div class="code"><pre>
+r.in.bin in=gpcp_v2.2_psg.1979 out=gpcp_1979. \
+         order=big bytes=4 -f header=576 anull=-99999 \
+         n=90 s=-90 w=0 e=360 rows=72 cols=144 bands=12
+</pre></div>
 
+
 <h2>SEE ALSO</h2>
 
 <em>
@@ -113,5 +134,6 @@
 
 Jacques Bouchard, France (bouchard at onera.fr)<br>
 Bob Covill, Canada (bcovill at tekmap.ns.ca)<br>
+Markus Metz<br>
 Man page: Zsolt Felker (felker at c160.pki.matav.hu)
 <p><i>Last changed: $Date$</i>



More information about the grass-commit mailing list