[GRASS-SVN] r67988 - in grass-addons/grass7/raster/r.futures: r.futures.calib r.futures.pga

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 2 12:00:48 PST 2016


Author: annakrat
Date: 2016-03-02 12:00:48 -0800 (Wed, 02 Mar 2016)
New Revision: 67988

Modified:
   grass-addons/grass7/raster/r.futures/r.futures.calib/r.futures.calib.py
   grass-addons/grass7/raster/r.futures/r.futures.pga/main.c
Log:
r.futures: make parameter incentive table not required; fix writing of the calibration file

Modified: grass-addons/grass7/raster/r.futures/r.futures.calib/r.futures.calib.py
===================================================================
--- grass-addons/grass7/raster/r.futures/r.futures.calib/r.futures.calib.py	2016-03-02 16:26:18 UTC (rev 67987)
+++ grass-addons/grass7/raster/r.futures/r.futures.calib/r.futures.calib.py	2016-03-02 20:00:48 UTC (rev 67988)
@@ -440,8 +440,8 @@
     gcore.message(_("Analyzing original patches..."))
     diff_development(dev_start, dev_end, options['subregions'], orig_patch_diff)
     patch_analysis(orig_patch_diff, threshold, tmp_patch_vect, tmp_patch_vect2, temp_file.name)
+    write_patches_file(tmp_patch_vect, cell_size, patches_file)
     if only_file:
-        write_patches_file(tmp_patch_vect, cell_size, patches_file)
         return
 
     area, perimeter = np.loadtxt(fname=temp_file.name, unpack=True)
@@ -500,6 +500,7 @@
                                               str(data['input_compactness_mean']), str(data['input_compactness_range']),
                                               str(data['compactness_distance'])]))
                             f.write('\n')
+                        f.flush()
                         proc_count = 0
                         proc_list = []
                         queue_list = []

Modified: grass-addons/grass7/raster/r.futures/r.futures.pga/main.c
===================================================================
--- grass-addons/grass7/raster/r.futures/r.futures.pga/main.c	2016-03-02 16:26:18 UTC (rev 67987)
+++ grass-addons/grass7/raster/r.futures/r.futures.pga/main.c	2016-03-02 20:00:48 UTC (rev 67988)
@@ -871,9 +871,11 @@
                          */
                         probAdd = getDevProbability(pThis, pParams);
                         /* replace with value from lookup table */
-                        lookupPos =
-                            (int)(probAdd * (pParams->nProbLookup - 1));
-                        probAdd = pParams->adProbLookup[lookupPos];
+                        if (pParams->adProbLookup) {
+                            lookupPos =
+                                    (int)(probAdd * (pParams->nProbLookup - 1));
+                            probAdd = pParams->adProbLookup[lookupPos];
+                        }
                         probAdd *= consWeight;
                         pNeighbours->aCandidates[pNeighbours->nCandidates].
                             probAdd = probAdd;
@@ -1602,7 +1604,7 @@
 
     opt.probLookupFile = G_define_standard_option(G_OPT_F_INPUT);
     opt.probLookupFile->key = "incentive_table";
-    opt.probLookupFile->required = YES;
+    opt.probLookupFile->required = NO;
     opt.probLookupFile->label =
         _("File containing incentive lookup table (infill vs. sprawl)");
     opt.probLookupFile->description =
@@ -1709,53 +1711,58 @@
     FILE *fp;
     char inBuff[N_MAXREADINLEN];
     char *pPtr;
+    if (opt.probLookupFile->answer) {
+        G_verbose_message("Reading probability lookup ...");
+        sParams.probLookupFile = opt.probLookupFile->answer;
 
-    G_verbose_message("Reading probability lookup ...");
-    sParams.probLookupFile = opt.probLookupFile->answer;
-
-    fp = fopen(sParams.probLookupFile, "r");
-    if (fp) {
-        parsedOK = 0;
-        if (fgets(inBuff, N_MAXREADINLEN, fp)) {
-            if (inBuff[0] == ',') {
-                sParams.nProbLookup = atoi(inBuff + 1);
-                if (sParams.nProbLookup > 0) {
-                    sParams.adProbLookup =
-                            (double *) G_malloc(sizeof(double) *
-                                                sParams.nProbLookup);
-                    if (sParams.adProbLookup) {
-                        parsedOK = 1;
-                        i = 0;
-                        while (parsedOK && i < sParams.nProbLookup) {
-                            parsedOK = 0;
-                            if (fgets(inBuff, N_MAXREADINLEN, fp)) {
-                                if (pPtr = strchr(inBuff, ',')) {
-                                    parsedOK = 1;
-                                    sParams.adProbLookup[i] =
-                                            atof(pPtr + 1);
-                                    G_debug(3,
-                                            "probability lookup table: i=%d, i/(n-1)=%f, result=%f",
-                                            i,
-                                            i * 1.0 /
-                                            (sParams.nProbLookup - 1),
-                                            sParams.adProbLookup[i]);
+        fp = fopen(sParams.probLookupFile, "r");
+        if (fp) {
+            parsedOK = 0;
+            if (fgets(inBuff, N_MAXREADINLEN, fp)) {
+                if (inBuff[0] == ',') {
+                    sParams.nProbLookup = atoi(inBuff + 1);
+                    if (sParams.nProbLookup > 0) {
+                        sParams.adProbLookup =
+                                (double *) G_malloc(sizeof(double) *
+                                                    sParams.nProbLookup);
+                        if (sParams.adProbLookup) {
+                            parsedOK = 1;
+                            i = 0;
+                            while (parsedOK && i < sParams.nProbLookup) {
+                                parsedOK = 0;
+                                if (fgets(inBuff, N_MAXREADINLEN, fp)) {
+                                    if (pPtr = strchr(inBuff, ',')) {
+                                        parsedOK = 1;
+                                        sParams.adProbLookup[i] =
+                                                atof(pPtr + 1);
+                                        G_debug(3,
+                                                "probability lookup table: i=%d, i/(n-1)=%f, result=%f",
+                                                i,
+                                                i * 1.0 /
+                                                (sParams.nProbLookup - 1),
+                                                sParams.adProbLookup[i]);
+                                    }
                                 }
+                                i++;
                             }
-                            i++;
                         }
                     }
                 }
             }
+            if (!parsedOK) {
+                G_fatal_error("Error parsing probability lookup file '%s'",
+                              sParams.probLookupFile);
+            }
+            fclose(fp);
         }
-        if (!parsedOK) {
-            G_fatal_error("Error parsing probability lookup file '%s'",
+        else {
+            G_fatal_error("Error opening probability lookup file '%s'",
                           sParams.probLookupFile);
         }
-        fclose(fp);
     }
     else {
-        G_fatal_error("Error opening probability lookup file '%s'",
-                      sParams.probLookupFile);
+        sParams.nProbLookup = 0;
+        sParams.adProbLookup = NULL;
     }
 
     sParams.patchMean = atof(opt.patchMean->answer);
@@ -1906,15 +1913,16 @@
                     G_debug(5, "logit value %f", val);
                     /* lookup table of probabilities is applied before consWeight */
                     /* replace with value from lookup table */
-                    lookupPos = (int)(pLandscape->asUndevs[id]
-                                      [pLandscape->num_undevSites[id]].
-                            logitVal * (pParams->nProbLookup - 1));
-                    if (lookupPos >= pParams->nProbLookup || lookupPos < 0)
-                        G_fatal_error("lookup position (%d) out of range [0, %d]",
-                                      lookupPos, pParams->nProbLookup - 1);
-                    pLandscape->asUndevs[id][pLandscape->num_undevSites[id]].
-                            logitVal = pParams->adProbLookup[lookupPos];
-
+                    if (pParams->adProbLookup) {
+                        lookupPos = (int)(pLandscape->asUndevs[id]
+                                          [pLandscape->num_undevSites[id]].
+                                logitVal * (pParams->nProbLookup - 1));
+                        if (lookupPos >= pParams->nProbLookup || lookupPos < 0)
+                            G_fatal_error("lookup position (%d) out of range [0, %d]",
+                                          lookupPos, pParams->nProbLookup - 1);
+                        pLandscape->asUndevs[id][pLandscape->num_undevSites[id]].
+                                logitVal = pParams->adProbLookup[lookupPos];
+                    }
                     // discount by a conservation factor
                     pLandscape->asUndevs[id][pLandscape->num_undevSites[id]].logitVal *= consWeight;
                     /* need to store this to put correct elements near top of list */



More information about the grass-commit mailing list