[GRASS-SVN] r32991 - in grass-addons/gipe: . i.dn2potrad.l7 i.dn2ref.ast i.dn2ref.l7 i.eb.deltat i.eb.disp i.eb.eta i.eb.evapfr i.eb.g0 i.eb.h0 i.eb.h_SEBAL01 i.eb.h_SEBAL95 i.eb.h_iter i.eb.molength i.eb.netrad i.eb.psi i.eb.rah i.eb.rohair i.eb.ublend i.eb.wetdrypix i.eb.z0m i.eb.z0m0 i.emissivity i.evapo.PT i.evapo.SENAY i.evapo.TSA i.evapo.potrad i.evapo.time_integration i.latitude i.lmf i.longitude i.qc.modis i.sattime i.sunhours i.vi i.vi.mpi i.water i.wi r.evapo.MH r.gaswap.serial r.out.vic r.soiltex2prop r.soilusda2tex r.uslek r.usler

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 21 22:44:28 EDT 2008


Author: ychemin
Date: 2008-08-21 22:44:27 -0400 (Thu, 21 Aug 2008)
New Revision: 32991

Modified:
   grass-addons/gipe/i.dn2potrad.l7/bb_alb_landsat.c
   grass-addons/gipe/i.dn2potrad.l7/date2doy.c
   grass-addons/gipe/i.dn2potrad.l7/dn2rad_landsat7.c
   grass-addons/gipe/i.dn2potrad.l7/emissivity_generic.c
   grass-addons/gipe/i.dn2potrad.l7/et_pot_day.c
   grass-addons/gipe/i.dn2potrad.l7/l7inread.c
   grass-addons/gipe/i.dn2potrad.l7/main.c
   grass-addons/gipe/i.dn2potrad.l7/ndvi.c
   grass-addons/gipe/i.dn2potrad.l7/r_net_day.c
   grass-addons/gipe/i.dn2potrad.l7/rad2ref_landsat7.c
   grass-addons/gipe/i.dn2potrad.l7/solar_day.c
   grass-addons/gipe/i.dn2potrad.l7/tempk_landsat7.c
   grass-addons/gipe/i.dn2ref.ast/main.c
   grass-addons/gipe/i.dn2ref.ast/rad2ref_aster.c
   grass-addons/gipe/i.dn2ref.l7/date2doy.c
   grass-addons/gipe/i.dn2ref.l7/dn2rad_landsat7.c
   grass-addons/gipe/i.dn2ref.l7/l7inread.c
   grass-addons/gipe/i.dn2ref.l7/main.c
   grass-addons/gipe/i.dn2ref.l7/rad2ref_landsat7.c
   grass-addons/gipe/i.eb.deltat/delta_t.c
   grass-addons/gipe/i.eb.deltat/main.c
   grass-addons/gipe/i.eb.disp/disp.c
   grass-addons/gipe/i.eb.disp/main.c
   grass-addons/gipe/i.eb.disp/savi2lai.c
   grass-addons/gipe/i.eb.eta/eta.c
   grass-addons/gipe/i.eb.eta/main.c
   grass-addons/gipe/i.eb.evapfr/evap_fr.c
   grass-addons/gipe/i.eb.evapfr/main.c
   grass-addons/gipe/i.eb.evapfr/soilmoisture.c
   grass-addons/gipe/i.eb.g0/g0.c
   grass-addons/gipe/i.eb.g0/main.c
   grass-addons/gipe/i.eb.h0/h0.c
   grass-addons/gipe/i.eb.h0/main.c
   grass-addons/gipe/i.eb.h_SEBAL01/main.c
   grass-addons/gipe/i.eb.h_SEBAL95/U_0.c
   grass-addons/gipe/i.eb.h_SEBAL95/dtair.c
   grass-addons/gipe/i.eb.h_SEBAL95/dtair_desert.c
   grass-addons/gipe/i.eb.h_SEBAL95/functions.h
   grass-addons/gipe/i.eb.h_SEBAL95/h1.c
   grass-addons/gipe/i.eb.h_SEBAL95/h_0.c
   grass-addons/gipe/i.eb.h_SEBAL95/main.c
   grass-addons/gipe/i.eb.h_SEBAL95/psi_h.c
   grass-addons/gipe/i.eb.h_SEBAL95/psi_m.c
   grass-addons/gipe/i.eb.h_SEBAL95/rah1.c
   grass-addons/gipe/i.eb.h_SEBAL95/rah_0.c
   grass-addons/gipe/i.eb.h_SEBAL95/roh_air.c
   grass-addons/gipe/i.eb.h_SEBAL95/roh_air_0.c
   grass-addons/gipe/i.eb.h_SEBAL95/sensi_h.c
   grass-addons/gipe/i.eb.h_SEBAL95/u_star.c
   grass-addons/gipe/i.eb.h_SEBAL95/zom_0.c
   grass-addons/gipe/i.eb.h_iter/fixed_deltat.c
   grass-addons/gipe/i.eb.h_iter/h0.c
   grass-addons/gipe/i.eb.h_iter/main.c
   grass-addons/gipe/i.eb.molength/h0.c
   grass-addons/gipe/i.eb.molength/main.c
   grass-addons/gipe/i.eb.molength/mo_length.c
   grass-addons/gipe/i.eb.netrad/main.c
   grass-addons/gipe/i.eb.netrad/r_net.c
   grass-addons/gipe/i.eb.psi/main.c
   grass-addons/gipe/i.eb.psi/psih.c
   grass-addons/gipe/i.eb.psi/psim.c
   grass-addons/gipe/i.eb.rah/main.c
   grass-addons/gipe/i.eb.rah/rah.c
   grass-addons/gipe/i.eb.rohair/main.c
   grass-addons/gipe/i.eb.rohair/roh_air.c
   grass-addons/gipe/i.eb.ublend/main.c
   grass-addons/gipe/i.eb.ublend/ublend.c
   grass-addons/gipe/i.eb.ublend/ustar.c
   grass-addons/gipe/i.eb.wetdrypix/main.c
   grass-addons/gipe/i.eb.z0m/main.c
   grass-addons/gipe/i.eb.z0m/z0m.c
   grass-addons/gipe/i.eb.z0m0/main.c
   grass-addons/gipe/i.eb.z0m0/zom_0.c
   grass-addons/gipe/i.emissivity/emissivity_generic.c
   grass-addons/gipe/i.emissivity/main.c
   grass-addons/gipe/i.evapo.PT/main.c
   grass-addons/gipe/i.evapo.PT/pt_daily_et.c
   grass-addons/gipe/i.evapo.PT/pt_delta.c
   grass-addons/gipe/i.evapo.PT/pt_ghamma.c
   grass-addons/gipe/i.evapo.SENAY/et_pot_day.c
   grass-addons/gipe/i.evapo.SENAY/evapfr_senay.c
   grass-addons/gipe/i.evapo.SENAY/main.c
   grass-addons/gipe/i.evapo.SENAY/r_net_day.c
   grass-addons/gipe/i.evapo.SENAY/r_net_day_bandara98.c
   grass-addons/gipe/i.evapo.SENAY/solar_day.c
   grass-addons/gipe/i.evapo.SENAY/solar_day_3d.c
   grass-addons/gipe/i.evapo.TSA/main.c
   grass-addons/gipe/i.evapo.TSA/tsa_daily_et.c
   grass-addons/gipe/i.evapo.TSA/tsa_g0g.c
   grass-addons/gipe/i.evapo.TSA/tsa_g0v.c
   grass-addons/gipe/i.evapo.TSA/tsa_hg.c
   grass-addons/gipe/i.evapo.TSA/tsa_hv.c
   grass-addons/gipe/i.evapo.TSA/tsa_ra.c
   grass-addons/gipe/i.evapo.TSA/tsa_rg.c
   grass-addons/gipe/i.evapo.TSA/tsa_rng.c
   grass-addons/gipe/i.evapo.TSA/tsa_rnv.c
   grass-addons/gipe/i.evapo.TSA/tsa_rv.c
   grass-addons/gipe/i.evapo.TSA/tsa_tempkg.c
   grass-addons/gipe/i.evapo.TSA/tsa_tempkv.c
   grass-addons/gipe/i.evapo.potrad/et_pot_day.c
   grass-addons/gipe/i.evapo.potrad/main.c
   grass-addons/gipe/i.evapo.potrad/r_net_day.c
   grass-addons/gipe/i.evapo.potrad/r_net_day_bandara98.c
   grass-addons/gipe/i.evapo.potrad/solar_day.c
   grass-addons/gipe/i.evapo.potrad/solar_day_3d.c
   grass-addons/gipe/i.evapo.time_integration/main.c
   grass-addons/gipe/i.latitude/main.c
   grass-addons/gipe/i.lmf/fitting.c
   grass-addons/gipe/i.lmf/invert_matrix.c
   grass-addons/gipe/i.lmf/lmf.c
   grass-addons/gipe/i.lmf/main.c
   grass-addons/gipe/i.lmf/make_matrix.c
   grass-addons/gipe/i.lmf/maxmin.c
   grass-addons/gipe/i.lmf/minmax.c
   grass-addons/gipe/i.longitude/main.c
   grass-addons/gipe/i.qc.modis/main.c
   grass-addons/gipe/i.qc.modis/qc250a.c
   grass-addons/gipe/i.qc.modis/qc250b.c
   grass-addons/gipe/i.qc.modis/qc250c.c
   grass-addons/gipe/i.qc.modis/qc250d.c
   grass-addons/gipe/i.qc.modis/qc250e.c
   grass-addons/gipe/i.qc.modis/qc250f.c
   grass-addons/gipe/i.qc.modis/qc500a.c
   grass-addons/gipe/i.qc.modis/qc500c.c
   grass-addons/gipe/i.qc.modis/qc500d.c
   grass-addons/gipe/i.qc.modis/qc500e.c
   grass-addons/gipe/i.sattime/main.c
   grass-addons/gipe/i.sunhours/main.c
   grass-addons/gipe/i.vi.mpi/main.c
   grass-addons/gipe/i.vi/arvi.c
   grass-addons/gipe/i.vi/dvi.c
   grass-addons/gipe/i.vi/evi.c
   grass-addons/gipe/i.vi/gari.c
   grass-addons/gipe/i.vi/gemi.c
   grass-addons/gipe/i.vi/gvi.c
   grass-addons/gipe/i.vi/ipvi.c
   grass-addons/gipe/i.vi/main.c
   grass-addons/gipe/i.vi/msavi.c
   grass-addons/gipe/i.vi/msavi2.c
   grass-addons/gipe/i.vi/ndvi.c
   grass-addons/gipe/i.vi/pvi.c
   grass-addons/gipe/i.vi/savi.c
   grass-addons/gipe/i.vi/sr.c
   grass-addons/gipe/i.vi/wdvi.c
   grass-addons/gipe/i.water/main.c
   grass-addons/gipe/i.water/water.c
   grass-addons/gipe/i.water/water_modis.c
   grass-addons/gipe/i.wi/lswi.c
   grass-addons/gipe/i.wi/main.c
   grass-addons/gipe/r.evapo.MH/main.c
   grass-addons/gipe/r.evapo.MH/mh_eto.c
   grass-addons/gipe/r.evapo.MH/mh_original.c
   grass-addons/gipe/r.evapo.MH/mh_samani.c
   grass-addons/gipe/r.gaswap.serial/evaluation.c
   grass-addons/gipe/r.gaswap.serial/gaswap.c
   grass-addons/gipe/r.gaswap.serial/gaswap.h
   grass-addons/gipe/r.gaswap.serial/main.c
   grass-addons/gipe/r.out.vic/main.c
   grass-addons/gipe/r.out.vic/veg_lib.c
   grass-addons/gipe/r.soiltex2prop/main.c
   grass-addons/gipe/r.soiltex2prop/prct2hf.c
   grass-addons/gipe/r.soiltex2prop/prct2ksat.c
   grass-addons/gipe/r.soiltex2prop/prct2porosity.c
   grass-addons/gipe/r.soiltex2prop/vector_multiplication.c
   grass-addons/gipe/r.soilusda2tex/main.c
   grass-addons/gipe/r.soilusda2tex/usda2pclay.c
   grass-addons/gipe/r.soilusda2tex/usda2psand.c
   grass-addons/gipe/r.soilusda2tex/usda2psilt.c
   grass-addons/gipe/r.uslek/main.c
   grass-addons/gipe/r.uslek/prct2tex.c
   grass-addons/gipe/r.uslek/prct2tex.h
   grass-addons/gipe/r.uslek/tex2usle_k.c
   grass-addons/gipe/r.usler/elswaify85.c
   grass-addons/gipe/r.usler/foster81.c
   grass-addons/gipe/r.usler/main.c
   grass-addons/gipe/r.usler/morgan74.c
   grass-addons/gipe/r.usler/roose75.c
   grass-addons/gipe/readme.gipe
Log:
GRASS GIS standard indentation parsing

Modified: grass-addons/gipe/i.dn2potrad.l7/bb_alb_landsat.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/bb_alb_landsat.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/bb_alb_landsat.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,19 +1,23 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Broadband albedo Landsat 5TM and 7ETM+ (maybe others too but not sure)*/
-
-double bb_alb_landsat( double bluechan, double greenchan, double redchan, double nirchan, double chan5, double chan7 )
-{
-	double	result;
-	
-	if( bluechan < 0 || greenchan < 0 || redchan < 0 || nirchan < 0 || chan5 < 0 || chan7 < 0){
-		result = -1.0;
-	} else {
-		result = ( 0.293*bluechan + 0.274*greenchan + 0.233*redchan + 0.156*nirchan + 0.033*chan5 + 0.011*chan7 ) ;
-	}
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Broadband albedo Landsat 5TM and 7ETM+ (maybe others too but not sure) */ 
+double bb_alb_landsat(double bluechan, double greenchan, double redchan,
+		       double nirchan, double chan5, double chan7) 
+{
+    double result;
+
+    if (bluechan < 0 || greenchan < 0 || redchan < 0 || nirchan < 0 ||
+	  chan5 < 0 || chan7 < 0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    (0.293 * bluechan + 0.274 * greenchan + 0.233 * redchan +
+	     0.156 * nirchan + 0.033 * chan5 + 0.011 * chan7);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/date2doy.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/date2doy.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/date2doy.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,66 +1,70 @@
+
 /*********************************************/
-/*This program converts day/month/year to doy*/
+/*This program converts day/month/year to doy */
+
 /*********************************************/
+
 /*********************************************/
 
 int date2doy(int day, int month, int year)
 {
-	int leap=0;
-	int day_month_tot=0;
-	int doy;
+    int leap = 0;
 
-	doy=0;	
+    int day_month_tot = 0;
 
-/*printf("Date is %i/%i/%i\n", day, month, year);*/
+    int doy;
 
-	if (month == 1) {
-		day_month_tot = 0;
-	}
-	else if (month == 2) {
-		day_month_tot = 31;
-	}
-	else if (month == 3) {
-		day_month_tot = 59;
-	}
-	else if (month == 4) {
-		day_month_tot = 90;
-	}
-	else if (month == 5) {
-		day_month_tot = 120;
-	}
-	else if (month == 6) {
-		day_month_tot = 151;
-	}
-	else if (month == 7) {
-		day_month_tot = 181;
-	}
-	else if (month == 8) {
-		day_month_tot = 212;
-	}
-	else if (month == 9) {
-		day_month_tot = 243;
-	}
-	else if (month == 10) {
-		day_month_tot = 273;
-	}
-	else if (month == 11) {
-		day_month_tot = 304;
-	}
-	else if (month == 12) {
-		day_month_tot = 334;
-	}
-	
-	/* Leap year if dividing by 4 leads % 0.0*/
-	
-	if (year/4*4 == year) {
-		leap = 1;
-	}
-	
-	doy=day_month_tot+day;
-	if(doy>59){
-		doy=day_month_tot+day+leap;
-	}
+    doy = 0;
 
-	return(doy);
+    /*printf("Date is %i/%i/%i\n", day, month, year); */
+
+    if (month == 1) {
+	day_month_tot = 0;
+    }
+    else if (month == 2) {
+	day_month_tot = 31;
+    }
+    else if (month == 3) {
+	day_month_tot = 59;
+    }
+    else if (month == 4) {
+	day_month_tot = 90;
+    }
+    else if (month == 5) {
+	day_month_tot = 120;
+    }
+    else if (month == 6) {
+	day_month_tot = 151;
+    }
+    else if (month == 7) {
+	day_month_tot = 181;
+    }
+    else if (month == 8) {
+	day_month_tot = 212;
+    }
+    else if (month == 9) {
+	day_month_tot = 243;
+    }
+    else if (month == 10) {
+	day_month_tot = 273;
+    }
+    else if (month == 11) {
+	day_month_tot = 304;
+    }
+    else if (month == 12) {
+	day_month_tot = 334;
+    }
+
+    /* Leap year if dividing by 4 leads % 0.0 */
+
+    if (year / 4 * 4 == year) {
+	leap = 1;
+    }
+
+    doy = day_month_tot + day;
+    if (doy > 59) {
+	doy = day_month_tot + day + leap;
+    }
+
+    return (doy);
 }
-

Modified: grass-addons/gipe/i.dn2potrad.l7/dn2rad_landsat7.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/dn2rad_landsat7.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/dn2rad_landsat7.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,19 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Conversion of DN to Radiance for Landsat 7ETM+
- * http://ltpwww.gsfc.nasa.gov/IAS/handbook/handbook_htmls/chapter11/chapter11.html#section11.3 
- */
-
-double dn2rad_landsat7( double Lmin, double LMax, double QCalMax, double QCalmin, int DN )
-{
-	double	result, gain, offset;
-	
-	gain 	= (LMax-Lmin)/(QCalMax-QCalmin);
-	offset 	= Lmin;
-	result 	= gain * ((double) DN - QCalmin) + offset ;
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Conversion of DN to Radiance for Landsat 7ETM+
+     * http://ltpwww.gsfc.nasa.gov/IAS/handbook/handbook_htmls/chapter11/chapter11.html#section11.3 
+     */ 
+double dn2rad_landsat7(double Lmin, double LMax, double QCalMax,
+			double QCalmin, int DN) 
+{
+    double result, gain, offset;
+
+    gain = (LMax - Lmin) / (QCalMax - QCalmin);
+    offset = Lmin;
+    result = gain * ((double)DN - QCalmin) + offset;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/emissivity_generic.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/emissivity_generic.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/emissivity_generic.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,23 +1,24 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Emissivity Generic mode (Reads directly from NDVI)
- * Estimation in the 8-14 micrometers range for sparse canopy
- */
-
-double emissivity_generic( double ndvi )
-{
-	double	result;
-	
-	if(ndvi < 0.16){
-		result = 1.0;
-	} else if(ndvi > 0.74){
-		result = 0.9;
-	} else {
-		result = 1.009 + 0.047*log(ndvi);
-	}
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Emissivity Generic mode (Reads directly from NDVI)
+     * Estimation in the 8-14 micrometers range for sparse canopy
+     */ 
+double emissivity_generic(double ndvi) 
+{
+    double result;
+
+    if (ndvi < 0.16) {
+	result = 1.0;
+    }
+    else if (ndvi > 0.74) {
+	result = 0.9;
+    }
+    else {
+	result = 1.009 + 0.047 * log(ndvi);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/et_pot_day.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/et_pot_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/et_pot_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,16 +1,18 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Potential ET after Bastiaanssen (1995)*/
-
-double et_pot_day( double bbalb, double solar, double tempk, double tsw, double roh_w )
-{
-	double latent, result;
-
-	latent=(2.501-(0.002361*(tempk-273.15)))*1000000.0;
-	result = ((((1.0 - bbalb)*solar)-(110.0*tsw))*86400*1000.0)/(latent*roh_w);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Potential ET after Bastiaanssen (1995) */ 
+double et_pot_day(double bbalb, double solar, double tempk, double tsw,
+		   double roh_w) 
+{
+    double latent, result;
+
+    latent = (2.501 - (0.002361 * (tempk - 273.15))) * 1000000.0;
+    result =
+	((((1.0 - bbalb) * solar) -
+	  (110.0 * tsw)) * 86400 * 1000.0) / (latent * roh_w);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/l7inread.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/l7inread.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/l7inread.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,388 +4,349 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
-int l7_in_read(char *metfName, char *b1, char *b2, char *b3, char *b4, char *b5, char *b61, char *b62, char *b7, char *b8, double *lmin,double *lmax,double *qcalmin,double *qcalmax,double *sun_elevation, double *sun_azimuth,int *day, int *month, int *year)
+int l7_in_read(char *metfName, char *b1, char *b2, char *b3, char *b4,
+	       char *b5, char *b61, char *b62, char *b7, char *b8,
+	       double *lmin, double *lmax, double *qcalmin, double *qcalmax,
+	       double *sun_elevation, double *sun_azimuth, int *day,
+	       int *month, int *year)
 {
-	FILE 		*f;
-	char 		s[1000], *ptr;
-	int 		i=0;
-	char 		*p;
+    FILE *f;
 
-	char		band1[80],band2[80],band3[80];
-	char		band4[80],band5[80],band61[80];
-	char		band62[80],band7[80],band8[80];/*Load .tif names*/
+    char s[1000], *ptr;
 
-	if((f=fopen(metfName,"r"))==NULL){
-		G_fatal_error (_(".met file [%s] not found, are you in the proper directory?"), metfName);
-	}
+    int i = 0;
 
-	while (fgets(s,1000,f)!=NULL)
-	{
-		ptr = strstr(s, "ACQUISITION_DATE");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =-");
-			*year = atoi(p);
-			p = strtok(NULL, "-");
-			*month = atoi(p);
-			p = strtok(NULL, "-");
-			*day = atoi(p);
-		}
-		ptr = strstr(s, "BAND1_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band1, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND2_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band2, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND3_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band3, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND4_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band4, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND5_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band5, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND61_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band61, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND62_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band62, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND7_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band7, 80, "%s", p);
-		}
-		ptr = strstr(s, "BAND8_FILE_NAME");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =\"");
-			snprintf(band8, 80, "%s", p);
-		}
-		ptr = strstr(s, "SUN_AZIMUTH");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			*sun_azimuth = atof(p);
-		
-		}
-		ptr = strstr(s, "SUN_ELEVATION");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			*sun_elevation = atof(p);
+    char *p;
 
-		}
-		ptr = strstr(s, "\tLMAX_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[0] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[0] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[1] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[1] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[2] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[2] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[3] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[3] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[4] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[4] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[5] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[5] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[6] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[6] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[7] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[7] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[8] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[8] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[0] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[0] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[1] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[1] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[2] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[2] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[3] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[3] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[4] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[4] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[5] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[5] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[6] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[6] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[7] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[7] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[8] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[8] = atof(p);
-		}
+    char band1[80], band2[80], band3[80];
+
+    char band4[80], band5[80], band61[80];
+
+    char band62[80], band7[80], band8[80];	/*Load .tif names */
+
+    if ((f = fopen(metfName, "r")) == NULL) {
+	G_fatal_error(_(".met file [%s] not found, are you in the proper directory?"),
+		      metfName);
+    }
+
+    while (fgets(s, 1000, f) != NULL) {
+	ptr = strstr(s, "ACQUISITION_DATE");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =-");
+	    *year = atoi(p);
+	    p = strtok(NULL, "-");
+	    *month = atoi(p);
+	    p = strtok(NULL, "-");
+	    *day = atoi(p);
 	}
+	ptr = strstr(s, "BAND1_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band1, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND2_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band2, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND3_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band3, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND4_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band4, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND5_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band5, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND61_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band61, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND62_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band62, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND7_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band7, 80, "%s", p);
+	}
+	ptr = strstr(s, "BAND8_FILE_NAME");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =\"");
+	    snprintf(band8, 80, "%s", p);
+	}
+	ptr = strstr(s, "SUN_AZIMUTH");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    *sun_azimuth = atof(p);
 
-	snprintf(b1, 80, "%s", band1);
-	snprintf(b2, 80, "%s", band2);
-	snprintf(b3, 80, "%s", band3);
-	snprintf(b4, 80, "%s", band4);
-	snprintf(b5, 80, "%s", band5);
-	snprintf(b61, 80, "%s", band61);
-	snprintf(b62, 80, "%s", band62);
-	snprintf(b7, 80, "%s", band7);
-	snprintf(b8, 80, "%s", band8);
-/*	printf("year = %i\n", year);
-	printf("month = %i\n", month);
-	printf("day = %i\n", day);
-	printf("sun azimuth = %f\n", sun_azimuth);
-	printf("sun elevation = %f\n", sun_elevation);
-	for (i=0;i<MAXFILES;i++){
-		printf("lmin[%i]=%f\n",i,lmin[i]);
-		printf("lmax[%i]=%f\n",i,lmax[i]);
-		printf("qcalmin[%i]=%f\n",i,qcalmin[i]);
-		printf("qcalmax[%i]=%f\n",i,qcalmax[i]);
 	}
-*/
-	(void)fclose(f);
-	return;
+	ptr = strstr(s, "SUN_ELEVATION");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    *sun_elevation = atof(p);
+
+	}
+	ptr = strstr(s, "\tLMAX_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[0] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[0] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[1] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[1] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[2] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[2] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[3] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[3] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[4] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[4] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[5] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[5] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[6] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[6] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[7] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[7] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[8] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[8] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[0] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[0] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[1] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[1] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[2] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[2] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[3] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[3] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[4] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[4] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[5] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[5] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[6] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[6] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[7] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[7] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[8] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[8] = atof(p);
+	}
+    }
+
+    snprintf(b1, 80, "%s", band1);
+    snprintf(b2, 80, "%s", band2);
+    snprintf(b3, 80, "%s", band3);
+    snprintf(b4, 80, "%s", band4);
+    snprintf(b5, 80, "%s", band5);
+    snprintf(b61, 80, "%s", band61);
+    snprintf(b62, 80, "%s", band62);
+    snprintf(b7, 80, "%s", band7);
+    snprintf(b8, 80, "%s", band8);
+    /*      printf("year = %i\n", year);
+       printf("month = %i\n", month);
+       printf("day = %i\n", day);
+       printf("sun azimuth = %f\n", sun_azimuth);
+       printf("sun elevation = %f\n", sun_elevation);
+       for (i=0;i<MAXFILES;i++){
+       printf("lmin[%i]=%f\n",i,lmin[i]);
+       printf("lmax[%i]=%f\n",i,lmax[i]);
+       printf("qcalmin[%i]=%f\n",i,qcalmin[i]);
+       printf("qcalmax[%i]=%f\n",i,qcalmax[i]);
+       }
+     */
+    (void)fclose(f);
+    return;
 }

Modified: grass-addons/gipe/i.dn2potrad.l7/main.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.dn2potrad.l7
@@ -23,500 +24,588 @@
 
 #define MAXFILES 9
 
-/*sun exo-atmospheric irradiance*/
+/*sun exo-atmospheric irradiance */
 #define KEXO1 1969.0
 #define KEXO2 1840.0
 #define KEXO3 1551.0
 #define KEXO4 1044.0
 #define KEXO5 225.7
 #define KEXO7 82.07
-#define KEXO8 1385.64 /*to find the real value in the internet*/
+#define KEXO8 1385.64		/*to find the real value in the internet */
 
 #define PI 3.1415926
 
 
-int l7_in_read(char *metfName, char *b1, char *b2, char *b3, char *b4, char *b5, char *b61, char *b62, char *b7, char *b8, double *lmin,double *lmax,double *qcalmin,double *qcalmax,double *sun_elevation, double *sun_azimuth,int *day, int *month, int *year);
+int l7_in_read(char *metfName, char *b1, char *b2, char *b3, char *b4,
+	       char *b5, char *b61, char *b62, char *b7, char *b8,
+	       double *lmin, double *lmax, double *qcalmin, double *qcalmax,
+	       double *sun_elevation, double *sun_azimuth, int *day,
+	       int *month, int *year);
 int date2doy(int day, int month, int year);
-double dn2rad_landsat7( double Lmin, double LMax, double QCalMax, double QCalmin, int DN );
-double rad2ref_landsat7( double radiance, double doy,double sun_elevation, double k_exo );
-double tempk_landsat7( double l6 );
-double bb_alb_landsat( double bluechan, double greenchan, double redchan, double nirchan, double chan5, double chan7 );
-double nd_vi( double redchan, double nirchan );
-double emissivity_generic( double ndvi );
-double solar_day(double lat, double doy, double tsw );
-double r_net_day( double bbalb, double solar, double tsw );
-double et_pot_day( double bbalb, double solar, double tempk, double tsw, double roh_w );
 
-int
-main(int argc, char *argv[])
+double dn2rad_landsat7(double Lmin, double LMax, double QCalMax,
+		       double QCalmin, int DN);
+double rad2ref_landsat7(double radiance, double doy, double sun_elevation,
+			double k_exo);
+double tempk_landsat7(double l6);
+
+double bb_alb_landsat(double bluechan, double greenchan, double redchan,
+		      double nirchan, double chan5, double chan7);
+double nd_vi(double redchan, double nirchan);
+
+double emissivity_generic(double ndvi);
+
+double solar_day(double lat, double doy, double tsw);
+
+double r_net_day(double bbalb, double solar, double tsw);
+
+double et_pot_day(double bbalb, double solar, double tempk, double tsw,
+		  double roh_w);
+
+int main(int argc, char *argv[])
 {
-	struct Cell_head cellhd;/*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
+    struct Cell_head cellhd;	/*region+header info */
 
-	struct GModule *module;
-	struct Option *input,*input1,*input2,*input3,*output;
-	
-	struct Flag *flag1;
-	struct History history; /*metadata*/
+    char *mapset;		/*mapset name */
 
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input, *input1, *input2, *input3, *output;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	char history_buf[200];
-	char *name; /*input raster name*/
-	char *result; /*output raster name*/
-	
-	/*File Descriptors*/
-	int infd[MAXFILES];
-	int outfd[MAXFILES];
+    /* FMEO Declarations**************** */
+    char history_buf[200];
 
-	char **names;
-	char **ptr;
-	
-	int i=0,j=0;
-	
-	void *inrast[MAXFILES];
-	DCELL *outrast[MAXFILES];
-	RASTER_MAP_TYPE in_data_type[MAXFILES];
-	RASTER_MAP_TYPE out_data_type = DCELL_TYPE; /* 0=numbers  1=text */
+    char *name;			/*input raster name */
 
-	double		kexo[MAXFILES];
-	/*Metfile*/
-	char		*metfName; /*met file, header in text format*/
-	char		b1[80],b2[80],b3[80];
-	char		b4[80],b5[80],b61[80];
-	char		b62[80],b7[80],b8[80];/*Load .tif names*/
-	double 		lmin[MAXFILES];
-	double 		lmax[MAXFILES];
-	double 		qcalmin[MAXFILES];
-	double 		qcalmax[MAXFILES];
-	double 		sun_elevation;
-	double 		sun_azimuth;/*not useful here, only for parser()*/
-	int 		day,month,year;	
-	/*EndofMetfile*/
-	int 		doy;
+    char *result;		/*output raster name */
 
-	int 		not_ll=0; /*if proj is not lat/long, it will be 1.*/
-	struct 		pj_info iproj;
-	struct 		pj_info oproj;
-/*	char 		*lat;*/
-	double 		xp, yp;
-	double		xmin, ymin;
-	double 		xmax, ymax;
-	double 		stepx,stepy;
-	double 		latitude, longitude;
+    /*File Descriptors */
+    int infd[MAXFILES];
 
-	int		chosen_tempk_band;
-	double		roh_w, tsw;
+    int outfd[MAXFILES];
+
+    char **names;
+
+    char **ptr;
+
+    int i = 0, j = 0;
+
+    void *inrast[MAXFILES];
+
+    DCELL *outrast[MAXFILES];
+
+    RASTER_MAP_TYPE in_data_type[MAXFILES];
+
+    RASTER_MAP_TYPE out_data_type = DCELL_TYPE;	/* 0=numbers  1=text */
+
+    double kexo[MAXFILES];
+
+    /*Metfile */
+    char *metfName;		/*met file, header in text format */
+
+    char b1[80], b2[80], b3[80];
+
+    char b4[80], b5[80], b61[80];
+
+    char b62[80], b7[80], b8[80];	/*Load .tif names */
+
+    double lmin[MAXFILES];
+
+    double lmax[MAXFILES];
+
+    double qcalmin[MAXFILES];
+
+    double qcalmax[MAXFILES];
+
+    double sun_elevation;
+
+    double sun_azimuth;		/*not useful here, only for parser() */
+
+    int day, month, year;
+
+    /*EndofMetfile */
+    int doy;
+
+    int not_ll = 0;		/*if proj is not lat/long, it will be 1. */
+
+    struct pj_info iproj;
+
+    struct pj_info oproj;
+
+    /*      char            *lat; */
+    double xp, yp;
+
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
+    int chosen_tempk_band;
+
+    double roh_w, tsw;
+
 	/************************************/
 
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->keywords = _("DN, reflectance, temperature, import, potential evapotranspiration");
-	module->description =
-		_("Calculates Potential evapotranspiration from Landsat 7 DN.\n");
+    module = G_define_module();
+    module->keywords =
+	_("DN, reflectance, temperature, import, potential evapotranspiration");
+    module->description =
+	_("Calculates Potential evapotranspiration from Landsat 7 DN.\n");
 
-	/* Define the different options */
-	input = G_define_option() ;
-	input->key        = _("metfile");
-	input->type       = TYPE_STRING;
-	input->required   = YES;
-	input->gisprompt  = _("old_file,file,file");
-	input->description= _("Landsat 7ETM+ Header File (.met)");
+    /* Define the different options */
+    input = G_define_option();
+    input->key = _("metfile");
+    input->type = TYPE_STRING;
+    input->required = YES;
+    input->gisprompt = _("old_file,file,file");
+    input->description = _("Landsat 7ETM+ Header File (.met)");
 
-	input1 = G_define_option() ;
-	input1->key        = _("tsw");
-	input1->type       = TYPE_DOUBLE;
-	input1->required   = YES;
-	input1->gisprompt  = _("value, parameter");
-	input1->description= _("Single-way transmissivity of the atmosphere [0.0-1.0]");
-	input1->answer	   = "0.7";
+    input1 = G_define_option();
+    input1->key = _("tsw");
+    input1->type = TYPE_DOUBLE;
+    input1->required = YES;
+    input1->gisprompt = _("value, parameter");
+    input1->description =
+	_("Single-way transmissivity of the atmosphere [0.0-1.0]");
+    input1->answer = "0.7";
 
-	input2 = G_define_option() ;
-	input2->key        = _("roh_w");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  = _("value, parameter");
-	input2->description= _("Density of water (~1000 g/m3)");
-	input2->answer	   = "1010.0";
+    input2 = G_define_option();
+    input2->key = _("roh_w");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("value, parameter");
+    input2->description = _("Density of water (~1000 g/m3)");
+    input2->answer = "1010.0";
 
-	input3 = G_define_option() ;
-	input3->key        = _("tempk_band");
-	input3->type       = TYPE_INTEGER;
-	input3->required   = YES;
-	input3->gisprompt  = _("value, parameter");
-	input3->description= _("5=band61 || 6=band62");
-	input3->answer     = "5";
+    input3 = G_define_option();
+    input3->key = _("tempk_band");
+    input3->type = TYPE_INTEGER;
+    input3->required = YES;
+    input3->gisprompt = _("value, parameter");
+    input3->description = _("5=band61 || 6=band62");
+    input3->answer = "5";
 
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("ETpot output layer name");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("ETpot output layer name");
 
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
-	
-	metfName	= input->answer;
-	result		= output->answer;
-	tsw		= atof(input1->answer);
-	roh_w           = atof(input2->answer);
-	chosen_tempk_band = atoi(input3->answer);
+    if (G_parser(argc, argv))
+	exit(-1);
 
-	if (tsw>=1.0||tsw<=0.0){
-		G_fatal_error (_("Tsw is out of range, correct it."));
-	}
-	if (roh_w>=1100.0||roh_w<=900.0){
-		G_fatal_error (_("Something wrong with roh_w value, please check."));
-	}
-	if (chosen_tempk_band!=5&&chosen_tempk_band!=6){
-		G_fatal_error (_("Tempk band is not good, change to 5 OR 6!"));
-	}
+    metfName = input->answer;
+    result = output->answer;
+    tsw = atof(input1->answer);
+    roh_w = atof(input2->answer);
+    chosen_tempk_band = atoi(input3->answer);
 
+    if (tsw >= 1.0 || tsw <= 0.0) {
+	G_fatal_error(_("Tsw is out of range, correct it."));
+    }
+    if (roh_w >= 1100.0 || roh_w <= 900.0) {
+	G_fatal_error(_("Something wrong with roh_w value, please check."));
+    }
+    if (chosen_tempk_band != 5 && chosen_tempk_band != 6) {
+	G_fatal_error(_("Tempk band is not good, change to 5 OR 6!"));
+    }
+
 	/******************************************/
-	/*Fetch parameters for DN2Rad2Ref correction*/
-	l7_in_read(metfName,b1,b2,b3,b4,b5,b61,b62,b7,b8,lmin,lmax,qcalmin,qcalmax,&sun_elevation,&sun_azimuth,&day,&month,&year);
-	doy = date2doy(day,month,year);
-/*	printf("%f/%f/%i-%i-%i\n",sun_elevation,sun_azimuth,day,month,year);
-	for(i=0;i<MAXFILES;i++){
-		printf("%i=>%f, %f, %f, %f\n",i,lmin[i],lmax[i],qcalmin[i],qcalmax[i]);
-	}
-	printf("b1=%s\n",b1);
-	printf("b2=%s\n",b2);
-	printf("b3=%s\n",b3);
-	printf("b4=%s\n",b4);
-	printf("b5=%s\n",b5);
-	printf("b61=%s\n",b61);
-	printf("b62=%s\n",b62);
-	printf("b7=%s\n",b7);
-	printf("b8=%s\n",b8);
-	printf("doy=%i\n",doy);
-*/
+    /*Fetch parameters for DN2Rad2Ref correction */
+    l7_in_read(metfName, b1, b2, b3, b4, b5, b61, b62, b7, b8, lmin, lmax,
+	       qcalmin, qcalmax, &sun_elevation, &sun_azimuth, &day, &month,
+	       &year);
+    doy = date2doy(day, month, year);
+    /*      printf("%f/%f/%i-%i-%i\n",sun_elevation,sun_azimuth,day,month,year);
+       for(i=0;i<MAXFILES;i++){
+       printf("%i=>%f, %f, %f, %f\n",i,lmin[i],lmax[i],qcalmin[i],qcalmax[i]);
+       }
+       printf("b1=%s\n",b1);
+       printf("b2=%s\n",b2);
+       printf("b3=%s\n",b3);
+       printf("b4=%s\n",b4);
+       printf("b5=%s\n",b5);
+       printf("b61=%s\n",b61);
+       printf("b62=%s\n",b62);
+       printf("b7=%s\n",b7);
+       printf("b8=%s\n",b8);
+       printf("doy=%i\n",doy);
+     */
+
 	/********************/
-	/*Prepare sun exo-atm irradiance*/
+    /*Prepare sun exo-atm irradiance */
+
 	/********************/
-	
-	kexo[0]=KEXO1;
-	kexo[1]=KEXO2;
-	kexo[2]=KEXO3;
-	kexo[3]=KEXO4;
-	kexo[4]=KEXO5;
-	kexo[5]=0.0;/*filling*/
-	kexo[6]=0.0;/*filling*/
-	kexo[7]=KEXO7;
-	kexo[8]=KEXO8;
-	
+
+    kexo[0] = KEXO1;
+    kexo[1] = KEXO2;
+    kexo[2] = KEXO3;
+    kexo[3] = KEXO4;
+    kexo[4] = KEXO5;
+    kexo[5] = 0.0;		/*filling */
+    kexo[6] = 0.0;		/*filling */
+    kexo[7] = KEXO7;
+    kexo[8] = KEXO8;
+
 	/***************************************************/
-	/*Band1*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b1, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b1);
-	}
-	if (G_legal_filename (b1) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b1);
-	}
-	infd[0] = G_open_cell_old (b1, mapset);
-	/* Allocate input buffer */
-	in_data_type[0] = G_raster_map_type(b1, mapset);
-	if( (infd[0] = G_open_cell_old(b1,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b1);
-	}
-	if( (G_get_cellhd(b1,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b1);
-	}
-	inrast[0] = G_allocate_raster_buf(in_data_type[0]);
+    /*Band1 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b1, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b1);
+    }
+    if (G_legal_filename(b1) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b1);
+    }
+    infd[0] = G_open_cell_old(b1, mapset);
+    /* Allocate input buffer */
+    in_data_type[0] = G_raster_map_type(b1, mapset);
+    if ((infd[0] = G_open_cell_old(b1, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b1);
+    }
+    if ((G_get_cellhd(b1, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b1);
+    }
+    inrast[0] = G_allocate_raster_buf(in_data_type[0]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band2*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b2, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b2);
-	}
-	if (G_legal_filename (b2) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b2);
-	}
-	infd[1] = G_open_cell_old (b2, mapset);
-	/* Allocate input buffer */
-	in_data_type[1] = G_raster_map_type(b2, mapset);
-	if( (infd[1] = G_open_cell_old(b2,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b2);
-	}
-	if( (G_get_cellhd(b2,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b2);
-	}
-	inrast[1] = G_allocate_raster_buf(in_data_type[1]);
+    /*Band2 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b2, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b2);
+    }
+    if (G_legal_filename(b2) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b2);
+    }
+    infd[1] = G_open_cell_old(b2, mapset);
+    /* Allocate input buffer */
+    in_data_type[1] = G_raster_map_type(b2, mapset);
+    if ((infd[1] = G_open_cell_old(b2, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b2);
+    }
+    if ((G_get_cellhd(b2, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b2);
+    }
+    inrast[1] = G_allocate_raster_buf(in_data_type[1]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band3*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b3, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b3);
-	}
-	if (G_legal_filename (b3) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b3);
-	}
-	infd[2] = G_open_cell_old (b3, mapset);
-	/* Allocate input buffer */
-	in_data_type[2] = G_raster_map_type(b3, mapset);
-	if( (infd[2] = G_open_cell_old(b3,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b3);
-	}
-	if( (G_get_cellhd(b3,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b3);
-	}
-	inrast[2] = G_allocate_raster_buf(in_data_type[2]);
+    /*Band3 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b3, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b3);
+    }
+    if (G_legal_filename(b3) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b3);
+    }
+    infd[2] = G_open_cell_old(b3, mapset);
+    /* Allocate input buffer */
+    in_data_type[2] = G_raster_map_type(b3, mapset);
+    if ((infd[2] = G_open_cell_old(b3, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b3);
+    }
+    if ((G_get_cellhd(b3, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b3);
+    }
+    inrast[2] = G_allocate_raster_buf(in_data_type[2]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band4*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b4, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b4);
-	}
-	if (G_legal_filename (b4) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b4);
-	}
-	infd[3] = G_open_cell_old (b4, mapset);
-	/* Allocate input buffer */
-	in_data_type[3] = G_raster_map_type(b4, mapset);
-	if( (infd[3] = G_open_cell_old(b4,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b4);
-	}
-	if( (G_get_cellhd(b4,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b4);
-	}
-	inrast[3] = G_allocate_raster_buf(in_data_type[3]);
+    /*Band4 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b4, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b4);
+    }
+    if (G_legal_filename(b4) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b4);
+    }
+    infd[3] = G_open_cell_old(b4, mapset);
+    /* Allocate input buffer */
+    in_data_type[3] = G_raster_map_type(b4, mapset);
+    if ((infd[3] = G_open_cell_old(b4, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b4);
+    }
+    if ((G_get_cellhd(b4, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b4);
+    }
+    inrast[3] = G_allocate_raster_buf(in_data_type[3]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band5*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b5, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b5);
-	}
-	if (G_legal_filename (b5) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b5);
-	}
-	infd[4] = G_open_cell_old (b5, mapset);
-	/* Allocate input buffer */
-	in_data_type[4] = G_raster_map_type(b5, mapset);
-	if( (infd[4] = G_open_cell_old(b5,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b5);
-	}
-	if( (G_get_cellhd(b5,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b5);
-	}
-	inrast[4] = G_allocate_raster_buf(in_data_type[4]);
+    /*Band5 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b5, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b5);
+    }
+    if (G_legal_filename(b5) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b5);
+    }
+    infd[4] = G_open_cell_old(b5, mapset);
+    /* Allocate input buffer */
+    in_data_type[4] = G_raster_map_type(b5, mapset);
+    if ((infd[4] = G_open_cell_old(b5, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b5);
+    }
+    if ((G_get_cellhd(b5, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b5);
+    }
+    inrast[4] = G_allocate_raster_buf(in_data_type[4]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band61*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b61, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b61);
-	}
-	if (G_legal_filename (b61) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b61);
-	}
-	infd[5] = G_open_cell_old (b61, mapset);
-	/* Allocate input buffer */
-	in_data_type[5] = G_raster_map_type(b61, mapset);
-	if( (infd[5] = G_open_cell_old(b61,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b61);
-	}
-	if( (G_get_cellhd(b61,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b61);
-	}
-	inrast[5] = G_allocate_raster_buf(in_data_type[5]);
+    /*Band61 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b61, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b61);
+    }
+    if (G_legal_filename(b61) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b61);
+    }
+    infd[5] = G_open_cell_old(b61, mapset);
+    /* Allocate input buffer */
+    in_data_type[5] = G_raster_map_type(b61, mapset);
+    if ((infd[5] = G_open_cell_old(b61, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b61);
+    }
+    if ((G_get_cellhd(b61, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b61);
+    }
+    inrast[5] = G_allocate_raster_buf(in_data_type[5]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band62*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b62, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b62);
-	}
-	if (G_legal_filename (b62) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b62);
-	}
-	infd[6] = G_open_cell_old (b62, mapset);
-	/* Allocate input buffer */
-	in_data_type[6] = G_raster_map_type(b62, mapset);
-	if( (infd[6] = G_open_cell_old(b62,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b62);
-	}
-	if( (G_get_cellhd(b62,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b62);
-	}
-	inrast[6] = G_allocate_raster_buf(in_data_type[6]);
+    /*Band62 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b62, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b62);
+    }
+    if (G_legal_filename(b62) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b62);
+    }
+    infd[6] = G_open_cell_old(b62, mapset);
+    /* Allocate input buffer */
+    in_data_type[6] = G_raster_map_type(b62, mapset);
+    if ((infd[6] = G_open_cell_old(b62, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b62);
+    }
+    if ((G_get_cellhd(b62, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b62);
+    }
+    inrast[6] = G_allocate_raster_buf(in_data_type[6]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band7*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b7, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b7);
-	}
-	if (G_legal_filename (b7) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b7);
-	}
-	infd[7] = G_open_cell_old (b7, mapset);
-	/* Allocate input buffer */
-	in_data_type[7] = G_raster_map_type(b7, mapset);
-	if( (infd[7] = G_open_cell_old(b7,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b7);
-	}
-	if( (G_get_cellhd(b7,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b7);
-	}
-	inrast[7] = G_allocate_raster_buf(in_data_type[7]);
+    /*Band7 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b7, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b7);
+    }
+    if (G_legal_filename(b7) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b7);
+    }
+    infd[7] = G_open_cell_old(b7, mapset);
+    /* Allocate input buffer */
+    in_data_type[7] = G_raster_map_type(b7, mapset);
+    if ((infd[7] = G_open_cell_old(b7, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b7);
+    }
+    if ((G_get_cellhd(b7, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b7);
+    }
+    inrast[7] = G_allocate_raster_buf(in_data_type[7]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/*Band8*/
-	/* find map in mapset */
-	mapset = G_find_cell2 (b8, "");
-        if (mapset == NULL){
-		G_fatal_error (_("cell file [%s] not found"), b8);
-	}
-	if (G_legal_filename (b8) < 0){
-		G_fatal_error (_("[%s] is an illegal name"), b8);
-	}
-	infd[8] = G_open_cell_old (b8, mapset);
-	/* Allocate input buffer */
-	in_data_type[8] = G_raster_map_type(b8, mapset);
-	if( (infd[8] = G_open_cell_old(b8,mapset)) < 0){
-		G_fatal_error(_("Cannot open cell file [%s]"), b8);
-	}
-	if( (G_get_cellhd(b8,mapset,&cellhd)) < 0){
-		G_fatal_error(_("Cannot read file header of [%s]"), b8);
-	}
-	inrast[8] = G_allocate_raster_buf(in_data_type[8]);
+    /*Band8 */
+    /* find map in mapset */
+    mapset = G_find_cell2(b8, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), b8);
+    }
+    if (G_legal_filename(b8) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), b8);
+    }
+    infd[8] = G_open_cell_old(b8, mapset);
+    /* Allocate input buffer */
+    in_data_type[8] = G_raster_map_type(b8, mapset);
+    if ((infd[8] = G_open_cell_old(b8, mapset)) < 0) {
+	G_fatal_error(_("Cannot open cell file [%s]"), b8);
+    }
+    if ((G_get_cellhd(b8, mapset, &cellhd)) < 0) {
+	G_fatal_error(_("Cannot read file header of [%s]"), b8);
+    }
+    inrast[8] = G_allocate_raster_buf(in_data_type[8]);
+
 	/***************************************************/
+
 	/***************************************************/
-	/* Allocate output buffer, use input map data_type */
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
+    /* Allocate output buffer, use input map data_type */
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
 
-	nrows = G_window_rows();
-	ncols = G_window_cols();
+    nrows = G_window_rows();
+    ncols = G_window_cols();
 
-	/* Following is for latitude conversion from UTM to Geo
-	 * Shamelessly stolen from r.sun !!!!	
-	 * Set up parameters for projection to lat/long if necessary */
-	if ((G_projection() != PROJECTION_LL)) {
-		not_ll=1;
-		struct Key_Value *in_proj_info, *in_unit_info;
-		if ((in_proj_info = G_get_projinfo()) == NULL)
-			G_fatal_error(_("Can't get projection info of current location"));
-		if ((in_unit_info = G_get_projunits()) == NULL)
-			G_fatal_error(_("Can't get projection units of current location"));
-		if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
-			G_fatal_error(_("Can't get projection key values of current location"));
-		G_free_key_value(in_proj_info);
-		G_free_key_value(in_unit_info);
-		/* Set output projection to latlong w/ same ellipsoid */
-		oproj.zone = 0;
-		oproj.meters = 1.;
-		sprintf(oproj.proj, "ll");
-		if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
-			G_fatal_error(_("Unable to set up lat/long projection parameters"));
-	}/*End of stolen from r.sun */
-	
-	out_data_type=DCELL_TYPE;
-	outrast[0] = G_allocate_raster_buf(out_data_type);
-	if ( (outfd[0] = G_open_raster_new (result,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result);
-	
-	/* Process pixels */
-	DCELL dout[MAXFILES];
-	DCELL d[MAXFILES];
-	DCELL d_albedo;
-	DCELL d_tempk;
-	DCELL d_ndvi;
-	DCELL d_e0;
-	DCELL d_lat;
-/*	DCELL d_doy;*/
-	DCELL d_tsw;
-/*	DCELL d_roh_w;*/
-	DCELL d_solar;
-	DCELL d_rnetd;
-	DCELL d_etpot;
-	for (row = 0; row < nrows; row++)
-	{
-		if (verbose){
-			G_percent (row, nrows, 2);
+    /* Following is for latitude conversion from UTM to Geo
+     * Shamelessly stolen from r.sun !!!!   
+     * Set up parameters for projection to lat/long if necessary */
+    if ((G_projection() != PROJECTION_LL)) {
+	not_ll = 1;
+	struct Key_Value *in_proj_info, *in_unit_info;
+
+	if ((in_proj_info = G_get_projinfo()) == NULL)
+	    G_fatal_error(_("Can't get projection info of current location"));
+	if ((in_unit_info = G_get_projunits()) == NULL)
+	    G_fatal_error(_("Can't get projection units of current location"));
+	if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
+	    G_fatal_error(_("Can't get projection key values of current location"));
+	G_free_key_value(in_proj_info);
+	G_free_key_value(in_unit_info);
+	/* Set output projection to latlong w/ same ellipsoid */
+	oproj.zone = 0;
+	oproj.meters = 1.;
+	sprintf(oproj.proj, "ll");
+	if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
+	    G_fatal_error(_("Unable to set up lat/long projection parameters"));
+    }				/*End of stolen from r.sun */
+
+    out_data_type = DCELL_TYPE;
+    outrast[0] = G_allocate_raster_buf(out_data_type);
+    if ((outfd[0] = G_open_raster_new(result, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+
+    /* Process pixels */
+    DCELL dout[MAXFILES];
+
+    DCELL d[MAXFILES];
+
+    DCELL d_albedo;
+
+    DCELL d_tempk;
+
+    DCELL d_ndvi;
+
+    DCELL d_e0;
+
+    DCELL d_lat;
+
+    /*      DCELL d_doy; */
+    DCELL d_tsw;
+
+    /*      DCELL d_roh_w; */
+    DCELL d_solar;
+
+    DCELL d_rnetd;
+
+    DCELL d_etpot;
+
+    for (row = 0; row < nrows; row++) {
+	if (verbose) {
+	    G_percent(row, nrows, 2);
+	}
+	/* read input map */
+	for (i = 0; i < MAXFILES; i++) {
+	    if ((G_get_raster_row(infd[i], inrast[i], row, in_data_type[i])) <
+		0) {
+		G_fatal_error(_("Could not read from <%s>"), i);
+	    }
+	}
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    /*Calculate Latitude */
+	    latitude = ymax - (row * stepy);
+	    longitude = xmin + (col * stepx);
+	    if (not_ll) {
+		if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
+		    G_fatal_error(_("Error in pj_do_proj"));
 		}
-		/* read input map */
-		for(i=0;i<MAXFILES;i++)
-		{
-			if((G_get_raster_row(infd[i],inrast[i],row,in_data_type[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),i);
-			}
+	    }
+	    else {
+		/*Do nothing */
+	    }
+	    d_lat = latitude;
+	    /*End of Calculate Latitude */
+
+	    /* Convert DN 2 radiance 2 reflectance */
+	    for (i = 0; i < MAXFILES; i++) {
+		d[i] = (double)((CELL *) inrast[i])[col];
+		dout[i] =
+		    dn2rad_landsat7(lmin[i], lmax[i], qcalmax[i], qcalmin[i],
+				    d[i]);
+		if (i == 5 || i == 6) {	/*if band 61/62, process brightness temperature */
+		    if (dout[i] <= 0.0) {
+			dout[i] = -999.99;
+		    }
+		    else {
+			dout[i] = tempk_landsat7(dout[i]);
+		    }
 		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			/*Calculate Latitude*/
-			latitude = ymax - ( row * stepy );
-			longitude = xmin + ( col * stepx );
-			if(not_ll){
-				if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
-					G_fatal_error(_("Error in pj_do_proj"));
-				}
-				}else{
-					/*Do nothing*/
-				}
-			d_lat = latitude;
-			/*End of Calculate Latitude*/
+		else {		/*process reflectance */
+		    dout[i] =
+			rad2ref_landsat7(dout[i], doy, sun_elevation,
+					 kexo[i]);
+		}
+	    }
+	    /* End of Convert DN 2 radiance 2 reflectance */
 
-			/* Convert DN 2 radiance 2 reflectance*/
-			for(i=0;i<MAXFILES;i++)
-			{
-				d[i] = (double) ((CELL *) inrast[i])[col];
-				dout[i]=dn2rad_landsat7(lmin[i],lmax[i],qcalmax[i],qcalmin[i],d[i]);
-				if(i==5||i==6){/*if band 61/62, process brightness temperature*/
-					if(dout[i]<=0.0){
-						dout[i]=-999.99;
-					}else{
-						dout[i]=tempk_landsat7(dout[i]);
-					}
-				}else{/*process reflectance*/
-					dout[i]=rad2ref_landsat7(dout[i],doy,sun_elevation,kexo[i]);
-				}
-			}
-			/* End of Convert DN 2 radiance 2 reflectance*/
+	    /* Process regular data */
+	    d_albedo =
+		bb_alb_landsat(dout[0], dout[1], dout[2], dout[3], dout[4],
+			       dout[7]);
+	    d_ndvi = nd_vi(d[2], d[3]);
+	    d_e0 = emissivity_generic(d_ndvi);
+	    d_solar = solar_day(d_lat, (double)doy, tsw);
+	    d_rnetd = r_net_day(d_albedo, d_solar, tsw);
+	    d_tempk = dout[chosen_tempk_band];
+	    d_etpot = et_pot_day(d_albedo, d_solar, d_tempk, tsw, roh_w);
+	    /* End of process regular data */
 
-			/* Process regular data*/
-			d_albedo 	= bb_alb_landsat(dout[0],dout[1],dout[2],dout[3],dout[4],dout[7]);
-			d_ndvi 		= nd_vi(d[2],d[3]);
-			d_e0 		= emissivity_generic( d_ndvi );
-			d_solar 	= solar_day(d_lat, (double) doy, tsw );
-			d_rnetd 	= r_net_day(d_albedo,d_solar,tsw );
-			d_tempk		= dout[chosen_tempk_band];
-			d_etpot		= et_pot_day(d_albedo,d_solar,d_tempk,tsw,roh_w);
-			/* End of process regular data*/
-
-			/* write output to file*/
-			outrast[0][col] = d_etpot;
-			/* End of write output to file*/
-		}
-		if (G_put_raster_row (outfd[0], outrast[0], out_data_type) < 0)
-			G_fatal_error (_("Cannot write to <%s.%i>"),result,0+1);
+	    /* write output to file */
+	    outrast[0][col] = d_etpot;
+	    /* End of write output to file */
 	}
-	G_free (inrast[0]);
-	G_close_cell (infd[0]);
-	G_free (outrast[0]);
-	G_close_cell (outfd[0]);
+	if (G_put_raster_row(outfd[0], outrast[0], out_data_type) < 0)
+	    G_fatal_error(_("Cannot write to <%s.%i>"), result, 0 + 1);
+    }
+    G_free(inrast[0]);
+    G_close_cell(infd[0]);
+    G_free(outrast[0]);
+    G_close_cell(outfd[0]);
 
-	G_command_history(&history);
-	G_write_history(result,&history);
+    G_command_history(&history);
+    G_write_history(result, &history);
 
-	return 0;
+    return 0;
 }

Modified: grass-addons/gipe/i.dn2potrad.l7/ndvi.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/ndvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/ndvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,21 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Normalized Difference Vegetation Index */
-
-
-double nd_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - redchan ) / ( nirchan + redchan );
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Normalized Difference Vegetation Index */ 
+double nd_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan - redchan) / (nirchan + redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/r_net_day.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/r_net_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/r_net_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Net Radiation after Bastiaanssen (1995) */
-
-double r_net_day( double bbalb, double solar, double tsw )
-{
-	double result;
-
-	result = ((1.0 - bbalb)*solar)-(110.0*tsw);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Net Radiation after Bastiaanssen (1995) */ 
+double r_net_day(double bbalb, double solar, double tsw) 
+{
+    double result;
+
+    result = ((1.0 - bbalb) * solar) - (110.0 * tsw);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/rad2ref_landsat7.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/rad2ref_landsat7.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/rad2ref_landsat7.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-#define PI 3.1415926
-
-/* Conversion of Radiance to Reflectance for Landsat 7ETM+ */
-
-double rad2ref_landsat7( double radiance, double doy,double sun_elevation, double k_exo )
-{
-	double	result, ds;
-	
-	ds = (1+0.01672*sin(2*PI*(doy-93.5)/365));
-	result = (radiance/((cos((90-sun_elevation)*PI/180)/(PI*ds*ds))*k_exo));
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+#define PI 3.1415926
+    
+    /* Conversion of Radiance to Reflectance for Landsat 7ETM+ */ 
+double rad2ref_landsat7(double radiance, double doy, double sun_elevation,
+			 double k_exo) 
+{
+    double result, ds;
+
+    ds = (1 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365));
+    result =
+	(radiance /
+	 ((cos((90 - sun_elevation) * PI / 180) / (PI * ds * ds)) * k_exo));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/solar_day.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/solar_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/solar_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,26 +1,23 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Solar Diurnal Radiation after Bastiaanssen (1995) */
-
-#define PI 3.1415927
-
-double solar_day(double lat, double doy, double tsw )
-{
-	double ws, cosun, n10_temp, delta, ds, result;
-
-
-	ds = 1.0 + 0.01672 * sin(2*PI*(doy-93.5)/365.0);
-	delta = 0.4093*sin((2*PI*doy/365)-1.39);
-	
-	 n10_temp =  lat * PI / 180.0;
-	 ws = acos(-tan(n10_temp)*tan(delta*PI/180.0));
-	 cosun = ws*sin(delta*PI/180.0)*sin(n10_temp)+cos(delta*PI/180.0)*cos(n10_temp)*sin(ws);
-	 
-	 result = ( cosun * 1367 * tsw ) / ( PI * ds * ds );
-
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Solar Diurnal Radiation after Bastiaanssen (1995) */ 
+    
+#define PI 3.1415927
+double solar_day(double lat, double doy, double tsw) 
+{
+    double ws, cosun, n10_temp, delta, ds, result;
+
+    ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
+    delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    n10_temp = lat * PI / 180.0;
+    ws = acos(-tan(n10_temp) * tan(delta * PI / 180.0));
+    cosun =
+	ws * sin(delta * PI / 180.0) * sin(n10_temp) +
+	cos(delta * PI / 180.0) * cos(n10_temp) * sin(ws);
+    result = (cosun * 1367 * tsw) / (PI * ds * ds);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2potrad.l7/tempk_landsat7.c
===================================================================
--- grass-addons/gipe/i.dn2potrad.l7/tempk_landsat7.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2potrad.l7/tempk_landsat7.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Surface temperature for Landsat 7ETM+ */
-
-double tempk_landsat7( double l6 )
-{
-	double	result;
-	
-	result = 1282.71 / (log ((666.09 / (l6)) + 1.0)) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Surface temperature for Landsat 7ETM+ */ 
+double tempk_landsat7(double l6) 
+{
+    double result;
+
+    result = 1282.71 / (log((666.09 / (l6)) + 1.0));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2ref.ast/main.c
===================================================================
--- grass-addons/gipe/i.dn2ref.ast/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.ast/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,11 +1,12 @@
+
 /****************************************************************************
  *
  * MODULE:       i.dn2ref.ast
- * AUTHOR(S):    Yann Chemin - ychemin at gmail.com
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
  * PURPOSE:      Calculate TOA Reflectance for Aster from DN.
  * 		 Input 9 bands (VNIR and SWIR).
  *
- * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
  *
  *               This program is free software under the GNU Lesser General Public
  *   	    	 License. Read the file COPYING that comes with GRASS for details.
@@ -54,289 +55,310 @@
 
 #define PI 3.1415926
 
-double rad2ref_aster( double radiance, double doy,double sun_elevation, double k_exo );
+double rad2ref_aster(double radiance, double doy, double sun_elevation,
+		     double k_exo);
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-	struct Cell_head cellhd;/*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
+    struct Cell_head cellhd;	/*region+header info */
 
-	struct GModule *module;
-	struct Option *input,*output;
-	struct Option *input1,*input2;
-	
-	struct Flag *flag1, *flag2;
-	struct History history; /*metadata*/
+    char *mapset;		/*mapset name */
 
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input, *output;
+
+    struct Option *input1, *input2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	char *name; /*input raster name*/
-	char *result; /*output raster name*/
-	
-	/*Prepare new names for output files*/
-	char *result0, *result1, *result2, *result3, *result4;
-	char *result5, *result6, *result7, *result8;
-	
-	/*File Descriptors*/
-	int nfiles;
-	int infd[MAXFILES];
-	int outfd[MAXFILES];
+    /* FMEO Declarations**************** */
+    char *name;			/*input raster name */
 
-	char **names;
-	char **ptr;
-	
-	int ok;
-	
-	int i=0,j=0;
-	int radiance=0;
-	
-	void *inrast[MAXFILES];
-	DCELL *outrast[MAXFILES];
-	int data_format; /* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
-	RASTER_MAP_TYPE in_data_type[MAXFILES];
-	RASTER_MAP_TYPE out_data_type = DCELL_TYPE; /* 0=numbers  1=text */
+    char *result;		/*output raster name */
 
-	double gain[MAXFILES], offset[MAXFILES];
-	double kexo[MAXFILES];
-	double doy,sun_elevation;
+    /*Prepare new names for output files */
+    char *result0, *result1, *result2, *result3, *result4;
+
+    char *result5, *result6, *result7, *result8;
+
+    /*File Descriptors */
+    int nfiles;
+
+    int infd[MAXFILES];
+
+    int outfd[MAXFILES];
+
+    char **names;
+
+    char **ptr;
+
+    int ok;
+
+    int i = 0, j = 0;
+
+    int radiance = 0;
+
+    void *inrast[MAXFILES];
+
+    DCELL *outrast[MAXFILES];
+
+    int data_format;		/* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
+
+    RASTER_MAP_TYPE in_data_type[MAXFILES];
+
+    RASTER_MAP_TYPE out_data_type = DCELL_TYPE;	/* 0=numbers  1=text */
+
+    double gain[MAXFILES], offset[MAXFILES];
+
+    double kexo[MAXFILES];
+
+    double doy, sun_elevation;
+
 	/************************************/
 
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->keywords = _("DN, radiance, reflectance, import");
-	module->description =
-		_("Calculates Top of Atmosphere Reflectance from ASTER DN.\n");
+    module = G_define_module();
+    module->keywords = _("DN, radiance, reflectance, import");
+    module->description =
+	_("Calculates Top of Atmosphere Reflectance from ASTER DN.\n");
 
-	/* Define the different options */
+    /* Define the different options */
 
-	input = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input->description= _("Names of ASTER DN layers (9 layers)");
+    input = G_define_standard_option(G_OPT_R_INPUTS);
+    input->description = _("Names of ASTER DN layers (9 layers)");
 
-	input1 = G_define_option() ;
-	input1->key        = _("doy");
-	input1->type       = TYPE_DOUBLE;
-	input1->required   = YES;
-	input1->gisprompt  = _("value, parameter");
-	input1->description= _("Day of Year of satellite overpass [0-366]");
-	
-	input2 = G_define_option() ;
-	input2->key        = _("sun_elevation");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  = _("value, parameter");
-	input2->description= _("Sun elevation angle (degrees, < 90.0)");
-	
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Base name of the output layers (will add .x)");
+    input1 = G_define_option();
+    input1->key = _("doy");
+    input1->type = TYPE_DOUBLE;
+    input1->required = YES;
+    input1->gisprompt = _("value, parameter");
+    input1->description = _("Day of Year of satellite overpass [0-366]");
 
-	/* Define the different flags */
+    input2 = G_define_option();
+    input2->key = _("sun_elevation");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("value, parameter");
+    input2->description = _("Sun elevation angle (degrees, < 90.0)");
 
-	flag1 = G_define_flag() ;
-	flag1->key         = _('r');
-	flag1->description = _("output is radiance (W/m2)");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Base name of the output layers (will add .x)");
 
-	/* FMEO init nfiles */
-	nfiles = 1;
+    /* Define the different flags */
+
+    flag1 = G_define_flag();
+    flag1->key = _('r');
+    flag1->description = _("output is radiance (W/m2)");
+
+    /* FMEO init nfiles */
+    nfiles = 1;
+
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
-	
-	ok = 1;
-	names   = input->answers;
-	ptr     = input->answers;
-	doy	= atof(input1->answer);
-	sun_elevation = atof(input2->answer);
-	result  = output->answer;
-	
-	radiance = (flag1->answer);
+    if (G_parser(argc, argv))
+	exit(-1);
+
+    ok = 1;
+    names = input->answers;
+    ptr = input->answers;
+    doy = atof(input1->answer);
+    sun_elevation = atof(input2->answer);
+    result = output->answer;
+
+    radiance = (flag1->answer);
+
 	/********************/
-	/*Prepare the ouput file names */
+    /*Prepare the ouput file names */
+
 	/********************/
 
-	result0=result;
-	result1=result;
-	result2=result;
-	result3=result;
-	result4=result;
-	result5=result;
-	result6=result;
-	result7=result;
-	result8=result;
-	
-	result0=strcat(result0,".1");
-	result1=strcat(result1,".2");
-	result2=strcat(result2,".3");
-	result3=strcat(result3,".4");
-	result4=strcat(result4,".5");
-	result5=strcat(result5,".6");
-	result6=strcat(result6,".7");
-	result7=strcat(result7,".8");
-	result8=strcat(result8,".9");
+    result0 = result;
+    result1 = result;
+    result2 = result;
+    result3 = result;
+    result4 = result;
+    result5 = result;
+    result6 = result;
+    result7 = result;
+    result8 = result;
 
+    result0 = strcat(result0, ".1");
+    result1 = strcat(result1, ".2");
+    result2 = strcat(result2, ".3");
+    result3 = strcat(result3, ".4");
+    result4 = strcat(result4, ".5");
+    result5 = strcat(result5, ".6");
+    result6 = strcat(result6, ".7");
+    result7 = strcat(result7, ".8");
+    result8 = strcat(result8, ".9");
+
 	/********************/
-	/*Prepare radiance boundaries*/
+    /*Prepare radiance boundaries */
+
 	/********************/
-	
-	gain[0]=L1GAIN;
-	gain[1]=L2GAIN;
-	gain[2]=L3GAIN;
-	gain[3]=L4GAIN;
-	gain[4]=L5GAIN;
-	gain[5]=L6GAIN;
-	gain[6]=L7GAIN;
-	gain[7]=L8GAIN;
-	gain[8]=L9GAIN;
 
-	offset[0]=L1OFFS;
-	offset[1]=L2OFFS;
-	offset[2]=L3OFFS;
-	offset[3]=L4OFFS;
-	offset[4]=L5OFFS;
-	offset[5]=L6OFFS;
-	offset[6]=L7OFFS;
-	offset[7]=L8OFFS;
-	offset[8]=L9OFFS;
-	
+    gain[0] = L1GAIN;
+    gain[1] = L2GAIN;
+    gain[2] = L3GAIN;
+    gain[3] = L4GAIN;
+    gain[4] = L5GAIN;
+    gain[5] = L6GAIN;
+    gain[6] = L7GAIN;
+    gain[7] = L8GAIN;
+    gain[8] = L9GAIN;
+
+    offset[0] = L1OFFS;
+    offset[1] = L2OFFS;
+    offset[2] = L3OFFS;
+    offset[3] = L4OFFS;
+    offset[4] = L5OFFS;
+    offset[5] = L6OFFS;
+    offset[6] = L7OFFS;
+    offset[7] = L8OFFS;
+    offset[8] = L9OFFS;
+
 	/********************/
-	/*Prepare sun exo-atm irradiance*/
+    /*Prepare sun exo-atm irradiance */
+
 	/********************/
-	
-	kexo[0]=KEXO1;
-	kexo[1]=KEXO2;
-	kexo[2]=KEXO3;
-	kexo[3]=KEXO4;
-	kexo[4]=KEXO5;
-	kexo[5]=KEXO6;
-	kexo[6]=KEXO7;
-	kexo[7]=KEXO8;
-	kexo[8]=KEXO9;
-	
+
+    kexo[0] = KEXO1;
+    kexo[1] = KEXO2;
+    kexo[2] = KEXO3;
+    kexo[3] = KEXO4;
+    kexo[4] = KEXO5;
+    kexo[5] = KEXO6;
+    kexo[6] = KEXO7;
+    kexo[7] = KEXO8;
+    kexo[8] = KEXO9;
+
 	/********************/
+
 	/********************/
-	for (; *ptr != NULL; ptr++)
-	{
-		if (nfiles >= MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name = *ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name);
-			ok = 0;
-		}
-		if (G_legal_filename (result) < 0)
-		{
-			G_fatal_error (_("[%s] is an illegal name"), result);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd[nfiles] = G_open_cell_old (name, mapset);
-		if (infd[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type[nfiles] = G_raster_map_type(name, mapset);
-		if( (infd[nfiles] = G_open_cell_old(name,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name);
-		}
-		if( (G_get_cellhd(name,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name);
-		}
-		inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
-		nfiles++;
+    for (; *ptr != NULL; ptr++) {
+	if (nfiles >= MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name = *ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name);
+	    ok = 0;
 	}
-	nfiles--;
-	if (nfiles <= 1){
-		G_fatal_error(_("The input band number should be 6"));
+	if (G_legal_filename(result) < 0) {
+	    G_fatal_error(_("[%s] is an illegal name"), result);
+	    ok = 0;
 	}
+	if (!ok) {
+	    continue;
+	}
+	infd[nfiles] = G_open_cell_old(name, mapset);
+	if (infd[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd[nfiles] = G_open_cell_old(name, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name);
+	}
+	if ((G_get_cellhd(name, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name);
+	}
+	inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 1) {
+	G_fatal_error(_("The input band number should be 6"));
+    }
+
 	/***************************************************/
-	/* Allocate output buffer, use input map data_type */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	out_data_type=DCELL_TYPE;
-	for(i=0;i<nfiles;i++){
-		outrast[i] = G_allocate_raster_buf(out_data_type);
+    /* Allocate output buffer, use input map data_type */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    out_data_type = DCELL_TYPE;
+    for (i = 0; i < nfiles; i++) {
+	outrast[i] = G_allocate_raster_buf(out_data_type);
+    }
+    if ((outfd[0] = G_open_raster_new(result0, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result0);
+    if ((outfd[1] = G_open_raster_new(result1, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if ((outfd[2] = G_open_raster_new(result2, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result2);
+    if ((outfd[3] = G_open_raster_new(result3, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result3);
+    if ((outfd[4] = G_open_raster_new(result4, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result4);
+    if ((outfd[5] = G_open_raster_new(result5, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result5);
+    if ((outfd[6] = G_open_raster_new(result6, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result6);
+    if ((outfd[7] = G_open_raster_new(result7, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result7);
+    if ((outfd[8] = G_open_raster_new(result8, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result8);
+    /* Process pixels */
+
+    DCELL dout[MAXFILES];
+
+    DCELL d[MAXFILES];
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read input map */
+	for (i = 1; i <= nfiles; i++) {
+	    if ((G_get_raster_row(infd[i], inrast[i], row, in_data_type[i])) <
+		0) {
+		G_fatal_error(_("Could not read from <%s>"), name);
+	    }
 	}
-	if ( (outfd[0] = G_open_raster_new (result0,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result0);
-	if ( (outfd[1] = G_open_raster_new (result1,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result1);
-	if ( (outfd[2] = G_open_raster_new (result2,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result2);
-	if ( (outfd[3] = G_open_raster_new (result3,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result3);
-	if ( (outfd[4] = G_open_raster_new (result4,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result4);
-	if ( (outfd[5] = G_open_raster_new (result5,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result5);
-	if ( (outfd[6] = G_open_raster_new (result6,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result6);
-	if ( (outfd[7] = G_open_raster_new (result7,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result7);
-	if ( (outfd[8] = G_open_raster_new (result8,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result8);
-	/* Process pixels */
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    for (i = 1; i <= nfiles; i++) {
+		switch (in_data_type[i]) {
+		case CELL_TYPE:
+		    d[i] = (double)((CELL *) inrast[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d[i] = (double)((FCELL *) inrast[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d[i] = (double)((DCELL *) inrast[i])[col];
+		    break;
+		}
 
-	DCELL dout[MAXFILES];
-	DCELL d[MAXFILES];
-	
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		/* read input map */
-		for (i=1;i<=nfiles;i++)
-		{
-			if( (G_get_raster_row(infd[i],inrast[i],row,in_data_type[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name);
-			}
+		if (radiance) {
+		    dout[i] = gain[i] * d[i] + offset[i];
 		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			for(i=1;i<=nfiles;i++)
-			{
-				switch(in_data_type[i])
-				{
-					case CELL_TYPE:
-						d[i] = (double) ((CELL *) inrast[i])[col];
-						break;
-					case FCELL_TYPE:
-						d[i] = (double) ((FCELL *) inrast[i])[col];
-						break;
-					case DCELL_TYPE:
-						d[i] = (double) ((DCELL *) inrast[i])[col];
-						break;
-				}
-			
-			if(radiance){
-				dout[i] = gain[i]*d[i]+offset[i];
-			} else {
-				dout[i] = gain[i]*d[i]+offset[i];
-				dout[i]	= rad2ref_aster(dout[i],doy,sun_elevation,kexo[i]);
-			}
-			outrast[i][col] = dout[i];
- 			}
+		else {
+		    dout[i] = gain[i] * d[i] + offset[i];
+		    dout[i] =
+			rad2ref_aster(dout[i], doy, sun_elevation, kexo[i]);
 		}
-		for(i=0;i<nfiles;i++){
-			if (G_put_raster_row (outfd[i], outrast[i], out_data_type) < 0)
-				G_fatal_error (_("Cannot write to <%s.%i>"),result,i+1);
-		}
+		outrast[i][col] = dout[i];
+	    }
 	}
-	for (i=1;i<=nfiles;i++)
-	{
-		G_free (inrast[i]);
-		G_close_cell (infd[i]);
-		G_free (outrast[i]);
-		G_close_cell (outfd[i]);
+	for (i = 0; i < nfiles; i++) {
+	    if (G_put_raster_row(outfd[i], outrast[i], out_data_type) < 0)
+		G_fatal_error(_("Cannot write to <%s.%i>"), result, i + 1);
 	}
-	return 0;
+    }
+    for (i = 1; i <= nfiles; i++) {
+	G_free(inrast[i]);
+	G_close_cell(infd[i]);
+	G_free(outrast[i]);
+	G_close_cell(outfd[i]);
+    }
+    return 0;
 }

Modified: grass-addons/gipe/i.dn2ref.ast/rad2ref_aster.c
===================================================================
--- grass-addons/gipe/i.dn2ref.ast/rad2ref_aster.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.ast/rad2ref_aster.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-#define PI 3.1415926
-
-/* Conversion of Radiance to Reflectance for ASTER */
-
-double rad2ref_aster( double radiance, double doy,double sun_elevation, double k_exo )
-{
-	double	result, ds;
-	
-	ds = (1+0.01672*sin(2*PI*(doy-93.5)/365));
-	result = (radiance/((cos((90-sun_elevation)*PI/180)/(PI*ds*ds))*k_exo));
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+#define PI 3.1415926
+    
+    /* Conversion of Radiance to Reflectance for ASTER */ 
+double rad2ref_aster(double radiance, double doy, double sun_elevation,
+		      double k_exo) 
+{
+    double result, ds;
+
+    ds = (1 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365));
+    result =
+	(radiance /
+	 ((cos((90 - sun_elevation) * PI / 180) / (PI * ds * ds)) * k_exo));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2ref.l7/date2doy.c
===================================================================
--- grass-addons/gipe/i.dn2ref.l7/date2doy.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.l7/date2doy.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,66 +1,70 @@
+
 /*********************************************/
-/*This program converts day/month/year to doy*/
+/*This program converts day/month/year to doy */
+
 /*********************************************/
+
 /*********************************************/
 
 int date2doy(int day, int month, int year)
 {
-	int leap=0;
-	int day_month_tot=0;
-	int doy;
+    int leap = 0;
 
-	doy=0;	
+    int day_month_tot = 0;
 
-/*printf("Date is %i/%i/%i\n", day, month, year);*/
+    int doy;
 
-	if (month == 1) {
-		day_month_tot = 0;
-	}
-	else if (month == 2) {
-		day_month_tot = 31;
-	}
-	else if (month == 3) {
-		day_month_tot = 59;
-	}
-	else if (month == 4) {
-		day_month_tot = 90;
-	}
-	else if (month == 5) {
-		day_month_tot = 120;
-	}
-	else if (month == 6) {
-		day_month_tot = 151;
-	}
-	else if (month == 7) {
-		day_month_tot = 181;
-	}
-	else if (month == 8) {
-		day_month_tot = 212;
-	}
-	else if (month == 9) {
-		day_month_tot = 243;
-	}
-	else if (month == 10) {
-		day_month_tot = 273;
-	}
-	else if (month == 11) {
-		day_month_tot = 304;
-	}
-	else if (month == 12) {
-		day_month_tot = 334;
-	}
-	
-	/* Leap year if dividing by 4 leads % 0.0*/
-	
-	if (year/4*4 == year) {
-		leap = 1;
-	}
-	
-	doy=day_month_tot+day;
-	if(doy>59){
-		doy=day_month_tot+day+leap;
-	}
+    doy = 0;
 
-	return(doy);
+    /*printf("Date is %i/%i/%i\n", day, month, year); */
+
+    if (month == 1) {
+	day_month_tot = 0;
+    }
+    else if (month == 2) {
+	day_month_tot = 31;
+    }
+    else if (month == 3) {
+	day_month_tot = 59;
+    }
+    else if (month == 4) {
+	day_month_tot = 90;
+    }
+    else if (month == 5) {
+	day_month_tot = 120;
+    }
+    else if (month == 6) {
+	day_month_tot = 151;
+    }
+    else if (month == 7) {
+	day_month_tot = 181;
+    }
+    else if (month == 8) {
+	day_month_tot = 212;
+    }
+    else if (month == 9) {
+	day_month_tot = 243;
+    }
+    else if (month == 10) {
+	day_month_tot = 273;
+    }
+    else if (month == 11) {
+	day_month_tot = 304;
+    }
+    else if (month == 12) {
+	day_month_tot = 334;
+    }
+
+    /* Leap year if dividing by 4 leads % 0.0 */
+
+    if (year / 4 * 4 == year) {
+	leap = 1;
+    }
+
+    doy = day_month_tot + day;
+    if (doy > 59) {
+	doy = day_month_tot + day + leap;
+    }
+
+    return (doy);
 }
-

Modified: grass-addons/gipe/i.dn2ref.l7/dn2rad_landsat7.c
===================================================================
--- grass-addons/gipe/i.dn2ref.l7/dn2rad_landsat7.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.l7/dn2rad_landsat7.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,20 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*
- * Conversion of DN to Radiance for Landsat 7ETM+
- * http://ltpwww.gsfc.nasa.gov/IAS/handbook/handbook_htmls/chapter11/chapter11.html#section11.3 
- */
-
-double dn2rad_landsat7( double Lmin, double LMax, double QCalMax, double QCalmin, int DN )
-{
-	double	result, gain, offset;
-	
-	gain 	= (LMax-Lmin)/(QCalMax-QCalmin);
-	offset 	= Lmin;
-	result 	= gain * ((double) DN - QCalmin) + offset ;
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*
+     * Conversion of DN to Radiance for Landsat 7ETM+
+     * http://ltpwww.gsfc.nasa.gov/IAS/handbook/handbook_htmls/chapter11/chapter11.html#section11.3 
+     */ 
+double dn2rad_landsat7(double Lmin, double LMax, double QCalMax,
+			double QCalmin, int DN) 
+{
+    double result, gain, offset;
+
+    gain = (LMax - Lmin) / (QCalMax - QCalmin);
+    offset = Lmin;
+    result = gain * ((double)DN - QCalmin) + offset;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.dn2ref.l7/l7inread.c
===================================================================
--- grass-addons/gipe/i.dn2ref.l7/l7inread.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.l7/l7inread.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,315 +3,280 @@
 #include<stdlib.h>
 #include<string.h>
 
-/*#define MAXFILES 9*/
+/*#define MAXFILES 9 */
 
-int l7_in_read(char *met_file, double *lmin,double *lmax,double *qcalmin,double *qcalmax,double *sun_elevation, double *sun_azimuth,int *day, int *month, int *year)
+int l7_in_read(char *met_file, double *lmin, double *lmax, double *qcalmin,
+	       double *qcalmax, double *sun_elevation, double *sun_azimuth,
+	       int *day, int *month, int *year)
 {
-	FILE *f;
-	char s[1000], *ptr;
-	int i=0;
-	char *p;
+    FILE *f;
 
-	f=fopen(met_file,"r");
+    char s[1000], *ptr;
 
-	if (!f)
+    int i = 0;
+
+    char *p;
+
+    f = fopen(met_file, "r");
+
+    if (!f)
 	return 1;
 
-	while (fgets(s,1000,f)!=NULL)
-	{
-		ptr = strstr(s, "ACQUISITION_DATE");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =-");
-			*year = atoi(p);
-			p = strtok(NULL, "-");
-			*month = atoi(p);
-			p = strtok(NULL, "-");
-			*day = atoi(p);
-		}
-		ptr = strstr(s, "SUN_AZIMUTH");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			*sun_azimuth = atof(p);
-		
-		}
-		ptr = strstr(s, "SUN_ELEVATION");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			*sun_elevation = atof(p);
+    while (fgets(s, 1000, f) != NULL) {
+	ptr = strstr(s, "ACQUISITION_DATE");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =-");
+	    *year = atoi(p);
+	    p = strtok(NULL, "-");
+	    *month = atoi(p);
+	    p = strtok(NULL, "-");
+	    *day = atoi(p);
+	}
+	ptr = strstr(s, "SUN_AZIMUTH");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    *sun_azimuth = atof(p);
 
-		}
-		ptr = strstr(s, "\tLMAX_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[0] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[0] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[1] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[1] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[2] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[2] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[3] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[3] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[4] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[4] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[5] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[5] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[6] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[6] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[7] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[7] = atof(p);
-		}
-		ptr = strstr(s, "\tLMAX_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmax[8] = atof(p);
-		}
-		ptr = strstr(s, "\tLMIN_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			lmin[8] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[0] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND1");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[0] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[1] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND2");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[1] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[2] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND3");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[2] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[3] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND4");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[3] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[4] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND5");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[4] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[5] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND61");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[5] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[6] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND62");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[6] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[7] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND7");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[7] = atof(p);
-		}
-		ptr = strstr(s, "QCALMAX_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmax[8] = atof(p);
-		}
-		ptr = strstr(s, "QCALMIN_BAND8");
-		if (ptr != NULL)
-		{
-			p = strtok(ptr, " =");
-			p = strtok(NULL, " =");
-			qcalmin[8] = atof(p);
-		}
 	}
+	ptr = strstr(s, "SUN_ELEVATION");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    *sun_elevation = atof(p);
 
-/*	printf("year = %i\n", year);
-	printf("month = %i\n", month);
-	printf("day = %i\n", day);
-	printf("sun azimuth = %f\n", sun_azimuth);
-	printf("sun elevation = %f\n", sun_elevation);
-	for (i=0;i<MAXFILES;i++){
-		printf("lmin[%i]=%f\n",i,lmin[i]);
-		printf("lmax[%i]=%f\n",i,lmax[i]);
-		printf("qcalmin[%i]=%f\n",i,qcalmin[i]);
-		printf("qcalmax[%i]=%f\n",i,qcalmax[i]);
 	}
-*/
-	(void)fclose(f);
-	return;
+	ptr = strstr(s, "\tLMAX_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[0] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[0] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[1] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[1] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[2] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[2] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[3] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[3] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[4] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[4] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[5] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[5] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[6] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[6] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[7] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[7] = atof(p);
+	}
+	ptr = strstr(s, "\tLMAX_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmax[8] = atof(p);
+	}
+	ptr = strstr(s, "\tLMIN_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    lmin[8] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[0] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND1");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[0] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[1] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND2");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[1] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[2] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND3");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[2] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[3] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND4");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[3] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[4] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND5");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[4] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[5] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND61");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[5] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[6] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND62");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[6] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[7] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND7");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[7] = atof(p);
+	}
+	ptr = strstr(s, "QCALMAX_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmax[8] = atof(p);
+	}
+	ptr = strstr(s, "QCALMIN_BAND8");
+	if (ptr != NULL) {
+	    p = strtok(ptr, " =");
+	    p = strtok(NULL, " =");
+	    qcalmin[8] = atof(p);
+	}
+    }
+
+    /*      printf("year = %i\n", year);
+       printf("month = %i\n", month);
+       printf("day = %i\n", day);
+       printf("sun azimuth = %f\n", sun_azimuth);
+       printf("sun elevation = %f\n", sun_elevation);
+       for (i=0;i<MAXFILES;i++){
+       printf("lmin[%i]=%f\n",i,lmin[i]);
+       printf("lmax[%i]=%f\n",i,lmax[i]);
+       printf("qcalmin[%i]=%f\n",i,qcalmin[i]);
+       printf("qcalmax[%i]=%f\n",i,qcalmax[i]);
+       }
+     */
+    (void)fclose(f);
+    return;
 }

Modified: grass-addons/gipe/i.dn2ref.l7/main.c
===================================================================
--- grass-addons/gipe/i.dn2ref.l7/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.l7/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.dn2ref.l7
@@ -23,7 +24,7 @@
 
 #define MAXFILES 7
 
-/*sun exo-atmospheric irradiance*/
+/*sun exo-atmospheric irradiance */
 #define KEXO1 1969.0
 #define KEXO2 1840.0
 #define KEXO3 1551.0
@@ -32,274 +33,310 @@
 #define KEXO7 82.07
 
 #define PI 3.1415926
-int l7_in_read(char *met_file, double *lmin,double *lmax,double *qcalmin,double *qcalmax,double *sun_elevation, double *sun_azimuth,int *day, int *month, int *year);
+int l7_in_read(char *met_file, double *lmin, double *lmax, double *qcalmin,
+	       double *qcalmax, double *sun_elevation, double *sun_azimuth,
+	       int *day, int *month, int *year);
 int date2doy(int day, int month, int year);
-double dn2rad_landsat7( double Lmin, double LMax, double QCalMax, double QCalmin, int DN );
-double rad2ref_landsat7( double radiance, double doy,double sun_elevation, double k_exo );
 
-int
-main(int argc, char *argv[])
+double dn2rad_landsat7(double Lmin, double LMax, double QCalMax,
+		       double QCalmin, int DN);
+double rad2ref_landsat7(double radiance, double doy, double sun_elevation,
+			double k_exo);
+
+int main(int argc, char *argv[])
 {
-	struct Cell_head cellhd;/*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
+    struct Cell_head cellhd;	/*region+header info */
 
-	struct GModule *module;
-	struct Option *input,*output;
-	struct Option *input1,*input2;
-	
-	struct Flag *flag1, *flag2;
-	struct History history; /*metadata*/
+    char *mapset;		/*mapset name */
 
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input, *output;
+
+    struct Option *input1, *input2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	char *name; /*input raster name*/
-	char *result; /*output raster name*/
-	
-	/*Prepare new names for output files*/
-	char *result0, *result1, *result2, *result3, *result4;
-	char *result5;
-	
-	/*File Descriptors*/
-	int nfiles;
-	int infd[MAXFILES];
-	int outfd[MAXFILES];
+    /* FMEO Declarations**************** */
+    char *name;			/*input raster name */
 
-	char **names;
-	char **ptr;
-	
-	int ok;
-	
-	int i=0,j=0;
-	int radiance=0;
-	
-	void *inrast[MAXFILES];
-	DCELL *outrast[MAXFILES];
-	int data_format; /* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
-	RASTER_MAP_TYPE in_data_type[MAXFILES];
-	RASTER_MAP_TYPE out_data_type = DCELL_TYPE; /* 0=numbers  1=text */
+    char *result;		/*output raster name */
 
-	double		kexo[MAXFILES];
-	/*Metfile*/
-	char		*metfName; /*met file, header in text format*/
-	double 		lmin[MAXFILES+2];
-	double 		lmax[MAXFILES+2];
-	double 		qcalmin[MAXFILES+2];
-	double 		qcalmax[MAXFILES+2];
-	double 		sun_elevation;
-	double 		sun_azimuth;/*not useful here, only for parser()*/
-	int 		day,month,year;	
-	/*EndofMetfile*/
-	int 		doy;
+    /*Prepare new names for output files */
+    char *result0, *result1, *result2, *result3, *result4;
+
+    char *result5;
+
+    /*File Descriptors */
+    int nfiles;
+
+    int infd[MAXFILES];
+
+    int outfd[MAXFILES];
+
+    char **names;
+
+    char **ptr;
+
+    int ok;
+
+    int i = 0, j = 0;
+
+    int radiance = 0;
+
+    void *inrast[MAXFILES];
+
+    DCELL *outrast[MAXFILES];
+
+    int data_format;		/* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
+
+    RASTER_MAP_TYPE in_data_type[MAXFILES];
+
+    RASTER_MAP_TYPE out_data_type = DCELL_TYPE;	/* 0=numbers  1=text */
+
+    double kexo[MAXFILES];
+
+    /*Metfile */
+    char *metfName;		/*met file, header in text format */
+
+    double lmin[MAXFILES + 2];
+
+    double lmax[MAXFILES + 2];
+
+    double qcalmin[MAXFILES + 2];
+
+    double qcalmax[MAXFILES + 2];
+
+    double sun_elevation;
+
+    double sun_azimuth;		/*not useful here, only for parser() */
+
+    int day, month, year;
+
+    /*EndofMetfile */
+    int doy;
+
 	/************************************/
 
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->keywords = _("DN, radiance, reflectance, import");
-	module->description =
-		_("Calculates Top of Atmosphere Reflectance from Landsat 7 DN.\n");
+    module = G_define_module();
+    module->keywords = _("DN, radiance, reflectance, import");
+    module->description =
+	_("Calculates Top of Atmosphere Reflectance from Landsat 7 DN.\n");
 
-	/* Define the different options */
-	input = G_define_option() ;
-	input->key        = _("metfile");
-	input->type       = TYPE_STRING;
-	input->required   = YES;
-	input->gisprompt  = _(".met file");
-	input->description= _("Landsat 7ETM+ Header File (.met)");
+    /* Define the different options */
+    input = G_define_option();
+    input->key = _("metfile");
+    input->type = TYPE_STRING;
+    input->required = YES;
+    input->gisprompt = _(".met file");
+    input->description = _("Landsat 7ETM+ Header File (.met)");
 
-	input1 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input1->description= _("Names of L7 DN layers (1,2,3,4,5,7)");
+    input1 = G_define_standard_option(G_OPT_R_INPUTS);
+    input1->description = _("Names of L7 DN layers (1,2,3,4,5,7)");
 
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Base name of the output layers (will add .x)");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Base name of the output layers (will add .x)");
 
-	/* Define the different flags */
+    /* Define the different flags */
 
-	flag1 = G_define_flag() ;
-	flag1->key         = _('r');
-	flag1->description = _("output is radiance (W/m2)");
+    flag1 = G_define_flag();
+    flag1->key = _('r');
+    flag1->description = _("output is radiance (W/m2)");
 
-	/* FMEO init nfiles */
-	nfiles = 1;
+    /* FMEO init nfiles */
+    nfiles = 1;
+
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
-	
-	ok = 1;
-	metfName = input->answer;
-	names   = input1->answers;
-	ptr     = input1->answers;
+    if (G_parser(argc, argv))
+	exit(-1);
 
-	result  = output->answer;
-	
-	radiance = (flag1->answer);
+    ok = 1;
+    metfName = input->answer;
+    names = input1->answers;
+    ptr = input1->answers;
+
+    result = output->answer;
+
+    radiance = (flag1->answer);
+
 	/********************/
-	/*Prepare the ouput file names */
+    /*Prepare the ouput file names */
+
 	/********************/
 
-	result0=result;
-	result1=result;
-	result2=result;
-	result3=result;
-	result4=result;
-	result5=result;
-	
-	result0=strcat(result0,".1");
-	result1=strcat(result1,".2");
-	result2=strcat(result2,".3");
-	result3=strcat(result3,".4");
-	result4=strcat(result4,".5");
-	result5=strcat(result5,".7");
+    result0 = result;
+    result1 = result;
+    result2 = result;
+    result3 = result;
+    result4 = result;
+    result5 = result;
 
+    result0 = strcat(result0, ".1");
+    result1 = strcat(result1, ".2");
+    result2 = strcat(result2, ".3");
+    result3 = strcat(result3, ".4");
+    result4 = strcat(result4, ".5");
+    result5 = strcat(result5, ".7");
+
 	/********************/
-	/*Prepare sun exo-atm irradiance*/
+    /*Prepare sun exo-atm irradiance */
+
 	/********************/
-	
-	kexo[0]=KEXO1;
-	kexo[1]=KEXO2;
-	kexo[2]=KEXO3;
-	kexo[3]=KEXO4;
-	kexo[4]=KEXO5;
-	kexo[5]=KEXO7;
-	
+
+    kexo[0] = KEXO1;
+    kexo[1] = KEXO2;
+    kexo[2] = KEXO3;
+    kexo[3] = KEXO4;
+    kexo[4] = KEXO5;
+    kexo[5] = KEXO7;
+
 	/******************************************/
-	/*Fetch parameters for DN2Rad2Ref correction*/
-	l7_in_read(metfName,lmin,lmax,qcalmin,qcalmax,&sun_elevation,&sun_azimuth,&day,&month,&year);
-	doy = date2doy(day,month,year);
-/*	printf("%f/%f/%i-%i-%i\n",sun_elevation,sun_azimuth,day,month,year);
-	for(i=0;i<MAXFILES;i++){
-	printf("%i=>%f, %f, %f, %f\n",i,lmin[i],lmax[i],qcalmin[i],qcalmax[i]);
-	}
-	printf("doy=%i\n",doy);
+    /*Fetch parameters for DN2Rad2Ref correction */
+    l7_in_read(metfName, lmin, lmax, qcalmin, qcalmax, &sun_elevation,
+	       &sun_azimuth, &day, &month, &year);
+    doy = date2doy(day, month, year);
+    /*      printf("%f/%f/%i-%i-%i\n",sun_elevation,sun_azimuth,day,month,year);
+       for(i=0;i<MAXFILES;i++){
+       printf("%i=>%f, %f, %f, %f\n",i,lmin[i],lmax[i],qcalmin[i],qcalmax[i]);
+       }
+       printf("doy=%i\n",doy);
 
-	Remap calibration parameters for this program
-	copy layer 8 (band 7) to layer 6
-	because we only use band 1,2,3,4,5,7
-	Change this if you use more bands
-	Remove this if you use all bands
-*/
-	lmin[5]=lmin[7];
-	lmax[5]=lmax[7];
-	qcalmin[5]=qcalmin[7];
-	qcalmax[5]=qcalmax[7];
+       Remap calibration parameters for this program
+       copy layer 8 (band 7) to layer 6
+       because we only use band 1,2,3,4,5,7
+       Change this if you use more bands
+       Remove this if you use all bands
+     */
+    lmin[5] = lmin[7];
+    lmax[5] = lmax[7];
+    qcalmin[5] = qcalmin[7];
+    qcalmax[5] = qcalmax[7];
+
 	/********************/
-	for (; *ptr != NULL; ptr++)
-	{
-		if (nfiles >= MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name = *ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name);
-			ok = 0;
-		}
-		if (G_legal_filename (result) < 0)
-		{
-			G_fatal_error (_("[%s] is an illegal name"), result);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd[nfiles] = G_open_cell_old (name, mapset);
-		if (infd[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type[nfiles] = G_raster_map_type(name, mapset);
-		if( (infd[nfiles] = G_open_cell_old(name,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name);
-		}
-		if( (G_get_cellhd(name,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name);
-		}
-		inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
-		nfiles++;
+    for (; *ptr != NULL; ptr++) {
+	if (nfiles >= MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name = *ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name);
+	    ok = 0;
 	}
-	nfiles--;
-	if (nfiles <= 1){
-		G_fatal_error(_("The input band number should be 6"));
+	if (G_legal_filename(result) < 0) {
+	    G_fatal_error(_("[%s] is an illegal name"), result);
+	    ok = 0;
 	}
-	
+	if (!ok) {
+	    continue;
+	}
+	infd[nfiles] = G_open_cell_old(name, mapset);
+	if (infd[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd[nfiles] = G_open_cell_old(name, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name);
+	}
+	if ((G_get_cellhd(name, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name);
+	}
+	inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 1) {
+	G_fatal_error(_("The input band number should be 6"));
+    }
+
 	/***************************************************/
-	/* Allocate output buffer, use input map data_type */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	out_data_type=DCELL_TYPE;
-	for(i=0;i<nfiles;i++){
-		outrast[i] = G_allocate_raster_buf(out_data_type);
+    /* Allocate output buffer, use input map data_type */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    out_data_type = DCELL_TYPE;
+    for (i = 0; i < nfiles; i++) {
+	outrast[i] = G_allocate_raster_buf(out_data_type);
+    }
+    if ((outfd[0] = G_open_raster_new(result0, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result0);
+    if ((outfd[1] = G_open_raster_new(result1, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if ((outfd[2] = G_open_raster_new(result2, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result2);
+    if ((outfd[3] = G_open_raster_new(result3, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result3);
+    if ((outfd[4] = G_open_raster_new(result4, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result4);
+    if ((outfd[5] = G_open_raster_new(result5, 1)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result5);
+    /* Process pixels */
+
+    DCELL dout[MAXFILES];
+
+    DCELL d[MAXFILES];
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read input map */
+	for (i = 1; i <= nfiles; i++) {
+	    if ((G_get_raster_row(infd[i], inrast[i], row, in_data_type[i])) <
+		0) {
+		G_fatal_error(_("Could not read from <%s>"), name);
+	    }
 	}
-	if ( (outfd[0] = G_open_raster_new (result0,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result0);
-	if ( (outfd[1] = G_open_raster_new (result1,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result1);
-	if ( (outfd[2] = G_open_raster_new (result2,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result2);
-	if ( (outfd[3] = G_open_raster_new (result3,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result3);
-	if ( (outfd[4] = G_open_raster_new (result4,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result4);
-	if ( (outfd[5] = G_open_raster_new (result5,1)) < 0)
-		G_fatal_error (_("Could not open <%s>"),result5);
-	/* Process pixels */
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    for (i = 1; i <= nfiles; i++) {
+		switch (in_data_type[i]) {
+		case CELL_TYPE:
+		    d[i] = (double)((CELL *) inrast[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d[i] = (double)((FCELL *) inrast[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d[i] = (double)((DCELL *) inrast[i])[col];
+		    break;
+		}
 
-	DCELL dout[MAXFILES];
-	DCELL d[MAXFILES];
-	
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		/* read input map */
-		for (i=1;i<=nfiles;i++)
-		{
-			if( (G_get_raster_row(infd[i],inrast[i],row,in_data_type[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name);
-			}
+		if (radiance) {
+		    dout[i] =
+			dn2rad_landsat7(lmin[i], lmax[i], qcalmax[i],
+					qcalmin[i], d[i]);
 		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			for(i=1;i<=nfiles;i++)
-			{
-				switch(in_data_type[i])
-				{
-					case CELL_TYPE:
-						d[i] = (double) ((CELL *) inrast[i])[col];
-						break;
-					case FCELL_TYPE:
-						d[i] = (double) ((FCELL *) inrast[i])[col];
-						break;
-					case DCELL_TYPE:
-						d[i] = (double) ((DCELL *) inrast[i])[col];
-						break;
-				}
-			
-			if(radiance){
-				dout[i]=dn2rad_landsat7(lmin[i],lmax[i],qcalmax[i],qcalmin[i],d[i]);
-			} else {
-				dout[i]=dn2rad_landsat7(lmin[i],lmax[i],qcalmax[i],qcalmin[i],d[i]);
-				dout[i]=rad2ref_landsat7(dout[i],doy,sun_elevation,kexo[i]);
-			}
-			outrast[i][col] = dout[i];
- 			}
+		else {
+		    dout[i] =
+			dn2rad_landsat7(lmin[i], lmax[i], qcalmax[i],
+					qcalmin[i], d[i]);
+		    dout[i] =
+			rad2ref_landsat7(dout[i], doy, sun_elevation,
+					 kexo[i]);
 		}
-		for(i=0;i<nfiles;i++){
-			if (G_put_raster_row (outfd[i], outrast[i], out_data_type) < 0)
-				G_fatal_error (_("Cannot write to <%s.%i>"),result,i+1);
-		}
+		outrast[i][col] = dout[i];
+	    }
 	}
-	for (i=1;i<=nfiles;i++)
-	{
-		G_free (inrast[i]);
-		G_close_cell (infd[i]);
-		G_free (outrast[i]);
-		G_close_cell (outfd[i]);
+	for (i = 0; i < nfiles; i++) {
+	    if (G_put_raster_row(outfd[i], outrast[i], out_data_type) < 0)
+		G_fatal_error(_("Cannot write to <%s.%i>"), result, i + 1);
 	}
-	return 0;
+    }
+    for (i = 1; i <= nfiles; i++) {
+	G_free(inrast[i]);
+	G_close_cell(infd[i]);
+	G_free(outrast[i]);
+	G_close_cell(outfd[i]);
+    }
+    return 0;
 }

Modified: grass-addons/gipe/i.dn2ref.l7/rad2ref_landsat7.c
===================================================================
--- grass-addons/gipe/i.dn2ref.l7/rad2ref_landsat7.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.dn2ref.l7/rad2ref_landsat7.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-#define PI 3.1415926
-
-/* Conversion of Radiance to Reflectance for Landsat 7ETM+ */
-
-double rad2ref_landsat7( double radiance, double doy,double sun_elevation, double k_exo )
-{
-	double	result, ds;
-	
-	ds = (1+0.01672*sin(2*PI*(doy-93.5)/365));
-	result = (radiance/((cos((90-sun_elevation)*PI/180)/(PI*ds*ds))*k_exo));
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+#define PI 3.1415926
+    
+    /* Conversion of Radiance to Reflectance for Landsat 7ETM+ */ 
+double rad2ref_landsat7(double radiance, double doy, double sun_elevation,
+			 double k_exo) 
+{
+    double result, ds;
+
+    ds = (1 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365));
+    result =
+	(radiance /
+	 ((cos((90 - sun_elevation) * PI / 180) / (PI * ds * ds)) * k_exo));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.eb.deltat/delta_t.c
===================================================================
--- grass-addons/gipe/i.eb.deltat/delta_t.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.deltat/delta_t.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -8,13 +8,14 @@
 
 double delta_t(double tempk)
 {
-	double result;
+    double result;
 
-	result = -321.147+1.095*tempk;
-	if(result<1){
-		result=1.0;
-	} else if(result>13){
-		result=13.0;
-	}
-	return result;
+    result = -321.147 + 1.095 * tempk;
+    if (result < 1) {
+	result = 1.0;
+    }
+    else if (result > 13) {
+	result = 13.0;
+    }
+    return result;
 }

Modified: grass-addons/gipe/i.eb.deltat/main.c
===================================================================
--- grass-addons/gipe/i.eb.deltat/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.deltat/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,164 +1,191 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.deltat
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the difference of temperature between 2 heights
- *                as seen in Pawan (2004) 
- *                This is a SEBAL initialization parameter for sensible heat. 
- *
- * COPYRIGHT:    (C) 2006 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double delta_t(double tempk);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int wim=0;
-	struct GModule *module;
-	struct Option *input1, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_tempk;
-	int outfd1;
-	
-	char *tempk;
-	char *delta;	
-	int i=0,j=0;
-	
-	void *inrast_tempk;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_tempk;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("delta T, energy balance, SEBAL");
-	module->description = _("difference of temperature between two heights as seen in Pawan (2004), this is part of sensible heat flux calculations, as in SEBAL (Bastiaanssen, 1995). A 'w' flag allows for a very generic approximation.");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("tempk");
-	input1->description=_("Name of the surface skin temperature map [Kelvin]");
-	input1->answer     =_("tempk");
-
-	output1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	output1->key        =_("delta");
-	output1->description=_("Name of the output delta layer");
-	output1->answer     =_("delta");
-
-	flag2 = G_define_flag();
-	flag2->key = 'w';
-	flag2->description = _("Wim's generic table");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	tempk	 	= input1->answer;
-		
-	result1  	= output1->answer;
-	wim 		= flag2->answer;
-	/***************************************************/
-	mapset = G_find_cell2(tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_tempk;
-		G_percent(row,nrows,2);
-		/* read soil input maps */	
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_tempk)){
-				G_set_d_null_value(&outrast1[col],1);
-			}else {
-				/****************************/
-				/* calculate delta T	    */
-				if(wim){
-					d = 0.3225 * d_tempk - 91.743;
-					if(d<1){
-						d=1.0;
-					} else if(d>13){
-						d=13.0;
-					}
-				}else{
-					d = delta_t(d_tempk);
-				}
-				if(abs(d)>50.0){
-					G_set_d_null_value(&outrast1[col],1);
-				}
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_tempk);
-	G_close_cell (infd_tempk);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.deltat
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the difference of temperature between 2 heights
+ *                as seen in Pawan (2004) 
+ *                This is a SEBAL initialization parameter for sensible heat. 
+ *
+ * COPYRIGHT:    (C) 2006 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double delta_t(double tempk);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int wim = 0;
+
+    struct GModule *module;
+
+    struct Option *input1, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_tempk;
+
+    int outfd1;
+
+    char *tempk;
+
+    char *delta;
+
+    int i = 0, j = 0;
+
+    void *inrast_tempk;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_tempk;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("delta T, energy balance, SEBAL");
+    module->description =
+	_("difference of temperature between two heights as seen in Pawan (2004), this is part of sensible heat flux calculations, as in SEBAL (Bastiaanssen, 1995). A 'w' flag allows for a very generic approximation.");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("tempk");
+    input1->description =
+	_("Name of the surface skin temperature map [Kelvin]");
+    input1->answer = _("tempk");
+    output1 = G_define_standard_option(G_OPT_R_INPUT);
+    output1->key = _("delta");
+    output1->description = _("Name of the output delta layer");
+    output1->answer = _("delta");
+    flag2 = G_define_flag();
+    flag2->key = 'w';
+    flag2->description = _("Wim's generic table");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    tempk = input1->answer;
+    result1 = output1->answer;
+    wim = flag2->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_tempk;
+	G_percent(row, nrows, 2);
+	
+	    /* read soil input maps */ 
+	    if (G_get_raster_row
+		(infd_tempk, inrast_tempk, row, data_type_tempk) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_tempk)) {
+		G_set_d_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		
+
+				/****************************/ 
+		    /* calculate delta T        */ 
+		    if (wim) {
+		    d = 0.3225 * d_tempk - 91.743;
+		    if (d < 1) {
+			d = 1.0;
+		    }
+		    else if (d > 13) {
+			d = 13.0;
+		    }
+		}
+		else {
+		    d = delta_t(d_tempk);
+		}
+		if (abs(d) > 50.0) {
+		    G_set_d_null_value(&outrast1[col], 1);
+		}
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_tempk);
+    G_close_cell(infd_tempk);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.disp/disp.c
===================================================================
--- grass-addons/gipe/i.eb.disp/disp.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.disp/disp.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,13 +2,15 @@
 #include<stdlib.h>
 #include<math.h>
 
-/* Found in Pawan (2004)*/
+/* Found in Pawan (2004) */
 
 double dis_p(double lai)
 {
-	double result;
+    double result;
 
-	result = 1*(1-((1-exp(-(pow(20.6*lai,0.5))))/(pow(20.6*lai,0.5))));
-	
-	return result;
+    result =
+	1 * (1 -
+	     ((1 - exp(-(pow(20.6 * lai, 0.5)))) / (pow(20.6 * lai, 0.5))));
+
+    return result;
 }

Modified: grass-addons/gipe/i.eb.disp/main.c
===================================================================
--- grass-addons/gipe/i.eb.disp/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.disp/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,151 +1,181 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.disp
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the displacement height above skin surface
- *                as seen in Pawan (2004), a flag makes savi2lai instead of
- *                direct LAI input.
- *
- * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-double dis_p(double lai);
-double savi_lai(double savi);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_lai;
-	int outfd1;
-	
-	char *disp;
-	char *lai;	
-	int i=0,j=0;
-	
-	void *inrast_lai;
-	unsigned char *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_lai;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("disp, energy balance, SEBAL");
-	module->description = _("Displacement height above skin surface, as seen in Pawan (2004). A flag (-s) permits direct SAVI input using the equation in the same document, be careful using it.");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("lai");
-	input1->description=_("Name of the LAI map [-]");
-	input1->answer     =_("lai");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("disp");
-	output1->description=_("Name of the output disp layer");
-	output1->answer     =_("disp");
-	
-	flag2 = G_define_flag();
-	flag2->key = 's';
-	flag2->description = _("use savi2lai conversion (Pawan, 2004)");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	lai	 	= input1->answer;
-		
-	result1  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(lai, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), lai);
-	}
-	data_type_lai = G_raster_map_type(lai,mapset);
-	if ( (infd_lai = G_open_cell_old (lai,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), lai);
-	if (G_get_cellhd (lai, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), lai);
-	inrast_lai = G_allocate_raster_buf(data_type_lai);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_lai;
-		G_percent(row,nrows,2);
-		/* read soil input maps */	
-		if(G_get_raster_row(infd_lai,inrast_lai,row,data_type_lai)<0)
-			G_fatal_error(_("Could not read from <%s>"),lai);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			d_lai = ((DCELL *) inrast_lai)[col];
-			if(G_is_d_null_value(&d_lai)){
-				((DCELL *) outrast1)[col] = -999.99;
-			} else {
-				if(flag2->answer){
-					/********************/
-					/* savi2lai()       */
-					d = savi_lai(d_lai);
-					/********************/
-					/* calculate disp   */
-					d = dis_p(d);
-					((DCELL *) outrast1)[col] = d;
-				} else {
-					/********************/
-					/* calculate disp   */
-					d = dis_p(d_lai);
-					((DCELL *) outrast1)[col] = d;
-				}
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_lai);
-	G_close_cell (infd_lai);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.disp
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the displacement height above skin surface
+ *                as seen in Pawan (2004), a flag makes savi2lai instead of
+ *                direct LAI input.
+ *
+ * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double dis_p(double lai);
+
+double savi_lai(double savi);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_lai;
+
+    int outfd1;
+
+    char *disp;
+
+    char *lai;
+
+    int i = 0, j = 0;
+
+    void *inrast_lai;
+
+    unsigned char *outrast1;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_lai;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("disp, energy balance, SEBAL");
+    module->description =
+	_("Displacement height above skin surface, as seen in Pawan (2004). A flag (-s) permits direct SAVI input using the equation in the same document, be careful using it.");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("lai");
+    input1->description = _("Name of the LAI map [-]");
+    input1->answer = _("lai");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("disp");
+    output1->description = _("Name of the output disp layer");
+    output1->answer = _("disp");
+    flag2 = G_define_flag();
+    flag2->key = 's';
+    flag2->description = _("use savi2lai conversion (Pawan, 2004)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    lai = input1->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lai, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), lai);
+    }
+    data_type_lai = G_raster_map_type(lai, mapset);
+    if ((infd_lai = G_open_cell_old(lai, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lai);
+    if (G_get_cellhd(lai, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), lai);
+    inrast_lai = G_allocate_raster_buf(data_type_lai);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_lai;
+	G_percent(row, nrows, 2);
+	
+	    /* read soil input maps */ 
+	    if (G_get_raster_row(infd_lai, inrast_lai, row, data_type_lai) <
+		0)
+	    G_fatal_error(_("Could not read from <%s>"), lai);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    d_lai = ((DCELL *) inrast_lai)[col];
+	    if (G_is_d_null_value(&d_lai)) {
+		((DCELL *) outrast1)[col] = -999.99;
+	    }
+	    else {
+		if (flag2->answer) {
+		    
+
+					/********************/ 
+			/* savi2lai()       */ 
+			d = savi_lai(d_lai);
+		    
+
+					/********************/ 
+			/* calculate disp   */ 
+			d = dis_p(d);
+		    ((DCELL *) outrast1)[col] = d;
+		}
+		else {
+		    
+
+					/********************/ 
+			/* calculate disp   */ 
+			d = dis_p(d_lai);
+		    ((DCELL *) outrast1)[col] = d;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_lai);
+    G_close_cell(infd_lai);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.disp/savi2lai.c
===================================================================
--- grass-addons/gipe/i.eb.disp/savi2lai.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.disp/savi2lai.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,19 +2,21 @@
 #include<stdlib.h>
 #include<math.h>
 
-/* Found in Pawan (2004)*/
+/* Found in Pawan (2004) */
 
 double savi_lai(double savi)
 {
-	double result;
+    double result;
 
-	if (savi < 0.1){
-		result = 0.00001;
-	} else if (savi > 0.689){
-		result = 6.0;
-	} else {
-		result = -(log((0.69-savi)/0.59)/0.91);
-	}
+    if (savi < 0.1) {
+	result = 0.00001;
+    }
+    else if (savi > 0.689) {
+	result = 6.0;
+    }
+    else {
+	result = -(log((0.69 - savi) / 0.59) / 0.91);
+    }
 
-	return result;
+    return result;
 }

Modified: grass-addons/gipe/i.eb.eta/eta.c
===================================================================
--- grass-addons/gipe/i.eb.eta/eta.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.eta/eta.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,13 +3,12 @@
 
 double et_a(double r_net_day, double evap_fr, double tempk)
 {
-	double latent, t_celsius, result;
+    double latent, t_celsius, result;
 
-	t_celsius = tempk - 273.15 ;
-	latent 	  = 86400/((2.501-0.002361*t_celsius)*pow(10,6));
+    t_celsius = tempk - 273.15;
+    latent = 86400 / ((2.501 - 0.002361 * t_celsius) * pow(10, 6));
 
-	result 	  = r_net_day * evap_fr * latent ;
-	
-	return result;
-}
+    result = r_net_day * evap_fr * latent;
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.eta/main.c
===================================================================
--- grass-addons/gipe/i.eb.eta/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.eta/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,213 +1,242 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.eta
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the actual evapotranspiration for diurnal period
- *               as seen in Bastiaanssen (1995) 
- *
- * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double et_a(double r_net_day, double evap_fr, double tempk);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_rnetday, infd_evapfr, infd_tempk;
-	int outfd1;
-	
-	char *rnetday,*evapfr,*tempk;
-	int i=0,j=0;
-	
-	void *inrast_rnetday, *inrast_evapfr, *inrast_tempk;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_rnetday;
-	RASTER_MAP_TYPE data_type_evapfr;
-	RASTER_MAP_TYPE data_type_tempk;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("actual evapotranspiration, energy balance, SEBAL");
-	module->description = _("actual evapotranspiration for diurnal period (Bastiaanssen, 1995)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("rnetday");
-	input1->description=_("Name of the diurnal Net Radiation map [W/m2]");
-	input1->answer     =_("rnetday");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("evapfr");
-	input2->description=_("Name of the evaporative fraction map [-]");
-	input2->answer     =_("evapfr");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("tempk");
-	input3->description=_("Name of the surface skin temperature [K]");
-	input3->answer     =_("tempk");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("eta");
-	output1->description=_("Name of the output actual diurnal evapotranspiration layer");
-	output1->answer     =_("eta");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	rnetday	 	= input1->answer;
-	evapfr	 	= input2->answer;
-	tempk		= input3->answer;
-	
-	result1  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(rnetday, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), rnetday);
-	}
-	data_type_rnetday = G_raster_map_type(rnetday,mapset);
-	if ( (infd_rnetday = G_open_cell_old (rnetday,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), rnetday);
-	if (G_get_cellhd (rnetday, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), rnetday);
-	inrast_rnetday = G_allocate_raster_buf(data_type_rnetday);
-	/***************************************************/
-	mapset = G_find_cell2 (evapfr, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),evapfr);
-	}
-	data_type_evapfr = G_raster_map_type(evapfr,mapset);
-	if ( (infd_evapfr = G_open_cell_old (evapfr,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), evapfr);
-	if (G_get_cellhd (evapfr, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), evapfr);
-	inrast_evapfr = G_allocate_raster_buf(data_type_evapfr);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rnetday;
-		DCELL d_evapfr;
-		DCELL d_tempk;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_rnetday,inrast_rnetday,row,data_type_rnetday)<0)
-			G_fatal_error(_("Could not read from <%s>"),rnetday);
-		if(G_get_raster_row(infd_evapfr,inrast_evapfr,row,data_type_evapfr)<0)
-			G_fatal_error(_("Could not read from <%s>"),evapfr);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_rnetday){
-				case CELL_TYPE:
-					d_rnetday = (double) ((CELL *) inrast_rnetday)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnetday = (double) ((FCELL *) inrast_rnetday)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnetday = ((DCELL *) inrast_rnetday)[col];
-					break;
-			}
-			switch(data_type_evapfr){
-				case CELL_TYPE:
-					d_evapfr = (double) ((CELL *) inrast_evapfr)[col];
-					break;
-				case FCELL_TYPE:
-					d_evapfr = (double) ((FCELL *) inrast_evapfr)[col];
-					break;
-				case DCELL_TYPE:
-					d_evapfr = ((DCELL *) inrast_evapfr)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_rnetday)||
-			G_is_d_null_value(&d_evapfr)||
-			G_is_d_null_value(&d_tempk)){
-				G_set_d_null_value(&outrast1[col],1);
-			}else {
-				/************************************/
-				/* calculate soil heat flux	    */
-				d = et_a(d_rnetday,d_evapfr,d_tempk);
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_rnetday);
-	G_free (inrast_evapfr);
-	G_free (inrast_tempk);
-	G_close_cell (infd_rnetday);
-	G_close_cell (infd_evapfr);
-	G_close_cell (infd_tempk);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.eta
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the actual evapotranspiration for diurnal period
+ *               as seen in Bastiaanssen (1995) 
+ *
+ * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double et_a(double r_net_day, double evap_fr, double tempk);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_rnetday, infd_evapfr, infd_tempk;
+
+    int outfd1;
+
+    char *rnetday, *evapfr, *tempk;
+
+    int i = 0, j = 0;
+
+    void *inrast_rnetday, *inrast_evapfr, *inrast_tempk;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_rnetday;
+    RASTER_MAP_TYPE data_type_evapfr;
+    RASTER_MAP_TYPE data_type_tempk;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("actual evapotranspiration, energy balance, SEBAL");
+    module->description =
+	_("actual evapotranspiration for diurnal period (Bastiaanssen, 1995)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("rnetday");
+    input1->description = _("Name of the diurnal Net Radiation map [W/m2]");
+    input1->answer = _("rnetday");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("evapfr");
+    input2->description = _("Name of the evaporative fraction map [-]");
+    input2->answer = _("evapfr");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("tempk");
+    input3->description = _("Name of the surface skin temperature [K]");
+    input3->answer = _("tempk");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("eta");
+    output1->description =
+	_("Name of the output actual diurnal evapotranspiration layer");
+    output1->answer = _("eta");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    rnetday = input1->answer;
+    evapfr = input2->answer;
+    tempk = input3->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rnetday, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), rnetday);
+    }
+    data_type_rnetday = G_raster_map_type(rnetday, mapset);
+    if ((infd_rnetday = G_open_cell_old(rnetday, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rnetday);
+    if (G_get_cellhd(rnetday, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), rnetday);
+    inrast_rnetday = G_allocate_raster_buf(data_type_rnetday);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(evapfr, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), evapfr);
+    }
+    data_type_evapfr = G_raster_map_type(evapfr, mapset);
+    if ((infd_evapfr = G_open_cell_old(evapfr, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), evapfr);
+    if (G_get_cellhd(evapfr, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), evapfr);
+    inrast_evapfr = G_allocate_raster_buf(data_type_evapfr);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rnetday;
+	DCELL d_evapfr;
+	DCELL d_tempk;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_rnetday, inrast_rnetday, row, data_type_rnetday) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), rnetday);
+	if (G_get_raster_row
+	     (infd_evapfr, inrast_evapfr, row, data_type_evapfr) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), evapfr);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_rnetday) {
+	    case CELL_TYPE:
+		d_rnetday = (double)((CELL *) inrast_rnetday)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnetday = (double)((FCELL *) inrast_rnetday)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnetday = ((DCELL *) inrast_rnetday)[col];
+		break;
+	    }
+	    switch (data_type_evapfr) {
+	    case CELL_TYPE:
+		d_evapfr = (double)((CELL *) inrast_evapfr)[col];
+		break;
+	    case FCELL_TYPE:
+		d_evapfr = (double)((FCELL *) inrast_evapfr)[col];
+		break;
+	    case DCELL_TYPE:
+		d_evapfr = ((DCELL *) inrast_evapfr)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_rnetday) ||
+		 G_is_d_null_value(&d_evapfr) ||
+		 G_is_d_null_value(&d_tempk)) {
+		G_set_d_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate soil heat flux         */ 
+		    d = et_a(d_rnetday, d_evapfr, d_tempk);
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_rnetday);
+    G_free(inrast_evapfr);
+    G_free(inrast_tempk);
+    G_close_cell(infd_rnetday);
+    G_close_cell(infd_evapfr);
+    G_close_cell(infd_tempk);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.evapfr/evap_fr.c
===================================================================
--- grass-addons/gipe/i.eb.evapfr/evap_fr.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.evapfr/evap_fr.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,10 +3,9 @@
 
 double evap_fr(double r_net, double g0, double h0)
 {
-	double result;
+    double result;
 
-	result = (r_net - g0 - h0) / (r_net - g0);
-	
-	return result;
-}
+    result = (r_net - g0 - h0) / (r_net - g0);
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.evapfr/main.c
===================================================================
--- grass-addons/gipe/i.eb.evapfr/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.evapfr/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,258 +1,289 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.evapfr
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the evaporative fraction
- *               as seen in Bastiaanssen (1995) 
- *
- * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double evap_fr(double r_net, double g0, double h0);
-double soilmoisture( double evapfr );
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int makin=0;/*Makin Flag for root zone soil moisture output*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *output1, *output2;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /* input raster name*/
-	char *result1, *result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_rnet, infd_g0, infd_h0;
-	int outfd1, outfd2;
-	
-	char *rnet,*g0,*h0;
-	char *evapfr, *theta;	
-	int i=0,j=0;
-	
-	void *inrast_rnet, *inrast_g0, *inrast_h0;
-	DCELL *outrast1, *outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_rnet;
-	RASTER_MAP_TYPE data_type_g0;
-	RASTER_MAP_TYPE data_type_h0;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("evaporative fraction, soil moisture, energy balance, SEBAL");
-	module->description = _("evaporative fraction (Bastiaanssen, 1995) and root zone soil moisture (Makin, Molden and Bastiaanssen, 2001)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("rnet");
-	input1->description=_("Name of the Net Radiation map [W/m2]");
-	input1->answer     =_("rnet");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("g0");
-	input2->description=_("Name of the soil heat flux map [W/m2]");
-	input2->answer     =_("g0");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("h0");
-	input3->description=_("Name of the sensible heat flux map [W/m2]");
-	input3->answer     =_("h0");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("evapfr");
-	output1->description=_("Name of the output evaporative fraction layer");
-	output1->answer     =_("evapfr");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("theta");
-	output2->required   = NO;
-	output2->description=_("Name of the output root zone soil moisture layer");
-	output2->answer     =_("theta");
-	
-	flag1 = G_define_flag();
-	flag1->key = 'm';
-	flag1->description = _("root zone soil moisture output (Makin, Molden and Bastiaanssen, 2001)");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	rnet	 	= input1->answer;
-	g0	 	= input2->answer;
-	h0		= input3->answer;
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	makin   = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(rnet, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), rnet);
-	}
-	data_type_rnet = G_raster_map_type(rnet,mapset);
-	if ( (infd_rnet = G_open_cell_old (rnet,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), rnet);
-	if (G_get_cellhd (rnet, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), rnet);
-	inrast_rnet = G_allocate_raster_buf(data_type_rnet);
-	/***************************************************/
-	mapset = G_find_cell2 (g0, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),g0);
-	}
-	data_type_g0 = G_raster_map_type(g0,mapset);
-	if ( (infd_g0 = G_open_cell_old (g0,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), g0);
-	if (G_get_cellhd (g0, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), g0);
-	inrast_g0 = G_allocate_raster_buf(data_type_g0);
-	/***************************************************/
-	mapset = G_find_cell2 (h0, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), h0);
-	}
-	data_type_h0 = G_raster_map_type(h0,mapset);
-	if ( (infd_h0 = G_open_cell_old (h0,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), h0);
-	if (G_get_cellhd (h0, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), h0);
-	inrast_h0 = G_allocate_raster_buf(data_type_h0);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(makin){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(makin){
-		if ( (outfd2 = G_open_raster_new (result2,data_type_output)) < 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rnet;
-		DCELL d_g0;
-		DCELL d_h0;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_rnet,inrast_rnet,row,data_type_rnet)<0)
-			G_fatal_error(_("Could not read from <%s>"),rnet);
-		if(G_get_raster_row(infd_g0,inrast_g0,row,data_type_g0)<0)
-			G_fatal_error(_("Could not read from <%s>"),g0);
-		if(G_get_raster_row(infd_h0,inrast_h0,row,data_type_h0)<0)
-			G_fatal_error(_("Could not read from <%s>"),h0);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_rnet){
-				case CELL_TYPE:
-					d_rnet = (double) ((CELL *) inrast_rnet)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnet = (double) ((FCELL *) inrast_rnet)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnet = ((DCELL *) inrast_rnet)[col];
-					break;
-			}
-			switch(data_type_g0){
-				case CELL_TYPE:
-					d_g0 = (double) ((CELL *) inrast_g0)[col];
-					break;
-				case FCELL_TYPE:
-					d_g0 = (double) ((FCELL *) inrast_g0)[col];
-					break;
-				case DCELL_TYPE:
-					d_g0 = ((DCELL *) inrast_g0)[col];
-					break;
-			}
-			switch(data_type_h0){
-				case CELL_TYPE:
-					d_h0 = (double) ((CELL *) inrast_h0)[col];
-					break;
-				case FCELL_TYPE:
-					d_h0 = (double) ((FCELL *) inrast_h0)[col];
-					break;
-				case DCELL_TYPE:
-					d_h0 = ((DCELL *) inrast_h0)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_rnet)||
-			G_is_d_null_value(&d_g0)||
-			G_is_d_null_value(&d_h0)){
-				G_set_d_null_value(&outrast1[col],1);
-				if(makin){
-					G_set_d_null_value(&outrast2[col],1);
-				}
-			}else {
-				/****************************************/
-				/* calculate evaporative fraction	*/
-				d = evap_fr(d_rnet,d_g0,d_h0);
-				outrast1[col] = d;
-				/****************************************/
-				/* calculate soil moisture		*/
-				if(makin){
-					d = soilmoisture(d);
-					outrast2[col] = d;
-				}
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-		if(makin){
-			if (G_put_raster_row (outfd2, outrast2, data_type_output) < 0)
-				G_fatal_error(_("Cannot write to output raster file"));
-		}
-	}
-
-	G_free (inrast_rnet);
-	G_free (inrast_g0);
-	G_free (inrast_h0);
-	G_close_cell (infd_rnet);
-	G_close_cell (infd_g0);
-	G_close_cell (infd_h0);
-	
-	G_free (outrast1);
-	G_free (outrast2);
-	if(makin){
-		G_close_cell (outfd1);
-		G_close_cell (outfd2);
-	}
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	if(makin){
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.evapfr
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the evaporative fraction
+ *               as seen in Bastiaanssen (1995) 
+ *
+ * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double evap_fr(double r_net, double g0, double h0);
+
+double soilmoisture(double evapfr);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int makin = 0;		/*Makin Flag for root zone soil moisture output */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *output1, *output2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/* input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_rnet, infd_g0, infd_h0;
+
+    int outfd1, outfd2;
+
+    char *rnet, *g0, *h0;
+
+    char *evapfr, *theta;
+
+    int i = 0, j = 0;
+
+    void *inrast_rnet, *inrast_g0, *inrast_h0;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_rnet;
+    RASTER_MAP_TYPE data_type_g0;
+    RASTER_MAP_TYPE data_type_h0;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords =
+	_("evaporative fraction, soil moisture, energy balance, SEBAL");
+    module->description =
+	_("evaporative fraction (Bastiaanssen, 1995) and root zone soil moisture (Makin, Molden and Bastiaanssen, 2001)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("rnet");
+    input1->description = _("Name of the Net Radiation map [W/m2]");
+    input1->answer = _("rnet");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("g0");
+    input2->description = _("Name of the soil heat flux map [W/m2]");
+    input2->answer = _("g0");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("h0");
+    input3->description = _("Name of the sensible heat flux map [W/m2]");
+    input3->answer = _("h0");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("evapfr");
+    output1->description =
+	_("Name of the output evaporative fraction layer");
+    output1->answer = _("evapfr");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("theta");
+    output2->required = NO;
+    output2->description =
+	_("Name of the output root zone soil moisture layer");
+    output2->answer = _("theta");
+    flag1 = G_define_flag();
+    flag1->key = 'm';
+    flag1->description =
+	_("root zone soil moisture output (Makin, Molden and Bastiaanssen, 2001)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    rnet = input1->answer;
+    g0 = input2->answer;
+    h0 = input3->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    makin = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rnet, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), rnet);
+    }
+    data_type_rnet = G_raster_map_type(rnet, mapset);
+    if ((infd_rnet = G_open_cell_old(rnet, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rnet);
+    if (G_get_cellhd(rnet, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), rnet);
+    inrast_rnet = G_allocate_raster_buf(data_type_rnet);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(g0, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), g0);
+    }
+    data_type_g0 = G_raster_map_type(g0, mapset);
+    if ((infd_g0 = G_open_cell_old(g0, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), g0);
+    if (G_get_cellhd(g0, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), g0);
+    inrast_g0 = G_allocate_raster_buf(data_type_g0);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(h0, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), h0);
+    }
+    data_type_h0 = G_raster_map_type(h0, mapset);
+    if ((infd_h0 = G_open_cell_old(h0, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), h0);
+    if (G_get_cellhd(h0, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), h0);
+    inrast_h0 = G_allocate_raster_buf(data_type_h0);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (makin) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (makin) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rnet;
+	DCELL d_g0;
+	DCELL d_h0;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_rnet, inrast_rnet, row, data_type_rnet)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), rnet);
+	if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), g0);
+	if (G_get_raster_row(infd_h0, inrast_h0, row, data_type_h0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), h0);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_rnet) {
+	    case CELL_TYPE:
+		d_rnet = (double)((CELL *) inrast_rnet)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnet = (double)((FCELL *) inrast_rnet)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnet = ((DCELL *) inrast_rnet)[col];
+		break;
+	    }
+	    switch (data_type_g0) {
+	    case CELL_TYPE:
+		d_g0 = (double)((CELL *) inrast_g0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_g0 = (double)((FCELL *) inrast_g0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_g0 = ((DCELL *) inrast_g0)[col];
+		break;
+	    }
+	    switch (data_type_h0) {
+	    case CELL_TYPE:
+		d_h0 = (double)((CELL *) inrast_h0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_h0 = (double)((FCELL *) inrast_h0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_h0 = ((DCELL *) inrast_h0)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_rnet) || G_is_d_null_value(&d_g0) ||
+		 G_is_d_null_value(&d_h0)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (makin) {
+		    G_set_d_null_value(&outrast2[col], 1);
+		}
+	    }
+	    else {
+		
+
+				/****************************************/ 
+		    /* calculate evaporative fraction       */ 
+		    d = evap_fr(d_rnet, d_g0, d_h0);
+		outrast1[col] = d;
+		
+
+				/****************************************/ 
+		    /* calculate soil moisture              */ 
+		    if (makin) {
+		    d = soilmoisture(d);
+		    outrast2[col] = d;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	if (makin) {
+	    if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output raster file"));
+	}
+	}
+    G_free(inrast_rnet);
+    G_free(inrast_g0);
+    G_free(inrast_h0);
+    G_close_cell(infd_rnet);
+    G_close_cell(infd_g0);
+    G_close_cell(infd_h0);
+    G_free(outrast1);
+    G_free(outrast2);
+    if (makin) {
+	G_close_cell(outfd1);
+	G_close_cell(outfd2);
+    }
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    if (makin) {
+	G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.evapfr/soilmoisture.c
===================================================================
--- grass-addons/gipe/i.eb.evapfr/soilmoisture.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.evapfr/soilmoisture.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,16 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* soil moisture in the root zone
- * Makin, Molden and Bastiaanssen, 2001
- */
-
-double soilmoisture( double evap_fr )
-{
-	double result;
-	
-	result = (exp((evap_fr-1.2836)/0.4213))/0.511;
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* soil moisture in the root zone
+     * Makin, Molden and Bastiaanssen, 2001
+     */ 
+double soilmoisture(double evap_fr) 
+{
+    double result;
+
+    result = (exp((evap_fr - 1.2836) / 0.4213)) / 0.511;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.eb.g0/g0.c
===================================================================
--- grass-addons/gipe/i.eb.g0/g0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.g0/g0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,31 +1,37 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-double g_0(double bbalb, double ndvi, double tempk, double rnet, double time, int roerink)
-{
-	double 	a, b, result;
-	double r0_coef;
-	
-	if (time<=9.0||time>15.0){
-		r0_coef = 1.1;
-	} else if (time>9.0&&time<=11.0){
-		r0_coef = 1.0;
-	} else if (time>11.0&&time<=13.0){
-		r0_coef = 0.9;
-	} else if (time>13.0&&time<=15.0){
-		r0_coef = 1.0;
-	}
-	a = (0.0032 * (bbalb/r0_coef) + 0.0062 * (bbalb/r0_coef) * (bbalb/r0_coef));
-	b = (1 - 0.978 * pow(ndvi,4));
-	
-	/* Spain (Bastiaanssen, 1995)*/
-	result = (rnet * (tempk-273.15) / bbalb) * a * b ;
-	
-	/* HAPEX-Sahel (Roerink, 1995)*/
-	if(roerink){
-		result = result * 1.430 - 0.0845;
-	}
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ double g_0(double bbalb, double ndvi, double tempk, double rnet,
+	     double time, int roerink) 
+{
+    double a, b, result;
+
+    double r0_coef;
+
+    if (time <= 9.0 || time > 15.0) {
+	r0_coef = 1.1;
+    }
+    else if (time > 9.0 && time <= 11.0) {
+	r0_coef = 1.0;
+    }
+    else if (time > 11.0 && time <= 13.0) {
+	r0_coef = 0.9;
+    }
+    else if (time > 13.0 && time <= 15.0) {
+	r0_coef = 1.0;
+    }
+    a = (0.0032 * (bbalb / r0_coef) +
+	  0.0062 * (bbalb / r0_coef) * (bbalb / r0_coef));
+    b = (1 - 0.978 * pow(ndvi, 4));
+    
+	/* Spain (Bastiaanssen, 1995) */ 
+	result = (rnet * (tempk - 273.15) / bbalb) * a * b;
+    
+	/* HAPEX-Sahel (Roerink, 1995) */ 
+	if (roerink) {
+	result = result * 1.430 - 0.0845;
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.eb.g0/main.c
===================================================================
--- grass-addons/gipe/i.eb.g0/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.g0/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,297 +1,330 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.g0
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates an approximation of soil heat flux
- *               as seen in Bastiaanssen (1995) using time of
- *               satellite overpass.
- *
- * COPYRIGHT:    (C) 2006-2007 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-double g_0(double bbalb, double ndvi, double tempk, double rnet, double time, int roerink);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int roerink=0;/*Roerink Flag for HAPEX-Sahel conditions*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*metadata*/
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /* input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_albedo, infd_ndvi, infd_tempk, infd_rnet, infd_time;
-	int outfd;
-	
-	char *albedo,*ndvi,*tempk,*rnet,*time;
-	
-	int i=0,j=0;
-	
-	void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet, *inrast_time;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_ndvi;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_rnet;
-	RASTER_MAP_TYPE data_type_time;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("soil heat flux, energy balance, SEBAL");
-	module->description = _("soil heat flux approximation (Bastiaanssen, 1995)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("albedo");
-	input1->description=_("Name of the Albedo map [0.0;1.0]");
-	input1->answer     =_("albedo");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("ndvi");
-	input2->description=_("Name of the ndvi map [-1.0;+1.0]");
-	input2->answer     =_("ndvi");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("tempk");
-	input3->description=_("Name of the Surface temperature map [degree Kelvin]");
-	input3->answer     =_("tempk");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("rnet");
-	input4->description=_("Name of the Net Radiation map [W/m2]");
-	input4->answer     =_("rnet");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("time");
-	input5->description=_("Name of the time of satellite overpass map [local UTC]");
-	input5->answer     =_("time");
-	
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("g0");
-	output1->description=_("Name of the output g0 layer");
-	output1->answer     =_("g0");
-
-	flag1 = G_define_flag();
-	flag1->key = 'r';
-	flag1->description = _("HAPEX-Sahel empirical correction (Roerink, 1995)");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	albedo	 	= input1->answer;
-	ndvi	 	= input2->answer;
-	tempk		= input3->answer;
-	rnet	 	= input4->answer;
-	time	 	= input5->answer;
-	
-	result  = output1->answer;
-	roerink = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(albedo, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), albedo);
-	}
-	data_type_albedo = G_raster_map_type(albedo,mapset);
-	if ( (infd_albedo = G_open_cell_old (albedo,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), albedo);
-	if (G_get_cellhd (albedo, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), albedo);
-	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
-	/***************************************************/
-	mapset = G_find_cell2 (ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (rnet, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), rnet);
-	}
-	data_type_rnet = G_raster_map_type(rnet,mapset);
-	if ( (infd_rnet = G_open_cell_old (rnet,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), rnet);
-	if (G_get_cellhd (rnet, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), rnet);
-	inrast_rnet = G_allocate_raster_buf(data_type_rnet);
-	/***************************************************/
-	mapset = G_find_cell2 (time, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), time);
-	}
-	data_type_time = G_raster_map_type(time,mapset);
-	if ( (infd_time = G_open_cell_old (time,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), time);
-	if (G_get_cellhd (time, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), time);
-	inrast_time = G_allocate_raster_buf(data_type_time);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_albedo;
-		DCELL d_ndvi;
-		DCELL d_tempk;
-		DCELL d_rnet;
-		DCELL d_time;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_rnet,inrast_rnet,row,data_type_rnet)<0)
-			G_fatal_error(_("Could not read from <%s>"),rnet);
-		if(G_get_raster_row(infd_time,inrast_time,row,data_type_time)<0)
-			G_fatal_error(_("Could not read from <%s>"),time);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_rnet){
-				case CELL_TYPE:
-					d_rnet = (double) ((CELL *) inrast_rnet)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnet = (double) ((FCELL *) inrast_rnet)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnet = ((DCELL *) inrast_rnet)[col];
-					break;
-			}
-			switch(data_type_time){
-				case CELL_TYPE:
-					d_time = (double) ((CELL *) inrast_time)[col];
-					break;
-				case FCELL_TYPE:
-					d_time = (double) ((FCELL *) inrast_time)[col];
-					break;
-				case DCELL_TYPE:
-					d_time = ((DCELL *) inrast_time)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_ndvi)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_rnet)||
-			G_is_d_null_value(&d_time)){
-			G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate soil heat flux	    */
-				d = g_0(d_albedo,d_ndvi,d_tempk,d_rnet,d_time,roerink);
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_albedo);
-	G_free (inrast_ndvi);
-	G_free (inrast_tempk);
-	G_free (inrast_rnet);
-	G_free (inrast_time);
-	G_close_cell (infd_albedo);
-	G_close_cell (infd_ndvi);
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_rnet);
-	G_close_cell (infd_time);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	/* Colors in grey shade */
-	G_init_colors(&colors);
-	G_add_color_rule(0.0,0,0,0,200.0,255,255,255,&colors);
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.g0
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates an approximation of soil heat flux
+ *               as seen in Bastiaanssen (1995) using time of
+ *               satellite overpass.
+ *
+ * COPYRIGHT:    (C) 2006-2007 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double g_0(double bbalb, double ndvi, double tempk, double rnet,
+	     double time, int roerink);
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int roerink = 0;		/*Roerink Flag for HAPEX-Sahel conditions */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/* input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_albedo, infd_ndvi, infd_tempk, infd_rnet, infd_time;
+
+    int outfd;
+
+    char *albedo, *ndvi, *tempk, *rnet, *time;
+
+    int i = 0, j = 0;
+
+    void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet,
+	*inrast_time;
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_albedo;
+    RASTER_MAP_TYPE data_type_ndvi;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_rnet;
+    RASTER_MAP_TYPE data_type_time;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("soil heat flux, energy balance, SEBAL");
+    module->description =
+	_("soil heat flux approximation (Bastiaanssen, 1995)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("albedo");
+    input1->description = _("Name of the Albedo map [0.0;1.0]");
+    input1->answer = _("albedo");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("ndvi");
+    input2->description = _("Name of the ndvi map [-1.0;+1.0]");
+    input2->answer = _("ndvi");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("tempk");
+    input3->description =
+	_("Name of the Surface temperature map [degree Kelvin]");
+    input3->answer = _("tempk");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("rnet");
+    input4->description = _("Name of the Net Radiation map [W/m2]");
+    input4->answer = _("rnet");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("time");
+    input5->description =
+	_("Name of the time of satellite overpass map [local UTC]");
+    input5->answer = _("time");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("g0");
+    output1->description = _("Name of the output g0 layer");
+    output1->answer = _("g0");
+    flag1 = G_define_flag();
+    flag1->key = 'r';
+    flag1->description =
+	_("HAPEX-Sahel empirical correction (Roerink, 1995)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    albedo = input1->answer;
+    ndvi = input2->answer;
+    tempk = input3->answer;
+    rnet = input4->answer;
+    time = input5->answer;
+    result = output1->answer;
+    roerink = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(albedo, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), albedo);
+    }
+    data_type_albedo = G_raster_map_type(albedo, mapset);
+    if ((infd_albedo = G_open_cell_old(albedo, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+    if (G_get_cellhd(albedo, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), albedo);
+    inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rnet, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), rnet);
+    }
+    data_type_rnet = G_raster_map_type(rnet, mapset);
+    if ((infd_rnet = G_open_cell_old(rnet, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rnet);
+    if (G_get_cellhd(rnet, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), rnet);
+    inrast_rnet = G_allocate_raster_buf(data_type_rnet);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(time, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), time);
+    }
+    data_type_time = G_raster_map_type(time, mapset);
+    if ((infd_time = G_open_cell_old(time, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), time);
+    if (G_get_cellhd(time, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), time);
+    inrast_time = G_allocate_raster_buf(data_type_time);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_albedo;
+	DCELL d_ndvi;
+	DCELL d_tempk;
+	DCELL d_rnet;
+	DCELL d_time;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_rnet, inrast_rnet, row, data_type_rnet) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), rnet);
+	if (G_get_raster_row(infd_time, inrast_time, row, data_type_time) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), time);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = ((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = ((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_rnet) {
+	    case CELL_TYPE:
+		d_rnet = (double)((CELL *) inrast_rnet)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnet = (double)((FCELL *) inrast_rnet)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnet = ((DCELL *) inrast_rnet)[col];
+		break;
+	    }
+	    switch (data_type_time) {
+	    case CELL_TYPE:
+		d_time = (double)((CELL *) inrast_time)[col];
+		break;
+	    case FCELL_TYPE:
+		d_time = (double)((FCELL *) inrast_time)[col];
+		break;
+	    case DCELL_TYPE:
+		d_time = ((DCELL *) inrast_time)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_albedo) || G_is_d_null_value(&d_ndvi)
+		 || G_is_d_null_value(&d_tempk) ||
+		 G_is_d_null_value(&d_rnet) || G_is_d_null_value(&d_time)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate soil heat flux         */ 
+		    d =
+		    g_0(d_albedo, d_ndvi, d_tempk, d_rnet, d_time, roerink);
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_albedo);
+    G_free(inrast_ndvi);
+    G_free(inrast_tempk);
+    G_free(inrast_rnet);
+    G_free(inrast_time);
+    G_close_cell(infd_albedo);
+    G_close_cell(infd_ndvi);
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_rnet);
+    G_close_cell(infd_time);
+    G_free(outrast);
+    G_close_cell(outfd);
+    
+	/* Colors in grey shade */ 
+	G_init_colors(&colors);
+    G_add_color_rule(0.0, 0, 0, 0, 200.0, 255, 255, 255, &colors);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.h0/h0.c
===================================================================
--- grass-addons/gipe/i.eb.h0/h0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h0/h0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,10 +3,9 @@
 
 double h0(double roh_air, double cp, double rah, double dtair)
 {
-	double result;
+    double result;
 
-	result = roh_air*cp*dtair / rah;
-	
-	return result;
-}
+    result = roh_air * cp * dtair / rah;
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.h0/main.c
===================================================================
--- grass-addons/gipe/i.eb.h0/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h0/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,264 +1,300 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.h0
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates sensible heat flux
- *               a flag allows the Bastiaanssen (1995) affine transform 
- *               of surface temperature as used in his SEBAL model.
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double h0(double roh_air, double cp, double rah, double dtair);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int sebal=0;/*SEBAL Flag for affine transform of surf. temp.*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5;
-	struct Option *input6, *input7, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_rohair, infd_tempk, infd_rah, infd_dtair;
-	int outfd;
-	
-	char *rohair,*tempk,*rah,*dtair;
-
-	double cp; /*air specific heat*/	
-	int i=0,j=0;
-	double a,b; /*SEBAL slope and intercepts of surf. temp.*/
-	
-	void *inrast_rohair, *inrast_tempk, *inrast_rah, *inrast_dtair;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_rohair;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_rah;
-	RASTER_MAP_TYPE data_type_dtair;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("sensible heat flux, energy balance, SEBAL");
-	module->description = _("sensible heat flux equation, including flag for SEBAL version (Bastiaanssen, 1995)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("rohair");
-	input1->description=_("Name of the air density map ~[0.9;1.5]");
-	input1->answer     =_("rohair");
-
-	input2 = G_define_option() ;
-	input2->key        =_("cp");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  =_("parameter, float number");
-	input2->description=_("Value of the air specific heat [1000.0;1020.0]");
-	input2->answer     =_("1004.0");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("dtair");
-	input3->description=_("Name of the skin-air Surface temperature difference map ~[0.0-80.0]");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("rah");
-	input4->description=_("Name of the aerodynamic resistance to heat transport map [s/m]");
-	input4->answer     =_("rah");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("tempk");
-	input5->required   = NO;
-	input5->description=_("Name of the surface skin temperature map [degrees Kelvin], used with -s flag and affine coefs, disables dtair input");
-
-	input6 = G_define_option() ;
-	input6->key        =_("a");
-	input6->type       = TYPE_DOUBLE;
-	input6->required   = NO;
-	input6->gisprompt  =_("parameter, float number");
-	input6->description=_("Value of the slope of the transform");
-	
-	input7 = G_define_option() ;
-	input7->key        =_("b");
-	input7->type       = TYPE_DOUBLE;
-	input7->required   = NO;
-	input7->gisprompt  =_("parameter, float number");
-	input7->description=_("Value of the intercept of the transform");
-	
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output h0 layer");
-
-	flag1 = G_define_flag();
-	flag1->key = 's';
-	flag1->description = _("Affine transform of Surface temperature, needs input of slope and intercept (Bastiaanssen, 1995)");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	rohair	 	= input1->answer;
-	cp	 	= atof(input2->answer);
-	dtair		= input3->answer;
-	rah	 	= input4->answer;
-	tempk	 	= input5->answer;
-	a		= atof(input6->answer);
-	b		= atof(input7->answer);
-	
-	result  = output1->answer;
-	sebal = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(rohair, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), rohair);
-	}
-	data_type_rohair = G_raster_map_type(rohair,mapset);
-	if ( (infd_rohair = G_open_cell_old (rohair,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), rohair);
-	if (G_get_cellhd (rohair, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), rohair);
-	inrast_rohair = G_allocate_raster_buf(data_type_rohair);
-	/***************************************************/
-	if(!sebal){
-		mapset = G_find_cell2 (dtair, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"),dtair);
-		}
-		data_type_dtair = G_raster_map_type(dtair,mapset);
-		if ( (infd_dtair = G_open_cell_old (dtair,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), dtair);
-		if (G_get_cellhd (dtair, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), dtair);
-		inrast_dtair = G_allocate_raster_buf(data_type_dtair);
-	}
-	/***************************************************/
-	mapset = G_find_cell2 (rah, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), rah);
-	}
-	data_type_rah = G_raster_map_type(rah,mapset);
-	if ( (infd_rah = G_open_cell_old (rah,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), rah);
-	if (G_get_cellhd (rah, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), rah);
-	inrast_rah = G_allocate_raster_buf(data_type_rah);
-	/***************************************************/
-	if(sebal){
-		mapset = G_find_cell2 (tempk, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), tempk);
-		}
-		data_type_tempk = G_raster_map_type(tempk,mapset);
-		if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-		if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-		inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	}
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_rah);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_rah)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rohair;
-		DCELL d_rah;
-		DCELL d_dtair;
-		DCELL d_affine;
-		DCELL d_tempk;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_rohair,inrast_rohair,row,data_type_rohair)<0)
-			G_fatal_error(_("Could not read from <%s>"),rohair);
-		if(G_get_raster_row(infd_rah,inrast_rah,row,data_type_rah)<0)
-			G_fatal_error(_("Could not read from <%s>"),rah);
-		if(!sebal){
-			if(G_get_raster_row(infd_dtair,inrast_dtair,row,data_type_dtair)<0)
-			G_fatal_error(_("Could not read from <%s>"),dtair);
-		}else{
-			if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			d_rohair = ((DCELL *) inrast_rohair)[col];
-			d_rah = ((DCELL *) inrast_rah)[col];
-			if(!sebal){
-				d_dtair = ((DCELL *) inrast_dtair)[col];
-			}else{
-				d_tempk = ((DCELL *) inrast_tempk)[col];
-			}
-			if(G_is_d_null_value(&d_rohair)||
-				G_is_d_null_value(&d_rah)||
-				(!sebal)&&G_is_d_null_value(&d_dtair)||
-				(sebal)&&G_is_d_null_value(&d_tempk)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate sensible heat flux	    */
-				if(!sebal){
-					d = h0(d_rohair,cp,d_rah,d_dtair);
-				}else{
-					d_affine=a*d_tempk+b;
-					d = h0(d_rohair,cp,d_rah,d_affine);
-				}
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_rohair);
-	G_free (inrast_rah);
-	if(!sebal){
-		G_free (inrast_dtair);
-	}else{
-		G_free (inrast_tempk);
-	}
-	G_close_cell (infd_rohair);
-	G_close_cell (infd_rah);
-	if(!sebal){
-		G_close_cell (infd_dtair);
-	}else{
-		G_close_cell (infd_tempk);
-	}
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.h0
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates sensible heat flux
+ *               a flag allows the Bastiaanssen (1995) affine transform 
+ *               of surface temperature as used in his SEBAL model.
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double h0(double roh_air, double cp, double rah, double dtair);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int sebal = 0;		/*SEBAL Flag for affine transform of surf. temp. */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5;
+
+    struct Option *input6, *input7, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_rohair, infd_tempk, infd_rah, infd_dtair;
+
+    int outfd;
+
+    char *rohair, *tempk, *rah, *dtair;
+
+    double cp;		/*air specific heat */
+
+    int i = 0, j = 0;
+
+    double a, b;		/*SEBAL slope and intercepts of surf. temp. */
+
+    void *inrast_rohair, *inrast_tempk, *inrast_rah, *inrast_dtair;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_rohair;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_rah;
+    RASTER_MAP_TYPE data_type_dtair;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("sensible heat flux, energy balance, SEBAL");
+    module->description =
+	_("sensible heat flux equation, including flag for SEBAL version (Bastiaanssen, 1995)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("rohair");
+    input1->description = _("Name of the air density map ~[0.9;1.5]");
+    input1->answer = _("rohair");
+    input2 = G_define_option();
+    input2->key = _("cp");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("parameter, float number");
+    input2->description =
+	_("Value of the air specific heat [1000.0;1020.0]");
+    input2->answer = _("1004.0");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("dtair");
+    input3->description =
+	_("Name of the skin-air Surface temperature difference map ~[0.0-80.0]");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("rah");
+    input4->description =
+	_("Name of the aerodynamic resistance to heat transport map [s/m]");
+    input4->answer = _("rah");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("tempk");
+    input5->required = NO;
+    input5->description =
+	_("Name of the surface skin temperature map [degrees Kelvin], used with -s flag and affine coefs, disables dtair input");
+    input6 = G_define_option();
+    input6->key = _("a");
+    input6->type = TYPE_DOUBLE;
+    input6->required = NO;
+    input6->gisprompt = _("parameter, float number");
+    input6->description = _("Value of the slope of the transform");
+    input7 = G_define_option();
+    input7->key = _("b");
+    input7->type = TYPE_DOUBLE;
+    input7->required = NO;
+    input7->gisprompt = _("parameter, float number");
+    input7->description = _("Value of the intercept of the transform");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output h0 layer");
+    flag1 = G_define_flag();
+    flag1->key = 's';
+    flag1->description =
+	_("Affine transform of Surface temperature, needs input of slope and intercept (Bastiaanssen, 1995)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    rohair = input1->answer;
+    cp = atof(input2->answer);
+    dtair = input3->answer;
+    rah = input4->answer;
+    tempk = input5->answer;
+    a = atof(input6->answer);
+    b = atof(input7->answer);
+    result = output1->answer;
+    sebal = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rohair, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), rohair);
+    }
+    data_type_rohair = G_raster_map_type(rohair, mapset);
+    if ((infd_rohair = G_open_cell_old(rohair, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rohair);
+    if (G_get_cellhd(rohair, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), rohair);
+    inrast_rohair = G_allocate_raster_buf(data_type_rohair);
+    
+
+	/***************************************************/ 
+	if (!sebal) {
+	mapset = G_find_cell2(dtair, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), dtair);
+	}
+	data_type_dtair = G_raster_map_type(dtair, mapset);
+	if ((infd_dtair = G_open_cell_old(dtair, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), dtair);
+	if (G_get_cellhd(dtair, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), dtair);
+	inrast_dtair = G_allocate_raster_buf(data_type_dtair);
+    }
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rah, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), rah);
+    }
+    data_type_rah = G_raster_map_type(rah, mapset);
+    if ((infd_rah = G_open_cell_old(rah, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rah);
+    if (G_get_cellhd(rah, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), rah);
+    inrast_rah = G_allocate_raster_buf(data_type_rah);
+    
+
+	/***************************************************/ 
+	if (sebal) {
+	mapset = G_find_cell2(tempk, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), tempk);
+	}
+	data_type_tempk = G_raster_map_type(tempk, mapset);
+	if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+	if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    }
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_rah);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_rah)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rohair;
+	DCELL d_rah;
+	DCELL d_dtair;
+	DCELL d_affine;
+	DCELL d_tempk;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_rohair, inrast_rohair, row, data_type_rohair) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), rohair);
+	if (G_get_raster_row(infd_rah, inrast_rah, row, data_type_rah) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), rah);
+	if (!sebal) {
+	    if (G_get_raster_row
+		 (infd_dtair, inrast_dtair, row, data_type_dtair) < 0)
+		G_fatal_error(_("Could not read from <%s>"), dtair);
+	}
+	else {
+	    if (G_get_raster_row
+		 (infd_tempk, inrast_tempk, row, data_type_tempk) < 0)
+		G_fatal_error(_("Could not read from <%s>"), tempk);
+	}
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    d_rohair = ((DCELL *) inrast_rohair)[col];
+	    d_rah = ((DCELL *) inrast_rah)[col];
+	    if (!sebal) {
+		d_dtair = ((DCELL *) inrast_dtair)[col];
+	    }
+	    else {
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+	    }
+	    if (G_is_d_null_value(&d_rohair) || G_is_d_null_value(&d_rah) ||
+		 (!sebal) && G_is_d_null_value(&d_dtair) || (sebal) &&
+		 G_is_d_null_value(&d_tempk)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate sensible heat flux     */ 
+		    if (!sebal) {
+		    d = h0(d_rohair, cp, d_rah, d_dtair);
+		}
+		else {
+		    d_affine = a * d_tempk + b;
+		    d = h0(d_rohair, cp, d_rah, d_affine);
+		}
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_rohair);
+    G_free(inrast_rah);
+    if (!sebal) {
+	G_free(inrast_dtair);
+    }
+    else {
+	G_free(inrast_tempk);
+    }
+    G_close_cell(infd_rohair);
+    G_close_cell(infd_rah);
+    if (!sebal) {
+	G_close_cell(infd_dtair);
+    }
+    else {
+	G_close_cell(infd_tempk);
+    }
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.h_SEBAL01/main.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL01/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL01/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.eb.h_SEBAL01
@@ -26,804 +27,942 @@
 
 #define PI 3.14159265358979
 
-double **G_alloc_matrix (int rows, int cols)
+double **G_alloc_matrix(int rows, int cols)
 {
-	double **m;
-	int i;
-	m = (double **) G_calloc (rows, sizeof(double *));
-	m[0] = (double *) G_calloc (rows*cols, sizeof(double));
-	for (i = 1; i < rows; i++)
-		m[i] = m[i-1] + cols;
-		
-	return m;	
+    double **m;
+
+    int i;
+
+    m = (double **)G_calloc(rows, sizeof(double *));
+    m[0] = (double *)G_calloc(rows * cols, sizeof(double));
+    for (i = 1; i < rows; i++)
+	m[i] = m[i - 1] + cols;
+
+    return m;
 }
 
 int main(int argc, char *argv[])
-{	
-	struct Cell_head cellhd;
-	char *mapset; /*mapset name*/
-	
-	/* buffer for in, tmp and out raster */
-	void *inrast_Rn, *inrast_g0;
-	void *inrast_z0m, *inrast_t0dem;
-	DCELL *outrast;
-	
-	int nrows, ncols;
-	int row, col;
-	int row_wet, col_wet;
-	int row_dry, col_dry;
-	double m_row_wet, m_col_wet;
-	double m_row_dry, m_col_dry;
-	int infd_Rn, infd_g0;
-	int infd_z0m, infd_t0dem;
-	int outfd;
-	
-	char *mapset_Rn, *mapset_g0;
-	char *mapset_z0m, *mapset_t0dem;
-	char *Rn, *g0; 
-	char *z0m, *t0dem; 
-	char *h0;
-	double ustar, ea;
-	
-        struct History history;
-	struct GModule *module;
-	struct Option *input_Rn, *input_g0;
-	struct Option *input_z0m, *input_t0dem, *input_ustar;
-	struct Option *input_ea, *output;
-	struct Option *input_row_wet, *input_col_wet;
-	struct Option *input_row_dry, *input_col_dry;
-	struct Flag *flag2, *flag3;
+{
+    struct Cell_head cellhd;
+
+    char *mapset;		/*mapset name */
+
+    /* buffer for in, tmp and out raster */
+    void *inrast_Rn, *inrast_g0;
+
+    void *inrast_z0m, *inrast_t0dem;
+
+    DCELL *outrast;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int row_wet, col_wet;
+
+    int row_dry, col_dry;
+
+    double m_row_wet, m_col_wet;
+
+    double m_row_dry, m_col_dry;
+
+    int infd_Rn, infd_g0;
+
+    int infd_z0m, infd_t0dem;
+
+    int outfd;
+
+    char *mapset_Rn, *mapset_g0;
+
+    char *mapset_z0m, *mapset_t0dem;
+
+    char *Rn, *g0;
+
+    char *z0m, *t0dem;
+
+    char *h0;
+
+    double ustar, ea;
+
+    struct History history;
+
+    struct GModule *module;
+
+    struct Option *input_Rn, *input_g0;
+
+    struct Option *input_z0m, *input_t0dem, *input_ustar;
+
+    struct Option *input_ea, *output;
+
+    struct Option *input_row_wet, *input_col_wet;
+
+    struct Option *input_row_dry, *input_col_dry;
+
+    struct Flag *flag2, *flag3;
+
 	/********************************/
-	RASTER_MAP_TYPE data_type_Rn;
-	RASTER_MAP_TYPE data_type_g0;
-	RASTER_MAP_TYPE data_type_z0m;
-	RASTER_MAP_TYPE data_type_t0dem;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_Rn;
+
+    RASTER_MAP_TYPE data_type_g0;
+
+    RASTER_MAP_TYPE data_type_z0m;
+
+    RASTER_MAP_TYPE data_type_t0dem;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
 	/********************************/
-	double xp, yp;
-	double xmin, ymin;
-	double xmax, ymax;
-	double stepx,stepy;
-	double latitude, longitude;
+    double xp, yp;
 
-	int rowDry, colDry, rowWet, colWet;
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
+    int rowDry, colDry, rowWet, colWet;
+
 	/********************************/
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description = _("Sensible Heat Flux iteration SEBAL 01");
-	
-	/* Define different options */
-	input_Rn = G_define_standard_option(G_OPT_R_INPUT);
-	input_Rn->key	= "Rn";
-	input_Rn->description = _("Name of instantaneous Net Radiation map [W/m2]");
+    G_gisinit(argv[0]);
 
-	input_g0 = G_define_standard_option(G_OPT_R_INPUT);
-	input_g0->key	= "g0";
-	input_g0->description = _("Name of instantaneous soil heat flux map [W/m2]");
+    module = G_define_module();
+    module->description = _("Sensible Heat Flux iteration SEBAL 01");
 
-	input_z0m = G_define_standard_option(G_OPT_R_INPUT);
-	input_z0m->key	= "z0m";
-	input_z0m->description = _("Name of aerodynamic resistance to heat momentum [s/m]");
+    /* Define different options */
+    input_Rn = G_define_standard_option(G_OPT_R_INPUT);
+    input_Rn->key = "Rn";
+    input_Rn->description =
+	_("Name of instantaneous Net Radiation map [W/m2]");
 
-	input_t0dem = G_define_standard_option(G_OPT_R_INPUT);
-	input_t0dem->key	= "t0dem";
-	input_t0dem->description = _("Name of altitude corrected surface temperature [K]");
-		
-	input_ustar 			= G_define_option();
-	input_ustar->key		= "ustar";
-	input_ustar->type 		= TYPE_DOUBLE;
-	input_ustar->required 		= YES;
-	input_ustar->gisprompt 		= "old,value";
-	input_ustar->answer 		= "0.32407";
-	input_ustar->description 	= _("Value of the friction velocity [m/s]");
-	input_ustar->guisection		= _("Parameters");
-		
-	input_ea 			= G_define_option();
-	input_ea->key			= "ea";
-	input_ea->type 			= TYPE_DOUBLE;
-	input_ea->required 		= YES;
-	input_ea->gisprompt 		= "old,value";
-	input_ea->answer 		= "1.511";
-	input_ea->description 		= _("Value of the actual vapour pressure [KPa]");
-	input_ea->guisection		= _("Parameters");
-		
-	input_row_wet 			= G_define_option();
-	input_row_wet->key		= "row_wet";
-	input_row_wet->type 		= TYPE_DOUBLE;
-	input_row_wet->required 	= NO;
-	input_row_wet->gisprompt 	= "old,value";
-	input_row_wet->description 	= _("Row value of the wet pixel");
-	input_row_wet->guisection	= _("Parameters");
-	
-	input_col_wet 			= G_define_option();
-	input_col_wet->key		= "col_wet";
-	input_col_wet->type 		= TYPE_DOUBLE;
-	input_col_wet->required 	= NO;
-	input_col_wet->gisprompt 	= "old,value";
-	input_col_wet->description 	= _("Column value of the wet pixel");
-	input_col_wet->guisection	= _("Parameters");
-	
-	input_row_dry 			= G_define_option();
-	input_row_dry->key		= "row_dry";
-	input_row_dry->type 		= TYPE_DOUBLE;
-	input_row_dry->required 	= NO;
-	input_row_dry->gisprompt 	= "old,value";
-	input_row_dry->description 	= _("Row value of the dry pixel");
-	input_row_dry->guisection	= _("Parameters");
-	
-	input_col_dry 			= G_define_option();
-	input_col_dry->key		= "col_dry";
-	input_col_dry->type 		= TYPE_DOUBLE;
-	input_col_dry->required 	= NO;
-	input_col_dry->gisprompt 	= "old,value";
-	input_col_dry->description 	= _("Column value of the dry pixel");
-	input_col_dry->guisection	= _("Parameters");
+    input_g0 = G_define_standard_option(G_OPT_R_INPUT);
+    input_g0->key = "g0";
+    input_g0->description =
+	_("Name of instantaneous soil heat flux map [W/m2]");
 
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Name of output sensible heat flux layer [W/m2]");
-	
-	/* Define the different flags */
-	flag2 = G_define_flag() ;
-	flag2->key         = 'a' ;
-	flag2->description = _("Automatic wet/dry pixel (careful!)") ;
+    input_z0m = G_define_standard_option(G_OPT_R_INPUT);
+    input_z0m->key = "z0m";
+    input_z0m->description =
+	_("Name of aerodynamic resistance to heat momentum [s/m]");
 
-	flag3 = G_define_flag() ;
-	flag3->key         = 'c' ;
-	flag3->description = _("Dry/Wet pixels coordinates are in image projection, not row/col");
-	
-	if (G_parser(argc, argv))
-		exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	Rn	= input_Rn->answer;
-	g0	= input_g0->answer;
-	z0m	= input_z0m->answer;
-	t0dem	= input_t0dem->answer;
-	
-	h0	= output->answer;
+    input_t0dem = G_define_standard_option(G_OPT_R_INPUT);
+    input_t0dem->key = "t0dem";
+    input_t0dem->description =
+	_("Name of altitude corrected surface temperature [K]");
 
-	ustar = atof(input_ustar->answer);
-	ea = atof(input_ea->answer);
+    input_ustar = G_define_option();
+    input_ustar->key = "ustar";
+    input_ustar->type = TYPE_DOUBLE;
+    input_ustar->required = YES;
+    input_ustar->gisprompt = "old,value";
+    input_ustar->answer = "0.32407";
+    input_ustar->description = _("Value of the friction velocity [m/s]");
+    input_ustar->guisection = _("Parameters");
 
-	m_row_wet = atof(input_row_wet->answer);
-	m_col_wet = atof(input_col_wet->answer);
-	m_row_dry = atof(input_row_dry->answer);
-	m_col_dry = atof(input_col_dry->answer);
-	if((!input_row_wet->answer||!input_col_wet->answer||
-	!input_row_dry->answer||!input_col_dry->answer)&&
-	!flag2->answer){
-		G_fatal_error(_("FATAL ERROR: either auto-mode either wet/dry pixels coordinates should be provided!"));
-	}
-	if(flag3->answer){
-		G_message(_("Manual wet/dry pixels in image coordinates"));
-		G_message(_("Wet Pixel=> x:%f y:%f"),m_col_wet,m_row_wet);
-		G_message(_("Dry Pixel=> x:%f y:%f"),m_col_dry,m_row_dry);
-	} else {
-		G_message(_("Wet Pixel=> row:%.0f col:%.0f"),m_row_wet,m_col_wet);
-		G_message(_("Dry Pixel=> row:%.0f col:%.0f"),m_row_dry,m_col_dry);
-	}
-	/* find maps in mapset */
-	mapset_Rn = G_find_cell2 (Rn, "");
-	if (mapset_Rn == NULL)
-        	G_fatal_error (_("cell file [%s] not found"), Rn);
-	mapset_g0 = G_find_cell2 (g0, "");
-	if (mapset_g0 == NULL)
-        	G_fatal_error (_("cell file [%s] not found"), g0);
-	mapset_z0m = G_find_cell2 (z0m, "");
-	if (mapset_z0m == NULL)
-        	G_fatal_error (_("cell file [%s] not found"), z0m);
-	mapset_t0dem = G_find_cell2 (t0dem, "");
-	if (mapset_t0dem == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), t0dem);
-	
-	/* check legal output name */ 
-	if (G_legal_filename (h0) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), h0);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_Rn 	= G_raster_map_type(Rn, mapset_Rn);
-	data_type_g0 	= G_raster_map_type(g0, mapset_g0);
-	data_type_z0m 	= G_raster_map_type(z0m, mapset_z0m);
-	data_type_t0dem	= G_raster_map_type(t0dem, mapset_t0dem);
-	
-	if ( (infd_Rn = G_open_cell_old (Rn, mapset_Rn)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), Rn);
-	if ( (infd_g0 = G_open_cell_old (g0, mapset_g0)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), g0);
-	if ( (infd_z0m = G_open_cell_old (z0m, mapset_z0m)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), z0m);
-	if ( (infd_t0dem = G_open_cell_old (t0dem, mapset_t0dem)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),t0dem);
-	
-	if (G_get_cellhd (Rn, mapset_Rn, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), Rn);
-	if (G_get_cellhd (g0, mapset_g0, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), g0);
-	if (G_get_cellhd (z0m, mapset_z0m, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), z0m);
-	if (G_get_cellhd (t0dem, mapset_t0dem, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), t0dem);
-	
-	/* Allocate input buffer */
-	inrast_Rn  	= G_allocate_raster_buf(data_type_Rn);
-	inrast_g0  	= G_allocate_raster_buf(data_type_g0);
-	inrast_z0m  	= G_allocate_raster_buf(data_type_z0m);
-	inrast_t0dem 	= G_allocate_raster_buf(data_type_t0dem);
+    input_ea = G_define_option();
+    input_ea->key = "ea";
+    input_ea->type = TYPE_DOUBLE;
+    input_ea->required = YES;
+    input_ea->gisprompt = "old,value";
+    input_ea->answer = "1.511";
+    input_ea->description = _("Value of the actual vapour pressure [KPa]");
+    input_ea->guisection = _("Parameters");
+
+    input_row_wet = G_define_option();
+    input_row_wet->key = "row_wet";
+    input_row_wet->type = TYPE_DOUBLE;
+    input_row_wet->required = NO;
+    input_row_wet->gisprompt = "old,value";
+    input_row_wet->description = _("Row value of the wet pixel");
+    input_row_wet->guisection = _("Parameters");
+
+    input_col_wet = G_define_option();
+    input_col_wet->key = "col_wet";
+    input_col_wet->type = TYPE_DOUBLE;
+    input_col_wet->required = NO;
+    input_col_wet->gisprompt = "old,value";
+    input_col_wet->description = _("Column value of the wet pixel");
+    input_col_wet->guisection = _("Parameters");
+
+    input_row_dry = G_define_option();
+    input_row_dry->key = "row_dry";
+    input_row_dry->type = TYPE_DOUBLE;
+    input_row_dry->required = NO;
+    input_row_dry->gisprompt = "old,value";
+    input_row_dry->description = _("Row value of the dry pixel");
+    input_row_dry->guisection = _("Parameters");
+
+    input_col_dry = G_define_option();
+    input_col_dry->key = "col_dry";
+    input_col_dry->type = TYPE_DOUBLE;
+    input_col_dry->required = NO;
+    input_col_dry->gisprompt = "old,value";
+    input_col_dry->description = _("Column value of the dry pixel");
+    input_col_dry->guisection = _("Parameters");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Name of output sensible heat flux layer [W/m2]");
+
+    /* Define the different flags */
+    flag2 = G_define_flag();
+    flag2->key = 'a';
+    flag2->description = _("Automatic wet/dry pixel (careful!)");
+
+    flag3 = G_define_flag();
+    flag3->key = 'c';
+    flag3->description =
+	_("Dry/Wet pixels coordinates are in image projection, not row/col");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    Rn = input_Rn->answer;
+    g0 = input_g0->answer;
+    z0m = input_z0m->answer;
+    t0dem = input_t0dem->answer;
+
+    h0 = output->answer;
+
+    ustar = atof(input_ustar->answer);
+    ea = atof(input_ea->answer);
+
+    m_row_wet = atof(input_row_wet->answer);
+    m_col_wet = atof(input_col_wet->answer);
+    m_row_dry = atof(input_row_dry->answer);
+    m_col_dry = atof(input_col_dry->answer);
+    if ((!input_row_wet->answer || !input_col_wet->answer ||
+	 !input_row_dry->answer || !input_col_dry->answer) &&
+	!flag2->answer) {
+	G_fatal_error(_("FATAL ERROR: either auto-mode either wet/dry pixels coordinates should be provided!"));
+    }
+    if (flag3->answer) {
+	G_message(_("Manual wet/dry pixels in image coordinates"));
+	G_message(_("Wet Pixel=> x:%f y:%f"), m_col_wet, m_row_wet);
+	G_message(_("Dry Pixel=> x:%f y:%f"), m_col_dry, m_row_dry);
+    }
+    else {
+	G_message(_("Wet Pixel=> row:%.0f col:%.0f"), m_row_wet, m_col_wet);
+	G_message(_("Dry Pixel=> row:%.0f col:%.0f"), m_row_dry, m_col_dry);
+    }
+    /* find maps in mapset */
+    mapset_Rn = G_find_cell2(Rn, "");
+    if (mapset_Rn == NULL)
+	G_fatal_error(_("cell file [%s] not found"), Rn);
+    mapset_g0 = G_find_cell2(g0, "");
+    if (mapset_g0 == NULL)
+	G_fatal_error(_("cell file [%s] not found"), g0);
+    mapset_z0m = G_find_cell2(z0m, "");
+    if (mapset_z0m == NULL)
+	G_fatal_error(_("cell file [%s] not found"), z0m);
+    mapset_t0dem = G_find_cell2(t0dem, "");
+    if (mapset_t0dem == NULL)
+	G_fatal_error(_("cell file [%s] not found"), t0dem);
+
+    /* check legal output name */
+    if (G_legal_filename(h0) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), h0);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_Rn = G_raster_map_type(Rn, mapset_Rn);
+    data_type_g0 = G_raster_map_type(g0, mapset_g0);
+    data_type_z0m = G_raster_map_type(z0m, mapset_z0m);
+    data_type_t0dem = G_raster_map_type(t0dem, mapset_t0dem);
+
+    if ((infd_Rn = G_open_cell_old(Rn, mapset_Rn)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), Rn);
+    if ((infd_g0 = G_open_cell_old(g0, mapset_g0)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), g0);
+    if ((infd_z0m = G_open_cell_old(z0m, mapset_z0m)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), z0m);
+    if ((infd_t0dem = G_open_cell_old(t0dem, mapset_t0dem)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), t0dem);
+
+    if (G_get_cellhd(Rn, mapset_Rn, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), Rn);
+    if (G_get_cellhd(g0, mapset_g0, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), g0);
+    if (G_get_cellhd(z0m, mapset_z0m, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), z0m);
+    if (G_get_cellhd(t0dem, mapset_t0dem, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), t0dem);
+
+    /* Allocate input buffer */
+    inrast_Rn = G_allocate_raster_buf(data_type_Rn);
+    inrast_g0 = G_allocate_raster_buf(data_type_g0);
+    inrast_z0m = G_allocate_raster_buf(data_type_z0m);
+    inrast_t0dem = G_allocate_raster_buf(data_type_t0dem);
+
 	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
+    G_debug(3, "number of rows %d", cellhd.rows);
+
 	/***************************************************/
-	/* Setup pixel location variables */
+    /* Setup pixel location variables */
+
 	/***************************************************/
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
 
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
 
-	nrows = G_window_rows();
-	ncols = G_window_cols();
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
 	/***************************************************/
-	/* Allocate output buffer */
+    /* Allocate output buffer */
+
 	/***************************************************/
-	outrast = G_allocate_d_raster_buf();
+    outrast = G_allocate_d_raster_buf();
 
-	if((outfd = G_open_raster_new (h0,DCELL_TYPE)) < 0)
-		G_fatal_error (_("Could not open <%s>"),h0);
+    if ((outfd = G_open_raster_new(h0, DCELL_TYPE)) < 0)
+	G_fatal_error(_("Could not open <%s>"), h0);
 
 	/***************************************************/
-	/* Allocate memory for temporary images		   */
-	double **d_Roh, **d_Rah;
-     	if( (d_Roh = G_alloc_matrix(nrows,ncols)) == NULL) 
-		G_message("cannot allocate memory for temporary d_Roh image");
-     	if( (d_Rah = G_alloc_matrix(nrows,ncols)) == NULL) 
-		G_message("cannot allocate memory for temporary d_Rah image");
+    /* Allocate memory for temporary images            */
+    double **d_Roh, **d_Rah;
+
+    if ((d_Roh = G_alloc_matrix(nrows, ncols)) == NULL)
+	G_message("cannot allocate memory for temporary d_Roh image");
+    if ((d_Rah = G_alloc_matrix(nrows, ncols)) == NULL)
+	G_message("cannot allocate memory for temporary d_Rah image");
+
 	/***************************************************/
 
-	/* MANUAL T0DEM WET/DRY PIXELS */
-	DCELL d_Rn_dry;
-	DCELL d_g0_dry;
-	DCELL d_t0dem_dry;
-	DCELL d_t0dem_wet;
+    /* MANUAL T0DEM WET/DRY PIXELS */
+    DCELL d_Rn_dry;
 
-	if(flag2->answer){
-		/* THREAD 3 */
-		/* Process tempk min / max pixels */
-		/* Internal use only */
-		DCELL d_Rn_wet;
-		DCELL d_g0_wet;
-		DCELL d_Rn;
-		DCELL d_g0;
-		DCELL d_h0;
-		DCELL t0dem_min;
-		DCELL t0dem_max;
+    DCELL d_g0_dry;
+
+    DCELL d_t0dem_dry;
+
+    DCELL d_t0dem_wet;
+
+    if (flag2->answer) {
+	/* THREAD 3 */
+	/* Process tempk min / max pixels */
+	/* Internal use only */
+	DCELL d_Rn_wet;
+
+	DCELL d_g0_wet;
+
+	DCELL d_Rn;
+
+	DCELL d_g0;
+
+	DCELL d_h0;
+
+	DCELL t0dem_min;
+
+	DCELL t0dem_max;
+
 		/*********************/
-		for (row = 0; row < nrows; row++){
-			DCELL d_t0dem;
-			G_percent(row,nrows,2);
-			if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem)<0)
-				G_fatal_error(_("Could not read from <%s>"),t0dem);
-			if(G_get_raster_row(infd_Rn, inrast_Rn, row,data_type_Rn) < 0)
-				G_fatal_error(_("Could not read from <%s>"),Rn);
-			if(G_get_raster_row(infd_g0, inrast_g0, row,data_type_g0) < 0)
-				G_fatal_error(_("Could not read from <%s>"),g0);
-			/*process the data */
-			for (col=0; col < ncols; col++)
-			{
-				switch(data_type_t0dem){
-					case CELL_TYPE:
-						d_t0dem = (double) ((CELL *) inrast_t0dem)[col];
-						break;
-					case FCELL_TYPE:
-						d_t0dem = (double) ((FCELL *) inrast_t0dem)[col];
-						break;
-					case DCELL_TYPE:
-						d_t0dem = (double) ((DCELL *) inrast_t0dem)[col];
-						break;
-				}
-				switch(data_type_Rn){
-					case CELL_TYPE:
-						d_Rn = (double) ((CELL *) inrast_Rn)[col];
-						break;
-					case FCELL_TYPE:
-						d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-						break;
-					case DCELL_TYPE:
-						d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-						break;
-				}
-				switch(data_type_g0){
-					case CELL_TYPE:
-						d_g0 = (double) ((CELL *) inrast_g0)[col];
-						break;
-					case FCELL_TYPE:
-						d_g0 = (double) ((FCELL *) inrast_g0)[col];
-						break;
-					case DCELL_TYPE:
-						d_g0 = (double) ((DCELL *) inrast_g0)[col];
-						break;
-				}
-				if(G_is_d_null_value(&d_t0dem)||
-				G_is_d_null_value(&d_Rn)||
-				G_is_d_null_value(&d_g0)){
-					/* do nothing */ 
-				}else{
-					if(d_t0dem<=250.0){
-						/* do nothing */ 
-					} else {
-						d_h0=d_Rn-d_g0;
-						if(d_t0dem<t0dem_min&&
-						d_Rn>0.0&&d_g0>0.0&&d_h0>0.0&&
-						d_h0<100.0){
-							t0dem_min=d_t0dem;
-							d_t0dem_wet=d_t0dem;
-							d_Rn_wet=d_Rn;
-							d_g0_wet=d_g0;
-							m_col_wet=col;
-							m_row_wet=row;
-						}
-						if(d_t0dem>t0dem_max&&
-						d_Rn>0.0&&d_g0>0.0&&d_h0>100.0&&
-						d_h0<500.0){
-							t0dem_max=d_t0dem;
-							d_t0dem_dry=d_t0dem;
-							d_Rn_dry=d_Rn;
-							d_g0_dry=d_g0;
-							m_col_dry=col;
-							m_row_dry=row;
-						}
-					}
-				}
-			}
-		}
-		G_message("row_wet=%d\tcol_wet=%d\n",row_wet,col_wet);
-		G_message("row_dry=%d\tcol_dry=%d\n",row_dry,col_dry);
-		G_message("g0_wet=%f\n",d_g0_wet);
-		G_message("Rn_wet=%f\n",d_Rn_wet);
-		G_message("LE_wet=%f\n",d_Rn_wet-d_g0_wet);
-		G_message("t0dem_dry=%f\n",d_t0dem_dry);
-		G_message("rnet_dry=%f\n",d_Rn_dry);
-		G_message("g0_dry=%f\n",d_g0_dry);
-		G_message("h0_dry=%f\n",d_Rn_dry-d_g0_dry);
-	} /* END OF FLAG2 */
+	for (row = 0; row < nrows; row++) {
+	    DCELL d_t0dem;
 
-
-	/* MANUAL T0DEM WET/DRY PIXELS */
-	/*DRY PIXEL*/
-	if(flag3->answer){
-		/*Calculate coordinates of row/col from projected ones*/
-		row = (int) (( ymax - m_row_dry ) / (double) stepy) ;
-		col = (int) (( m_col_dry - xmin ) / (double) stepx) ;
-		G_message("Dry Pixel | row:%i col:%i",row,col);
-	} else {
-		row = (int) m_row_dry;
-		col = (int) m_col_dry;
-		G_message("Dry Pixel | row:%i col:%i",row,col);
-	}
-	rowDry=row;
-	colDry=col;
-	if(G_get_raster_row(infd_Rn,inrast_Rn,row,data_type_Rn)<0)
-		G_fatal_error(_("Could not read from <%s>"),Rn);
-	if(G_get_raster_row(infd_g0,inrast_g0,row,data_type_g0)<0)
-		G_fatal_error(_("Could not read from <%s>"),g0);
-	if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem)<0)
-		G_fatal_error(_("Could not read from <%s>"),t0dem);
-	switch(data_type_Rn){
+	    G_percent(row, nrows, 2);
+	    if (G_get_raster_row
+		(infd_t0dem, inrast_t0dem, row, data_type_t0dem) < 0)
+		G_fatal_error(_("Could not read from <%s>"), t0dem);
+	    if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+		G_fatal_error(_("Could not read from <%s>"), Rn);
+	    if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+		G_fatal_error(_("Could not read from <%s>"), g0);
+	    /*process the data */
+	    for (col = 0; col < ncols; col++) {
+		switch (data_type_t0dem) {
 		case CELL_TYPE:
-			d_Rn_dry = (double) ((CELL *) inrast_Rn)[col];
-			break;
+		    d_t0dem = (double)((CELL *) inrast_t0dem)[col];
+		    break;
 		case FCELL_TYPE:
-			d_Rn_dry = (double) ((FCELL *) inrast_Rn)[col];
-			break;
+		    d_t0dem = (double)((FCELL *) inrast_t0dem)[col];
+		    break;
 		case DCELL_TYPE:
-			d_Rn_dry = (double) ((DCELL *) inrast_Rn)[col];
-			break;
-	}
-	switch(data_type_g0){
+		    d_t0dem = (double)((DCELL *) inrast_t0dem)[col];
+		    break;
+		}
+		switch (data_type_Rn) {
 		case CELL_TYPE:
-			d_g0_dry = (double) ((CELL *) inrast_g0)[col];
-			break;
+		    d_Rn = (double)((CELL *) inrast_Rn)[col];
+		    break;
 		case FCELL_TYPE:
-			d_g0_dry = (double) ((FCELL *) inrast_g0)[col];
-			break;
+		    d_Rn = (double)((FCELL *) inrast_Rn)[col];
+		    break;
 		case DCELL_TYPE:
-			d_g0_dry = (double) ((DCELL *) inrast_g0)[col];
-			break;
-	}
-	switch(data_type_t0dem){
+		    d_Rn = (double)((DCELL *) inrast_Rn)[col];
+		    break;
+		}
+		switch (data_type_g0) {
 		case CELL_TYPE:
-			d_t0dem_dry = (double) ((CELL *) inrast_t0dem)[col];
-			break;
+		    d_g0 = (double)((CELL *) inrast_g0)[col];
+		    break;
 		case FCELL_TYPE:
-			d_t0dem_dry = (double) ((FCELL *) inrast_t0dem)[col];
-			break;
+		    d_g0 = (double)((FCELL *) inrast_g0)[col];
+		    break;
 		case DCELL_TYPE:
-			d_t0dem_dry = (double) ((DCELL *) inrast_t0dem)[col];
-			break;
-	}
-	/*WET PIXEL*/
-	if(flag3->answer){
-		/*Calculate coordinates of row/col from projected ones*/
-		row = (int) (( ymax - m_row_wet ) / (double) stepy) ;
-		col = (int) (( m_col_wet - xmin ) / (double) stepx) ;
-		G_message("Wet Pixel | row:%i col:%i",row,col);
-	} else {
-		row = m_row_wet;
-		col = m_col_wet;
-		G_message("Wet Pixel | row:%i col:%i",row,col);
-	}
-	rowWet=row;
-	colWet=col;
-	if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem)<0)
-		G_fatal_error(_("Could not read from <%s>"),t0dem);
-	switch(data_type_t0dem){
-		case CELL_TYPE:
-			d_t0dem_wet = (double) ((CELL *) inrast_t0dem)[col];
-			break;
-		case FCELL_TYPE:
-			d_t0dem_wet = (double) ((FCELL *) inrast_t0dem)[col];
-			break;
-		case DCELL_TYPE:
-			d_t0dem_wet = (double) ((DCELL *) inrast_t0dem)[col];
-			break;
-	}
-	/* END OF MANUAL WET/DRY PIXELS */
-	double h_dry;
-	h_dry=d_Rn_dry-d_g0_dry;
-	G_message("h_dry = %f",h_dry);
-	G_message("t0dem_dry = %f",d_t0dem_dry);
-	G_message("t0dem_wet = %f",d_t0dem_wet);
-	DCELL d_rah_dry;
-	DCELL d_roh_dry;
-
-	/* INITIALIZATION */
-	for (row = 0; row < nrows; row++)
-	{	
-		DCELL d_t0dem;
-		DCELL d_z0m; 	
-		DCELL d_rah1; 	
-		DCELL d_roh1; 	
-		DCELL d_u5;
-		G_percent(row,nrows,2);
-		/* read a line input maps into buffers*/	
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem) < 0)
-			G_fatal_error(_("Could not read from <%s>"),t0dem);
-		/* read every cell in the line buffers */
-		for(col=0;col<ncols;col++)
-		{
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = (double) ((DCELL *) inrast_z0m)[col];
-					break;
+		    d_g0 = (double)((DCELL *) inrast_g0)[col];
+		    break;
+		}
+		if (G_is_d_null_value(&d_t0dem) ||
+		    G_is_d_null_value(&d_Rn) || G_is_d_null_value(&d_g0)) {
+		    /* do nothing */
+		}
+		else {
+		    if (d_t0dem <= 250.0) {
+			/* do nothing */
+		    }
+		    else {
+			d_h0 = d_Rn - d_g0;
+			if (d_t0dem < t0dem_min &&
+			    d_Rn > 0.0 && d_g0 > 0.0 && d_h0 > 0.0 &&
+			    d_h0 < 100.0) {
+			    t0dem_min = d_t0dem;
+			    d_t0dem_wet = d_t0dem;
+			    d_Rn_wet = d_Rn;
+			    d_g0_wet = d_g0;
+			    m_col_wet = col;
+			    m_row_wet = row;
 			}
-			switch(data_type_t0dem){
-				case CELL_TYPE:
-					d_t0dem = (double) ((CELL *) inrast_t0dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_t0dem = (double) ((FCELL *) inrast_t0dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_t0dem = (double) ((DCELL *) inrast_t0dem)[col];
-					break;
+			if (d_t0dem > t0dem_max &&
+			    d_Rn > 0.0 && d_g0 > 0.0 && d_h0 > 100.0 &&
+			    d_h0 < 500.0) {
+			    t0dem_max = d_t0dem;
+			    d_t0dem_dry = d_t0dem;
+			    d_Rn_dry = d_Rn;
+			    d_g0_dry = d_g0;
+			    m_col_dry = col;
+			    m_row_dry = row;
 			}
-			if(G_is_d_null_value(&d_t0dem) ||
-				G_is_d_null_value(&d_z0m)){
-				/* do nothing */
-				d_Roh[row][col] = -999.9;
-				d_Rah[row][col] = -999.9;
-			} else {
-				d_u5=(ustar/0.41)*log(5/d_z0m);
-				d_rah1=(1/(d_u5*pow(0.41,2)))*log(5/d_z0m)*log(5/(d_z0m*0.1));
-				d_roh1=((998-ea)/(d_t0dem*2.87))+(ea/(d_t0dem*4.61));
-				if(d_roh1>5){
-					d_roh1=1.0;
-				} else {
-					d_roh1=((1000-4.65)/(d_t0dem*2.87))+(4.65/(d_t0dem*4.61));
-				}
-				if(row==rowDry&&col==colDry){/*collect dry pix info*/
-					d_rah_dry = d_rah1;
-					d_roh_dry = d_roh1;
-					G_message("d_rah_dry=%f d_roh_dry=%f",d_rah_dry,d_roh_dry);
-				}
-				d_Roh[row][col] = d_roh1;
-				d_Rah[row][col] = d_rah1;
-			}
+		    }
 		}
-	}	
-	DCELL d_dT_dry;
-	/*Calculate dT_dry*/	
-	d_dT_dry = (h_dry*d_rah_dry)/(1004*d_roh_dry);
-	double a, b;
-	/*Calculate coefficients for next dT equation*/	
-	/*a = 1.0/ ((d_dT_dry-0.0) / (d_t0dem_dry-d_t0dem_wet));*/
-	/*b = ( a * d_t0dem_wet ) * (-1.0);*/
+	    }
+	}
+	G_message("row_wet=%d\tcol_wet=%d\n", row_wet, col_wet);
+	G_message("row_dry=%d\tcol_dry=%d\n", row_dry, col_dry);
+	G_message("g0_wet=%f\n", d_g0_wet);
+	G_message("Rn_wet=%f\n", d_Rn_wet);
+	G_message("LE_wet=%f\n", d_Rn_wet - d_g0_wet);
+	G_message("t0dem_dry=%f\n", d_t0dem_dry);
+	G_message("rnet_dry=%f\n", d_Rn_dry);
+	G_message("g0_dry=%f\n", d_g0_dry);
+	G_message("h0_dry=%f\n", d_Rn_dry - d_g0_dry);
+    }				/* END OF FLAG2 */
 
-	double sumx=d_t0dem_wet+d_t0dem_dry;
-	double sumy=d_dT_dry+0.0;
-	double sumx2=pow(d_t0dem_wet,2)+pow(d_t0dem_dry,2);
-	double sumxy=(d_t0dem_wet*0.0)+(d_t0dem_dry*d_dT_dry);
-	a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx,2)/2.0));
-	b = (sumy - ( a * sumx)) / 2.0; 
-	G_message("d_dT_dry=%f",d_dT_dry);
-	G_message("dT1=%f * t0dem + (%f)", a, b);
 
-	DCELL d_h_dry;
+    /* MANUAL T0DEM WET/DRY PIXELS */
+    /*DRY PIXEL */
+    if (flag3->answer) {
+	/*Calculate coordinates of row/col from projected ones */
+	row = (int)((ymax - m_row_dry) / (double)stepy);
+	col = (int)((m_col_dry - xmin) / (double)stepx);
+	G_message("Dry Pixel | row:%i col:%i", row, col);
+    }
+    else {
+	row = (int)m_row_dry;
+	col = (int)m_col_dry;
+	G_message("Dry Pixel | row:%i col:%i", row, col);
+    }
+    rowDry = row;
+    colDry = col;
+    if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+	G_fatal_error(_("Could not read from <%s>"), Rn);
+    if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	G_fatal_error(_("Could not read from <%s>"), g0);
+    if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) < 0)
+	G_fatal_error(_("Could not read from <%s>"), t0dem);
+    switch (data_type_Rn) {
+    case CELL_TYPE:
+	d_Rn_dry = (double)((CELL *) inrast_Rn)[col];
+	break;
+    case FCELL_TYPE:
+	d_Rn_dry = (double)((FCELL *) inrast_Rn)[col];
+	break;
+    case DCELL_TYPE:
+	d_Rn_dry = (double)((DCELL *) inrast_Rn)[col];
+	break;
+    }
+    switch (data_type_g0) {
+    case CELL_TYPE:
+	d_g0_dry = (double)((CELL *) inrast_g0)[col];
+	break;
+    case FCELL_TYPE:
+	d_g0_dry = (double)((FCELL *) inrast_g0)[col];
+	break;
+    case DCELL_TYPE:
+	d_g0_dry = (double)((DCELL *) inrast_g0)[col];
+	break;
+    }
+    switch (data_type_t0dem) {
+    case CELL_TYPE:
+	d_t0dem_dry = (double)((CELL *) inrast_t0dem)[col];
+	break;
+    case FCELL_TYPE:
+	d_t0dem_dry = (double)((FCELL *) inrast_t0dem)[col];
+	break;
+    case DCELL_TYPE:
+	d_t0dem_dry = (double)((DCELL *) inrast_t0dem)[col];
+	break;
+    }
+    /*WET PIXEL */
+    if (flag3->answer) {
+	/*Calculate coordinates of row/col from projected ones */
+	row = (int)((ymax - m_row_wet) / (double)stepy);
+	col = (int)((m_col_wet - xmin) / (double)stepx);
+	G_message("Wet Pixel | row:%i col:%i", row, col);
+    }
+    else {
+	row = m_row_wet;
+	col = m_col_wet;
+	G_message("Wet Pixel | row:%i col:%i", row, col);
+    }
+    rowWet = row;
+    colWet = col;
+    if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) < 0)
+	G_fatal_error(_("Could not read from <%s>"), t0dem);
+    switch (data_type_t0dem) {
+    case CELL_TYPE:
+	d_t0dem_wet = (double)((CELL *) inrast_t0dem)[col];
+	break;
+    case FCELL_TYPE:
+	d_t0dem_wet = (double)((FCELL *) inrast_t0dem)[col];
+	break;
+    case DCELL_TYPE:
+	d_t0dem_wet = (double)((DCELL *) inrast_t0dem)[col];
+	break;
+    }
+    /* END OF MANUAL WET/DRY PIXELS */
+    double h_dry;
 
-	/* ITERATION 1 */
-	
-	for (row = 0; row < nrows; row++)
-	{	
-		DCELL d_t0dem;
-		DCELL d_z0m; 	
-		DCELL d_h1; 	
-		DCELL d_rah1; 	
-		DCELL d_rah2; 	
-		DCELL d_roh1; 	
-		DCELL d_L; 	
-		DCELL d_x; 	
-		DCELL d_psih; 	
-		DCELL d_psim; 	
-		DCELL d_u5;
-		G_percent(row,nrows,2);
-		/* read a line input maps into buffers*/	
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem) < 0)
-			G_fatal_error(_("Could not read from <%s>"),t0dem);
-		/* read every cell in the line buffers */
-		for(col=0;col<ncols;col++)
-		{
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = (double) ((DCELL *) inrast_z0m)[col];
-					break;
-			}
-			switch(data_type_t0dem){
-				case CELL_TYPE:
-					d_t0dem = (double) ((CELL *) inrast_t0dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_t0dem = (double) ((FCELL *) inrast_t0dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_t0dem = (double) ((DCELL *) inrast_t0dem)[col];
-					break;
-			}
-			d_rah1 = d_Rah[row][col];
-			d_roh1 = d_Roh[row][col];
-			if(G_is_d_null_value(&d_t0dem) ||
-				G_is_d_null_value(&d_z0m)){
-				/* do nothing */
-			} else {
-				if(d_rah1<1.0){
-					d_h1 = 0.0;
-				} else {
-					d_h1 = (1004*d_roh1)*(a*d_t0dem+b)/d_rah1;
-				}
-				d_L=-1004*d_roh1*pow(ustar,3)*d_t0dem/(d_h1*9.81*0.41);
-				d_x=pow((1-16*(5/d_L)),0.25);
-				d_psim=2*log((1+d_x)/2)+log((1+pow(d_x,2))/2)-2*atan(d_x)+0.5*PI;
-				d_psih=2*log((1+pow(d_x,2))/2);
-				d_u5=(ustar/0.41)*log(5/d_z0m);
-				d_rah2=(1/(d_u5*pow(0.41,2)))*log((5/d_z0m)-d_psim)*log((5/(d_z0m*0.1))-d_psih);
-				if(row==rowDry&&col==colDry){/*collect dry pix info*/
-					d_rah_dry = d_rah2;
-					d_h_dry = d_h1;
-				}
-				d_Rah[row][col] = d_rah1;
-			}
+    h_dry = d_Rn_dry - d_g0_dry;
+    G_message("h_dry = %f", h_dry);
+    G_message("t0dem_dry = %f", d_t0dem_dry);
+    G_message("t0dem_wet = %f", d_t0dem_wet);
+    DCELL d_rah_dry;
+
+    DCELL d_roh_dry;
+
+    /* INITIALIZATION */
+    for (row = 0; row < nrows; row++) {
+	DCELL d_t0dem;
+
+	DCELL d_z0m;
+
+	DCELL d_rah1;
+
+	DCELL d_roh1;
+
+	DCELL d_u5;
+
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) <
+	    0)
+	    G_fatal_error(_("Could not read from <%s>"), t0dem);
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = (double)((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    switch (data_type_t0dem) {
+	    case CELL_TYPE:
+		d_t0dem = (double)((CELL *) inrast_t0dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_t0dem = (double)((FCELL *) inrast_t0dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_t0dem = (double)((DCELL *) inrast_t0dem)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_t0dem) || G_is_d_null_value(&d_z0m)) {
+		/* do nothing */
+		d_Roh[row][col] = -999.9;
+		d_Rah[row][col] = -999.9;
+	    }
+	    else {
+		d_u5 = (ustar / 0.41) * log(5 / d_z0m);
+		d_rah1 =
+		    (1 / (d_u5 * pow(0.41, 2))) * log(5 / d_z0m) * log(5 /
+								       (d_z0m
+									*
+									0.1));
+		d_roh1 =
+		    ((998 - ea) / (d_t0dem * 2.87)) + (ea / (d_t0dem * 4.61));
+		if (d_roh1 > 5) {
+		    d_roh1 = 1.0;
 		}
+		else {
+		    d_roh1 =
+			((1000 - 4.65) / (d_t0dem * 2.87)) +
+			(4.65 / (d_t0dem * 4.61));
+		}
+		if (row == rowDry && col == colDry) {	/*collect dry pix info */
+		    d_rah_dry = d_rah1;
+		    d_roh_dry = d_roh1;
+		    G_message("d_rah_dry=%f d_roh_dry=%f", d_rah_dry,
+			      d_roh_dry);
+		}
+		d_Roh[row][col] = d_roh1;
+		d_Rah[row][col] = d_rah1;
+	    }
 	}
+    }
+    DCELL d_dT_dry;
 
-	/*Calculate dT_dry*/	
-	d_dT_dry = (d_h_dry*d_rah_dry)/(1004*d_roh_dry);
-	/*Calculate coefficients for next dT equation*/	
-/*	a = (d_dT_dry-0)/(d_t0dem_dry-d_t0dem_wet);*/
-/*	b = (-1.0) * ( a * d_t0dem_wet );*/
-/*	G_message("d_dT_dry=%f",d_dT_dry);*/
-/*	G_message("dT2=%f * t0dem + (%f)", a, b);*/
-	sumx=d_t0dem_wet+d_t0dem_dry;
-	sumy=d_dT_dry+0.0;
-	sumx2=pow(d_t0dem_wet,2)+pow(d_t0dem_dry,2);
-	sumxy=(d_t0dem_wet*0.0)+(d_t0dem_dry*d_dT_dry);
-	a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx,2)/2.0));
-	b = (sumy - ( a * sumx)) / 2.0; 
-	G_message("d_dT_dry=%f",d_dT_dry);
-	G_message("dT1=%f * t0dem + (%f)", a, b);
+    /*Calculate dT_dry */
+    d_dT_dry = (h_dry * d_rah_dry) / (1004 * d_roh_dry);
+    double a, b;
 
-	/* ITERATION 2 */
+    /*Calculate coefficients for next dT equation */
+    /*a = 1.0/ ((d_dT_dry-0.0) / (d_t0dem_dry-d_t0dem_wet)); */
+    /*b = ( a * d_t0dem_wet ) * (-1.0); */
+
+    double sumx = d_t0dem_wet + d_t0dem_dry;
+
+    double sumy = d_dT_dry + 0.0;
+
+    double sumx2 = pow(d_t0dem_wet, 2) + pow(d_t0dem_dry, 2);
+
+    double sumxy = (d_t0dem_wet * 0.0) + (d_t0dem_dry * d_dT_dry);
+
+    a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx, 2) / 2.0));
+    b = (sumy - (a * sumx)) / 2.0;
+    G_message("d_dT_dry=%f", d_dT_dry);
+    G_message("dT1=%f * t0dem + (%f)", a, b);
+
+    DCELL d_h_dry;
+
+    /* ITERATION 1 */
+
+    for (row = 0; row < nrows; row++) {
+	DCELL d_t0dem;
+
+	DCELL d_z0m;
+
+	DCELL d_h1;
+
+	DCELL d_rah1;
+
+	DCELL d_rah2;
+
+	DCELL d_roh1;
+
+	DCELL d_L;
+
+	DCELL d_x;
+
+	DCELL d_psih;
+
+	DCELL d_psim;
+
+	DCELL d_u5;
+
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) <
+	    0)
+	    G_fatal_error(_("Could not read from <%s>"), t0dem);
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = (double)((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    switch (data_type_t0dem) {
+	    case CELL_TYPE:
+		d_t0dem = (double)((CELL *) inrast_t0dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_t0dem = (double)((FCELL *) inrast_t0dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_t0dem = (double)((DCELL *) inrast_t0dem)[col];
+		break;
+	    }
+	    d_rah1 = d_Rah[row][col];
+	    d_roh1 = d_Roh[row][col];
+	    if (G_is_d_null_value(&d_t0dem) || G_is_d_null_value(&d_z0m)) {
+		/* do nothing */
+	    }
+	    else {
+		if (d_rah1 < 1.0) {
+		    d_h1 = 0.0;
+		}
+		else {
+		    d_h1 = (1004 * d_roh1) * (a * d_t0dem + b) / d_rah1;
+		}
+		d_L =
+		    -1004 * d_roh1 * pow(ustar,
+					 3) * d_t0dem / (d_h1 * 9.81 * 0.41);
+		d_x = pow((1 - 16 * (5 / d_L)), 0.25);
+		d_psim =
+		    2 * log((1 + d_x) / 2) + log((1 + pow(d_x, 2)) / 2) -
+		    2 * atan(d_x) + 0.5 * PI;
+		d_psih = 2 * log((1 + pow(d_x, 2)) / 2);
+		d_u5 = (ustar / 0.41) * log(5 / d_z0m);
+		d_rah2 =
+		    (1 / (d_u5 * pow(0.41, 2))) * log((5 / d_z0m) -
+						      d_psim) * log((5 /
+								     (d_z0m *
+								      0.1)) -
+								    d_psih);
+		if (row == rowDry && col == colDry) {	/*collect dry pix info */
+		    d_rah_dry = d_rah2;
+		    d_h_dry = d_h1;
+		}
+		d_Rah[row][col] = d_rah1;
+	    }
+	}
+    }
+
+    /*Calculate dT_dry */
+    d_dT_dry = (d_h_dry * d_rah_dry) / (1004 * d_roh_dry);
+    /*Calculate coefficients for next dT equation */
+    /*      a = (d_dT_dry-0)/(d_t0dem_dry-d_t0dem_wet); */
+    /*      b = (-1.0) * ( a * d_t0dem_wet ); */
+    /*      G_message("d_dT_dry=%f",d_dT_dry); */
+    /*      G_message("dT2=%f * t0dem + (%f)", a, b); */
+    sumx = d_t0dem_wet + d_t0dem_dry;
+    sumy = d_dT_dry + 0.0;
+    sumx2 = pow(d_t0dem_wet, 2) + pow(d_t0dem_dry, 2);
+    sumxy = (d_t0dem_wet * 0.0) + (d_t0dem_dry * d_dT_dry);
+    a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx, 2) / 2.0));
+    b = (sumy - (a * sumx)) / 2.0;
+    G_message("d_dT_dry=%f", d_dT_dry);
+    G_message("dT1=%f * t0dem + (%f)", a, b);
+
+    /* ITERATION 2 */
+
 	/***************************************************/
+
 	/***************************************************/
-	
-	for (row = 0; row < nrows; row++)
-	{	
-		DCELL d_t0dem;
-		DCELL d_z0m; 	
-		DCELL d_rah2; 	
-		DCELL d_rah3; 	
-		DCELL d_roh1; 	
-		DCELL d_h2; 	
-		DCELL d_L; 	
-		DCELL d_x; 	
-		DCELL d_psih; 	
-		DCELL d_psim; 	
-		DCELL d_u5;
-		G_percent(row,nrows,2);
-		/* read a line input maps into buffers*/	
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem) < 0)
-			G_fatal_error(_("Could not read from <%s>"),t0dem);
-		/* read every cell in the line buffers */
-		for(col=0;col<ncols;col++)
-		{
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = (double) ((DCELL *) inrast_z0m)[col];
-					break;
-			}
-			switch(data_type_t0dem){
-				case CELL_TYPE:
-					d_t0dem = (double) ((CELL *) inrast_t0dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_t0dem = (double) ((FCELL *) inrast_t0dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_t0dem = (double) ((DCELL *) inrast_t0dem)[col];
-					break;
-			}
-			d_rah2 = d_Rah[row][col];
-			d_roh1 = d_Roh[row][col];
-			if(G_is_d_null_value(&d_t0dem) ||
-				G_is_d_null_value(&d_z0m)){
-				/* do nothing */
-			} else {
-				if(d_rah2<1.0){
-					d_h2=0.0;
-				} else {
-					d_h2 = (1004*d_roh1)*(a*d_t0dem+b)/d_rah2;
-				}
-				d_L=-1004*d_roh1*pow(ustar,3)*d_t0dem/(d_h2*9.81*0.41);
-				d_x=pow((1-16*(5/d_L)),0.25);
-				d_psim=2*log((1+d_x)/2)+log((1+pow(d_x,2))/2)-2*atan(d_x)+0.5*PI;
-				d_psih=2*log((1+pow(d_x,2))/2);
-				d_u5=(ustar/0.41)*log(5/d_z0m);
-				d_rah3=(1/(d_u5*pow(0.41,2)))*log((5/d_z0m)-d_psim)*log((5/(d_z0m*0.1))-d_psih);
-				if(row==rowDry&&col==colDry){/*collect dry pix info*/
-					d_rah_dry = d_rah2;
-					d_h_dry = d_h2;
-				}
-				d_Rah[row][col] = d_rah2;
-			}
+
+    for (row = 0; row < nrows; row++) {
+	DCELL d_t0dem;
+
+	DCELL d_z0m;
+
+	DCELL d_rah2;
+
+	DCELL d_rah3;
+
+	DCELL d_roh1;
+
+	DCELL d_h2;
+
+	DCELL d_L;
+
+	DCELL d_x;
+
+	DCELL d_psih;
+
+	DCELL d_psim;
+
+	DCELL d_u5;
+
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) <
+	    0)
+	    G_fatal_error(_("Could not read from <%s>"), t0dem);
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = (double)((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    switch (data_type_t0dem) {
+	    case CELL_TYPE:
+		d_t0dem = (double)((CELL *) inrast_t0dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_t0dem = (double)((FCELL *) inrast_t0dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_t0dem = (double)((DCELL *) inrast_t0dem)[col];
+		break;
+	    }
+	    d_rah2 = d_Rah[row][col];
+	    d_roh1 = d_Roh[row][col];
+	    if (G_is_d_null_value(&d_t0dem) || G_is_d_null_value(&d_z0m)) {
+		/* do nothing */
+	    }
+	    else {
+		if (d_rah2 < 1.0) {
+		    d_h2 = 0.0;
 		}
+		else {
+		    d_h2 = (1004 * d_roh1) * (a * d_t0dem + b) / d_rah2;
+		}
+		d_L =
+		    -1004 * d_roh1 * pow(ustar,
+					 3) * d_t0dem / (d_h2 * 9.81 * 0.41);
+		d_x = pow((1 - 16 * (5 / d_L)), 0.25);
+		d_psim =
+		    2 * log((1 + d_x) / 2) + log((1 + pow(d_x, 2)) / 2) -
+		    2 * atan(d_x) + 0.5 * PI;
+		d_psih = 2 * log((1 + pow(d_x, 2)) / 2);
+		d_u5 = (ustar / 0.41) * log(5 / d_z0m);
+		d_rah3 =
+		    (1 / (d_u5 * pow(0.41, 2))) * log((5 / d_z0m) -
+						      d_psim) * log((5 /
+								     (d_z0m *
+								      0.1)) -
+								    d_psih);
+		if (row == rowDry && col == colDry) {	/*collect dry pix info */
+		    d_rah_dry = d_rah2;
+		    d_h_dry = d_h2;
+		}
+		d_Rah[row][col] = d_rah2;
+	    }
 	}
+    }
 
-	/*Calculate dT_dry*/	
-	d_dT_dry = (d_h_dry*d_rah_dry)/(1004*d_roh_dry);
-	/*Calculate coefficients for next dT equation*/	
-/*	a = (d_dT_dry-0)/(d_t0dem_dry-d_t0dem_wet);*/
-/*	b = (-1.0) * ( a * d_t0dem_wet );*/
-/*	G_message("d_dT_dry=%f",d_dT_dry);*/
-/*	G_message("dT3=%f * t0dem + (%f)", a, b);*/
-	sumx=d_t0dem_wet+d_t0dem_dry;
-	sumy=d_dT_dry+0.0;
-	sumx2=pow(d_t0dem_wet,2)+pow(d_t0dem_dry,2);
-	sumxy=(d_t0dem_wet*0.0)+(d_t0dem_dry*d_dT_dry);
-	a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx,2)/2.0));
-	b = (sumy - ( a * sumx)) / 2.0; 
-	G_message("d_dT_dry=%f",d_dT_dry);
-	G_message("dT1=%f * t0dem + (%f)", a, b);
+    /*Calculate dT_dry */
+    d_dT_dry = (d_h_dry * d_rah_dry) / (1004 * d_roh_dry);
+    /*Calculate coefficients for next dT equation */
+    /*      a = (d_dT_dry-0)/(d_t0dem_dry-d_t0dem_wet); */
+    /*      b = (-1.0) * ( a * d_t0dem_wet ); */
+    /*      G_message("d_dT_dry=%f",d_dT_dry); */
+    /*      G_message("dT3=%f * t0dem + (%f)", a, b); */
+    sumx = d_t0dem_wet + d_t0dem_dry;
+    sumy = d_dT_dry + 0.0;
+    sumx2 = pow(d_t0dem_wet, 2) + pow(d_t0dem_dry, 2);
+    sumxy = (d_t0dem_wet * 0.0) + (d_t0dem_dry * d_dT_dry);
+    a = (sumxy - ((sumx * sumy) / 2.0)) / (sumx2 - (pow(sumx, 2) / 2.0));
+    b = (sumy - (a * sumx)) / 2.0;
+    G_message("d_dT_dry=%f", d_dT_dry);
+    G_message("dT1=%f * t0dem + (%f)", a, b);
 
 
 
-	/* ITERATION 3 */
+    /* ITERATION 3 */
+
 	/***************************************************/
+
 	/***************************************************/
-	
-	for (row = 0; row < nrows; row++)
-	{	
-		DCELL d_t0dem;
-		DCELL d_z0m; 	
-		DCELL d_rah3; 	
-		DCELL d_roh1; 	
-		DCELL d_h3; 	
-		DCELL d_L; 	
-		DCELL d_x; 	
-		DCELL d_psih; 	
-		DCELL d_psim; 	
-		DCELL d;	/* Output pixel */
-		G_percent(row,nrows,2);
-		/* read a line input maps into buffers*/	
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		if(G_get_raster_row(infd_t0dem,inrast_t0dem,row,data_type_t0dem) < 0)
-			G_fatal_error(_("Could not read from <%s>"),t0dem);
-		/* read every cell in the line buffers */
-		for(col=0;col<ncols;col++)
-		{
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = (double) ((DCELL *) inrast_z0m)[col];
-					break;
-			}
-			switch(data_type_t0dem){
-				case CELL_TYPE:
-					d_t0dem = (double) ((CELL *) inrast_t0dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_t0dem = (double) ((FCELL *) inrast_t0dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_t0dem = (double) ((DCELL *) inrast_t0dem)[col];
-					break;
-			}
-			d_rah3 = d_Rah[row][col];
-			d_roh1 = d_Roh[row][col];
-			if(G_is_d_null_value(&d_t0dem) ||
-				G_is_d_null_value(&d_z0m)){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				if(d_rah3 < 1.0){
-					d_h3=0.0;
-				} else {
-					d_h3 = (1004*d_roh1)*(a*d_t0dem+b)/d_rah3;
-				}
-				if( d_h3 < 0 && d_h3 > -50 ){
-					d_h3 = 0.0;
-				}
-				if( d_h3 < -50 || d_h3 > 1000 ){
-					G_set_d_null_value(&outrast[col],1);
-				}
-				outrast[col] = d_h3;
-			}
+
+    for (row = 0; row < nrows; row++) {
+	DCELL d_t0dem;
+
+	DCELL d_z0m;
+
+	DCELL d_rah3;
+
+	DCELL d_roh1;
+
+	DCELL d_h3;
+
+	DCELL d_L;
+
+	DCELL d_x;
+
+	DCELL d_psih;
+
+	DCELL d_psim;
+
+	DCELL d;		/* Output pixel */
+
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	if (G_get_raster_row(infd_t0dem, inrast_t0dem, row, data_type_t0dem) <
+	    0)
+	    G_fatal_error(_("Could not read from <%s>"), t0dem);
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = (double)((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    switch (data_type_t0dem) {
+	    case CELL_TYPE:
+		d_t0dem = (double)((CELL *) inrast_t0dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_t0dem = (double)((FCELL *) inrast_t0dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_t0dem = (double)((DCELL *) inrast_t0dem)[col];
+		break;
+	    }
+	    d_rah3 = d_Rah[row][col];
+	    d_roh1 = d_Roh[row][col];
+	    if (G_is_d_null_value(&d_t0dem) || G_is_d_null_value(&d_z0m)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		if (d_rah3 < 1.0) {
+		    d_h3 = 0.0;
 		}
-		if (G_put_d_raster_row (outfd, outrast) < 0)
-			G_fatal_error ("Cannot write to output file <%s>",h0);
+		else {
+		    d_h3 = (1004 * d_roh1) * (a * d_t0dem + b) / d_rah3;
+		}
+		if (d_h3 < 0 && d_h3 > -50) {
+		    d_h3 = 0.0;
+		}
+		if (d_h3 < -50 || d_h3 > 1000) {
+		    G_set_d_null_value(&outrast[col], 1);
+		}
+		outrast[col] = d_h3;
+	    }
 	}
+	if (G_put_d_raster_row(outfd, outrast) < 0)
+	    G_fatal_error("Cannot write to output file <%s>", h0);
+    }
 
 
-	G_free(inrast_z0m);
-	G_close_cell (infd_z0m);
-	G_free(inrast_t0dem);
-	G_close_cell (infd_t0dem);
+    G_free(inrast_z0m);
+    G_close_cell(infd_z0m);
+    G_free(inrast_t0dem);
+    G_close_cell(infd_t0dem);
 
-	G_free(outrast);
-	G_close_cell (outfd);
-        
-	/* add command line incantation to history file */
-        G_short_history(h0, "raster", &history);
-        G_command_history(&history);
-        G_write_history(h0, &history);
+    G_free(outrast);
+    G_close_cell(outfd);
 
-	exit(EXIT_SUCCESS);
+    /* add command line incantation to history file */
+    G_short_history(h0, "raster", &history);
+    G_command_history(&history);
+    G_write_history(h0, &history);
+
+    exit(EXIT_SUCCESS);
 }

Modified: grass-addons/gipe/i.eb.h_SEBAL95/U_0.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/U_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/U_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,10 +4,11 @@
 
 double U_0(double zom_0, double u2m)
 {
-	double u_0;
+    double u_0;
 
-	u_0 = u2m*0.41*log10(200/(0.15/7))/(log10(2/(0.15/7))*log10(200/zom_0)); 
+    u_0 =
+	u2m * 0.41 * log10(200 / (0.15 / 7)) / (log10(2 / (0.15 / 7)) *
+						log10(200 / zom_0));
 
-	return (u_0);
+    return (u_0);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/dtair.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/dtair.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/dtair.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,15 +4,16 @@
 
 /* Pixel-based input required are: tempk water & desert
  * additionally, dtair in Desert point should be given
- */ 
+ */
 
 
-double dt_air(double t0_dem, double tempk_water, double tempk_desert, double dtair_desert)
+double dt_air(double t0_dem, double tempk_water, double tempk_desert,
+	      double dtair_desert)
 {
-	double a, b, result;
-	a = (dtair_desert-0.0)/(tempk_desert-tempk_water);
-	b = dtair_desert - a * tempk_desert;
-	result = t0_dem * a + b;
-	return result;
+    double a, b, result;
+
+    a = (dtair_desert - 0.0) / (tempk_desert - tempk_water);
+    b = dtair_desert - a * tempk_desert;
+    result = t0_dem * a + b;
+    return result;
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/dtair_desert.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/dtair_desert.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/dtair_desert.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,14 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-#include"functions.h"
-
-double dt_air_desert(double h_desert, double roh_air_desert, double rah_desert)
-{
-	double result;
-	
-	result = (h_desert * rah_desert)/(roh_air_desert * 1004);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+#include"functions.h"
+ double dt_air_desert(double h_desert, double roh_air_desert,
+		       double rah_desert) 
+{
+    double result;
+
+    result = (h_desert * rah_desert) / (roh_air_desert * 1004);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.eb.h_SEBAL95/functions.h
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/functions.h	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/functions.h	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,51 +1,82 @@
 /* These are the headers for the RS functions used in SEBAL */
-/* ychemin at yahoo.com - yann.chemin at ait.ac.th */
-/* LGPL - 2004 */
+/* 2004 */
 
 /* Initial functions */
-double DN2Rad_landsat7( double Lmin, double LMax, double QCalMax, double QCalmin, int DN );
-double Rad2Ref_landsat7( double radiance, double doy,double sun_elevation, double k_exo );
-double tempk_landsat7( double chan6 );
-double bb_alb_aster( double greenchan, double redchan, double nirchan, double swirchan1, double swirchan2, double swirchan3, double swirchan4, double swirchan5, double swirchan6 );
-double bb_alb_landsat( double bluechan, double greenchan, double redchan, double nirchan, double chan5, double chan7 );
-double bb_alb_noaa( double redchan, double nirchan );
-double bb_alb_modis( double redchan, double nirchan, double chan3, double chan4, double chan5, double chan6, double chan7 );
-double nd_vi( double redchan, double nirchan );
-double emissivity_generic( double ndvi );
-double emissivity_modis( double e31, double e32 );
-double t0_dem( double dem, double tempk );
-double t_air_approx( double temp_k );
+double DN2Rad_landsat7(double Lmin, double LMax, double QCalMax,
+		       double QCalmin, int DN);
+double Rad2Ref_landsat7(double radiance, double doy, double sun_elevation,
+			double k_exo);
+double tempk_landsat7(double chan6);
 
+double bb_alb_aster(double greenchan, double redchan, double nirchan,
+		    double swirchan1, double swirchan2, double swirchan3,
+		    double swirchan4, double swirchan5, double swirchan6);
+double bb_alb_landsat(double bluechan, double greenchan, double redchan,
+		      double nirchan, double chan5, double chan7);
+double bb_alb_noaa(double redchan, double nirchan);
+
+double bb_alb_modis(double redchan, double nirchan, double chan3,
+		    double chan4, double chan5, double chan6, double chan7);
+double nd_vi(double redchan, double nirchan);
+
+double emissivity_generic(double ndvi);
+
+double emissivity_modis(double e31, double e32);
+
+double t0_dem(double dem, double tempk);
+
+double t_air_approx(double temp_k);
+
 /* Instantaneous rnet and g0 */
-double r_net( double bbalb, double ndvi, double tempk, double tair,  double e0, double tsw, double doy, double utc, double sunzangle );
+double r_net(double bbalb, double ndvi, double tempk, double tair, double e0,
+	     double tsw, double doy, double utc, double sunzangle);
 double g_0(double bbalb, double ndvi, double tempk, double rnet, double utc);
 
 /* Diurnal r_net and etpot */
-double solar_day(double lat, double doy, double tsw );
-double r_net_day( double bbalb, double solar, double tsw );
-double et_pot_day( double bbalb, double solar, double tempk, double tsw );
+double solar_day(double lat, double doy, double tsw);
 
+double r_net_day(double bbalb, double solar, double tsw);
+
+double et_pot_day(double bbalb, double solar, double tempk, double tsw);
+
 /* Sensible heat flux functions */
-double sensi_h( int iteration, double tempk_water, double tempk_desert, double t0_dem, double tempk, double ndvi, double ndvi_max, double dem, double rnet_desert, double g0_desert, double t0_dem_desert, double u2m, double dem_desert);
+double sensi_h(int iteration, double tempk_water, double tempk_desert,
+	       double t0_dem, double tempk, double ndvi, double ndvi_max,
+	       double dem, double rnet_desert, double g0_desert,
+	       double t0_dem_desert, double u2m, double dem_desert);
 double roh_air_0(double tempk);
+
 double zom_0(double ndvi, double ndvi_max);
+
 double U_0(double zom_0, double u2m);
+
 double rah_0(double zom_0, double u_0);
+
 double h_0(double roh_air, double rah, double dtair);
-double dt_air_approx( double temp_k );
+
+double dt_air_approx(double temp_k);
+
 double dt_air_0(double t0_dem, double tempk_water, double tempk_desert);
-double dt_air_desert(double h_desert, double roh_air_desert, double rah_desert);
-double dt_air(double t0_dem, double tempk_water, double tempk_desert, double dtair_desert);
+
+double dt_air_desert(double h_desert, double roh_air_desert,
+		     double rah_desert);
+double dt_air(double t0_dem, double tempk_water, double tempk_desert,
+	      double dtair_desert);
 double rohair(double dem, double tempk, double dtair);
+
 double h1(double roh_air, double rah, double dtair);
-double u_star(double t0_dem, double h, double ustar, double roh_air, double zom, double u2m);
+
+double u_star(double t0_dem, double h, double ustar, double roh_air,
+	      double zom, double u2m);
 double psi_h(double t0_dem, double h, double U_0, double roh_air);
+
 double rah1(double psih, double u_star);
 
 /* Final outputs */
 double evap_fr(double r_net, double g0, double h);
+
 double et_a(double r_net_day, double evap_f, double tempk);
-double biomass( double ndvi, double solar_day, double evap_fr, double light_use_ef );
-double soilmoisture( double ndvi, double evap_fr );
 
-
+double biomass(double ndvi, double solar_day, double evap_fr,
+	       double light_use_ef);
+double soilmoisture(double ndvi, double evap_fr);

Modified: grass-addons/gipe/i.eb.h_SEBAL95/h1.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/h1.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/h1.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,10 +4,9 @@
 
 double h1(double roh_air, double rah, double dtair)
 {
-	double result;
+    double result;
 
-	result = roh_air*1004*dtair / rah;
-	
-	return result;
-}
+    result = roh_air * 1004 * dtair / rah;
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.h_SEBAL95/h_0.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/h_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/h_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,10 +4,9 @@
 
 double h_0(double roh_air, double rah, double dtair)
 {
-	double result;
+    double result;
 
-	result = roh_air*1004*(dtair) / rah;
-	
-	return (result);
-}
+    result = roh_air * 1004 * (dtair) / rah;
 
+    return (result);
+}

Modified: grass-addons/gipe/i.eb.h_SEBAL95/main.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.eb.h_SEBAL95
@@ -24,949 +25,1054 @@
 #include <math.h>
 #include <grass/glocale.h>
 
-double sensi_h( int iteration, double tempk_water, double tempk_desert, double t0_dem, double tempk, double ndvi, double ndvi_max, double dem, double rnet_desert, double g0_desert, double t0_dem_desert, double u2m, double dem_desert);
+double sensi_h(int iteration, double tempk_water, double tempk_desert,
+	       double t0_dem, double tempk, double ndvi, double ndvi_max,
+	       double dem, double rnet_desert, double g0_desert,
+	       double t0_dem_desert, double u2m, double dem_desert);
 
 int main(int argc, char *argv[])
-{	
-	struct Cell_head cellhd;
-	char *mapset; /* mapset name*/
-	
-	/* buffer for in out raster */
-	void *inrast_T,*inrast_ndvi,*inrast_dem, *inrast_u2m;
-	void *inrast_Rn,*inrast_g0,*inrast_albedo;
-	DCELL *outrast;
-	
-	int nrows, ncols;
-	int row, col;
-	int row_wet, col_wet;
-	int row_dry, col_dry;
-	double m_row_wet, m_col_wet;
-	double m_row_dry, m_col_dry;
-	int infd_T,infd_ndvi,infd_dem,infd_u2m;
-	int infd_Rn,infd_g0,infd_albedo;
-	int outfd;
-	
-	char *mapset_T,*mapset_ndvi,*mapset_dem,*mapset_u2m;
-	char *mapset_Rn,*mapset_g0,*mapset_albedo;
-	char *T, *ndvi, *dem, *u2m, *Rn, *g0, *albedo; 
-	char *h0;
-	
-        struct History history;
-	struct GModule *module;
-	struct Option *input_T, *input_ndvi, *input_dem, *input_u2m;
-	struct Option *input_Rn, *input_g0, *input_albedo;
-	struct Option *output;
-	struct Option *input_row_wet, *input_col_wet;
-	struct Option *input_row_dry, *input_col_dry;
-	struct Option *input_iter;
-	struct Flag *flag1, *flag2, *flag3, *day, *zero;
+{
+    struct Cell_head cellhd;
+
+    char *mapset;		/* mapset name */
+
+    /* buffer for in out raster */
+    void *inrast_T, *inrast_ndvi, *inrast_dem, *inrast_u2m;
+
+    void *inrast_Rn, *inrast_g0, *inrast_albedo;
+
+    DCELL *outrast;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int row_wet, col_wet;
+
+    int row_dry, col_dry;
+
+    double m_row_wet, m_col_wet;
+
+    double m_row_dry, m_col_dry;
+
+    int infd_T, infd_ndvi, infd_dem, infd_u2m;
+
+    int infd_Rn, infd_g0, infd_albedo;
+
+    int outfd;
+
+    char *mapset_T, *mapset_ndvi, *mapset_dem, *mapset_u2m;
+
+    char *mapset_Rn, *mapset_g0, *mapset_albedo;
+
+    char *T, *ndvi, *dem, *u2m, *Rn, *g0, *albedo;
+
+    char *h0;
+
+    struct History history;
+
+    struct GModule *module;
+
+    struct Option *input_T, *input_ndvi, *input_dem, *input_u2m;
+
+    struct Option *input_Rn, *input_g0, *input_albedo;
+
+    struct Option *output;
+
+    struct Option *input_row_wet, *input_col_wet;
+
+    struct Option *input_row_dry, *input_col_dry;
+
+    struct Option *input_iter;
+
+    struct Flag *flag1, *flag2, *flag3, *day, *zero;
+
 	/*******************************/
-	RASTER_MAP_TYPE data_type_T;
-	RASTER_MAP_TYPE data_type_ndvi;
-	RASTER_MAP_TYPE data_type_dem;
-	RASTER_MAP_TYPE data_type_u2m;
-	RASTER_MAP_TYPE data_type_Rn;
-	RASTER_MAP_TYPE data_type_g0;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_T;
+
+    RASTER_MAP_TYPE data_type_ndvi;
+
+    RASTER_MAP_TYPE data_type_dem;
+
+    RASTER_MAP_TYPE data_type_u2m;
+
+    RASTER_MAP_TYPE data_type_Rn;
+
+    RASTER_MAP_TYPE data_type_g0;
+
+    RASTER_MAP_TYPE data_type_albedo;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
 	/*******************************/
-	int iteration = 10; /*SEBAL95 loop number*/
+    int iteration = 10;		/*SEBAL95 loop number */
+
 	/********************************/
-	/* Stats for dry/wet pixels	*/
-	double t0dem_min=400.0,t0dem_max=200.0;
-	double tempk_min=400.0,tempk_max=200.0;
+    /* Stats for dry/wet pixels     */
+    double t0dem_min = 400.0, t0dem_max = 200.0;
+
+    double tempk_min = 400.0, tempk_max = 200.0;
+
 	/********************************/
-	double xp, yp;
-	double xmin, ymin;
-	double xmax, ymax;
-	double stepx,stepy;
-	double latitude, longitude;
+    double xp, yp;
+
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
 	/********************************/
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description = _("Sensible Heat Flux iteration SEBAL 95");
-	
-	/* Define different options */
-	input_T 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_T->key	= "tempk";
-	input_T->description = _("Name of Surface Skin Temperature input map [K]");
+    G_gisinit(argv[0]);
 
-	input_dem 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_dem->key	= "dem";
-	input_dem->description = _("Name of dem input map [m a.s.l.]");
+    module = G_define_module();
+    module->description = _("Sensible Heat Flux iteration SEBAL 95");
 
-	input_u2m 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_u2m->key	= "u2m";
-	input_u2m->description = _("Name of Wind speed at 2m height input map [m/s]");
-	
-	input_ndvi 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_ndvi->key	= "ndvi";
-	input_ndvi->description = _("Name of NDVI input map [-]");
-	
-	input_albedo 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_albedo->key= "albedo";
-	input_albedo->description = _("Name of Albedo input map [-]");
-	
-	input_Rn 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_Rn->key	= "rnet";
-	input_Rn->description = _("Name of instantaneous Net Solar Radiation input map [W/m2]");
-	
-	input_g0 	= G_define_standard_option(G_OPT_R_INPUT);
-	input_g0->key	= "g0";
-	input_g0->description = _("Name of instantaneous Soil Heat Flux input map [W/m2]");
-	
-	input_iter 		= G_define_option();
-	input_iter->key		= "iteration";
-	input_iter->type	= TYPE_INTEGER;
-	input_iter->required 	= NO;
-	input_iter->gisprompt 	= "old,value";
-	input_iter->description	= _("Value of the number of SEBAL95 loops (default is 10)");
-	input_iter->guisection	= _("Optional");
-	
-	input_row_wet 			= G_define_option();
-	input_row_wet->key		= "row_wet";
-	input_row_wet->type 		= TYPE_INTEGER;
-	input_row_wet->required 	= NO;
-	input_row_wet->gisprompt 	= "old,value";
-	input_row_wet->description 	= _("Row value of the wet pixel");
-	input_row_wet->guisection	= _("Optional");
-	
-	input_col_wet 			= G_define_option();
-	input_col_wet->key		= "col_wet";
-	input_col_wet->type 		= TYPE_INTEGER;
-	input_col_wet->required 	= NO;
-	input_col_wet->gisprompt 	= "old,value";
-	input_col_wet->description 	= _("Column value of the wet pixel");
-	input_col_wet->guisection	= _("Optional");
-	
-	input_row_dry 			= G_define_option();
-	input_row_dry->key		= "row_dry";
-	input_row_dry->type 		= TYPE_INTEGER;
-	input_row_dry->required 	= NO;
-	input_row_dry->gisprompt 	= "old,value";
-	input_row_dry->description 	= _("Row value of the dry pixel");
-	input_row_dry->guisection	= _("Optional");
-	
-	input_col_dry 			= G_define_option();
-	input_col_dry->key		= "col_dry";
-	input_col_dry->type 		= TYPE_INTEGER;
-	input_col_dry->required 	= NO;
-	input_col_dry->gisprompt 	= "old,value";
-	input_col_dry->description 	= _("Column value of the dry pixel");
-	input_col_dry->guisection	= _("Optional");
+    /* Define different options */
+    input_T = G_define_standard_option(G_OPT_R_INPUT);
+    input_T->key = "tempk";
+    input_T->description =
+	_("Name of Surface Skin Temperature input map [K]");
 
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Name of output sensible heat flux layer [W/m2]");
-	
-	/* Define the different flags */
-	flag1 = G_define_flag() ;
-	flag1->key         = 't' ;
-	flag1->description = _("Temperature histogram check (careful!)") ;
+    input_dem = G_define_standard_option(G_OPT_R_INPUT);
+    input_dem->key = "dem";
+    input_dem->description = _("Name of dem input map [m a.s.l.]");
 
-	flag2 = G_define_flag() ;
-	flag2->key         = 'a' ;
-	flag2->description = _("Automatic wet/dry pixel (careful!)") ;
-	
-	flag3 = G_define_flag() ;
-	flag3->key         = 'c' ;
-	flag3->description = _("Coordinates of manual dry/wet pixels are in image projection and not row/col") ;
-	
-	zero = G_define_flag() ;
-	zero->key         = 'z' ;
-	zero->description = _("set negative evapo to zero");
-	
-	if (G_parser(argc, argv))
-		exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	T	= input_T->answer;
-	dem	= input_dem->answer;
-	u2m	= input_u2m->answer;
-	ndvi	= input_ndvi->answer;
-	Rn	= input_Rn->answer;
-	g0	= input_g0->answer;
-	albedo	= input_albedo->answer;
-	
-	h0	= output->answer;
+    input_u2m = G_define_standard_option(G_OPT_R_INPUT);
+    input_u2m->key = "u2m";
+    input_u2m->description =
+	_("Name of Wind speed at 2m height input map [m/s]");
 
-	if(input_iter->answer){
-		iteration=atoi(input_iter->answer);
+    input_ndvi = G_define_standard_option(G_OPT_R_INPUT);
+    input_ndvi->key = "ndvi";
+    input_ndvi->description = _("Name of NDVI input map [-]");
+
+    input_albedo = G_define_standard_option(G_OPT_R_INPUT);
+    input_albedo->key = "albedo";
+    input_albedo->description = _("Name of Albedo input map [-]");
+
+    input_Rn = G_define_standard_option(G_OPT_R_INPUT);
+    input_Rn->key = "rnet";
+    input_Rn->description =
+	_("Name of instantaneous Net Solar Radiation input map [W/m2]");
+
+    input_g0 = G_define_standard_option(G_OPT_R_INPUT);
+    input_g0->key = "g0";
+    input_g0->description =
+	_("Name of instantaneous Soil Heat Flux input map [W/m2]");
+
+    input_iter = G_define_option();
+    input_iter->key = "iteration";
+    input_iter->type = TYPE_INTEGER;
+    input_iter->required = NO;
+    input_iter->gisprompt = "old,value";
+    input_iter->description =
+	_("Value of the number of SEBAL95 loops (default is 10)");
+    input_iter->guisection = _("Optional");
+
+    input_row_wet = G_define_option();
+    input_row_wet->key = "row_wet";
+    input_row_wet->type = TYPE_INTEGER;
+    input_row_wet->required = NO;
+    input_row_wet->gisprompt = "old,value";
+    input_row_wet->description = _("Row value of the wet pixel");
+    input_row_wet->guisection = _("Optional");
+
+    input_col_wet = G_define_option();
+    input_col_wet->key = "col_wet";
+    input_col_wet->type = TYPE_INTEGER;
+    input_col_wet->required = NO;
+    input_col_wet->gisprompt = "old,value";
+    input_col_wet->description = _("Column value of the wet pixel");
+    input_col_wet->guisection = _("Optional");
+
+    input_row_dry = G_define_option();
+    input_row_dry->key = "row_dry";
+    input_row_dry->type = TYPE_INTEGER;
+    input_row_dry->required = NO;
+    input_row_dry->gisprompt = "old,value";
+    input_row_dry->description = _("Row value of the dry pixel");
+    input_row_dry->guisection = _("Optional");
+
+    input_col_dry = G_define_option();
+    input_col_dry->key = "col_dry";
+    input_col_dry->type = TYPE_INTEGER;
+    input_col_dry->required = NO;
+    input_col_dry->gisprompt = "old,value";
+    input_col_dry->description = _("Column value of the dry pixel");
+    input_col_dry->guisection = _("Optional");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Name of output sensible heat flux layer [W/m2]");
+
+    /* Define the different flags */
+    flag1 = G_define_flag();
+    flag1->key = 't';
+    flag1->description = _("Temperature histogram check (careful!)");
+
+    flag2 = G_define_flag();
+    flag2->key = 'a';
+    flag2->description = _("Automatic wet/dry pixel (careful!)");
+
+    flag3 = G_define_flag();
+    flag3->key = 'c';
+    flag3->description =
+	_("Coordinates of manual dry/wet pixels are in image projection and not row/col");
+
+    zero = G_define_flag();
+    zero->key = 'z';
+    zero->description = _("set negative evapo to zero");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    T = input_T->answer;
+    dem = input_dem->answer;
+    u2m = input_u2m->answer;
+    ndvi = input_ndvi->answer;
+    Rn = input_Rn->answer;
+    g0 = input_g0->answer;
+    albedo = input_albedo->answer;
+
+    h0 = output->answer;
+
+    if (input_iter->answer) {
+	iteration = atoi(input_iter->answer);
+    }
+    if (input_row_wet->answer && input_row_dry &&
+	input_col_wet->answer && input_col_dry) {
+	m_row_wet = atof(input_row_wet->answer);
+	m_col_wet = atof(input_col_wet->answer);
+	m_row_dry = atof(input_row_dry->answer);
+	m_col_dry = atof(input_col_dry->answer);
+	if (flag3->answer) {
+	    G_message("Manual wet/dry pixels in image coordinates");
+	    G_message("Wet Pixel=> x:%f y:%f", m_col_wet, m_row_wet);
+	    G_message("Dry Pixel=> x:%f y:%f", m_col_dry, m_row_dry);
 	}
-	if(input_row_wet->answer&&input_row_dry&&
-	input_col_wet->answer&&input_col_dry){
-		m_row_wet = atof(input_row_wet->answer);
-		m_col_wet = atof(input_col_wet->answer);
-		m_row_dry = atof(input_row_dry->answer);
-		m_col_dry = atof(input_col_dry->answer);
-		if(flag3->answer){
-			G_message("Manual wet/dry pixels in image coordinates");
-			G_message("Wet Pixel=> x:%f y:%f",m_col_wet,m_row_wet);
-			G_message("Dry Pixel=> x:%f y:%f",m_col_dry,m_row_dry);
-		} else {
-			G_message("Wet Pixel=> row:%.0f col:%.0f",m_row_wet,m_col_wet);
-			G_message("Dry Pixel=> row:%.0f col:%.0f",m_row_dry,m_col_dry);
-		}
+	else {
+	    G_message("Wet Pixel=> row:%.0f col:%.0f", m_row_wet, m_col_wet);
+	    G_message("Dry Pixel=> row:%.0f col:%.0f", m_row_dry, m_col_dry);
 	}
-	/* find maps in mapset */
-	mapset_T = G_find_cell2 (T, "");
-	if (mapset_T == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), T);
-	mapset_dem = G_find_cell2 (dem, "");
-	if (mapset_dem == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), dem);
-	mapset_u2m = G_find_cell2 (u2m, "");
-	if (mapset_u2m == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), u2m);
-	mapset_ndvi = G_find_cell2 (ndvi, "");
-	if (mapset_ndvi == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), ndvi);
-	mapset_Rn = G_find_cell2 (Rn, "");
-	if (mapset_Rn == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), Rn);
-	mapset_g0 = G_find_cell2 (g0, "");
-	if (mapset_g0 == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), g0);
-	mapset_albedo = G_find_cell2 (albedo, "");
-	if (mapset_albedo == NULL)
-		G_fatal_error(_("cell file [%s] not found"),albedo);
-	
-	/* check legal output name */ 
-	if (G_legal_filename (h0) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), h0);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_T 	= G_raster_map_type(T, mapset_T);
-	data_type_dem 	= G_raster_map_type(dem, mapset_dem);
-	data_type_u2m 	= G_raster_map_type(u2m, mapset_u2m);
-	data_type_ndvi 	= G_raster_map_type(ndvi, mapset_ndvi);
-	data_type_Rn 	= G_raster_map_type(Rn, mapset_Rn);
-	data_type_g0 	= G_raster_map_type(g0, mapset_g0);
-	data_type_albedo = G_raster_map_type(albedo, mapset_albedo);
-	
-	if ( (infd_T = G_open_cell_old (T, mapset_T)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), T);
-	if ( (infd_dem = G_open_cell_old (dem, mapset_dem)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),dem);
-	if ( (infd_u2m = G_open_cell_old (u2m, mapset_u2m)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),u2m);
-	if ( (infd_ndvi = G_open_cell_old (ndvi, mapset_ndvi)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),ndvi);
-	if ( (infd_Rn = G_open_cell_old (Rn, mapset_Rn)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),Rn);
-	if ( (infd_g0 = G_open_cell_old (g0, mapset_g0)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),g0);
-	if((infd_albedo=G_open_cell_old (albedo,mapset_albedo)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"),albedo);
-	
-	if (G_get_cellhd (T, mapset_T, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), T);
-	if (G_get_cellhd (dem, mapset_dem, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), dem);
-	if (G_get_cellhd (u2m, mapset_u2m, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), u2m);
-	if (G_get_cellhd (ndvi, mapset_ndvi, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), ndvi);
-	if (G_get_cellhd (Rn, mapset_Rn, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), Rn);
-	if (G_get_cellhd (g0, mapset_g0, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), g0);
-	if (G_get_cellhd (albedo, mapset_albedo, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), albedo);
-	
-	/* Allocate input buffer */
-	inrast_T  	= G_allocate_raster_buf(data_type_T);
-	inrast_dem 	= G_allocate_raster_buf(data_type_dem);
-	inrast_u2m 	= G_allocate_raster_buf(data_type_u2m);
-	inrast_ndvi 	= G_allocate_raster_buf(data_type_ndvi);
-	inrast_Rn 	= G_allocate_raster_buf(data_type_Rn);
-	inrast_g0 	= G_allocate_raster_buf(data_type_g0);
-	inrast_albedo 	= G_allocate_raster_buf(data_type_albedo);
-	
+    }
+    /* find maps in mapset */
+    mapset_T = G_find_cell2(T, "");
+    if (mapset_T == NULL)
+	G_fatal_error(_("cell file [%s] not found"), T);
+    mapset_dem = G_find_cell2(dem, "");
+    if (mapset_dem == NULL)
+	G_fatal_error(_("cell file [%s] not found"), dem);
+    mapset_u2m = G_find_cell2(u2m, "");
+    if (mapset_u2m == NULL)
+	G_fatal_error(_("cell file [%s] not found"), u2m);
+    mapset_ndvi = G_find_cell2(ndvi, "");
+    if (mapset_ndvi == NULL)
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    mapset_Rn = G_find_cell2(Rn, "");
+    if (mapset_Rn == NULL)
+	G_fatal_error(_("cell file [%s] not found"), Rn);
+    mapset_g0 = G_find_cell2(g0, "");
+    if (mapset_g0 == NULL)
+	G_fatal_error(_("cell file [%s] not found"), g0);
+    mapset_albedo = G_find_cell2(albedo, "");
+    if (mapset_albedo == NULL)
+	G_fatal_error(_("cell file [%s] not found"), albedo);
+
+    /* check legal output name */
+    if (G_legal_filename(h0) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), h0);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_T = G_raster_map_type(T, mapset_T);
+    data_type_dem = G_raster_map_type(dem, mapset_dem);
+    data_type_u2m = G_raster_map_type(u2m, mapset_u2m);
+    data_type_ndvi = G_raster_map_type(ndvi, mapset_ndvi);
+    data_type_Rn = G_raster_map_type(Rn, mapset_Rn);
+    data_type_g0 = G_raster_map_type(g0, mapset_g0);
+    data_type_albedo = G_raster_map_type(albedo, mapset_albedo);
+
+    if ((infd_T = G_open_cell_old(T, mapset_T)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), T);
+    if ((infd_dem = G_open_cell_old(dem, mapset_dem)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), dem);
+    if ((infd_u2m = G_open_cell_old(u2m, mapset_u2m)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), u2m);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset_ndvi)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if ((infd_Rn = G_open_cell_old(Rn, mapset_Rn)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), Rn);
+    if ((infd_g0 = G_open_cell_old(g0, mapset_g0)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), g0);
+    if ((infd_albedo = G_open_cell_old(albedo, mapset_albedo)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+
+    if (G_get_cellhd(T, mapset_T, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), T);
+    if (G_get_cellhd(dem, mapset_dem, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), dem);
+    if (G_get_cellhd(u2m, mapset_u2m, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), u2m);
+    if (G_get_cellhd(ndvi, mapset_ndvi, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
+    if (G_get_cellhd(Rn, mapset_Rn, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), Rn);
+    if (G_get_cellhd(g0, mapset_g0, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), g0);
+    if (G_get_cellhd(albedo, mapset_albedo, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), albedo);
+
+    /* Allocate input buffer */
+    inrast_T = G_allocate_raster_buf(data_type_T);
+    inrast_dem = G_allocate_raster_buf(data_type_dem);
+    inrast_u2m = G_allocate_raster_buf(data_type_u2m);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    inrast_Rn = G_allocate_raster_buf(data_type_Rn);
+    inrast_g0 = G_allocate_raster_buf(data_type_g0);
+    inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+
 	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
+    G_debug(3, "number of rows %d", cellhd.rows);
+
 	/***************************************************/
-	/* Setup pixel location variables */
+    /* Setup pixel location variables */
+
 	/***************************************************/
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
 
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
 
-	nrows = G_window_rows();
-	ncols = G_window_cols();
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
 	/***************************************************/
-	/* Allocate output buffer */
+    /* Allocate output buffer */
+
 	/***************************************************/
-	outrast = G_allocate_d_raster_buf();
+    outrast = G_allocate_d_raster_buf();
 
-	if((outfd = G_open_raster_new (h0,DCELL_TYPE)) < 0)
-		G_fatal_error (_("Could not open <%s>"),h0);
+    if ((outfd = G_open_raster_new(h0, DCELL_TYPE)) < 0)
+	G_fatal_error(_("Could not open <%s>"), h0);
 
-	DCELL d_ndvi; 		/* Input raster */
-	DCELL d_ndvi_max = 0.0;	/* Generated here */
-	/* THREAD 1 */
-	/* NDVI Max */
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		if (G_get_d_raster_row (infd_ndvi, inrast_ndvi, row) < 0)
-			G_fatal_error (_("Could not read from <%s>"),ndvi);
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_ndvi)){
-				/* do nothing */ 
-			} else if ((d_ndvi)>d_ndvi_max&&(d_ndvi)<0.999){
-				d_ndvi_max	= d_ndvi;
-			}
-		}
+    DCELL d_ndvi;		/* Input raster */
+
+    DCELL d_ndvi_max = 0.0;	/* Generated here */
+
+    /* THREAD 1 */
+    /* NDVI Max */
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	if (G_get_d_raster_row(infd_ndvi, inrast_ndvi, row) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_ndvi)) {
+		/* do nothing */
+	    }
+	    else if ((d_ndvi) > d_ndvi_max && (d_ndvi) < 0.999) {
+		d_ndvi_max = d_ndvi;
+	    }
 	}
-	G_message("ndvi_max=%f\n",d_ndvi_max);
-	/* Pick up wet and dry pixel values */
-	DCELL d_Rn; 		/* Input raster */
-	DCELL d_g0; 		/* Input raster */
-	DCELL d_tempk_wet;
-	DCELL d_tempk_dry;
-	DCELL d_Rn_dry;
-	DCELL d_g0_dry;
-	DCELL d_t0dem_dry;
-	DCELL d_dem_dry;
-	DCELL d_dT_dry;
-	DCELL d_ndvi_dry;
-	DCELL d_dT;
-	/*START Temperature minimum search */
-	/* THREAD 1 */
-	/*This is correcting for un-Earthly temperatures*/
-	/*It finds when histogram is actually starting to pull up...*/
-	int peak1, peak2, peak3;
-	int i_peak1, i_peak2, i_peak3;
-	int bottom1a, bottom1b;
-	int bottom2a, bottom2b;
-	int bottom3a, bottom3b;
-	int i_bottom1a, i_bottom1b;
-	int i_bottom2a, i_bottom2b;
-	int i_bottom3a, i_bottom3b;
-	if(flag1->answer){
-		int i=0;
-		int histogram[400];
-		for (i=0;i<400;i++){
-			histogram[i]=0;
-		}
-		DCELL d_T;
+    }
+    G_message("ndvi_max=%f\n", d_ndvi_max);
+    /* Pick up wet and dry pixel values */
+    DCELL d_Rn;			/* Input raster */
+
+    DCELL d_g0;			/* Input raster */
+
+    DCELL d_tempk_wet;
+
+    DCELL d_tempk_dry;
+
+    DCELL d_Rn_dry;
+
+    DCELL d_g0_dry;
+
+    DCELL d_t0dem_dry;
+
+    DCELL d_dem_dry;
+
+    DCELL d_dT_dry;
+
+    DCELL d_ndvi_dry;
+
+    DCELL d_dT;
+
+    /*START Temperature minimum search */
+    /* THREAD 1 */
+    /*This is correcting for un-Earthly temperatures */
+    /*It finds when histogram is actually starting to pull up... */
+    int peak1, peak2, peak3;
+
+    int i_peak1, i_peak2, i_peak3;
+
+    int bottom1a, bottom1b;
+
+    int bottom2a, bottom2b;
+
+    int bottom3a, bottom3b;
+
+    int i_bottom1a, i_bottom1b;
+
+    int i_bottom2a, i_bottom2b;
+
+    int i_bottom3a, i_bottom3b;
+
+    if (flag1->answer) {
+	int i = 0;
+
+	int histogram[400];
+
+	for (i = 0; i < 400; i++) {
+	    histogram[i] = 0;
+	}
+	DCELL d_T;
+
 		/****************************/
-		/* Process pixels histogram */
-		for (row = 0; row < nrows; row++)
-		{
-			G_percent (row, nrows, 2);
-			/* read input map */
-			if( (G_get_raster_row(infd_T,inrast_T,row,data_type_T)) < 0){
-				G_fatal_error (_("Could not read from <%s>"),T);
-			}
-			/*process the data */
-			for (col=0; col < ncols; col++)
-			{
-				switch(data_type_T)
-				{
-					case CELL_TYPE:
-						d_T = (double) ((CELL *) inrast_T)[col];
-						break;
-					case FCELL_TYPE:
-						d_T = (double) ((FCELL *) inrast_T)[col];
-						break;
-					case DCELL_TYPE:
-						d_T = (double) ((DCELL *) inrast_T)[col];
-						break;
-				}
-				if(G_is_d_null_value(&d_T)){
-					/*Do nothing*/
-				} else {
-					int temp;
-					temp		= (int) d_T;
-					if(temp>0){
-						histogram[temp]=histogram[temp]+1.0;
-					}
-				}
-			}
+	/* Process pixels histogram */
+	for (row = 0; row < nrows; row++) {
+	    G_percent(row, nrows, 2);
+	    /* read input map */
+	    if ((G_get_raster_row(infd_T, inrast_T, row, data_type_T)) < 0) {
+		G_fatal_error(_("Could not read from <%s>"), T);
+	    }
+	    /*process the data */
+	    for (col = 0; col < ncols; col++) {
+		switch (data_type_T) {
+		case CELL_TYPE:
+		    d_T = (double)((CELL *) inrast_T)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_T = (double)((FCELL *) inrast_T)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_T = (double)((DCELL *) inrast_T)[col];
+		    break;
 		}
-		int sum=0;
-		double average=0.0;
-		for(i=0;i<400;i++){
-			sum += histogram[i];
+		if (G_is_d_null_value(&d_T)) {
+		    /*Do nothing */
 		}
-		average = (double) sum / 400.0; 
-		G_message("Histogram of Temperature map (if it has rogue values to clean)");
-		peak1=0;
-		peak2=0;
-		peak3=0;
-		i_peak1=0;
-		i_peak2=0;
-		i_peak3=0;
-		bottom1a=100000;
-		bottom1b=100000;
-		bottom2a=100000;
-		bottom2b=100000;
-		bottom3a=100000;
-		bottom3b=100000;
-		i_bottom1a=1000;
-		i_bottom1b=1000;
-		i_bottom2a=1000;
-		i_bottom2b=1000;
-		i_bottom3a=1000;
-		i_bottom3b=1000;
-		for(i=0;i<400;i++){
-			/* Search for highest peak of dataset (2) */
-			/* Highest Peak */
-			if(histogram[i]>peak2){
-				peak2 = histogram[i];
-				i_peak2=i;
-			}
+		else {
+		    int temp;
+
+		    temp = (int)d_T;
+		    if (temp > 0) {
+			histogram[temp] = histogram[temp] + 1.0;
+		    }
 		}
-		int stop=0;
-		for(i=i_peak2;i>5;i--){
-			if(((histogram[i]+histogram[i-1]+histogram[i-2]+histogram[i-3]+histogram[i-4])/5)<histogram[i]&&stop==0){
-				bottom2a = histogram[i];
-				i_bottom2a = i;
-			} else if(((histogram[i]+histogram[i-1]+histogram[i-2]+histogram[i-3]+histogram[i-4])/5)>histogram[i]&&stop==0){
-				/*Search for peaks of datasets (1)*/
-				peak1 = histogram[i];
-				i_peak1=i;
-				stop=1;
-			}	
-		}
-		stop=0;
-		for(i=i_peak2;i<395;i++){
-			if(((histogram[i]+histogram[i+1]+histogram[i+2]+histogram[i+3]+histogram[i+4])/5)<histogram[i]&&stop==0){
-				bottom2b = histogram[i];
-				i_bottom2b = i;
-			} else if(((histogram[i]+histogram[i+1]+histogram[i+2]+histogram[i+3]+histogram[i+4])/5)>histogram[i]&&stop==0){
-				/*Search for peaks of datasets (3)*/
-				peak3 = histogram[i];
-				i_peak3=i;
-				stop=1;
-			}	
-		}
-		/* First histogram lower bound */
-		for(i=250;i<i_peak1;i++){
-			if(histogram[i]<bottom1a){
-				bottom1a = histogram[i];
-				i_bottom1a = i;
-			}
-		}
-		/* First histogram higher bound */
-		for(i=i_peak2;i>i_peak1;i--){
-			if(histogram[i]<=bottom1b){
-				bottom1b = histogram[i];
-				i_bottom1b = i;
-			}
-		}
-		/* Third histogram lower bound */
-		for(i=i_peak2;i<i_peak3;i++){
-			if(histogram[i]<bottom3a){
-				bottom3a = histogram[i];
-				i_bottom3a = i;
-			}
-		}	
-		/* Third histogram higher bound */
-		for(i=399;i>i_peak3;i--){
-			if(histogram[i]<bottom3b){
-				bottom3b = histogram[i];
-				i_bottom3b = i;
-			}
-		}	
-		G_message("bottom1a: [%i]=>%i\n",i_bottom1a, bottom1a);
-		G_message("peak1: [%i]=>%i\n",i_peak1, peak1);
-		G_message("bottom1b: [%i]=>%i\n",i_bottom1b, bottom1b);
-		G_message("bottom2a: [%i]=>%i\n",i_bottom2a, bottom2a);
-		G_message("peak2: [%i]=>%i\n",i_peak2, peak2);
-		G_message("bottom2b: [%i]=>%i\n",i_bottom2b, bottom2b);
-		G_message("bottom3a: [%i]=>%i\n",i_bottom3a, bottom3a);
-		G_message("peak3: [%i]=>%i\n",i_peak3, peak3);
-		G_message("bottom3b: [%i]=>%i\n",i_bottom3b, bottom3b);
-	}/*END OF FLAG1*/
+	    }
+	}
+	int sum = 0;
 
-	/* End of processing histogram*/
+	double average = 0.0;
+
+	for (i = 0; i < 400; i++) {
+	    sum += histogram[i];
+	}
+	average = (double)sum / 400.0;
+	G_message
+	    ("Histogram of Temperature map (if it has rogue values to clean)");
+	peak1 = 0;
+	peak2 = 0;
+	peak3 = 0;
+	i_peak1 = 0;
+	i_peak2 = 0;
+	i_peak3 = 0;
+	bottom1a = 100000;
+	bottom1b = 100000;
+	bottom2a = 100000;
+	bottom2b = 100000;
+	bottom3a = 100000;
+	bottom3b = 100000;
+	i_bottom1a = 1000;
+	i_bottom1b = 1000;
+	i_bottom2a = 1000;
+	i_bottom2b = 1000;
+	i_bottom3a = 1000;
+	i_bottom3b = 1000;
+	for (i = 0; i < 400; i++) {
+	    /* Search for highest peak of dataset (2) */
+	    /* Highest Peak */
+	    if (histogram[i] > peak2) {
+		peak2 = histogram[i];
+		i_peak2 = i;
+	    }
+	}
+	int stop = 0;
+
+	for (i = i_peak2; i > 5; i--) {
+	    if (((histogram[i] + histogram[i - 1] + histogram[i - 2] +
+		  histogram[i - 3] + histogram[i - 4]) / 5) < histogram[i] &&
+		stop == 0) {
+		bottom2a = histogram[i];
+		i_bottom2a = i;
+	    }
+	    else if (((histogram[i] + histogram[i - 1] + histogram[i - 2] +
+		       histogram[i - 3] + histogram[i - 4]) / 5) >
+		     histogram[i] && stop == 0) {
+		/*Search for peaks of datasets (1) */
+		peak1 = histogram[i];
+		i_peak1 = i;
+		stop = 1;
+	    }
+	}
+	stop = 0;
+	for (i = i_peak2; i < 395; i++) {
+	    if (((histogram[i] + histogram[i + 1] + histogram[i + 2] +
+		  histogram[i + 3] + histogram[i + 4]) / 5) < histogram[i] &&
+		stop == 0) {
+		bottom2b = histogram[i];
+		i_bottom2b = i;
+	    }
+	    else if (((histogram[i] + histogram[i + 1] + histogram[i + 2] +
+		       histogram[i + 3] + histogram[i + 4]) / 5) >
+		     histogram[i] && stop == 0) {
+		/*Search for peaks of datasets (3) */
+		peak3 = histogram[i];
+		i_peak3 = i;
+		stop = 1;
+	    }
+	}
+	/* First histogram lower bound */
+	for (i = 250; i < i_peak1; i++) {
+	    if (histogram[i] < bottom1a) {
+		bottom1a = histogram[i];
+		i_bottom1a = i;
+	    }
+	}
+	/* First histogram higher bound */
+	for (i = i_peak2; i > i_peak1; i--) {
+	    if (histogram[i] <= bottom1b) {
+		bottom1b = histogram[i];
+		i_bottom1b = i;
+	    }
+	}
+	/* Third histogram lower bound */
+	for (i = i_peak2; i < i_peak3; i++) {
+	    if (histogram[i] < bottom3a) {
+		bottom3a = histogram[i];
+		i_bottom3a = i;
+	    }
+	}
+	/* Third histogram higher bound */
+	for (i = 399; i > i_peak3; i--) {
+	    if (histogram[i] < bottom3b) {
+		bottom3b = histogram[i];
+		i_bottom3b = i;
+	    }
+	}
+	G_message("bottom1a: [%i]=>%i\n", i_bottom1a, bottom1a);
+	G_message("peak1: [%i]=>%i\n", i_peak1, peak1);
+	G_message("bottom1b: [%i]=>%i\n", i_bottom1b, bottom1b);
+	G_message("bottom2a: [%i]=>%i\n", i_bottom2a, bottom2a);
+	G_message("peak2: [%i]=>%i\n", i_peak2, peak2);
+	G_message("bottom2b: [%i]=>%i\n", i_bottom2b, bottom2b);
+	G_message("bottom3a: [%i]=>%i\n", i_bottom3a, bottom3a);
+	G_message("peak3: [%i]=>%i\n", i_peak3, peak3);
+	G_message("bottom3b: [%i]=>%i\n", i_bottom3b, bottom3b);
+    }				/*END OF FLAG1 */
+
+    /* End of processing histogram */
+
 	/*******************/
 
-	/*Process wet pixel values*/
-	/* FLAG2 */
-	if(flag2->answer){
-		/* THREAD 3 */
-		/* Process tempk min / max pixels */
-		/* Internal use only */
-		DCELL d_Rn_wet;
-		DCELL d_g0_wet;
+    /*Process wet pixel values */
+    /* FLAG2 */
+    if (flag2->answer) {
+	/* THREAD 3 */
+	/* Process tempk min / max pixels */
+	/* Internal use only */
+	DCELL d_Rn_wet;
+
+	DCELL d_g0_wet;
+
 		/*********************/
-		for (row = 0; row < nrows; row++){
-			DCELL d_albedo;
-			DCELL d_tempk;
-			DCELL d_dem;
-			DCELL d_t0dem;
-			DCELL d_h0=100.0;/*for flag 1*/
-			DCELL d_h0_max=100.0;/*for flag 1*/
-			G_percent(row,nrows,2);
-			if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-				G_fatal_error(_("Could not read from <%s>"),albedo);
-			if(G_get_raster_row(infd_T,inrast_T,row,data_type_T)<0)
-				G_fatal_error(_("Could not read from <%s>"),T);
-			if(G_get_raster_row(infd_dem,inrast_dem,row,data_type_dem)<0)
-				G_fatal_error(_("Could not read from <%s>"),dem);
-			if(G_get_raster_row(infd_Rn, inrast_Rn, row,data_type_Rn) < 0)
-				G_fatal_error(_("Could not read from <%s>"),Rn);
-			if(G_get_raster_row(infd_g0, inrast_g0, row,data_type_g0) < 0)
-				G_fatal_error(_("Could not read from <%s>"),g0);
-			if (G_get_d_raster_row (infd_ndvi, inrast_ndvi, row) < 0)
-				G_fatal_error (_("Could not read from <%s>"),ndvi);
-			/*process the data */
-			for (col=0; col < ncols; col++)
-			{
-				switch(data_type_albedo){
-					case CELL_TYPE:
-						d_albedo = (double) ((CELL *) inrast_albedo)[col];
-						break;
-					case FCELL_TYPE:
-						d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-						break;
-					case DCELL_TYPE:
-						d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-						break;
-				}
-				switch(data_type_T){
-					case CELL_TYPE:
-						d_tempk = (double) ((CELL *) inrast_T)[col];
-						break;
-					case FCELL_TYPE:
-						d_tempk = (double) ((FCELL *) inrast_T)[col];
-						break;
-					case DCELL_TYPE:
-						d_tempk = (double) ((DCELL *) inrast_T)[col];
-						break;
-				}
-				switch(data_type_dem){
-					case CELL_TYPE:
-						d_dem = (double) ((CELL *) inrast_dem)[col];
-						break;
-					case FCELL_TYPE:
-						d_dem = (double) ((FCELL *) inrast_dem)[col];
-						break;
-					case DCELL_TYPE:
-						d_dem = (double) ((DCELL *) inrast_dem)[col];
-						break;
-				}
-				switch(data_type_Rn){
-					case CELL_TYPE:
-						d_Rn = (double) ((CELL *) inrast_Rn)[col];
-						break;
-					case FCELL_TYPE:
-						d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-						break;
-					case DCELL_TYPE:
-						d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-						break;
-				}
-				switch(data_type_g0){
-					case CELL_TYPE:
-						d_g0 = (double) ((CELL *) inrast_g0)[col];
-						break;
-					case FCELL_TYPE:
-						d_g0 = (double) ((FCELL *) inrast_g0)[col];
-						break;
-					case DCELL_TYPE:
-						d_g0 = (double) ((DCELL *) inrast_g0)[col];
-						break;
-				}
-				switch(data_type_ndvi){
-					case CELL_TYPE:
-						d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-						break;
-					case FCELL_TYPE:
-						d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-						break;
-					case DCELL_TYPE:
-						d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-						break;
-				}
-				if(G_is_d_null_value(&d_albedo)||
-				G_is_d_null_value(&d_tempk)||
-				G_is_d_null_value(&d_dem)||
-				G_is_d_null_value(&d_Rn)||
-				G_is_d_null_value(&d_g0)||
-				G_is_d_null_value(&d_ndvi)){
-					/* do nothing */ 
-				}else{
-					d_t0dem = d_tempk + 0.00627*d_dem;
-					if(d_t0dem<=250.0||d_tempk<=250.0){
-						/* do nothing */ 
-					} else {
-						d_h0=d_Rn-d_g0;
-						if(d_t0dem<t0dem_min&&d_albedo<0.15){
-							t0dem_min=d_t0dem;
-							tempk_min=d_tempk;
-							d_tempk_wet=d_tempk;
-							d_Rn_wet=d_Rn;
-							d_g0_wet=d_g0;
-							col_wet=col;
-							row_wet=row;
-						}
-						if(flag1->answer&&
-						d_tempk>=(double)i_peak1-5.0&&
-						d_tempk<(double)i_peak1+1.0){
-							tempk_min=d_tempk;
-							d_tempk_wet=d_tempk;
-							d_Rn_wet=d_Rn;
-							d_g0_wet=d_g0;
-							col_wet=col;
-							row_wet=row;
-						}
-						if(d_t0dem>t0dem_max){
-							t0dem_max=d_t0dem;
-							d_t0dem_dry=d_t0dem;
-							tempk_max=d_tempk;
-							d_tempk_dry=d_tempk;
-							d_Rn_dry=d_Rn;
-							d_g0_dry=d_g0;
-							d_dem_dry=d_dem;
-							col_dry=col;
-							row_dry=row;
-							d_ndvi_dry=d_ndvi;
-						}
-						if(flag1->answer&&
-						d_tempk>=(double)i_peak3-0.0&&
-						d_tempk<(double)i_peak3+7.0&&
-						d_h0>100.0&&d_h0>d_h0_max&&
-						d_g0>10.0&&d_Rn>100.0&&
-						d_albedo>0.3){
-							tempk_max=d_tempk;
-							d_tempk_dry=d_tempk;
-							d_Rn_dry=d_Rn;
-							d_g0_dry=d_g0;
-							d_h0_max=d_h0;
-							d_dem_dry=d_dem;
-							col_dry=col;
-							row_dry=row;
-							d_ndvi_dry=d_ndvi;
-						}
-					}
-				}
-			}
+	for (row = 0; row < nrows; row++) {
+	    DCELL d_albedo;
+
+	    DCELL d_tempk;
+
+	    DCELL d_dem;
+
+	    DCELL d_t0dem;
+
+	    DCELL d_h0 = 100.0;	/*for flag 1 */
+
+	    DCELL d_h0_max = 100.0;	/*for flag 1 */
+
+	    G_percent(row, nrows, 2);
+	    if (G_get_raster_row
+		(infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+		G_fatal_error(_("Could not read from <%s>"), albedo);
+	    if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+		G_fatal_error(_("Could not read from <%s>"), T);
+	    if (G_get_raster_row(infd_dem, inrast_dem, row, data_type_dem) <
+		0)
+		G_fatal_error(_("Could not read from <%s>"), dem);
+	    if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+		G_fatal_error(_("Could not read from <%s>"), Rn);
+	    if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+		G_fatal_error(_("Could not read from <%s>"), g0);
+	    if (G_get_d_raster_row(infd_ndvi, inrast_ndvi, row) < 0)
+		G_fatal_error(_("Could not read from <%s>"), ndvi);
+	    /*process the data */
+	    for (col = 0; col < ncols; col++) {
+		switch (data_type_albedo) {
+		case CELL_TYPE:
+		    d_albedo = (double)((CELL *) inrast_albedo)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		    break;
 		}
-		G_message("tempk_min=%f\ntempk_max=%f\n",tempk_min,tempk_max);
-		G_message("row_wet=%d\tcol_wet=%d\n",row_wet,col_wet);
-		G_message("row_dry=%d\tcol_dry=%d\n",row_dry,col_dry);
-		G_message("tempk_wet=%f\n",d_tempk_wet);
-		G_message("g0_wet=%f\n",d_g0_wet);
-		G_message("Rn_wet=%f\n",d_Rn_wet);
-		G_message("LE_wet=%f\n",d_Rn_wet-d_g0_wet);
-		G_message("tempk_dry=%f\n",d_tempk_dry);
-		G_message("dem_dry=%f\n",d_dem_dry);
-		G_message("t0dem_dry=%f\n",d_t0dem_dry);
-		G_message("rnet_dry=%f\n",d_Rn_dry);
-		G_message("g0_dry=%f\n",d_g0_dry);
-		G_message("h0_dry=%f\n",d_Rn_dry-d_g0_dry);
-	} /* END OF FLAG2 */
-	
-	/* MANUAL WET/DRY PIXELS */
-	if(input_row_wet->answer&&input_row_dry->answer&&
-	input_col_wet->answer&&input_col_dry->answer){
-		/*DRY PIXEL*/
-		if(flag3->answer){
-			/*Calculate coordinates of row/col from projected ones*/
-			row = (int) (( ymax - m_row_dry ) / (double) stepy) ;
-			col = (int) (( m_col_dry - xmin ) / (double) stepx) ;
-			G_message("Dry Pixel | row:%i col:%i",row,col);
-			m_row_dry = row;
-			m_col_dry = col;
+		switch (data_type_T) {
+		case CELL_TYPE:
+		    d_tempk = (double)((CELL *) inrast_T)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_tempk = (double)((FCELL *) inrast_T)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_tempk = (double)((DCELL *) inrast_T)[col];
+		    break;
 		}
-		row = (int) m_row_dry;
-		col = (int) m_col_dry;
-		G_message("Dry Pixel | row:%i col:%i",row,col);
-		DCELL d_tempk;
-		DCELL d_dem;
-		DCELL d_t0dem;
-		if(G_get_raster_row(infd_T,inrast_T,row,data_type_T)<0)
-			G_fatal_error(_("Could not read from <%s>"),T);
-		if(G_get_raster_row(infd_dem,inrast_dem,row,data_type_dem)<0)
-			G_fatal_error(_("Could not read from <%s>"),dem);
-		if(G_get_raster_row(infd_Rn, inrast_Rn, row,data_type_Rn) < 0)
-			G_fatal_error(_("Could not read from <%s>"),Rn);
-		if(G_get_raster_row(infd_g0, inrast_g0, row,data_type_g0) < 0)
-			G_fatal_error(_("Could not read from <%s>"),g0);
-		switch(data_type_T){
-			case CELL_TYPE:
-				d_tempk = (double) ((CELL *) inrast_T)[col];
-				break;
-			case FCELL_TYPE:
-				d_tempk = (double) ((FCELL *) inrast_T)[col];
-				break;
-			case DCELL_TYPE:
-				d_tempk = (double) ((DCELL *) inrast_T)[col];
-				break;
+		switch (data_type_dem) {
+		case CELL_TYPE:
+		    d_dem = (double)((CELL *) inrast_dem)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_dem = (double)((FCELL *) inrast_dem)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_dem = (double)((DCELL *) inrast_dem)[col];
+		    break;
 		}
-		switch(data_type_dem){
-			case CELL_TYPE:
-				d_dem = (double) ((CELL *) inrast_dem)[col];
-				break;
-			case FCELL_TYPE:
-				d_dem = (double) ((FCELL *) inrast_dem)[col];
-				break;
-			case DCELL_TYPE:
-				d_dem = (double) ((DCELL *) inrast_dem)[col];
-				break;
+		switch (data_type_Rn) {
+		case CELL_TYPE:
+		    d_Rn = (double)((CELL *) inrast_Rn)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_Rn = (double)((FCELL *) inrast_Rn)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_Rn = (double)((DCELL *) inrast_Rn)[col];
+		    break;
 		}
-		switch(data_type_Rn){
-			case CELL_TYPE:
-				d_Rn = (double) ((CELL *) inrast_Rn)[col];
-				break;
-			case FCELL_TYPE:
-				d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-				break;
-			case DCELL_TYPE:
-				d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-				break;
+		switch (data_type_g0) {
+		case CELL_TYPE:
+		    d_g0 = (double)((CELL *) inrast_g0)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_g0 = (double)((FCELL *) inrast_g0)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_g0 = (double)((DCELL *) inrast_g0)[col];
+		    break;
 		}
-		switch(data_type_g0){
-			case CELL_TYPE:
-				d_g0 = (double) ((CELL *) inrast_g0)[col];
-				break;
-			case FCELL_TYPE:
-				d_g0 = (double) ((FCELL *) inrast_g0)[col];
-				break;
-			case DCELL_TYPE:
-				d_g0 = (double) ((DCELL *) inrast_g0)[col];
-				break;
+		switch (data_type_ndvi) {
+		case CELL_TYPE:
+		    d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		    break;
 		}
-		d_t0dem 	= d_tempk + 0.00627 * d_dem;
-		d_t0dem_dry	= d_t0dem;
-		d_tempk_dry	= d_tempk;
-		d_Rn_dry	= d_Rn;
-		d_g0_dry	= d_g0;
-		d_dem_dry	= d_dem;
-		/*WET PIXEL*/
-		if(flag3->answer){
-			/*Calculate coordinates of row/col from projected ones*/
-			row = (int) (( ymax - m_row_wet ) / (double) stepy) ;
-			col = (int) (( m_col_wet - xmin ) / (double) stepx) ;
-			G_message("Wet Pixel | row:%i col:%i",row,col);
-			m_row_wet = row;
-			m_col_wet = col;
-		} 
-		row = m_row_wet;
-		col = m_col_wet;
-		G_message("Wet Pixel | row:%i col:%i",row,col);
-		if(G_get_raster_row(infd_T,inrast_T,row,data_type_T)<0)
-			G_fatal_error(_("Could not read from <%s>"),T);
-		switch(data_type_T){
-			case CELL_TYPE:
-				d_tempk = (double) ((CELL *) inrast_T)[col];
-				break;
-			case FCELL_TYPE:
-				d_tempk = (double) ((FCELL *) inrast_T)[col];
-				break;
-			case DCELL_TYPE:
-				d_tempk = (double) ((DCELL *) inrast_T)[col];
-				break;
+		if (G_is_d_null_value(&d_albedo) ||
+		    G_is_d_null_value(&d_tempk) ||
+		    G_is_d_null_value(&d_dem) ||
+		    G_is_d_null_value(&d_Rn) ||
+		    G_is_d_null_value(&d_g0) || G_is_d_null_value(&d_ndvi)) {
+		    /* do nothing */
 		}
-		d_tempk_wet	= d_tempk;
-		G_message("Manual Pixels\n");
-		G_message("***************************\n");
-		G_message("row_wet=%d\tcol_wet=%d\n",(int) m_row_wet,(int) m_col_wet);
-		G_message("row_dry=%d\tcol_dry=%d\n",(int) m_row_dry,(int) m_col_dry);
-		G_message("tempk_wet=%f\n",d_tempk_wet);
-		G_message("tempk_dry=%f\n",d_tempk_dry);
-		G_message("dem_dry=%f\n",d_dem_dry);
-		G_message("t0dem_dry=%f\n",d_t0dem_dry);
-		G_message("rnet_dry=%f\n",d_Rn_dry);
-		G_message("g0_dry=%f\n",d_g0_dry);
-		G_message("h0_dry=%f\n",d_Rn_dry-d_g0_dry);
-	}
-	/* END OF MANUAL WET/DRY PIXELS */
-
-	for (row = 0; row < nrows; row++)
-	{	
-		DCELL d_albedo;
-		DCELL d_tempk;
-		DCELL d_dem;
-		DCELL d_u2m;
-		DCELL d_t0dem;
-		DCELL d;	/* Output pixel */
-		G_percent(row,nrows,2);
-		/* read a line input maps into buffers*/	
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_T,inrast_T,row,data_type_T)<0)
-			G_fatal_error(_("Could not read from <%s>"),T);
-		if(G_get_raster_row(infd_dem,inrast_dem,row,data_type_dem)<0)
-			G_fatal_error(_("Could not read from <%s>"),dem);
-		if(G_get_raster_row(infd_u2m,inrast_u2m,row,data_type_u2m)<0)
-			G_fatal_error(_("Could not read from <%s>"),u2m);
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi) < 0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		if(G_get_raster_row(infd_Rn,inrast_Rn,row,data_type_Rn) < 0)
-			G_fatal_error(_("Could not read from <%s>"),Rn);
-		if(G_get_raster_row(infd_g0,inrast_g0,row,data_type_g0) < 0)
-			G_fatal_error(_("Could not read from <%s>"),g0);
-		/* read every cell in the line buffers */
-		for (col=0; col < ncols; col++){
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-					break;
+		else {
+		    d_t0dem = d_tempk + 0.00627 * d_dem;
+		    if (d_t0dem <= 250.0 || d_tempk <= 250.0) {
+			/* do nothing */
+		    }
+		    else {
+			d_h0 = d_Rn - d_g0;
+			if (d_t0dem < t0dem_min && d_albedo < 0.15) {
+			    t0dem_min = d_t0dem;
+			    tempk_min = d_tempk;
+			    d_tempk_wet = d_tempk;
+			    d_Rn_wet = d_Rn;
+			    d_g0_wet = d_g0;
+			    col_wet = col;
+			    row_wet = row;
 			}
-			switch(data_type_T){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_T)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_T)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_T)[col];
-					break;
+			if (flag1->answer &&
+			    d_tempk >= (double)i_peak1 - 5.0 &&
+			    d_tempk < (double)i_peak1 + 1.0) {
+			    tempk_min = d_tempk;
+			    d_tempk_wet = d_tempk;
+			    d_Rn_wet = d_Rn;
+			    d_g0_wet = d_g0;
+			    col_wet = col;
+			    row_wet = row;
 			}
-			switch(data_type_dem){
-				case CELL_TYPE:
-					d_dem = (double) ((CELL *) inrast_dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_dem = (double) ((FCELL *) inrast_dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_dem = (double) ((DCELL *) inrast_dem)[col];
-					break;
+			if (d_t0dem > t0dem_max) {
+			    t0dem_max = d_t0dem;
+			    d_t0dem_dry = d_t0dem;
+			    tempk_max = d_tempk;
+			    d_tempk_dry = d_tempk;
+			    d_Rn_dry = d_Rn;
+			    d_g0_dry = d_g0;
+			    d_dem_dry = d_dem;
+			    col_dry = col;
+			    row_dry = row;
+			    d_ndvi_dry = d_ndvi;
 			}
-			switch(data_type_u2m){
-				case CELL_TYPE:
-					d_u2m = (double) ((CELL *) inrast_u2m)[col];
-					break;
-				case FCELL_TYPE:
-					d_u2m = (double) ((FCELL *) inrast_u2m)[col];
-					break;
-				case DCELL_TYPE:
-					d_u2m = (double) ((DCELL *) inrast_u2m)[col];
-					break;
+			if (flag1->answer &&
+			    d_tempk >= (double)i_peak3 - 0.0 &&
+			    d_tempk < (double)i_peak3 + 7.0 &&
+			    d_h0 > 100.0 && d_h0 > d_h0_max &&
+			    d_g0 > 10.0 && d_Rn > 100.0 && d_albedo > 0.3) {
+			    tempk_max = d_tempk;
+			    d_tempk_dry = d_tempk;
+			    d_Rn_dry = d_Rn;
+			    d_g0_dry = d_g0;
+			    d_h0_max = d_h0;
+			    d_dem_dry = d_dem;
+			    col_dry = col;
+			    row_dry = row;
+			    d_ndvi_dry = d_ndvi;
 			}
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			switch(data_type_Rn){
-				case CELL_TYPE:
-					d_Rn = (double) ((CELL *) inrast_Rn)[col];
-					break;
-				case FCELL_TYPE:
-					d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-					break;
-				case DCELL_TYPE:
-					d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-					break;
-			}
-			switch(data_type_g0){
-				case CELL_TYPE:
-					d_g0 = (double) ((CELL *) inrast_g0)[col];
-					break;
-				case FCELL_TYPE:
-					d_g0 = (double) ((FCELL *) inrast_g0)[col];
-					break;
-				case DCELL_TYPE:
-					d_g0 = (double) ((DCELL *) inrast_g0)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_tempk) ||
-			G_is_d_null_value(&d_dem) ||
-			G_is_d_null_value(&d_u2m) ||
-			G_is_d_null_value(&d_ndvi) ||
-			G_is_d_null_value(&d_Rn) ||
-			G_is_d_null_value(&d_g0) ||
-			d_g0<0.0 || d_Rn<0.0 ||
-			d_dem<=-100.0 || d_dem>9000.0 ||
-			d_tempk<200.0){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				/* Albedo < 0*/
-				if(d_albedo<0.001){
-					d_albedo=0.001;
-				}
-				/* Calculate T0dem */
-				d_t0dem = (double)d_tempk + 0.00627 * (double) d_dem;
-			/*	G_message("**InLoop d_t0dem=%5.3f",d_t0dem);
-				G_message(" d_dem=%5.3f",d_dem);
-				G_message(" d_tempk=%5.3f",d_tempk);
-				G_message(" d_albedo=%5.3f",d_albedo);
-				G_message(" d_Rn=%5.3f",d_Rn);
-				G_message(" d_g0=%5.3f",d_g0);
-				G_message(" d_ndvi=%5.3f",d_ndvi);
-				G_message(" d_u_hu=%5.3f",d_u_hu);
-			*/	/* Calculate sensible heat flux */
-				d = sensi_h(iteration,
-						d_tempk_wet,
-						d_tempk_dry,
-						d_t0dem,
-						d_tempk,
-						d_ndvi,
-						d_ndvi_max,
-						d_dem,
-						d_Rn_dry,
-						d_g0_dry,
-						d_t0dem_dry,
-						d_u2m,
-						d_dem_dry);
-		/*		G_message(" d_h0=%5.3f",d);*/
-				if (zero->answer && d<0.0){
-					d=0.0;
-				}
-				outrast[col] = d;
-			}
+		    }
 		}
-		if (G_put_d_raster_row (outfd, outrast) < 0)
-			G_fatal_error (_("Cannot write to <%s>"),h0);
-	}	
-	G_free(inrast_T);
-	G_close_cell (infd_T);
-	G_free(inrast_dem);
-	G_close_cell (infd_dem);
-	G_free(inrast_u2m);
-	G_close_cell (infd_u2m);
-	G_free(inrast_ndvi);
-	G_close_cell (infd_ndvi);
-	G_free(inrast_Rn);
-	G_close_cell (infd_Rn);
-	G_free(inrast_g0);
-	G_close_cell (infd_g0);
-	G_free(inrast_albedo);
-	G_close_cell (infd_albedo);
-	
-	G_free(outrast);
-	G_close_cell (outfd);
-        /* add command line incantation to history file */
-        G_short_history(h0, "raster", &history);
-        G_command_history(&history);
-        G_write_history(h0, &history);
+	    }
+	}
+	G_message("tempk_min=%f\ntempk_max=%f\n", tempk_min, tempk_max);
+	G_message("row_wet=%d\tcol_wet=%d\n", row_wet, col_wet);
+	G_message("row_dry=%d\tcol_dry=%d\n", row_dry, col_dry);
+	G_message("tempk_wet=%f\n", d_tempk_wet);
+	G_message("g0_wet=%f\n", d_g0_wet);
+	G_message("Rn_wet=%f\n", d_Rn_wet);
+	G_message("LE_wet=%f\n", d_Rn_wet - d_g0_wet);
+	G_message("tempk_dry=%f\n", d_tempk_dry);
+	G_message("dem_dry=%f\n", d_dem_dry);
+	G_message("t0dem_dry=%f\n", d_t0dem_dry);
+	G_message("rnet_dry=%f\n", d_Rn_dry);
+	G_message("g0_dry=%f\n", d_g0_dry);
+	G_message("h0_dry=%f\n", d_Rn_dry - d_g0_dry);
+    }				/* END OF FLAG2 */
 
-	exit(EXIT_SUCCESS);
+    /* MANUAL WET/DRY PIXELS */
+    if (input_row_wet->answer && input_row_dry->answer &&
+	input_col_wet->answer && input_col_dry->answer) {
+	/*DRY PIXEL */
+	if (flag3->answer) {
+	    /*Calculate coordinates of row/col from projected ones */
+	    row = (int)((ymax - m_row_dry) / (double)stepy);
+	    col = (int)((m_col_dry - xmin) / (double)stepx);
+	    G_message("Dry Pixel | row:%i col:%i", row, col);
+	    m_row_dry = row;
+	    m_col_dry = col;
+	}
+	row = (int)m_row_dry;
+	col = (int)m_col_dry;
+	G_message("Dry Pixel | row:%i col:%i", row, col);
+	DCELL d_tempk;
+
+	DCELL d_dem;
+
+	DCELL d_t0dem;
+
+	if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), T);
+	if (G_get_raster_row(infd_dem, inrast_dem, row, data_type_dem) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), dem);
+	if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Rn);
+	if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), g0);
+	switch (data_type_T) {
+	case CELL_TYPE:
+	    d_tempk = (double)((CELL *) inrast_T)[col];
+	    break;
+	case FCELL_TYPE:
+	    d_tempk = (double)((FCELL *) inrast_T)[col];
+	    break;
+	case DCELL_TYPE:
+	    d_tempk = (double)((DCELL *) inrast_T)[col];
+	    break;
+	}
+	switch (data_type_dem) {
+	case CELL_TYPE:
+	    d_dem = (double)((CELL *) inrast_dem)[col];
+	    break;
+	case FCELL_TYPE:
+	    d_dem = (double)((FCELL *) inrast_dem)[col];
+	    break;
+	case DCELL_TYPE:
+	    d_dem = (double)((DCELL *) inrast_dem)[col];
+	    break;
+	}
+	switch (data_type_Rn) {
+	case CELL_TYPE:
+	    d_Rn = (double)((CELL *) inrast_Rn)[col];
+	    break;
+	case FCELL_TYPE:
+	    d_Rn = (double)((FCELL *) inrast_Rn)[col];
+	    break;
+	case DCELL_TYPE:
+	    d_Rn = (double)((DCELL *) inrast_Rn)[col];
+	    break;
+	}
+	switch (data_type_g0) {
+	case CELL_TYPE:
+	    d_g0 = (double)((CELL *) inrast_g0)[col];
+	    break;
+	case FCELL_TYPE:
+	    d_g0 = (double)((FCELL *) inrast_g0)[col];
+	    break;
+	case DCELL_TYPE:
+	    d_g0 = (double)((DCELL *) inrast_g0)[col];
+	    break;
+	}
+	d_t0dem = d_tempk + 0.00627 * d_dem;
+	d_t0dem_dry = d_t0dem;
+	d_tempk_dry = d_tempk;
+	d_Rn_dry = d_Rn;
+	d_g0_dry = d_g0;
+	d_dem_dry = d_dem;
+	/*WET PIXEL */
+	if (flag3->answer) {
+	    /*Calculate coordinates of row/col from projected ones */
+	    row = (int)((ymax - m_row_wet) / (double)stepy);
+	    col = (int)((m_col_wet - xmin) / (double)stepx);
+	    G_message("Wet Pixel | row:%i col:%i", row, col);
+	    m_row_wet = row;
+	    m_col_wet = col;
+	}
+	row = m_row_wet;
+	col = m_col_wet;
+	G_message("Wet Pixel | row:%i col:%i", row, col);
+	if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), T);
+	switch (data_type_T) {
+	case CELL_TYPE:
+	    d_tempk = (double)((CELL *) inrast_T)[col];
+	    break;
+	case FCELL_TYPE:
+	    d_tempk = (double)((FCELL *) inrast_T)[col];
+	    break;
+	case DCELL_TYPE:
+	    d_tempk = (double)((DCELL *) inrast_T)[col];
+	    break;
+	}
+	d_tempk_wet = d_tempk;
+	G_message("Manual Pixels\n");
+	G_message("***************************\n");
+	G_message("row_wet=%d\tcol_wet=%d\n", (int)m_row_wet, (int)m_col_wet);
+	G_message("row_dry=%d\tcol_dry=%d\n", (int)m_row_dry, (int)m_col_dry);
+	G_message("tempk_wet=%f\n", d_tempk_wet);
+	G_message("tempk_dry=%f\n", d_tempk_dry);
+	G_message("dem_dry=%f\n", d_dem_dry);
+	G_message("t0dem_dry=%f\n", d_t0dem_dry);
+	G_message("rnet_dry=%f\n", d_Rn_dry);
+	G_message("g0_dry=%f\n", d_g0_dry);
+	G_message("h0_dry=%f\n", d_Rn_dry - d_g0_dry);
+    }
+    /* END OF MANUAL WET/DRY PIXELS */
+
+    for (row = 0; row < nrows; row++) {
+	DCELL d_albedo;
+
+	DCELL d_tempk;
+
+	DCELL d_dem;
+
+	DCELL d_u2m;
+
+	DCELL d_t0dem;
+
+	DCELL d;		/* Output pixel */
+
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row
+	    (infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), T);
+	if (G_get_raster_row(infd_dem, inrast_dem, row, data_type_dem) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), dem);
+	if (G_get_raster_row(infd_u2m, inrast_u2m, row, data_type_u2m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), u2m);
+	if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Rn);
+	if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), g0);
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_T) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_T)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_T)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_T)[col];
+		break;
+	    }
+	    switch (data_type_dem) {
+	    case CELL_TYPE:
+		d_dem = (double)((CELL *) inrast_dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dem = (double)((FCELL *) inrast_dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dem = (double)((DCELL *) inrast_dem)[col];
+		break;
+	    }
+	    switch (data_type_u2m) {
+	    case CELL_TYPE:
+		d_u2m = (double)((CELL *) inrast_u2m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_u2m = (double)((FCELL *) inrast_u2m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_u2m = (double)((DCELL *) inrast_u2m)[col];
+		break;
+	    }
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    switch (data_type_Rn) {
+	    case CELL_TYPE:
+		d_Rn = (double)((CELL *) inrast_Rn)[col];
+		break;
+	    case FCELL_TYPE:
+		d_Rn = (double)((FCELL *) inrast_Rn)[col];
+		break;
+	    case DCELL_TYPE:
+		d_Rn = (double)((DCELL *) inrast_Rn)[col];
+		break;
+	    }
+	    switch (data_type_g0) {
+	    case CELL_TYPE:
+		d_g0 = (double)((CELL *) inrast_g0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_g0 = (double)((FCELL *) inrast_g0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_g0 = (double)((DCELL *) inrast_g0)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_tempk) ||
+		G_is_d_null_value(&d_dem) ||
+		G_is_d_null_value(&d_u2m) ||
+		G_is_d_null_value(&d_ndvi) ||
+		G_is_d_null_value(&d_Rn) ||
+		G_is_d_null_value(&d_g0) ||
+		d_g0 < 0.0 || d_Rn < 0.0 ||
+		d_dem <= -100.0 || d_dem > 9000.0 || d_tempk < 200.0) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		/* Albedo < 0 */
+		if (d_albedo < 0.001) {
+		    d_albedo = 0.001;
+		}
+		/* Calculate T0dem */
+		d_t0dem = (double)d_tempk + 0.00627 * (double)d_dem;
+		/*      G_message("**InLoop d_t0dem=%5.3f",d_t0dem);
+		   G_message(" d_dem=%5.3f",d_dem);
+		   G_message(" d_tempk=%5.3f",d_tempk);
+		   G_message(" d_albedo=%5.3f",d_albedo);
+		   G_message(" d_Rn=%5.3f",d_Rn);
+		   G_message(" d_g0=%5.3f",d_g0);
+		   G_message(" d_ndvi=%5.3f",d_ndvi);
+		   G_message(" d_u_hu=%5.3f",d_u_hu);
+		 *//* Calculate sensible heat flux */
+		d = sensi_h(iteration,
+			    d_tempk_wet,
+			    d_tempk_dry,
+			    d_t0dem,
+			    d_tempk,
+			    d_ndvi,
+			    d_ndvi_max,
+			    d_dem,
+			    d_Rn_dry,
+			    d_g0_dry, d_t0dem_dry, d_u2m, d_dem_dry);
+		/*              G_message(" d_h0=%5.3f",d); */
+		if (zero->answer && d < 0.0) {
+		    d = 0.0;
+		}
+		outrast[col] = d;
+	    }
+	}
+	if (G_put_d_raster_row(outfd, outrast) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), h0);
+    }
+    G_free(inrast_T);
+    G_close_cell(infd_T);
+    G_free(inrast_dem);
+    G_close_cell(infd_dem);
+    G_free(inrast_u2m);
+    G_close_cell(infd_u2m);
+    G_free(inrast_ndvi);
+    G_close_cell(infd_ndvi);
+    G_free(inrast_Rn);
+    G_close_cell(infd_Rn);
+    G_free(inrast_g0);
+    G_close_cell(infd_g0);
+    G_free(inrast_albedo);
+    G_close_cell(infd_albedo);
+
+    G_free(outrast);
+    G_close_cell(outfd);
+    /* add command line incantation to history file */
+    G_short_history(h0, "raster", &history);
+    G_command_history(&history);
+    G_write_history(h0, &history);
+
+    exit(EXIT_SUCCESS);
 }

Modified: grass-addons/gipe/i.eb.h_SEBAL95/psi_h.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/psi_h.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/psi_h.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -6,25 +6,28 @@
 
 double psi_h(double t0_dem, double h, double U_0, double roh_air)
 {
-	double result;
-	double n5_temp, n11_mem, n12_mem;
+    double result;
 
-	if(h != 0.0){
-		n5_temp = (-1004* roh_air*pow(U_0,3)* t0_dem)/(0.41*9.81* h);
-	} else {
-		n5_temp = -1000.0;
-	}
+    double n5_temp, n11_mem, n12_mem;
 
-	if(n5_temp < 0.0){
-		n12_mem = pow((1-16*(2/n5_temp)),0.25);
-		n11_mem = (2*log10((1+pow(n12_mem,2))/2));
-	} else {
-		n12_mem = 1.0;
-		n11_mem = -5*2/n5_temp;
-	}
+    if (h != 0.0) {
+	n5_temp =
+	    (-1004 * roh_air * pow(U_0, 3) * t0_dem) / (0.41 * 9.81 * h);
+    }
+    else {
+	n5_temp = -1000.0;
+    }
 
-	result = n11_mem;
+    if (n5_temp < 0.0) {
+	n12_mem = pow((1 - 16 * (2 / n5_temp)), 0.25);
+	n11_mem = (2 * log10((1 + pow(n12_mem, 2)) / 2));
+    }
+    else {
+	n12_mem = 1.0;
+	n11_mem = -5 * 2 / n5_temp;
+    }
 
-	return (result);
+    result = n11_mem;
+
+    return (result);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/psi_m.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/psi_m.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/psi_m.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -6,21 +6,26 @@
 
 double psi_m(double t0_dem, double h, double ustar, double roh_air, double hu)
 {
-	double result;
-	double n5, n10, n12;
-	if(h != 0.0){
-		n5 = (-1004* roh_air*pow(ustar,3)* t0_dem)/(0.41*9.81* h);
-	} else {
-		n5 = -1000.0;
-	}
-	if(n5 < 0.0){
-		n12 = pow((1-16*(hu/n5)),0.25);
-		n10 = (2*log((1+n12)/2))+log((1+pow(n12,2))/2)-2*atan(n12)+0.5*PI;
-	} else {
-		n12 = 1.0;
-		n10 = -5*hu/n5;
-	}
-	result = n10;
-	return (result);
+    double result;
+
+    double n5, n10, n12;
+
+    if (h != 0.0) {
+	n5 = (-1004 * roh_air * pow(ustar, 3) * t0_dem) / (0.41 * 9.81 * h);
+    }
+    else {
+	n5 = -1000.0;
+    }
+    if (n5 < 0.0) {
+	n12 = pow((1 - 16 * (hu / n5)), 0.25);
+	n10 =
+	    (2 * log((1 + n12) / 2)) + log((1 + pow(n12, 2)) / 2) -
+	    2 * atan(n12) + 0.5 * PI;
+    }
+    else {
+	n12 = 1.0;
+	n10 = -5 * hu / n5;
+    }
+    result = n10;
+    return (result);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/rah1.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/rah1.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/rah1.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -6,10 +6,9 @@
 
 double rah1(double psih, double ustar)
 {
-	double result;
+    double result;
 
-	result = (log10(2/0.01)-psih)/(ustar*0.41);
+    result = (log10(2 / 0.01) - psih) / (ustar * 0.41);
 
-	return (result);
+    return (result);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/rah_0.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/rah_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/rah_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,10 +4,9 @@
 
 double rah_0(double zom_0, double u_0)
 {
-	double result;
+    double result;
 
-	result = log10(2/0.01)/(u_0*0.41);
+    result = log10(2 / 0.01) / (u_0 * 0.41);
 
-	return (result);
+    return (result);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/roh_air.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/roh_air.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/roh_air.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,12 +4,11 @@
 
 double rohair(double dem, double tempk, double dtair)
 {
-	double a, b, result;
+    double a, b, result;
 
-	a = tempk - dtair;
-	b = (( a - 0.00627*dem)/a);
-	result = 349.467 * pow( b , 5.26)/ a ;
-	
-	return result;
-}
+    a = tempk - dtair;
+    b = ((a - 0.00627 * dem) / a);
+    result = 349.467 * pow(b, 5.26) / a;
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.h_SEBAL95/roh_air_0.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/roh_air_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/roh_air_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,11 +4,10 @@
 
 double roh_air_0(double tempk)
 {
-	double A, B, result;
-	
-	A = (18*(6.11*exp(17.27*34.8/(34.8+237.3)))/100.0);
-	B = (18*(6.11*exp(17.27*34.8/(34.8+237.3)))/100.0);
-	result = (1000.0 - A)/(tempk*2.87)+ B/(tempk*4.61);
-	return result;
-}
+    double A, B, result;
 
+    A = (18 * (6.11 * exp(17.27 * 34.8 / (34.8 + 237.3))) / 100.0);
+    B = (18 * (6.11 * exp(17.27 * 34.8 / (34.8 + 237.3))) / 100.0);
+    result = (1000.0 - A) / (tempk * 2.87) + B / (tempk * 4.61);
+    return result;
+}

Modified: grass-addons/gipe/i.eb.h_SEBAL95/sensi_h.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/sensi_h.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/sensi_h.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -9,103 +9,123 @@
 /* Arrays Declarations */
 #define ITER_MAX 10
 
-double sensi_h( int iteration, double tempk_water, double tempk_desert, double t0_dem, double tempk, double ndvi, double ndvi_max, double dem, double rnet_desert, double g0_desert, double t0_dem_desert, double u2m, double dem_desert)
+double sensi_h(int iteration, double tempk_water, double tempk_desert,
+	       double t0_dem, double tempk, double ndvi, double ndvi_max,
+	       double dem, double rnet_desert, double g0_desert,
+	       double t0_dem_desert, double u2m, double dem_desert)
 {
-	/* Arrays Declarations */
-	double dtair[ITER_MAX], roh_air[ITER_MAX], rah[ITER_MAX];
-	double h[ITER_MAX];
-	double ustar[ITER_MAX], zom[ITER_MAX];
+    /* Arrays Declarations */
+    double dtair[ITER_MAX], roh_air[ITER_MAX], rah[ITER_MAX];
 
-	/* Declarations */
-	int	i, j, ic, debug=0;
-	double u_0, zom0;
-	double h_desert, rah_desert, roh_air_desert;
-	double dtair_desert;
-	double psih_desert,ustar_desert,ustar_desertold,zom_desert;
-	double psih;
-	double result;
+    double h[ITER_MAX];
 
-	/* Fat-free junk food */
-	if (iteration>ITER_MAX){
-		iteration=ITER_MAX;
-	}
+    double ustar[ITER_MAX], zom[ITER_MAX];
 
-	if(debug==1){
-		printf("*****************************\n");
-		printf("t0_dem = %5.3f\n",t0_dem);
-		printf("ndvi = %5.3f ndvimax = %5.3f\n",ndvi,ndvi_max);
-		printf("*****************************\n");
-	}
-/*	dtair[0] 	= dt_air_0(t0_dem, tempk_water, tempk_desert);*/
-	dtair[0] = 5.0;
-/* 	printf("*****************************dtair = %5.3f\n",dtair[0]);*/
-	roh_air[0] 	= roh_air_0(tempk);
-/* 	printf("*****************************rohair=%5.3f\n",roh_air[0]);*/
-	roh_air_desert 	= roh_air_0(tempk_desert);
-/* 	printf("**rohairdesert = %5.3f\n",roh_air_desert);*/
-	zom0 		= zom_0(ndvi, ndvi_max);
-/* 	printf("*****************************zom = %5.3f\n",zom0);*/
-	u_0 		= U_0(zom0, u2m);
-/* 	printf("*****************************u0\n");*/
-	rah[0] 		= rah_0(zom0, u_0);
-/* 	printf("*****************************rah = %5.3f\n",rah[0]);*/
-	h[0] 		= h_0(roh_air[0], rah[0], dtair[0]);
-/* 	printf("*****************************h\n");*/
-	if(debug==1){
-		printf("dtair[0]	= %5.3f K\n", dtair[0]);
-		printf("roh_air[0] 	= %5.3f kg/m3\n", roh_air[0]);
-		printf("roh_air_desert0 = %5.3f kg/m3\n", roh_air_desert);
-		printf("zom_0 		= %5.3f\n", zom0);
-		printf("u_0 		= %5.3f\n", u_0);
-		printf("rah[0] 		= %5.3f s/m\n", rah[0]);
-		printf("h[0] 		= %5.3f W/m2\n", h[0]);
-	}
+    /* Declarations */
+    int i, j, ic, debug = 0;
 
+    double u_0, zom0;
+
+    double h_desert, rah_desert, roh_air_desert;
+
+    double dtair_desert;
+
+    double psih_desert, ustar_desert, ustar_desertold, zom_desert;
+
+    double psih;
+
+    double result;
+
+    /* Fat-free junk food */
+    if (iteration > ITER_MAX) {
+	iteration = ITER_MAX;
+    }
+
+    if (debug == 1) {
+	printf("*****************************\n");
+	printf("t0_dem = %5.3f\n", t0_dem);
+	printf("ndvi = %5.3f ndvimax = %5.3f\n", ndvi, ndvi_max);
+	printf("*****************************\n");
+    }
+    /*      dtair[0]        = dt_air_0(t0_dem, tempk_water, tempk_desert); */
+    dtair[0] = 5.0;
+    /*      printf("*****************************dtair = %5.3f\n",dtair[0]); */
+    roh_air[0] = roh_air_0(tempk);
+    /*      printf("*****************************rohair=%5.3f\n",roh_air[0]); */
+    roh_air_desert = roh_air_0(tempk_desert);
+    /*      printf("**rohairdesert = %5.3f\n",roh_air_desert); */
+    zom0 = zom_0(ndvi, ndvi_max);
+    /*      printf("*****************************zom = %5.3f\n",zom0); */
+    u_0 = U_0(zom0, u2m);
+    /*      printf("*****************************u0\n"); */
+    rah[0] = rah_0(zom0, u_0);
+    /*      printf("*****************************rah = %5.3f\n",rah[0]); */
+    h[0] = h_0(roh_air[0], rah[0], dtair[0]);
+    /*      printf("*****************************h\n"); */
+    if (debug == 1) {
+	printf("dtair[0]	= %5.3f K\n", dtair[0]);
+	printf("roh_air[0] 	= %5.3f kg/m3\n", roh_air[0]);
+	printf("roh_air_desert0 = %5.3f kg/m3\n", roh_air_desert);
+	printf("zom_0 		= %5.3f\n", zom0);
+	printf("u_0 		= %5.3f\n", u_0);
+	printf("rah[0] 		= %5.3f s/m\n", rah[0]);
+	printf("h[0] 		= %5.3f W/m2\n", h[0]);
+    }
+
 /*----------------------------------------------------------------*/
-/*Main iteration loop of SEBAL*/
-	zom[0] = zom0;
-	for(ic=1;ic<iteration+1;ic++){
-		if(debug==1){
-			printf("\n ******** ITERATION %i *********\n",ic);
-		}
-		/* Where is roh_air[i]? */
-		psih = psi_h(t0_dem,h[ic-1],u_0,roh_air[ic-1]);
-		ustar[ic] = u_star(t0_dem,h[ic-1],u_0,roh_air[ic-1],zom[0],u2m);
-		rah[ic] = rah1(psih, ustar[ic]);	
-		/* get desert point values from maps */
-		if(ic==1){
-			h_desert	= rnet_desert - g0_desert;
-			zom_desert	= 0.002;
-			psih_desert 	= psi_h(t0_dem_desert,h_desert,u_0,roh_air_desert);
-			ustar_desert	= u_star(t0_dem_desert,h_desert,u_0,roh_air_desert,zom_desert,u2m);
-		} else {
-			roh_air_desert	= rohair(dem_desert,tempk_desert,dtair_desert);
-			h_desert	= h1(roh_air_desert,rah_desert,dtair_desert);
-			ustar_desertold = ustar_desert;
-			psih_desert 	= psi_h(t0_dem_desert,h_desert,ustar_desertold,roh_air_desert);
-			ustar_desert	= u_star(t0_dem_desert,h_desert,ustar_desertold,roh_air_desert,zom_desert,u2m);
-		}
-		rah_desert	= rah1(psih_desert,ustar_desert);
-		dtair_desert 	= dt_air_desert(h_desert, roh_air_desert, rah_desert);
-		/* This should find the new dtair from inversed h equation...*/
-		dtair[ic] 	= dt_air(t0_dem, tempk_water, tempk_desert, dtair_desert);
-		/* This produces h[ic] and roh_air[ic+1] */
-		roh_air[ic] 	= rohair(dem, tempk, dtair[ic]);
-		h[ic] 		= h1(roh_air[ic], rah[ic], dtair[ic]);
-		/* Output values of the iteration parameters */
-		if(debug==1){
-			printf("psih[%i] 	= %5.3f\n", ic, psih);
-			printf("ustar[%i] 	= %5.3f\n", ic, ustar[ic]);
-			printf("rah[%i] 	= %5.3f s/m\n",ic, rah[ic]);
-			printf("h_desert 	= %5.3f\n", h_desert);
-			printf("rohair_desert	= %5.3f\n", roh_air_desert);
-			printf("psih_desert 	= %5.3f\tustar_desert = %5.3f\trah_desert = %5.3f\n", psih_desert, ustar_desert, rah_desert);
-			printf("dtair_desert 	= %8.5f\n", dtair_desert);	
-			printf("dtair[%i] 	= %5.3f K\n", ic, dtair[ic]);	
-			printf("roh_air[%i] 	= %5.3f kg/m3\n", ic, roh_air[ic]);	
-			printf("h[%i] 		= %5.3f W/m2\n",ic, h[ic]);
-		}
+    /*Main iteration loop of SEBAL */
+    zom[0] = zom0;
+    for (ic = 1; ic < iteration + 1; ic++) {
+	if (debug == 1) {
+	    printf("\n ******** ITERATION %i *********\n", ic);
 	}
-	return h[iteration];
+	/* Where is roh_air[i]? */
+	psih = psi_h(t0_dem, h[ic - 1], u_0, roh_air[ic - 1]);
+	ustar[ic] =
+	    u_star(t0_dem, h[ic - 1], u_0, roh_air[ic - 1], zom[0], u2m);
+	rah[ic] = rah1(psih, ustar[ic]);
+	/* get desert point values from maps */
+	if (ic == 1) {
+	    h_desert = rnet_desert - g0_desert;
+	    zom_desert = 0.002;
+	    psih_desert = psi_h(t0_dem_desert, h_desert, u_0, roh_air_desert);
+	    ustar_desert =
+		u_star(t0_dem_desert, h_desert, u_0, roh_air_desert,
+		       zom_desert, u2m);
+	}
+	else {
+	    roh_air_desert = rohair(dem_desert, tempk_desert, dtair_desert);
+	    h_desert = h1(roh_air_desert, rah_desert, dtair_desert);
+	    ustar_desertold = ustar_desert;
+	    psih_desert =
+		psi_h(t0_dem_desert, h_desert, ustar_desertold,
+		      roh_air_desert);
+	    ustar_desert =
+		u_star(t0_dem_desert, h_desert, ustar_desertold,
+		       roh_air_desert, zom_desert, u2m);
+	}
+	rah_desert = rah1(psih_desert, ustar_desert);
+	dtair_desert = dt_air_desert(h_desert, roh_air_desert, rah_desert);
+	/* This should find the new dtair from inversed h equation... */
+	dtair[ic] = dt_air(t0_dem, tempk_water, tempk_desert, dtair_desert);
+	/* This produces h[ic] and roh_air[ic+1] */
+	roh_air[ic] = rohair(dem, tempk, dtair[ic]);
+	h[ic] = h1(roh_air[ic], rah[ic], dtair[ic]);
+	/* Output values of the iteration parameters */
+	if (debug == 1) {
+	    printf("psih[%i] 	= %5.3f\n", ic, psih);
+	    printf("ustar[%i] 	= %5.3f\n", ic, ustar[ic]);
+	    printf("rah[%i] 	= %5.3f s/m\n", ic, rah[ic]);
+	    printf("h_desert 	= %5.3f\n", h_desert);
+	    printf("rohair_desert	= %5.3f\n", roh_air_desert);
+	    printf
+		("psih_desert 	= %5.3f\tustar_desert = %5.3f\trah_desert = %5.3f\n",
+		 psih_desert, ustar_desert, rah_desert);
+	    printf("dtair_desert 	= %8.5f\n", dtair_desert);
+	    printf("dtair[%i] 	= %5.3f K\n", ic, dtair[ic]);
+	    printf("roh_air[%i] 	= %5.3f kg/m3\n", ic, roh_air[ic]);
+	    printf("h[%i] 		= %5.3f W/m2\n", ic, h[ic]);
+	}
+    }
+    return h[iteration];
 }
- 

Modified: grass-addons/gipe/i.eb.h_SEBAL95/u_star.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/u_star.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/u_star.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,35 +4,47 @@
 
 #define PI 3.1415927
 
-double u_star(double t0_dem, double h, double ustar, double roh_air, double zom, double u2m)
+double u_star(double t0_dem, double h, double ustar, double roh_air,
+	      double zom, double u2m)
 {
-	double result;
-	double n5_temp; /* Monin-Obukov Length 		*/
-        double n10_mem; /* psi m 			*/
-	double n31_mem; /* x for U200 (that is bh...) 	*/
-	double hv=0.15;	/* crop height (m) 		*/
-	double bh=200;	/* blending height (m) 		*/
+    double result;
 
-	if(h != 0.0){
-		n5_temp = (-1004* roh_air*pow(ustar,3)* t0_dem)/(0.41*9.81* h);
-	} else {
-		n5_temp = -1000.0;
-	}
+    double n5_temp;		/* Monin-Obukov Length          */
 
-	if(n5_temp < 0.0){
+    double n10_mem;		/* psi m                        */
 
-		n31_mem = pow((1-16*(200/n5_temp)),0.25);
-		n10_mem = (2*log10((1+n31_mem)/2)+log10((1+pow(n31_mem,2))/2)-2*atan(n31_mem)+0.5*PI);
+    double n31_mem;		/* x for U200 (that is bh...)   */
 
-	} else {
+    double hv = 0.15;		/* crop height (m)              */
 
-/*		n31_mem = 1.0;*/
-		n10_mem = -5*2/n5_temp;
+    double bh = 200;		/* blending height (m)          */
 
-	}
+    if (h != 0.0) {
+	n5_temp =
+	    (-1004 * roh_air * pow(ustar, 3) * t0_dem) / (0.41 * 9.81 * h);
+    }
+    else {
+	n5_temp = -1000.0;
+    }
 
-	result = ((u2m*0.41/log10(2/(hv/7)))/0.41*log10(bh /(hv/7)*0.41))/(log10(bh / zom)-n10_mem);
-	
-	return (result);
+    if (n5_temp < 0.0) {
+
+	n31_mem = pow((1 - 16 * (200 / n5_temp)), 0.25);
+	n10_mem =
+	    (2 * log10((1 + n31_mem) / 2) + log10((1 + pow(n31_mem, 2)) / 2) -
+	     2 * atan(n31_mem) + 0.5 * PI);
+
+    }
+    else {
+
+	/*              n31_mem = 1.0; */
+	n10_mem = -5 * 2 / n5_temp;
+
+    }
+
+    result =
+	((u2m * 0.41 / log10(2 / (hv / 7))) / 0.41 *
+	 log10(bh / (hv / 7) * 0.41)) / (log10(bh / zom) - n10_mem);
+
+    return (result);
 }
-

Modified: grass-addons/gipe/i.eb.h_SEBAL95/zom_0.c
===================================================================
--- grass-addons/gipe/i.eb.h_SEBAL95/zom_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_SEBAL95/zom_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,15 +4,17 @@
 
 double zom_0(double ndvi, double ndvi_max)
 {
-	double a, b, zom;
-	double hv_ndvimax=1.5; /* crop vegetation height (m) */
-	double hv_desert=0.002; /* desert base vegetation height (m) */
+    double a, b, zom;
 
-	a = (log(hv_desert)-((log(hv_ndvimax/7)-log(hv_desert))/(ndvi_max-0.02)*0.02));
-	b = ((log(hv_ndvimax/7)-log(hv_desert))/(ndvi_max-0.02)) * ndvi;
-	zom = exp(a+b); 
-	/* Greece works and SEBAL01*/
-	/*zom = exp(3.3219*ndvi-3.9939);*/
-	return (zom);
+    double hv_ndvimax = 1.5;	/* crop vegetation height (m) */
+
+    double hv_desert = 0.002;	/* desert base vegetation height (m) */
+
+    a = (log(hv_desert) -
+	 ((log(hv_ndvimax / 7) - log(hv_desert)) / (ndvi_max - 0.02) * 0.02));
+    b = ((log(hv_ndvimax / 7) - log(hv_desert)) / (ndvi_max - 0.02)) * ndvi;
+    zom = exp(a + b);
+    /* Greece works and SEBAL01 */
+    /*zom = exp(3.3219*ndvi-3.9939); */
+    return (zom);
 }
-

Modified: grass-addons/gipe/i.eb.h_iter/fixed_deltat.c
===================================================================
--- grass-addons/gipe/i.eb.h_iter/fixed_deltat.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_iter/fixed_deltat.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,68 +2,91 @@
 #include<stdlib.h>
 #include<math.h>
 
-#define PI 3.14159265358979323846 
+#define PI 3.14159265358979323846
 
-double fixed_deltat(double u_hu, double roh_air,double cp,double dt,double disp,double z0m,double z0h,double tempk,double hu, int iteration){
-	int i;
-	double ublend;
-	double length;
-	double xm,xh;
-	double psim, psih;
-	double ustar;
-	double rah;
-	double h_in;
-	double temp1;
-	
-	if(log(hu-disp)-log(z0m)==0.0){
-		ublend=u_hu*(log(100-disp)-log(z0m))/(log(hu-disp)-log(z0m)+0.001);
-	} else {
-		ublend=u_hu*(log(100-disp)-log(z0m))/(log(hu-disp)-log(z0m));
-	}
-	psim=0.0;
-	psih=0.0;
+double fixed_deltat(double u_hu, double roh_air, double cp, double dt,
+		    double disp, double z0m, double z0h, double tempk,
+		    double hu, int iteration)
+{
+    int i;
 
-	for(i=0;i<iteration;i++){
-		if((log((100-disp)/z0m)-psim)==0.0){
-			ustar = 0.41*ublend/(log((100-disp)/z0m)-psim+0.0001);
-		} else {
-			ustar = 0.41*ublend/(log((100-disp)/z0m)-psim);
-		}
-		if(z0h==0.0){
-			z0h=0.00001;
-		}
-		if(ustar==0.0){
-			ustar=0.00001;
-		}
-		if(((hu-disp)/z0h)-psih==0.0){
-			rah   = (log((hu-disp)/z0h)-psih+0.00001)/(0.41*ustar);
-		} else {			
-			rah   = (log((hu-disp)/z0h)-psih)/(0.41*ustar);
-		}
-		if(rah==0.0){
-			rah=0.00001;
-		}			
-		h_in  = roh_air * cp * dt / rah;
-		if(h_in==0.0){
-			h_in=0.00001;
-		}
-		length= -roh_air*cp*pow(ustar,3)*tempk/(0.41*9.81*h_in);
-		if(length==0.0){
-			length=-0.00001;
-		}
-		xm    = pow(1.0-16.0*((100-disp)/length),0.25);
-		xh    = pow(1.0-16.0*((hu-disp)/length),0.25);
-		if((1.0+xm)/2.0==0.0||(1+xm*xm)/2.0==0.0){
-			psim  = 2.0*log((1.0+xm+0.00001)/2.0)+log((1+xm*xm)/2.0)-2*atan(xm)+0.5*PI+0.00001;
-		} else {
-			psim  = 2.0*log((1.0+xm)/2.0)+log((1+xm*xm)/2.0)-2*atan(xm)+0.5*PI;
-		}
-		if((1.0+xh*xh)/2.0==0.0){
-			psih  = 2.0*log((1.0+xh*xh+0.00001)/2.0);
-		} else {
-			psih  = 2.0*log((1.0+xh*xh)/2.0);
-		}
+    double ublend;
+
+    double length;
+
+    double xm, xh;
+
+    double psim, psih;
+
+    double ustar;
+
+    double rah;
+
+    double h_in;
+
+    double temp1;
+
+    if (log(hu - disp) - log(z0m) == 0.0) {
+	ublend =
+	    u_hu * (log(100 - disp) - log(z0m)) / (log(hu - disp) - log(z0m) +
+						   0.001);
+    }
+    else {
+	ublend =
+	    u_hu * (log(100 - disp) - log(z0m)) / (log(hu - disp) - log(z0m));
+    }
+    psim = 0.0;
+    psih = 0.0;
+
+    for (i = 0; i < iteration; i++) {
+	if ((log((100 - disp) / z0m) - psim) == 0.0) {
+	    ustar = 0.41 * ublend / (log((100 - disp) / z0m) - psim + 0.0001);
 	}
+	else {
+	    ustar = 0.41 * ublend / (log((100 - disp) / z0m) - psim);
+	}
+	if (z0h == 0.0) {
+	    z0h = 0.00001;
+	}
+	if (ustar == 0.0) {
+	    ustar = 0.00001;
+	}
+	if (((hu - disp) / z0h) - psih == 0.0) {
+	    rah = (log((hu - disp) / z0h) - psih + 0.00001) / (0.41 * ustar);
+	}
+	else {
+	    rah = (log((hu - disp) / z0h) - psih) / (0.41 * ustar);
+	}
+	if (rah == 0.0) {
+	    rah = 0.00001;
+	}
+	h_in = roh_air * cp * dt / rah;
+	if (h_in == 0.0) {
+	    h_in = 0.00001;
+	}
+	length = -roh_air * cp * pow(ustar, 3) * tempk / (0.41 * 9.81 * h_in);
+	if (length == 0.0) {
+	    length = -0.00001;
+	}
+	xm = pow(1.0 - 16.0 * ((100 - disp) / length), 0.25);
+	xh = pow(1.0 - 16.0 * ((hu - disp) / length), 0.25);
+	if ((1.0 + xm) / 2.0 == 0.0 || (1 + xm * xm) / 2.0 == 0.0) {
+	    psim =
+		2.0 * log((1.0 + xm + 0.00001) / 2.0) +
+		log((1 + xm * xm) / 2.0) - 2 * atan(xm) + 0.5 * PI + 0.00001;
+	}
+	else {
+	    psim =
+		2.0 * log((1.0 + xm) / 2.0) + log((1 + xm * xm) / 2.0) -
+		2 * atan(xm) + 0.5 * PI;
+	}
+	if ((1.0 + xh * xh) / 2.0 == 0.0) {
+	    psih = 2.0 * log((1.0 + xh * xh + 0.00001) / 2.0);
+	}
+	else {
+	    psih = 2.0 * log((1.0 + xh * xh) / 2.0);
+	}
+    }
 
-	return rah;
+    return rah;
 }

Modified: grass-addons/gipe/i.eb.h_iter/h0.c
===================================================================
--- grass-addons/gipe/i.eb.h_iter/h0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_iter/h0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,13 +3,14 @@
 
 double h0(double roh_air, double cp, double rah, double dtair)
 {
-	double result;
-	if(rah<0.001){
-		result=-1.0;
-	} else {
-		result = roh_air*cp*dtair / rah;
-	}
-	
-	return result;
+    double result;
+
+    if (rah < 0.001) {
+	result = -1.0;
+    }
+    else {
+	result = roh_air * cp * dtair / rah;
+    }
+
+    return result;
 }
-

Modified: grass-addons/gipe/i.eb.h_iter/main.c
===================================================================
--- grass-addons/gipe/i.eb.h_iter/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.h_iter/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,494 +1,555 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.h_iter
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates sensible heat flux by SEBAL iteration
- *               if your DeltaT (or eq. to make it from surf.temp)
- *               is validated.
- *               ! Delta T will not be reassessed in the iterations !
- *               A flag allows the Bastiaanssen (1995) affine transform 
- *               of surface temperature as used in his SEBAL model.
- *               This has been seen in Pawan (2004).
- *
- * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- * CHANGELOG:	06 June 08: 	Added iteration number input
- * 		28 October 06:  Added u2m input 
- * 				(wind speed at 2 meters height)
- * 		30 June 07: 	Debugging a Seg Fault
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double fixed_deltat(double u2m, double roh_air,double cp,double dt,double disp,double z0m,double z0h,double tempk,double hu,int iteration);
-double h0(double roh_air, double cp, double rah, double dtair);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	/*If !sebal then use Delta T input file*/
-	int sebal=0;/*SEBAL Flag for affine transform of surf. temp.*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5;
-	struct Option *input6, *input7, *input8, *input9, *input10;
-	struct Option *input11, *input_hu, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_rohair, infd_tempk, infd_dtair;
-	int infd_disp, infd_z0m, infd_z0h, infd_u_hu;
-	int infd_hu, outfd;
-	
-	char *rohair,*tempk,*dtair, *disp, *z0m, *z0h, *u_hu, *hu;
-
-	double cp; /*air specific heat*/	
-	int i=0,j=0;
-	double a,b; /*SEBAL slope and intercepts of surf. temp.*/
-	int iteration;
-	
-	void *inrast_rohair, *inrast_tempk, *inrast_dtair;
-	void *inrast_disp, *inrast_z0m, *inrast_z0h;
-	void *inrast_u_hu, *inrast_hu;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_rohair;
-	RASTER_MAP_TYPE data_type_dtair;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_disp;
-	RASTER_MAP_TYPE data_type_z0m;
-	RASTER_MAP_TYPE data_type_z0h;
-	RASTER_MAP_TYPE data_type_u_hu;
-	RASTER_MAP_TYPE data_type_hu;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("sensible heat flux, energy balance, SEBAL");
-	module->description = _("sensible heat flux equation as in Pawan (2004), including flags for delta T affine transform from surf. temp.");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("rohair");
-	input1->description=_("Name of the air density map ~[0.9;1.5], Pawan (2004) use 1.12 constant value");
-	input1->answer     =_("rohair");
-
-	input2 = G_define_option() ;
-	input2->key        =_("cp");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  =_("parameter, float number");
-	input2->description=_("Value of the air specific heat [1000.0;1020.0]");
-	input2->answer     =_("1004.16");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("tempk");
-	input3->description=_("Name of the surface skin temperature map [degrees Kelvin],if used with -s flag and affine coefs, it disables dtair input");
-	input3->answer     =_("tempk");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("dtair");
-	input4->required   = NO;
-	input4->description=_("Name of the skin-air Surface temperature difference map ~[0.0-80.0], required unless you use -s flag (then you must give a & b coefs below)");
-
-	input5 = G_define_option() ;
-	input5->key        =_("a");
-	input5->type       = TYPE_DOUBLE;
-	input5->required   = NO;
-	input5->gisprompt  =_("parameter, float number");
-	input5->description=_("-s flag: Value of the slope of the affine transform");
-	
-	input6 = G_define_option() ;
-	input6->key        =_("b");
-	input6->type       = TYPE_DOUBLE;
-	input6->required   = NO;
-	input6->gisprompt  =_("parameter, float number");
-	input6->description=_("-s flag: Value of the intercept of the affine transform");
-	
-	input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input7->key        =_("disp");
-	input7->description=_("Name of the displacement height input map (m)");
-	input7->answer     =_("disp");
-	
-	input8 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input8->key        =_("z0m");
-	input8->description=_("Name of the z0m input map (s/m)");
-	input8->answer     =_("z0m");
-	
-	input9 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input9->key        =_("z0h");
-	input9->description=_("Name of the z0h input map (s/m)");
-	input9->answer     =_("z0h");
-	
-	input10 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input10->key        =_("u_hu");
-	input10->description=_("Name of the wind speed at height (hu) input map (m/s)");
-	input10->answer     =_("u_hu");
-	
-	input_hu = G_define_standard_option(G_OPT_R_INPUT) ;
-	input_hu->key        =_("hu");
-	input_hu->description=_("Name of the height (hu) of measurement of wind speed input map (m/s)");
-	input_hu->answer     =_("hu");
-	
-	input11 = G_define_option() ;
-	input11->key        =_("iteration");
-	input11->type       = TYPE_INTEGER;
-	input11->required   = NO;
-	input11->gisprompt  =_("parameter, integer number");
-	input11->description=_("number of iteration of rah stabilization");
-	input11->answer     =_("3");
-		
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output h0 layer");
-
-	flag1 = G_define_flag();
-	flag1->key = 's';
-	flag1->description = _("Affine transform of Surface temperature into delta T, needs input of slope and intercept (Bastiaanssen, 1995)");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	rohair	 	= input1->answer;
-	cp	 	= atof(input2->answer);
-	if(input3->answer){
-		tempk	= input3->answer;
-	}
-	if(input4->answer){
-		dtair	= input4->answer;
-	}
-	if(input5->answer){
-		a	= atof(input5->answer);
-	}
-	if(input6->answer){
-		b	= atof(input6->answer);
-	}
-	disp	 	= input7->answer;
-	z0m	 	= input8->answer;
-	z0h	 	= input9->answer;
-	u_hu	 	= input10->answer;
-	hu	 	= input_hu->answer;
-	if(input11->answer){
-		iteration = atoi(input11->answer);
-	}else{
-		iteration = 3;
-	}
-	
-	result  	= output1->answer;
-	sebal 		= flag1->answer;
-	/***************************************************/
-	/* TEST FOR -s FLAG COEFS 
-	 * Return error if not proper flag coefs
-	 * 						   */
-	/***************************************************/
-	if (sebal&&!a&&!b){
-		G_fatal_error(_("FATAL ERROR: -s Flag requires coefs a & b!"));
-	} else if (!sebal&&!dtair){
-		G_fatal_error(_("FATAL ERROR: No -s Flag, use DeltaT map!"));
-	}
-	/***************************************************/
-	mapset = G_find_cell2(rohair, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), rohair);
-	}
-	data_type_rohair = G_raster_map_type(rohair,mapset);
-	if ( (infd_rohair = G_open_cell_old (rohair,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), rohair);
-	if (G_get_cellhd (rohair, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), rohair);
-	inrast_rohair = G_allocate_raster_buf(data_type_rohair);
-	/***************************************************/
-	if(!sebal){
-		mapset = G_find_cell2 (dtair, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"),dtair);
-		}
-		data_type_dtair = G_raster_map_type(dtair,mapset);
-		if ( (infd_dtair = G_open_cell_old (dtair,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), dtair);
-		if (G_get_cellhd (dtair, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), dtair);
-		inrast_dtair = G_allocate_raster_buf(data_type_dtair);
-	}
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (disp, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), disp);
-	}
-	data_type_disp = G_raster_map_type(disp,mapset);
-	if ( (infd_disp = G_open_cell_old (disp,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), disp);
-	if (G_get_cellhd (disp, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), disp);
-	inrast_disp = G_allocate_raster_buf(data_type_disp);
-	/***************************************************/
-	mapset = G_find_cell2 (z0m, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), z0m);
-	}
-	data_type_z0m = G_raster_map_type(z0m,mapset);
-	if ( (infd_z0m = G_open_cell_old (z0m,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), z0m);
-	if (G_get_cellhd (z0m, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), z0m);
-	inrast_z0m = G_allocate_raster_buf(data_type_z0m);
-	/***************************************************/
-	mapset = G_find_cell2 (z0h, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), z0h);
-	}
-	data_type_z0h = G_raster_map_type(z0h,mapset);
-	if ( (infd_z0h = G_open_cell_old (z0h,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), z0h);
-	if (G_get_cellhd (z0h, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), z0h);
-	inrast_z0h = G_allocate_raster_buf(data_type_z0h);
-	/***************************************************/
-	mapset = G_find_cell2 (u_hu, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), u_hu);
-	}
-	data_type_u_hu = G_raster_map_type(u_hu,mapset);
-	if ( (infd_u_hu = G_open_cell_old (u_hu,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), u_hu);
-	if (G_get_cellhd (u_hu, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), u_hu);
-	inrast_u_hu = G_allocate_raster_buf(data_type_u_hu);
-	/***************************************************/
-	mapset = G_find_cell2 (hu, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), hu);
-	}
-	data_type_hu = G_raster_map_type(hu,mapset);
-	if ( (infd_hu = G_open_cell_old (hu,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), hu);
-	if (G_get_cellhd (hu, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), hu);
-	inrast_hu = G_allocate_raster_buf(data_type_hu);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rohair;
-		DCELL d_rah;
-		DCELL d_dtair;
-		DCELL d_affine;
-		DCELL d_tempk;
-		DCELL d_disp;
-		DCELL d_z0m;
-		DCELL d_z0h;
-		DCELL d_u_hu;
-		DCELL d_hu;
-		G_percent(row,nrows,2);
-		/* printf("row = %i/%i\n",row,nrows);*/
-		/* read input maps */	
-		if(G_get_raster_row(infd_rohair,inrast_rohair,row,data_type_rohair)<0)
-			G_fatal_error(_("Could not read from <%s>"),rohair);
-		if(!sebal){
-			if(G_get_raster_row(infd_dtair,inrast_dtair,row,data_type_dtair)<0)
-			G_fatal_error(_("Could not read from <%s>"),dtair);
-		}
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_disp,inrast_disp,row,data_type_disp)<0)
-			G_fatal_error(_("Could not read from <%s>"),disp);
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		if(G_get_raster_row(infd_z0h,inrast_z0h,row,data_type_z0h)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0h);
-		if(G_get_raster_row(infd_u_hu,inrast_u_hu,row,data_type_u_hu)<0)
-			G_fatal_error(_("Could not read from <%s>"),u_hu);
-		if(G_get_raster_row(infd_hu,inrast_hu,row,data_type_hu)<0)
-			G_fatal_error(_("Could not read from <%s>"),hu);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_rohair){
-				case CELL_TYPE:
-					d_rohair = (double) ((CELL *) inrast_rohair)[col];
-					break;
-				case FCELL_TYPE:
-					d_rohair = (double) ((FCELL *) inrast_rohair)[col];
-					break;
-				case DCELL_TYPE:
-					d_rohair = ((DCELL *) inrast_rohair)[col];
-					break;
-			}
-			if(!sebal){
-				switch(data_type_dtair){
-					case CELL_TYPE:
-						d_dtair = (double) ((CELL *) inrast_dtair)[col];
-						break;
-					case FCELL_TYPE:
-						d_dtair = (double) ((FCELL *) inrast_dtair)[col];
-						break;
-					case DCELL_TYPE:
-						d_dtair = ((DCELL *) inrast_dtair)[col];
-						break;
-				}
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_disp = (double) ((CELL *) inrast_disp)[col];
-					break;
-				case FCELL_TYPE:
-					d_disp = (double) ((FCELL *) inrast_disp)[col];
-					break;
-				case DCELL_TYPE:
-					d_disp = ((DCELL *) inrast_disp)[col];
-					break;
-			}
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = ((DCELL *) inrast_z0m)[col];
-					break;
-			}
-			switch(data_type_z0h){
-				case CELL_TYPE:
-					d_z0h = (double) ((CELL *) inrast_z0h)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0h = (double) ((FCELL *) inrast_z0h)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0h = ((DCELL *) inrast_z0h)[col];
-					break;
-			}
-			switch(data_type_u_hu){
-				case CELL_TYPE:
-					d_u_hu = (double) ((CELL *) inrast_u_hu)[col];
-					break;
-				case FCELL_TYPE:
-					d_u_hu = (double) ((FCELL *) inrast_u_hu)[col];
-					break;
-				case DCELL_TYPE:
-					d_u_hu = ((DCELL *) inrast_u_hu)[col];
-					break;
-			}
-			switch(data_type_hu){
-				case CELL_TYPE:
-					d_hu = (double) ((CELL *) inrast_hu)[col];
-					break;
-				case FCELL_TYPE:
-					d_hu = (double) ((FCELL *) inrast_hu)[col];
-					break;
-				case DCELL_TYPE:
-					d_hu = ((DCELL *) inrast_hu)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_rohair)||
-			((!sebal)&&G_is_d_null_value(&d_dtair))||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_disp)||
-			G_is_d_null_value(&d_z0m)||
-			G_is_d_null_value(&d_z0h)||
-			G_is_d_null_value(&d_u_hu)||
-			G_is_d_null_value(&d_hu)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate sensible heat flux	    */
-				if(sebal){/*if -s flag then calculate Delta T from Coefs*/
-					d_affine=a*d_tempk+b;
-					/* Run iterations to find Rah*/
-					d_rah=fixed_deltat(d_u_hu,d_rohair,cp,d_affine,d_disp,d_z0m,d_z0h,d_tempk,d_hu,iteration);
-					/*Process h*/
-					d = h0(d_rohair,cp,d_rah,d_affine);
-				}else{/* not -s flag then take delta T map input directly*/
-					/* Run iterations to find Rah*/
-					d_rah=fixed_deltat(d_u_hu,d_rohair,cp,d_dtair,d_disp,d_z0m,d_z0h,d_tempk,d_hu,iteration);
-					/*Process h*/
-					d = h0(d_rohair,cp,d_rah,d_dtair);
-				}
-				 outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-	G_free (inrast_rohair);
-	if(!sebal){
-		G_free (inrast_dtair);
-	}
-	G_free (inrast_tempk);
-	G_free (inrast_disp);
-	G_free (inrast_z0m);
-	G_free (inrast_z0h);
-	G_free (inrast_u_hu);
-	G_free (inrast_hu);
-
-	G_close_cell (infd_rohair);
-	if(!sebal){
-		G_close_cell (infd_dtair);
-	}
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_disp);
-	G_close_cell (infd_z0m);
-	G_close_cell (infd_z0h);
-	G_close_cell (infd_u_hu);
-	G_close_cell (infd_hu);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.h_iter
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates sensible heat flux by SEBAL iteration
+ *               if your DeltaT (or eq. to make it from surf.temp)
+ *               is validated.
+ *               ! Delta T will not be reassessed in the iterations !
+ *               A flag allows the Bastiaanssen (1995) affine transform 
+ *               of surface temperature as used in his SEBAL model.
+ *               This has been seen in Pawan (2004).
+ *
+ * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ * CHANGELOG:	06 June 08: 	Added iteration number input
+ * 		28 October 06:  Added u2m input 
+ * 				(wind speed at 2 meters height)
+ * 		30 June 07: 	Debugging a Seg Fault
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double fixed_deltat(double u2m, double roh_air, double cp, double dt,
+		     double disp, double z0m, double z0h, double tempk,
+		     double hu, int iteration);
+double h0(double roh_air, double cp, double rah, double dtair);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    
+	/*If !sebal then use Delta T input file */ 
+    int sebal = 0;		/*SEBAL Flag for affine transform of surf. temp. */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5;
+
+    struct Option *input6, *input7, *input8, *input9, *input10;
+
+    struct Option *input11, *input_hu, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_rohair, infd_tempk, infd_dtair;
+
+    int infd_disp, infd_z0m, infd_z0h, infd_u_hu;
+
+    int infd_hu, outfd;
+
+    char *rohair, *tempk, *dtair, *disp, *z0m, *z0h, *u_hu, *hu;
+
+    double cp;		/*air specific heat */
+
+    int i = 0, j = 0;
+
+    double a, b;		/*SEBAL slope and intercepts of surf. temp. */
+
+    int iteration;
+
+    void *inrast_rohair, *inrast_tempk, *inrast_dtair;
+
+    void *inrast_disp, *inrast_z0m, *inrast_z0h;
+
+    void *inrast_u_hu, *inrast_hu;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_rohair;
+    RASTER_MAP_TYPE data_type_dtair;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_disp;
+    RASTER_MAP_TYPE data_type_z0m;
+    RASTER_MAP_TYPE data_type_z0h;
+    RASTER_MAP_TYPE data_type_u_hu;
+    RASTER_MAP_TYPE data_type_hu;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("sensible heat flux, energy balance, SEBAL");
+    module->description =
+	_("sensible heat flux equation as in Pawan (2004), including flags for delta T affine transform from surf. temp.");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("rohair");
+    input1->description =
+	_("Name of the air density map ~[0.9;1.5], Pawan (2004) use 1.12 constant value");
+    input1->answer = _("rohair");
+    input2 = G_define_option();
+    input2->key = _("cp");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("parameter, float number");
+    input2->description =
+	_("Value of the air specific heat [1000.0;1020.0]");
+    input2->answer = _("1004.16");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("tempk");
+    input3->description =
+	_("Name of the surface skin temperature map [degrees Kelvin],if used with -s flag and affine coefs, it disables dtair input");
+    input3->answer = _("tempk");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("dtair");
+    input4->required = NO;
+    input4->description =
+	_("Name of the skin-air Surface temperature difference map ~[0.0-80.0], required unless you use -s flag (then you must give a & b coefs below)");
+    input5 = G_define_option();
+    input5->key = _("a");
+    input5->type = TYPE_DOUBLE;
+    input5->required = NO;
+    input5->gisprompt = _("parameter, float number");
+    input5->description =
+	_("-s flag: Value of the slope of the affine transform");
+    input6 = G_define_option();
+    input6->key = _("b");
+    input6->type = TYPE_DOUBLE;
+    input6->required = NO;
+    input6->gisprompt = _("parameter, float number");
+    input6->description =
+	_("-s flag: Value of the intercept of the affine transform");
+    input7 = G_define_standard_option(G_OPT_R_INPUT);
+    input7->key = _("disp");
+    input7->description = _("Name of the displacement height input map (m)");
+    input7->answer = _("disp");
+    input8 = G_define_standard_option(G_OPT_R_INPUT);
+    input8->key = _("z0m");
+    input8->description = _("Name of the z0m input map (s/m)");
+    input8->answer = _("z0m");
+    input9 = G_define_standard_option(G_OPT_R_INPUT);
+    input9->key = _("z0h");
+    input9->description = _("Name of the z0h input map (s/m)");
+    input9->answer = _("z0h");
+    input10 = G_define_standard_option(G_OPT_R_INPUT);
+    input10->key = _("u_hu");
+    input10->description =
+	_("Name of the wind speed at height (hu) input map (m/s)");
+    input10->answer = _("u_hu");
+    input_hu = G_define_standard_option(G_OPT_R_INPUT);
+    input_hu->key = _("hu");
+    input_hu->description =
+	_("Name of the height (hu) of measurement of wind speed input map (m/s)");
+    input_hu->answer = _("hu");
+    input11 = G_define_option();
+    input11->key = _("iteration");
+    input11->type = TYPE_INTEGER;
+    input11->required = NO;
+    input11->gisprompt = _("parameter, integer number");
+    input11->description = _("number of iteration of rah stabilization");
+    input11->answer = _("3");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output h0 layer");
+    flag1 = G_define_flag();
+    flag1->key = 's';
+    flag1->description =
+	_("Affine transform of Surface temperature into delta T, needs input of slope and intercept (Bastiaanssen, 1995)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    rohair = input1->answer;
+    cp = atof(input2->answer);
+    if (input3->answer) {
+	tempk = input3->answer;
+    }
+    if (input4->answer) {
+	dtair = input4->answer;
+    }
+    if (input5->answer) {
+	a = atof(input5->answer);
+    }
+    if (input6->answer) {
+	b = atof(input6->answer);
+    }
+    disp = input7->answer;
+    z0m = input8->answer;
+    z0h = input9->answer;
+    u_hu = input10->answer;
+    hu = input_hu->answer;
+    if (input11->answer) {
+	iteration = atoi(input11->answer);
+    }
+    else {
+	iteration = 3;
+    }
+    result = output1->answer;
+    sebal = flag1->answer;
+    
+
+	/***************************************************/ 
+	/* TEST FOR -s FLAG COEFS 
+	 * Return error if not proper flag coefs
+	 *                                                 */ 
+
+	/***************************************************/ 
+	if (sebal && !a && !b) {
+	G_fatal_error(_("FATAL ERROR: -s Flag requires coefs a & b!"));
+    }
+    else if (!sebal && !dtair) {
+	G_fatal_error(_("FATAL ERROR: No -s Flag, use DeltaT map!"));
+    }
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rohair, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), rohair);
+    }
+    data_type_rohair = G_raster_map_type(rohair, mapset);
+    if ((infd_rohair = G_open_cell_old(rohair, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rohair);
+    if (G_get_cellhd(rohair, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), rohair);
+    inrast_rohair = G_allocate_raster_buf(data_type_rohair);
+    
+
+	/***************************************************/ 
+	if (!sebal) {
+	mapset = G_find_cell2(dtair, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), dtair);
+	}
+	data_type_dtair = G_raster_map_type(dtair, mapset);
+	if ((infd_dtair = G_open_cell_old(dtair, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), dtair);
+	if (G_get_cellhd(dtair, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), dtair);
+	inrast_dtair = G_allocate_raster_buf(data_type_dtair);
+    }
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(disp, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), disp);
+    }
+    data_type_disp = G_raster_map_type(disp, mapset);
+    if ((infd_disp = G_open_cell_old(disp, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), disp);
+    if (G_get_cellhd(disp, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), disp);
+    inrast_disp = G_allocate_raster_buf(data_type_disp);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(z0m, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), z0m);
+    }
+    data_type_z0m = G_raster_map_type(z0m, mapset);
+    if ((infd_z0m = G_open_cell_old(z0m, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), z0m);
+    if (G_get_cellhd(z0m, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), z0m);
+    inrast_z0m = G_allocate_raster_buf(data_type_z0m);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(z0h, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), z0h);
+    }
+    data_type_z0h = G_raster_map_type(z0h, mapset);
+    if ((infd_z0h = G_open_cell_old(z0h, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), z0h);
+    if (G_get_cellhd(z0h, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), z0h);
+    inrast_z0h = G_allocate_raster_buf(data_type_z0h);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(u_hu, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), u_hu);
+    }
+    data_type_u_hu = G_raster_map_type(u_hu, mapset);
+    if ((infd_u_hu = G_open_cell_old(u_hu, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), u_hu);
+    if (G_get_cellhd(u_hu, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), u_hu);
+    inrast_u_hu = G_allocate_raster_buf(data_type_u_hu);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(hu, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), hu);
+    }
+    data_type_hu = G_raster_map_type(hu, mapset);
+    if ((infd_hu = G_open_cell_old(hu, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), hu);
+    if (G_get_cellhd(hu, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), hu);
+    inrast_hu = G_allocate_raster_buf(data_type_hu);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rohair;
+	DCELL d_rah;
+	DCELL d_dtair;
+	DCELL d_affine;
+	DCELL d_tempk;
+	DCELL d_disp;
+	DCELL d_z0m;
+	DCELL d_z0h;
+	DCELL d_u_hu;
+	DCELL d_hu;
+	G_percent(row, nrows, 2);
+	
+	    /* printf("row = %i/%i\n",row,nrows); */ 
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_rohair, inrast_rohair, row, data_type_rohair) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), rohair);
+	if (!sebal) {
+	    if (G_get_raster_row
+		 (infd_dtair, inrast_dtair, row, data_type_dtair) < 0)
+		G_fatal_error(_("Could not read from <%s>"), dtair);
+	}
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_disp, inrast_disp, row, data_type_disp) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), disp);
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	if (G_get_raster_row(infd_z0h, inrast_z0h, row, data_type_z0h) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0h);
+	if (G_get_raster_row(infd_u_hu, inrast_u_hu, row, data_type_u_hu) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), u_hu);
+	if (G_get_raster_row(infd_hu, inrast_hu, row, data_type_hu) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), hu);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_rohair) {
+	    case CELL_TYPE:
+		d_rohair = (double)((CELL *) inrast_rohair)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rohair = (double)((FCELL *) inrast_rohair)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rohair = ((DCELL *) inrast_rohair)[col];
+		break;
+	    }
+	    if (!sebal) {
+		switch (data_type_dtair) {
+		case CELL_TYPE:
+		    d_dtair = (double)((CELL *) inrast_dtair)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_dtair = (double)((FCELL *) inrast_dtair)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_dtair = ((DCELL *) inrast_dtair)[col];
+		    break;
+		}
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_disp = (double)((CELL *) inrast_disp)[col];
+		break;
+	    case FCELL_TYPE:
+		d_disp = (double)((FCELL *) inrast_disp)[col];
+		break;
+	    case DCELL_TYPE:
+		d_disp = ((DCELL *) inrast_disp)[col];
+		break;
+	    }
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = ((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    switch (data_type_z0h) {
+	    case CELL_TYPE:
+		d_z0h = (double)((CELL *) inrast_z0h)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0h = (double)((FCELL *) inrast_z0h)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0h = ((DCELL *) inrast_z0h)[col];
+		break;
+	    }
+	    switch (data_type_u_hu) {
+	    case CELL_TYPE:
+		d_u_hu = (double)((CELL *) inrast_u_hu)[col];
+		break;
+	    case FCELL_TYPE:
+		d_u_hu = (double)((FCELL *) inrast_u_hu)[col];
+		break;
+	    case DCELL_TYPE:
+		d_u_hu = ((DCELL *) inrast_u_hu)[col];
+		break;
+	    }
+	    switch (data_type_hu) {
+	    case CELL_TYPE:
+		d_hu = (double)((CELL *) inrast_hu)[col];
+		break;
+	    case FCELL_TYPE:
+		d_hu = (double)((FCELL *) inrast_hu)[col];
+		break;
+	    case DCELL_TYPE:
+		d_hu = ((DCELL *) inrast_hu)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_rohair) || 
+		 ((!sebal) && G_is_d_null_value(&d_dtair)) ||
+		 G_is_d_null_value(&d_tempk) || G_is_d_null_value(&d_disp)
+		 || G_is_d_null_value(&d_z0m) || G_is_d_null_value(&d_z0h)
+		 || G_is_d_null_value(&d_u_hu) ||
+		 G_is_d_null_value(&d_hu)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate sensible heat flux     */ 
+		    if (sebal) {	/*if -s flag then calculate Delta T from Coefs */
+		    d_affine = a * d_tempk + b;
+		    
+			/* Run iterations to find Rah */ 
+			d_rah =
+			fixed_deltat(d_u_hu, d_rohair, cp, d_affine, d_disp,
+				     d_z0m, d_z0h, d_tempk, d_hu, iteration);
+		    
+			/*Process h */ 
+			d = h0(d_rohair, cp, d_rah, d_affine);
+		}
+		else {		/* not -s flag then take delta T map input directly */
+		    
+			/* Run iterations to find Rah */ 
+			d_rah =
+			fixed_deltat(d_u_hu, d_rohair, cp, d_dtair, d_disp,
+				     d_z0m, d_z0h, d_tempk, d_hu, iteration);
+		    
+			/*Process h */ 
+			d = h0(d_rohair, cp, d_rah, d_dtair);
+		}
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_rohair);
+    if (!sebal) {
+	G_free(inrast_dtair);
+    }
+    G_free(inrast_tempk);
+    G_free(inrast_disp);
+    G_free(inrast_z0m);
+    G_free(inrast_z0h);
+    G_free(inrast_u_hu);
+    G_free(inrast_hu);
+    G_close_cell(infd_rohair);
+    if (!sebal) {
+	G_close_cell(infd_dtair);
+    }
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_disp);
+    G_close_cell(infd_z0m);
+    G_close_cell(infd_z0h);
+    G_close_cell(infd_u_hu);
+    G_close_cell(infd_hu);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.molength/h0.c
===================================================================
--- grass-addons/gipe/i.eb.molength/h0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.molength/h0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,10 +3,9 @@
 
 double h0(double roh_air, double cp, double rah, double dtair)
 {
-	double result;
+    double result;
 
-	result = roh_air*cp*dtair / rah;
-	
-	return result;
-}
+    result = roh_air * cp * dtair / rah;
 
+    return result;
+}

Modified: grass-addons/gipe/i.eb.molength/main.c
===================================================================
--- grass-addons/gipe/i.eb.molength/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.molength/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,257 +1,282 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.molength
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the Monin-Obukov Length
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double mo_length(double roh_air, double cp, double ustar, double tempk, double h0);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5;
-	struct Option *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_rohair, infd_tempk, infd_ustar, infd_h0;
-	int outfd;
-	
-	char *rohair,*tempk,*ustar,*h0;
-
-	double cp; /*air specific heat*/	
-	int i=0,j=0;
-	
-	void *inrast_rohair, *inrast_tempk, *inrast_ustar, *inrast_h0;
-	DCELL *outrast;
-	
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_rohair;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_ustar;
-	RASTER_MAP_TYPE data_type_h0;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("Monin-Obukov Length, energy balance, SEBAL");
-	module->description = _("Monin-Obukov Length");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("rohair");
-	input1->description=_("Name of the air density map ~[0.9;1.5]");
-
-	input2 = G_define_option() ;
-	input2->key        =_("cp");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  =_("parameter, float number");
-	input2->description=_("Value of the air specific heat [1000.0;1020.0]");
-	input2->answer     =_("1004.0");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("ustar");
-	input3->description=_("Name of the ustar map");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("tempk");
-	input4->description=_("Name of the surface skin temperature map [degrees Kelvin]");
-	
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("h0");
-	input5->description=_("Name of the sensible heat flux map");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output Monin-Obukov Length layer");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	rohair	 	= input1->answer;
-	cp	 	= atof(input2->answer);
-	ustar		= input3->answer;
-	tempk	 	= input4->answer;
-	h0	 	= input5->answer;
-	
-	result  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(rohair, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), rohair);
-	}
-	data_type_rohair = G_raster_map_type(rohair,mapset);
-	if ( (infd_rohair = G_open_cell_old (rohair,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), rohair);
-	if (G_get_cellhd (rohair, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), rohair);
-	inrast_rohair = G_allocate_raster_buf(data_type_rohair);
-	/***************************************************/
-	mapset = G_find_cell2 (ustar, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), ustar);
-	}
-	data_type_ustar = G_raster_map_type(ustar,mapset);
-	if ( (infd_ustar = G_open_cell_old (ustar,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ustar);
-	if (G_get_cellhd (ustar, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ustar);
-	inrast_ustar = G_allocate_raster_buf(data_type_ustar);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (h0, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), h0);
-	}
-	data_type_h0 = G_raster_map_type(h0,mapset);
-	if ( (infd_h0 = G_open_cell_old (h0,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), h0);
-	if (G_get_cellhd (h0, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), h0);
-	inrast_h0 = G_allocate_raster_buf(data_type_h0);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rohair;
-		DCELL d_ustar;
-		DCELL d_tempk;
-		DCELL d_h0;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_rohair,inrast_rohair,row,data_type_rohair)<0)
-			G_fatal_error(_("Could not read from <%s>"),rohair);
-		if(G_get_raster_row(infd_ustar,inrast_ustar,row,data_type_ustar)<0)
-			G_fatal_error(_("Could not read from <%s>"),ustar);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_h0,inrast_h0,row,data_type_h0)<0)
-			G_fatal_error(_("Could not read from <%s>"),h0);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_rohair){
-				case CELL_TYPE:
-					d_rohair = (double) ((CELL *) inrast_rohair)[col];
-					break;
-				case FCELL_TYPE:
-					d_rohair = (double) ((FCELL *) inrast_rohair)[col];
-					break;
-				case DCELL_TYPE:
-					d_rohair = ((DCELL *) inrast_rohair)[col];
-					break;
-			}
-			switch(data_type_ustar){
-				case CELL_TYPE:
-					d_ustar = (double) ((CELL *) inrast_ustar)[col];
-					break;
-				case FCELL_TYPE:
-					d_ustar = (double) ((FCELL *) inrast_ustar)[col];
-					break;
-				case DCELL_TYPE:
-					d_ustar = ((DCELL *) inrast_ustar)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_h0){
-				case CELL_TYPE:
-					d_h0 = (double) ((CELL *) inrast_h0)[col];
-					break;
-				case FCELL_TYPE:
-					d_h0 = (double) ((FCELL *) inrast_h0)[col];
-					break;
-				case DCELL_TYPE:
-					d_h0 = ((DCELL *) inrast_h0)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_rohair)||
-			G_is_d_null_value(&d_ustar)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_h0)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate Monin-Obukov Length    */
-				d = mo_length(d_rohair,cp,d_ustar,d_tempk, d_h0);
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_rohair);
-	G_free (inrast_ustar);
-	G_free (inrast_tempk);
-	G_free (inrast_h0);
-
-	G_close_cell (infd_rohair);
-	G_close_cell (infd_ustar);
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_h0);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.molength
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the Monin-Obukov Length
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double mo_length(double roh_air, double cp, double ustar, double tempk,
+		  double h0);
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5;
+
+    struct Option *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_rohair, infd_tempk, infd_ustar, infd_h0;
+
+    int outfd;
+
+    char *rohair, *tempk, *ustar, *h0;
+
+    double cp;		/*air specific heat */
+
+    int i = 0, j = 0;
+
+    void *inrast_rohair, *inrast_tempk, *inrast_ustar, *inrast_h0;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_rohair;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_ustar;
+    RASTER_MAP_TYPE data_type_h0;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("Monin-Obukov Length, energy balance, SEBAL");
+    module->description = _("Monin-Obukov Length");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("rohair");
+    input1->description = _("Name of the air density map ~[0.9;1.5]");
+    input2 = G_define_option();
+    input2->key = _("cp");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("parameter, float number");
+    input2->description =
+	_("Value of the air specific heat [1000.0;1020.0]");
+    input2->answer = _("1004.0");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("ustar");
+    input3->description = _("Name of the ustar map");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("tempk");
+    input4->description =
+	_("Name of the surface skin temperature map [degrees Kelvin]");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("h0");
+    input5->description = _("Name of the sensible heat flux map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output Monin-Obukov Length layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    rohair = input1->answer;
+    cp = atof(input2->answer);
+    ustar = input3->answer;
+    tempk = input4->answer;
+    h0 = input5->answer;
+    result = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(rohair, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), rohair);
+    }
+    data_type_rohair = G_raster_map_type(rohair, mapset);
+    if ((infd_rohair = G_open_cell_old(rohair, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), rohair);
+    if (G_get_cellhd(rohair, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), rohair);
+    inrast_rohair = G_allocate_raster_buf(data_type_rohair);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ustar, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), ustar);
+    }
+    data_type_ustar = G_raster_map_type(ustar, mapset);
+    if ((infd_ustar = G_open_cell_old(ustar, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ustar);
+    if (G_get_cellhd(ustar, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ustar);
+    inrast_ustar = G_allocate_raster_buf(data_type_ustar);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(h0, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), h0);
+    }
+    data_type_h0 = G_raster_map_type(h0, mapset);
+    if ((infd_h0 = G_open_cell_old(h0, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), h0);
+    if (G_get_cellhd(h0, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), h0);
+    inrast_h0 = G_allocate_raster_buf(data_type_h0);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rohair;
+	DCELL d_ustar;
+	DCELL d_tempk;
+	DCELL d_h0;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_rohair, inrast_rohair, row, data_type_rohair) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), rohair);
+	if (G_get_raster_row(infd_ustar, inrast_ustar, row, data_type_ustar)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ustar);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_h0, inrast_h0, row, data_type_h0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), h0);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_rohair) {
+	    case CELL_TYPE:
+		d_rohair = (double)((CELL *) inrast_rohair)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rohair = (double)((FCELL *) inrast_rohair)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rohair = ((DCELL *) inrast_rohair)[col];
+		break;
+	    }
+	    switch (data_type_ustar) {
+	    case CELL_TYPE:
+		d_ustar = (double)((CELL *) inrast_ustar)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ustar = (double)((FCELL *) inrast_ustar)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ustar = ((DCELL *) inrast_ustar)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_h0) {
+	    case CELL_TYPE:
+		d_h0 = (double)((CELL *) inrast_h0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_h0 = (double)((FCELL *) inrast_h0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_h0 = ((DCELL *) inrast_h0)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_rohair) || G_is_d_null_value(&d_ustar)
+		 || G_is_d_null_value(&d_tempk) ||
+		 G_is_d_null_value(&d_h0)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate Monin-Obukov Length    */ 
+		    d = mo_length(d_rohair, cp, d_ustar, d_tempk, d_h0);
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_rohair);
+    G_free(inrast_ustar);
+    G_free(inrast_tempk);
+    G_free(inrast_h0);
+    G_close_cell(infd_rohair);
+    G_close_cell(infd_ustar);
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_h0);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.molength/mo_length.c
===================================================================
--- grass-addons/gipe/i.eb.molength/mo_length.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.molength/mo_length.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,12 +1,12 @@
 #include<stdio.h>
 #include<math.h>
 
-double mo_length(double roh_air, double cp, double ustar, double tempk, double h0)
+double mo_length(double roh_air, double cp, double ustar, double tempk,
+		 double h0)
 {
-	double result;
+    double result;
 
-	result = -roh_air*cp*pow(ustar,3)*tempk/(0.41*9.81*h0);
-	
-	return result;
+    result = -roh_air * cp * pow(ustar, 3) * tempk / (0.41 * 9.81 * h0);
+
+    return result;
 }
-

Modified: grass-addons/gipe/i.eb.netrad/main.c
===================================================================
--- grass-addons/gipe/i.eb.netrad/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.netrad/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,438 +1,485 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.netrad
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the instantaneous net radiation at 
- *               as seen in Bastiaanssen (1995) using time of
- *               satellite overpass.
- *
- * COPYRIGHT:    (C) 2006-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double r_net( double bbalb, double ndvi, double tempk, double dtair,  double e0, double tsw, double doy, double utc, double sunzangle);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5;
-	struct Option *input6, *input7, *input8, *input9, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_albedo, infd_ndvi, infd_tempk, infd_time, infd_dtair;
-	int infd_emissivity, infd_tsw, infd_doy, infd_sunzangle;
-	int outfd;
-	
-	char *albedo,*ndvi,*tempk,*time,*dtair,*emissivity;
-	char *tsw,*doy,*sunzangle;
-	
-	int i=0,j=0;
-	
-	void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet;
-	void *inrast_time, *inrast_dtair, *inrast_emissivity, *inrast_tsw;
-	void *inrast_doy, *inrast_sunzangle;
-
-	DCELL *outrast;
-
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_ndvi;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_time;
-	RASTER_MAP_TYPE data_type_dtair;
-	RASTER_MAP_TYPE data_type_emissivity;
-	RASTER_MAP_TYPE data_type_tsw;
-	RASTER_MAP_TYPE data_type_doy;
-	RASTER_MAP_TYPE data_type_sunzangle;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("net radiation, energy balance, SEBAL");
-	module->description = _("net radiation approximation (Bastiaanssen, 1995)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("albedo");
-	input1->description=_("Name of the Albedo map [0.0;1.0]");
-	input1->answer     =_("albedo");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("ndvi");
-	input2->description=_("Name of the ndvi map [-1.0;+1.0]");
-	input2->answer     =_("ndvi");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("tempk");
-	input3->description=_("Name of the Surface temperature map [degree Kelvin]");
-	input3->answer     =_("tempk");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("time");
-	input4->description=_("Name of the map of local UTC time of satellite overpass [hh.hhh]");
-	input4->answer     =_("time");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("dtair");
-	input5->description=_("Name of the difference of temperature from surface skin to about 2 m height [K]");
-	input5->answer     =_("dtair");
-	
-	input6 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input6->key        =_("emissivity");
-	input6->description=_("Name of the emissivity map [-]");
-	input6->answer     =_("emissivity");
-
-	input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input7->key        =_("tsw");
-	input7->description=_("Name of the single-way atmospheric transmissivitymap [-]");
-	input7->answer     =_("tsw");
-
-	input8 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input8->key        =_("doy");
-	input8->description=_("Name of the Day Of Year (DOY) map [-]");
-	input8->answer     =_("doy");
-
-	input9 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input9->key        =_("sunzangle");
-	input9->description=_("Name of the sun zenith angle map [degrees]");
-	input9->answer     =_("sunzangle");
-
-	output1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	output1->key        =_("rnet");
-	output1->description=_("Name of the output rnet layer");
-	output1->answer     =_("rnet");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	albedo	 	= input1->answer;
-	ndvi	 	= input2->answer;
-	tempk		= input3->answer;
-	time	 	= input4->answer;
-	dtair	 	= input5->answer;
-	emissivity	= input6->answer;
-	tsw		= input7->answer;
-	doy		= input8->answer;
-	sunzangle	= input9->answer;
-
-	result  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(albedo, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), albedo);
-	}
-	data_type_albedo = G_raster_map_type(albedo,mapset);
-	if ( (infd_albedo = G_open_cell_old (albedo,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), albedo);
-	if (G_get_cellhd (albedo, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), albedo);
-	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
-	/***************************************************/
-	mapset = G_find_cell2 (ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (dtair, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), dtair);
-	}
-	data_type_dtair = G_raster_map_type(dtair,mapset);
-	if ( (infd_dtair = G_open_cell_old (dtair,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), dtair);
-	if (G_get_cellhd (dtair, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), dtair);
-	inrast_dtair = G_allocate_raster_buf(data_type_dtair);
-	/***************************************************/
-	mapset = G_find_cell2 (time, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), time);
-	}
-	data_type_time = G_raster_map_type(time,mapset);
-	if ( (infd_time = G_open_cell_old (time,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), time);
-	if (G_get_cellhd (time, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), time);
-	inrast_time = G_allocate_raster_buf(data_type_time);
-	/***************************************************/
-	mapset = G_find_cell2 (emissivity, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), emissivity);
-	}
-	data_type_emissivity = G_raster_map_type(emissivity,mapset);
-	if ( (infd_emissivity = G_open_cell_old (emissivity,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), emissivity);
-	if (G_get_cellhd (emissivity, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), emissivity);
-	inrast_emissivity = G_allocate_raster_buf(data_type_emissivity);
-	/***************************************************/
-	mapset = G_find_cell2 (tsw, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tsw);
-	}
-	data_type_tsw = G_raster_map_type(tsw,mapset);
-	if ( (infd_tsw = G_open_cell_old (tsw,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tsw);
-	if (G_get_cellhd (tsw, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tsw);
-	inrast_tsw = G_allocate_raster_buf(data_type_tsw);
-	/***************************************************/
-	mapset = G_find_cell2 (doy, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), doy);
-	}
-	data_type_doy = G_raster_map_type(doy,mapset);
-	if ( (infd_doy = G_open_cell_old (doy,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), doy);
-	if (G_get_cellhd (doy, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), doy);
-	inrast_doy = G_allocate_raster_buf(data_type_doy);
-	/***************************************************/
-	mapset = G_find_cell2 (sunzangle, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), sunzangle);
-	}
-	data_type_sunzangle = G_raster_map_type(sunzangle,mapset);
-	if ( (infd_sunzangle = G_open_cell_old (sunzangle,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), sunzangle);
-	if (G_get_cellhd (sunzangle, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), sunzangle);
-	inrast_sunzangle = G_allocate_raster_buf(data_type_sunzangle);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_albedo;
-		DCELL d_ndvi;
-		DCELL d_tempk;
-		DCELL d_dtair;
-		DCELL d_time;
-		DCELL d_emissivity;
-		DCELL d_tsw;
-		DCELL d_doy;
-		DCELL d_sunzangle;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_dtair,inrast_dtair,row,data_type_dtair)<0)
-			G_fatal_error(_("Could not read from <%s>"),dtair);
-		if(G_get_raster_row(infd_time,inrast_time,row,data_type_time)<0)
-			G_fatal_error(_("Could not read from <%s>"),time);
-		if(G_get_raster_row(infd_emissivity,inrast_emissivity,row,data_type_emissivity)<0)
-			G_fatal_error(_("Could not read from <%s>"),emissivity);
-		if(G_get_raster_row(infd_tsw,inrast_tsw,row,data_type_tsw)<0)
-			G_fatal_error(_("Could not read from <%s>"),tsw);
-		if(G_get_raster_row(infd_doy,inrast_doy,row,data_type_doy)<0)
-			G_fatal_error(_("Could not read from <%s>"),doy);
-		if(G_get_raster_row(infd_sunzangle,inrast_sunzangle,row,data_type_sunzangle)<0)
-			G_fatal_error(_("Could not read from <%s>"),sunzangle);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_dtair){
-				case CELL_TYPE:
-					d_dtair = (double) ((CELL *) inrast_dtair)[col];
-					break;
-				case FCELL_TYPE:
-					d_dtair = (double) ((FCELL *) inrast_dtair)[col];
-					break;
-				case DCELL_TYPE:
-					d_dtair = (double) ((DCELL *) inrast_dtair)[col];
-					break;
-			}
-			switch(data_type_time){
-				case CELL_TYPE:
-					d_time = (double) ((CELL *) inrast_time)[col];
-					break;
-				case FCELL_TYPE:
-					d_time = (double) ((FCELL *) inrast_time)[col];
-					break;
-				case DCELL_TYPE:
-					d_time = (double) ((DCELL *) inrast_time)[col];
-					break;
-			}
-			switch(data_type_emissivity){
-				case CELL_TYPE:
-					d_emissivity = (double) ((CELL *) inrast_emissivity)[col];
-					break;
-				case FCELL_TYPE:
-					d_emissivity = (double) ((FCELL *) inrast_emissivity)[col];
-					break;
-				case DCELL_TYPE:
-					d_emissivity = (double) ((DCELL *) inrast_emissivity)[col];
-					break;
-			}
-			switch(data_type_tsw){
-				case CELL_TYPE:
-					d_tsw = (double) ((CELL *) inrast_tsw)[col];
-					break;
-				case FCELL_TYPE:
-					d_tsw = (double) ((FCELL *) inrast_tsw)[col];
-					break;
-				case DCELL_TYPE:
-					d_tsw = (double) ((DCELL *) inrast_tsw)[col];
-					break;
-			}
-			switch(data_type_doy){
-				case CELL_TYPE:
-					d_doy = (double) ((CELL *) inrast_doy)[col];
-					break;
-				case FCELL_TYPE:
-					d_doy = (double) ((FCELL *) inrast_doy)[col];
-					break;
-				case DCELL_TYPE:
-					d_doy = (double) ((DCELL *) inrast_doy)[col];
-					break;
-			}
-			switch(data_type_sunzangle){
-				case CELL_TYPE:
-					d_sunzangle = (double) ((CELL *) inrast_sunzangle)[col];
-					break;
-				case FCELL_TYPE:
-					d_sunzangle = (double) ((FCELL *) inrast_sunzangle)[col];
-					break;
-				case DCELL_TYPE:
-					d_sunzangle = (double) ((DCELL *) inrast_sunzangle)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_ndvi)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_dtair)||
-			G_is_d_null_value(&d_time)||
-			G_is_d_null_value(&d_emissivity)||
-			G_is_d_null_value(&d_tsw)||
-			G_is_d_null_value(&d_doy)||
-			G_is_d_null_value(&d_sunzangle)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate the net radiation	    */
-				d = r_net(d_albedo,d_ndvi,d_tempk,d_dtair,d_emissivity,d_tsw,d_doy,d_time,d_sunzangle); 
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_albedo);
-	G_free (inrast_ndvi);
-	G_free (inrast_tempk);
-	G_free (inrast_dtair);
-	G_free (inrast_time);
-	G_free (inrast_emissivity);
-	G_free (inrast_tsw);
-	G_free (inrast_doy);
-	G_free (inrast_sunzangle);
-	G_close_cell (infd_albedo);
-	G_close_cell (infd_ndvi);
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_dtair);
-	G_close_cell (infd_time);
-	G_close_cell (infd_emissivity);
-	G_close_cell (infd_tsw);
-	G_close_cell (infd_doy);
-	G_close_cell (infd_sunzangle);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-	
-	/* Colors in grey shade */
-	G_init_colors(&colors);
-	G_add_color_rule(0,0,0,0,900,255,255,255,&colors);
-	/* Metadata */
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.netrad
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the instantaneous net radiation at 
+ *               as seen in Bastiaanssen (1995) using time of
+ *               satellite overpass.
+ *
+ * COPYRIGHT:    (C) 2006-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double r_net(double bbalb, double ndvi, double tempk, double dtair,
+	      double e0, double tsw, double doy, double utc,
+	      double sunzangle);
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5;
+
+    struct Option *input6, *input7, *input8, *input9, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_albedo, infd_ndvi, infd_tempk, infd_time, infd_dtair;
+
+    int infd_emissivity, infd_tsw, infd_doy, infd_sunzangle;
+
+    int outfd;
+
+    char *albedo, *ndvi, *tempk, *time, *dtair, *emissivity;
+
+    char *tsw, *doy, *sunzangle;
+
+    int i = 0, j = 0;
+
+    void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet;
+
+    void *inrast_time, *inrast_dtair, *inrast_emissivity, *inrast_tsw;
+
+    void *inrast_doy, *inrast_sunzangle;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_albedo;
+    RASTER_MAP_TYPE data_type_ndvi;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_time;
+    RASTER_MAP_TYPE data_type_dtair;
+    RASTER_MAP_TYPE data_type_emissivity;
+    RASTER_MAP_TYPE data_type_tsw;
+    RASTER_MAP_TYPE data_type_doy;
+    RASTER_MAP_TYPE data_type_sunzangle;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("net radiation, energy balance, SEBAL");
+    module->description =
+	_("net radiation approximation (Bastiaanssen, 1995)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("albedo");
+    input1->description = _("Name of the Albedo map [0.0;1.0]");
+    input1->answer = _("albedo");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("ndvi");
+    input2->description = _("Name of the ndvi map [-1.0;+1.0]");
+    input2->answer = _("ndvi");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("tempk");
+    input3->description =
+	_("Name of the Surface temperature map [degree Kelvin]");
+    input3->answer = _("tempk");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("time");
+    input4->description =
+	_("Name of the map of local UTC time of satellite overpass [hh.hhh]");
+    input4->answer = _("time");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("dtair");
+    input5->description =
+	_("Name of the difference of temperature from surface skin to about 2 m height [K]");
+    input5->answer = _("dtair");
+    input6 = G_define_standard_option(G_OPT_R_INPUT);
+    input6->key = _("emissivity");
+    input6->description = _("Name of the emissivity map [-]");
+    input6->answer = _("emissivity");
+    input7 = G_define_standard_option(G_OPT_R_INPUT);
+    input7->key = _("tsw");
+    input7->description =
+	_("Name of the single-way atmospheric transmissivitymap [-]");
+    input7->answer = _("tsw");
+    input8 = G_define_standard_option(G_OPT_R_INPUT);
+    input8->key = _("doy");
+    input8->description = _("Name of the Day Of Year (DOY) map [-]");
+    input8->answer = _("doy");
+    input9 = G_define_standard_option(G_OPT_R_INPUT);
+    input9->key = _("sunzangle");
+    input9->description = _("Name of the sun zenith angle map [degrees]");
+    input9->answer = _("sunzangle");
+    output1 = G_define_standard_option(G_OPT_R_INPUT);
+    output1->key = _("rnet");
+    output1->description = _("Name of the output rnet layer");
+    output1->answer = _("rnet");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    albedo = input1->answer;
+    ndvi = input2->answer;
+    tempk = input3->answer;
+    time = input4->answer;
+    dtair = input5->answer;
+    emissivity = input6->answer;
+    tsw = input7->answer;
+    doy = input8->answer;
+    sunzangle = input9->answer;
+    result = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(albedo, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), albedo);
+    }
+    data_type_albedo = G_raster_map_type(albedo, mapset);
+    if ((infd_albedo = G_open_cell_old(albedo, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+    if (G_get_cellhd(albedo, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), albedo);
+    inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(dtair, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), dtair);
+    }
+    data_type_dtair = G_raster_map_type(dtair, mapset);
+    if ((infd_dtair = G_open_cell_old(dtair, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), dtair);
+    if (G_get_cellhd(dtair, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), dtair);
+    inrast_dtair = G_allocate_raster_buf(data_type_dtair);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(time, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), time);
+    }
+    data_type_time = G_raster_map_type(time, mapset);
+    if ((infd_time = G_open_cell_old(time, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), time);
+    if (G_get_cellhd(time, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), time);
+    inrast_time = G_allocate_raster_buf(data_type_time);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(emissivity, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), emissivity);
+    }
+    data_type_emissivity = G_raster_map_type(emissivity, mapset);
+    if ((infd_emissivity = G_open_cell_old(emissivity, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), emissivity);
+    if (G_get_cellhd(emissivity, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), emissivity);
+    inrast_emissivity = G_allocate_raster_buf(data_type_emissivity);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tsw, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tsw);
+    }
+    data_type_tsw = G_raster_map_type(tsw, mapset);
+    if ((infd_tsw = G_open_cell_old(tsw, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tsw);
+    if (G_get_cellhd(tsw, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tsw);
+    inrast_tsw = G_allocate_raster_buf(data_type_tsw);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(doy, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), doy);
+    }
+    data_type_doy = G_raster_map_type(doy, mapset);
+    if ((infd_doy = G_open_cell_old(doy, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), doy);
+    if (G_get_cellhd(doy, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), doy);
+    inrast_doy = G_allocate_raster_buf(data_type_doy);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(sunzangle, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), sunzangle);
+    }
+    data_type_sunzangle = G_raster_map_type(sunzangle, mapset);
+    if ((infd_sunzangle = G_open_cell_old(sunzangle, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), sunzangle);
+    if (G_get_cellhd(sunzangle, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), sunzangle);
+    inrast_sunzangle = G_allocate_raster_buf(data_type_sunzangle);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_albedo;
+	DCELL d_ndvi;
+	DCELL d_tempk;
+	DCELL d_dtair;
+	DCELL d_time;
+	DCELL d_emissivity;
+	DCELL d_tsw;
+	DCELL d_doy;
+	DCELL d_sunzangle;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_dtair, inrast_dtair, row, data_type_dtair)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), dtair);
+	if (G_get_raster_row(infd_time, inrast_time, row, data_type_time) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), time);
+	if (G_get_raster_row
+	     (infd_emissivity, inrast_emissivity, row,
+	      data_type_emissivity) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), emissivity);
+	if (G_get_raster_row(infd_tsw, inrast_tsw, row, data_type_tsw) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tsw);
+	if (G_get_raster_row(infd_doy, inrast_doy, row, data_type_doy) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), doy);
+	if (G_get_raster_row
+	     (infd_sunzangle, inrast_sunzangle, row, data_type_sunzangle) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), sunzangle);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = ((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_dtair) {
+	    case CELL_TYPE:
+		d_dtair = (double)((CELL *) inrast_dtair)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dtair = (double)((FCELL *) inrast_dtair)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dtair = (double)((DCELL *) inrast_dtair)[col];
+		break;
+	    }
+	    switch (data_type_time) {
+	    case CELL_TYPE:
+		d_time = (double)((CELL *) inrast_time)[col];
+		break;
+	    case FCELL_TYPE:
+		d_time = (double)((FCELL *) inrast_time)[col];
+		break;
+	    case DCELL_TYPE:
+		d_time = (double)((DCELL *) inrast_time)[col];
+		break;
+	    }
+	    switch (data_type_emissivity) {
+	    case CELL_TYPE:
+		d_emissivity = (double)((CELL *) inrast_emissivity)[col];
+		break;
+	    case FCELL_TYPE:
+		d_emissivity = (double)((FCELL *) inrast_emissivity)[col];
+		break;
+	    case DCELL_TYPE:
+		d_emissivity = (double)((DCELL *) inrast_emissivity)[col];
+		break;
+	    }
+	    switch (data_type_tsw) {
+	    case CELL_TYPE:
+		d_tsw = (double)((CELL *) inrast_tsw)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tsw = (double)((FCELL *) inrast_tsw)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tsw = (double)((DCELL *) inrast_tsw)[col];
+		break;
+	    }
+	    switch (data_type_doy) {
+	    case CELL_TYPE:
+		d_doy = (double)((CELL *) inrast_doy)[col];
+		break;
+	    case FCELL_TYPE:
+		d_doy = (double)((FCELL *) inrast_doy)[col];
+		break;
+	    case DCELL_TYPE:
+		d_doy = (double)((DCELL *) inrast_doy)[col];
+		break;
+	    }
+	    switch (data_type_sunzangle) {
+	    case CELL_TYPE:
+		d_sunzangle = (double)((CELL *) inrast_sunzangle)[col];
+		break;
+	    case FCELL_TYPE:
+		d_sunzangle = (double)((FCELL *) inrast_sunzangle)[col];
+		break;
+	    case DCELL_TYPE:
+		d_sunzangle = (double)((DCELL *) inrast_sunzangle)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_albedo) || G_is_d_null_value(&d_ndvi)
+		 || G_is_d_null_value(&d_tempk) ||
+		 G_is_d_null_value(&d_dtair) || G_is_d_null_value(&d_time)
+		 || G_is_d_null_value(&d_emissivity) ||
+		 G_is_d_null_value(&d_tsw) || G_is_d_null_value(&d_doy) ||
+		 G_is_d_null_value(&d_sunzangle)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate the net radiation      */ 
+		    d =
+		    r_net(d_albedo, d_ndvi, d_tempk, d_dtair, d_emissivity,
+			  d_tsw, d_doy, d_time, d_sunzangle);
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_albedo);
+    G_free(inrast_ndvi);
+    G_free(inrast_tempk);
+    G_free(inrast_dtair);
+    G_free(inrast_time);
+    G_free(inrast_emissivity);
+    G_free(inrast_tsw);
+    G_free(inrast_doy);
+    G_free(inrast_sunzangle);
+    G_close_cell(infd_albedo);
+    G_close_cell(infd_ndvi);
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_dtair);
+    G_close_cell(infd_time);
+    G_close_cell(infd_emissivity);
+    G_close_cell(infd_tsw);
+    G_close_cell(infd_doy);
+    G_close_cell(infd_sunzangle);
+    G_free(outrast);
+    G_close_cell(outfd);
+    
+	/* Colors in grey shade */ 
+	G_init_colors(&colors);
+    G_add_color_rule(0, 0, 0, 0, 900, 255, 255, 255, &colors);
+    
+	/* Metadata */ 
+	G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.netrad/r_net.c
===================================================================
--- grass-addons/gipe/i.eb.netrad/r_net.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.netrad/r_net.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,41 +1,48 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-#define PI 3.1415927
-
-double r_net( double bbalb, double ndvi, double tempk, double dtair,  double e0, double tsw, double doy, double utc, double sunzangle )
-{
-	/* Tsw =  atmospheric transmissivity single-way (~0.7 -) */
-	/* DOY = Day of Year */
-	/* utc = UTC time of sat overpass*/
-	/* sunzangle = sun zenith angle at sat. overpass */
-	/* tair = air temperature (approximative, or met.station) */
-	
-	double 	Kin=0.0, Lin=0.0, Lout=0.0, Lcorr=0.0, result=0.0;
-	double 	temp=0.0, ds=0.0, e_atm=0.0, delta=0.0;
-	double	tsw_for_e_atm=0.7; /*Special tsw, consider it a coefficient*/
-
-	/* Atmospheric emissivity (Bastiaanssen, 1995)*/
-	e_atm	=  1.08 * pow(-log(tsw),0.265) ;
-	/* Atmospheric emissivity (Pawan, 2004)*/
-/* 	e_atm	= 0.85 * pow(-log(tsw),0.09);*/
-
-/*	ds = 1.0 + 0.01672 * sin(2*PI*(doy-93.5)/365);*/
-	ds = 1.0/pow((1+0.033*cos(2*PI*doy/365)),2);
-	delta = 0.4093*sin((2*PI*doy/365)-1.39);
-	
-	/* Kin is the shortwave incoming radiation*/
-	Kin	= 1358.0 * (cos(sunzangle*PI/180) * tsw / (ds*ds) );
-	/* Lin is incoming longwave radiation*/
-	Lin	= (e_atm) * 5.67 * pow(10,-8) * pow((tempk-dtair),4);
-	/* Lout is surface grey body emission in Longwave spectrum*/
-	Lout	= e0 * 5.67 * pow(10,-8) * pow(tempk,4);
-	/* Lcorr is outgoing longwave radiation "reflected" by the emissivity*/
-	Lcorr	= (1.0 - e0) * Lin;
-	result	= (1.0 - bbalb) * Kin + Lin - Lout - Lcorr  ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ 
+#define PI 3.1415927
+double r_net(double bbalb, double ndvi, double tempk, double dtair,
+	      double e0, double tsw, double doy, double utc,
+	      double sunzangle) 
+{
+    
+	/* Tsw =  atmospheric transmissivity single-way (~0.7 -) */ 
+	/* DOY = Day of Year */ 
+	/* utc = UTC time of sat overpass */ 
+	/* sunzangle = sun zenith angle at sat. overpass */ 
+	/* tair = air temperature (approximative, or met.station) */ 
+    double Kin = 0.0, Lin = 0.0, Lout = 0.0, Lcorr = 0.0, result = 0.0;
+
+    double temp = 0.0, ds = 0.0, e_atm = 0.0, delta = 0.0;
+
+    double tsw_for_e_atm = 0.7;	/*Special tsw, consider it a coefficient */
+
+    
+	/* Atmospheric emissivity (Bastiaanssen, 1995) */ 
+	e_atm = 1.08 * pow(-log(tsw), 0.265);
+    
+	/* Atmospheric emissivity (Pawan, 2004) */ 
+	/*      e_atm   = 0.85 * pow(-log(tsw),0.09); */ 
+	
+	/*      ds = 1.0 + 0.01672 * sin(2*PI*(doy-93.5)/365); */ 
+	ds = 1.0 / pow((1 + 0.033 * cos(2 * PI * doy / 365)), 2);
+    delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    
+	/* Kin is the shortwave incoming radiation */ 
+	Kin = 1358.0 * (cos(sunzangle * PI / 180) * tsw / (ds * ds));
+    
+	/* Lin is incoming longwave radiation */ 
+	Lin = (e_atm) * 5.67 * pow(10, -8) * pow((tempk - dtair), 4);
+    
+	/* Lout is surface grey body emission in Longwave spectrum */ 
+	Lout = e0 * 5.67 * pow(10, -8) * pow(tempk, 4);
+    
+	/* Lcorr is outgoing longwave radiation "reflected" by the emissivity */ 
+	Lcorr = (1.0 - e0) * Lin;
+    result = (1.0 - bbalb) * Kin + Lin - Lout - Lcorr;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.eb.psi/main.c
===================================================================
--- grass-addons/gipe/i.eb.psi/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.psi/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,217 +1,239 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.psi
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the psichrometric parameter for heat,
- *               a flag permits output for momentum.
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-double psi_m(double disp,double molength,double height);
-double psi_h(double disp,double molength, double height);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int momentum=0;/*Flag for psim*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *output1, *output2;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1, *result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_disp,infd_molength;
-	int outfd1,outfd2;
-	
-	char *disp,*molength;
-
-	double height, height_m; /*z height for heat and momentum*/	
-	int i=0,j=0;
-	
-	void *inrast_disp, *inrast_molength;
-	DCELL *outrast1,*outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_disp;
-	RASTER_MAP_TYPE data_type_molength;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("psichrometric, energy balance, SEBAL");
-	module->description = _("psichrometric paramters for heat (standard) and momentum (need flag).");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("disp");
-	input1->description=_("Name of the displacement height map");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("molength");
-	input2->description=_("Name of the Monin-Obukov length map");
-
-	input3 = G_define_option() ;
-	input3->key        =_("height");
-	input3->type       = TYPE_DOUBLE;
-	input3->required   = YES;
-	input3->gisprompt  =_("Value, parameter");
-	input3->description=_("Value of the height for heat flux (2.0 in Pawan,2004)");
-	input3->answer     =_("2.0");
-
-	input4 = G_define_option() ;
-	input4->key        =_("height_m");
-	input4->type       = TYPE_DOUBLE;
-	input4->required   = NO;
-	input4->gisprompt  =_("Value, parameter");
-	input4->description=_("Value of the blending height for momentum (100.0 in Pawan, 2004). Use with flag -m.");
-	input4->answer     =_("100.0");
-	
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output psih layer");
-
-        output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("psim");
-	output2->description=_("Name of the output psim layer");
-
-	flag1 = G_define_flag();
-	flag1->key = 'm';
-	flag1->description = _("Output psim (requires height_m parameter)");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	disp	 	= input1->answer;
-	molength 	= input2->answer;
-	height		= atof(input3->answer);
-	height_m 	= atof(input4->answer);
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	momentum = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2 (disp, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), disp);
-	}
-	data_type_disp = G_raster_map_type(disp,mapset);
-	if ( (infd_disp = G_open_cell_old (disp,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), disp);
-	if (G_get_cellhd (disp, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), disp);
-	inrast_disp = G_allocate_raster_buf(data_type_disp);
-	/***************************************************/
-	mapset = G_find_cell2 (molength, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), molength);
-	}
-	data_type_molength = G_raster_map_type(molength,mapset);
-	if ( (infd_molength = G_open_cell_old (molength,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), molength);
-	if (G_get_cellhd (molength, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), molength);
-	inrast_molength = G_allocate_raster_buf(data_type_molength);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(momentum){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(momentum){
-		if((outfd2 = G_open_raster_new (result2,data_type_output)) < 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_disp;
-		DCELL d_molength;
-		DCELL d_psih;
-		DCELL d_psim;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_disp,inrast_disp,row,data_type_disp)<0)
-			G_fatal_error(_("Could not read from <%s>"),disp);
-		if(G_get_raster_row(infd_molength,inrast_molength,row,data_type_molength)<0)
-			G_fatal_error(_("Could not read from <%s>"),molength);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			d_disp = ((DCELL *) inrast_disp)[col];
-			d_molength = ((DCELL *) inrast_molength)[col];
-			if(G_is_d_null_value(&d_disp)||
-				G_is_d_null_value(&d_molength)){
-				G_set_d_null_value(&outrast1[col],1);
-				if(momentum){
-					G_set_d_null_value(&outrast2[col],1);
-				}
-			}else {
-				/************************************/
-				/* calculate psih   */
-				d_psih=psi_h(d_disp,d_molength,height);
-				outrast1[col] = d_psih;
-				if(momentum){
-					d_psim=psi_m(d_disp,d_molength,height_m);
-					outrast2[col] = d_psim;
-				 }
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-		if(momentum){
-			if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-				G_fatal_error(_("Cannot write to output raster file"));
-		}
-	}
-	G_free (inrast_disp);
-	G_free (inrast_molength);
-
-	G_close_cell (infd_disp);
-	G_close_cell (infd_molength);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	if(momentum){
-		G_free (outrast2);
-		G_close_cell (outfd2);
-
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.psi
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the psichrometric parameter for heat,
+ *               a flag permits output for momentum.
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double psi_m(double disp, double molength, double height);
+
+double psi_h(double disp, double molength, double height);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int momentum = 0;		/*Flag for psim */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *output1, *output2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_disp, infd_molength;
+
+    int outfd1, outfd2;
+
+    char *disp, *molength;
+
+    double height, height_m;	/*z height for heat and momentum */
+
+    int i = 0, j = 0;
+
+    void *inrast_disp, *inrast_molength;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_disp;
+    RASTER_MAP_TYPE data_type_molength;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("psichrometric, energy balance, SEBAL");
+    module->description =
+	_("psichrometric paramters for heat (standard) and momentum (need flag).");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("disp");
+    input1->description = _("Name of the displacement height map");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("molength");
+    input2->description = _("Name of the Monin-Obukov length map");
+    input3 = G_define_option();
+    input3->key = _("height");
+    input3->type = TYPE_DOUBLE;
+    input3->required = YES;
+    input3->gisprompt = _("Value, parameter");
+    input3->description =
+	_("Value of the height for heat flux (2.0 in Pawan,2004)");
+    input3->answer = _("2.0");
+    input4 = G_define_option();
+    input4->key = _("height_m");
+    input4->type = TYPE_DOUBLE;
+    input4->required = NO;
+    input4->gisprompt = _("Value, parameter");
+    input4->description =
+	_("Value of the blending height for momentum (100.0 in Pawan, 2004). Use with flag -m.");
+    input4->answer = _("100.0");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output psih layer");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("psim");
+    output2->description = _("Name of the output psim layer");
+    flag1 = G_define_flag();
+    flag1->key = 'm';
+    flag1->description = _("Output psim (requires height_m parameter)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    disp = input1->answer;
+    molength = input2->answer;
+    height = atof(input3->answer);
+    height_m = atof(input4->answer);
+    result1 = output1->answer;
+    result2 = output2->answer;
+    momentum = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(disp, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), disp);
+    }
+    data_type_disp = G_raster_map_type(disp, mapset);
+    if ((infd_disp = G_open_cell_old(disp, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), disp);
+    if (G_get_cellhd(disp, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), disp);
+    inrast_disp = G_allocate_raster_buf(data_type_disp);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(molength, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), molength);
+    }
+    data_type_molength = G_raster_map_type(molength, mapset);
+    if ((infd_molength = G_open_cell_old(molength, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), molength);
+    if (G_get_cellhd(molength, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), molength);
+    inrast_molength = G_allocate_raster_buf(data_type_molength);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (momentum) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (momentum) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_disp;
+	DCELL d_molength;
+	DCELL d_psih;
+	DCELL d_psim;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_disp, inrast_disp, row, data_type_disp)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), disp);
+	if (G_get_raster_row
+	     (infd_molength, inrast_molength, row, data_type_molength) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), molength);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    d_disp = ((DCELL *) inrast_disp)[col];
+	    d_molength = ((DCELL *) inrast_molength)[col];
+	    if (G_is_d_null_value(&d_disp) ||
+		 G_is_d_null_value(&d_molength)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (momentum) {
+		    G_set_d_null_value(&outrast2[col], 1);
+		}
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate psih   */ 
+		    d_psih = psi_h(d_disp, d_molength, height);
+		outrast1[col] = d_psih;
+		if (momentum) {
+		    d_psim = psi_m(d_disp, d_molength, height_m);
+		    outrast2[col] = d_psim;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	if (momentum) {
+	    if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output raster file"));
+	}
+	}
+    G_free(inrast_disp);
+    G_free(inrast_molength);
+    G_close_cell(infd_disp);
+    G_close_cell(infd_molength);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    if (momentum) {
+	G_free(outrast2);
+	G_close_cell(outfd2);
+	G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.psi/psih.c
===================================================================
--- grass-addons/gipe/i.eb.psi/psih.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.psi/psih.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,11 +3,12 @@
 #include<math.h>
 
 
-double psi_h(double disp,double molength, double height){
-	double xh, psih;
-	
-	xh    = pow(1.0-16.0*((height-disp)/molength),0.25);
-	psih  = psih  = 2.0*log((1.0+xh*xh)/2.0);
+double psi_h(double disp, double molength, double height)
+{
+    double xh, psih;
 
-	return psih;
+    xh = pow(1.0 - 16.0 * ((height - disp) / molength), 0.25);
+    psih = psih = 2.0 * log((1.0 + xh * xh) / 2.0);
+
+    return psih;
 }

Modified: grass-addons/gipe/i.eb.psi/psim.c
===================================================================
--- grass-addons/gipe/i.eb.psi/psim.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.psi/psim.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -4,11 +4,14 @@
 
 #define PI 3.14159265358979323846
 
-double psi_m(double disp,double molength,double height){
-	double xm, psim;
-	
-	xm    = pow(1.0-16.0*((height-disp)/molength),0.25);
-	psim  = 2.0*log((1.0+xm)/2.0)+log((1+xm*xm)-2*atan(xm)+0.5*PI);
-	
-	return psim;
+double psi_m(double disp, double molength, double height)
+{
+    double xm, psim;
+
+    xm = pow(1.0 - 16.0 * ((height - disp) / molength), 0.25);
+    psim =
+	2.0 * log((1.0 + xm) / 2.0) + log((1 + xm * xm) - 2 * atan(xm) +
+					  0.5 * PI);
+
+    return psim;
 }

Modified: grass-addons/gipe/i.eb.rah/main.c
===================================================================
--- grass-addons/gipe/i.eb.rah/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.rah/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,283 +1,316 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.rah
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates aerodynamic resistance to heat transport
- *               This has been seen in Pawan (2004).
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double ra_h(double disp,double z0h,double psih,double ustar,double hu);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *input5, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_disp, infd_z0h, infd_psih, infd_ustar;
-	int infd_hu, outfd;
-	
-	char *disp, *z0h, *psih, *ustar, *hu;
-
-	double cp; /*air specific heat*/	
-	int i=0,j=0;
-	double a,b; /*SEBAL slope and intercepts of surf. temp.*/
-	
-	void *inrast_disp, *inrast_z0h, *inrast_psih;
-	void *inrast_hu, *inrast_ustar;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_disp;
-	RASTER_MAP_TYPE data_type_z0h;
-	RASTER_MAP_TYPE data_type_psih;
-	RASTER_MAP_TYPE data_type_ustar;
-	RASTER_MAP_TYPE data_type_hu;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("rah, energy balance, SEBAL");
-	module->description = _("aerodynamic resistance to heat transport as in Pawan (2004).");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("disp");
-	input1->description=_("Name of the displacement height map");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("z0h");
-	input2->description=_("Name of the height of heat flux roughness length map");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("psih");
-	input3->description=_("Name of the psichrometric parameter for heat flux map");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("ustar");
-	input4->description=_("Name of the nominal wind speed map");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("hu");
-	input5->description=_("Name of the height of wind measurement (typically 2 m) map");
-	
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output rah layer");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	disp	 	= input1->answer;
-	z0h	 	= input2->answer;
-	psih		= input3->answer;
-	ustar	 	= input4->answer;
-	hu	 	= input5->answer;
-	
-	result  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2 (disp, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), disp);
-	}
-	data_type_disp = G_raster_map_type(disp,mapset);
-	if ( (infd_disp = G_open_cell_old (disp,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), disp);
-	if (G_get_cellhd (disp, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), disp);
-	inrast_disp = G_allocate_raster_buf(data_type_disp);
-	/***************************************************/
-	mapset = G_find_cell2 (z0h, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), z0h);
-	}
-	data_type_z0h = G_raster_map_type(z0h,mapset);
-	if ( (infd_z0h = G_open_cell_old (z0h,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), z0h);
-	if (G_get_cellhd (z0h, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), z0h);
-	inrast_z0h = G_allocate_raster_buf(data_type_z0h);
-	/***************************************************/
-	mapset = G_find_cell2 (psih, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), psih);
-	}
-	data_type_psih = G_raster_map_type(psih,mapset);
-	if ( (infd_psih = G_open_cell_old (psih,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), psih);
-	if (G_get_cellhd (psih, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), psih);
-	inrast_psih = G_allocate_raster_buf(data_type_psih);
-	/***************************************************/
-	mapset = G_find_cell2 (ustar, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), ustar);
-	}
-	data_type_ustar = G_raster_map_type(ustar,mapset);
-	if ( (infd_ustar = G_open_cell_old (ustar,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ustar);
-	if (G_get_cellhd (ustar, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ustar);
-	inrast_ustar = G_allocate_raster_buf(data_type_ustar);
-	/***************************************************/
-	mapset = G_find_cell2 (hu, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), hu);
-	}
-	data_type_hu = G_raster_map_type(hu,mapset);
-	if ( (infd_ustar = G_open_cell_old (ustar,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), hu);
-	if (G_get_cellhd (hu, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), hu);
-	inrast_hu = G_allocate_raster_buf(data_type_hu);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_rah;
-		DCELL d_disp;
-		DCELL d_z0h;
-		DCELL d_psih;
-		DCELL d_ustar;
-		DCELL d_hu;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_disp,inrast_disp,row,data_type_disp)<0)
-			G_fatal_error(_("Could not read from <%s>"),disp);
-		if(G_get_raster_row(infd_z0h,inrast_z0h,row,data_type_z0h)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0h);
-		if(G_get_raster_row(infd_psih,inrast_psih,row,data_type_psih)<0)
-			G_fatal_error(_("Could not read from <%s>"),psih);
-		if(G_get_raster_row(infd_ustar,inrast_ustar,row,data_type_ustar)<0)
-			G_fatal_error(_("Could not read from <%s>"),ustar);
-		if(G_get_raster_row(infd_hu,inrast_hu,row,data_type_hu)<0)
-			G_fatal_error(_("Could not read from <%s>"),hu);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_disp = (double) ((CELL *) inrast_disp)[col];
-					break;
-				case FCELL_TYPE:
-					d_disp = (double) ((FCELL *) inrast_disp)[col];
-					break;
-				case DCELL_TYPE:
-					d_disp = (double) ((DCELL *) inrast_disp)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_z0h = (double) ((CELL *) inrast_z0h)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0h = (double) ((FCELL *) inrast_z0h)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0h = (double) ((DCELL *) inrast_z0h)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_psih = (double) ((CELL *) inrast_psih)[col];
-					break;
-				case FCELL_TYPE:
-					d_psih = (double) ((FCELL *) inrast_psih)[col];
-					break;
-				case DCELL_TYPE:
-					d_psih = (double) ((DCELL *) inrast_psih)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_ustar = (double) ((CELL *) inrast_ustar)[col];
-					break;
-				case FCELL_TYPE:
-					d_ustar = (double) ((FCELL *) inrast_ustar)[col];
-					break;
-				case DCELL_TYPE:
-					d_ustar = (double) ((DCELL *) inrast_ustar)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_hu = (double) ((CELL *) inrast_hu)[col];
-					break;
-				case FCELL_TYPE:
-					d_hu = (double) ((FCELL *) inrast_hu)[col];
-					break;
-				case DCELL_TYPE:
-					d_hu = (double) ((FCELL *) inrast_hu)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_disp)||
-			G_is_d_null_value(&d_z0h)||
-			G_is_d_null_value(&d_psih)||
-			G_is_d_null_value(&d_ustar)||
-			G_is_d_null_value(&d_hu)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate rah   */
-				d_rah=ra_h(d_disp,d_z0h,d_psih,d_ustar,d_hu);
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-	G_free (inrast_disp);
-	G_free (inrast_z0h);
-	G_free (inrast_psih);
-	G_free (inrast_ustar);
-	G_free (inrast_hu);
-
-	G_close_cell (infd_disp);
-	G_close_cell (infd_z0h);
-	G_close_cell (infd_psih);
-	G_close_cell (infd_ustar);
-	G_close_cell (infd_hu);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.rah
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates aerodynamic resistance to heat transport
+ *               This has been seen in Pawan (2004).
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double ra_h(double disp, double z0h, double psih, double ustar, double hu);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *input5, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_disp, infd_z0h, infd_psih, infd_ustar;
+
+    int infd_hu, outfd;
+
+    char *disp, *z0h, *psih, *ustar, *hu;
+
+    double cp;		/*air specific heat */
+
+    int i = 0, j = 0;
+
+    double a, b;		/*SEBAL slope and intercepts of surf. temp. */
+
+    void *inrast_disp, *inrast_z0h, *inrast_psih;
+
+    void *inrast_hu, *inrast_ustar;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_disp;
+    RASTER_MAP_TYPE data_type_z0h;
+    RASTER_MAP_TYPE data_type_psih;
+    RASTER_MAP_TYPE data_type_ustar;
+    RASTER_MAP_TYPE data_type_hu;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("rah, energy balance, SEBAL");
+    module->description =
+	_("aerodynamic resistance to heat transport as in Pawan (2004).");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("disp");
+    input1->description = _("Name of the displacement height map");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("z0h");
+    input2->description =
+	_("Name of the height of heat flux roughness length map");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("psih");
+    input3->description =
+	_("Name of the psichrometric parameter for heat flux map");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("ustar");
+    input4->description = _("Name of the nominal wind speed map");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("hu");
+    input5->description =
+	_("Name of the height of wind measurement (typically 2 m) map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output rah layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    disp = input1->answer;
+    z0h = input2->answer;
+    psih = input3->answer;
+    ustar = input4->answer;
+    hu = input5->answer;
+    result = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(disp, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), disp);
+    }
+    data_type_disp = G_raster_map_type(disp, mapset);
+    if ((infd_disp = G_open_cell_old(disp, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), disp);
+    if (G_get_cellhd(disp, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), disp);
+    inrast_disp = G_allocate_raster_buf(data_type_disp);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(z0h, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), z0h);
+    }
+    data_type_z0h = G_raster_map_type(z0h, mapset);
+    if ((infd_z0h = G_open_cell_old(z0h, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), z0h);
+    if (G_get_cellhd(z0h, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), z0h);
+    inrast_z0h = G_allocate_raster_buf(data_type_z0h);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(psih, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), psih);
+    }
+    data_type_psih = G_raster_map_type(psih, mapset);
+    if ((infd_psih = G_open_cell_old(psih, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), psih);
+    if (G_get_cellhd(psih, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), psih);
+    inrast_psih = G_allocate_raster_buf(data_type_psih);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ustar, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), ustar);
+    }
+    data_type_ustar = G_raster_map_type(ustar, mapset);
+    if ((infd_ustar = G_open_cell_old(ustar, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ustar);
+    if (G_get_cellhd(ustar, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ustar);
+    inrast_ustar = G_allocate_raster_buf(data_type_ustar);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(hu, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), hu);
+    }
+    data_type_hu = G_raster_map_type(hu, mapset);
+    if ((infd_ustar = G_open_cell_old(ustar, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), hu);
+    if (G_get_cellhd(hu, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), hu);
+    inrast_hu = G_allocate_raster_buf(data_type_hu);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_rah;
+	DCELL d_disp;
+	DCELL d_z0h;
+	DCELL d_psih;
+	DCELL d_ustar;
+	DCELL d_hu;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_disp, inrast_disp, row, data_type_disp)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), disp);
+	if (G_get_raster_row(infd_z0h, inrast_z0h, row, data_type_z0h) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0h);
+	if (G_get_raster_row(infd_psih, inrast_psih, row, data_type_psih) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), psih);
+	if (G_get_raster_row(infd_ustar, inrast_ustar, row, data_type_ustar)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ustar);
+	if (G_get_raster_row(infd_hu, inrast_hu, row, data_type_hu) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), hu);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_disp = (double)((CELL *) inrast_disp)[col];
+		break;
+	    case FCELL_TYPE:
+		d_disp = (double)((FCELL *) inrast_disp)[col];
+		break;
+	    case DCELL_TYPE:
+		d_disp = (double)((DCELL *) inrast_disp)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_z0h = (double)((CELL *) inrast_z0h)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0h = (double)((FCELL *) inrast_z0h)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0h = (double)((DCELL *) inrast_z0h)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_psih = (double)((CELL *) inrast_psih)[col];
+		break;
+	    case FCELL_TYPE:
+		d_psih = (double)((FCELL *) inrast_psih)[col];
+		break;
+	    case DCELL_TYPE:
+		d_psih = (double)((DCELL *) inrast_psih)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_ustar = (double)((CELL *) inrast_ustar)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ustar = (double)((FCELL *) inrast_ustar)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ustar = (double)((DCELL *) inrast_ustar)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_hu = (double)((CELL *) inrast_hu)[col];
+		break;
+	    case FCELL_TYPE:
+		d_hu = (double)((FCELL *) inrast_hu)[col];
+		break;
+	    case DCELL_TYPE:
+		d_hu = (double)((FCELL *) inrast_hu)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_disp) || G_is_d_null_value(&d_z0h) ||
+		 G_is_d_null_value(&d_psih) || G_is_d_null_value(&d_ustar)
+		 || G_is_d_null_value(&d_hu)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate rah   */ 
+		    d_rah = ra_h(d_disp, d_z0h, d_psih, d_ustar, d_hu);
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_disp);
+    G_free(inrast_z0h);
+    G_free(inrast_psih);
+    G_free(inrast_ustar);
+    G_free(inrast_hu);
+    G_close_cell(infd_disp);
+    G_close_cell(infd_z0h);
+    G_close_cell(infd_psih);
+    G_close_cell(infd_ustar);
+    G_close_cell(infd_hu);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.rah/rah.c
===================================================================
--- grass-addons/gipe/i.eb.rah/rah.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.rah/rah.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,10 +3,11 @@
 #include<math.h>
 
 
-double ra_h(double disp,double z0h,double psih,double ustar,double hu){
-	double rah;
-	
-	rah   = (log((hu-disp)/z0h)-psih)/(0.41*ustar);
+double ra_h(double disp, double z0h, double psih, double ustar, double hu)
+{
+    double rah;
 
-	return rah;
+    rah = (log((hu - disp) / z0h) - psih) / (0.41 * ustar);
+
+    return rah;
 }

Modified: grass-addons/gipe/i.eb.rohair/main.c
===================================================================
--- grass-addons/gipe/i.eb.rohair/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.rohair/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,175 +1,200 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.rohair
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the standard air density as seen in Pawan (2004)
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double roh_air(double dem, double tempka);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *output1;
-	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_dem, infd_tempka;
-	int outfd1;
-	
-	char *rohair;
-	char *dem, *tempka;	
-	int i=0,j=0;
-	
-	void *inrast_dem, *inrast_tempka;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_dem;
-	RASTER_MAP_TYPE data_type_tempka;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("rohair, energy balance, SEBAL");
-	module->description = _("Standard height-based Air Density as seen in Pawan (2004).");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("dem");
-	input1->description=_("Name of the DEM map [m]");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("tempka");
-	input2->description=_("Name of the Air Temperature map [K]");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("rohair");
-	output1->description=_("Name of the output rohair layer");
-	
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	dem	 	= input1->answer;
-	tempka	 	= input2->answer;
-		
-	result1  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(dem, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), dem);
-	}
-	data_type_dem = G_raster_map_type(dem,mapset);
-	if ( (infd_dem = G_open_cell_old (dem,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), dem);
-	if (G_get_cellhd (dem, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), dem);
-	inrast_dem = G_allocate_raster_buf(data_type_dem);
-	/***************************************************/
-	/***************************************************/
-	mapset = G_find_cell2(tempka, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), tempka);
-	}
-	data_type_tempka = G_raster_map_type(tempka,mapset);
-	if ( (infd_tempka = G_open_cell_old (tempka,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), tempka);
-	if (G_get_cellhd (tempka, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), tempka);
-	inrast_tempka = G_allocate_raster_buf(data_type_tempka);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_dem;
-		DCELL d_tempka;
-		/* read input maps */	
-		if(G_get_raster_row(infd_dem,inrast_dem,row,data_type_dem)<0)
-			G_fatal_error(_("Could not read from <%s>"),dem);
-		if(G_get_raster_row(infd_tempka,inrast_tempka,row,data_type_tempka)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempka);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_dem){
-				case CELL_TYPE:
-					d_dem = (double) ((CELL *) inrast_dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_dem = (double) ((FCELL *) inrast_dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_dem = ((DCELL *) inrast_dem)[col];
-					break;
-			}
-			switch(data_type_tempka){
-				case CELL_TYPE:
-					d_tempka = (double) ((CELL *) inrast_tempka)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempka = (double) ((FCELL *) inrast_tempka)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempka = ((DCELL *) inrast_tempka)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_dem)||
-			G_is_d_null_value(&d_tempka)){
-				G_set_d_null_value(&outrast1[col],1);
-			} else {
-				/********************/
-				/* calculate rohair */
-				d = roh_air(d_dem, d_tempka);
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_dem);
-	G_free (inrast_tempka);
-	G_close_cell (infd_dem);
-	G_close_cell (infd_tempka);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.rohair
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the standard air density as seen in Pawan (2004)
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double roh_air(double dem, double tempka);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *output1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_dem, infd_tempka;
+
+    int outfd1;
+
+    char *rohair;
+
+    char *dem, *tempka;
+
+    int i = 0, j = 0;
+
+    void *inrast_dem, *inrast_tempka;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_dem;
+    RASTER_MAP_TYPE data_type_tempka;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("rohair, energy balance, SEBAL");
+    module->description =
+	_("Standard height-based Air Density as seen in Pawan (2004).");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("dem");
+    input1->description = _("Name of the DEM map [m]");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("tempka");
+    input2->description = _("Name of the Air Temperature map [K]");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("rohair");
+    output1->description = _("Name of the output rohair layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    dem = input1->answer;
+    tempka = input2->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(dem, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), dem);
+    }
+    data_type_dem = G_raster_map_type(dem, mapset);
+    if ((infd_dem = G_open_cell_old(dem, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), dem);
+    if (G_get_cellhd(dem, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), dem);
+    inrast_dem = G_allocate_raster_buf(data_type_dem);
+    
+
+	/***************************************************/ 
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempka, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), tempka);
+    }
+    data_type_tempka = G_raster_map_type(tempka, mapset);
+    if ((infd_tempka = G_open_cell_old(tempka, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempka);
+    if (G_get_cellhd(tempka, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), tempka);
+    inrast_tempka = G_allocate_raster_buf(data_type_tempka);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_dem;
+	DCELL d_tempka;
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_dem, inrast_dem, row, data_type_dem) <
+		0)
+	    G_fatal_error(_("Could not read from <%s>"), dem);
+	if (G_get_raster_row
+	     (infd_tempka, inrast_tempka, row, data_type_tempka) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempka);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_dem) {
+	    case CELL_TYPE:
+		d_dem = (double)((CELL *) inrast_dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dem = (double)((FCELL *) inrast_dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dem = ((DCELL *) inrast_dem)[col];
+		break;
+	    }
+	    switch (data_type_tempka) {
+	    case CELL_TYPE:
+		d_tempka = (double)((CELL *) inrast_tempka)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempka = (double)((FCELL *) inrast_tempka)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempka = ((DCELL *) inrast_tempka)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_dem) || G_is_d_null_value(&d_tempka)) {
+		G_set_d_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		
+
+				/********************/ 
+		    /* calculate rohair */ 
+		    d = roh_air(d_dem, d_tempka);
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_dem);
+    G_free(inrast_tempka);
+    G_close_cell(infd_dem);
+    G_close_cell(infd_tempka);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.rohair/roh_air.c
===================================================================
--- grass-addons/gipe/i.eb.rohair/roh_air.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.rohair/roh_air.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -8,17 +8,18 @@
 
 double roh_air(double dem, double tempka)
 {
-	double b, result;
+    double b, result;
 
-	b = (( tempka - (0.00627 * dem )) / tempka );
-	
-	result = 349.467 * pow( b , 5.26 ) / tempka ;
+    b = ((tempka - (0.00627 * dem)) / tempka);
 
-	if (result > 1.5){
-		result = -999.99;
-	} else if (result < 0.0){
-		result = -999.99;
-	}
+    result = 349.467 * pow(b, 5.26) / tempka;
 
-	return result;
+    if (result > 1.5) {
+	result = -999.99;
+    }
+    else if (result < 0.0) {
+	result = -999.99;
+    }
+
+    return result;
 }

Modified: grass-addons/gipe/i.eb.ublend/main.c
===================================================================
--- grass-addons/gipe/i.eb.ublend/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.ublend/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,230 +1,258 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.ublend
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the wind speed at blendiong height
- * 		 as seen in Pawan (2004)
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double u_blend(double u_hmoment, double disp,double hblend,double z0m, double hmoment);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *input5, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_u_hmoment,infd_disp,infd_z0m;
-	int outfd;
-	
-	char *u_hmoment, *disp, *z0m;
-
-	double hblend, hmoment;
-	int i=0,j=0;
-	
-	void *inrast_u_hmoment, *inrast_disp, *inrast_z0m;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_u_hmoment;
-	RASTER_MAP_TYPE data_type_disp;
-	RASTER_MAP_TYPE data_type_z0m;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("ublend, energy balance, SEBAL");
-	module->description = _("Wind speed at blending height.");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("u_hm");
-	input1->description=_("Name of the wind speed at momentum height map (2.0m height in Pawan, 2004)");
-
-	input2 = G_define_option() ;
-	input2->key        =_("hmoment");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = YES;
-	input2->gisprompt  =_("Value, parameter");
-	input2->description=_("Value of the momentum height (2.0 in Pawan, 2004)");
-	input2->answer     =_("2.0");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("disp");
-	input3->description=_("Name of the displacement height map");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("z0m");
-	input4->description=_("Name of the surface roughness for momentum map");
-	
-	input5 = G_define_option() ;
-	input5->key        =_("hblend");
-	input5->type       = TYPE_DOUBLE;
-	input5->required   = YES;
-	input5->gisprompt  =_("Value, parameter");
-	input5->description=_("Value of the blending height (100.0 in Pawan, 2004)");
-	input5->answer     =_("100.0");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output ublend layer");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	u_hmoment 	= input1->answer;
-	hmoment	 	= atof(input2->answer);
-	disp	 	= input3->answer;
-	z0m	 	= input4->answer;
-	hblend		= atof(input5->answer);
-	
-	result  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2 (u_hmoment, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), u_hmoment);
-	}
-	data_type_u_hmoment = G_raster_map_type(u_hmoment,mapset);
-	if ( (infd_u_hmoment = G_open_cell_old (u_hmoment,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), u_hmoment);
-	if (G_get_cellhd (u_hmoment, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), u_hmoment);
-	inrast_u_hmoment = G_allocate_raster_buf(data_type_u_hmoment);
-	/***************************************************/
-	mapset = G_find_cell2 (disp, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), disp);
-	}
-	data_type_disp = G_raster_map_type(disp,mapset);
-	if ( (infd_disp = G_open_cell_old (disp,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), disp);
-	if (G_get_cellhd (disp, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), disp);
-	inrast_disp = G_allocate_raster_buf(data_type_disp);
-	/***************************************************/
-	mapset = G_find_cell2 (z0m, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), z0m);
-	}
-	data_type_z0m = G_raster_map_type(z0m,mapset);
-	if ( (infd_z0m = G_open_cell_old (z0m,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), z0m);
-	if (G_get_cellhd (z0m, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), z0m);
-	inrast_z0m = G_allocate_raster_buf(data_type_z0m);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_disp;
-		DCELL d_z0m;
-		DCELL d_u_hmoment;
-		DCELL d_ublend;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_u_hmoment,inrast_u_hmoment,row,data_type_u_hmoment)<0)
-			G_fatal_error(_("Could not read from <%s>"),u_hmoment);
-		if(G_get_raster_row(infd_disp,inrast_disp,row,data_type_disp)<0)
-			G_fatal_error(_("Could not read from <%s>"),disp);
-		if(G_get_raster_row(infd_z0m,inrast_z0m,row,data_type_z0m)<0)
-			G_fatal_error(_("Could not read from <%s>"),z0m);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_u_hmoment){
-				case CELL_TYPE:
-					d_u_hmoment = (double) ((CELL *) inrast_u_hmoment)[col];
-					break;
-				case FCELL_TYPE:
-					d_u_hmoment = (double) ((FCELL *) inrast_u_hmoment)[col];
-					break;
-				case DCELL_TYPE:
-					d_u_hmoment = ((DCELL *) inrast_u_hmoment)[col];
-					break;
-			}
-			switch(data_type_disp){
-				case CELL_TYPE:
-					d_disp = (double) ((CELL *) inrast_disp)[col];
-					break;
-				case FCELL_TYPE:
-					d_disp = (double) ((FCELL *) inrast_disp)[col];
-					break;
-				case DCELL_TYPE:
-					d_disp = ((DCELL *) inrast_disp)[col];
-					break;
-			}
-			switch(data_type_z0m){
-				case CELL_TYPE:
-					d_z0m = (double) ((CELL *) inrast_z0m)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0m = (double) ((FCELL *) inrast_z0m)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0m = ((DCELL *) inrast_z0m)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_disp)||
-			G_is_d_null_value(&d_z0m)||
-			G_is_d_null_value(&d_u_hmoment)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* calculate ustar   */
-				d_ublend=u_blend(d_u_hmoment, d_disp, hblend,d_z0m, d_u_hmoment);
-				outrast[col] = d_ublend;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-	G_free (inrast_u_hmoment);
-	G_free (inrast_disp);
-	G_free (inrast_z0m);
-
-	G_close_cell (infd_u_hmoment);
-	G_close_cell (infd_disp);
-	G_close_cell (infd_z0m);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.ublend
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the wind speed at blendiong height
+ * 		 as seen in Pawan (2004)
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double u_blend(double u_hmoment, double disp, double hblend, double z0m,
+		double hmoment);
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *input5, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_u_hmoment, infd_disp, infd_z0m;
+
+    int outfd;
+
+    char *u_hmoment, *disp, *z0m;
+
+    double hblend, hmoment;
+
+    int i = 0, j = 0;
+
+    void *inrast_u_hmoment, *inrast_disp, *inrast_z0m;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_u_hmoment;
+    RASTER_MAP_TYPE data_type_disp;
+    RASTER_MAP_TYPE data_type_z0m;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("ublend, energy balance, SEBAL");
+    module->description = _("Wind speed at blending height.");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("u_hm");
+    input1->description =
+	_("Name of the wind speed at momentum height map (2.0m height in Pawan, 2004)");
+    input2 = G_define_option();
+    input2->key = _("hmoment");
+    input2->type = TYPE_DOUBLE;
+    input2->required = YES;
+    input2->gisprompt = _("Value, parameter");
+    input2->description =
+	_("Value of the momentum height (2.0 in Pawan, 2004)");
+    input2->answer = _("2.0");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("disp");
+    input3->description = _("Name of the displacement height map");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("z0m");
+    input4->description =
+	_("Name of the surface roughness for momentum map");
+    input5 = G_define_option();
+    input5->key = _("hblend");
+    input5->type = TYPE_DOUBLE;
+    input5->required = YES;
+    input5->gisprompt = _("Value, parameter");
+    input5->description =
+	_("Value of the blending height (100.0 in Pawan, 2004)");
+    input5->answer = _("100.0");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output ublend layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    u_hmoment = input1->answer;
+    hmoment = atof(input2->answer);
+    disp = input3->answer;
+    z0m = input4->answer;
+    hblend = atof(input5->answer);
+    result = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(u_hmoment, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), u_hmoment);
+    }
+    data_type_u_hmoment = G_raster_map_type(u_hmoment, mapset);
+    if ((infd_u_hmoment = G_open_cell_old(u_hmoment, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), u_hmoment);
+    if (G_get_cellhd(u_hmoment, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), u_hmoment);
+    inrast_u_hmoment = G_allocate_raster_buf(data_type_u_hmoment);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(disp, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), disp);
+    }
+    data_type_disp = G_raster_map_type(disp, mapset);
+    if ((infd_disp = G_open_cell_old(disp, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), disp);
+    if (G_get_cellhd(disp, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), disp);
+    inrast_disp = G_allocate_raster_buf(data_type_disp);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(z0m, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), z0m);
+    }
+    data_type_z0m = G_raster_map_type(z0m, mapset);
+    if ((infd_z0m = G_open_cell_old(z0m, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), z0m);
+    if (G_get_cellhd(z0m, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), z0m);
+    inrast_z0m = G_allocate_raster_buf(data_type_z0m);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_disp;
+	DCELL d_z0m;
+	DCELL d_u_hmoment;
+	DCELL d_ublend;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_u_hmoment, inrast_u_hmoment, row,
+		 data_type_u_hmoment) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), u_hmoment);
+	if (G_get_raster_row(infd_disp, inrast_disp, row, data_type_disp) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), disp);
+	if (G_get_raster_row(infd_z0m, inrast_z0m, row, data_type_z0m) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), z0m);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_u_hmoment) {
+	    case CELL_TYPE:
+		d_u_hmoment = (double)((CELL *) inrast_u_hmoment)[col];
+		break;
+	    case FCELL_TYPE:
+		d_u_hmoment = (double)((FCELL *) inrast_u_hmoment)[col];
+		break;
+	    case DCELL_TYPE:
+		d_u_hmoment = ((DCELL *) inrast_u_hmoment)[col];
+		break;
+	    }
+	    switch (data_type_disp) {
+	    case CELL_TYPE:
+		d_disp = (double)((CELL *) inrast_disp)[col];
+		break;
+	    case FCELL_TYPE:
+		d_disp = (double)((FCELL *) inrast_disp)[col];
+		break;
+	    case DCELL_TYPE:
+		d_disp = ((DCELL *) inrast_disp)[col];
+		break;
+	    }
+	    switch (data_type_z0m) {
+	    case CELL_TYPE:
+		d_z0m = (double)((CELL *) inrast_z0m)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0m = (double)((FCELL *) inrast_z0m)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0m = ((DCELL *) inrast_z0m)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_disp) || G_is_d_null_value(&d_z0m) ||
+		 G_is_d_null_value(&d_u_hmoment)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate ustar   */ 
+		    d_ublend =
+		    u_blend(d_u_hmoment, d_disp, hblend, d_z0m, d_u_hmoment);
+		outrast[col] = d_ublend;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_u_hmoment);
+    G_free(inrast_disp);
+    G_free(inrast_z0m);
+    G_close_cell(infd_u_hmoment);
+    G_close_cell(infd_disp);
+    G_close_cell(infd_z0m);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.ublend/ublend.c
===================================================================
--- grass-addons/gipe/i.eb.ublend/ublend.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.ublend/ublend.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,12 +2,16 @@
 #include<stdlib.h>
 #include<math.h>
 
-/*Wind speed at blending height*/
+/*Wind speed at blending height */
 
-double u_blend(double u_hmoment, double disp,double hblend,double z0m, double hmoment){
-	double ublend;
-	
-	ublend=u_hmoment*(log(hblend-disp)-log(z0m))/(log(hmoment-disp)-log(z0m));
+double u_blend(double u_hmoment, double disp, double hblend, double z0m,
+	       double hmoment)
+{
+    double ublend;
 
-	return ublend;
+    ublend =
+	u_hmoment * (log(hblend - disp) - log(z0m)) / (log(hmoment - disp) -
+						       log(z0m));
+
+    return ublend;
 }

Modified: grass-addons/gipe/i.eb.ublend/ustar.c
===================================================================
--- grass-addons/gipe/i.eb.ublend/ustar.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.ublend/ustar.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -3,10 +3,12 @@
 #include<math.h>
 
 
-double u_star(double ublend,double hblend,double disp,double z0m,double psim){
-	double ustar;
-	
-	ustar = 0.41*ublend/(log((hblend-disp)/z0m)-psim);
+double u_star(double ublend, double hblend, double disp, double z0m,
+	      double psim)
+{
+    double ustar;
 
-	return ustar;
+    ustar = 0.41 * ublend / (log((hblend - disp) / z0m) - psim);
+
+    return ustar;
 }

Modified: grass-addons/gipe/i.eb.wetdrypix/main.c
===================================================================
--- grass-addons/gipe/i.eb.wetdrypix/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.wetdrypix/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.eb.wetdrypix
@@ -27,584 +28,637 @@
 #include <grass/glocale.h>
 
 int main(int argc, char *argv[])
-{	
-	struct Cell_head cellhd;
-	
-	/* buffer for in out raster */
-	DCELL *inrast_T,*inrast_alb;
-	DCELL *inrast_DEM,*inrast_Rn,*inrast_g0;
-	CELL *outrast,*outrast1;
+{
+    struct Cell_head cellhd;
 
-	int nrows, ncols;
-	int row, col;
-	int infd_T,infd_alb,infd_DEM,infd_Rn,infd_g0;
-	int outfd, outfd1;
-	
-	char *mapset_T,*mapset_alb,*mapset_DEM,*mapset_Rn,*mapset_g0;
-	char *T, *alb, *DEM, *Rn, *g0; 
-	char *wetF, *dryF;
+    /* buffer for in out raster */
+    DCELL *inrast_T, *inrast_alb;
 
-        struct History history;
-	struct GModule *module;
-	struct Option *input_T, *input_alb;
-	struct Option *input_DEM, *input_Rn, *input_g0;
-	struct Option *output, *output1;
+    DCELL *inrast_DEM, *inrast_Rn, *inrast_g0;
+
+    CELL *outrast, *outrast1;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int infd_T, infd_alb, infd_DEM, infd_Rn, infd_g0;
+
+    int outfd, outfd1;
+
+    char *mapset_T, *mapset_alb, *mapset_DEM, *mapset_Rn, *mapset_g0;
+
+    char *T, *alb, *DEM, *Rn, *g0;
+
+    char *wetF, *dryF;
+
+    struct History history;
+
+    struct GModule *module;
+
+    struct Option *input_T, *input_alb;
+
+    struct Option *input_DEM, *input_Rn, *input_g0;
+
+    struct Option *output, *output1;
+
 	/*******************************/
-	RASTER_MAP_TYPE data_type_T;
-	RASTER_MAP_TYPE data_type_DEM;
-	RASTER_MAP_TYPE data_type_Rn;
-	RASTER_MAP_TYPE data_type_g0;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_output=CELL_TYPE;
+    RASTER_MAP_TYPE data_type_T;
+
+    RASTER_MAP_TYPE data_type_DEM;
+
+    RASTER_MAP_TYPE data_type_Rn;
+
+    RASTER_MAP_TYPE data_type_g0;
+
+    RASTER_MAP_TYPE data_type_albedo;
+
+    RASTER_MAP_TYPE data_type_output = CELL_TYPE;
+
 	/*******************************/
-	
-	struct Flag *flag1, *zero;
-	
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description = _("Wet and Dry pixels candidates for SEBAL 95");
-	
-	/* Define different options */
-	input_T = G_define_standard_option(G_OPT_R_INPUT);
-	input_T->key	= "T";
-	input_T->description = _("Name of Surface Skin Temperature input map [K]");
-		
-	input_alb = G_define_standard_option(G_OPT_R_INPUT);
-	input_alb->key	= "alb";
-	input_alb->description = _("Name of Broadband Albedo input map [-]");
-		
-	input_DEM = G_define_standard_option(G_OPT_R_INPUT);
-	input_DEM->key	= "DEM";
-	input_DEM->description = _("Name of DEM input map [m a.s.l.]");
-	
-	input_Rn = G_define_standard_option(G_OPT_R_INPUT);
-	input_Rn->key	= "Rn";
-	input_Rn->description = _("Name of Diurnal Net Solar Radiation input map [W/m2]");
-	
-	input_g0 = G_define_standard_option(G_OPT_R_INPUT);
-	input_g0->key	= "g0";
-	input_g0->description = _("Name of Soil Heat Flux input map [W/m2]");	
-	
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key        = "wet";
-	output->description= _("Name of output wet pixels areas layer [-]");
-	
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        = "dry";
-	output1->description= _("Name of output dry pixels areas layer [-]");
-	
-	zero = G_define_flag() ;
-	zero->key         = 'z' ;
-	zero->description = _("set negative to zero");
-	
-	if (G_parser(argc, argv))
-	  exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	T=input_T->answer;
-	alb=input_alb->answer;
-	DEM=input_DEM->answer;
-	Rn=input_Rn->answer;
-	g0=input_g0->answer;
-	
-	wetF=output->answer;
-	dryF=output1->answer;
-	
-	/* find maps in mapset */
-	mapset_T = G_find_cell2 (T, "");
-	if (mapset_T == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), T);
-	mapset_alb = G_find_cell2 (alb, "");
-	if (mapset_alb == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), alb);
-	mapset_DEM = G_find_cell2 (DEM, "");
-	if (mapset_DEM == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), DEM);
-	mapset_Rn = G_find_cell2 (Rn, "");
-	if (mapset_Rn == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), Rn);
-	mapset_g0 = G_find_cell2 (g0, "");
-	if (mapset_g0 == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), g0);
 
-	/* check legal output name */ 
-	if (G_legal_filename (wetF) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), wetF);
-	if (G_legal_filename (dryF) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), dryF);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_T 	= G_raster_map_type(T, mapset_T);
-	data_type_DEM 	= G_raster_map_type(DEM, mapset_DEM);
-	data_type_Rn 	= G_raster_map_type(Rn, mapset_Rn);
-	data_type_g0 	= G_raster_map_type(g0, mapset_g0);
-	data_type_albedo = G_raster_map_type(alb, mapset_alb);
+    struct Flag *flag1, *zero;
 
-	if ( (infd_T = G_open_cell_old (T, mapset_T)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), T);
-	if ( (infd_alb = G_open_cell_old (alb, mapset_alb)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),alb);
-	if ( (infd_DEM = G_open_cell_old (DEM, mapset_DEM)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),DEM);
-	if ( (infd_Rn = G_open_cell_old (Rn, mapset_Rn)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),Rn);
-	if ( (infd_g0 = G_open_cell_old (g0, mapset_g0)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),g0);
-	
-	if (G_get_cellhd (T, mapset_T, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), T);
-	if (G_get_cellhd (alb, mapset_alb, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), alb);
-	if (G_get_cellhd (DEM, mapset_DEM, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), DEM);
-	if (G_get_cellhd (Rn, mapset_Rn, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), Rn);
-	if (G_get_cellhd (g0, mapset_g0, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), g0);
+    G_gisinit(argv[0]);
 
-	/* Allocate input buffer */
-	inrast_T  	= G_allocate_raster_buf(data_type_T);
-	inrast_DEM 	= G_allocate_raster_buf(data_type_DEM);
-	inrast_Rn 	= G_allocate_raster_buf(data_type_Rn);
-	inrast_g0 	= G_allocate_raster_buf(data_type_g0);
-	inrast_alb 	= G_allocate_raster_buf(data_type_albedo);
-	
-	/* Allocate output buffer */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	outrast1 = G_allocate_raster_buf(data_type_output);
+    module = G_define_module();
+    module->description = _("Wet and Dry pixels candidates for SEBAL 95");
 
-	if ( (outfd = G_open_raster_new (wetF,data_type_output)) < 0)
-		G_fatal_error (_("Could not open <%s>"),wetF);
-	if ( (outfd1 = G_open_raster_new (dryF,data_type_output)) < 0)
-		G_fatal_error (_("Could not open <%s>"),dryF);
+    /* Define different options */
+    input_T = G_define_standard_option(G_OPT_R_INPUT);
+    input_T->key = "T";
+    input_T->description =
+	_("Name of Surface Skin Temperature input map [K]");
 
-	/*START Temperature minimum search */
-	/* THREAD 1 */
-	/*This is correcting for un-Earthly temperatures*/
-	/*It finds when histogram is actually starting to pull up...*/
-	int peak1, peak2, peak3;
-	int i_peak1, i_peak2, i_peak3;
-	int bottom1a, bottom1b;
-	int bottom2a, bottom2b;
-	int bottom3a, bottom3b;
-	int i_bottom1a, i_bottom1b;
-	int i_bottom2a, i_bottom2b;
-	int i_bottom3a, i_bottom3b;
-	int i=0;
-	int histogram[400];
-	for (i=0;i<400;i++){
-		histogram[i]=0;
-	}
-	DCELL d_T;
+    input_alb = G_define_standard_option(G_OPT_R_INPUT);
+    input_alb->key = "alb";
+    input_alb->description = _("Name of Broadband Albedo input map [-]");
+
+    input_DEM = G_define_standard_option(G_OPT_R_INPUT);
+    input_DEM->key = "DEM";
+    input_DEM->description = _("Name of DEM input map [m a.s.l.]");
+
+    input_Rn = G_define_standard_option(G_OPT_R_INPUT);
+    input_Rn->key = "Rn";
+    input_Rn->description =
+	_("Name of Diurnal Net Solar Radiation input map [W/m2]");
+
+    input_g0 = G_define_standard_option(G_OPT_R_INPUT);
+    input_g0->key = "g0";
+    input_g0->description = _("Name of Soil Heat Flux input map [W/m2]");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key = "wet";
+    output->description = _("Name of output wet pixels areas layer [-]");
+
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = "dry";
+    output1->description = _("Name of output dry pixels areas layer [-]");
+
+    zero = G_define_flag();
+    zero->key = 'z';
+    zero->description = _("set negative to zero");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    T = input_T->answer;
+    alb = input_alb->answer;
+    DEM = input_DEM->answer;
+    Rn = input_Rn->answer;
+    g0 = input_g0->answer;
+
+    wetF = output->answer;
+    dryF = output1->answer;
+
+    /* find maps in mapset */
+    mapset_T = G_find_cell2(T, "");
+    if (mapset_T == NULL)
+	G_fatal_error(_("cell file [%s] not found"), T);
+    mapset_alb = G_find_cell2(alb, "");
+    if (mapset_alb == NULL)
+	G_fatal_error(_("cell file [%s] not found"), alb);
+    mapset_DEM = G_find_cell2(DEM, "");
+    if (mapset_DEM == NULL)
+	G_fatal_error(_("cell file [%s] not found"), DEM);
+    mapset_Rn = G_find_cell2(Rn, "");
+    if (mapset_Rn == NULL)
+	G_fatal_error(_("cell file [%s] not found"), Rn);
+    mapset_g0 = G_find_cell2(g0, "");
+    if (mapset_g0 == NULL)
+	G_fatal_error(_("cell file [%s] not found"), g0);
+
+    /* check legal output name */
+    if (G_legal_filename(wetF) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), wetF);
+    if (G_legal_filename(dryF) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), dryF);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_T = G_raster_map_type(T, mapset_T);
+    data_type_DEM = G_raster_map_type(DEM, mapset_DEM);
+    data_type_Rn = G_raster_map_type(Rn, mapset_Rn);
+    data_type_g0 = G_raster_map_type(g0, mapset_g0);
+    data_type_albedo = G_raster_map_type(alb, mapset_alb);
+
+    if ((infd_T = G_open_cell_old(T, mapset_T)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), T);
+    if ((infd_alb = G_open_cell_old(alb, mapset_alb)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), alb);
+    if ((infd_DEM = G_open_cell_old(DEM, mapset_DEM)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), DEM);
+    if ((infd_Rn = G_open_cell_old(Rn, mapset_Rn)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), Rn);
+    if ((infd_g0 = G_open_cell_old(g0, mapset_g0)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), g0);
+
+    if (G_get_cellhd(T, mapset_T, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), T);
+    if (G_get_cellhd(alb, mapset_alb, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), alb);
+    if (G_get_cellhd(DEM, mapset_DEM, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), DEM);
+    if (G_get_cellhd(Rn, mapset_Rn, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), Rn);
+    if (G_get_cellhd(g0, mapset_g0, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), g0);
+
+    /* Allocate input buffer */
+    inrast_T = G_allocate_raster_buf(data_type_T);
+    inrast_DEM = G_allocate_raster_buf(data_type_DEM);
+    inrast_Rn = G_allocate_raster_buf(data_type_Rn);
+    inrast_g0 = G_allocate_raster_buf(data_type_g0);
+    inrast_alb = G_allocate_raster_buf(data_type_albedo);
+
+    /* Allocate output buffer */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    outrast1 = G_allocate_raster_buf(data_type_output);
+
+    if ((outfd = G_open_raster_new(wetF, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), wetF);
+    if ((outfd1 = G_open_raster_new(dryF, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), dryF);
+
+    /*START Temperature minimum search */
+    /* THREAD 1 */
+    /*This is correcting for un-Earthly temperatures */
+    /*It finds when histogram is actually starting to pull up... */
+    int peak1, peak2, peak3;
+
+    int i_peak1, i_peak2, i_peak3;
+
+    int bottom1a, bottom1b;
+
+    int bottom2a, bottom2b;
+
+    int bottom3a, bottom3b;
+
+    int i_bottom1a, i_bottom1b;
+
+    int i_bottom2a, i_bottom2b;
+
+    int i_bottom3a, i_bottom3b;
+
+    int i = 0;
+
+    int histogram[400];
+
+    for (i = 0; i < 400; i++) {
+	histogram[i] = 0;
+    }
+    DCELL d_T;
+
 	/****************************/
-	/* Process pixels histogram */
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		/* read input map */
-		if( (G_get_raster_row(infd_T,inrast_T,row, data_type_T)) < 0){
-			G_fatal_error (_("Could not read from <%s>"),T);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_T)
-			{
-				case CELL_TYPE:
-					d_T = (double) ((CELL *) inrast_T)[col];
-					break;
-				case FCELL_TYPE:
-					d_T = (double) ((FCELL *) inrast_T)[col];
-					break;
-				case DCELL_TYPE:
-					d_T = (double) ((DCELL *) inrast_T)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_T)){
-				/*Do nothing*/
-			} else {
-				int temp;
-				temp		= (int) d_T;
-				if(temp>0){
-					histogram[temp]=histogram[temp]+1.0;
-				}
-			}
-		}
+    /* Process pixels histogram */
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read input map */
+	if ((G_get_raster_row(infd_T, inrast_T, row, data_type_T)) < 0) {
+	    G_fatal_error(_("Could not read from <%s>"), T);
 	}
-	G_message("Histogram of Temperature map (if it has rogue values to clean)");
-	peak1=0;
-	peak2=0;
-	peak3=0;
-	i_peak1=0;
-	i_peak2=0;
-	i_peak3=0;
-	bottom1a=100000;
-	bottom1b=100000;
-	bottom2a=100000;
-	bottom2b=100000;
-	bottom3a=100000;
-	bottom3b=100000;
-	i_bottom1a=1000;
-	i_bottom1b=1000;
-	i_bottom2a=1000;
-	i_bottom2b=1000;
-	i_bottom3a=1000;
-	i_bottom3b=1000;
-	for(i=0;i<400;i++){
-		/* Search for highest peak of dataset (2) */
-		/* Highest Peak */
-		if(histogram[i]>peak2){
-			peak2 = histogram[i];
-			i_peak2=i;
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_T) {
+	    case CELL_TYPE:
+		d_T = (double)((CELL *) inrast_T)[col];
+		break;
+	    case FCELL_TYPE:
+		d_T = (double)((FCELL *) inrast_T)[col];
+		break;
+	    case DCELL_TYPE:
+		d_T = (double)((DCELL *) inrast_T)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_T)) {
+		/*Do nothing */
+	    }
+	    else {
+		int temp;
+
+		temp = (int)d_T;
+		if (temp > 0) {
+		    histogram[temp] = histogram[temp] + 1.0;
 		}
+	    }
 	}
-	int stop=0;
-	for(i=i_peak2;i>5;i--){
-		if(((histogram[i]+histogram[i-1]+histogram[i-2]+histogram[i-3]+histogram[i-4])/5)<histogram[i]&&stop==0){
-			bottom2a = histogram[i];
-			i_bottom2a = i;
-		} else if(((histogram[i]+histogram[i-1]+histogram[i-2]+histogram[i-3]+histogram[i-4])/5)>histogram[i]&&stop==0){
-			/*Search for peaks of datasets (1)*/
-			peak1 = histogram[i];
-			i_peak1=i;
-			stop=1;
-		}	
+    }
+    G_message
+	("Histogram of Temperature map (if it has rogue values to clean)");
+    peak1 = 0;
+    peak2 = 0;
+    peak3 = 0;
+    i_peak1 = 0;
+    i_peak2 = 0;
+    i_peak3 = 0;
+    bottom1a = 100000;
+    bottom1b = 100000;
+    bottom2a = 100000;
+    bottom2b = 100000;
+    bottom3a = 100000;
+    bottom3b = 100000;
+    i_bottom1a = 1000;
+    i_bottom1b = 1000;
+    i_bottom2a = 1000;
+    i_bottom2b = 1000;
+    i_bottom3a = 1000;
+    i_bottom3b = 1000;
+    for (i = 0; i < 400; i++) {
+	/* Search for highest peak of dataset (2) */
+	/* Highest Peak */
+	if (histogram[i] > peak2) {
+	    peak2 = histogram[i];
+	    i_peak2 = i;
 	}
-	stop=0;
-	for(i=i_peak2;i<395;i++){
-		if(((histogram[i]+histogram[i+1]+histogram[i+2]+histogram[i+3]+histogram[i+4])/5)<histogram[i]&&stop==0){
-			bottom2b = histogram[i];
-			i_bottom2b = i;
-		} else if(((histogram[i]+histogram[i+1]+histogram[i+2]+histogram[i+3]+histogram[i+4])/5)>histogram[i]&&stop==0){
-			/*Search for peaks of datasets (3)*/
-			peak3 = histogram[i];
-			i_peak3=i;
-			stop=1;
-		}	
+    }
+    int stop = 0;
+
+    for (i = i_peak2; i > 5; i--) {
+	if (((histogram[i] + histogram[i - 1] + histogram[i - 2] +
+	      histogram[i - 3] + histogram[i - 4]) / 5) < histogram[i] &&
+	    stop == 0) {
+	    bottom2a = histogram[i];
+	    i_bottom2a = i;
 	}
-	/* First histogram lower bound */
-	for(i=250;i<i_peak1;i++){
-		if(histogram[i]<bottom1a){
-			bottom1a = histogram[i];
-			i_bottom1a = i;
-		}
+	else if (((histogram[i] + histogram[i - 1] + histogram[i - 2] +
+		   histogram[i - 3] + histogram[i - 4]) / 5) > histogram[i] &&
+		 stop == 0) {
+	    /*Search for peaks of datasets (1) */
+	    peak1 = histogram[i];
+	    i_peak1 = i;
+	    stop = 1;
 	}
-	/* First histogram higher bound */
-	for(i=i_peak2;i>i_peak1;i--){
-		if(histogram[i]<=bottom1b){
-			bottom1b = histogram[i];
-			i_bottom1b = i;
-		}
+    }
+    stop = 0;
+    for (i = i_peak2; i < 395; i++) {
+	if (((histogram[i] + histogram[i + 1] + histogram[i + 2] +
+	      histogram[i + 3] + histogram[i + 4]) / 5) < histogram[i] &&
+	    stop == 0) {
+	    bottom2b = histogram[i];
+	    i_bottom2b = i;
 	}
-	/* Third histogram lower bound */
-	for(i=i_peak2;i<i_peak3;i++){
-		if(histogram[i]<bottom3a){
-			bottom3a = histogram[i];
-			i_bottom3a = i;
+	else if (((histogram[i] + histogram[i + 1] + histogram[i + 2] +
+		   histogram[i + 3] + histogram[i + 4]) / 5) > histogram[i] &&
+		 stop == 0) {
+	    /*Search for peaks of datasets (3) */
+	    peak3 = histogram[i];
+	    i_peak3 = i;
+	    stop = 1;
+	}
+    }
+    /* First histogram lower bound */
+    for (i = 250; i < i_peak1; i++) {
+	if (histogram[i] < bottom1a) {
+	    bottom1a = histogram[i];
+	    i_bottom1a = i;
+	}
+    }
+    /* First histogram higher bound */
+    for (i = i_peak2; i > i_peak1; i--) {
+	if (histogram[i] <= bottom1b) {
+	    bottom1b = histogram[i];
+	    i_bottom1b = i;
+	}
+    }
+    /* Third histogram lower bound */
+    for (i = i_peak2; i < i_peak3; i++) {
+	if (histogram[i] < bottom3a) {
+	    bottom3a = histogram[i];
+	    i_bottom3a = i;
+	}
+    }
+    /* Third histogram higher bound */
+    for (i = 399; i > i_peak3; i--) {
+	if (histogram[i] < bottom3b) {
+	    bottom3b = histogram[i];
+	    i_bottom3b = i;
+	}
+    }
+
+    DCELL d_tempk;		/* Input raster */
+
+    DCELL d_albedo;		/* Input raster */
+
+    DCELL d_dem;		/* Input raster */
+
+    DCELL d_Rn;			/* Input raster */
+
+    DCELL d_g0;			/* Input raster */
+
+    CELL c_wet;			/* Output pixel */
+
+    CELL c_dry;			/* Output pixel */
+
+    DCELL d_dem_dry;		/* Generated here */
+
+    DCELL d_t0dem, d_t0dem_dry;	/* Generated here */
+
+    DCELL t0dem_min, tempk_min;	/* Generated here */
+
+    DCELL t0dem_max, tempk_max;	/* Generated here */
+
+    DCELL d_tempk_max, d_tempk_min;	/* Generated here */
+
+    DCELL d_h0, d_h0_max;	/* Generated here */
+
+    DCELL d_tempk_wet, d_tempk_dry;	/* Generated here */
+
+    DCELL d_Rn_wet, d_Rn_dry;	/* Generated here */
+
+    DCELL d_g0_wet, d_g0_dry;	/* Generated here */
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), T);
+	if (G_get_raster_row(infd_alb, inrast_alb, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), alb);
+	if (G_get_raster_row(infd_DEM, inrast_DEM, row, data_type_DEM) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), DEM);
+	if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Rn);
+	if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), g0);
+
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_alb)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_alb)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_alb)[col];
+		break;
+	    }
+	    switch (data_type_T) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_T)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_T)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_T)[col];
+		break;
+	    }
+	    switch (data_type_DEM) {
+	    case CELL_TYPE:
+		d_dem = (double)((CELL *) inrast_DEM)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dem = (double)((FCELL *) inrast_DEM)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dem = (double)((DCELL *) inrast_DEM)[col];
+		break;
+	    }
+	    switch (data_type_Rn) {
+	    case CELL_TYPE:
+		d_Rn = (double)((CELL *) inrast_Rn)[col];
+		break;
+	    case FCELL_TYPE:
+		d_Rn = (double)((FCELL *) inrast_Rn)[col];
+		break;
+	    case DCELL_TYPE:
+		d_Rn = (double)((DCELL *) inrast_Rn)[col];
+		break;
+	    }
+	    switch (data_type_g0) {
+	    case CELL_TYPE:
+		d_g0 = (double)((CELL *) inrast_g0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_g0 = (double)((FCELL *) inrast_g0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_g0 = (double)((DCELL *) inrast_g0)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_albedo) ||
+		G_is_d_null_value(&d_tempk) ||
+		G_is_d_null_value(&d_dem) ||
+		G_is_d_null_value(&d_Rn) || G_is_d_null_value(&d_g0)) {
+		/* do nothing */
+	    }
+	    else {
+		d_t0dem = d_tempk + 0.001649 * d_dem;
+		if (d_t0dem <= 250.0 || d_tempk <= 250.0) {
+		    /* do nothing */
 		}
-	}	
-	/* Third histogram higher bound */
-	for(i=399;i>i_peak3;i--){
-		if(histogram[i]<bottom3b){
-			bottom3b = histogram[i];
-			i_bottom3b = i;
+		else {
+		    d_h0 = d_Rn - d_g0;
+		    if (d_t0dem < t0dem_min && d_albedo < 0.15) {
+			t0dem_min = d_t0dem;
+			tempk_min = d_tempk;
+			d_tempk_wet = d_tempk;
+			d_Rn_wet = d_Rn;
+			d_g0_wet = d_g0;
+		    }
+		    if (d_tempk >= (double)i_peak1 - 5.0 &&
+			d_tempk < (double)i_peak1 + 1.0) {
+			tempk_min = d_tempk;
+			d_tempk_wet = d_tempk;
+			d_Rn_wet = d_Rn;
+			d_g0_wet = d_g0;
+		    }
+		    if (d_t0dem > t0dem_max) {
+			t0dem_max = d_t0dem;
+			d_t0dem_dry = d_t0dem;
+			tempk_max = d_tempk;
+			d_tempk_dry = d_tempk;
+			d_Rn_dry = d_Rn;
+			d_g0_dry = d_g0;
+			d_dem_dry = d_dem;
+		    }
+		    if (d_tempk >= (double)i_peak3 - 0.0 &&
+			d_tempk < (double)i_peak3 + 7.0 &&
+			d_h0 > 100.0 && d_h0 > d_h0_max &&
+			d_g0 > 10.0 && d_Rn > 100.0 && d_albedo > 0.3) {
+			tempk_max = d_tempk;
+			d_tempk_dry = d_tempk;
+			d_Rn_dry = d_Rn;
+			d_g0_dry = d_g0;
+			d_h0_max = d_h0;
+			d_dem_dry = d_dem;
+		    }
 		}
-	}	
+	    }
+	}
+    }
+    G_message("tempk_min=%f", tempk_min);
+    G_message("tempk_max=%f", tempk_max);
 
-	DCELL d_tempk; 		/* Input raster */
-	DCELL d_albedo;		/* Input raster */
-	DCELL d_dem; 		/* Input raster */
-	DCELL d_Rn;		/* Input raster */
-	DCELL d_g0;		/* Input raster */
-	CELL c_wet;	/* Output pixel */
-	CELL c_dry;	/* Output pixel */
-	DCELL d_dem_dry;	/* Generated here */
-	DCELL d_t0dem, d_t0dem_dry;	/* Generated here */
-	DCELL t0dem_min, tempk_min;	/* Generated here */
-	DCELL t0dem_max, tempk_max;	/* Generated here */
-	DCELL d_tempk_max, d_tempk_min;	/* Generated here */
-	DCELL d_h0, d_h0_max;	/* Generated here */
-	DCELL d_tempk_wet, d_tempk_dry;	/* Generated here */
-	DCELL d_Rn_wet, d_Rn_dry;	/* Generated here */
-	DCELL d_g0_wet, d_g0_dry;	/* Generated here */
 
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		/* read a line input maps into buffers*/	
-		if (G_get_raster_row (infd_T, inrast_T, row, data_type_T) < 0)
-			G_fatal_error (_("Could not read from <%s>"),T);
-		if (G_get_raster_row (infd_alb, inrast_alb, row, data_type_albedo) < 0)
-			G_fatal_error (_("Could not read from <%s>"),alb);
-		if (G_get_raster_row (infd_DEM, inrast_DEM, row, data_type_DEM) < 0)
-			G_fatal_error (_("Could not read from <%s>"),DEM);
-		if (G_get_raster_row (infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
-			G_fatal_error (_("Could not read from <%s>"),Rn);
-		if (G_get_raster_row (infd_g0, inrast_g0, row, data_type_g0) < 0)
-			G_fatal_error (_("Could not read from <%s>"),g0);
-		
-		/* read every cell in the line buffers */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_alb)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_alb)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_alb)[col];
-					break;
-			}
-			switch(data_type_T){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_T)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_T)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_T)[col];
-					break;
-			}
-			switch(data_type_DEM){
-				case CELL_TYPE:
-					d_dem = (double) ((CELL *) inrast_DEM)[col];
-					break;
-				case FCELL_TYPE:
-					d_dem = (double) ((FCELL *) inrast_DEM)[col];
-					break;
-				case DCELL_TYPE:
-					d_dem = (double) ((DCELL *) inrast_DEM)[col];
-					break;
-			}
-			switch(data_type_Rn){
-				case CELL_TYPE:
-					d_Rn = (double) ((CELL *) inrast_Rn)[col];
-					break;
-				case FCELL_TYPE:
-					d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-					break;
-				case DCELL_TYPE:
-					d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-					break;
-			}
-			switch(data_type_g0){
-				case CELL_TYPE:
-					d_g0 = (double) ((CELL *) inrast_g0)[col];
-					break;
-				case FCELL_TYPE:
-					d_g0 = (double) ((FCELL *) inrast_g0)[col];
-					break;
-				case DCELL_TYPE:
-					d_g0 = (double) ((DCELL *) inrast_g0)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_dem)||
-			G_is_d_null_value(&d_Rn)||
-			G_is_d_null_value(&d_g0)){
-				/* do nothing */ 
-			}else{
-				d_t0dem = d_tempk + 0.001649*d_dem;
-				if(d_t0dem<=250.0||d_tempk<=250.0){
-					/* do nothing */ 
-				} else {
-					d_h0=d_Rn-d_g0;
-					if(d_t0dem<t0dem_min&&d_albedo<0.15){
-						t0dem_min=d_t0dem;
-						tempk_min=d_tempk;
-						d_tempk_wet=d_tempk;
-						d_Rn_wet=d_Rn;
-						d_g0_wet=d_g0;
-					}
-					if(d_tempk>=(double)i_peak1-5.0&&
-					d_tempk<(double)i_peak1+1.0){
-						tempk_min=d_tempk;
-						d_tempk_wet=d_tempk;
-						d_Rn_wet=d_Rn;
-						d_g0_wet=d_g0;
-					}
-					if(d_t0dem>t0dem_max){
-						t0dem_max=d_t0dem;
-						d_t0dem_dry=d_t0dem;
-						tempk_max=d_tempk;
-						d_tempk_dry=d_tempk;
-						d_Rn_dry=d_Rn;
-						d_g0_dry=d_g0;
-						d_dem_dry=d_dem;
-					}
-					if(d_tempk>=(double)i_peak3-0.0&&
-					d_tempk<(double)i_peak3+7.0&&
-					d_h0>100.0&&d_h0>d_h0_max&&
-					d_g0>10.0&&d_Rn>100.0&&
-					d_albedo>0.3){
-						tempk_max=d_tempk;
-						d_tempk_dry=d_tempk;
-						d_Rn_dry=d_Rn;
-						d_g0_dry=d_g0;
-						d_h0_max=d_h0;
-						d_dem_dry=d_dem;
-					}
-				}
-			}
+    /* ALLOCATE CLASSES: WET=1 DRY=1 OTHER=NULL OR 0.0 */
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read a line input maps into buffers */
+	if (G_get_raster_row(infd_T, inrast_T, row, data_type_T) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), T);
+	if (G_get_raster_row(infd_alb, inrast_alb, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), alb);
+	if (G_get_raster_row(infd_DEM, inrast_DEM, row, data_type_DEM) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), DEM);
+	if (G_get_raster_row(infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Rn);
+	if (G_get_raster_row(infd_g0, inrast_g0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), g0);
+
+	/* read every cell in the line buffers */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_alb)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_alb)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_alb)[col];
+		break;
+	    }
+	    switch (data_type_T) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_T)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_T)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_T)[col];
+		break;
+	    }
+	    switch (data_type_DEM) {
+	    case CELL_TYPE:
+		d_dem = (double)((CELL *) inrast_DEM)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dem = (double)((FCELL *) inrast_DEM)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dem = (double)((DCELL *) inrast_DEM)[col];
+		break;
+	    }
+	    switch (data_type_Rn) {
+	    case CELL_TYPE:
+		d_Rn = (double)((CELL *) inrast_Rn)[col];
+		break;
+	    case FCELL_TYPE:
+		d_Rn = (double)((FCELL *) inrast_Rn)[col];
+		break;
+	    case DCELL_TYPE:
+		d_Rn = (double)((DCELL *) inrast_Rn)[col];
+		break;
+	    }
+	    switch (data_type_g0) {
+	    case CELL_TYPE:
+		d_g0 = (double)((CELL *) inrast_g0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_g0 = (double)((FCELL *) inrast_g0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_g0 = (double)((DCELL *) inrast_g0)[col];
+		break;
+	    }
+	    /* Initialize pixels as negative */
+	    c_wet = -1;
+	    c_dry = -1;
+
+	    if (G_is_d_null_value(&d_albedo) ||
+		G_is_d_null_value(&d_tempk) ||
+		G_is_d_null_value(&d_dem) ||
+		G_is_d_null_value(&d_Rn) || G_is_d_null_value(&d_g0)) {
+		/* do nothing */
+		G_set_c_null_value(&outrast[col], 1);
+		G_set_c_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		/* if Albedo is high and H is positive */
+		if (d_albedo > 0.3 &&	/*(d_Rn-d_g0) > 100.0 && */
+		    d_tempk >= (double)i_peak3 - 5.0 &&
+		    d_tempk <= (double)i_peak3 + 5.0) {
+		    c_dry = 1;	/* Dry pixel candidate */
 		}
-	}
-	G_message("tempk_min=%f",tempk_min);
-	G_message("tempk_max=%f",tempk_max);
 
+		/* if Albedo is not high and H is low */
+		if (d_albedo < 0.15 &&	/*(d_Rn-d_g0) <= 100.0 && */
+		    d_tempk >= (double)i_peak1 - 5.0 &&
+		    d_tempk <= (double)i_peak1 + 5.0) {
+		    c_wet = 1;	/* Wet pixel Candidate */
+		}
+		/*flag NULL -> 0 */
+		if (zero->answer && c_wet < 0) {
+		    c_wet = 0;
+		}
+		if (zero->answer && c_dry < 0) {
+		    c_dry = 0;
+		}
+		/*results */
+		outrast[col] = c_wet;
+		outrast1[col] = c_dry;
 
-	/* ALLOCATE CLASSES: WET=1 DRY=1 OTHER=NULL OR 0.0 */
+		/*clean up */
+		if (c_wet == -1)
+		    G_set_c_null_value(&outrast[col], 1);
+		if (c_dry == -1)
+		    G_set_c_null_value(&outrast1[col], 1);
+	    }
+	}
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), wetF);
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), dryF);
+    }
+    G_free(inrast_T);
+    G_free(inrast_alb);
+    G_free(inrast_DEM);
+    G_free(inrast_Rn);
+    G_free(inrast_g0);
+    G_free(outrast);
+    G_free(outrast1);
+    G_close_cell(infd_T);
+    G_close_cell(infd_alb);
+    G_close_cell(infd_DEM);
+    G_close_cell(infd_Rn);
+    G_close_cell(infd_g0);
+    G_close_cell(outfd);
+    G_close_cell(outfd1);
 
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		/* read a line input maps into buffers*/	
-		if (G_get_raster_row (infd_T, inrast_T, row, data_type_T) < 0)
-			G_fatal_error (_("Could not read from <%s>"),T);
-		if (G_get_raster_row (infd_alb, inrast_alb, row, data_type_albedo) < 0)
-			G_fatal_error (_("Could not read from <%s>"),alb);
-		if (G_get_raster_row (infd_DEM, inrast_DEM, row, data_type_DEM) < 0)
-			G_fatal_error (_("Could not read from <%s>"),DEM);
-		if (G_get_raster_row (infd_Rn, inrast_Rn, row, data_type_Rn) < 0)
-			G_fatal_error (_("Could not read from <%s>"),Rn);
-		if (G_get_raster_row (infd_g0, inrast_g0, row, data_type_g0) < 0)
-			G_fatal_error (_("Could not read from <%s>"),g0);
-		
-		/* read every cell in the line buffers */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_alb)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_alb)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_alb)[col];
-					break;
-			}
-			switch(data_type_T){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_T)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_T)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_T)[col];
-					break;
-			}
-			switch(data_type_DEM){
-				case CELL_TYPE:
-					d_dem = (double) ((CELL *) inrast_DEM)[col];
-					break;
-				case FCELL_TYPE:
-					d_dem = (double) ((FCELL *) inrast_DEM)[col];
-					break;
-				case DCELL_TYPE:
-					d_dem = (double) ((DCELL *) inrast_DEM)[col];
-					break;
-			}
-			switch(data_type_Rn){
-				case CELL_TYPE:
-					d_Rn = (double) ((CELL *) inrast_Rn)[col];
-					break;
-				case FCELL_TYPE:
-					d_Rn = (double) ((FCELL *) inrast_Rn)[col];
-					break;
-				case DCELL_TYPE:
-					d_Rn = (double) ((DCELL *) inrast_Rn)[col];
-					break;
-			}
-			switch(data_type_g0){
-				case CELL_TYPE:
-					d_g0 = (double) ((CELL *) inrast_g0)[col];
-					break;
-				case FCELL_TYPE:
-					d_g0 = (double) ((FCELL *) inrast_g0)[col];
-					break;
-				case DCELL_TYPE:
-					d_g0 = (double) ((DCELL *) inrast_g0)[col];
-					break;
-			}
-			/* Initialize pixels as negative */
-			c_wet = -1;
-			c_dry = -1;
-			
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_dem)||
-			G_is_d_null_value(&d_Rn)||
-			G_is_d_null_value(&d_g0)){
-				/* do nothing */ 
-				G_set_c_null_value(&outrast[col],1);
-				G_set_c_null_value(&outrast1[col],1);
-			}else{
-				/* if Albedo is high and H is positive */
-				if(d_albedo > 0.3 && /*(d_Rn-d_g0) > 100.0 &&*/
-				d_tempk >= (double)i_peak3-5.0 &&
-				d_tempk <= (double)i_peak3+5.0){
-					c_dry = 1; /* Dry pixel candidate */
-				}
-				
-				/* if Albedo is not high and H is low */
-				if (d_albedo < 0.15 && /*(d_Rn-d_g0) <= 100.0 &&*/
-				d_tempk >= (double) i_peak1-5.0 &&
-				d_tempk <= (double) i_peak1+5.0){
-					c_wet = 1; /* Wet pixel Candidate */
-				}	
-				/*flag NULL -> 0*/
-				if (zero->answer && c_wet<0){
-					c_wet = 0;
-				}
-				if (zero->answer && c_dry<0){
-					c_dry = 0;
-				}
-				/*results*/
-				outrast[col] = c_wet;
-				outrast1[col] = c_dry;
-				
-				/*clean up*/
-				if(c_wet==-1)
-					G_set_c_null_value(&outrast[col],1);
-				if(c_dry==-1)
-					G_set_c_null_value(&outrast1[col],1);
-			}
-		}	
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error (_("Cannot write to <%s>"),wetF);
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error (_("Cannot write to <%s>"),dryF);
-	}	
-	G_free(inrast_T);
-	G_free(inrast_alb);
-	G_free(inrast_DEM);
-	G_free(inrast_Rn);
-	G_free(inrast_g0);
-	G_free(outrast);
-	G_free(outrast1);
-	G_close_cell (infd_T);
-	G_close_cell (infd_alb);
-	G_close_cell (infd_DEM);
-	G_close_cell (infd_Rn);
-	G_close_cell (infd_g0);
-	G_close_cell (outfd);
-	G_close_cell (outfd1);
-	
-        /* add command line incantation to history file */
-        G_short_history(wetF, "raster", &history);
-        G_command_history(&history);
-        G_write_history(wetF, &history);
-        G_short_history(dryF, "raster", &history);
-        G_command_history(&history);
-        G_write_history(dryF, &history);
+    /* add command line incantation to history file */
+    G_short_history(wetF, "raster", &history);
+    G_command_history(&history);
+    G_write_history(wetF, &history);
+    G_short_history(dryF, "raster", &history);
+    G_command_history(&history);
+    G_write_history(dryF, &history);
 
-	exit(EXIT_SUCCESS);
+    exit(EXIT_SUCCESS);
 }

Modified: grass-addons/gipe/i.eb.z0m/main.c
===================================================================
--- grass-addons/gipe/i.eb.z0m/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.z0m/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,192 +1,216 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.z0m
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the momentum roughness length
- *               as seen in Bastiaanssen (1995) 
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double z_0m(double sa_vi);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int heat=0;/*Flag for surf. roughness for heat transport output*/
-	struct GModule *module;
-	struct Option *input1, *input2, *output1, *output2;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /* input raster name*/
-	char *result1, *result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_savi;
-	int outfd1, outfd2;
-	
-	char *savi;
-	char *z0m, *z0h;	
-	double coef_z0h;
-	int i=0,j=0;
-	
-	void *inrast_savi;
-	DCELL *outrast1, *outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_savi;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("roughness length, energy balance, SEBAL");
-	module->description = _("Momentum roughness length (z0m) and surface roughness for heat transport (z0h) as seen in Pawan (2004)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("savi");
-	input1->description=_("Name of the SAVI map [-1.0;1.0]");
-
-	input2 = G_define_option() ;
-	input2->key        =_("coef");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = NO;
-	input2->gisprompt  =_("parameter,value");
-	input2->description=_("Value of the converion factor from z0m and z0h (Pawan, 2004, used 0.1)");
-	input2->answer     =_("0.1");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output z0m layer");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("z0h");
-	output2->required   = NO;
-	output2->description=_("Name of the output z0h layer");
-	
-	flag1 = G_define_flag();
-	flag1->key = 'h';
-	flag1->description = _("z0h output (You have to input a coef value)");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	savi	 	= input1->answer;
-	coef_z0h 	= atof(input2->answer);
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	heat    = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(savi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), savi);
-	}
-	data_type_savi = G_raster_map_type(savi,mapset);
-	if ( (infd_savi = G_open_cell_old (savi,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), savi);
-	if (G_get_cellhd (savi, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), savi);
-	inrast_savi = G_allocate_raster_buf(data_type_savi);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(input2->answer&&output2->answer){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(input2->answer&&output2->answer){
-		if ( (outfd2 = G_open_raster_new (result2,data_type_output)) < 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_savi;
-		DCELL d_z0h;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_savi,inrast_savi,row,data_type_savi)<0)
-			G_fatal_error(_("Could not read from <%s>"),savi);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_savi){
-				case CELL_TYPE:
-					d_savi = (double) ((CELL *) inrast_savi)[col];
-					break;
-				case FCELL_TYPE:
-					d_savi = (double) ((FCELL *) inrast_savi)[col];
-					break;
-				case DCELL_TYPE:
-					d_savi = ((DCELL *) inrast_savi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_savi)){
-				G_set_d_null_value(&outrast1[col],1);
-				if(input2->answer&&output2->answer){
-					G_set_d_null_value(&outrast2[col],1);
-				}
-			}else {
-				/****************************/
-				/* calculate z0m	    */
-				d = z_0m(d_savi);
-				outrast1[col] = d;
-				if(input2->answer&&output2->answer){
-					d_z0h = d * coef_z0h;
-					outrast2[col] = d_z0h;
-				}
-			}
-		}
-		if (G_put_raster_row(outfd1,outrast1,data_type_output)<0)
-			G_fatal_error(_("Cannot write to output raster file"));
-		if(input2->answer&&output2->answer){
-			if (G_put_raster_row(outfd2,outrast2,data_type_output)<0)
-				G_fatal_error(_("Cannot write to output raster file"));
-		}
-	}
-
-	G_free (inrast_savi);
-	G_close_cell (infd_savi);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	if(input2->answer&&output2->answer){
-		G_free (outrast2);
-		G_close_cell (outfd2);
-	}
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	if(input2->answer&&output2->answer){
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.z0m
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the momentum roughness length
+ *               as seen in Bastiaanssen (1995) 
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double z_0m(double sa_vi);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int heat = 0;		/*Flag for surf. roughness for heat transport output */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *output1, *output2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/* input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_savi;
+
+    int outfd1, outfd2;
+
+    char *savi;
+
+    char *z0m, *z0h;
+
+    double coef_z0h;
+
+    int i = 0, j = 0;
+
+    void *inrast_savi;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_savi;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("roughness length, energy balance, SEBAL");
+    module->description =
+	_("Momentum roughness length (z0m) and surface roughness for heat transport (z0h) as seen in Pawan (2004)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("savi");
+    input1->description = _("Name of the SAVI map [-1.0;1.0]");
+    input2 = G_define_option();
+    input2->key = _("coef");
+    input2->type = TYPE_DOUBLE;
+    input2->required = NO;
+    input2->gisprompt = _("parameter,value");
+    input2->description =
+	_("Value of the converion factor from z0m and z0h (Pawan, 2004, used 0.1)");
+    input2->answer = _("0.1");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output z0m layer");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("z0h");
+    output2->required = NO;
+    output2->description = _("Name of the output z0h layer");
+    flag1 = G_define_flag();
+    flag1->key = 'h';
+    flag1->description = _("z0h output (You have to input a coef value)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    savi = input1->answer;
+    coef_z0h = atof(input2->answer);
+    result1 = output1->answer;
+    result2 = output2->answer;
+    heat = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(savi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), savi);
+    }
+    data_type_savi = G_raster_map_type(savi, mapset);
+    if ((infd_savi = G_open_cell_old(savi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), savi);
+    if (G_get_cellhd(savi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), savi);
+    inrast_savi = G_allocate_raster_buf(data_type_savi);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (input2->answer && output2->answer) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (input2->answer && output2->answer) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_savi;
+	DCELL d_z0h;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_savi, inrast_savi, row, data_type_savi)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), savi);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_savi) {
+	    case CELL_TYPE:
+		d_savi = (double)((CELL *) inrast_savi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_savi = (double)((FCELL *) inrast_savi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_savi = ((DCELL *) inrast_savi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_savi)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (input2->answer && output2->answer) {
+		    G_set_d_null_value(&outrast2[col], 1);
+		}
+	    }
+	    else {
+		
+
+				/****************************/ 
+		    /* calculate z0m            */ 
+		    d = z_0m(d_savi);
+		outrast1[col] = d;
+		if (input2->answer && output2->answer) {
+		    d_z0h = d * coef_z0h;
+		    outrast2[col] = d_z0h;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	if (input2->answer && output2->answer) {
+	    if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output raster file"));
+	}
+	}
+    G_free(inrast_savi);
+    G_close_cell(infd_savi);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    if (input2->answer && output2->answer) {
+	G_free(outrast2);
+	G_close_cell(outfd2);
+    }
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    if (input2->answer && output2->answer) {
+	G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.z0m/z0m.c
===================================================================
--- grass-addons/gipe/i.eb.z0m/z0m.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.z0m/z0m.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,13 +2,13 @@
 #include<stdlib.h>
 #include<math.h>
 
-/* Momentum roughness length (z0m) as seen in Pawan (2004)*/
+/* Momentum roughness length (z0m) as seen in Pawan (2004) */
 
 double z_0m(double sa_vi)
 {
-	double result;
+    double result;
 
-	result = exp(-5.809+5.62*sa_vi);
+    result = exp(-5.809 + 5.62 * sa_vi);
 
-	return result;
+    return result;
 }

Modified: grass-addons/gipe/i.eb.z0m0/main.c
===================================================================
--- grass-addons/gipe/i.eb.z0m0/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.z0m0/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,243 +1,271 @@
-/****************************************************************************
- *
- * MODULE:       i.eb.z0m0
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the momentum roughness length
- *               as seen in Bastiaanssen (1995) 
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double zom_0(double ndvi, double ndvi_max, double hv_ndvimax, double hv_desert);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int heat=0;/*Flag for surf. roughness for heat transport output*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *output1, *output2;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1, *result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_ndvi;
-	int outfd1, outfd2;
-	
-	char *ndvi;
-	char *z0m, *z0h;	
-	double coef_z0h, hv_max, hmr;
-	int i=0,j=0;
-	
-	void *inrast_ndvi;
-	DCELL *outrast1, *outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_ndvi;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("roughness length, energy balance, SEBAL");
-	module->description = _("Momentum roughness length (z0m) and surface roughness for heat transport (z0h) as seen in Bastiaanssen (2004)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("ndvi");
-	input1->description=_("Name of the NDVI map [-1.0;1.0]");
-
-	input2 = G_define_option() ;
-	input2->key        =_("coef");
-	input2->type       = TYPE_DOUBLE;
-	input2->required   = NO;
-	input2->gisprompt  =_("parameter,value");
-	input2->description=_("Value of the converion factor from z0m and z0h (Bastiaanssen (2005) used 0.1)");
-	input2->answer     =_("0.1");
-
-	input3 = G_define_option() ;
-	input3->key        =_("hv_max");
-	input3->type       = TYPE_DOUBLE;
-	input3->required   = YES;
-	input3->gisprompt  =_("parameter,value");
-	input3->description=_("Value of the vegetation height at max(NDVI) i.e. standard C3 crop could be 1.5m");
-	input3->answer     =_("1.5");
-
-	input4 = G_define_option() ;
-	input4->key        =_("hmr");
-	input4->type       = TYPE_DOUBLE;
-	input4->required   = YES;
-	input4->gisprompt  =_("parameter,value");
-	input4->description=_("Value of the micro-relief height (h.m-r.) on flat bare ground, most references point to 2cm");
-	input4->answer     =_("0.02");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output z0m layer");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("z0h");
-	output2->required   = NO;
-	output2->description=_("Name of the output z0h layer");
-	
-	flag1 = G_define_flag();
-	flag1->key = 'h';
-	flag1->description = _("z0h output (You have to input a coef value)");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	ndvi	 	= input1->answer;
-	if(input2->answer)
-		coef_z0h 	= atof(input2->answer);
-	hv_max	 	= atof(input3->answer);
-	hmr	 	= atof(input4->answer);
-	
-	result1  = output1->answer;
-	if(output2->answer)
-		result2  = output2->answer;
-	if(flag1->answer)
-		heat    = flag1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(input2->answer&&output2->answer){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(input2->answer&&output2->answer){
-		if ( (outfd2 = G_open_raster_new (result2,data_type_output)) < 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	DCELL d_ndvi; 		/* Input raster */
-	DCELL d_ndvi_max = 0.0;	/* Generated here */
-	/* THREAD 1 */
-	/* NDVI Max */
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent (row, nrows, 2);
-		if (G_get_d_raster_row (infd_ndvi, inrast_ndvi, row) < 0)
-			G_fatal_error (_("Could not read from <%s>"),ndvi);
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_ndvi)){
-				/* do nothing */ 
-			} else if ((d_ndvi)>d_ndvi_max&&(d_ndvi)<0.98){
-				d_ndvi_max	= d_ndvi;
-			}
-		}
-	}
-	G_message("ndvi_max=%f\n",d_ndvi_max);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_z0h;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_ndvi)){
-				G_set_d_null_value(&outrast1[col],1);
-				if(input2->answer&&output2->answer){
-					G_set_d_null_value(&outrast2[col],1);
-				}
-			}else {
-				/****************************/
-				/* calculate z0m	    */
-				d = zom_0( d_ndvi, d_ndvi_max, hv_max, hmr );
-				outrast1[col] = d;
-				if(input2->answer&&output2->answer){
-					d_z0h = d * coef_z0h;
-					outrast2[col] = d_z0h;
-				}
-			}
-		}
-		if (G_put_raster_row(outfd1,outrast1,data_type_output)<0)
-			G_fatal_error(_("Cannot write to output z0m raster file"));
-		if(input2->answer&&output2->answer){
-			if (G_put_raster_row(outfd2,outrast2,data_type_output)<0)
-				G_fatal_error(_("Cannot write to output z0h raster file"));
-		}
-	}
-
-	G_free (inrast_ndvi);
-	G_close_cell (infd_ndvi);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	if(input2->answer&&output2->answer){
-		G_free (outrast2);
-		G_close_cell (outfd2);
-	}
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	if(input2->answer&&output2->answer){
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.eb.z0m0
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the momentum roughness length
+ *               as seen in Bastiaanssen (1995) 
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double zom_0(double ndvi, double ndvi_max, double hv_ndvimax,
+	      double hv_desert);
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int heat = 0;		/*Flag for surf. roughness for heat transport output */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *output1, *output2;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_ndvi;
+
+    int outfd1, outfd2;
+
+    char *ndvi;
+
+    char *z0m, *z0h;
+
+    double coef_z0h, hv_max, hmr;
+
+    int i = 0, j = 0;
+
+    void *inrast_ndvi;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_ndvi;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("roughness length, energy balance, SEBAL");
+    module->description =
+	_("Momentum roughness length (z0m) and surface roughness for heat transport (z0h) as seen in Bastiaanssen (2004)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("ndvi");
+    input1->description = _("Name of the NDVI map [-1.0;1.0]");
+    input2 = G_define_option();
+    input2->key = _("coef");
+    input2->type = TYPE_DOUBLE;
+    input2->required = NO;
+    input2->gisprompt = _("parameter,value");
+    input2->description =
+	_("Value of the converion factor from z0m and z0h (Bastiaanssen (2005) used 0.1)");
+    input2->answer = _("0.1");
+    input3 = G_define_option();
+    input3->key = _("hv_max");
+    input3->type = TYPE_DOUBLE;
+    input3->required = YES;
+    input3->gisprompt = _("parameter,value");
+    input3->description =
+	_("Value of the vegetation height at max(NDVI) i.e. standard C3 crop could be 1.5m");
+    input3->answer = _("1.5");
+    input4 = G_define_option();
+    input4->key = _("hmr");
+    input4->type = TYPE_DOUBLE;
+    input4->required = YES;
+    input4->gisprompt = _("parameter,value");
+    input4->description =
+	_("Value of the micro-relief height (h.m-r.) on flat bare ground, most references point to 2cm");
+    input4->answer = _("0.02");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output z0m layer");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("z0h");
+    output2->required = NO;
+    output2->description = _("Name of the output z0h layer");
+    flag1 = G_define_flag();
+    flag1->key = 'h';
+    flag1->description = _("z0h output (You have to input a coef value)");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    ndvi = input1->answer;
+    if (input2->answer)
+	coef_z0h = atof(input2->answer);
+    hv_max = atof(input3->answer);
+    hmr = atof(input4->answer);
+    result1 = output1->answer;
+    if (output2->answer)
+	result2 = output2->answer;
+    if (flag1->answer)
+	heat = flag1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (input2->answer && output2->answer) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (input2->answer && output2->answer) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    DCELL d_ndvi;		/* Input raster */
+    DCELL d_ndvi_max = 0.0;	/* Generated here */
+    
+	/* THREAD 1 */ 
+	/* NDVI Max */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	G_percent(row, nrows, 2);
+	if (G_get_d_raster_row(infd_ndvi, inrast_ndvi, row) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_ndvi)) {
+		
+		    /* do nothing */ 
+	    }
+	    else if ((d_ndvi) > d_ndvi_max && (d_ndvi) < 0.98) {
+		d_ndvi_max = d_ndvi;
+	    }
+	    }
+	}
+    G_message("ndvi_max=%f\n", d_ndvi_max);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_z0h;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_ndvi)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (input2->answer && output2->answer) {
+		    G_set_d_null_value(&outrast2[col], 1);
+		}
+	    }
+	    else {
+		
+
+				/****************************/ 
+		    /* calculate z0m            */ 
+		    d = zom_0(d_ndvi, d_ndvi_max, hv_max, hmr);
+		outrast1[col] = d;
+		if (input2->answer && output2->answer) {
+		    d_z0h = d * coef_z0h;
+		    outrast2[col] = d_z0h;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output z0m raster file"));
+	if (input2->answer && output2->answer) {
+	    if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output z0h raster file"));
+	}
+	}
+    G_free(inrast_ndvi);
+    G_close_cell(infd_ndvi);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    if (input2->answer && output2->answer) {
+	G_free(outrast2);
+	G_close_cell(outfd2);
+    }
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    if (input2->answer && output2->answer) {
+	G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.eb.z0m0/zom_0.c
===================================================================
--- grass-addons/gipe/i.eb.z0m0/zom_0.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.eb.z0m0/zom_0.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,16 +1,18 @@
 #include<stdio.h>
 #include<math.h>
 
-double zom_0(double ndvi, double ndvi_max, double hv_ndvimax, double hv_desert )
+double zom_0(double ndvi, double ndvi_max, double hv_ndvimax,
+	     double hv_desert)
 {
-	double a, b, z0m;
-	/* hv_ndvimax = crop vegetation height (m) */
-	/* hv_desert=0.002 = desert base vegetation height (m) */
-	
-	a = (log(hv_desert)-((log(hv_ndvimax/7)-log(hv_desert))/(ndvi_max-0.02)*0.02));
-	b = (log(hv_ndvimax/7)-log(hv_desert))/(ndvi_max-0.02)* ndvi;
-	z0m = exp(a+b); 
-	
-	return (z0m);
+    double a, b, z0m;
+
+    /* hv_ndvimax = crop vegetation height (m) */
+    /* hv_desert=0.002 = desert base vegetation height (m) */
+
+    a = (log(hv_desert) -
+	 ((log(hv_ndvimax / 7) - log(hv_desert)) / (ndvi_max - 0.02) * 0.02));
+    b = (log(hv_ndvimax / 7) - log(hv_desert)) / (ndvi_max - 0.02) * ndvi;
+    z0m = exp(a + b);
+
+    return (z0m);
 }
-

Modified: grass-addons/gipe/i.emissivity/emissivity_generic.c
===================================================================
--- grass-addons/gipe/i.emissivity/emissivity_generic.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.emissivity/emissivity_generic.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,23 +1,24 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Emissivity Generic mode (Reads directly from NDVI)
- * Estimation in the 8-14 micrometers range for sparse canopy
- */
-
-double emissivity_generic( double ndvi )
-{
-	double	result;
-	
-	if(ndvi < 0.16){
-		result = 1.0;
-	} else if(ndvi > 0.74){
-		result = 0.9;
-	} else {
-		result = 1.009 + 0.047*log(ndvi);
-	}
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Emissivity Generic mode (Reads directly from NDVI)
+     * Estimation in the 8-14 micrometers range for sparse canopy
+     */ 
+double emissivity_generic(double ndvi) 
+{
+    double result;
+
+    if (ndvi < 0.16) {
+	result = 1.0;
+    }
+    else if (ndvi > 0.74) {
+	result = 0.9;
+    }
+    else {
+	result = 1.009 + 0.047 * log(ndvi);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.emissivity/main.c
===================================================================
--- grass-addons/gipe/i.emissivity/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.emissivity/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,140 +1,165 @@
-/****************************************************************************
- *
- * MODULE:       i.emissivity
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the emissivity from NDVI (empirical)
- *                as seen in Caselles and Colles (1997). 
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double emissivity_generic( double ndvi );
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *output1;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_ndvi;
-	int outfd1;
-	
-	char *ndvi;
-	char *emissivity;	
-	int i=0,j=0;
-	
-	void *inrast_ndvi;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_ndvi;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("emissivity, land flux, energy balance");
-	module->description = _("Emissivity from NDVI, generic method for spares land.");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->description=_("Name of the NDVI map [-]");
-
-	output1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	output1->description=_("Name of the output emissivity layer");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	ndvi	 	= input1->answer;
-		
-	result1  	= output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_ndvi;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_ndvi)){
-				G_set_d_null_value(&outrast1[col],1);
-			} else {
-				/****************************/
-				/* calculate emissivity	    */
-				d = emissivity_generic(d_ndvi);
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_ndvi);
-	G_close_cell (infd_ndvi);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.emissivity
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the emissivity from NDVI (empirical)
+ *                as seen in Caselles and Colles (1997). 
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double emissivity_generic(double ndvi);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *output1;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_ndvi;
+
+    int outfd1;
+
+    char *ndvi;
+
+    char *emissivity;
+
+    int i = 0, j = 0;
+
+    void *inrast_ndvi;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_ndvi;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("emissivity, land flux, energy balance");
+    module->description =
+	_("Emissivity from NDVI, generic method for spares land.");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->description = _("Name of the NDVI map [-]");
+    output1 = G_define_standard_option(G_OPT_R_INPUT);
+    output1->description = _("Name of the output emissivity layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    ndvi = input1->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_ndvi;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = ((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_ndvi)) {
+		G_set_d_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		
+
+				/****************************/ 
+		    /* calculate emissivity     */ 
+		    d = emissivity_generic(d_ndvi);
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_ndvi);
+    G_close_cell(infd_ndvi);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.evapo.PT/main.c
===================================================================
--- grass-addons/gipe/i.evapo.PT/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.PT/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 *
 * MODULE:	i.evapo.PT
@@ -23,275 +24,294 @@
 #include <grass/glocale.h>
 
 double pt_delta(double tempka);
+
 double pt_ghamma(double tempka, double p_atm);
-double pt_daily_et(double alpha_pt, double delta_pt, double ghamma_pt, double rnet, double g0, double tempka );
 
+double pt_daily_et(double alpha_pt, double delta_pt, double ghamma_pt,
+		   double rnet, double g0, double tempka);
+
 int main(int argc, char *argv[])
-{	
-	/* buffer for input-output rasters */
-	void *inrast_TEMPKA,*inrast_PATM,*inrast_RNET,*inrast_G0;
-	DCELL *outrast;
-	
-	/* pointers to input-output raster files */
-	int infd_TEMPKA,infd_PATM,infd_RNET,infd_G0;
-	int outfd;
+{
+    /* buffer for input-output rasters */
+    void *inrast_TEMPKA, *inrast_PATM, *inrast_RNET, *inrast_G0;
 
-	/* mapsets for input raster files */
-	char *mapset_TEMPKA,*mapset_PATM,*mapset_RNET,*mapset_G0;
+    DCELL *outrast;
 
-	/* names of input-output raster files */
-	char *RNET, *TEMPKA, *PATM, *G0;
-	char *ETa; 
+    /* pointers to input-output raster files */
+    int infd_TEMPKA, infd_PATM, infd_RNET, infd_G0;
 
-	/* input-output cell values */
-	DCELL d_tempka,d_pt_patm,d_rnet,d_g0;
-	DCELL d_pt_alpha, d_pt_delta, d_pt_ghamma, d_daily_et;
+    int outfd;
 
-	/* region informations and handler */
-	struct Cell_head cellhd;
-	int nrows, ncols;
-	int row, col;
+    /* mapsets for input raster files */
+    char *mapset_TEMPKA, *mapset_PATM, *mapset_RNET, *mapset_G0;
 
-	/* parser stuctures definition */
-	struct GModule *module;
-	struct Option *input_RNET,*input_TEMPKA, *input_PATM, *input_G0, *input_PT;
-	struct Option *output;
-	struct Flag *flag1, *zero;
-	struct Colors color;
-	struct History history;
+    /* names of input-output raster files */
+    char *RNET, *TEMPKA, *PATM, *G0;
 
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_tempka;
-	RASTER_MAP_TYPE data_type_patm;
-	RASTER_MAP_TYPE data_type_rnet;
-	RASTER_MAP_TYPE data_type_g0;
-	RASTER_MAP_TYPE data_type_eta;
+    char *ETa;
 
-	/* Initialize the GIS calls */
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description =
-		_("Evapotranspiration Calculation "
-		"Prestley and Taylor formulation, 1972.");
-	
-	/* Define different options */
-	input_RNET = G_define_standard_option(G_OPT_R_INPUT);
-	input_RNET->key			= "RNET";
-	input_RNET->key_desc		= "[W/m2]";
-	input_RNET->description 	= _("Name of Net Radiation raster map");
-	
-	input_G0 = G_define_standard_option(G_OPT_R_INPUT);
-	input_G0->key			= "G0";
-	input_G0->key_desc		= "[W/m2]";
-	input_G0->description		= _("Name of Soil Heat Flux raster map");
-		
-	input_TEMPKA = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPKA->key		= "TEMPKA";
-	input_TEMPKA->key_desc		= "[K]";
-	input_TEMPKA->description	= _("Name of air temperature raster map");
-		
-	input_PATM = G_define_standard_option(G_OPT_R_INPUT);
-	input_PATM->key			= "PATM";
-	input_PATM->key_desc		= "[millibars]";
-	input_PATM->description		= _("Name of Atmospheric Pressure raster map");
-	
-	input_PT = G_define_option();
-	input_PT->key			= "PT";
-	input_PT->key_desc		= "[-]";
-	input_PT->type			= TYPE_DOUBLE;
-	input_PT->required		= YES;
-	input_PT->gisprompt		= "old,cell,raster";
-	input_PT->description		= _("Prestley-Taylor Coefficient");
-	input_PT->answer		= "1.26";
-	
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key_desc		= "[mm/d]";
-	output->description		= _("Name of output Evapotranspiration layer");
-	
-	/* Define the different flags */
-	zero = G_define_flag() ;
-	zero->key			= 'z' ;
-	zero->description		= _("set negative ETa to zero");
-	
-	if (G_parser(argc, argv))
-		exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	RNET	= input_RNET->answer;
-	TEMPKA	= input_TEMPKA->answer;
-	PATM	= input_PATM->answer;
-	G0	= input_G0->answer;
-	d_pt_alpha = atof(input_PT->answer);
-	
-	ETa	= output->answer;
-	
-	/* find maps in mapset */
-	mapset_RNET = G_find_cell2 (RNET, "");
-	if (mapset_RNET == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), RNET);
-	mapset_TEMPKA = G_find_cell2 (TEMPKA, "");
-	if (mapset_TEMPKA == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPKA);
-	mapset_PATM = G_find_cell2 (PATM, "");
-	if (mapset_PATM == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), PATM);
-	mapset_G0 = G_find_cell2 (G0, "");
-	if (mapset_G0 == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), G0);
-	
-	/* check legal output name */ 
-	if (G_legal_filename (ETa) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), ETa);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_rnet = G_raster_map_type(RNET,mapset_RNET);
-	data_type_tempka = G_raster_map_type(TEMPKA,mapset_TEMPKA);
-	data_type_patm = G_raster_map_type(PATM,mapset_PATM);
-	data_type_g0 = G_raster_map_type(G0,mapset_G0);
-	
-	/* open pointers to input raster files */
-	if ( (infd_RNET = G_open_cell_old (RNET, mapset_RNET)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), RNET);
-	if ( (infd_TEMPKA = G_open_cell_old (TEMPKA, mapset_TEMPKA)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPKA);
-	if ( (infd_PATM = G_open_cell_old (PATM, mapset_PATM)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), PATM);
-	if ( (infd_G0 = G_open_cell_old (G0, mapset_G0)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), G0);
+    /* input-output cell values */
+    DCELL d_tempka, d_pt_patm, d_rnet, d_g0;
 
-	/* read headers of raster files */
-	if (G_get_cellhd (RNET, mapset_RNET, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), RNET);
-	if (G_get_cellhd (TEMPKA, mapset_TEMPKA, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPKA);
-	if (G_get_cellhd (PATM, mapset_PATM, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), PATM);
-	if (G_get_cellhd (G0, mapset_G0, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), G0);
+    DCELL d_pt_alpha, d_pt_delta, d_pt_ghamma, d_daily_et;
 
-	/* Allocate input buffer */
-	inrast_RNET  = G_allocate_raster_buf(data_type_rnet);
-	inrast_TEMPKA = G_allocate_raster_buf(data_type_tempka);
-	inrast_PATM = G_allocate_raster_buf(data_type_patm);
-	inrast_G0 = G_allocate_raster_buf(data_type_g0);
-	
-	/* get rows and columns number of the current region */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
+    /* region informations and handler */
+    struct Cell_head cellhd;
 
-	/* allocate output buffer */
-	outrast = G_allocate_raster_buf(data_type_output);
+    int nrows, ncols;
 
-	/* open pointers to output raster files */
-	if ( (outfd = G_open_raster_new (ETa,data_type_output)) < 0)
-		G_fatal_error (_("Could not open <%s>"),ETa);
-	
+    int row, col;
 
-	/* start the loop through cells */
-	for (row = 0; row < nrows; row++)
-	{
-				
-		G_percent(row, nrows, 2);
-		/* read input raster row into line buffer*/	
-		if (G_get_raster_row (infd_RNET, inrast_RNET, row,data_type_rnet) < 0)
-			G_fatal_error (_("Could not read from <%s>"),RNET);
-		if (G_get_raster_row (infd_TEMPKA, inrast_TEMPKA, row,data_type_tempka) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPKA);
-		if (G_get_raster_row (infd_PATM, inrast_PATM, row,data_type_patm) < 0)
-			G_fatal_error (_("Could not read from <%s>"),PATM);
-		if (G_get_raster_row (infd_G0, inrast_G0, row,data_type_g0) < 0)
-			G_fatal_error (_("Could not read from <%s>"),G0);
-		
-		for (col=0; col < ncols; col++)
-		{
-			/* read current cell from line buffer */
-			switch(data_type_rnet){
-				case CELL_TYPE:
-					d_rnet	= (double) ((CELL *) inrast_RNET)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnet	= (double) ((FCELL *) inrast_RNET)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnet	= ((DCELL *) inrast_RNET)[col];
-					break;
-			}
-			switch(data_type_tempka){
-				case CELL_TYPE:
-					d_tempka = (double) ((CELL *) inrast_TEMPKA)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempka = (double) ((FCELL *) inrast_TEMPKA)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempka = ((DCELL *) inrast_TEMPKA)[col];
-					break;
-			}
-			switch(data_type_patm){
-				case CELL_TYPE:
-					d_pt_patm = (double) ((CELL *) inrast_PATM)[col];
-					break;
-				case FCELL_TYPE:
-					d_pt_patm = (double) ((FCELL *) inrast_PATM)[col];
-					break;
-				case DCELL_TYPE:
-					d_pt_patm = ((DCELL *) inrast_PATM)[col];
-					break;
-			}
-			switch(data_type_g0){
-				case CELL_TYPE:
-					d_g0	= (double) ((CELL *) inrast_G0)[col];
-					break;
-				case FCELL_TYPE:
-					d_g0	= (double) ((FCELL *) inrast_G0)[col];
-					break;
-				case DCELL_TYPE:
-					d_g0	= ((DCELL *) inrast_G0)[col];
-					break;
-			}
+    /* parser stuctures definition */
+    struct GModule *module;
 
-			/*Delta_pt and Ghamma_pt*/
-			d_pt_delta = pt_delta( d_tempka);
-			d_pt_ghamma = pt_ghamma(d_tempka, d_pt_patm);
-			
-			/*Calculate ET*/
-			d_daily_et = pt_daily_et( d_pt_alpha, d_pt_delta, d_pt_ghamma, d_rnet, d_g0, d_tempka );
-			if (zero->answer && d_daily_et<0)
-				d_daily_et=0.0;
-			
-			/* write calculated ETP to output line buffer */
-			outrast[col] = d_daily_et;
-		}
-		
-		/* write output line buffer to output raster file */
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error (_("Cannot write to <%s>"), ETa);
-			
+    struct Option *input_RNET, *input_TEMPKA, *input_PATM, *input_G0,
+	*input_PT;
+    struct Option *output;
+
+    struct Flag *flag1, *zero;
+
+    struct Colors color;
+
+    struct History history;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
+    RASTER_MAP_TYPE data_type_tempka;
+
+    RASTER_MAP_TYPE data_type_patm;
+
+    RASTER_MAP_TYPE data_type_rnet;
+
+    RASTER_MAP_TYPE data_type_g0;
+
+    RASTER_MAP_TYPE data_type_eta;
+
+    /* Initialize the GIS calls */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description =
+	_("Evapotranspiration Calculation "
+	  "Prestley and Taylor formulation, 1972.");
+
+    /* Define different options */
+    input_RNET = G_define_standard_option(G_OPT_R_INPUT);
+    input_RNET->key = "RNET";
+    input_RNET->key_desc = "[W/m2]";
+    input_RNET->description = _("Name of Net Radiation raster map");
+
+    input_G0 = G_define_standard_option(G_OPT_R_INPUT);
+    input_G0->key = "G0";
+    input_G0->key_desc = "[W/m2]";
+    input_G0->description = _("Name of Soil Heat Flux raster map");
+
+    input_TEMPKA = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPKA->key = "TEMPKA";
+    input_TEMPKA->key_desc = "[K]";
+    input_TEMPKA->description = _("Name of air temperature raster map");
+
+    input_PATM = G_define_standard_option(G_OPT_R_INPUT);
+    input_PATM->key = "PATM";
+    input_PATM->key_desc = "[millibars]";
+    input_PATM->description = _("Name of Atmospheric Pressure raster map");
+
+    input_PT = G_define_option();
+    input_PT->key = "PT";
+    input_PT->key_desc = "[-]";
+    input_PT->type = TYPE_DOUBLE;
+    input_PT->required = YES;
+    input_PT->gisprompt = "old,cell,raster";
+    input_PT->description = _("Prestley-Taylor Coefficient");
+    input_PT->answer = "1.26";
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key_desc = "[mm/d]";
+    output->description = _("Name of output Evapotranspiration layer");
+
+    /* Define the different flags */
+    zero = G_define_flag();
+    zero->key = 'z';
+    zero->description = _("set negative ETa to zero");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    RNET = input_RNET->answer;
+    TEMPKA = input_TEMPKA->answer;
+    PATM = input_PATM->answer;
+    G0 = input_G0->answer;
+    d_pt_alpha = atof(input_PT->answer);
+
+    ETa = output->answer;
+
+    /* find maps in mapset */
+    mapset_RNET = G_find_cell2(RNET, "");
+    if (mapset_RNET == NULL)
+	G_fatal_error(_("cell file [%s] not found"), RNET);
+    mapset_TEMPKA = G_find_cell2(TEMPKA, "");
+    if (mapset_TEMPKA == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPKA);
+    mapset_PATM = G_find_cell2(PATM, "");
+    if (mapset_PATM == NULL)
+	G_fatal_error(_("cell file [%s] not found"), PATM);
+    mapset_G0 = G_find_cell2(G0, "");
+    if (mapset_G0 == NULL)
+	G_fatal_error(_("cell file [%s] not found"), G0);
+
+    /* check legal output name */
+    if (G_legal_filename(ETa) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), ETa);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_rnet = G_raster_map_type(RNET, mapset_RNET);
+    data_type_tempka = G_raster_map_type(TEMPKA, mapset_TEMPKA);
+    data_type_patm = G_raster_map_type(PATM, mapset_PATM);
+    data_type_g0 = G_raster_map_type(G0, mapset_G0);
+
+    /* open pointers to input raster files */
+    if ((infd_RNET = G_open_cell_old(RNET, mapset_RNET)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), RNET);
+    if ((infd_TEMPKA = G_open_cell_old(TEMPKA, mapset_TEMPKA)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPKA);
+    if ((infd_PATM = G_open_cell_old(PATM, mapset_PATM)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), PATM);
+    if ((infd_G0 = G_open_cell_old(G0, mapset_G0)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), G0);
+
+    /* read headers of raster files */
+    if (G_get_cellhd(RNET, mapset_RNET, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), RNET);
+    if (G_get_cellhd(TEMPKA, mapset_TEMPKA, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPKA);
+    if (G_get_cellhd(PATM, mapset_PATM, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), PATM);
+    if (G_get_cellhd(G0, mapset_G0, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), G0);
+
+    /* Allocate input buffer */
+    inrast_RNET = G_allocate_raster_buf(data_type_rnet);
+    inrast_TEMPKA = G_allocate_raster_buf(data_type_tempka);
+    inrast_PATM = G_allocate_raster_buf(data_type_patm);
+    inrast_G0 = G_allocate_raster_buf(data_type_g0);
+
+    /* get rows and columns number of the current region */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
+    /* allocate output buffer */
+    outrast = G_allocate_raster_buf(data_type_output);
+
+    /* open pointers to output raster files */
+    if ((outfd = G_open_raster_new(ETa, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), ETa);
+
+
+    /* start the loop through cells */
+    for (row = 0; row < nrows; row++) {
+
+	G_percent(row, nrows, 2);
+	/* read input raster row into line buffer */
+	if (G_get_raster_row(infd_RNET, inrast_RNET, row, data_type_rnet) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), RNET);
+	if (G_get_raster_row
+	    (infd_TEMPKA, inrast_TEMPKA, row, data_type_tempka) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPKA);
+	if (G_get_raster_row(infd_PATM, inrast_PATM, row, data_type_patm) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), PATM);
+	if (G_get_raster_row(infd_G0, inrast_G0, row, data_type_g0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), G0);
+
+	for (col = 0; col < ncols; col++) {
+	    /* read current cell from line buffer */
+	    switch (data_type_rnet) {
+	    case CELL_TYPE:
+		d_rnet = (double)((CELL *) inrast_RNET)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnet = (double)((FCELL *) inrast_RNET)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnet = ((DCELL *) inrast_RNET)[col];
+		break;
+	    }
+	    switch (data_type_tempka) {
+	    case CELL_TYPE:
+		d_tempka = (double)((CELL *) inrast_TEMPKA)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempka = (double)((FCELL *) inrast_TEMPKA)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempka = ((DCELL *) inrast_TEMPKA)[col];
+		break;
+	    }
+	    switch (data_type_patm) {
+	    case CELL_TYPE:
+		d_pt_patm = (double)((CELL *) inrast_PATM)[col];
+		break;
+	    case FCELL_TYPE:
+		d_pt_patm = (double)((FCELL *) inrast_PATM)[col];
+		break;
+	    case DCELL_TYPE:
+		d_pt_patm = ((DCELL *) inrast_PATM)[col];
+		break;
+	    }
+	    switch (data_type_g0) {
+	    case CELL_TYPE:
+		d_g0 = (double)((CELL *) inrast_G0)[col];
+		break;
+	    case FCELL_TYPE:
+		d_g0 = (double)((FCELL *) inrast_G0)[col];
+		break;
+	    case DCELL_TYPE:
+		d_g0 = ((DCELL *) inrast_G0)[col];
+		break;
+	    }
+
+	    /*Delta_pt and Ghamma_pt */
+	    d_pt_delta = pt_delta(d_tempka);
+	    d_pt_ghamma = pt_ghamma(d_tempka, d_pt_patm);
+
+	    /*Calculate ET */
+	    d_daily_et =
+		pt_daily_et(d_pt_alpha, d_pt_delta, d_pt_ghamma, d_rnet, d_g0,
+			    d_tempka);
+	    if (zero->answer && d_daily_et < 0)
+		d_daily_et = 0.0;
+
+	    /* write calculated ETP to output line buffer */
+	    outrast[col] = d_daily_et;
 	}
-	/* free buffers and close input maps */
 
-	G_free(inrast_RNET);
-	G_free(inrast_TEMPKA);
-	G_free(inrast_PATM);
-	G_free(inrast_G0);
-	G_close_cell (infd_RNET);
-	G_close_cell (infd_TEMPKA);
-	G_close_cell (infd_PATM);
-	G_close_cell (infd_G0);
-	
-	/* generate color table between -20 and 20 */
-	G_make_rainbow_colors(&color, -20, 20);
-	G_write_colors(ETa,G_mapset(),&color);
+	/* write output line buffer to output raster file */
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), ETa);
 
-	G_short_history(ETa,"raster", &history);
-	G_command_history(&history);
-	G_write_history(ETa, &history);
+    }
+    /* free buffers and close input maps */
 
-	/* free buffers and close output map */
-	G_free(outrast);
-	G_close_cell (outfd);
+    G_free(inrast_RNET);
+    G_free(inrast_TEMPKA);
+    G_free(inrast_PATM);
+    G_free(inrast_G0);
+    G_close_cell(infd_RNET);
+    G_close_cell(infd_TEMPKA);
+    G_close_cell(infd_PATM);
+    G_close_cell(infd_G0);
 
-	return (0);
+    /* generate color table between -20 and 20 */
+    G_make_rainbow_colors(&color, -20, 20);
+    G_write_colors(ETa, G_mapset(), &color);
+
+    G_short_history(ETa, "raster", &history);
+    G_command_history(&history);
+    G_write_history(ETa, &history);
+
+    /* free buffers and close output map */
+    G_free(outrast);
+    G_close_cell(outfd);
+
+    return (0);
 }
-

Modified: grass-addons/gipe/i.evapo.PT/pt_daily_et.c
===================================================================
--- grass-addons/gipe/i.evapo.PT/pt_daily_et.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.PT/pt_daily_et.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,25 +1,30 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*Prestely and Taylor, 1972. */
-
-double pt_daily_et(double alpha_pt,double delta_pt,double ghamma_pt,double rnet,double g0,double tempka)
-{
-	double 	result, latentHv, t_celsius;
-	double	roh_w=1004.15;/*mass density of water*/
-	double	vap_slope_ratio;
-
-	/*Latent Heat of vaporization (W/m2/d)*/
- 	t_celsius = tempka - 273.15 ;
-	latentHv    = 86400/((2.501-0.002361*t_celsius)*pow(10,6));
-	/* Ratio of slope of saturation-vapour pressure Vs Temperature*/
-	/* ghamma_pt = psychrometric constant */
-	vap_slope_ratio = delta_pt / ( delta_pt + ghamma_pt );
-
-	/*(Rn-g0)/latentHv returns [-]*/
-	result = (alpha_pt/roh_w) * vap_slope_ratio * (rnet-g0)/latentHv;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*Prestely and Taylor, 1972. */ 
+double pt_daily_et(double alpha_pt, double delta_pt, double ghamma_pt,
+		    double rnet, double g0, double tempka) 
+{
+    double result, latentHv, t_celsius;
+
+    double roh_w = 1004.15;	/*mass density of water */
+
+    double vap_slope_ratio;
+
+    
+	/*Latent Heat of vaporization (W/m2/d) */ 
+	t_celsius = tempka - 273.15;
+    latentHv = 86400 / ((2.501 - 0.002361 * t_celsius) * pow(10, 6));
+    
+	/* Ratio of slope of saturation-vapour pressure Vs Temperature */ 
+	/* ghamma_pt = psychrometric constant */ 
+	vap_slope_ratio = delta_pt / (delta_pt + ghamma_pt);
+    
+	/*(Rn-g0)/latentHv returns [-] */ 
+	result =
+	(alpha_pt / roh_w) * vap_slope_ratio * (rnet - g0) / latentHv;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.PT/pt_delta.c
===================================================================
--- grass-addons/gipe/i.evapo.PT/pt_delta.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.PT/pt_delta.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,21 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Prestely and Taylor, 1972. 
- * INPUT in Kelvin
- */
-
-double pt_delta(double tempka)
-{
-	double 	a, b, result;
-	
-	tempka -= 273.15;/*Celsius*/
-	b = tempka + 237.3 ;
-	a = ( 17.27 * tempka ) /  b ;
-
-	result = 2504.0 * exp(a) / pow(b,2) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Prestely and Taylor, 1972. 
+     * INPUT in Kelvin
+     */ 
+double pt_delta(double tempka) 
+{
+    double a, b, result;
+
+    tempka -= 273.15;		/*Celsius */
+    b = tempka + 237.3;
+    a = (17.27 * tempka) / b;
+    result = 2504.0 * exp(a) / pow(b, 2);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.PT/pt_ghamma.c
===================================================================
--- grass-addons/gipe/i.evapo.PT/pt_ghamma.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.PT/pt_ghamma.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Prestely and Taylor, 1972. 
- * INPUT in Kelvin
- */
-
-double pt_ghamma(double tempka, double patm_pt)
-{
-	double 	a, result;
-	double Cp = 1004.16;
-
-	tempka -= 273.15;
-
-	a = 0.622 * pow(10,7) * (2.501 - (2.361 * pow(10,-3) * tempka));
-
-	result = Cp * patm_pt / a ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Prestely and Taylor, 1972. 
+     * INPUT in Kelvin
+     */ 
+double pt_ghamma(double tempka, double patm_pt) 
+{
+    double a, result;
+
+    double Cp = 1004.16;
+
+    tempka -= 273.15;
+    a = 0.622 * pow(10, 7) * (2.501 - (2.361 * pow(10, -3) * tempka));
+    result = Cp * patm_pt / a;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/et_pot_day.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/et_pot_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/et_pot_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,16 +1,15 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Potential ET after Bastiaanssen (1995)*/
-
-double et_pot_day( double rnetd, double tempk, double roh_w )
-{
-	double latent, result;
-
-	latent=(2.501-(0.002361*(tempk-273.15)))*1000000.0;
-	result = (rnetd*86400*1000.0)/(latent*roh_w);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Potential ET after Bastiaanssen (1995) */ 
+double et_pot_day(double rnetd, double tempk, double roh_w) 
+{
+    double latent, result;
+
+    latent = (2.501 - (0.002361 * (tempk - 273.15))) * 1000000.0;
+    result = (rnetd * 86400 * 1000.0) / (latent * roh_w);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/evapfr_senay.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/evapfr_senay.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/evapfr_senay.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Evaporative fraction from Senay (2007)*/
-
-double evapfr_senay( double t_hot, double t_cold, double tempk)
-{
-	double result;
-
-	result = (t_hot-tempk)/(t_hot-t_cold);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Evaporative fraction from Senay (2007) */ 
+double evapfr_senay(double t_hot, double t_cold, double tempk) 
+{
+    double result;
+
+    result = (t_hot - tempk) / (t_hot - t_cold);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/main.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,671 +1,747 @@
-/****************************************************************************
- *
- * MODULE:       i.evapo.SENAY
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Creates a map of actual evapotranspiration following
- *               the method of Senay (2007).
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double solar_day(double lat, double doy, double tsw );
-double solar_day_3d(double lat, double doy, double tsw, double slope, double aspect);
-double r_net_day( double bbalb, double solar, double tsw );
-double r_net_day_bandara98( double surface_albedo, double solar_day, double apparent_atm_emissivity, double surface_emissivity, double air_temperature );
-double et_pot_day( double rnetd, double tempk, double roh_w );
-double evapfr_senay( double t_hot, double t_cold, double tempk);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *input5, *input6, *input7, *input8, *input9;
-	struct Option *input10, *input11, *input12, *input13;
-	struct Option *output1, *output2;
-	
-	struct Flag *flag2, *flag3;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1,*result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_albedo, infd_tempk, infd_dem;
-	int infd_lat, infd_doy, infd_tsw;
-	int infd_slope, infd_aspect;
-	int infd_tair, infd_e0;
-	int infd_ndvi;
-	int outfd1, outfd2;
-	
-	char *albedo,*tempk,*dem,*lat,*doy,*tsw;
-	char *slope,*aspect,*tair,*e0, *ndvi;
-	double roh_w, e_atm;
-	int i=0,j=0;
-	
-	void *inrast_albedo, *inrast_tempk;
-	void *inrast_dem, *inrast_lat;
-	void *inrast_doy, *inrast_tsw;
-	void *inrast_slope, *inrast_aspect;
-	void *inrast_tair, *inrast_e0;
-	void *inrast_ndvi;
-
-	DCELL *outrast1, *outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_dem;
-	RASTER_MAP_TYPE data_type_lat;
-	RASTER_MAP_TYPE data_type_doy;
-	RASTER_MAP_TYPE data_type_tsw;
-	RASTER_MAP_TYPE data_type_slope;
-	RASTER_MAP_TYPE data_type_aspect;
-	RASTER_MAP_TYPE data_type_tair;
-	RASTER_MAP_TYPE data_type_e0;
-	RASTER_MAP_TYPE data_type_ndvi;
-	/********************************/
-	/* Stats for Senay equation	*/
-	double t0dem_min=400.0,t0dem_max=200.0;
-	double tempk_min=400.0,tempk_max=200.0;
-	/********************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("Actual ET, evapotranspiration, Senay");
-	module->description = _("Actual evapotranspiration, method after Senay (2007)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("albedo");
-	input1->description=_("Name of the Albedo layer [0.0-1.0]");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("tempk");
-	input2->description=_("Name of the temperature layer [Degree Kelvin]");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("dem");
-	input3->description=_("Name of the elevation layer [m]");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("lat");
-	input4->description=_("Name of the degree latitude layer [dd.ddd]");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("doy");
-	input5->description=_("Name of the Day of Year layer [0.0-366.0]");
-
-	input6 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input6->key        =_("tsw");
-	input6->description=_("Name of the single-way transmissivity layer [0.0-1.0]");
-
-	input7 = G_define_option() ;
-	input7->key        =_("roh_w");
-	input7->type       = TYPE_DOUBLE;
-	input7->required   = YES;
-	input7->description=_("Value of the density of fresh water ~[1000-1020]");
-	input7->answer     =_("1005.0");
-
-	input8 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input8->key        =_("slope");
-	input8->required   = NO;
-	input8->description=_("Name of the Slope layer ~[0-90]");
-	input8->guisection = _("Optional");
-
-	input9 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input9->key        =_("aspect");
-	input9->required   = NO;
-	input9->description=_("Name of the Aspect layer ~[0-360]");
-	input9->guisection = _("Optional");
-
-	input10 = G_define_option() ;
-	input10->key        =_("e_atm");
-	input10->type       = TYPE_DOUBLE;
-	input10->required   = NO;
-	input10->description=_("Value of the apparent atmospheric emissivity (Bandara, 1998 used 0.845 for Sri Lanka)");
-	input10->guisection = _("Optional");
-
-	input11 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input11->key        =_("t_air");
-	input11->required   = NO;
-	input11->description=_("Name of the Air Temperature layer [Kelvin], use with -b");
-	input11->guisection = _("Optional");
-
-	input12 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input12->key        =_("e0");
-	input12->required   = NO;
-	input12->description=_("Name of the Surface Emissivity layer [-], use with -b");
-	input12->guisection = _("Optional");
-
-	input13 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input13->key        =_("ndvi");
-	input13->description=_("Name of the NDVI layer [-]");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output Actual ET layer");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("evapfr");
-	output2->required   = NO;
-	output2->description=_("Name of the output evaporative fraction layer");
-	output2->guisection = _("Optional");
-
-	flag2 = G_define_flag();
-	flag2->key = 'd';
-	flag2->description = _("Slope/Aspect correction");
-
-	flag3 = G_define_flag();
-	flag3->key = 'b';
-	flag3->description = _("Net Radiation Bandara (1998), generic Longwave calculation, need apparent atmospheric emissivity, Air temperature and surface emissivity inputs");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	albedo	 	= input1->answer;
-	tempk	 	= input2->answer;
-	dem	 	= input3->answer;
-	lat		= input4->answer;
-	doy	 	= input5->answer;
-	tsw	 	= input6->answer;
-	roh_w	 	= atof(input7->answer);
-	slope	 	= input8->answer;
-	aspect	 	= input9->answer;
-	if(input10->answer){
-		e_atm 	= atof(input10->answer);
-	}
-	tair	 	= input11->answer;
-	e0	 	= input12->answer;
-	ndvi	 	= input13->answer;
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	/***************************************************/
-	mapset = G_find_cell2(albedo, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), albedo);
-	}
-	data_type_albedo = G_raster_map_type(albedo,mapset);
-	if ( (infd_albedo = G_open_cell_old (albedo,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), albedo);
-	if (G_get_cellhd (albedo, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), albedo);
-	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (dem, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), dem);
-	}
-	data_type_dem = G_raster_map_type(dem,mapset);
-	if ( (infd_dem = G_open_cell_old (dem,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), dem);
-	if (G_get_cellhd (dem, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), dem);
-	inrast_dem = G_allocate_raster_buf(data_type_dem);
-	/***************************************************/
-	mapset = G_find_cell2 (lat, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), lat);
-	}
-	data_type_lat = G_raster_map_type(lat,mapset);
-	if ( (infd_lat = G_open_cell_old (lat,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), lat);
-	if (G_get_cellhd (lat, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), lat);
-	inrast_lat = G_allocate_raster_buf(data_type_lat);
-	/***************************************************/
-	mapset = G_find_cell2 (doy, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), doy);
-	}
-	data_type_doy = G_raster_map_type(doy,mapset);
-	if ( (infd_doy = G_open_cell_old (doy,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), doy);
-	if (G_get_cellhd (doy, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), doy);
-	inrast_doy = G_allocate_raster_buf(data_type_doy);
-	/***************************************************/
-	mapset = G_find_cell2 (tsw, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tsw);
-	}
-	data_type_tsw = G_raster_map_type(tsw,mapset);
-	if ( (infd_tsw = G_open_cell_old (tsw,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tsw);
-	if (G_get_cellhd (tsw, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tsw);
-	inrast_tsw = G_allocate_raster_buf(data_type_tsw);
-	/***************************************************/
-	if(flag2->answer){
-		mapset = G_find_cell2 (slope, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), slope);
-		}
-		data_type_slope = G_raster_map_type(slope,mapset);
-		if ( (infd_slope = G_open_cell_old (slope,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), slope);
-		if (G_get_cellhd (slope, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), slope);
-		inrast_slope = G_allocate_raster_buf(data_type_slope);
-	}
-	/***************************************************/
-	if(flag2->answer){
-		mapset = G_find_cell2 (aspect, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), aspect);
-		}
-		data_type_aspect = G_raster_map_type(aspect,mapset);
-		if ( (infd_aspect = G_open_cell_old (aspect,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), aspect);
-		if (G_get_cellhd (aspect, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), aspect);
-		inrast_aspect = G_allocate_raster_buf(data_type_aspect);
-	}
-	/***************************************************/
-	if(flag3->answer){
-		mapset = G_find_cell2 (tair, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), tair);
-		}
-		data_type_tair = G_raster_map_type(tair,mapset);
-		if ( (infd_tair = G_open_cell_old (tair,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), tair);
-		if (G_get_cellhd (tair, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), tair);
-		inrast_tair = G_allocate_raster_buf(data_type_tair);
-	}
-	/***************************************************/
-	if(flag3->answer){
-		mapset = G_find_cell2 (e0, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), e0);
-		}
-		data_type_e0 = G_raster_map_type(e0,mapset);
-		if ( (infd_e0 = G_open_cell_old (e0,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), e0);
-		if (G_get_cellhd (e0, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), e0);
-		inrast_e0 = G_allocate_raster_buf(data_type_e0);
-	}
-	/***************************************************/
-	mapset = G_find_cell2 (ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(result2){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(result2){
-		if ((outfd2 = G_open_raster_new (result2,data_type_output))< 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	/* Process tempk min / max pixels for SENAY Evapfr */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_albedo;
-		DCELL d_tempk;
-		DCELL d_dem;
-		DCELL d_t0dem;
-		DCELL d_ndvi;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_dem,inrast_dem,row,data_type_dem)<0)
-			G_fatal_error(_("Could not read from <%s>"),dem);
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_dem){
-				case CELL_TYPE:
-					d_dem = (double) ((CELL *) inrast_dem)[col];
-					break;
-				case FCELL_TYPE:
-					d_dem = (double) ((FCELL *) inrast_dem)[col];
-					break;
-				case DCELL_TYPE:
-					d_dem = (double) ((DCELL *) inrast_dem)[col];
-					break;
-			}
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = (double) ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_tempk)||
-			G_is_d_null_value(&d_dem)||
-			G_is_d_null_value(&d_ndvi)){
-				/* do nothing */ 
-			} else {
-				d_t0dem = d_tempk + 0.00649*d_dem;
-				if(d_t0dem<0.0||d_albedo<0.001){
-					/* do nothing */ 
-				} else {
-					if(d_tempk<tempk_min&&
-					d_albedo<0.1){
-						t0dem_min=d_t0dem;
-						tempk_min=d_tempk;
-					}else if(d_tempk>tempk_max&&
-						d_albedo>0.3){
-						t0dem_max=d_t0dem;
-						tempk_max=d_tempk;
-					}
-				}
-			}
-		}
-	}
-	G_message("tempk_min=%f\ntempk_max=%f\n",tempk_min, tempk_max);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_albedo;
-		DCELL d_tempk;
-		DCELL d_lat;
-		DCELL d_doy;
-		DCELL d_tsw;
-		DCELL d_solar;
-		DCELL d_rnetd;
-		DCELL d_slope;
-		DCELL d_aspect;
-		DCELL d_tair;
-		DCELL d_e0;
-		DCELL d_ndvi;
-		DCELL d_etpotd;
-		DCELL d_evapfr;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_lat,inrast_lat,row,data_type_lat)<0)
-			G_fatal_error(_("Could not read from <%s>"),lat);
-		if(G_get_raster_row(infd_doy,inrast_doy,row,data_type_doy)<0)
-			G_fatal_error(_("Could not read from <%s>"),doy);
-		if(G_get_raster_row(infd_tsw,inrast_tsw,row,data_type_tsw)<0)
-			G_fatal_error(_("Could not read from <%s>"),tsw);
-		if(flag2->answer){
-			if(G_get_raster_row(infd_slope,inrast_slope,row,data_type_slope)<0)
-				G_fatal_error(_("Could not read from <%s>"),slope);
-			if(G_get_raster_row(infd_aspect,inrast_aspect,row,data_type_aspect)<0)
-				G_fatal_error(_("Could not read from <%s>"),aspect);
-		}
-		if(flag3->answer){
-			if(G_get_raster_row(infd_tair,inrast_tair,row,data_type_tair)<0)
-				G_fatal_error(_("Could not read from <%s>"),tair);
-			if(G_get_raster_row(infd_e0,inrast_e0,row,data_type_e0)<0)
-				G_fatal_error(_("Could not read from <%s>"),e0);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_lat){
-				case CELL_TYPE:
-					d_lat = (double) ((CELL *) inrast_lat)[col];
-					break;
-				case FCELL_TYPE:
-					d_lat = (double) ((FCELL *) inrast_lat)[col];
-					break;
-				case DCELL_TYPE:
-					d_lat = (double) ((DCELL *) inrast_lat)[col];
-					break;
-			}
-			switch(data_type_doy){
-				case CELL_TYPE:
-					d_doy = (double) ((CELL *) inrast_doy)[col];
-					break;
-				case FCELL_TYPE:
-					d_doy = (double) ((FCELL *) inrast_doy)[col];
-					break;
-				case DCELL_TYPE:
-					d_doy = (double) ((DCELL *) inrast_doy)[col];
-					break;
-			}
-			switch(data_type_tsw){
-				case CELL_TYPE:
-					d_tsw = (double) ((CELL *) inrast_tsw)[col];
-					break;
-				case FCELL_TYPE:
-					d_tsw = (double) ((FCELL *) inrast_tsw)[col];
-					break;
-				case DCELL_TYPE:
-					d_tsw = (double) ((DCELL *) inrast_tsw)[col];
-					break;
-			}
-			if(flag2->answer){
-				switch(data_type_slope){
-					case CELL_TYPE:
-						d_slope = (double) ((CELL *) inrast_slope)[col];
-						break;
-					case FCELL_TYPE:
-						d_slope = (double) ((FCELL *) inrast_slope)[col];
-						break;
-					case DCELL_TYPE:
-						d_slope = (double) ((DCELL *) inrast_slope)[col];
-						break;
-				}
-				switch(data_type_aspect){
-					case CELL_TYPE:
-						d_aspect = (double) ((CELL *) inrast_aspect)[col];
-						break;
-					case FCELL_TYPE:
-						d_aspect = (double) ((FCELL *) inrast_aspect)[col];
-						break;
-					case DCELL_TYPE:
-						d_aspect = (double) ((DCELL *) inrast_aspect)[col];
-						break;
-				}
-			}
-			if(flag3->answer){
-				switch(data_type_tair){
-					case CELL_TYPE:
-						d_tair = (double) ((CELL *) inrast_tair)[col];
-						break;
-					case FCELL_TYPE:
-						d_tair = (double) ((FCELL *) inrast_tair)[col];
-						break;
-					case DCELL_TYPE:
-						d_tair = (double) ((DCELL *) inrast_tair)[col];
-						break;
-				}
-				switch(data_type_e0){
-					case CELL_TYPE:
-						d_e0 = (double) ((CELL *) inrast_e0)[col];
-						break;
-					case FCELL_TYPE:
-						d_e0 = (double) ((FCELL *) inrast_e0)[col];
-						break;
-					case DCELL_TYPE:
-						d_e0 = (double) ((DCELL *) inrast_e0)[col];
-						break;
-				}
-			}
-			if(G_is_d_null_value(&d_albedo)||
-			G_is_d_null_value(&d_tempk)||
-			d_tempk<10.0||
-			G_is_d_null_value(&d_lat)||
-			G_is_d_null_value(&d_doy)||
-			G_is_d_null_value(&d_tsw)||
-			G_is_d_null_value(&d_ndvi)||
-			((flag2->answer)&&G_is_d_null_value(&d_slope))||
-			((flag2->answer)&&G_is_d_null_value(&d_aspect))||
-			((flag3->answer)&&G_is_d_null_value(&d_tair))||
-			((flag3->answer)&&G_is_d_null_value(&d_e0))){
-				G_set_d_null_value(&outrast1[col],1);
-				if (result2)
-					G_set_d_null_value(&outrast2[col],1);
-			} else {
-				if(flag2->answer){
-					d_solar = solar_day_3d(d_lat,d_doy,d_tsw,d_slope,d_aspect);
-				} else {
-					d_solar = solar_day(d_lat, d_doy, d_tsw );
-				}
-				d_evapfr=evapfr_senay(tempk_max,tempk_min, d_tempk);
-				/*If water then no water stress*/
-				if(d_albedo<=0.1&&d_ndvi<=0.0){
-					d_evapfr=1.0;
-				}
-				/*some points are colder than selected low*/
-				if(d_evapfr>1.0){
-					d_evapfr=1.0;
-				}
-				if(result2){
-					outrast2[col] = d_evapfr;
-				}
-				if(flag3->answer){
-					d_rnetd = r_net_day_bandara98(d_albedo,d_solar,e_atm,d_e0,d_tair);
-				} else {
-					d_rnetd = r_net_day(d_albedo,d_solar,d_tsw);
-				}
-				d_etpotd = et_pot_day(d_rnetd,d_tempk,roh_w);
-				d = d_etpotd * d_evapfr;
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-		if(result2){
-			if (G_put_raster_row (outfd2, outrast2, data_type_output) < 0)
-				G_fatal_error(_("Cannot write to output raster file"));
-		}
-	}
-
-	G_free (inrast_albedo);
-	G_free (inrast_tempk);
-	G_free (inrast_dem);
-	G_free (inrast_lat);
-	G_free (inrast_doy);
-	G_free (inrast_tsw);
-	G_close_cell (infd_albedo);
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_dem);
-	G_close_cell (infd_lat);
-	G_close_cell (infd_doy);
-	G_close_cell (infd_tsw);
-	
-	if (flag3->answer){
-		G_free (inrast_tair);
-		G_close_cell (infd_tair);
-		G_free (inrast_e0);
-		G_close_cell (infd_e0);
-	}
-
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	if (result2){
-		G_free (outrast2);
-		G_close_cell (outfd2);
-		/* Color table for evaporative fraction */
-		G_init_colors(&colors);
-		G_add_color_rule(0,0,0,0,1,255,255,255,&colors);
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-
-	/* Color table for evapotranspiration */
-	G_init_colors(&colors);
-	G_add_color_rule(0,0,0,0,10,255,255,255,&colors);
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.evapo.SENAY
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Creates a map of actual evapotranspiration following
+ *               the method of Senay (2007).
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double solar_day(double lat, double doy, double tsw);
+
+double solar_day_3d(double lat, double doy, double tsw, double slope,
+		     double aspect);
+double r_net_day(double bbalb, double solar, double tsw);
+
+double r_net_day_bandara98(double surface_albedo, double solar_day,
+			    double apparent_atm_emissivity,
+			    double surface_emissivity,
+			    double air_temperature);
+double et_pot_day(double rnetd, double tempk, double roh_w);
+
+double evapfr_senay(double t_hot, double t_cold, double tempk);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *input5, *input6, *input7, *input8, *input9;
+
+    struct Option *input10, *input11, *input12, *input13;
+
+    struct Option *output1, *output2;
+
+    struct Flag *flag2, *flag3;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_albedo, infd_tempk, infd_dem;
+
+    int infd_lat, infd_doy, infd_tsw;
+
+    int infd_slope, infd_aspect;
+
+    int infd_tair, infd_e0;
+
+    int infd_ndvi;
+
+    int outfd1, outfd2;
+
+    char *albedo, *tempk, *dem, *lat, *doy, *tsw;
+
+    char *slope, *aspect, *tair, *e0, *ndvi;
+
+    double roh_w, e_atm;
+
+    int i = 0, j = 0;
+
+    void *inrast_albedo, *inrast_tempk;
+
+    void *inrast_dem, *inrast_lat;
+
+    void *inrast_doy, *inrast_tsw;
+
+    void *inrast_slope, *inrast_aspect;
+
+    void *inrast_tair, *inrast_e0;
+
+    void *inrast_ndvi;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_albedo;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_dem;
+    RASTER_MAP_TYPE data_type_lat;
+    RASTER_MAP_TYPE data_type_doy;
+    RASTER_MAP_TYPE data_type_tsw;
+    RASTER_MAP_TYPE data_type_slope;
+    RASTER_MAP_TYPE data_type_aspect;
+    RASTER_MAP_TYPE data_type_tair;
+    RASTER_MAP_TYPE data_type_e0;
+    RASTER_MAP_TYPE data_type_ndvi;
+    
+
+	/********************************/ 
+	/* Stats for Senay equation     */ 
+    double t0dem_min = 400.0, t0dem_max = 200.0;
+
+    double tempk_min = 400.0, tempk_max = 200.0;
+
+    
+
+	/********************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("Actual ET, evapotranspiration, Senay");
+    module->description =
+	_("Actual evapotranspiration, method after Senay (2007)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("albedo");
+    input1->description = _("Name of the Albedo layer [0.0-1.0]");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("tempk");
+    input2->description = _("Name of the temperature layer [Degree Kelvin]");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("dem");
+    input3->description = _("Name of the elevation layer [m]");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("lat");
+    input4->description = _("Name of the degree latitude layer [dd.ddd]");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("doy");
+    input5->description = _("Name of the Day of Year layer [0.0-366.0]");
+    input6 = G_define_standard_option(G_OPT_R_INPUT);
+    input6->key = _("tsw");
+    input6->description =
+	_("Name of the single-way transmissivity layer [0.0-1.0]");
+    input7 = G_define_option();
+    input7->key = _("roh_w");
+    input7->type = TYPE_DOUBLE;
+    input7->required = YES;
+    input7->description =
+	_("Value of the density of fresh water ~[1000-1020]");
+    input7->answer = _("1005.0");
+    input8 = G_define_standard_option(G_OPT_R_INPUT);
+    input8->key = _("slope");
+    input8->required = NO;
+    input8->description = _("Name of the Slope layer ~[0-90]");
+    input8->guisection = _("Optional");
+    input9 = G_define_standard_option(G_OPT_R_INPUT);
+    input9->key = _("aspect");
+    input9->required = NO;
+    input9->description = _("Name of the Aspect layer ~[0-360]");
+    input9->guisection = _("Optional");
+    input10 = G_define_option();
+    input10->key = _("e_atm");
+    input10->type = TYPE_DOUBLE;
+    input10->required = NO;
+    input10->description =
+	_("Value of the apparent atmospheric emissivity (Bandara, 1998 used 0.845 for Sri Lanka)");
+    input10->guisection = _("Optional");
+    input11 = G_define_standard_option(G_OPT_R_INPUT);
+    input11->key = _("t_air");
+    input11->required = NO;
+    input11->description =
+	_("Name of the Air Temperature layer [Kelvin], use with -b");
+    input11->guisection = _("Optional");
+    input12 = G_define_standard_option(G_OPT_R_INPUT);
+    input12->key = _("e0");
+    input12->required = NO;
+    input12->description =
+	_("Name of the Surface Emissivity layer [-], use with -b");
+    input12->guisection = _("Optional");
+    input13 = G_define_standard_option(G_OPT_R_INPUT);
+    input13->key = _("ndvi");
+    input13->description = _("Name of the NDVI layer [-]");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output Actual ET layer");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("evapfr");
+    output2->required = NO;
+    output2->description =
+	_("Name of the output evaporative fraction layer");
+    output2->guisection = _("Optional");
+    flag2 = G_define_flag();
+    flag2->key = 'd';
+    flag2->description = _("Slope/Aspect correction");
+    flag3 = G_define_flag();
+    flag3->key = 'b';
+    flag3->description =
+	_("Net Radiation Bandara (1998), generic Longwave calculation, need apparent atmospheric emissivity, Air temperature and surface emissivity inputs");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    albedo = input1->answer;
+    tempk = input2->answer;
+    dem = input3->answer;
+    lat = input4->answer;
+    doy = input5->answer;
+    tsw = input6->answer;
+    roh_w = atof(input7->answer);
+    slope = input8->answer;
+    aspect = input9->answer;
+    if (input10->answer) {
+	e_atm = atof(input10->answer);
+    }
+    tair = input11->answer;
+    e0 = input12->answer;
+    ndvi = input13->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(albedo, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), albedo);
+    }
+    data_type_albedo = G_raster_map_type(albedo, mapset);
+    if ((infd_albedo = G_open_cell_old(albedo, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+    if (G_get_cellhd(albedo, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), albedo);
+    inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(dem, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), dem);
+    }
+    data_type_dem = G_raster_map_type(dem, mapset);
+    if ((infd_dem = G_open_cell_old(dem, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), dem);
+    if (G_get_cellhd(dem, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), dem);
+    inrast_dem = G_allocate_raster_buf(data_type_dem);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lat, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), lat);
+    }
+    data_type_lat = G_raster_map_type(lat, mapset);
+    if ((infd_lat = G_open_cell_old(lat, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lat);
+    if (G_get_cellhd(lat, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), lat);
+    inrast_lat = G_allocate_raster_buf(data_type_lat);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(doy, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), doy);
+    }
+    data_type_doy = G_raster_map_type(doy, mapset);
+    if ((infd_doy = G_open_cell_old(doy, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), doy);
+    if (G_get_cellhd(doy, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), doy);
+    inrast_doy = G_allocate_raster_buf(data_type_doy);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tsw, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tsw);
+    }
+    data_type_tsw = G_raster_map_type(tsw, mapset);
+    if ((infd_tsw = G_open_cell_old(tsw, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tsw);
+    if (G_get_cellhd(tsw, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tsw);
+    inrast_tsw = G_allocate_raster_buf(data_type_tsw);
+    
+
+	/***************************************************/ 
+	if (flag2->answer) {
+	mapset = G_find_cell2(slope, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), slope);
+	}
+	data_type_slope = G_raster_map_type(slope, mapset);
+	if ((infd_slope = G_open_cell_old(slope, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), slope);
+	if (G_get_cellhd(slope, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), slope);
+	inrast_slope = G_allocate_raster_buf(data_type_slope);
+    }
+    
+
+	/***************************************************/ 
+	if (flag2->answer) {
+	mapset = G_find_cell2(aspect, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), aspect);
+	}
+	data_type_aspect = G_raster_map_type(aspect, mapset);
+	if ((infd_aspect = G_open_cell_old(aspect, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), aspect);
+	if (G_get_cellhd(aspect, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), aspect);
+	inrast_aspect = G_allocate_raster_buf(data_type_aspect);
+    }
+    
+
+	/***************************************************/ 
+	if (flag3->answer) {
+	mapset = G_find_cell2(tair, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), tair);
+	}
+	data_type_tair = G_raster_map_type(tair, mapset);
+	if ((infd_tair = G_open_cell_old(tair, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), tair);
+	if (G_get_cellhd(tair, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), tair);
+	inrast_tair = G_allocate_raster_buf(data_type_tair);
+    }
+    
+
+	/***************************************************/ 
+	if (flag3->answer) {
+	mapset = G_find_cell2(e0, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), e0);
+	}
+	data_type_e0 = G_raster_map_type(e0, mapset);
+	if ((infd_e0 = G_open_cell_old(e0, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), e0);
+	if (G_get_cellhd(e0, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), e0);
+	inrast_e0 = G_allocate_raster_buf(data_type_e0);
+    }
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (result2) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (result2) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    
+	/* Process tempk min / max pixels for SENAY Evapfr */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_albedo;
+	DCELL d_tempk;
+	DCELL d_dem;
+	DCELL d_t0dem;
+	DCELL d_ndvi;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row
+	     (infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_dem, inrast_dem, row, data_type_dem) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), dem);
+	if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi) <
+	     0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_dem) {
+	    case CELL_TYPE:
+		d_dem = (double)((CELL *) inrast_dem)[col];
+		break;
+	    case FCELL_TYPE:
+		d_dem = (double)((FCELL *) inrast_dem)[col];
+		break;
+	    case DCELL_TYPE:
+		d_dem = (double)((DCELL *) inrast_dem)[col];
+		break;
+	    }
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_albedo) || G_is_d_null_value(&d_tempk)
+		 || G_is_d_null_value(&d_dem) ||
+		 G_is_d_null_value(&d_ndvi)) {
+		
+		    /* do nothing */ 
+	    }
+	    else {
+		d_t0dem = d_tempk + 0.00649 * d_dem;
+		if (d_t0dem < 0.0 || d_albedo < 0.001) {
+		    
+			/* do nothing */ 
+		}
+		else {
+		    if (d_tempk < tempk_min && d_albedo < 0.1) {
+			t0dem_min = d_t0dem;
+			tempk_min = d_tempk;
+		    }
+		    else if (d_tempk > tempk_max && d_albedo > 0.3) {
+			t0dem_max = d_t0dem;
+			tempk_max = d_tempk;
+		    }
+		}
+	    }
+	    }
+	}
+    G_message("tempk_min=%f\ntempk_max=%f\n", tempk_min, tempk_max);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_albedo;
+	DCELL d_tempk;
+	DCELL d_lat;
+	DCELL d_doy;
+	DCELL d_tsw;
+	DCELL d_solar;
+	DCELL d_rnetd;
+	DCELL d_slope;
+	DCELL d_aspect;
+	DCELL d_tair;
+	DCELL d_e0;
+	DCELL d_ndvi;
+	DCELL d_etpotd;
+	DCELL d_evapfr;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_lat, inrast_lat, row, data_type_lat) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), lat);
+	if (G_get_raster_row(infd_doy, inrast_doy, row, data_type_doy) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), doy);
+	if (G_get_raster_row(infd_tsw, inrast_tsw, row, data_type_tsw) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tsw);
+	if (flag2->answer) {
+	    if (G_get_raster_row
+		 (infd_slope, inrast_slope, row, data_type_slope) < 0)
+		G_fatal_error(_("Could not read from <%s>"), slope);
+	    if (G_get_raster_row
+		 (infd_aspect, inrast_aspect, row, data_type_aspect) < 0)
+		G_fatal_error(_("Could not read from <%s>"), aspect);
+	}
+	if (flag3->answer) {
+	    if (G_get_raster_row(infd_tair, inrast_tair, row, data_type_tair)
+		 < 0)
+		G_fatal_error(_("Could not read from <%s>"), tair);
+	    if (G_get_raster_row(infd_e0, inrast_e0, row, data_type_e0) < 0)
+		G_fatal_error(_("Could not read from <%s>"), e0);
+	}
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_lat) {
+	    case CELL_TYPE:
+		d_lat = (double)((CELL *) inrast_lat)[col];
+		break;
+	    case FCELL_TYPE:
+		d_lat = (double)((FCELL *) inrast_lat)[col];
+		break;
+	    case DCELL_TYPE:
+		d_lat = (double)((DCELL *) inrast_lat)[col];
+		break;
+	    }
+	    switch (data_type_doy) {
+	    case CELL_TYPE:
+		d_doy = (double)((CELL *) inrast_doy)[col];
+		break;
+	    case FCELL_TYPE:
+		d_doy = (double)((FCELL *) inrast_doy)[col];
+		break;
+	    case DCELL_TYPE:
+		d_doy = (double)((DCELL *) inrast_doy)[col];
+		break;
+	    }
+	    switch (data_type_tsw) {
+	    case CELL_TYPE:
+		d_tsw = (double)((CELL *) inrast_tsw)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tsw = (double)((FCELL *) inrast_tsw)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tsw = (double)((DCELL *) inrast_tsw)[col];
+		break;
+	    }
+	    if (flag2->answer) {
+		switch (data_type_slope) {
+		case CELL_TYPE:
+		    d_slope = (double)((CELL *) inrast_slope)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_slope = (double)((FCELL *) inrast_slope)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_slope = (double)((DCELL *) inrast_slope)[col];
+		    break;
+		}
+		switch (data_type_aspect) {
+		case CELL_TYPE:
+		    d_aspect = (double)((CELL *) inrast_aspect)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_aspect = (double)((FCELL *) inrast_aspect)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_aspect = (double)((DCELL *) inrast_aspect)[col];
+		    break;
+		}
+	    }
+	    if (flag3->answer) {
+		switch (data_type_tair) {
+		case CELL_TYPE:
+		    d_tair = (double)((CELL *) inrast_tair)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_tair = (double)((FCELL *) inrast_tair)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_tair = (double)((DCELL *) inrast_tair)[col];
+		    break;
+		}
+		switch (data_type_e0) {
+		case CELL_TYPE:
+		    d_e0 = (double)((CELL *) inrast_e0)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_e0 = (double)((FCELL *) inrast_e0)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_e0 = (double)((DCELL *) inrast_e0)[col];
+		    break;
+		}
+	    }
+	    if (G_is_d_null_value(&d_albedo) || G_is_d_null_value(&d_tempk)
+		 || d_tempk < 10.0 || G_is_d_null_value(&d_lat) ||
+		 G_is_d_null_value(&d_doy) || G_is_d_null_value(&d_tsw) ||
+		 G_is_d_null_value(&d_ndvi) || ((flag2->answer) &&
+						  G_is_d_null_value(&d_slope))
+		 || ((flag2->answer) && G_is_d_null_value(&d_aspect)) ||
+		 ((flag3->answer) && G_is_d_null_value(&d_tair)) ||
+		 ((flag3->answer) && G_is_d_null_value(&d_e0))) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (result2)
+		    G_set_d_null_value(&outrast2[col], 1);
+	    }
+	    else {
+		if (flag2->answer) {
+		    d_solar =
+			solar_day_3d(d_lat, d_doy, d_tsw, d_slope, d_aspect);
+		}
+		else {
+		    d_solar = solar_day(d_lat, d_doy, d_tsw);
+		}
+		d_evapfr = evapfr_senay(tempk_max, tempk_min, d_tempk);
+		
+		    /*If water then no water stress */ 
+		    if (d_albedo <= 0.1 && d_ndvi <= 0.0) {
+		    d_evapfr = 1.0;
+		}
+		
+		    /*some points are colder than selected low */ 
+		    if (d_evapfr > 1.0) {
+		    d_evapfr = 1.0;
+		}
+		if (result2) {
+		    outrast2[col] = d_evapfr;
+		}
+		if (flag3->answer) {
+		    d_rnetd =
+			r_net_day_bandara98(d_albedo, d_solar, e_atm, d_e0,
+					    d_tair);
+		}
+		else {
+		    d_rnetd = r_net_day(d_albedo, d_solar, d_tsw);
+		}
+		d_etpotd = et_pot_day(d_rnetd, d_tempk, roh_w);
+		d = d_etpotd * d_evapfr;
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	if (result2) {
+	    if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output raster file"));
+	}
+	}
+    G_free(inrast_albedo);
+    G_free(inrast_tempk);
+    G_free(inrast_dem);
+    G_free(inrast_lat);
+    G_free(inrast_doy);
+    G_free(inrast_tsw);
+    G_close_cell(infd_albedo);
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_dem);
+    G_close_cell(infd_lat);
+    G_close_cell(infd_doy);
+    G_close_cell(infd_tsw);
+    if (flag3->answer) {
+	G_free(inrast_tair);
+	G_close_cell(infd_tair);
+	G_free(inrast_e0);
+	G_close_cell(infd_e0);
+    }
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    if (result2) {
+	G_free(outrast2);
+	G_close_cell(outfd2);
+	
+	    /* Color table for evaporative fraction */ 
+	    G_init_colors(&colors);
+	G_add_color_rule(0, 0, 0, 0, 1, 255, 255, 255, &colors);
+	G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    
+	/* Color table for evapotranspiration */ 
+	G_init_colors(&colors);
+    G_add_color_rule(0, 0, 0, 0, 10, 255, 255, 255, &colors);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/r_net_day.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/r_net_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/r_net_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Net Radiation after Bastiaanssen (1995)*/
-
-double r_net_day( double bbalb, double solar, double tsw )
-{
-	double result;
-
-	result = ((1.0 - bbalb)*solar)-(110.0*tsw);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Net Radiation after Bastiaanssen (1995) */ 
+double r_net_day(double bbalb, double solar, double tsw) 
+{
+    double result;
+
+    result = ((1.0 - bbalb) * solar) - (110.0 * tsw);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/r_net_day_bandara98.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/r_net_day_bandara98.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/r_net_day_bandara98.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,22 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Net Radiation after Bandara (1998)*/
-
-double r_net_day_bandara98( double surface_albedo, double solar_day, double apparent_atm_emissivity, double surface_emissivity, double air_temperature )
-{
-	double longwave_balance, result;
-	double sigma=5.67*pow(10,-8);/*Stefann-Boltzmann Constant*/
-	
-	longwave_balance = (apparent_atm_emissivity-surface_emissivity) * sigma * pow(air_temperature,4) ;
-
-	result = ((1.0 - surface_albedo)*solar_day) - longwave_balance ;
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Net Radiation after Bandara (1998) */ 
+double r_net_day_bandara98(double surface_albedo, double solar_day,
+			    double apparent_atm_emissivity,
+			    double surface_emissivity,
+			    double air_temperature) 
+{
+    double longwave_balance, result;
+
+    double sigma = 5.67 * pow(10, -8);	/*Stefann-Boltzmann Constant */
+
+    longwave_balance =
+	(apparent_atm_emissivity -
+	 surface_emissivity) * sigma * pow(air_temperature, 4);
+    result = ((1.0 - surface_albedo) * solar_day) - longwave_balance;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/solar_day.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/solar_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/solar_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,24 +1,23 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Solar Diurnal Radiation after Bastiaanssen (1995) */
-
-#define PI 3.1415927
-
-double solar_day(double lat, double doy, double tsw )
-{
-	double ws, cosun, latrad, delta, deltarad, ds, result;
-
-	ds = 1.0 + 0.01672 * sin(2*PI*(doy-93.5)/365.0);
-	deltarad = 0.4093*sin((2*PI*doy/365)-1.39);
-	latrad =  lat * PI / 180.0;
-	
-	ws = acos(-tan(latrad)*tan(deltarad));
-	cosun = ws*sin(deltarad)*sin(latrad)+cos(deltarad)*cos(latrad)*sin(ws);
-	
-	result = ( cosun * 1367 * tsw ) / ( PI * ds * ds );
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Solar Diurnal Radiation after Bastiaanssen (1995) */ 
+    
+#define PI 3.1415927
+double solar_day(double lat, double doy, double tsw) 
+{
+    double ws, cosun, latrad, delta, deltarad, ds, result;
+
+    ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
+    deltarad = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    latrad = lat * PI / 180.0;
+    ws = acos(-tan(latrad) * tan(deltarad));
+    cosun =
+	ws * sin(deltarad) * sin(latrad) +
+	cos(deltarad) * cos(latrad) * sin(ws);
+    result = (cosun * 1367 * tsw) / (PI * ds * ds);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.SENAY/solar_day_3d.c
===================================================================
--- grass-addons/gipe/i.evapo.SENAY/solar_day_3d.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.SENAY/solar_day_3d.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,38 +1,35 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Solar Diurnal Radiation after Bastiaanssen (1995)
- * Includes Slope and aspect correction
- */
-#define PI 3.1415927
-
-double solar_day_3d(double lat, double doy, double tsw, double slope, double aspect )
-{
-	double ws, costheta, latrad, delta, deltarad, ds, result;
-	double temp1, temp2, temp3, temp4, temp5;
-	double slrad, asprad; /*slope and aspect in radians*/
-
-	ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
-	delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
-	deltarad  = delta * PI / 180.0;
-	latrad 	  = lat * PI / 180.0;
-	slrad  = slope * PI / 180.0;
-	asprad = aspect * PI / 180.0;
-
-	ws = acos(-tan(latrad)*tan(deltarad));
-
-	temp1 = sin(deltarad) * sin(latrad) * cos(slrad);
-	temp2 = sin(deltarad) * cos(latrad) * sin(slrad) * cos(asprad);
-	temp3 = cos(deltarad) * cos(latrad) * cos(slrad) * cos(ws*PI/180.0);
-	temp4 = cos(deltarad) * sin(slrad) * cos(asprad) * cos(ws*PI/180.0);
-	temp5 = cos(deltarad) * sin(slrad) * sin(asprad) * sin(ws*PI/180.0);
-
-	costheta = (temp1 - temp2 + temp3 + temp4 + temp5) / cos(slrad);
-	
-	result = ( costheta * 1367 * tsw ) / ( PI * ds * ds );
-
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Solar Diurnal Radiation after Bastiaanssen (1995)
+     * Includes Slope and aspect correction
+     */ 
+#define PI 3.1415927
+double solar_day_3d(double lat, double doy, double tsw, double slope,
+		     double aspect) 
+{
+    double ws, costheta, latrad, delta, deltarad, ds, result;
+
+    double temp1, temp2, temp3, temp4, temp5;
+
+    double slrad, asprad;	/*slope and aspect in radians */
+
+    ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
+    delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    deltarad = delta * PI / 180.0;
+    latrad = lat * PI / 180.0;
+    slrad = slope * PI / 180.0;
+    asprad = aspect * PI / 180.0;
+    ws = acos(-tan(latrad) * tan(deltarad));
+    temp1 = sin(deltarad) * sin(latrad) * cos(slrad);
+    temp2 = sin(deltarad) * cos(latrad) * sin(slrad) * cos(asprad);
+    temp3 = cos(deltarad) * cos(latrad) * cos(slrad) * cos(ws * PI / 180.0);
+    temp4 = cos(deltarad) * sin(slrad) * cos(asprad) * cos(ws * PI / 180.0);
+    temp5 = cos(deltarad) * sin(slrad) * sin(asprad) * sin(ws * PI / 180.0);
+    costheta = (temp1 - temp2 + temp3 + temp4 + temp5) / cos(slrad);
+    result = (costheta * 1367 * tsw) / (PI * ds * ds);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/main.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 *
 * MODULE:	i.evapo.TSA
@@ -24,687 +25,754 @@
 #include <grass/glocale.h>
 
 
-/*protos Net radiation*/
+/*protos Net radiation */
 double rn_g(double rnet, double fv);
+
 double rn_v(double rnet, double fv);
-/*protos temperature fractions*/
+
+/*protos temperature fractions */
 double tempk_g(double tempk, double tempk_v, double fv);
+
 double tempk_v(double tempk, double fv);
-/*protos soil heat flux*/
+
+/*protos soil heat flux */
 double g_0g(double rnet);
+
 double g_0v(double bbalb, double ndvi, double tempk_v, double rnet);
-/*protos sensible heat flux*/
-double h_g(double tempk_g, double tempk_v, double tempk_a, double r_g, double r_v, double r_a );
-double h_v(double tempk_g, double tempk_v, double tempk_a, double r_g, double r_v, double r_a );
-/*protos necessary for sensible heat flux calculations*/
-double ra(double d, double z0, double h, double z, double u_z, double tempk_a, double tempk_v);
-double rg(double d, double z0, double z0s, double h, double z, double w, double u_z, double tempk_a, double tempk_v);
-double rv(double d, double z0, double h, double z, double w, double u_z, double tempk_a, double tempk_v);
-/*proto ET*/
+
+/*protos sensible heat flux */
+double h_g(double tempk_g, double tempk_v, double tempk_a, double r_g,
+	   double r_v, double r_a);
+double h_v(double tempk_g, double tempk_v, double tempk_a, double r_g,
+	   double r_v, double r_a);
+/*protos necessary for sensible heat flux calculations */
+double ra(double d, double z0, double h, double z, double u_z, double tempk_a,
+	  double tempk_v);
+double rg(double d, double z0, double z0s, double h, double z, double w,
+	  double u_z, double tempk_a, double tempk_v);
+double rv(double d, double z0, double h, double z, double w, double u_z,
+	  double tempk_a, double tempk_v);
+/*proto ET */
 double daily_et(double et_instantaneous, double time, double sunshine_hours);
 
 
 int main(int argc, char *argv[])
-{	
-	/* buffer for input-output rasters */
-	void *inrast_FV,*inrast_TEMPK,*inrast_TEMPKA,*inrast_ALB,*inrast_NDVI,*inrast_RNET;
-	void *inrast_UZ,*inrast_DISP,*inrast_Z0,*inrast_HV;
-	void *inrast_Z0S,*inrast_W,*inrast_TIME,*inrast_SUNH;
-	
-	DCELL *outrast;
-	
-	/* pointers to input-output raster files */
-	int infd_FV,infd_TEMPK,infd_TEMPKA,infd_ALB,infd_NDVI,infd_RNET;
-	int infd_UZ,infd_DISP,infd_Z0,infd_HV;
-	int infd_Z0S,infd_W,infd_TIME,infd_SUNH,outfd;
+{
+    /* buffer for input-output rasters */
+    void *inrast_FV, *inrast_TEMPK, *inrast_TEMPKA, *inrast_ALB, *inrast_NDVI,
+	*inrast_RNET;
+    void *inrast_UZ, *inrast_DISP, *inrast_Z0, *inrast_HV;
 
-	/* mapsets for input raster files */
-	char *mapset_FV,*mapset_TEMPK,*mapset_TEMPKA,*mapset_ALB,*mapset_NDVI,*mapset_RNET;
-	char *mapset_UZ,*mapset_DISP,*mapset_Z0,*mapset_HV;
-	char *mapset_Z0S,*mapset_W,*mapset_TIME,*mapset_SUNH;
+    void *inrast_Z0S, *inrast_W, *inrast_TIME, *inrast_SUNH;
 
-	/* names of input-output raster files */
-	char *RNET, *FV, *TEMPK, *TEMPKA, *ALB, *NDVI;
-	char *UZ, *DISP, *Z0, *HV, *W, *TIME, *SUNH, *Z0S, *ETa; 
+    DCELL *outrast;
 
-	/* input-output cell values */
-	DCELL d_fv,d_tempk,d_tempka,d_alb,d_ndvi,d_rnet;
-	DCELL d_uz, d_z, d_disp, d_z0, d_hv, d_w, d_daily_et;
-	DCELL d_rn_g,d_rn_v;
-	DCELL d_tempk_g,d_tempk_v;
-	DCELL d_g0_g,d_g0_v;
-	DCELL d_ra;
-	DCELL d_z0s;
-	DCELL d_rg,d_rv;
-	DCELL d_h_g,d_h_v;
-	DCELL d_le_inst_g,d_le_inst_v,d_le_inst;
-	DCELL d_time,d_sunh;
+    /* pointers to input-output raster files */
+    int infd_FV, infd_TEMPK, infd_TEMPKA, infd_ALB, infd_NDVI, infd_RNET;
 
-	/* region informations and handler */
-	struct Cell_head cellhd;
-	int nrows, ncols;
-	int row, col;
+    int infd_UZ, infd_DISP, infd_Z0, infd_HV;
 
-	/* parser stuctures definition */
-	struct GModule *module;
-	struct Option *input_RNET, *input_FV, *input_TEMPK, *input_TEMPKA, *input_ALB, *input_NDVI;
-	struct Option *input_UZ,*input_Z,*input_DISP,*input_Z0,*input_HV;
-	struct Option *input_Z0S,*input_W,*input_TIME,*input_SUNH,*output;
-	struct Flag *zero;
-	struct Colors color;
-	struct History history;
+    int infd_Z0S, infd_W, infd_TIME, infd_SUNH, outfd;
 
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_fv;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_tempka;
-	RASTER_MAP_TYPE data_type_alb;
-	RASTER_MAP_TYPE data_type_ndvi;
-	RASTER_MAP_TYPE data_type_rnet;
-	RASTER_MAP_TYPE data_type_uz;
-	RASTER_MAP_TYPE data_type_disp;
-	RASTER_MAP_TYPE data_type_z0;
-	RASTER_MAP_TYPE data_type_hv;
-	RASTER_MAP_TYPE data_type_w;
-	RASTER_MAP_TYPE data_type_time;
-	RASTER_MAP_TYPE data_type_sunh;
-	RASTER_MAP_TYPE data_type_z0s;
-	RASTER_MAP_TYPE data_type_eta;
+    /* mapsets for input raster files */
+    char *mapset_FV, *mapset_TEMPK, *mapset_TEMPKA, *mapset_ALB, *mapset_NDVI,
+	*mapset_RNET;
+    char *mapset_UZ, *mapset_DISP, *mapset_Z0, *mapset_HV;
 
-	/* Initialize the GIS calls */
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description =
-		_("Actual Evapotranspiration Calculation "
-		"Two-Source Algorithm formulation, after Chen et al., 2005.");
-	
-	/* Define different options */
-	input_RNET = G_define_standard_option(G_OPT_R_INPUT);
-	input_RNET->key			= "RNET";
-	input_RNET->key_desc		= "[W/m2]";
-	input_RNET->description 	= _("Name of Net Radiation raster layer");
-	
-	input_FV = G_define_standard_option(G_OPT_R_INPUT);
-	input_FV->key			= "FV";
-	input_FV->key_desc		= "[-]";
-	input_FV->description		= _("Name of Vegetation Fraction raster layer");
-		
-	input_TEMPK = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPK->key		= "TEMPK";
-	input_TEMPK->key_desc		= "[K]";
-	input_TEMPK->description	= _("Name of surface temperature raster layer");
-		
-	input_TEMPKA = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPKA->key		= "TEMPKA";
-	input_TEMPKA->key_desc		= "[K]";
-	input_TEMPKA->description	= _("Name of air temperature raster layer");
-		
-	input_ALB = G_define_standard_option(G_OPT_R_INPUT);
-	input_ALB->key			= "ALB";
-	input_ALB->key_desc		= "[-]";
-	input_ALB->description		= _("Name of Albedo raster layer");
-	
-	input_NDVI = G_define_standard_option(G_OPT_R_INPUT);
-	input_NDVI->key			= "NDVI";
-	input_NDVI->key_desc		= "[-]";
-	input_NDVI->description		= _("Name of NDVI raster layer");
-	
-	input_UZ = G_define_standard_option(G_OPT_R_INPUT);
-	input_UZ->key			= "UZ";
-	input_UZ->key_desc		= "[m/s]";
-	input_UZ->description		= _("Name of wind speed (at z ref. height) raster layer");
-	
-	input_Z = G_define_option();
-	input_Z->key			= "Z";
-	input_Z->key_desc		= "[m]";
-	input_Z->type			= TYPE_DOUBLE;
-	input_Z->required		= YES;
-	input_Z->answer			= "2.0";
-	input_Z->gisprompt		= "value,parameter";
-	input_Z->description		= _("Value of reference height for UZ");
-	
-	input_DISP = G_define_standard_option(G_OPT_R_INPUT);
-	input_DISP->key			= "DISP";
-	input_DISP->key_desc		= "[m]";
-	input_DISP->required		= NO;
-	input_DISP->description		= _("Name of displacement height raster layer");
-	
-	input_Z0 = G_define_standard_option(G_OPT_R_INPUT);
-	input_Z0->key			= "Z0";
-	input_Z0->key_desc		= "[m]";
-	input_Z0->required		= NO;
-	input_Z0->description		= _("Name of surface roughness length raster layer");
-	
-	input_HV = G_define_standard_option(G_OPT_R_INPUT);
-	input_HV->key			= "HV";
-	input_HV->key_desc		= "[m]";
-	input_HV->required		= NO;
-	input_HV->description		= _("Name of vegetation height raster layer (one out of DISP, Z0 or HV should be given!)");
-	
-	input_Z0S = G_define_standard_option(G_OPT_R_INPUT);
-	input_Z0S->key			= "Z0S";
-	input_Z0S->key_desc		= "[m]";
-	input_Z0S->description		= _("Name of bare soil surface roughness length raster layer");
-	
-	input_W = G_define_standard_option(G_OPT_R_INPUT);
-	input_W->key			= "W";
-	input_W->key_desc		= "[g]";
-	input_W->description		= _("Name of leaf weight raster layer");
-	
-	input_TIME = G_define_standard_option(G_OPT_R_INPUT);
-	input_TIME->key			= "TIME";
-	input_TIME->key_desc		= "[HH.HH]";
-	input_TIME->description		= _("Name of local Time at satellite overpass raster layer");
-	
-	input_SUNH = G_define_standard_option(G_OPT_R_INPUT);
-	input_SUNH->key			= "SUNH";
-	input_SUNH->key_desc		= "[HH.HH]";
-	input_SUNH->description		= _("Name of Sunshine hours raster layer");
-	
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key_desc		= "[mm/d]";
-	output->description		= _("Name of output Actual Evapotranspiration layer");
-	
-	/* Define the different flags */
-	zero = G_define_flag() ;
-	zero->key			= 'z' ;
-	zero->description		= _("set negative ETa to zero");
-	
-	if (G_parser(argc, argv))
-		exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	RNET	= input_RNET->answer;
-	FV	= input_FV->answer;
-	TEMPK	= input_TEMPK->answer;
-	TEMPKA	= input_TEMPKA->answer;
-	ALB	= input_ALB->answer;
-	NDVI	= input_NDVI->answer;
-	UZ	= input_UZ->answer;
-	d_z	= atof(input_Z->answer);
-	DISP	= input_DISP->answer;
-	Z0	= input_Z0->answer;
-	HV	= input_HV->answer;
-	Z0S	= input_Z0S->answer;
-	W	= input_W->answer;
-	TIME	= input_TIME->answer;
-	SUNH	= input_SUNH->answer;
-	
-	ETa	= output->answer;
-	
-	/* find maps in mapset */
-	mapset_RNET = G_find_cell2 (RNET, "");
-	if (mapset_RNET == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), RNET);
-	mapset_FV = G_find_cell2 (FV, "");
-	if (mapset_FV == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), FV);
-	mapset_TEMPK = G_find_cell2 (TEMPK, "");
-	if (mapset_TEMPK == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPK);
-	mapset_TEMPKA = G_find_cell2 (TEMPKA, "");
-	if (mapset_TEMPKA == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPKA);
-	mapset_ALB = G_find_cell2 (ALB, "");
-	if (mapset_ALB == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), ALB);
-	mapset_NDVI = G_find_cell2 (NDVI, "");
-	if (mapset_NDVI == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), NDVI);
-	mapset_UZ = G_find_cell2 (UZ, "");
-	if (mapset_UZ == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), UZ);
-	if(DISP!=NULL){
-		mapset_DISP = G_find_cell2 (DISP, "");
-		if (mapset_DISP == NULL)
-	        	G_fatal_error (_("cell file [%s] not found"), DISP);
-	}
-	if(Z0!=NULL){
-		mapset_Z0 = G_find_cell2 (Z0, "");
-		if (Z0 && mapset_Z0 == NULL)
-	       		G_fatal_error (_("cell file [%s] not found"), Z0);
-	}
-	if(HV!=NULL){
-		mapset_HV = G_find_cell2 (HV, "");
-		if (HV && mapset_HV == NULL)
-	       		G_fatal_error (_("cell file [%s] not found"), HV);
-	}
-	mapset_Z0S = G_find_cell2 (Z0S, "");
-	if (mapset_Z0S == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), Z0S);
-	mapset_W = G_find_cell2 (W, "");
-	if (mapset_W == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), W);
-	mapset_TIME = G_find_cell2 (TIME, "");
-	if (mapset_TIME == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TIME);
-	mapset_SUNH = G_find_cell2 (SUNH, "");
-	if (mapset_SUNH == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), SUNH);
-	
-	/* check legal output name */ 
-	if (G_legal_filename (ETa) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), ETa);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_rnet = G_raster_map_type(RNET,mapset_RNET);
-	data_type_fv = G_raster_map_type(FV,mapset_FV);
-	data_type_tempk = G_raster_map_type(TEMPK,mapset_TEMPK);
-	data_type_tempka = G_raster_map_type(TEMPKA,mapset_TEMPKA);
-	data_type_alb = G_raster_map_type(ALB,mapset_ALB);
-	data_type_ndvi = G_raster_map_type(NDVI,mapset_NDVI);
-	data_type_uz = G_raster_map_type(UZ,mapset_UZ);
-	if(DISP!=NULL){
-		data_type_disp = G_raster_map_type(DISP,mapset_DISP);
-	}
-	if(Z0!=NULL){
-		data_type_z0 = G_raster_map_type(Z0,mapset_Z0);
-	}
-	if(HV!=NULL){
-		data_type_hv = G_raster_map_type(HV,mapset_HV);
-	}
-	data_type_z0s = G_raster_map_type(Z0S,mapset_Z0S);
-	data_type_w = G_raster_map_type(W,mapset_W);
-	data_type_time = G_raster_map_type(TIME,mapset_TIME);
-	data_type_sunh = G_raster_map_type(SUNH,mapset_SUNH);
-	
-	/* open pointers to input raster files */
-	if ( (infd_RNET = G_open_cell_old (RNET, mapset_RNET)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), RNET);
-	if ( (infd_FV = G_open_cell_old (FV, mapset_FV)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"),FV);
-	if ( (infd_TEMPK = G_open_cell_old (TEMPK, mapset_TEMPK)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPK);
-	if ( (infd_TEMPKA = G_open_cell_old (TEMPKA, mapset_TEMPKA)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPKA);
-	if ( (infd_ALB = G_open_cell_old (ALB, mapset_ALB)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), ALB);
-	if ( (infd_NDVI = G_open_cell_old (NDVI, mapset_NDVI)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), NDVI);
-	if ( (infd_UZ = G_open_cell_old (UZ, mapset_UZ)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), UZ);
-	if ((DISP!=NULL) && (infd_DISP = G_open_cell_old (DISP, mapset_DISP)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), DISP);
-	if ((Z0!=NULL) && (infd_Z0 = G_open_cell_old (Z0, mapset_Z0)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), Z0);
-	if ((HV!=NULL) && (infd_HV = G_open_cell_old (HV, mapset_HV)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), HV);
-	if ( (infd_Z0S = G_open_cell_old (Z0S, mapset_Z0S)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), SUNH);
-	if ( (infd_W = G_open_cell_old (W, mapset_W)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), W);
-	if ( (infd_TIME = G_open_cell_old (TIME, mapset_TIME)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TIME);
-	if ( (infd_SUNH = G_open_cell_old (SUNH, mapset_SUNH)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), SUNH);
+    char *mapset_Z0S, *mapset_W, *mapset_TIME, *mapset_SUNH;
 
-	/* read headers of raster files */
-	if (G_get_cellhd (RNET, mapset_RNET, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), RNET);
-	if (G_get_cellhd (FV, mapset_FV, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), FV);
-	if (G_get_cellhd (TEMPK, mapset_TEMPK, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPK);
-	if (G_get_cellhd (TEMPKA, mapset_TEMPKA, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPKA);
-	if (G_get_cellhd (ALB, mapset_ALB, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), ALB);
-	if (G_get_cellhd (NDVI, mapset_NDVI, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), NDVI);
-	if (G_get_cellhd (UZ, mapset_UZ, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), UZ);
-	if ((DISP!=NULL) && G_get_cellhd (DISP, mapset_DISP, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), DISP);
-	if ((Z0!=NULL) && G_get_cellhd (Z0, mapset_Z0, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), Z0);
-	if ((HV!=NULL) && G_get_cellhd (HV, mapset_HV, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), HV);
-	if (G_get_cellhd (Z0S, mapset_Z0S, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), Z0S);
-	if (G_get_cellhd (W, mapset_W, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), W);
-	if (G_get_cellhd (TIME, mapset_TIME, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TIME);
-	if (G_get_cellhd (SUNH, mapset_SUNH, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), SUNH);
+    /* names of input-output raster files */
+    char *RNET, *FV, *TEMPK, *TEMPKA, *ALB, *NDVI;
 
-	/* Allocate input buffer */
-	inrast_RNET  = G_allocate_raster_buf(data_type_rnet);
-	inrast_FV = G_allocate_raster_buf(data_type_fv);
-	inrast_TEMPK = G_allocate_raster_buf(data_type_tempk);
-	inrast_TEMPKA = G_allocate_raster_buf(data_type_tempka);
-	inrast_ALB = G_allocate_raster_buf(data_type_alb);
-	inrast_NDVI = G_allocate_raster_buf(data_type_ndvi);
-	inrast_UZ = G_allocate_raster_buf(data_type_uz);
-	if(DISP!=NULL){
-		inrast_DISP = G_allocate_raster_buf(data_type_disp);
-	}
-	if(Z0!=NULL){
-		inrast_Z0 = G_allocate_raster_buf(data_type_z0);
-	}
-	if(HV!=NULL){
-		inrast_HV = G_allocate_raster_buf(data_type_hv);
-	}
-	inrast_Z0S = G_allocate_raster_buf(data_type_z0s);
-	inrast_W = G_allocate_raster_buf(data_type_w);
-	inrast_TIME = G_allocate_raster_buf(data_type_time);
-	inrast_SUNH = G_allocate_raster_buf(data_type_sunh);
-	
-	/* get rows and columns number of the current region */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
+    char *UZ, *DISP, *Z0, *HV, *W, *TIME, *SUNH, *Z0S, *ETa;
 
-	/* allocate output buffer */
-	outrast = G_allocate_raster_buf(data_type_output);
+    /* input-output cell values */
+    DCELL d_fv, d_tempk, d_tempka, d_alb, d_ndvi, d_rnet;
 
-	/* open pointers to output raster files */
-	if ( (outfd = G_open_raster_new (ETa,data_type_output)) < 0)
-		G_fatal_error (_("Could not open <%s>"),ETa);
-	
+    DCELL d_uz, d_z, d_disp, d_z0, d_hv, d_w, d_daily_et;
 
-	/* start the loop through cells */
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent(row, nrows, 2);
-				
-		/* read input raster row into line buffer*/	
-		if (G_get_raster_row (infd_RNET, inrast_RNET, row,data_type_rnet) < 0)
-			G_fatal_error (_("Could not read from <%s>"),RNET);
-		if (G_get_raster_row (infd_FV, inrast_FV, row,data_type_fv) < 0)
-			G_fatal_error (_("Could not read from <%s>"),FV);
-		if (G_get_raster_row (infd_TEMPK, inrast_TEMPK, row,data_type_tempk) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPK);
-		if (G_get_raster_row (infd_TEMPKA, inrast_TEMPKA, row,data_type_tempka) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPKA);
-		if (G_get_raster_row (infd_ALB, inrast_ALB, row,data_type_alb) < 0)
-			G_fatal_error (_("Could not read from <%s>"),ALB);
-		if (G_get_raster_row (infd_NDVI, inrast_NDVI, row,data_type_ndvi) < 0)
-			G_fatal_error (_("Could not read from <%s>"),NDVI);
-		if (G_get_raster_row (infd_UZ, inrast_UZ, row,data_type_uz) < 0)
-			G_fatal_error (_("Could not read from <%s>"),UZ);
-		if ((DISP!=NULL) && G_get_raster_row (infd_DISP, inrast_DISP, row,data_type_disp) < 0)
-			G_fatal_error (_("Could not read from <%s>"),DISP);
-		if ((Z0!=NULL) && G_get_raster_row (infd_Z0, inrast_Z0, row,data_type_z0) < 0)
-			G_fatal_error (_("Could not read from <%s>"),Z0);
-		if ((HV!=NULL) && G_get_raster_row (infd_HV, inrast_HV, row,data_type_hv) < 0)
-			G_fatal_error (_("Could not read from <%s>"),HV);
-		if (G_get_raster_row (infd_Z0S, inrast_Z0S, row,data_type_z0s) < 0)
-			G_fatal_error (_("Could not read from <%s>"),Z0S);
-		if (G_get_raster_row (infd_W, inrast_W, row,data_type_w) < 0)
-			G_fatal_error (_("Could not read from <%s>"),W);
-		if (G_get_raster_row (infd_TIME, inrast_TIME, row,data_type_time) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TIME);
-		if (G_get_raster_row (infd_SUNH, inrast_SUNH, row,data_type_sunh) < 0)
-			G_fatal_error (_("Could not read from <%s>"),SUNH);
-		
-		for (col=0; col < ncols; col++)
-		{
-			/* read current cell from line buffer */
-			switch(data_type_rnet){
-				case CELL_TYPE:
-					d_rnet	= (double) ((CELL *) inrast_RNET)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnet	= (double) ((FCELL *) inrast_RNET)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnet	= ((DCELL *) inrast_RNET)[col];
-					break;
-			}
-			switch(data_type_fv){
-				case CELL_TYPE:
-					d_fv = (double) ((CELL *) inrast_FV)[col];
-					break;
-				case FCELL_TYPE:
-					d_fv = (double) ((FCELL *) inrast_FV)[col];
-					break;
-				case DCELL_TYPE:
-					d_fv = ((DCELL *) inrast_FV)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk	= (double) ((CELL *) inrast_TEMPK)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk	= (double) ((FCELL *) inrast_TEMPK)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk	= ((DCELL *) inrast_TEMPK)[col];
-					break;
-			}
-			switch(data_type_tempka){
-				case CELL_TYPE:
-					d_tempka = (double) ((CELL *) inrast_TEMPKA)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempka = (double) ((FCELL *) inrast_TEMPKA)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempka = ((DCELL *) inrast_TEMPKA)[col];
-					break;
-			}
-			switch(data_type_alb){
-				case CELL_TYPE:
-					d_alb = (double) ((CELL *) inrast_ALB)[col];
-					break;
-				case FCELL_TYPE:
-					d_alb = (double) ((FCELL *) inrast_ALB)[col];
-					break;
-				case DCELL_TYPE:
-					d_alb = ((DCELL *) inrast_ALB)[col];
-					break;
-			}
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi 	= (double) ((CELL *) inrast_NDVI)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi 	= (double) ((FCELL *) inrast_NDVI)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi 	= ((DCELL *) inrast_NDVI)[col];
-					break;
-			}
-			switch(data_type_uz){
-				case CELL_TYPE:
-					d_uz = (double) ((CELL *) inrast_UZ)[col];
-					break;
-				case FCELL_TYPE:
-					d_uz = (double) ((FCELL *) inrast_UZ)[col];
-					break;
-				case DCELL_TYPE:
-					d_uz = ((DCELL *) inrast_UZ)[col];
-					break;
-			}
-			if(DISP!=NULL){
-				switch(data_type_tempk){
-					case CELL_TYPE:
-						d_disp	= (double) ((CELL *) inrast_DISP)[col];
-						break;
-					case FCELL_TYPE:
-						d_disp	= (double) ((FCELL *) inrast_DISP)[col];
-						break;
-					case DCELL_TYPE:
-						d_disp	= ((DCELL *) inrast_DISP)[col];
-						break;
-				}
-			}else{
-				d_disp	= -10.0;/*negative, see inside functions*/
-			}
-			if(Z0!=NULL){
-			switch(data_type_z0){
-				case CELL_TYPE:
-					d_z0 = (double) ((CELL *) inrast_Z0)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0 = (double) ((FCELL *) inrast_Z0)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0	= ((DCELL *) inrast_Z0)[col];
-					break;
-			}
-			}else{
-				d_z0	= -10.0;/*negative, see inside functions*/
-			}
-			if(HV!=NULL){
-			switch(data_type_hv){
-				case CELL_TYPE:
-					d_hv	= (double) ((CELL *) inrast_HV)[col];
-					break;
-				case FCELL_TYPE:
-					d_hv	= (double) ((FCELL *) inrast_HV)[col];
-					break;
-				case DCELL_TYPE:
-					d_hv	= ((DCELL *) inrast_HV)[col];
-					break;
-			}
-			}else{
-				d_hv	= -10.0;/*negative, see inside functions*/
-			}
-			switch(data_type_z0s){
-				case CELL_TYPE:
-					d_z0s	= (double) ((CELL *) inrast_Z0S)[col];
-					break;
-				case FCELL_TYPE:
-					d_z0s	= (double) ((FCELL *) inrast_Z0S)[col];
-					break;
-				case DCELL_TYPE:
-					d_z0s	= ((DCELL *) inrast_Z0S)[col];
-					break;
-			}
-			switch(data_type_w){
-				case CELL_TYPE:
-					d_w = (double) ((CELL *) inrast_W)[col];
-					break;
-				case FCELL_TYPE:
-					d_w = (double) ((FCELL *) inrast_W)[col];
-					break;
-				case DCELL_TYPE:
-					d_w = ((DCELL *) inrast_W)[col];
-					break;
-			}
-			switch(data_type_time){
-				case CELL_TYPE:
-					d_time	= (double) ((CELL *) inrast_TIME)[col];
-					break;
-				case FCELL_TYPE:
-					d_time	= (double) ((FCELL *) inrast_TIME)[col];
-					break;
-				case DCELL_TYPE:
-					d_time	= ((DCELL *) inrast_TIME)[col];
-					break;
-			}
-			switch(data_type_sunh){
-				case CELL_TYPE:
-					d_sunh	= (double) ((CELL *) inrast_SUNH)[col];
-					break;
-				case FCELL_TYPE:
-					d_sunh	= (double) ((FCELL *) inrast_SUNH)[col];
-					break;
-				case DCELL_TYPE:
-					d_sunh	= ((DCELL *) inrast_SUNH)[col];
-					break;
-			}
+    DCELL d_rn_g, d_rn_v;
 
-			/*Calculate Net radiation fractions*/
-			d_rn_g 		= rn_g( d_rnet, d_fv);
-			d_rn_v 		= rn_v( d_rnet, d_fv);
-			
-			/*Calculate temperature fractions*/
-			d_tempk_v 	= tempk_v( d_tempk, d_fv);
-			d_tempk_g 	= tempk_g( d_tempk, d_tempk_v, d_fv);
-			
-			/*Calculate soil heat flux fractions*/
-			d_g0_g 		= g_0g( d_rnet);
-			d_g0_v 		= g_0v( d_alb, d_ndvi, d_tempk_v, d_rnet);
-			
-			/*Data necessary for sensible heat flux calculations*/
-			if(d_disp<0.0&&d_z0<0.0&&d_hv<0.0){
-				G_fatal_error (_("DISP, Z0 and HV are all negative, cannot continue, bailing out... Check your optional input files again"));
-			}else{
-				d_ra	= ra( d_disp, d_z0, d_hv, d_z, d_uz, d_tempka, d_tempk_v);
-				d_rg	= rg( d_disp, d_z0, d_z0s, d_hv, d_z, d_w, d_uz, d_tempka, d_tempk_v);
-				d_rv	= rv( d_disp, d_z0, d_hv, d_z, d_w, d_uz, d_tempka, d_tempk_v);
-			}
-			/*Calculate sensible heat flux fractions*/
-			d_h_g 		= h_g( d_tempk_g, d_tempk_v, d_tempka, d_rg, d_rv, d_ra );
-			d_h_v 		= h_v( d_tempk_g, d_tempk_v, d_tempka, d_rg, d_rv, d_ra );
-			
-			/*Calculate LE*/
-			d_le_inst_v	= d_rn_v - d_g0_v - d_h_v;
-			d_le_inst_g	= d_rn_g - d_g0_g - d_h_g;
-			d_le_inst	= d_le_inst_v + d_le_inst_g;
-			
-			/*Calculate ET*/
-			d_daily_et	= daily_et( d_le_inst, d_time, d_sunh);
-			
+    DCELL d_tempk_g, d_tempk_v;
 
-			if (zero->answer && d_daily_et<0)
-				d_daily_et=0.0;
-			
-			/* write calculated ETP to output line buffer */
-			outrast[col] = d_daily_et;
+    DCELL d_g0_g, d_g0_v;
+
+    DCELL d_ra;
+
+    DCELL d_z0s;
+
+    DCELL d_rg, d_rv;
+
+    DCELL d_h_g, d_h_v;
+
+    DCELL d_le_inst_g, d_le_inst_v, d_le_inst;
+
+    DCELL d_time, d_sunh;
+
+    /* region informations and handler */
+    struct Cell_head cellhd;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    /* parser stuctures definition */
+    struct GModule *module;
+
+    struct Option *input_RNET, *input_FV, *input_TEMPK, *input_TEMPKA,
+	*input_ALB, *input_NDVI;
+    struct Option *input_UZ, *input_Z, *input_DISP, *input_Z0, *input_HV;
+
+    struct Option *input_Z0S, *input_W, *input_TIME, *input_SUNH, *output;
+
+    struct Flag *zero;
+
+    struct Colors color;
+
+    struct History history;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
+    RASTER_MAP_TYPE data_type_fv;
+
+    RASTER_MAP_TYPE data_type_tempk;
+
+    RASTER_MAP_TYPE data_type_tempka;
+
+    RASTER_MAP_TYPE data_type_alb;
+
+    RASTER_MAP_TYPE data_type_ndvi;
+
+    RASTER_MAP_TYPE data_type_rnet;
+
+    RASTER_MAP_TYPE data_type_uz;
+
+    RASTER_MAP_TYPE data_type_disp;
+
+    RASTER_MAP_TYPE data_type_z0;
+
+    RASTER_MAP_TYPE data_type_hv;
+
+    RASTER_MAP_TYPE data_type_w;
+
+    RASTER_MAP_TYPE data_type_time;
+
+    RASTER_MAP_TYPE data_type_sunh;
+
+    RASTER_MAP_TYPE data_type_z0s;
+
+    RASTER_MAP_TYPE data_type_eta;
+
+    /* Initialize the GIS calls */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description =
+	_("Actual Evapotranspiration Calculation "
+	  "Two-Source Algorithm formulation, after Chen et al., 2005.");
+
+    /* Define different options */
+    input_RNET = G_define_standard_option(G_OPT_R_INPUT);
+    input_RNET->key = "RNET";
+    input_RNET->key_desc = "[W/m2]";
+    input_RNET->description = _("Name of Net Radiation raster layer");
+
+    input_FV = G_define_standard_option(G_OPT_R_INPUT);
+    input_FV->key = "FV";
+    input_FV->key_desc = "[-]";
+    input_FV->description = _("Name of Vegetation Fraction raster layer");
+
+    input_TEMPK = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPK->key = "TEMPK";
+    input_TEMPK->key_desc = "[K]";
+    input_TEMPK->description = _("Name of surface temperature raster layer");
+
+    input_TEMPKA = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPKA->key = "TEMPKA";
+    input_TEMPKA->key_desc = "[K]";
+    input_TEMPKA->description = _("Name of air temperature raster layer");
+
+    input_ALB = G_define_standard_option(G_OPT_R_INPUT);
+    input_ALB->key = "ALB";
+    input_ALB->key_desc = "[-]";
+    input_ALB->description = _("Name of Albedo raster layer");
+
+    input_NDVI = G_define_standard_option(G_OPT_R_INPUT);
+    input_NDVI->key = "NDVI";
+    input_NDVI->key_desc = "[-]";
+    input_NDVI->description = _("Name of NDVI raster layer");
+
+    input_UZ = G_define_standard_option(G_OPT_R_INPUT);
+    input_UZ->key = "UZ";
+    input_UZ->key_desc = "[m/s]";
+    input_UZ->description =
+	_("Name of wind speed (at z ref. height) raster layer");
+
+    input_Z = G_define_option();
+    input_Z->key = "Z";
+    input_Z->key_desc = "[m]";
+    input_Z->type = TYPE_DOUBLE;
+    input_Z->required = YES;
+    input_Z->answer = "2.0";
+    input_Z->gisprompt = "value,parameter";
+    input_Z->description = _("Value of reference height for UZ");
+
+    input_DISP = G_define_standard_option(G_OPT_R_INPUT);
+    input_DISP->key = "DISP";
+    input_DISP->key_desc = "[m]";
+    input_DISP->required = NO;
+    input_DISP->description = _("Name of displacement height raster layer");
+
+    input_Z0 = G_define_standard_option(G_OPT_R_INPUT);
+    input_Z0->key = "Z0";
+    input_Z0->key_desc = "[m]";
+    input_Z0->required = NO;
+    input_Z0->description =
+	_("Name of surface roughness length raster layer");
+
+    input_HV = G_define_standard_option(G_OPT_R_INPUT);
+    input_HV->key = "HV";
+    input_HV->key_desc = "[m]";
+    input_HV->required = NO;
+    input_HV->description =
+	_("Name of vegetation height raster layer (one out of DISP, Z0 or HV should be given!)");
+
+    input_Z0S = G_define_standard_option(G_OPT_R_INPUT);
+    input_Z0S->key = "Z0S";
+    input_Z0S->key_desc = "[m]";
+    input_Z0S->description =
+	_("Name of bare soil surface roughness length raster layer");
+
+    input_W = G_define_standard_option(G_OPT_R_INPUT);
+    input_W->key = "W";
+    input_W->key_desc = "[g]";
+    input_W->description = _("Name of leaf weight raster layer");
+
+    input_TIME = G_define_standard_option(G_OPT_R_INPUT);
+    input_TIME->key = "TIME";
+    input_TIME->key_desc = "[HH.HH]";
+    input_TIME->description =
+	_("Name of local Time at satellite overpass raster layer");
+
+    input_SUNH = G_define_standard_option(G_OPT_R_INPUT);
+    input_SUNH->key = "SUNH";
+    input_SUNH->key_desc = "[HH.HH]";
+    input_SUNH->description = _("Name of Sunshine hours raster layer");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key_desc = "[mm/d]";
+    output->description = _("Name of output Actual Evapotranspiration layer");
+
+    /* Define the different flags */
+    zero = G_define_flag();
+    zero->key = 'z';
+    zero->description = _("set negative ETa to zero");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    RNET = input_RNET->answer;
+    FV = input_FV->answer;
+    TEMPK = input_TEMPK->answer;
+    TEMPKA = input_TEMPKA->answer;
+    ALB = input_ALB->answer;
+    NDVI = input_NDVI->answer;
+    UZ = input_UZ->answer;
+    d_z = atof(input_Z->answer);
+    DISP = input_DISP->answer;
+    Z0 = input_Z0->answer;
+    HV = input_HV->answer;
+    Z0S = input_Z0S->answer;
+    W = input_W->answer;
+    TIME = input_TIME->answer;
+    SUNH = input_SUNH->answer;
+
+    ETa = output->answer;
+
+    /* find maps in mapset */
+    mapset_RNET = G_find_cell2(RNET, "");
+    if (mapset_RNET == NULL)
+	G_fatal_error(_("cell file [%s] not found"), RNET);
+    mapset_FV = G_find_cell2(FV, "");
+    if (mapset_FV == NULL)
+	G_fatal_error(_("cell file [%s] not found"), FV);
+    mapset_TEMPK = G_find_cell2(TEMPK, "");
+    if (mapset_TEMPK == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPK);
+    mapset_TEMPKA = G_find_cell2(TEMPKA, "");
+    if (mapset_TEMPKA == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPKA);
+    mapset_ALB = G_find_cell2(ALB, "");
+    if (mapset_ALB == NULL)
+	G_fatal_error(_("cell file [%s] not found"), ALB);
+    mapset_NDVI = G_find_cell2(NDVI, "");
+    if (mapset_NDVI == NULL)
+	G_fatal_error(_("cell file [%s] not found"), NDVI);
+    mapset_UZ = G_find_cell2(UZ, "");
+    if (mapset_UZ == NULL)
+	G_fatal_error(_("cell file [%s] not found"), UZ);
+    if (DISP != NULL) {
+	mapset_DISP = G_find_cell2(DISP, "");
+	if (mapset_DISP == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), DISP);
+    }
+    if (Z0 != NULL) {
+	mapset_Z0 = G_find_cell2(Z0, "");
+	if (Z0 && mapset_Z0 == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), Z0);
+    }
+    if (HV != NULL) {
+	mapset_HV = G_find_cell2(HV, "");
+	if (HV && mapset_HV == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), HV);
+    }
+    mapset_Z0S = G_find_cell2(Z0S, "");
+    if (mapset_Z0S == NULL)
+	G_fatal_error(_("cell file [%s] not found"), Z0S);
+    mapset_W = G_find_cell2(W, "");
+    if (mapset_W == NULL)
+	G_fatal_error(_("cell file [%s] not found"), W);
+    mapset_TIME = G_find_cell2(TIME, "");
+    if (mapset_TIME == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TIME);
+    mapset_SUNH = G_find_cell2(SUNH, "");
+    if (mapset_SUNH == NULL)
+	G_fatal_error(_("cell file [%s] not found"), SUNH);
+
+    /* check legal output name */
+    if (G_legal_filename(ETa) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), ETa);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_rnet = G_raster_map_type(RNET, mapset_RNET);
+    data_type_fv = G_raster_map_type(FV, mapset_FV);
+    data_type_tempk = G_raster_map_type(TEMPK, mapset_TEMPK);
+    data_type_tempka = G_raster_map_type(TEMPKA, mapset_TEMPKA);
+    data_type_alb = G_raster_map_type(ALB, mapset_ALB);
+    data_type_ndvi = G_raster_map_type(NDVI, mapset_NDVI);
+    data_type_uz = G_raster_map_type(UZ, mapset_UZ);
+    if (DISP != NULL) {
+	data_type_disp = G_raster_map_type(DISP, mapset_DISP);
+    }
+    if (Z0 != NULL) {
+	data_type_z0 = G_raster_map_type(Z0, mapset_Z0);
+    }
+    if (HV != NULL) {
+	data_type_hv = G_raster_map_type(HV, mapset_HV);
+    }
+    data_type_z0s = G_raster_map_type(Z0S, mapset_Z0S);
+    data_type_w = G_raster_map_type(W, mapset_W);
+    data_type_time = G_raster_map_type(TIME, mapset_TIME);
+    data_type_sunh = G_raster_map_type(SUNH, mapset_SUNH);
+
+    /* open pointers to input raster files */
+    if ((infd_RNET = G_open_cell_old(RNET, mapset_RNET)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), RNET);
+    if ((infd_FV = G_open_cell_old(FV, mapset_FV)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), FV);
+    if ((infd_TEMPK = G_open_cell_old(TEMPK, mapset_TEMPK)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPK);
+    if ((infd_TEMPKA = G_open_cell_old(TEMPKA, mapset_TEMPKA)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPKA);
+    if ((infd_ALB = G_open_cell_old(ALB, mapset_ALB)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ALB);
+    if ((infd_NDVI = G_open_cell_old(NDVI, mapset_NDVI)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), NDVI);
+    if ((infd_UZ = G_open_cell_old(UZ, mapset_UZ)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), UZ);
+    if ((DISP != NULL) &&
+	(infd_DISP = G_open_cell_old(DISP, mapset_DISP)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), DISP);
+    if ((Z0 != NULL) && (infd_Z0 = G_open_cell_old(Z0, mapset_Z0)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), Z0);
+    if ((HV != NULL) && (infd_HV = G_open_cell_old(HV, mapset_HV)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), HV);
+    if ((infd_Z0S = G_open_cell_old(Z0S, mapset_Z0S)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), SUNH);
+    if ((infd_W = G_open_cell_old(W, mapset_W)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), W);
+    if ((infd_TIME = G_open_cell_old(TIME, mapset_TIME)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TIME);
+    if ((infd_SUNH = G_open_cell_old(SUNH, mapset_SUNH)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), SUNH);
+
+    /* read headers of raster files */
+    if (G_get_cellhd(RNET, mapset_RNET, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), RNET);
+    if (G_get_cellhd(FV, mapset_FV, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), FV);
+    if (G_get_cellhd(TEMPK, mapset_TEMPK, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPK);
+    if (G_get_cellhd(TEMPKA, mapset_TEMPKA, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPKA);
+    if (G_get_cellhd(ALB, mapset_ALB, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), ALB);
+    if (G_get_cellhd(NDVI, mapset_NDVI, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), NDVI);
+    if (G_get_cellhd(UZ, mapset_UZ, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), UZ);
+    if ((DISP != NULL) && G_get_cellhd(DISP, mapset_DISP, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), DISP);
+    if ((Z0 != NULL) && G_get_cellhd(Z0, mapset_Z0, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), Z0);
+    if ((HV != NULL) && G_get_cellhd(HV, mapset_HV, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), HV);
+    if (G_get_cellhd(Z0S, mapset_Z0S, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), Z0S);
+    if (G_get_cellhd(W, mapset_W, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), W);
+    if (G_get_cellhd(TIME, mapset_TIME, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TIME);
+    if (G_get_cellhd(SUNH, mapset_SUNH, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), SUNH);
+
+    /* Allocate input buffer */
+    inrast_RNET = G_allocate_raster_buf(data_type_rnet);
+    inrast_FV = G_allocate_raster_buf(data_type_fv);
+    inrast_TEMPK = G_allocate_raster_buf(data_type_tempk);
+    inrast_TEMPKA = G_allocate_raster_buf(data_type_tempka);
+    inrast_ALB = G_allocate_raster_buf(data_type_alb);
+    inrast_NDVI = G_allocate_raster_buf(data_type_ndvi);
+    inrast_UZ = G_allocate_raster_buf(data_type_uz);
+    if (DISP != NULL) {
+	inrast_DISP = G_allocate_raster_buf(data_type_disp);
+    }
+    if (Z0 != NULL) {
+	inrast_Z0 = G_allocate_raster_buf(data_type_z0);
+    }
+    if (HV != NULL) {
+	inrast_HV = G_allocate_raster_buf(data_type_hv);
+    }
+    inrast_Z0S = G_allocate_raster_buf(data_type_z0s);
+    inrast_W = G_allocate_raster_buf(data_type_w);
+    inrast_TIME = G_allocate_raster_buf(data_type_time);
+    inrast_SUNH = G_allocate_raster_buf(data_type_sunh);
+
+    /* get rows and columns number of the current region */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
+    /* allocate output buffer */
+    outrast = G_allocate_raster_buf(data_type_output);
+
+    /* open pointers to output raster files */
+    if ((outfd = G_open_raster_new(ETa, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), ETa);
+
+
+    /* start the loop through cells */
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+
+	/* read input raster row into line buffer */
+	if (G_get_raster_row(infd_RNET, inrast_RNET, row, data_type_rnet) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), RNET);
+	if (G_get_raster_row(infd_FV, inrast_FV, row, data_type_fv) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), FV);
+	if (G_get_raster_row(infd_TEMPK, inrast_TEMPK, row, data_type_tempk) <
+	    0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPK);
+	if (G_get_raster_row
+	    (infd_TEMPKA, inrast_TEMPKA, row, data_type_tempka) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPKA);
+	if (G_get_raster_row(infd_ALB, inrast_ALB, row, data_type_alb) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), ALB);
+	if (G_get_raster_row(infd_NDVI, inrast_NDVI, row, data_type_ndvi) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), NDVI);
+	if (G_get_raster_row(infd_UZ, inrast_UZ, row, data_type_uz) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), UZ);
+	if ((DISP != NULL) &&
+	    G_get_raster_row(infd_DISP, inrast_DISP, row, data_type_disp) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), DISP);
+	if ((Z0 != NULL) &&
+	    G_get_raster_row(infd_Z0, inrast_Z0, row, data_type_z0) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Z0);
+	if ((HV != NULL) &&
+	    G_get_raster_row(infd_HV, inrast_HV, row, data_type_hv) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), HV);
+	if (G_get_raster_row(infd_Z0S, inrast_Z0S, row, data_type_z0s) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), Z0S);
+	if (G_get_raster_row(infd_W, inrast_W, row, data_type_w) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), W);
+	if (G_get_raster_row(infd_TIME, inrast_TIME, row, data_type_time) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TIME);
+	if (G_get_raster_row(infd_SUNH, inrast_SUNH, row, data_type_sunh) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), SUNH);
+
+	for (col = 0; col < ncols; col++) {
+	    /* read current cell from line buffer */
+	    switch (data_type_rnet) {
+	    case CELL_TYPE:
+		d_rnet = (double)((CELL *) inrast_RNET)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnet = (double)((FCELL *) inrast_RNET)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnet = ((DCELL *) inrast_RNET)[col];
+		break;
+	    }
+	    switch (data_type_fv) {
+	    case CELL_TYPE:
+		d_fv = (double)((CELL *) inrast_FV)[col];
+		break;
+	    case FCELL_TYPE:
+		d_fv = (double)((FCELL *) inrast_FV)[col];
+		break;
+	    case DCELL_TYPE:
+		d_fv = ((DCELL *) inrast_FV)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_TEMPK)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_TEMPK)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = ((DCELL *) inrast_TEMPK)[col];
+		break;
+	    }
+	    switch (data_type_tempka) {
+	    case CELL_TYPE:
+		d_tempka = (double)((CELL *) inrast_TEMPKA)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempka = (double)((FCELL *) inrast_TEMPKA)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempka = ((DCELL *) inrast_TEMPKA)[col];
+		break;
+	    }
+	    switch (data_type_alb) {
+	    case CELL_TYPE:
+		d_alb = (double)((CELL *) inrast_ALB)[col];
+		break;
+	    case FCELL_TYPE:
+		d_alb = (double)((FCELL *) inrast_ALB)[col];
+		break;
+	    case DCELL_TYPE:
+		d_alb = ((DCELL *) inrast_ALB)[col];
+		break;
+	    }
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_NDVI)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_NDVI)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = ((DCELL *) inrast_NDVI)[col];
+		break;
+	    }
+	    switch (data_type_uz) {
+	    case CELL_TYPE:
+		d_uz = (double)((CELL *) inrast_UZ)[col];
+		break;
+	    case FCELL_TYPE:
+		d_uz = (double)((FCELL *) inrast_UZ)[col];
+		break;
+	    case DCELL_TYPE:
+		d_uz = ((DCELL *) inrast_UZ)[col];
+		break;
+	    }
+	    if (DISP != NULL) {
+		switch (data_type_tempk) {
+		case CELL_TYPE:
+		    d_disp = (double)((CELL *) inrast_DISP)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_disp = (double)((FCELL *) inrast_DISP)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_disp = ((DCELL *) inrast_DISP)[col];
+		    break;
 		}
-		
+	    }
+	    else {
+		d_disp = -10.0;	/*negative, see inside functions */
+	    }
+	    if (Z0 != NULL) {
+		switch (data_type_z0) {
+		case CELL_TYPE:
+		    d_z0 = (double)((CELL *) inrast_Z0)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_z0 = (double)((FCELL *) inrast_Z0)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_z0 = ((DCELL *) inrast_Z0)[col];
+		    break;
+		}
+	    }
+	    else {
+		d_z0 = -10.0;	/*negative, see inside functions */
+	    }
+	    if (HV != NULL) {
+		switch (data_type_hv) {
+		case CELL_TYPE:
+		    d_hv = (double)((CELL *) inrast_HV)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_hv = (double)((FCELL *) inrast_HV)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_hv = ((DCELL *) inrast_HV)[col];
+		    break;
+		}
+	    }
+	    else {
+		d_hv = -10.0;	/*negative, see inside functions */
+	    }
+	    switch (data_type_z0s) {
+	    case CELL_TYPE:
+		d_z0s = (double)((CELL *) inrast_Z0S)[col];
+		break;
+	    case FCELL_TYPE:
+		d_z0s = (double)((FCELL *) inrast_Z0S)[col];
+		break;
+	    case DCELL_TYPE:
+		d_z0s = ((DCELL *) inrast_Z0S)[col];
+		break;
+	    }
+	    switch (data_type_w) {
+	    case CELL_TYPE:
+		d_w = (double)((CELL *) inrast_W)[col];
+		break;
+	    case FCELL_TYPE:
+		d_w = (double)((FCELL *) inrast_W)[col];
+		break;
+	    case DCELL_TYPE:
+		d_w = ((DCELL *) inrast_W)[col];
+		break;
+	    }
+	    switch (data_type_time) {
+	    case CELL_TYPE:
+		d_time = (double)((CELL *) inrast_TIME)[col];
+		break;
+	    case FCELL_TYPE:
+		d_time = (double)((FCELL *) inrast_TIME)[col];
+		break;
+	    case DCELL_TYPE:
+		d_time = ((DCELL *) inrast_TIME)[col];
+		break;
+	    }
+	    switch (data_type_sunh) {
+	    case CELL_TYPE:
+		d_sunh = (double)((CELL *) inrast_SUNH)[col];
+		break;
+	    case FCELL_TYPE:
+		d_sunh = (double)((FCELL *) inrast_SUNH)[col];
+		break;
+	    case DCELL_TYPE:
+		d_sunh = ((DCELL *) inrast_SUNH)[col];
+		break;
+	    }
 
-		/* write output line buffer to output raster file */
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error (_("Cannot write to <%s>"), ETa);
-			
-	}
-	/* free buffers and close input maps */
+	    /*Calculate Net radiation fractions */
+	    d_rn_g = rn_g(d_rnet, d_fv);
+	    d_rn_v = rn_v(d_rnet, d_fv);
 
-	G_free(inrast_RNET);
-	G_free(inrast_FV);
-	G_free(inrast_TEMPK);
-	G_free(inrast_TEMPKA);
-	G_free(inrast_ALB);
-	G_free(inrast_NDVI);
-	G_free(inrast_UZ);
-	if(DISP!=NULL){
-		G_free(inrast_DISP);
+	    /*Calculate temperature fractions */
+	    d_tempk_v = tempk_v(d_tempk, d_fv);
+	    d_tempk_g = tempk_g(d_tempk, d_tempk_v, d_fv);
+
+	    /*Calculate soil heat flux fractions */
+	    d_g0_g = g_0g(d_rnet);
+	    d_g0_v = g_0v(d_alb, d_ndvi, d_tempk_v, d_rnet);
+
+	    /*Data necessary for sensible heat flux calculations */
+	    if (d_disp < 0.0 && d_z0 < 0.0 && d_hv < 0.0) {
+		G_fatal_error(_("DISP, Z0 and HV are all negative, cannot continue, bailing out... Check your optional input files again"));
+	    }
+	    else {
+		d_ra = ra(d_disp, d_z0, d_hv, d_z, d_uz, d_tempka, d_tempk_v);
+		d_rg =
+		    rg(d_disp, d_z0, d_z0s, d_hv, d_z, d_w, d_uz, d_tempka,
+		       d_tempk_v);
+		d_rv =
+		    rv(d_disp, d_z0, d_hv, d_z, d_w, d_uz, d_tempka,
+		       d_tempk_v);
+	    }
+	    /*Calculate sensible heat flux fractions */
+	    d_h_g = h_g(d_tempk_g, d_tempk_v, d_tempka, d_rg, d_rv, d_ra);
+	    d_h_v = h_v(d_tempk_g, d_tempk_v, d_tempka, d_rg, d_rv, d_ra);
+
+	    /*Calculate LE */
+	    d_le_inst_v = d_rn_v - d_g0_v - d_h_v;
+	    d_le_inst_g = d_rn_g - d_g0_g - d_h_g;
+	    d_le_inst = d_le_inst_v + d_le_inst_g;
+
+	    /*Calculate ET */
+	    d_daily_et = daily_et(d_le_inst, d_time, d_sunh);
+
+
+	    if (zero->answer && d_daily_et < 0)
+		d_daily_et = 0.0;
+
+	    /* write calculated ETP to output line buffer */
+	    outrast[col] = d_daily_et;
 	}
-	if(Z0!=NULL){
-		G_free(inrast_Z0);
-	}
-	if(HV!=NULL){
-		G_free(inrast_HV);
-	}
-	G_free(inrast_TIME);
-	G_free(inrast_SUNH);
-	G_close_cell (infd_RNET);
-	G_close_cell (infd_FV);
-	G_close_cell (infd_TEMPK);
-	G_close_cell (infd_TEMPKA);
-	G_close_cell (infd_ALB);
-	G_close_cell (infd_NDVI);
-	G_close_cell (infd_UZ);
-	if(DISP!=NULL){
-		G_close_cell (infd_DISP);
-	}
-	if(Z0!=NULL){
-		G_close_cell (infd_Z0);
-	}
-	if(HV!=NULL){
-		G_close_cell (infd_HV);
-	}
-	G_close_cell (infd_TIME);
-	G_close_cell (infd_SUNH);
-	
-	/* generate color table between -20 and 20 */
-	G_make_rainbow_colors(&color, -20, 20);
-	G_write_colors(ETa,G_mapset(),&color);
 
-	G_short_history(ETa,"raster", &history);
-	G_command_history(&history);
-	G_write_history(ETa, &history);
 
-	/* free buffers and close output map */
-	G_free(outrast);
-	G_close_cell (outfd);
+	/* write output line buffer to output raster file */
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), ETa);
 
-	return (0);
+    }
+    /* free buffers and close input maps */
+
+    G_free(inrast_RNET);
+    G_free(inrast_FV);
+    G_free(inrast_TEMPK);
+    G_free(inrast_TEMPKA);
+    G_free(inrast_ALB);
+    G_free(inrast_NDVI);
+    G_free(inrast_UZ);
+    if (DISP != NULL) {
+	G_free(inrast_DISP);
+    }
+    if (Z0 != NULL) {
+	G_free(inrast_Z0);
+    }
+    if (HV != NULL) {
+	G_free(inrast_HV);
+    }
+    G_free(inrast_TIME);
+    G_free(inrast_SUNH);
+    G_close_cell(infd_RNET);
+    G_close_cell(infd_FV);
+    G_close_cell(infd_TEMPK);
+    G_close_cell(infd_TEMPKA);
+    G_close_cell(infd_ALB);
+    G_close_cell(infd_NDVI);
+    G_close_cell(infd_UZ);
+    if (DISP != NULL) {
+	G_close_cell(infd_DISP);
+    }
+    if (Z0 != NULL) {
+	G_close_cell(infd_Z0);
+    }
+    if (HV != NULL) {
+	G_close_cell(infd_HV);
+    }
+    G_close_cell(infd_TIME);
+    G_close_cell(infd_SUNH);
+
+    /* generate color table between -20 and 20 */
+    G_make_rainbow_colors(&color, -20, 20);
+    G_write_colors(ETa, G_mapset(), &color);
+
+    G_short_history(ETa, "raster", &history);
+    G_command_history(&history);
+    G_write_history(ETa, &history);
+
+    /* free buffers and close output map */
+    G_free(outrast);
+    G_close_cell(outfd);
+
+    return (0);
 }
-

Modified: grass-addons/gipe/i.evapo.TSA/tsa_daily_et.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_daily_et.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_daily_et.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,23 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Xie, X.Q., 1991: Estimation of daily evapo-transpiration (ET) 
- * from one time-of-day remotely sensed canopy temperature. 
- * Remote Sensing of Environment China, 6, pp.253-259.
- */ 
-
-#define PI 3.1415927
-
-double daily_et(double et_instantaneous, double time, double sunshine_hours)
-{
-	double 	n_e, result;
-	/*Daily ET hours*/
-	n_e = sunshine_hours - 2.0 ;
-	
-	result = et_instantaneous * (2 * n_e) / (PI * sin(PI*time/n_e)) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Xie, X.Q., 1991: Estimation of daily evapo-transpiration (ET) 
+     * from one time-of-day remotely sensed canopy temperature. 
+     * Remote Sensing of Environment China, 6, pp.253-259.
+     */ 
+    
+#define PI 3.1415927
+double daily_et(double et_instantaneous, double time,
+		 double sunshine_hours) 
+{
+    double n_e, result;
+
+    
+	/*Daily ET hours */ 
+	n_e = sunshine_hours - 2.0;
+    result = et_instantaneous * (2 * n_e) / (PI * sin(PI * time / n_e));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_g0g.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_g0g.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_g0g.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,17 +1,16 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double g_0g(double rnet)
-{
-	double 	result;
-	
-	result = (rnet * 0.4) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double g_0g(double rnet) 
+{
+    double result;
+
+    result = (rnet * 0.4);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_g0v.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_g0v.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_g0v.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,20 +1,18 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double g_0v(double bbalb, double ndvi, double tempk_v, double rnet)
-{
-	double 	a, b, result;
-	
-	a = (0.0032 * bbalb) + (bbalb * bbalb);
-	b = (1 - 0.978 * pow(ndvi,4));
-	
-	result = (rnet * (tempk_v-273.15) / bbalb) * a * b ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double g_0v(double bbalb, double ndvi, double tempk_v, double rnet) 
+{
+    double a, b, result;
+
+    a = (0.0032 * bbalb) + (bbalb * bbalb);
+    b = (1 - 0.978 * pow(ndvi, 4));
+    result = (rnet * (tempk_v - 273.15) / bbalb) * a * b;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_hg.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_hg.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_hg.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,21 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double h_g(double tempk_g, double tempk_v, double tempk_a, double r_g, double r_v, double r_a )
-{
-	double 	a, b, c, result;
-	
-	a = r_g * tempk_v - r_g * tempk_a + r_v * tempk_g - r_v * tempk_a;
-	b = r_v * r_g + r_g * r_a + r_v * r_a ;
-
-	c = r_a * ( a / b );
-	result = (tempk_g - c - tempk_a) / r_g ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double h_g(double tempk_g, double tempk_v, double tempk_a, double r_g,
+	    double r_v, double r_a) 
+{
+    double a, b, c, result;
+
+    a = r_g * tempk_v - r_g * tempk_a + r_v * tempk_g - r_v * tempk_a;
+    b = r_v * r_g + r_g * r_a + r_v * r_a;
+    c = r_a * (a / b);
+    result = (tempk_g - c - tempk_a) / r_g;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_hv.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_hv.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_hv.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,21 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double h_v(double tempk_g, double tempk_v, double tempk_a, double r_g, double r_v, double r_a )
-{
-	double 	a, b, c, result;
-	
-	a = r_g * tempk_v - r_g * tempk_a + r_v * tempk_g - r_v * tempk_a;
-	b = r_v * r_g + r_g * r_a + r_v * r_a ;
-
-	c = r_a * ( a / b );
-	result = (tempk_v - c - tempk_a) / r_v ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double h_v(double tempk_g, double tempk_v, double tempk_a, double r_g,
+	    double r_v, double r_a) 
+{
+    double a, b, c, result;
+
+    a = r_g * tempk_v - r_g * tempk_a + r_v * tempk_g - r_v * tempk_a;
+    b = r_v * r_g + r_g * r_a + r_v * r_a;
+    c = r_a * (a / b);
+    result = (tempk_v - c - tempk_a) / r_v;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_ra.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_ra.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_ra.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,66 +1,74 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- * 
- * d = zero plane displacement (m)
- * z0 = surface roughness length gouverning heat and vapour transfers (m)
- * h = vegetation height (m)
- * z = reference height of air temperature and humidity measurements (m)
- * u_z = wind speed at reference height z (m/s)
- * tempk_a = air temperature (at reference height z?) (K)
- * tempk_v = surface temperature of vegetation (K)
- *
- * If h (vegetation height) is given then d and z0 will be:
- * d = 0.63h
- * z0 = 0.13h
- * If d OR z0 are positive a relationship will find the second one
- * based on the equations above
- * If d AND z0 are positive they are used regardless of h values
- */
-
-double ra(double d, double z0, double h, double z, double u_z, double tempk_a, double tempk_v)
-{
-	double 	ra_0, molength, u_star, psi, result;
-	double vonKarman = 0.41; /*von Karman constant*/
-	double g = 9.81 ; /*gravitational acceleration (m/s)*/
-	
-
-	/*Deal with input of vegetation height*/
-	if ( h > 0.0 ){
-		d = 0.63*h;
-		z0 = 0.13*h;
-	/*Deal with input of displacement height*/
-	} else if (d > 0.0 && z0 < 0.0){
-		z0 = 0.13 * (d/0.63);
-	/*Deal with input of surface roughness length*/
-	} else if (d < 0.0 && z0 > 0.0){
-		d = 0.63 * (z0/0.13);
-	}
-
-	ra_0 = pow(log ((z-d)/z0),2) / ( pow(vonKarman,2)* u_z );
-		
-	/* psi, molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
-	 * An operational model for estimating evapotranspiration 
-	 * through Surface Energy Partitioning (RESEP).
-	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
-	 */
-	u_star = vonKarman * u_z / log((z-d)/z0);
-	molength = u_star*((tempk_v+tempk_a)/2)/(vonKarman*g*(tempk_a-tempk_v)/u_z) ;
-	
-	if( ((z-d)/molength) < -0.03 ){
-		psi = pow((1-16*((z-d)/molength)),0.5);
-	} else {
-		psi = (1+5*((z-d)/molength));
-	}
-	/* psi & ra: Wang, C.Y., Niu Z., Tang H.J. 2002. 
-	 * Technology of Earth Observation and Precision agriculture
-	 * (Beijing: Science Press)
-	 */
-	result = ra_0 * ( 1 + (psi*(z-d)/z0) ) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     * 
+     * d = zero plane displacement (m)
+     * z0 = surface roughness length gouverning heat and vapour transfers (m)
+     * h = vegetation height (m)
+     * z = reference height of air temperature and humidity measurements (m)
+     * u_z = wind speed at reference height z (m/s)
+     * tempk_a = air temperature (at reference height z?) (K)
+     * tempk_v = surface temperature of vegetation (K)
+     *
+     * If h (vegetation height) is given then d and z0 will be:
+     * d = 0.63h
+     * z0 = 0.13h
+     * If d OR z0 are positive a relationship will find the second one
+     * based on the equations above
+     * If d AND z0 are positive they are used regardless of h values
+     */ 
+double ra(double d, double z0, double h, double z, double u_z,
+	   double tempk_a, double tempk_v) 
+{
+    double ra_0, molength, u_star, psi, result;
+
+    double vonKarman = 0.41;	/*von Karman constant */
+
+    double g = 9.81;		/*gravitational acceleration (m/s) */
+
+    
+	/*Deal with input of vegetation height */ 
+	if (h > 0.0) {
+	d = 0.63 * h;
+	z0 = 0.13 * h;
+	
+	    /*Deal with input of displacement height */ 
+    }
+    else if (d > 0.0 && z0 < 0.0) {
+	z0 = 0.13 * (d / 0.63);
+	
+	    /*Deal with input of surface roughness length */ 
+    }
+    else if (d < 0.0 && z0 > 0.0) {
+	d = 0.63 * (z0 / 0.13);
+    }
+    ra_0 = pow(log((z - d) / z0), 2) / (pow(vonKarman, 2) * u_z);
+    
+	/* psi, molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
+	 * An operational model for estimating evapotranspiration 
+	 * through Surface Energy Partitioning (RESEP).
+	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
+	 */ 
+	u_star = vonKarman * u_z / log((z - d) / z0);
+    molength =
+	u_star * ((tempk_v + tempk_a) / 2) / (vonKarman * g *
+					      (tempk_a - tempk_v) / u_z);
+    if (((z - d) / molength) < -0.03) {
+	psi = pow((1 - 16 * ((z - d) / molength)), 0.5);
+    }
+    else {
+	psi = (1 + 5 * ((z - d) / molength));
+    }
+    
+	/* psi & ra: Wang, C.Y., Niu Z., Tang H.J. 2002. 
+	 * Technology of Earth Observation and Precision agriculture
+	 * (Beijing: Science Press)
+	 */ 
+	result = ra_0 * (1 + (psi * (z - d) / z0));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_rg.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_rg.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_rg.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,67 +1,75 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- *
- * d = zero plane displacement (m)
- * z0 = surface roughness length gouverning heat and vapour transfers (m)
- * h = vegetation height (m)
- * w = weight of leaf (g)
- * z = reference height of air temperature and humidity measurements (m)
- * u_z = wind speed at reference height (m/s)
- * u_h = wind speed at plant height (m/s)
- * tempk_a = air temperature (at reference height z?) (K)
- * tempk_v = surface temperature of vegetation (K)
- *
- * If h (vegetation height) is given then d and z0 will be:
- * d = 0.63h
- * z0 = 0.13h
- * If d OR z0 are positive a relationship will find the second one
- * based on the equations above
- * If d AND z0 are positive they are used regardless of h values
- */
-
-double rg(double d, double z0, double z0s, double h, double z, double w, double u_z, double tempk_a, double tempk_v)
-{
-	double 	alpha, molength, u_star, k_h, temp, result;
-	double vonKarman = 0.41; /* von Karman constant*/
-	double g = 9.81 ; /*gravitational acceleration (m/s)*/
-	
-
-	/*Deal with input of vegetation height*/
-	if ( h > 0.0 ){
-		d = 0.63*h;
-		z0 = 0.13*h;
-	/*Deal with input of displacement height*/
-	} else if (d > 0.0 && z0 < 0.0){
-		z0 = 0.13 * (d/0.63);
-	/*Deal with input of surface roughness length*/
-	} else if (d < 0.0 && z0 > 0.0){
-		d = 0.63 * (z0/0.13);
-	}
-
-	if ( h < 0.0 ) {
-		h = d / 0.63 ; 
-	}
-
-	/* molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
-	 * An operational model for estimating evapotranspiration 
-	 * through Surface Energy Partitioning (RESEP).
-	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
-	 */
-	u_star = vonKarman * u_z / log((z-d)/z0);
-	molength = u_star*((tempk_v+tempk_a)/2)/(vonKarman*g*(tempk_a-tempk_v)/u_z) ;
-	/* rv: Choudhury B.J. and Monteith J.L. 1988. 
-	 * A four-layer model for the heat budget of homogeneous land surfaces.
-	 * Quarterly Journal of the Royal Meteorological Society,114,pp.373-398.
-	 */
-	k_h = 1.5*pow(vonKarman,2)*(h-d)*u_z/(log((z-d)/z0));
-	alpha = 1.0 / ((d/h)*log((h-d)/z0));
-	temp = h*exp(alpha)/(alpha*k_h) ;
-	result = temp*(exp(-alpha*z0s/h)-exp(-alpha*(d+z0)/h));
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     *
+     * d = zero plane displacement (m)
+     * z0 = surface roughness length gouverning heat and vapour transfers (m)
+     * h = vegetation height (m)
+     * w = weight of leaf (g)
+     * z = reference height of air temperature and humidity measurements (m)
+     * u_z = wind speed at reference height (m/s)
+     * u_h = wind speed at plant height (m/s)
+     * tempk_a = air temperature (at reference height z?) (K)
+     * tempk_v = surface temperature of vegetation (K)
+     *
+     * If h (vegetation height) is given then d and z0 will be:
+     * d = 0.63h
+     * z0 = 0.13h
+     * If d OR z0 are positive a relationship will find the second one
+     * based on the equations above
+     * If d AND z0 are positive they are used regardless of h values
+     */ 
+double rg(double d, double z0, double z0s, double h, double z, double w,
+	   double u_z, double tempk_a, double tempk_v) 
+{
+    double alpha, molength, u_star, k_h, temp, result;
+
+    double vonKarman = 0.41;	/* von Karman constant */
+
+    double g = 9.81;		/*gravitational acceleration (m/s) */
+
+    
+	/*Deal with input of vegetation height */ 
+	if (h > 0.0) {
+	d = 0.63 * h;
+	z0 = 0.13 * h;
+	
+	    /*Deal with input of displacement height */ 
+    }
+    else if (d > 0.0 && z0 < 0.0) {
+	z0 = 0.13 * (d / 0.63);
+	
+	    /*Deal with input of surface roughness length */ 
+    }
+    else if (d < 0.0 && z0 > 0.0) {
+	d = 0.63 * (z0 / 0.13);
+    }
+    if (h < 0.0) {
+	h = d / 0.63;
+    }
+    
+	/* molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
+	 * An operational model for estimating evapotranspiration 
+	 * through Surface Energy Partitioning (RESEP).
+	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
+	 */ 
+	u_star = vonKarman * u_z / log((z - d) / z0);
+    molength =
+	u_star * ((tempk_v + tempk_a) / 2) / (vonKarman * g *
+					      (tempk_a - tempk_v) / u_z);
+    
+	/* rv: Choudhury B.J. and Monteith J.L. 1988. 
+	 * A four-layer model for the heat budget of homogeneous land surfaces.
+	 * Quarterly Journal of the Royal Meteorological Society,114,pp.373-398.
+	 */ 
+	k_h = 1.5 * pow(vonKarman, 2) * (h - d) * u_z / (log((z - d) / z0));
+    alpha = 1.0 / ((d / h) * log((h - d) / z0));
+    temp = h * exp(alpha) / (alpha * k_h);
+    result = temp * (exp(-alpha * z0s / h) - exp(-alpha * (d + z0) / h));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_rng.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_rng.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_rng.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,17 +1,16 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double rn_g(double rnet, double fv)
-{
-	double 	result;
-	
-	result = (1 - fv) * rnet ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double rn_g(double rnet, double fv) 
+{
+    double result;
+
+    result = (1 - fv) * rnet;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_rnv.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_rnv.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_rnv.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,17 +1,16 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double rn_v(double rnet, double fv)
-{
-	double 	result;
-	
-	result = fv * rnet ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double rn_v(double rnet, double fv) 
+{
+    double result;
+
+    result = fv * rnet;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_rv.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_rv.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_rv.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,60 +1,73 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- * 
- * d = zero plane displacement (m)
- * z0 = surface roughness length gouverning heat and vapour transfers (m)
- * h = vegetation height (m)
- * w = weight of leaf (g)
- * z = reference height of air temperature and humidity measurements (m)
- * u_z = wind speed at reference height (m/s)
- * u_h = wind speed at plant height (m/s)
- * tempk_a = air temperature (at reference height z?) (K)
- * tempk_v = surface temperature of vegetation (K)
- *
- * If h (vegetation height) is given then d and z0 will be:
- * d = 0.63h
- * z0 = 0.13h
- * If d OR z0 are positive a relationship will find the second one
- * based on the equations above
- * If d AND z0 are positive they are used regardless of h values
- */
-
-double rv(double d, double z0, double h, double z, double w, double u_z, double tempk_a, double tempk_v)
-{
-	double 	alpha, molength, u_star, u_h, result;
-	double vonKarman = 0.41; /* von Karman constant */
-	double g = 9.81 ; /* gravitational acceleration (m/s) */
-
-	/* Deal with input of vegetation height */
-	if ( h > 0.0 ){
-		d = 0.63*h;
-		z0 = 0.13*h;
-	/* Deal with input of displacement height */
-	} else if (d > 0.0 && z0 < 0.0){
-		z0 = 0.13 * (d/0.63);
-	/* Deal with input of surface roughness length */
-	} else if (d < 0.0 && z0 > 0.0){
-		d = 0.63 * (z0/0.13);
-	}
-	/* molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
-	 * An operational model for estimating evapotranspiration 
-	 * through Surface Energy Partitioning (RESEP).
-	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
-	 */
-	u_star = vonKarman * u_z / log((z-d)/z0);
-	molength = u_star*((tempk_v+tempk_a)/2)/(vonKarman*g*(tempk_a-tempk_v)/u_z) ;
-	/* rv: Choudhury B.J. and Monteith J.L. 1988. 
-	 * A four-layer model for the heat budget of homogeneous land surfaces.
-	 * Quarterly Journal of the Royal Meteorological Society,114,pp.373-398.
-	 */
-	u_h = 1.5*u_z*log((h-d)/z0)/(log((z-d)/z0));
-	alpha = 1.0 / ((d/h)*log((h-d)/z0));
-	result = 50.0*alpha*pow(abs(w/u_h),0.5) / (molength*(1-exp(-alpha/2))) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     * 
+     * d = zero plane displacement (m)
+     * z0 = surface roughness length gouverning heat and vapour transfers (m)
+     * h = vegetation height (m)
+     * w = weight of leaf (g)
+     * z = reference height of air temperature and humidity measurements (m)
+     * u_z = wind speed at reference height (m/s)
+     * u_h = wind speed at plant height (m/s)
+     * tempk_a = air temperature (at reference height z?) (K)
+     * tempk_v = surface temperature of vegetation (K)
+     *
+     * If h (vegetation height) is given then d and z0 will be:
+     * d = 0.63h
+     * z0 = 0.13h
+     * If d OR z0 are positive a relationship will find the second one
+     * based on the equations above
+     * If d AND z0 are positive they are used regardless of h values
+     */ 
+double rv(double d, double z0, double h, double z, double w, double u_z,
+	   double tempk_a, double tempk_v) 
+{
+    double alpha, molength, u_star, u_h, result;
+
+    double vonKarman = 0.41;	/* von Karman constant */
+
+    double g = 9.81;		/* gravitational acceleration (m/s) */
+
+    
+	/* Deal with input of vegetation height */ 
+	if (h > 0.0) {
+	d = 0.63 * h;
+	z0 = 0.13 * h;
+	
+	    /* Deal with input of displacement height */ 
+    }
+    else if (d > 0.0 && z0 < 0.0) {
+	z0 = 0.13 * (d / 0.63);
+	
+	    /* Deal with input of surface roughness length */ 
+    }
+    else if (d < 0.0 && z0 > 0.0) {
+	d = 0.63 * (z0 / 0.13);
+    }
+    
+	/* molength, u_star: Ambast S.K., Keshari A.K., Gosain A.K. 2002.
+	 * An operational model for estimating evapotranspiration 
+	 * through Surface Energy Partitioning (RESEP).
+	 * International Journal of Remote Sensing, 23, pp. 4917-4930.
+	 */ 
+	u_star = vonKarman * u_z / log((z - d) / z0);
+    molength =
+	u_star * ((tempk_v + tempk_a) / 2) / (vonKarman * g *
+					      (tempk_a - tempk_v) / u_z);
+    
+	/* rv: Choudhury B.J. and Monteith J.L. 1988. 
+	 * A four-layer model for the heat budget of homogeneous land surfaces.
+	 * Quarterly Journal of the Royal Meteorological Society,114,pp.373-398.
+	 */ 
+	u_h = 1.5 * u_z * log((h - d) / z0) / (log((z - d) / z0));
+    alpha = 1.0 / ((d / h) * log((h - d) / z0));
+    result =
+	50.0 * alpha * pow(abs(w / u_h),
+			   0.5) / (molength * (1 - exp(-alpha / 2)));
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_tempkg.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_tempkg.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_tempkg.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,20 +1,18 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-/* Temperature of ground from Tvegetation
- * Based on two sources pixel split
- * Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double tempk_g(double tempk, double tempk_v, double fv)
-{
-	double 	result;
-	
-	result = (tempk - (fv*tempk_v)) / (1 - fv) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ 
+    /* Temperature of ground from Tvegetation
+     * Based on two sources pixel split
+     * Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double tempk_g(double tempk, double tempk_v, double fv) 
+{
+    double result;
+
+    result = (tempk - (fv * tempk_v)) / (1 - fv);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.TSA/tsa_tempkv.c
===================================================================
--- grass-addons/gipe/i.evapo.TSA/tsa_tempkv.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.TSA/tsa_tempkv.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,22 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-/* Temperature of vegetation
- * Based on two sources pixel split
- * Chen et al., 2005. IJRS 26(8):1755-1762.
- * Estimation of daily evapotranspiration using a two-layer remote sensing model.
- */
-
-double tempk_v(double tempk, double fv)
-{
-	double 	a, result;
-	
-	a = (fv-(pow(fv,0.5)*pow(0.97,0.25)))/((pow((1-fv),0.5)*pow(0.93,0.25))-1-fv);
-	
-	result = tempk / ( fv - (1 - fv) * a ) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ 
+    /* Temperature of vegetation
+     * Based on two sources pixel split
+     * Chen et al., 2005. IJRS 26(8):1755-1762.
+     * Estimation of daily evapotranspiration using a two-layer remote sensing model.
+     */ 
+double tempk_v(double tempk, double fv) 
+{
+    double a, result;
+
+    a =
+	(fv -
+	 (pow(fv, 0.5) * pow(0.97, 0.25))) / ((pow((1 - fv), 0.5) *
+					       pow(0.93, 0.25)) - 1 - fv);
+    result = tempk / (fv - (1 - fv) * a);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/et_pot_day.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/et_pot_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/et_pot_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,16 +1,15 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Potential ET after Bastiaanssen (1995)*/
-
-double et_pot_day( double rnetd, double tempk, double roh_w )
-{
-	double latent, result;
-
-	latent=(2.501-(0.002361*(tempk-273.15)))*1000000.0;
-	result = (rnetd*86400*1000.0)/(latent*roh_w);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Potential ET after Bastiaanssen (1995) */ 
+double et_pot_day(double rnetd, double tempk, double roh_w) 
+{
+    double latent, result;
+
+    latent = (2.501 - (0.002361 * (tempk - 273.15))) * 1000000.0;
+    result = (rnetd * 86400 * 1000.0) / (latent * roh_w);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/main.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,530 +1,587 @@
-/****************************************************************************
- *
- * MODULE:       i.evapo.potrad
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Creates a map of potential evapotranspiration following
- *               the condition that all net radiation becomes ET
- *               (thus it can be called a "radiative ET pot")
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double solar_day(double lat, double doy, double tsw );
-double solar_day_3d(double lat, double doy, double tsw, double slope, double aspect);
-double r_net_day( double bbalb, double solar, double tsw );
-double r_net_day_bandara98( double surface_albedo, double solar_day, double apparent_atm_emissivity, double surface_emissivity, double air_temperature );
-double et_pot_day( double rnetd, double tempk, double roh_w );
-
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *input5, *input6, *input7, *input8;
-	struct Option *input9, *input10, *input11;
-	struct Option *output1, *output2;
-	
-	struct Flag *flag1, *flag2, *flag3, *flag4;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1,*result2; /*output raster name*/
-	/*File Descriptors*/
-	int infd_albedo, infd_tempk, infd_lat, infd_doy, infd_tsw;
-	int infd_slope, infd_aspect;
-	int infd_tair, infd_e0;
-	int outfd1, outfd2;
-	
-	char *albedo,*tempk,*lat,*doy,*tsw,*slope,*aspect,*tair,*e0;
-	double roh_w, e_atm;
-	int i=0,j=0;
-	
-	void *inrast_albedo, *inrast_tempk, *inrast_lat;
-	void *inrast_doy, *inrast_tsw;
-	void *inrast_slope, *inrast_aspect;
-	void *inrast_tair, *inrast_e0;
-
-	DCELL *outrast1, *outrast2;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_tempk;
-	RASTER_MAP_TYPE data_type_lat;
-	RASTER_MAP_TYPE data_type_doy;
-	RASTER_MAP_TYPE data_type_tsw;
-	RASTER_MAP_TYPE data_type_slope;
-	RASTER_MAP_TYPE data_type_aspect;
-	RASTER_MAP_TYPE data_type_tair;
-	RASTER_MAP_TYPE data_type_e0;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("Potential ET, evapotranspiration");
-	module->description = _("Potential evapotranspiration, radiative method after Bastiaanssen (1995)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("albedo");
-	input1->description=_("Name of the Albedo layer [0.0-1.0]");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("tempk");
-	input2->description=_("Name of the temperature layer [Degree Kelvin]");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("lat");
-	input3->description=_("Name of the degree latitude layer [dd.ddd]");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("doy");
-	input4->description=_("Name of the Day of Year layer [0.0-366.0]");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("tsw");
-	input5->description=_("Name of the single-way transmissivity layer [0.05-1.0], defaults to 1.0 if no input file");
-
-	input6 = G_define_option() ;
-	input6->key        =_("roh_w");
-	input6->type       = TYPE_DOUBLE;
-	input6->required   = YES;
-	input6->gisprompt  =_("value, parameter");
-	input6->description=_("Value of the density of fresh water ~[1000-1020]");
-	input6->answer     =_("1005.0");
-
-	input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input7->key        =_("slope");
-	input7->required   = NO;
-	input7->description=_("Name of the Slope layer ~[0-90]");
-	input7->guisection = _("Optional");
-
-	input8 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input8->key        =_("aspect");
-	input8->required   = NO;
-	input8->description=_("Name of the Aspect layer ~[0-360]");
-	input8->guisection = _("Optional");
-
-	input9 = G_define_option() ;
-	input9->key        =_("e_atm");
-	input9->type       = TYPE_DOUBLE;
-	input9->required   = NO;
-	input9->gisprompt  =_("value, parameter");
-	input9->description=_("Value of the apparent atmospheric emissivity (Bandara, 1998 used 0.845 for Sri Lanka)");
-	input9->answer     =_("0.845");
-	input9->guisection = _("Optional");
-
-	input10 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input10->key        =_("t_air");
-	input10->required   = NO;
-	input10->description=_("Name of the Air Temperature layer [Kelvin], use with -b");
-	input10->guisection = _("Optional");
-
-	input11 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input11->key        =_("e0");
-	input11->required   = NO;
-	input11->description=_("Name of the Surface Emissivity layer [-], use with -b");
-	input11->guisection = _("Optional");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("OUTPUT: Name of the Potential ET layer");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("rnetd");
-	output2->required   = NO;
-	output2->description=_("OUTPUT: Name of the Diurnal Net Radiation layer");
-	output2->guisection = _("Optional");
-
-	flag1 = G_define_flag();
-	flag1->key = 'r';
-	flag1->description = _("Output Diurnal Net Radiation (for i.eb.eta)");
-	
-	flag2 = G_define_flag();
-	flag2->key = 'd';
-	flag2->description = _("Slope/Aspect correction");
-
-	flag3 = G_define_flag();
-	flag3->key = 'b';
-	flag3->description = _("Net Radiation Bandara (1998), generic Longwave calculation, need apparent atmospheric emissivity, Air temperature and surface emissivity inputs");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	albedo	 	= input1->answer;
-	tempk	 	= input2->answer;
-	lat		= input3->answer;
-	doy	 	= input4->answer;
-	tsw	 	= input5->answer;
-	roh_w	 	= atof(input6->answer);
-	slope	 	= input7->answer;
-	aspect	 	= input8->answer;
-	e_atm	 	= atof(input9->answer);
-	tair	 	= input10->answer;
-	e0	 	= input11->answer;
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	/***************************************************/
-	mapset = G_find_cell2(albedo, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), albedo);
-	}
-	data_type_albedo = G_raster_map_type(albedo,mapset);
-	if ( (infd_albedo = G_open_cell_old (albedo,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), albedo);
-	if (G_get_cellhd (albedo, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), albedo);
-	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
-	/***************************************************/
-	mapset = G_find_cell2 (tempk, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), tempk);
-	}
-	data_type_tempk = G_raster_map_type(tempk,mapset);
-	if ( (infd_tempk = G_open_cell_old (tempk,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tempk);
-	if (G_get_cellhd (tempk, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tempk);
-	inrast_tempk = G_allocate_raster_buf(data_type_tempk);
-	/***************************************************/
-	mapset = G_find_cell2 (lat, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), lat);
-	}
-	data_type_lat = G_raster_map_type(lat,mapset);
-	if ( (infd_lat = G_open_cell_old (lat,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), lat);
-	if (G_get_cellhd (lat, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), lat);
-	inrast_lat = G_allocate_raster_buf(data_type_lat);
-	/***************************************************/
-	mapset = G_find_cell2 (doy, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), doy);
-	}
-	data_type_doy = G_raster_map_type(doy,mapset);
-	if ( (infd_doy = G_open_cell_old (doy,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), doy);
-	if (G_get_cellhd (doy, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), doy);
-	inrast_doy = G_allocate_raster_buf(data_type_doy);
-	/***************************************************/
-	mapset = G_find_cell2 (tsw, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), tsw);
-	}
-	data_type_tsw = G_raster_map_type(tsw,mapset);
-	if ( (infd_tsw = G_open_cell_old (tsw,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), tsw);
-	if (G_get_cellhd (tsw, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), tsw);
-	inrast_tsw = G_allocate_raster_buf(data_type_tsw);
-	/***************************************************/
-	if(flag2->answer){
-		mapset = G_find_cell2 (slope, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), slope);
-		}
-		data_type_slope = G_raster_map_type(slope,mapset);
-		if ( (infd_slope = G_open_cell_old (slope,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), slope);
-		if (G_get_cellhd (slope, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), slope);
-		inrast_slope = G_allocate_raster_buf(data_type_slope);
-	}
-	/***************************************************/
-	if(flag2->answer){
-		mapset = G_find_cell2 (aspect, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), aspect);
-		}
-		data_type_aspect = G_raster_map_type(aspect,mapset);
-		if ( (infd_aspect = G_open_cell_old (aspect,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), aspect);
-		if (G_get_cellhd (aspect, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), aspect);
-		inrast_aspect = G_allocate_raster_buf(data_type_aspect);
-	}
-	/***************************************************/
-	if(flag3->answer){
-		mapset = G_find_cell2 (tair, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), tair);
-		}
-		data_type_tair = G_raster_map_type(tair,mapset);
-		if ( (infd_tair = G_open_cell_old (tair,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), tair);
-		if (G_get_cellhd (tair, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), tair);
-		inrast_tair = G_allocate_raster_buf(data_type_tair);
-	}
-	/***************************************************/
-	if(flag3->answer){
-		mapset = G_find_cell2 (e0, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("Cell file [%s] not found"), e0);
-		}
-		data_type_e0 = G_raster_map_type(e0,mapset);
-		if ( (infd_e0 = G_open_cell_old (e0,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), e0);
-		if (G_get_cellhd (e0, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), e0);
-		inrast_e0 = G_allocate_raster_buf(data_type_e0);
-	}
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if(result2){
-		outrast2 = G_allocate_raster_buf(data_type_output);
-	}
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if(result2){
-		if ((outfd2 = G_open_raster_new (result2,data_type_output))< 0)
-			G_fatal_error(_("Could not open <%s>"),result2);
-	}
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_albedo;
-		DCELL d_tempk;
-		DCELL d_lat;
-		DCELL d_doy;
-		DCELL d_tsw;
-		DCELL d_solar;
-		DCELL d_rnetd;
-		DCELL d_slope;
-		DCELL d_aspect;
-		DCELL d_tair;
-		DCELL d_e0;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		if(G_get_raster_row(infd_tempk,inrast_tempk,row,data_type_tempk)<0)
-			G_fatal_error(_("Could not read from <%s>"),tempk);
-		if(G_get_raster_row(infd_lat,inrast_lat,row,data_type_lat)<0)
-			G_fatal_error(_("Could not read from <%s>"),lat);
-		if(G_get_raster_row(infd_doy,inrast_doy,row,data_type_doy)<0)
-			G_fatal_error(_("Could not read from <%s>"),doy);
-		if(G_get_raster_row(infd_tsw,inrast_tsw,row,data_type_tsw)<0)
-			G_fatal_error(_("Could not read from <%s>"),tsw);
-		if(flag2->answer){
-			if(G_get_raster_row(infd_slope,inrast_slope,row,data_type_slope)<0)
-				G_fatal_error(_("Could not read from <%s>"),slope);
-			if(G_get_raster_row(infd_aspect,inrast_aspect,row,data_type_aspect)<0)
-				G_fatal_error(_("Could not read from <%s>"),aspect);
-		}
-		if(flag3->answer){
-			if(G_get_raster_row(infd_tair,inrast_tair,row,data_type_tair)<0)
-				G_fatal_error(_("Could not read from <%s>"),tair);
-			if(G_get_raster_row(infd_e0,inrast_e0,row,data_type_e0)<0)
-				G_fatal_error(_("Could not read from <%s>"),e0);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = (double) ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			switch(data_type_tempk){
-				case CELL_TYPE:
-					d_tempk = (double) ((CELL *) inrast_tempk)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempk = (double) ((FCELL *) inrast_tempk)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempk = (double) ((DCELL *) inrast_tempk)[col];
-					break;
-			}
-			switch(data_type_lat){
-				case CELL_TYPE:
-					d_lat = (double) ((CELL *) inrast_lat)[col];
-					break;
-				case FCELL_TYPE:
-					d_lat = (double) ((FCELL *) inrast_lat)[col];
-					break;
-				case DCELL_TYPE:
-					d_lat = (double) ((DCELL *) inrast_lat)[col];
-					break;
-			}
-			switch(data_type_doy){
-				case CELL_TYPE:
-					d_doy = (double) ((CELL *) inrast_doy)[col];
-					break;
-				case FCELL_TYPE:
-					d_doy = (double) ((FCELL *) inrast_doy)[col];
-					break;
-				case DCELL_TYPE:
-					d_doy = (double) ((DCELL *) inrast_doy)[col];
-					break;
-			}
-			switch(data_type_tsw){
-				case CELL_TYPE:
-					d_tsw = (double) ((CELL *) inrast_tsw)[col];
-					break;
-				case FCELL_TYPE:
-					d_tsw = (double) ((FCELL *) inrast_tsw)[col];
-					break;
-				case DCELL_TYPE:
-					d_tsw = (double) ((DCELL *) inrast_tsw)[col];
-					break;
-			}
-			if(flag2->answer){
-				switch(data_type_slope){
-					case CELL_TYPE:
-						d_slope = (double) ((CELL *) inrast_slope)[col];
-						break;
-					case FCELL_TYPE:
-						d_slope = (double) ((FCELL *) inrast_slope)[col];
-						break;
-					case DCELL_TYPE:
-						d_slope = (double) ((DCELL *) inrast_slope)[col];
-						break;
-				}
-				switch(data_type_aspect){
-					case CELL_TYPE:
-						d_aspect = (double) ((CELL *) inrast_aspect)[col];
-						break;
-					case FCELL_TYPE:
-						d_aspect = (double) ((FCELL *) inrast_aspect)[col];
-						break;
-					case DCELL_TYPE:
-						d_aspect = (double) ((DCELL *) inrast_aspect)[col];
-						break;
-				}
-			}
-			if(flag3->answer){
-				switch(data_type_tair){
-					case CELL_TYPE:
-						d_tair = (double) ((CELL *) inrast_tair)[col];
-						break;
-					case FCELL_TYPE:
-						d_tair = (double) ((FCELL *) inrast_tair)[col];
-						break;
-					case DCELL_TYPE:
-						d_tair = (double) ((DCELL *) inrast_tair)[col];
-						break;
-				}
-				switch(data_type_e0){
-					case CELL_TYPE:
-						d_e0 = (double) ((CELL *) inrast_e0)[col];
-						break;
-					case FCELL_TYPE:
-						d_e0 = (double) ((FCELL *) inrast_e0)[col];
-						break;
-					case DCELL_TYPE:
-						d_e0 = (double) ((DCELL *) inrast_e0)[col];
-						break;
-				}
-			}
-			if(G_is_d_null_value(&d_albedo)||G_is_d_null_value(&d_tempk)||
-			(d_tempk<10.0)||G_is_d_null_value(&d_lat)||
-			G_is_d_null_value(&d_doy)||G_is_d_null_value(&d_tsw)||
-			((flag2->answer)&&G_is_d_null_value(&d_slope))||
-			((flag2->answer)&&G_is_d_null_value(&d_aspect))||
-			((flag3->answer)&&G_is_d_null_value(&d_tair))||
-			((flag3->answer)&&G_is_d_null_value(&d_e0))){
-				G_set_d_null_value(&outrast1[col],1);
-				if (result2)
-					G_set_d_null_value(&outrast2[col],1);
-			} else {
-				if(flag2->answer){
-					d_solar = solar_day_3d(d_lat,d_doy,d_tsw,d_slope,d_aspect);
-				}else {
-					d_solar = solar_day(d_lat, d_doy, d_tsw );
-				}
-				if(flag3->answer){
-					d_rnetd = r_net_day_bandara98(d_albedo, d_solar,e_atm, d_e0, d_tair);
-				} else {
-					d_rnetd = r_net_day(d_albedo,d_solar,d_tsw);
-				}
-				if(result2){
-					outrast2[col] = d_rnetd;
-				}
-				d = et_pot_day(d_rnetd,d_tempk,roh_w);
-				d = d * d_tsw;
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-		if(result2){
-			if (G_put_raster_row (outfd2, outrast2, data_type_output) < 0)
-				G_fatal_error(_("Cannot write to output raster file"));
-		}
-	}
-
-	G_free (inrast_albedo);
-	G_free (inrast_tempk);
-	G_free (inrast_lat);
-	G_free (inrast_doy);
-	G_close_cell (infd_albedo);
-	G_close_cell (infd_tempk);
-	G_close_cell (infd_lat);
-	G_close_cell (infd_doy);
-
-	G_free (inrast_tsw);
-	G_close_cell (infd_tsw);
-	if (flag3->answer){
-		G_free (inrast_tair);
-		G_close_cell (infd_tair);
-		G_free (inrast_e0);
-		G_close_cell (infd_e0);
-	}	
-
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	if (result2){
-		G_free (outrast2);
-		G_close_cell (outfd2);
-		/* Color rule */
-		G_init_colors(&colors);
-		G_add_color_rule(0,0,0,0,400,255,255,255,&colors);
-		/* Metadata */
-		G_short_history(result2, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result2,&history);
-	}
-
-	/* Color rule */
-	G_init_colors(&colors);
-	G_add_color_rule(0,0,0,0,10,255,255,255,&colors);
-
-	/* Metadata */
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.evapo.potrad
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Creates a map of potential evapotranspiration following
+ *               the condition that all net radiation becomes ET
+ *               (thus it can be called a "radiative ET pot")
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double solar_day(double lat, double doy, double tsw);
+
+double solar_day_3d(double lat, double doy, double tsw, double slope,
+		     double aspect);
+double r_net_day(double bbalb, double solar, double tsw);
+
+double r_net_day_bandara98(double surface_albedo, double solar_day,
+			    double apparent_atm_emissivity,
+			    double surface_emissivity,
+			    double air_temperature);
+double et_pot_day(double rnetd, double tempk, double roh_w);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *input5, *input6, *input7, *input8;
+
+    struct Option *input9, *input10, *input11;
+
+    struct Option *output1, *output2;
+
+    struct Flag *flag1, *flag2, *flag3, *flag4;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1, *result2;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_albedo, infd_tempk, infd_lat, infd_doy, infd_tsw;
+
+    int infd_slope, infd_aspect;
+
+    int infd_tair, infd_e0;
+
+    int outfd1, outfd2;
+
+    char *albedo, *tempk, *lat, *doy, *tsw, *slope, *aspect, *tair, *e0;
+
+    double roh_w, e_atm;
+
+    int i = 0, j = 0;
+
+    void *inrast_albedo, *inrast_tempk, *inrast_lat;
+
+    void *inrast_doy, *inrast_tsw;
+
+    void *inrast_slope, *inrast_aspect;
+
+    void *inrast_tair, *inrast_e0;
+
+    DCELL * outrast1, *outrast2;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_albedo;
+    RASTER_MAP_TYPE data_type_tempk;
+    RASTER_MAP_TYPE data_type_lat;
+    RASTER_MAP_TYPE data_type_doy;
+    RASTER_MAP_TYPE data_type_tsw;
+    RASTER_MAP_TYPE data_type_slope;
+    RASTER_MAP_TYPE data_type_aspect;
+    RASTER_MAP_TYPE data_type_tair;
+    RASTER_MAP_TYPE data_type_e0;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("Potential ET, evapotranspiration");
+    module->description =
+	_("Potential evapotranspiration, radiative method after Bastiaanssen (1995)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("albedo");
+    input1->description = _("Name of the Albedo layer [0.0-1.0]");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("tempk");
+    input2->description = _("Name of the temperature layer [Degree Kelvin]");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("lat");
+    input3->description = _("Name of the degree latitude layer [dd.ddd]");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("doy");
+    input4->description = _("Name of the Day of Year layer [0.0-366.0]");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("tsw");
+    input5->description =
+	_("Name of the single-way transmissivity layer [0.05-1.0], defaults to 1.0 if no input file");
+    input6 = G_define_option();
+    input6->key = _("roh_w");
+    input6->type = TYPE_DOUBLE;
+    input6->required = YES;
+    input6->gisprompt = _("value, parameter");
+    input6->description =
+	_("Value of the density of fresh water ~[1000-1020]");
+    input6->answer = _("1005.0");
+    input7 = G_define_standard_option(G_OPT_R_INPUT);
+    input7->key = _("slope");
+    input7->required = NO;
+    input7->description = _("Name of the Slope layer ~[0-90]");
+    input7->guisection = _("Optional");
+    input8 = G_define_standard_option(G_OPT_R_INPUT);
+    input8->key = _("aspect");
+    input8->required = NO;
+    input8->description = _("Name of the Aspect layer ~[0-360]");
+    input8->guisection = _("Optional");
+    input9 = G_define_option();
+    input9->key = _("e_atm");
+    input9->type = TYPE_DOUBLE;
+    input9->required = NO;
+    input9->gisprompt = _("value, parameter");
+    input9->description =
+	_("Value of the apparent atmospheric emissivity (Bandara, 1998 used 0.845 for Sri Lanka)");
+    input9->answer = _("0.845");
+    input9->guisection = _("Optional");
+    input10 = G_define_standard_option(G_OPT_R_INPUT);
+    input10->key = _("t_air");
+    input10->required = NO;
+    input10->description =
+	_("Name of the Air Temperature layer [Kelvin], use with -b");
+    input10->guisection = _("Optional");
+    input11 = G_define_standard_option(G_OPT_R_INPUT);
+    input11->key = _("e0");
+    input11->required = NO;
+    input11->description =
+	_("Name of the Surface Emissivity layer [-], use with -b");
+    input11->guisection = _("Optional");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("OUTPUT: Name of the Potential ET layer");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("rnetd");
+    output2->required = NO;
+    output2->description =
+	_("OUTPUT: Name of the Diurnal Net Radiation layer");
+    output2->guisection = _("Optional");
+    flag1 = G_define_flag();
+    flag1->key = 'r';
+    flag1->description = _("Output Diurnal Net Radiation (for i.eb.eta)");
+    flag2 = G_define_flag();
+    flag2->key = 'd';
+    flag2->description = _("Slope/Aspect correction");
+    flag3 = G_define_flag();
+    flag3->key = 'b';
+    flag3->description =
+	_("Net Radiation Bandara (1998), generic Longwave calculation, need apparent atmospheric emissivity, Air temperature and surface emissivity inputs");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    albedo = input1->answer;
+    tempk = input2->answer;
+    lat = input3->answer;
+    doy = input4->answer;
+    tsw = input5->answer;
+    roh_w = atof(input6->answer);
+    slope = input7->answer;
+    aspect = input8->answer;
+    e_atm = atof(input9->answer);
+    tair = input10->answer;
+    e0 = input11->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(albedo, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), albedo);
+    }
+    data_type_albedo = G_raster_map_type(albedo, mapset);
+    if ((infd_albedo = G_open_cell_old(albedo, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+    if (G_get_cellhd(albedo, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), albedo);
+    inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tempk, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), tempk);
+    }
+    data_type_tempk = G_raster_map_type(tempk, mapset);
+    if ((infd_tempk = G_open_cell_old(tempk, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tempk);
+    if (G_get_cellhd(tempk, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tempk);
+    inrast_tempk = G_allocate_raster_buf(data_type_tempk);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lat, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), lat);
+    }
+    data_type_lat = G_raster_map_type(lat, mapset);
+    if ((infd_lat = G_open_cell_old(lat, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lat);
+    if (G_get_cellhd(lat, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), lat);
+    inrast_lat = G_allocate_raster_buf(data_type_lat);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(doy, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), doy);
+    }
+    data_type_doy = G_raster_map_type(doy, mapset);
+    if ((infd_doy = G_open_cell_old(doy, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), doy);
+    if (G_get_cellhd(doy, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), doy);
+    inrast_doy = G_allocate_raster_buf(data_type_doy);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(tsw, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), tsw);
+    }
+    data_type_tsw = G_raster_map_type(tsw, mapset);
+    if ((infd_tsw = G_open_cell_old(tsw, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), tsw);
+    if (G_get_cellhd(tsw, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), tsw);
+    inrast_tsw = G_allocate_raster_buf(data_type_tsw);
+    
+
+	/***************************************************/ 
+	if (flag2->answer) {
+	mapset = G_find_cell2(slope, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), slope);
+	}
+	data_type_slope = G_raster_map_type(slope, mapset);
+	if ((infd_slope = G_open_cell_old(slope, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), slope);
+	if (G_get_cellhd(slope, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), slope);
+	inrast_slope = G_allocate_raster_buf(data_type_slope);
+    }
+    
+
+	/***************************************************/ 
+	if (flag2->answer) {
+	mapset = G_find_cell2(aspect, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), aspect);
+	}
+	data_type_aspect = G_raster_map_type(aspect, mapset);
+	if ((infd_aspect = G_open_cell_old(aspect, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), aspect);
+	if (G_get_cellhd(aspect, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), aspect);
+	inrast_aspect = G_allocate_raster_buf(data_type_aspect);
+    }
+    
+
+	/***************************************************/ 
+	if (flag3->answer) {
+	mapset = G_find_cell2(tair, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), tair);
+	}
+	data_type_tair = G_raster_map_type(tair, mapset);
+	if ((infd_tair = G_open_cell_old(tair, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), tair);
+	if (G_get_cellhd(tair, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), tair);
+	inrast_tair = G_allocate_raster_buf(data_type_tair);
+    }
+    
+
+	/***************************************************/ 
+	if (flag3->answer) {
+	mapset = G_find_cell2(e0, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), e0);
+	}
+	data_type_e0 = G_raster_map_type(e0, mapset);
+	if ((infd_e0 = G_open_cell_old(e0, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), e0);
+	if (G_get_cellhd(e0, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), e0);
+	inrast_e0 = G_allocate_raster_buf(data_type_e0);
+    }
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if (result2) {
+	outrast2 = G_allocate_raster_buf(data_type_output);
+    }
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if (result2) {
+	if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result2);
+    }
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_albedo;
+	DCELL d_tempk;
+	DCELL d_lat;
+	DCELL d_doy;
+	DCELL d_tsw;
+	DCELL d_solar;
+	DCELL d_rnetd;
+	DCELL d_slope;
+	DCELL d_aspect;
+	DCELL d_tair;
+	DCELL d_e0;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row
+		(infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), albedo);
+	if (G_get_raster_row(infd_tempk, inrast_tempk, row, data_type_tempk)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tempk);
+	if (G_get_raster_row(infd_lat, inrast_lat, row, data_type_lat) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), lat);
+	if (G_get_raster_row(infd_doy, inrast_doy, row, data_type_doy) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), doy);
+	if (G_get_raster_row(infd_tsw, inrast_tsw, row, data_type_tsw) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), tsw);
+	if (flag2->answer) {
+	    if (G_get_raster_row
+		 (infd_slope, inrast_slope, row, data_type_slope) < 0)
+		G_fatal_error(_("Could not read from <%s>"), slope);
+	    if (G_get_raster_row
+		 (infd_aspect, inrast_aspect, row, data_type_aspect) < 0)
+		G_fatal_error(_("Could not read from <%s>"), aspect);
+	}
+	if (flag3->answer) {
+	    if (G_get_raster_row(infd_tair, inrast_tair, row, data_type_tair)
+		 < 0)
+		G_fatal_error(_("Could not read from <%s>"), tair);
+	    if (G_get_raster_row(infd_e0, inrast_e0, row, data_type_e0) < 0)
+		G_fatal_error(_("Could not read from <%s>"), e0);
+	}
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_albedo) {
+	    case CELL_TYPE:
+		d_albedo = (double)((CELL *) inrast_albedo)[col];
+		break;
+	    case FCELL_TYPE:
+		d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		break;
+	    case DCELL_TYPE:
+		d_albedo = (double)((DCELL *) inrast_albedo)[col];
+		break;
+	    }
+	    switch (data_type_tempk) {
+	    case CELL_TYPE:
+		d_tempk = (double)((CELL *) inrast_tempk)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempk = (double)((FCELL *) inrast_tempk)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempk = (double)((DCELL *) inrast_tempk)[col];
+		break;
+	    }
+	    switch (data_type_lat) {
+	    case CELL_TYPE:
+		d_lat = (double)((CELL *) inrast_lat)[col];
+		break;
+	    case FCELL_TYPE:
+		d_lat = (double)((FCELL *) inrast_lat)[col];
+		break;
+	    case DCELL_TYPE:
+		d_lat = (double)((DCELL *) inrast_lat)[col];
+		break;
+	    }
+	    switch (data_type_doy) {
+	    case CELL_TYPE:
+		d_doy = (double)((CELL *) inrast_doy)[col];
+		break;
+	    case FCELL_TYPE:
+		d_doy = (double)((FCELL *) inrast_doy)[col];
+		break;
+	    case DCELL_TYPE:
+		d_doy = (double)((DCELL *) inrast_doy)[col];
+		break;
+	    }
+	    switch (data_type_tsw) {
+	    case CELL_TYPE:
+		d_tsw = (double)((CELL *) inrast_tsw)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tsw = (double)((FCELL *) inrast_tsw)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tsw = (double)((DCELL *) inrast_tsw)[col];
+		break;
+	    }
+	    if (flag2->answer) {
+		switch (data_type_slope) {
+		case CELL_TYPE:
+		    d_slope = (double)((CELL *) inrast_slope)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_slope = (double)((FCELL *) inrast_slope)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_slope = (double)((DCELL *) inrast_slope)[col];
+		    break;
+		}
+		switch (data_type_aspect) {
+		case CELL_TYPE:
+		    d_aspect = (double)((CELL *) inrast_aspect)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_aspect = (double)((FCELL *) inrast_aspect)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_aspect = (double)((DCELL *) inrast_aspect)[col];
+		    break;
+		}
+	    }
+	    if (flag3->answer) {
+		switch (data_type_tair) {
+		case CELL_TYPE:
+		    d_tair = (double)((CELL *) inrast_tair)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_tair = (double)((FCELL *) inrast_tair)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_tair = (double)((DCELL *) inrast_tair)[col];
+		    break;
+		}
+		switch (data_type_e0) {
+		case CELL_TYPE:
+		    d_e0 = (double)((CELL *) inrast_e0)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_e0 = (double)((FCELL *) inrast_e0)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_e0 = (double)((DCELL *) inrast_e0)[col];
+		    break;
+		}
+	    }
+	    if (G_is_d_null_value(&d_albedo) || G_is_d_null_value(&d_tempk)
+		 || (d_tempk < 10.0) || G_is_d_null_value(&d_lat) ||
+		 G_is_d_null_value(&d_doy) || G_is_d_null_value(&d_tsw) ||
+		 ((flag2->answer) && G_is_d_null_value(&d_slope)) ||
+		 ((flag2->answer) && G_is_d_null_value(&d_aspect)) ||
+		 ((flag3->answer) && G_is_d_null_value(&d_tair)) ||
+		 ((flag3->answer) && G_is_d_null_value(&d_e0))) {
+		G_set_d_null_value(&outrast1[col], 1);
+		if (result2)
+		    G_set_d_null_value(&outrast2[col], 1);
+	    }
+	    else {
+		if (flag2->answer) {
+		    d_solar =
+			solar_day_3d(d_lat, d_doy, d_tsw, d_slope, d_aspect);
+		}
+		else {
+		    d_solar = solar_day(d_lat, d_doy, d_tsw);
+		}
+		if (flag3->answer) {
+		    d_rnetd =
+			r_net_day_bandara98(d_albedo, d_solar, e_atm, d_e0,
+					    d_tair);
+		}
+		else {
+		    d_rnetd = r_net_day(d_albedo, d_solar, d_tsw);
+		}
+		if (result2) {
+		    outrast2[col] = d_rnetd;
+		}
+		d = et_pot_day(d_rnetd, d_tempk, roh_w);
+		d = d * d_tsw;
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	if (result2) {
+	    if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+		G_fatal_error(_("Cannot write to output raster file"));
+	}
+	}
+    G_free(inrast_albedo);
+    G_free(inrast_tempk);
+    G_free(inrast_lat);
+    G_free(inrast_doy);
+    G_close_cell(infd_albedo);
+    G_close_cell(infd_tempk);
+    G_close_cell(infd_lat);
+    G_close_cell(infd_doy);
+    G_free(inrast_tsw);
+    G_close_cell(infd_tsw);
+    if (flag3->answer) {
+	G_free(inrast_tair);
+	G_close_cell(infd_tair);
+	G_free(inrast_e0);
+	G_close_cell(infd_e0);
+    }
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    if (result2) {
+	G_free(outrast2);
+	G_close_cell(outfd2);
+	
+	    /* Color rule */ 
+	    G_init_colors(&colors);
+	G_add_color_rule(0, 0, 0, 0, 400, 255, 255, 255, &colors);
+	
+	    /* Metadata */ 
+	    G_short_history(result2, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result2, &history);
+    }
+    
+	/* Color rule */ 
+	G_init_colors(&colors);
+    G_add_color_rule(0, 0, 0, 0, 10, 255, 255, 255, &colors);
+    
+	/* Metadata */ 
+	G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/r_net_day.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/r_net_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/r_net_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,14 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Net Radiation after Bastiaanssen (1995) */
-
-double r_net_day( double bbalb, double solar, double tsw )
-{
-	double result;
-
-	result = ((1.0 - bbalb)*solar)-(110.0*tsw);
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Net Radiation after Bastiaanssen (1995) */ 
+double r_net_day(double bbalb, double solar, double tsw) 
+{
+    double result;
+
+    result = ((1.0 - bbalb) * solar) - (110.0 * tsw);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/r_net_day_bandara98.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/r_net_day_bandara98.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/r_net_day_bandara98.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,18 +1,22 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Diurnal Net Radiation after Bandara (1998)*/
-
-double r_net_day_bandara98( double surface_albedo, double solar_day, double apparent_atm_emissivity, double surface_emissivity, double air_temperature )
-{
-	double longwave_balance, result;
-	double sigma=5.67*pow(10,-8);/*Stefann-Boltzmann Constant*/
-	
-	longwave_balance = (apparent_atm_emissivity-surface_emissivity) * sigma * pow(air_temperature,4) ;
-
-	result = ((1.0 - surface_albedo)*solar_day) - longwave_balance ;
-
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Diurnal Net Radiation after Bandara (1998) */ 
+double r_net_day_bandara98(double surface_albedo, double solar_day,
+			    double apparent_atm_emissivity,
+			    double surface_emissivity,
+			    double air_temperature) 
+{
+    double longwave_balance, result;
+
+    double sigma = 5.67 * pow(10, -8);	/*Stefann-Boltzmann Constant */
+
+    longwave_balance =
+	(apparent_atm_emissivity -
+	 surface_emissivity) * sigma * pow(air_temperature, 4);
+    result = ((1.0 - surface_albedo) * solar_day) - longwave_balance;
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/solar_day.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/solar_day.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/solar_day.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,29 +1,31 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Solar Diurnal Radiation after Bastiaanssen (1995) */
-
-#define PI 3.1415927
-
-double solar_day(double lat, double doy, double tsw )
-{
-	double ws, cosun, latrad, delta, deltarad, ds, result;
-
-	/*Sun-Earth Distance (ds; A.U.)*/
-	ds = 1.0 + 0.01672 * sin(2*PI*(doy-93.5)/365.0);
-	/*Solar declination (delta; radians)*/
-	deltarad = 0.4093*sin((2*PI*doy/365)-1.39);
-	/*Convert latitude in radians*/
-	latrad =  lat * PI / 180.0;
-	
-	/*Convert latitude in radians*/
-	ws = acos(-tan(latrad)*tan(deltarad));
-	cosun = ws*sin(deltarad)*sin(latrad)+cos(deltarad)*cos(latrad)*sin(ws);
-	
-	result = ( cosun * 1367 * tsw ) / ( PI * ds * ds );
-
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Solar Diurnal Radiation after Bastiaanssen (1995) */ 
+    
+#define PI 3.1415927
+double solar_day(double lat, double doy, double tsw) 
+{
+    double ws, cosun, latrad, delta, deltarad, ds, result;
+
+    
+	/*Sun-Earth Distance (ds; A.U.) */ 
+	ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
+    
+	/*Solar declination (delta; radians) */ 
+	deltarad = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    
+	/*Convert latitude in radians */ 
+	latrad = lat * PI / 180.0;
+    
+	/*Convert latitude in radians */ 
+	ws = acos(-tan(latrad) * tan(deltarad));
+    cosun =
+	ws * sin(deltarad) * sin(latrad) +
+	cos(deltarad) * cos(latrad) * sin(ws);
+    result = (cosun * 1367 * tsw) / (PI * ds * ds);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.potrad/solar_day_3d.c
===================================================================
--- grass-addons/gipe/i.evapo.potrad/solar_day_3d.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.potrad/solar_day_3d.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,39 +1,36 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Average Solar Diurnal Radiation after Bastiaanssen (1995)
- * Includes Slope and aspect correction
- */
-
-#define PI 3.1415927
-
-double solar_day_3d(double lat, double doy, double tsw, double slope, double aspect )
-{
-	double ws, costheta, latrad, delta, deltarad, ds, result;
-	double temp1, temp2, temp3, temp4, temp5;
-	double slrad, asprad; /*slope and aspect in radians*/
-
-	ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
-	delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
-	deltarad  = delta * PI / 180.0;
-	latrad 	  = lat * PI / 180.0;
-	slrad  = slope * PI / 180.0;
-	asprad = aspect * PI / 180.0;
-
-	ws = acos(-tan(latrad)*tan(deltarad));
-
-	temp1 = sin(deltarad) * sin(latrad) * cos(slrad);
-	temp2 = sin(deltarad) * cos(latrad) * sin(slrad) * cos(asprad);
-	temp3 = cos(deltarad) * cos(latrad) * cos(slrad) * cos(ws*PI/180.0);
-	temp4 = cos(deltarad) * sin(slrad) * cos(asprad) * cos(ws*PI/180.0);
-	temp5 = cos(deltarad) * sin(slrad) * sin(asprad) * sin(ws*PI/180.0);
-
-	costheta = (temp1 - temp2 + temp3 + temp4 + temp5) / cos(slrad);
-	
-	result = ( costheta * 1367 * tsw ) / ( PI * ds * ds );
-
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Average Solar Diurnal Radiation after Bastiaanssen (1995)
+     * Includes Slope and aspect correction
+     */ 
+    
+#define PI 3.1415927
+double solar_day_3d(double lat, double doy, double tsw, double slope,
+		     double aspect) 
+{
+    double ws, costheta, latrad, delta, deltarad, ds, result;
+
+    double temp1, temp2, temp3, temp4, temp5;
+
+    double slrad, asprad;	/*slope and aspect in radians */
+
+    ds = 1.0 + 0.01672 * sin(2 * PI * (doy - 93.5) / 365.0);
+    delta = 0.4093 * sin((2 * PI * doy / 365) - 1.39);
+    deltarad = delta * PI / 180.0;
+    latrad = lat * PI / 180.0;
+    slrad = slope * PI / 180.0;
+    asprad = aspect * PI / 180.0;
+    ws = acos(-tan(latrad) * tan(deltarad));
+    temp1 = sin(deltarad) * sin(latrad) * cos(slrad);
+    temp2 = sin(deltarad) * cos(latrad) * sin(slrad) * cos(asprad);
+    temp3 = cos(deltarad) * cos(latrad) * cos(slrad) * cos(ws * PI / 180.0);
+    temp4 = cos(deltarad) * sin(slrad) * cos(asprad) * cos(ws * PI / 180.0);
+    temp5 = cos(deltarad) * sin(slrad) * sin(asprad) * sin(ws * PI / 180.0);
+    costheta = (temp1 - temp2 + temp3 + temp4 + temp5) / cos(slrad);
+    result = (costheta * 1367 * tsw) / (PI * ds * ds);
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.evapo.time_integration/main.c
===================================================================
--- grass-addons/gipe/i.evapo.time_integration/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.evapo.time_integration/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.evapo.time_integration
@@ -21,375 +22,402 @@
 
 #define MAXFILES 400
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-	struct Cell_head cellhd;/*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
+    struct Cell_head cellhd;	/*region+header info */
 
-	struct GModule *module;
-	struct Option *input, *input1, *input2, *input3, *output;
-	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input, *input1, *input2, *input3, *output;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	char *name,*name1,*name2; /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int nfiles, nfiles1, nfiles2;
-	int infd[MAXFILES], infd1[MAXFILES], infd2[MAXFILES];
-	int outfd;
+    /* FMEO Declarations**************** */
+    char *name, *name1, *name2;	/*input raster name */
+
+    char *result;		/*output raster name */
+
+    /*File Descriptors */
+    int nfiles, nfiles1, nfiles2;
+
+    int infd[MAXFILES], infd1[MAXFILES], infd2[MAXFILES];
+
+    int outfd;
+
 	/****************************************/
-	/* Pointers for file names 		*/
-	char **names;
-	char **ptr;
-	char **names1;
-	char **ptr1;
-	char **names2;
-	char **ptr2;
+    /* Pointers for file names              */
+    char **names;
+
+    char **ptr;
+
+    char **names1;
+
+    char **ptr1;
+
+    char **names2;
+
+    char **ptr2;
+
 	/****************************************/
-	double DOYbeforeETa[MAXFILES],DOYafterETa[MAXFILES];
-	int bfr,aft;
+    double DOYbeforeETa[MAXFILES], DOYafterETa[MAXFILES];
+
+    int bfr, aft;
+
 	/****************************************/
-	
-	int ok;
-	int i=0,j=0;
-	double etodoy; /*minimum ETo DOY*/
-	
-	void *inrast[MAXFILES], *inrast1[MAXFILES], *inrast2[MAXFILES];
-	DCELL *outrast;
-	int data_format; /* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
-	RASTER_MAP_TYPE in_data_type[MAXFILES]; /* ETa */
-	RASTER_MAP_TYPE in_data_type1[MAXFILES]; /* DOY of ETa */
-	RASTER_MAP_TYPE in_data_type2[MAXFILES]; /* ETo */
-	RASTER_MAP_TYPE out_data_type = DCELL_TYPE;
 
+    int ok;
+
+    int i = 0, j = 0;
+
+    double etodoy;		/*minimum ETo DOY */
+
+    void *inrast[MAXFILES], *inrast1[MAXFILES], *inrast2[MAXFILES];
+
+    DCELL *outrast;
+
+    int data_format;		/* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
+
+    RASTER_MAP_TYPE in_data_type[MAXFILES];	/* ETa */
+
+    RASTER_MAP_TYPE in_data_type1[MAXFILES];	/* DOY of ETa */
+
+    RASTER_MAP_TYPE in_data_type2[MAXFILES];	/* ETo */
+
+    RASTER_MAP_TYPE out_data_type = DCELL_TYPE;
+
 	/************************************/
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->keywords = _("evapotranspiration,temporal,integration");
-	module->description =
-		_("Temporal integration of satellite ET actual (ETa) following the daily ET reference (ETo) from meteorological station(s)\n");
+    module = G_define_module();
+    module->keywords = _("evapotranspiration,temporal,integration");
+    module->description =
+	_("Temporal integration of satellite ET actual (ETa) following the daily ET reference (ETo) from meteorological station(s)\n");
 
-	/* Define the different options */
+    /* Define the different options */
 
-	input = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input->key        =_("eta");
-	input->description= _("Names of satellite ETa layers [mm/d or cm/d]");
+    input = G_define_standard_option(G_OPT_R_INPUTS);
+    input->key = _("eta");
+    input->description = _("Names of satellite ETa layers [mm/d or cm/d]");
 
-	input1 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input1->key        =_("eta_doy");
-	input1->description= _("Names of satellite ETa Day of Year (DOY) layers [0-400] [-]");
+    input1 = G_define_standard_option(G_OPT_R_INPUTS);
+    input1->key = _("eta_doy");
+    input1->description =
+	_("Names of satellite ETa Day of Year (DOY) layers [0-400] [-]");
 
-	input2 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input2->key        =_("eto");
-	input2->description= _("Names of meteorological station ETo layers [0-400] [mm/d or cm/d]");
+    input2 = G_define_standard_option(G_OPT_R_INPUTS);
+    input2->key = _("eto");
+    input2->description =
+	_("Names of meteorological station ETo layers [0-400] [mm/d or cm/d]");
 
-	input3 = G_define_option() ;
-	input3->key        =_("eto_doy_min");
-	input3->type       = TYPE_DOUBLE;
-	input3->required   = YES;
-	input3->description =_("Value of DOY for ETo first day");
+    input3 = G_define_option();
+    input3->key = _("eto_doy_min");
+    input3->type = TYPE_DOUBLE;
+    input3->required = YES;
+    input3->description = _("Value of DOY for ETo first day");
 
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Name of the output temporally integrated ETa layer");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description =
+	_("Name of the output temporally integrated ETa layer");
 
-	/* FMEO init nfiles */
-	nfiles = 1;
-	nfiles1 = 1;
-	nfiles2 = 1;
+    /* FMEO init nfiles */
+    nfiles = 1;
+    nfiles1 = 1;
+    nfiles2 = 1;
+
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
-	
-	ok = 1;
-	names	= input->answers;
-	ptr	= input->answers;
-	names1	= input1->answers;
-	ptr1	= input1->answers;
-	names2	= input2->answers;
-	ptr2	= input2->answers;
-	etodoy	= atof(input3->answer);
+    if (G_parser(argc, argv))
+	exit(-1);
 
-	result	= output->answer;
+    ok = 1;
+    names = input->answers;
+    ptr = input->answers;
+    names1 = input1->answers;
+    ptr1 = input1->answers;
+    names2 = input2->answers;
+    ptr2 = input2->answers;
+    etodoy = atof(input3->answer);
+
+    result = output->answer;
+
 	/****************************************/
-	if (G_legal_filename (result) < 0)
-	{
-		G_fatal_error (_("[%s] is an illegal name"), result);
-		ok = 0;
-	}
+    if (G_legal_filename(result) < 0) {
+	G_fatal_error(_("[%s] is an illegal name"), result);
+	ok = 0;
+    }
+
 	/****************************************/
-	for (; *ptr != NULL; ptr++)
-	{
-		if (nfiles > MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name = *ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd[nfiles] = G_open_cell_old (name, mapset);
-		if (infd[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type[nfiles] = G_raster_map_type(name, mapset);
-		if( (infd[nfiles] = G_open_cell_old(name,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name);
-		}
-		if( (G_get_cellhd(name,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name);
-		}
-		inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
-		nfiles++;
+    for (; *ptr != NULL; ptr++) {
+	if (nfiles > MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name = *ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name);
+	    ok = 0;
 	}
-	nfiles--;
-	if (nfiles <= 1){
-		G_fatal_error(_("The min specified input map is two"));
+	if (!ok) {
+	    continue;
 	}
+	infd[nfiles] = G_open_cell_old(name, mapset);
+	if (infd[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd[nfiles] = G_open_cell_old(name, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name);
+	}
+	if ((G_get_cellhd(name, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name);
+	}
+	inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 1) {
+	G_fatal_error(_("The min specified input map is two"));
+    }
+
 	/****************************************/
+
 	/****************************************/
+
 	/****************************************/
-	ok=1;
-	for (; *ptr1 != NULL; ptr1++)
-	{
-		if (nfiles1 > MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name1 = *ptr1;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name1, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name1);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd1[nfiles] = G_open_cell_old (name1, mapset);
-		if (infd1[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type1[nfiles] = G_raster_map_type(name1, mapset);
-		if( (infd1[nfiles] = G_open_cell_old(name1,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name1);
-		}
-		if( (G_get_cellhd(name1,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name1);
-		}
-		inrast1[nfiles] = G_allocate_raster_buf(in_data_type[nfiles1]);
-		nfiles1++;
+    ok = 1;
+    for (; *ptr1 != NULL; ptr1++) {
+	if (nfiles1 > MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name1 = *ptr1;
+	/* find map in mapset */
+	mapset = G_find_cell2(name1, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name1);
+	    ok = 0;
 	}
-	nfiles1--;
-	if (nfiles1 <= 1){
-		G_fatal_error(_("The min specified input map is two"));
+	if (!ok) {
+	    continue;
 	}
+	infd1[nfiles] = G_open_cell_old(name1, mapset);
+	if (infd1[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type1[nfiles] = G_raster_map_type(name1, mapset);
+	if ((infd1[nfiles] = G_open_cell_old(name1, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name1);
+	}
+	if ((G_get_cellhd(name1, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name1);
+	}
+	inrast1[nfiles] = G_allocate_raster_buf(in_data_type[nfiles1]);
+	nfiles1++;
+    }
+    nfiles1--;
+    if (nfiles1 <= 1) {
+	G_fatal_error(_("The min specified input map is two"));
+    }
+
 	/****************************************/
-	if(nfiles!=nfiles1)	
-		G_fatal_error (_("ETa and ETa_DOY file numbers are not equal!"));
+    if (nfiles != nfiles1)
+	G_fatal_error(_("ETa and ETa_DOY file numbers are not equal!"));
+
 	/****************************************/
+
 	/****************************************/
-	ok=1;
-	for (; *ptr2 != NULL; ptr2++)
-	{
-		if (nfiles > MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name2 = *ptr2;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name2, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name2);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd2[nfiles2] = G_open_cell_old (name2, mapset);
-		if (infd2[nfiles2] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type2[nfiles2] = G_raster_map_type(name2, mapset);
-		if( (infd2[nfiles2] = G_open_cell_old(name2,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name2);
-		}
-		if( (G_get_cellhd(name2,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name2);
-		}
-		inrast2[nfiles2] = G_allocate_raster_buf(in_data_type2[nfiles2]);
-		nfiles2++;
+    ok = 1;
+    for (; *ptr2 != NULL; ptr2++) {
+	if (nfiles > MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name2 = *ptr2;
+	/* find map in mapset */
+	mapset = G_find_cell2(name2, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name2);
+	    ok = 0;
 	}
-	nfiles2--;
-	if (nfiles2 <= 1){
-		G_fatal_error(_("The min specified input map is two"));
+	if (!ok) {
+	    continue;
 	}
+	infd2[nfiles2] = G_open_cell_old(name2, mapset);
+	if (infd2[nfiles2] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type2[nfiles2] = G_raster_map_type(name2, mapset);
+	if ((infd2[nfiles2] = G_open_cell_old(name2, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name2);
+	}
+	if ((G_get_cellhd(name2, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name2);
+	}
+	inrast2[nfiles2] = G_allocate_raster_buf(in_data_type2[nfiles2]);
+	nfiles2++;
+    }
+    nfiles2--;
+    if (nfiles2 <= 1) {
+	G_fatal_error(_("The min specified input map is two"));
+    }
+
 	/****************************************/
+
 	/****************************************/
-	
+
 	/***************************************************/
-	/* Allocate output buffer, use input map data_type */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(out_data_type);
-	
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,1)) < 0){
-		G_fatal_error (_("Could not open <%s>"),result);
+    /* Allocate output buffer, use input map data_type */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(out_data_type);
+
+    /* Create New raster files */
+    if ((outfd = G_open_raster_new(result, 1)) < 0) {
+	G_fatal_error(_("Could not open <%s>"), result);
+    }
+
+	/*******************/
+    /* Process pixels */
+    double doy[MAXFILES];
+
+    double sum[MAXFILES];
+
+    for (row = 0; row < nrows; row++) {
+	DCELL d_out;
+
+	DCELL d_ETrF[MAXFILES];
+
+	DCELL d[MAXFILES];
+
+	DCELL d1[MAXFILES];
+
+	DCELL d2[MAXFILES];
+
+	G_percent(row, nrows, 2);
+	/* read input map */
+	for (i = 1; i <= nfiles; i++) {
+	    if ((G_get_raster_row(infd[i], inrast[i], row, in_data_type[i])) <
+		0) {
+		G_fatal_error(_("Could not read from <%s>"), name);
+	    }
 	}
-	/*******************/
-	/* Process pixels */
-	double doy[MAXFILES];
-	double sum[MAXFILES];
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d_out;
-		DCELL d_ETrF[MAXFILES];
-		DCELL d[MAXFILES];
-		DCELL d1[MAXFILES];
-		DCELL d2[MAXFILES];
-		G_percent (row, nrows, 2);
-		/* read input map */
-		for (i=1;i<=nfiles;i++)
-		{
-			if( (G_get_raster_row(infd[i],inrast[i],row,in_data_type[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name);
-			}
+	for (i = 1; i <= nfiles1; i++) {
+	    if ((G_get_raster_row
+		 (infd1[i], inrast1[i], row, in_data_type1[i])) < 0) {
+		G_fatal_error(_("Could not read from <%s>"), name1);
+	    }
+	}
+	for (i = 1; i <= nfiles2; i++) {
+	    if ((G_get_raster_row
+		 (infd2[i], inrast2[i], row, in_data_type2[i])) < 0) {
+		G_fatal_error(_("Could not read from <%s>"), name2);
+	    }
+	}
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    for (i = 1; i <= nfiles; i++) {
+		switch (in_data_type[i]) {
+		case CELL_TYPE:
+		    d[i] = (double)((CELL *) inrast[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d[i] = (double)((FCELL *) inrast[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d[i] = (double)((DCELL *) inrast[i])[col];
+		    break;
 		}
-		for (i=1;i<=nfiles1;i++)
-		{
-			if( (G_get_raster_row(infd1[i],inrast1[i],row,in_data_type1[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name1);
-			}
+	    }
+	    for (i = 1; i <= nfiles1; i++) {
+		switch (in_data_type1[i]) {
+		case CELL_TYPE:
+		    d1[i] = (double)((CELL *) inrast1[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d1[i] = (double)((FCELL *) inrast1[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d1[i] = (double)((DCELL *) inrast1[i])[col];
+		    break;
 		}
-		for (i=1;i<=nfiles2;i++)
-		{
-			if( (G_get_raster_row(infd2[i],inrast2[i],row,in_data_type2[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name2);
-			}
+	    }
+	    for (i = 1; i <= nfiles2; i++) {
+		switch (in_data_type2[i]) {
+		case CELL_TYPE:
+		    d2[i] = (double)((CELL *) inrast2[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d2[i] = (double)((FCELL *) inrast2[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d2[i] = (double)((DCELL *) inrast2[i])[col];
+		    break;
 		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			for(i=1;i<=nfiles;i++)
-			{
-				switch(in_data_type[i])
-				{
-					case CELL_TYPE:
-						d[i] = (double) ((CELL *) inrast[i])[col];
-						break;
-					case FCELL_TYPE:
-						d[i] = (double) ((FCELL *) inrast[i])[col];
-						break;
-					case DCELL_TYPE:
-						d[i] = (double) ((DCELL *) inrast[i])[col];
-						break;
-				}
-			}
-			for(i=1;i<=nfiles1;i++)
-			{
-				switch(in_data_type1[i])
-				{
-					case CELL_TYPE:
-						d1[i] = (double) ((CELL *) inrast1[i])[col];
-						break;
-					case FCELL_TYPE:
-						d1[i] = (double) ((FCELL *) inrast1[i])[col];
-						break;
-					case DCELL_TYPE:
-						d1[i] = (double) ((DCELL *) inrast1[i])[col];
-						break;
-				}
-			}
-			for(i=1;i<=nfiles2;i++)
-			{
-				switch(in_data_type2[i])
-				{
-					case CELL_TYPE:
-						d2[i] = (double) ((CELL *) inrast2[i])[col];
-						break;
-					case FCELL_TYPE:
-						d2[i] = (double) ((FCELL *) inrast2[i])[col];
-						break;
-					case DCELL_TYPE:
-						d2[i] = (double) ((DCELL *) inrast2[i])[col];
-						break;
-				}
-			}
-			/* Find out the DOY of the eto image	*/
-			/* DOY_eto_index = ModisDOY - etodoymin	*/
-			for(i=1;i<=nfiles1;i++)
-			{
-				doy[i] = d1[i] - etodoy;
-				d_ETrF[i]=d[i]/d2[(int)doy[i]];
-			}
-			for(i=1;i<=nfiles1;i++)
-			{
-				if(i==1)
-					DOYbeforeETa[i]=etodoy;
-				else
-					DOYbeforeETa[i]=(d[i]-d[i-1])/2.0;
-				if(i==nfiles1)
-					DOYafterETa[i]=etodoy+nfiles2;
-				else
-					DOYafterETa[i]=(d[i+1]-d[i])/2.0;
-			}	
-			sum[MAXFILES]=0.0;
-			for(i=1;i<=nfiles1;i++)
-			{
-				bfr = (int) DOYbeforeETa[i];
-				aft = (int) DOYafterETa[i];
-				for(j=bfr;j<aft;j++)
-				{
-					sum[i]+=d2[j];	
-				}
-			}
-			d_out = 0.0;
-			for(i=1;i<=nfiles1;i++)
-			{
-				d_out += d_ETrF[i]*sum[i];
-			}	
-			outrast[col] = d_out;
+	    }
+	    /* Find out the DOY of the eto image    */
+	    /* DOY_eto_index = ModisDOY - etodoymin */
+	    for (i = 1; i <= nfiles1; i++) {
+		doy[i] = d1[i] - etodoy;
+		d_ETrF[i] = d[i] / d2[(int)doy[i]];
+	    }
+	    for (i = 1; i <= nfiles1; i++) {
+		if (i == 1)
+		    DOYbeforeETa[i] = etodoy;
+		else
+		    DOYbeforeETa[i] = (d[i] - d[i - 1]) / 2.0;
+		if (i == nfiles1)
+		    DOYafterETa[i] = etodoy + nfiles2;
+		else
+		    DOYafterETa[i] = (d[i + 1] - d[i]) / 2.0;
+	    }
+	    sum[MAXFILES] = 0.0;
+	    for (i = 1; i <= nfiles1; i++) {
+		bfr = (int)DOYbeforeETa[i];
+		aft = (int)DOYafterETa[i];
+		for (j = bfr; j < aft; j++) {
+		    sum[i] += d2[j];
 		}
-		if (G_put_raster_row (outfd, outrast, out_data_type) < 0)
-			G_fatal_error (_("Cannot write to <%s>"),result);
+	    }
+	    d_out = 0.0;
+	    for (i = 1; i <= nfiles1; i++) {
+		d_out += d_ETrF[i] * sum[i];
+	    }
+	    outrast[col] = d_out;
 	}
-	for (i=1;i<=nfiles;i++)
-	{
-		G_free (inrast[i]);
-		G_close_cell (infd[i]);
-		G_free (inrast1[i]);
-		G_close_cell (infd1[i]);
-		G_free (inrast2[i]);
-		G_close_cell (infd2[i]);
-	}
-	G_free (outrast);
-	G_close_cell (outfd);
-	
-	/* Color table from 0.0 to 10.0 */
-	G_init_colors(&colors);
-	G_add_color_rule(0.0,0,0,0,10.0,255,255,255,&colors);
-	/* Metadata */
-	G_short_history(result,"raster",&history);
-	G_command_history(&history);
-	G_write_history(result,&history);
+	if (G_put_raster_row(outfd, outrast, out_data_type) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), result);
+    }
+    for (i = 1; i <= nfiles; i++) {
+	G_free(inrast[i]);
+	G_close_cell(infd[i]);
+	G_free(inrast1[i]);
+	G_close_cell(infd1[i]);
+	G_free(inrast2[i]);
+	G_close_cell(infd2[i]);
+    }
+    G_free(outrast);
+    G_close_cell(outfd);
 
-	exit(EXIT_SUCCESS);
+    /* Color table from 0.0 to 10.0 */
+    G_init_colors(&colors);
+    G_add_color_rule(0.0, 0, 0, 0, 10.0, 255, 255, 255, &colors);
+    /* Metadata */
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+
+    exit(EXIT_SUCCESS);
 }

Modified: grass-addons/gipe/i.latitude/main.c
===================================================================
--- grass-addons/gipe/i.latitude/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.latitude/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,163 +1,187 @@
-/****************************************************************************
- *
- * MODULE:       i.latitude
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the longitude of the pixels in the map. 
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/gprojects.h>
-#include <grass/glocale.h>
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int not_ll=0;/*if proj is not lat/long, it will be 1.*/
-	struct GModule *module;
-	struct Option *input1, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	struct pj_info iproj;
-	struct pj_info oproj;
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd;
-	int outfd1;
-	
-	char *in;
-	int i=0,j=0;
-	double xp, yp;
-	double xmin, ymin;
-	double xmax, ymax;
-	double stepx,stepy;
-	double latitude, longitude;
-
-	void *inrast;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_inrast;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("latitude, projection");
-	module->description = _("creates a latitude map");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->description=_("Name of the input map");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output latitude layer");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	in	 	= input1->answer;
-	result1  	= output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(in, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), in);
-	}
-	data_type_inrast = G_raster_map_type(in,mapset);
-	if ( (infd = G_open_cell_old (in,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), in);
-	if (G_get_cellhd (in, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), in);
-	inrast = G_allocate_raster_buf(data_type_inrast);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
-
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
-
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	
-	/* Shamelessly stolen from r.sun !!!!	*/
-	/* Set up parameters for projection to lat/long if necessary */
-	if ((G_projection() != PROJECTION_LL)) {
-		not_ll=1;
-		struct Key_Value *in_proj_info, *in_unit_info;
-		if ((in_proj_info = G_get_projinfo()) == NULL)
-			G_fatal_error(_("Can't get projection info of current location"));
-		if ((in_unit_info = G_get_projunits()) == NULL)
-			G_fatal_error(_("Can't get projection units of current location"));
-		if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
-			G_fatal_error(_("Can't get projection key values of current location"));
-		G_free_key_value(in_proj_info);
-		G_free_key_value(in_unit_info);
-		/* Set output projection to latlong w/ same ellipsoid */
-		oproj.zone = 0;
-		oproj.meters = 1.;
-		sprintf(oproj.proj, "ll");
-		if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
-			G_fatal_error(_("Unable to set up lat/long projection parameters"));
-	}/* End of stolen from r.sun :P*/
-	
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_lat;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd,inrast,row,data_type_inrast)<0)
-			G_fatal_error(_("Could not read from <%s>"),in);
-		for (col=0; col < ncols; col++)
-		{
-			latitude = ymax - ( row * stepy );
-			longitude = xmin + ( col * stepx );
-			if(not_ll){
-				if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
-				    G_fatal_error(_("Error in pj_do_proj"));
-				}
-			}else{
-				/*Do nothing*/
-			}	
-			d_lat = latitude;
-			outrast1[col] = d_lat;
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast);
-	G_close_cell (infd);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.latitude
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the longitude of the pixels in the map. 
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/gprojects.h>
+#include <grass/glocale.h>
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int not_ll = 0;		/*if proj is not lat/long, it will be 1. */
+
+    struct GModule *module;
+
+    struct Option *input1, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    struct pj_info iproj;
+
+    struct pj_info oproj;
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd;
+
+    int outfd1;
+
+    char *in;
+
+    int i = 0, j = 0;
+
+    double xp, yp;
+
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
+    void *inrast;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_inrast;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("latitude, projection");
+    module->description = _("creates a latitude map");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->description = _("Name of the input map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output latitude layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    in = input1->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(in, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), in);
+    }
+    data_type_inrast = G_raster_map_type(in, mapset);
+    if ((infd = G_open_cell_old(in, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), in);
+    if (G_get_cellhd(in, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), in);
+    inrast = G_allocate_raster_buf(data_type_inrast);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    
+	/* Shamelessly stolen from r.sun !!!!   */ 
+	/* Set up parameters for projection to lat/long if necessary */ 
+	if ((G_projection() != PROJECTION_LL)) {
+	not_ll = 1;
+	struct Key_Value *in_proj_info, *in_unit_info;
+
+	if ((in_proj_info = G_get_projinfo()) == NULL)
+	    G_fatal_error(_("Can't get projection info of current location"));
+	if ((in_unit_info = G_get_projunits()) == NULL)
+	    G_fatal_error(_("Can't get projection units of current location"));
+	if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
+	    G_fatal_error(_("Can't get projection key values of current location"));
+	G_free_key_value(in_proj_info);
+	G_free_key_value(in_unit_info);
+	
+	    /* Set output projection to latlong w/ same ellipsoid */ 
+	    oproj.zone = 0;
+	oproj.meters = 1.;
+	sprintf(oproj.proj, "ll");
+	if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
+	    G_fatal_error(_("Unable to set up lat/long projection parameters"));
+    }				/* End of stolen from r.sun :P */
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_lat;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row(infd, inrast, row, data_type_inrast) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), in);
+	for (col = 0; col < ncols; col++)
+	     {
+	    latitude = ymax - (row * stepy);
+	    longitude = xmin + (col * stepx);
+	    if (not_ll) {
+		if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
+		    G_fatal_error(_("Error in pj_do_proj"));
+		}
+	    }
+	    else {
+		
+		    /*Do nothing */ 
+	    }
+	    d_lat = latitude;
+	    outrast1[col] = d_lat;
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast);
+    G_close_cell(infd);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.lmf/fitting.c
===================================================================
--- grass-addons/gipe/i.lmf/fitting.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/fitting.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,73 +1,88 @@
-/*************************************************************
-******Triangular Function Fitting********************
-*************************************************************/
-#include<stdio.h>
-#include<stdlib.h>
-
-#define NMAX 200
-#define MAXF 50
-int invert_matrix(double a[][MAXF], int order);
-
-int fitting(int npoint, int nfunc, double *dat, int *idx1, double f[][MAXF], double *c, double *vfit){
-	int	i, j, k, k1, k2, nn;
-        double 	tf[MAXF][MAXF];
-        double 	tinv[MAXF][MAXF];
-        double 	vec[MAXF];
-        double 	tfij;
-	double 	sum;
-
-        nn=nfunc*2+2;
-
-        /*Value Initialization*/
-        for(i=0;i<nn;i++){
-                c[i]=0.0;
-        }
-        for(i=0;i<npoint;i++){
-		vfit[i]=0.0;
-        }
-        /*Matrix Initialization*/
-        for(i=0;i<nn;i++){
-                for(j=0;j<nn;j++){
-                        tf[i][j]=0.0;
-                        tf[j][i]=0.0;
-                }
-                vec[i]=0.0;
-        }
-        /*Making Matrix*/
-        for(i=0;i<npoint;i++){
-                if(idx1[i]==1){
-                        for(k1=0;k1<nn;k1++){
-                                for(k2=0;k2<nn;k2++){
-                                        tf[k1][k2]=tf[k1][k2]+f[i][k2]*f[i][k1];
-                                }
-                                vec[k1]=vec[k1]+dat[i]*f[i][k1];
-                        }
-                }
-        }
-        /*Matrix Copy and Inversion*/
-        for(i=0;i<nn;i++){
-                for(j=i;j<nn;j++){
-                        tfij=tf[i][j];
-                        tinv[i][j]=tfij;
-                        tinv[j][i]=tfij;
-                }
-        }
-        invert_matrix(tinv,nn);
-        /*Calculation of Coefficients*/
-        for(i=0;i<nn;i++){
-                sum=0.0;
-                for(j=0;j<nn;j++){
-                        sum=sum+tinv[i][j]*vec[j];
-                }
-                c[i]=sum;
-        }
-        /*Calculating theoretical value*/
-        for(i=0;i<npoint;i++){
-                sum=0.0;
-                for(k=0;k<nn;k++){
-                        sum=sum+c[k]*f[i][k];
-                }
-                vfit[i]=sum;
-        }
-        return;
-}
+
+/*************************************************************
+******Triangular Function Fitting********************
+*************************************************************/  
+#include<stdio.h>
+#include<stdlib.h>
+    
+#define NMAX 200
+#define MAXF 50
+int invert_matrix(double a[][MAXF], int order);
+
+int fitting(int npoint, int nfunc, double *dat, int *idx1, double f[][MAXF],
+	      double *c, double *vfit)
+{
+    int i, j, k, k1, k2, nn;
+
+    double tf[MAXF][MAXF];
+
+    double tinv[MAXF][MAXF];
+
+    double vec[MAXF];
+
+    double tfij;
+
+    double sum;
+
+    nn = nfunc * 2 + 2;
+    
+	/*Value Initialization */ 
+	for (i = 0; i < nn; i++) {
+	c[i] = 0.0;
+    }
+    for (i = 0; i < npoint; i++) {
+	vfit[i] = 0.0;
+    }
+    
+	/*Matrix Initialization */ 
+	for (i = 0; i < nn; i++) {
+	for (j = 0; j < nn; j++) {
+	    tf[i][j] = 0.0;
+	    tf[j][i] = 0.0;
+	}
+	vec[i] = 0.0;
+    }
+    
+	/*Making Matrix */ 
+	for (i = 0; i < npoint; i++) {
+	if (idx1[i] == 1) {
+	    for (k1 = 0; k1 < nn; k1++) {
+		for (k2 = 0; k2 < nn; k2++) {
+		    tf[k1][k2] = tf[k1][k2] + f[i][k2] * f[i][k1];
+		}
+		vec[k1] = vec[k1] + dat[i] * f[i][k1];
+	    }
+	}
+    }
+    
+	/*Matrix Copy and Inversion */ 
+	for (i = 0; i < nn; i++) {
+	for (j = i; j < nn; j++) {
+	    tfij = tf[i][j];
+	    tinv[i][j] = tfij;
+	    tinv[j][i] = tfij;
+	}
+    }
+    invert_matrix(tinv, nn);
+    
+	/*Calculation of Coefficients */ 
+	for (i = 0; i < nn; i++) {
+	sum = 0.0;
+	for (j = 0; j < nn; j++) {
+	    sum = sum + tinv[i][j] * vec[j];
+	}
+	c[i] = sum;
+    }
+    
+	/*Calculating theoretical value */ 
+	for (i = 0; i < npoint; i++) {
+	sum = 0.0;
+	for (k = 0; k < nn; k++) {
+	    sum = sum + c[k] * f[i][k];
+	}
+	vfit[i] = sum;
+    }
+    return;
+}
+
+

Modified: grass-addons/gipe/i.lmf/invert_matrix.c
===================================================================
--- grass-addons/gipe/i.lmf/invert_matrix.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/invert_matrix.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,108 +1,126 @@
-/****************************************************************
- * Inverse and Determinant of A by the Gauss-Jordan Method.
- * m is the order of the square matrix, A.
- * A-Inverse replaces A.
- * Determinant of A is placed in DET.
- * Cooley and Lohnes (1971:63)
- ****************************************************************/
-#include<stdio.h>
-#include<stdlib.h>
-#include<math.h>
-
-#define IDIM 50
-#define MAXF 50
-
-int invert_matrix(double a[][MAXF], int order){
-	double 	ipvt[IDIM+1];
-	double 	pvt[IDIM+1];
-	double 	ind[IDIM+1][1];
-	int 	i,j,k,l,l1;
-	double 	amax,swap;
-	int 	irow,icol;
-
-	int	m=order;
-
-	for(j=0;j<m;j++){
-		ipvt[j]=0.0;
-	}
-
-	for(i=0;i<m;i++){
-		/*SEARCH FOR THE PIVOT ELEMENT*/
-		double temporary=0;
-		amax=0.0;
-		for(j=0;j<m;j++){
-			if(ipvt[j]!=1){
-				for(k=0;k<m;k++){
-					if(ipvt[k]==0&&abs(amax)<abs(a[j][k])){
-						irow=j;
-						icol=k;
-						amax=a[j][k];
-					} else if(ipvt[j]==1){
-						temporary=1;
-						break;
-					} else if(ipvt[j]>1||ipvt[j]<0){
-						temporary=5;
-						break;
-					}
-				}
-				if(temporary==5||temporary==1){
-					break;
-				}
-			} else {
-				break;
-			}
-			if(temporary==5){
-				break;
-			}
-		}
-		if(temporary!=5){
-			/*INTERCHANGE ROWS TO PUT PIVOT ELEMENT ON DIAGONAL*/
-			if(irow!=icol){
-				for(l=0;l<m;l++){
-					swap=a[irow][l];
-					a[irow][l]=a[icol][l];
-					a[icol][l]=swap;
-				}
-			}
-			ind[i][0]=irow;
-			ind[i][1]=icol;
-			pvt[i]=a[icol][icol];
-			/*DIVIDE THE PIVOT ROW BY THE PIVOT ELEMENT*/
-			a[icol][icol]=1.0;
-			for(l=0;l<m;l++){
-				a[icol][l]=a[icol][l]/pvt[i];
-			} 
-			/*REDUCE THE NON-PIVOT ROWS*/
-			for(l1=0;l1<m;l1++){
-				if(l1!=icol){
-					swap=a[l1][icol];
-					a[l1][icol]=0.0;
-					if(swap<pow(0.1,-30)&&swap>pow(-0.1,-30)){
-						swap=0.0;
-					}
-					for(l=0;l<m;l++){
-						a[l1][l]=a[l1][l]-a[icol][l]*swap;
-					}
-				}
-			}
-		} else {
-			break;
-		}
-	} 
-	/*INTERCHANGE THE COLUMNS*/
-	for(i=0;i<m;i++){
-		l=m-i-1;
-		if(ind[l][0]!=ind[l][1]){
-			irow=ind[l][0];
-			icol=ind[l][1];
-              		for(k=0;k<m;k++){
-				swap=a[k][irow];
-				a[k][irow]=a[k][icol];
-				a[k][icol]=swap;
-			}
-		}else if(ind[l][0]==ind[l][1]){
-			break;
-		}
-	}
-	return;
-}
+
+/****************************************************************
+ * Inverse and Determinant of A by the Gauss-Jordan Method.
+ * m is the order of the square matrix, A.
+ * A-Inverse replaces A.
+ * Determinant of A is placed in DET.
+ * Cooley and Lohnes (1971:63)
+ ****************************************************************/  
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+    
+#define IDIM 50
+#define MAXF 50
+int invert_matrix(double a[][MAXF], int order)
+{
+    double ipvt[IDIM + 1];
+
+    double pvt[IDIM + 1];
+
+    double ind[IDIM + 1][1];
+
+    int i, j, k, l, l1;
+
+    double amax, swap;
+
+    int irow, icol;
+
+    int m = order;
+
+    for (j = 0; j < m; j++) {
+	ipvt[j] = 0.0;
+    }
+    for (i = 0; i < m; i++) {
+	
+	    /*SEARCH FOR THE PIVOT ELEMENT */ 
+	double temporary = 0;
+
+	amax = 0.0;
+	for (j = 0; j < m; j++) {
+	    if (ipvt[j] != 1) {
+		for (k = 0; k < m; k++) {
+		    if (ipvt[k] == 0 && abs(amax) < abs(a[j][k])) {
+			irow = j;
+			icol = k;
+			amax = a[j][k];
+		    }
+		    else if (ipvt[j] == 1) {
+			temporary = 1;
+			break;
+		    }
+		    else if (ipvt[j] > 1 || ipvt[j] < 0) {
+			temporary = 5;
+			break;
+		    }
+		}
+		if (temporary == 5 || temporary == 1) {
+		    break;
+		}
+	    }
+	    else {
+		break;
+	    }
+	    if (temporary == 5) {
+		break;
+	    }
+	}
+	if (temporary != 5) {
+	    
+		/*INTERCHANGE ROWS TO PUT PIVOT ELEMENT ON DIAGONAL */ 
+		if (irow != icol) {
+		for (l = 0; l < m; l++) {
+		    swap = a[irow][l];
+		    a[irow][l] = a[icol][l];
+		    a[icol][l] = swap;
+		}
+	    }
+	    ind[i][0] = irow;
+	    ind[i][1] = icol;
+	    pvt[i] = a[icol][icol];
+	    
+		/*DIVIDE THE PIVOT ROW BY THE PIVOT ELEMENT */ 
+		a[icol][icol] = 1.0;
+	    for (l = 0; l < m; l++) {
+		a[icol][l] = a[icol][l] / pvt[i];
+	    }
+	    
+		/*REDUCE THE NON-PIVOT ROWS */ 
+		for (l1 = 0; l1 < m; l1++) {
+		if (l1 != icol) {
+		    swap = a[l1][icol];
+		    a[l1][icol] = 0.0;
+		    if (swap < pow(0.1, -30) && swap > pow(-0.1, -30)) {
+			swap = 0.0;
+		    }
+		    for (l = 0; l < m; l++) {
+			a[l1][l] = a[l1][l] - a[icol][l] * swap;
+		    }
+		}
+	    }
+	}
+	else {
+	    break;
+	}
+    }
+    
+	/*INTERCHANGE THE COLUMNS */ 
+	for (i = 0; i < m; i++) {
+	l = m - i - 1;
+	if (ind[l][0] != ind[l][1]) {
+	    irow = ind[l][0];
+	    icol = ind[l][1];
+	    for (k = 0; k < m; k++) {
+		swap = a[k][irow];
+		a[k][irow] = a[k][icol];
+		a[k][icol] = swap;
+	    }
+	}
+	else if (ind[l][0] == ind[l][1]) {
+	    break;
+	}
+    }
+    return;
+}
+
+

Modified: grass-addons/gipe/i.lmf/lmf.c
===================================================================
--- grass-addons/gipe/i.lmf/lmf.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/lmf.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,113 +1,148 @@
-/********************************************************************
-* LMF SUBROUTINE: Trying to process one pixel at a time         ****
-*                       in a single function                    ****
-********************************************************************
-********************************************************************
-* NBANDS = how many dates in this time-series                   ****
-* NPOINT = how many dates of this time-series cover one year    ****
-*               (NPOINT=46 for MODIS 8-days product)            ****
-********************************************************************/
-#include<stdio.h>
-#include<stdlib.h>
-
-#define MAXB 200
-/* NMAX=MAXB */
-#define NMAX 200
-#define MAXF 50
-
-#define TRUE 1
-#define FALSE 0
-
-#define PI 3.1415926535897932385
-int make_matrix(int n,int npoint,int nfunc,int *numk,double f[][MAXF]);
-int fitting(int npoint, int nfunc, double *dat, int *idx1, double f[][MAXF], double *c, double *vfit);
-int minmax(int n, int nwin, double *dat);
-int maxmin(int n, int nwin, double *dat);
-
-
-int lmf(int nbands, int npoint, double *inpix, double *outpix){
-        int     i,j,k,idk,norder,nfunc;
-	int	nwin,mmsw,nds,isum;
-        int     fitsw,fitmd;
-        double  tcld,thh,thl;
-	double	vaic,delta;
-        int     idx1[NMAX];
-        /*for fitting*/
-        double  dat[NMAX];
-        double  f[NMAX][MAXF],c[MAXF];
-        double  vfit[NMAX];
-        double  ipoint[MAXB];
-        /*for iteration*/
-        double  dat0[NMAX],c0[MAXF];
-        double  dat1[NMAX];
-        /*for Matrix Inversion*/
-        int     numk[MAXF]={ 1,2,3,4,6,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
-        /*Set Defaults*/
-        nfunc=6;
-        for(i=0;i<NMAX;i++){
-		for(j=0;j<MAXF;j++){
-			f[i][j]=0.0;
-		}
-	}
-        make_matrix(nbands,npoint,nfunc,numk,f);
-        nwin=3;
-        mmsw=1;
-        /*nds=npoint;*/
-        nds=2*nfunc+2;
-        /*LMF Process*/
-        /*Checking Pixel Value*/
-        fitsw=1;
-        /*fitting method*/
-        tcld=0.15;
-        fitmd=2;
-        /*FITMD0----*/
-        if(fitmd==0){
-                thh=tcld*2.0;
-                thl=-tcld;
-        }
-        /*FITMD1----*/
-        if(fitmd==1){
-               thh=tcld;
-               thl=-tcld*2.0;
-        }
-        /*FITMD2----*/
-        if(fitmd==2){
-                thh=tcld;
-                thl=-tcld;
-        }
-        /*initialize arrays*/
-        for(k=0;k<nbands;k++){
-                dat0[k]=0.0;
-                outpix[k]=0.0;
-                ipoint[k]=1;
-                idx1[k]=TRUE;
-        }
-        for(k=0;k<nds;k++){
-                c[k]=0.0;
-        }
-        for(k=0;k<nbands;k++){
-                if(inpix[k]>0.375){
-                        ipoint[k]=0;
-                }
-                if(k>=2){
-                        idk=(inpix[k]-inpix[k-1]);
-                        if(idk>0.125){
-                                ipoint[k]=0;
-                        }
-                }
-                dat0[k]=inpix[k];
-                dat[k]=dat0[k];
-         }
-         /*Minimax Filter*/
-         if(mmsw>=1&&fitmd==0){
-                minmax(nbands,nwin,dat);
-         }else if(mmsw>=1&&fitmd==1){
-                maxmin(nbands,nwin,dat);
-         }
-         /*Fitting*/
-         fitting(nbands,3,dat,idx1,f,c,vfit);
-         for(k=0;k<nbands;k++){
-                outpix[k]=vfit[k];
-         }
-         return;
-}
+
+/********************************************************************
+* LMF SUBROUTINE: Trying to process one pixel at a time         ****
+*                       in a single function                    ****
+********************************************************************
+********************************************************************
+* NBANDS = how many dates in this time-series                   ****
+* NPOINT = how many dates of this time-series cover one year    ****
+*               (NPOINT=46 for MODIS 8-days product)            ****
+********************************************************************/  
+#include<stdio.h>
+#include<stdlib.h>
+    
+#define MAXB 200
+    /* NMAX=MAXB */ 
+#define NMAX 200
+#define MAXF 50
+    
+#define TRUE 1
+#define FALSE 0
+    
+#define PI 3.1415926535897932385
+int make_matrix(int n, int npoint, int nfunc, int *numk, double f[][MAXF]);
+
+int fitting(int npoint, int nfunc, double *dat, int *idx1, double f[][MAXF],
+	     double *c, double *vfit);
+int minmax(int n, int nwin, double *dat);
+
+int maxmin(int n, int nwin, double *dat);
+
+int lmf(int nbands, int npoint, double *inpix, double *outpix)
+{
+    int i, j, k, idk, norder, nfunc;
+
+    int nwin, mmsw, nds, isum;
+
+    int fitsw, fitmd;
+
+    double tcld, thh, thl;
+
+    double vaic, delta;
+
+    int idx1[NMAX];
+
+    
+	/*for fitting */ 
+    double dat[NMAX];
+
+    double f[NMAX][MAXF], c[MAXF];
+
+    double vfit[NMAX];
+
+    double ipoint[MAXB];
+
+    
+	/*for iteration */ 
+    double dat0[NMAX], c0[MAXF];
+
+    double dat1[NMAX];
+
+    
+	/*for Matrix Inversion */ 
+    int numk[MAXF] =
+	{ 1, 2, 3, 4, 6, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0 };
+    
+	/*Set Defaults */ 
+	nfunc = 6;
+    for (i = 0; i < NMAX; i++) {
+	for (j = 0; j < MAXF; j++) {
+	    f[i][j] = 0.0;
+	}
+    }
+    make_matrix(nbands, npoint, nfunc, numk, f);
+    nwin = 3;
+    mmsw = 1;
+    
+	/*nds=npoint; */ 
+	nds = 2 * nfunc + 2;
+    
+	/*LMF Process */ 
+	/*Checking Pixel Value */ 
+	fitsw = 1;
+    
+	/*fitting method */ 
+	tcld = 0.15;
+    fitmd = 2;
+    
+	/*FITMD0---- */ 
+	if (fitmd == 0) {
+	thh = tcld * 2.0;
+	thl = -tcld;
+    }
+    
+	/*FITMD1---- */ 
+	if (fitmd == 1) {
+	thh = tcld;
+	thl = -tcld * 2.0;
+    }
+    
+	/*FITMD2---- */ 
+	if (fitmd == 2) {
+	thh = tcld;
+	thl = -tcld;
+    }
+    
+	/*initialize arrays */ 
+	for (k = 0; k < nbands; k++) {
+	dat0[k] = 0.0;
+	outpix[k] = 0.0;
+	ipoint[k] = 1;
+	idx1[k] = TRUE;
+    }
+    for (k = 0; k < nds; k++) {
+	c[k] = 0.0;
+    }
+    for (k = 0; k < nbands; k++) {
+	if (inpix[k] > 0.375) {
+	    ipoint[k] = 0;
+	}
+	if (k >= 2) {
+	    idk = (inpix[k] - inpix[k - 1]);
+	    if (idk > 0.125) {
+		ipoint[k] = 0;
+	    }
+	}
+	dat0[k] = inpix[k];
+	dat[k] = dat0[k];
+    }
+    
+	/*Minimax Filter */ 
+	if (mmsw >= 1 && fitmd == 0) {
+	minmax(nbands, nwin, dat);
+    }
+    else if (mmsw >= 1 && fitmd == 1) {
+	maxmin(nbands, nwin, dat);
+    }
+    
+	/*Fitting */ 
+	fitting(nbands, 3, dat, idx1, f, c, vfit);
+    for (k = 0; k < nbands; k++) {
+	outpix[k] = vfit[k];
+    }
+    return;
+}
+
+

Modified: grass-addons/gipe/i.lmf/main.c
===================================================================
--- grass-addons/gipe/i.lmf/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.lmf
@@ -41,183 +42,194 @@
 
 int lmf(int nbands, int npoint, double *inpix, double *outpix);
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-	struct 	Cell_head cellhd;/*region+header info*/
-	char 	*mapset; /*mapset name*/
-	int 	nrows, ncols;
-	int 	row,col;
+    struct Cell_head cellhd;	/*region+header info */
 
-	struct 	GModule *module;
-	struct 	Option *input,*ndate,*output;
-	
-	struct 	History history; /*metadata*/
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input, *ndate, *output;
+
+    struct History history;	/*metadata */
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	char 	*name; /*input raster name*/
-	char 	*result[MAXFILES]; /*output raster name*/
-	/*File Descriptors*/
-	int 	nfiles;
-	int 	infd[MAXFILES];
-	int 	outfd[MAXFILES];
+    /* FMEO Declarations**************** */
+    char *name;			/*input raster name */
 
-	char 	**names;
-	char 	**ptr;
-	
-	int 	ok;
-	
-	int 	i=0,j=0;
-	
-	void 	*inrast[MAXFILES];
-	DCELL 	*outrast[MAXFILES];
-	int 	data_format; /* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
-	RASTER_MAP_TYPE in_data_type[MAXFILES]; /* 0=numbers  1=text */
-	RASTER_MAP_TYPE out_data_type = DCELL_TYPE;
+    char *result[MAXFILES];	/*output raster name */
 
-	int	ndates;
-	double	inpix[MAXFILES]={0.0};
-	double	outpix[MAXFILES]={0.0};
+    /*File Descriptors */
+    int nfiles;
+
+    int infd[MAXFILES];
+
+    int outfd[MAXFILES];
+
+    char **names;
+
+    char **ptr;
+
+    int ok;
+
+    int i = 0, j = 0;
+
+    void *inrast[MAXFILES];
+
+    DCELL *outrast[MAXFILES];
+
+    int data_format;		/* 0=double  1=float  2=32bit signed int  5=8bit unsigned int (ie text) */
+
+    RASTER_MAP_TYPE in_data_type[MAXFILES];	/* 0=numbers  1=text */
+
+    RASTER_MAP_TYPE out_data_type = DCELL_TYPE;
+
+    int ndates;
+    double inpix[MAXFILES] = { 0.0 };
+    double outpix[MAXFILES] = { 0.0 };
+
 	/************************************/
 
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->keywords = _("LMF,Vegetation Indices,Atmospheric temporal correction");
-	module->description =
-		_("Temporal Local Maximum Fitting of vegetation indices, works also for surface reflectance data. Number of bands is potentially several years, nfiles and ndates are respectively the number of pixels and the number of pixels in a year\n");
+    module = G_define_module();
+    module->keywords =
+	_("LMF,Vegetation Indices,Atmospheric temporal correction");
+    module->description =
+	_("Temporal Local Maximum Fitting of vegetation indices, works also for surface reflectance data. Number of bands is potentially several years, nfiles and ndates are respectively the number of pixels and the number of pixels in a year\n");
 
-	/* Define the different options */
+    /* Define the different options */
 
-	input 		  = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input->description= _("Names of input layers");
+    input = G_define_standard_option(G_OPT_R_INPUTS);
+    input->description = _("Names of input layers");
 
-	ndate 		  = G_define_option();
-	ndate->key	  = _("ndate");
-	ndate->type       = TYPE_INTEGER;
-	ndate->required   = YES;
-	ndate->gisprompt  =_("parameter, integer");
-	ndate->description= _("Number of map layers per year");
+    ndate = G_define_option();
+    ndate->key = _("ndate");
+    ndate->type = TYPE_INTEGER;
+    ndate->required = YES;
+    ndate->gisprompt = _("parameter, integer");
+    ndate->description = _("Number of map layers per year");
 
-	output 		   = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description= _("Name of the output layer");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Name of the output layer");
 
-	/* FMEO init nfiles */
-	nfiles = 1;
+    /* FMEO init nfiles */
+    nfiles = 1;
+
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
-	
-	ok = 1;
-	names	= input->answers;
-	ptr	= input->answers;
+    if (G_parser(argc, argv))
+	exit(-1);
 
-	ndates  = atoi(ndate->answer);
+    ok = 1;
+    names = input->answers;
+    ptr = input->answers;
 
-	for (; *ptr != NULL; ptr++)
-	{
-		if (nfiles >= MAXFILES)
-			G_fatal_error (_("%s - too many ETa files. Only %d allowed"), G_program_name(), MAXFILES);
-		name = *ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error (_("cell file [%s] not found"), name);
-			ok = 0;
-		}
-		if (!ok){
-			continue;
-		}
-		infd[nfiles] = G_open_cell_old (name, mapset);
-		if (infd[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		in_data_type[nfiles] = G_raster_map_type(name, mapset);
-		if( (infd[nfiles] = G_open_cell_old(name,mapset)) < 0){
-			G_fatal_error(_("Cannot open cell file [%s]"), name);
-		}
-		if( (G_get_cellhd(name,mapset,&cellhd)) < 0){
-			G_fatal_error(_("Cannot read file header of [%s]"), name);
-		}
-		inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
-		nfiles++;
+    ndates = atoi(ndate->answer);
+
+    for (; *ptr != NULL; ptr++) {
+	if (nfiles >= MAXFILES)
+	    G_fatal_error(_("%s - too many ETa files. Only %d allowed"),
+			  G_program_name(), MAXFILES);
+	name = *ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), name);
+	    ok = 0;
 	}
-	nfiles--;
-	if (nfiles <= 10){
-		G_fatal_error(_("The min specified input map is ten"));
+	if (!ok) {
+	    continue;
 	}
-	
+	infd[nfiles] = G_open_cell_old(name, mapset);
+	if (infd[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	in_data_type[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd[nfiles] = G_open_cell_old(name, mapset)) < 0) {
+	    G_fatal_error(_("Cannot open cell file [%s]"), name);
+	}
+	if ((G_get_cellhd(name, mapset, &cellhd)) < 0) {
+	    G_fatal_error(_("Cannot read file header of [%s]"), name);
+	}
+	inrast[nfiles] = G_allocate_raster_buf(in_data_type[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 10) {
+	G_fatal_error(_("The min specified input map is ten"));
+    }
+
 	/***************************************************/
-	/* Allocate output buffer, use input map data_type */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	for(i=0;i<nfiles;i++){
-		sprintf(result[i],output->answer,".",i+1);
-		if (G_legal_filename (result[i]) < 0)
-		{
-			G_fatal_error (_("[%s] is an illegal name"), result[i]);
-			ok = 0;
-		}
-		outrast[i] = G_allocate_raster_buf(out_data_type);
-		/* Create New raster files */
-		if ( (outfd[i] = G_open_raster_new (result[i],1)) < 0){
-			G_fatal_error (_("Could not open <%s>"),result[i]);
-		}
+    /* Allocate output buffer, use input map data_type */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    for (i = 0; i < nfiles; i++) {
+	sprintf(result[i], output->answer, ".", i + 1);
+	if (G_legal_filename(result[i]) < 0) {
+	    G_fatal_error(_("[%s] is an illegal name"), result[i]);
+	    ok = 0;
 	}
+	outrast[i] = G_allocate_raster_buf(out_data_type);
+	/* Create New raster files */
+	if ((outfd[i] = G_open_raster_new(result[i], 1)) < 0) {
+	    G_fatal_error(_("Could not open <%s>"), result[i]);
+	}
+    }
+
 	/*******************/
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL de;
-		DCELL d[MAXFILES];
-		G_percent (row, nrows, 2);
-		/* read input map */
-		for (i=1;i<=nfiles;i++)
-		{
-			if( (G_get_raster_row(infd[i],inrast[i],row,in_data_type[i])) < 0){
-				G_fatal_error (_("Could not read from <%s>"),name);
-			}
+    /* Process pixels */
+    for (row = 0; row < nrows; row++) {
+	DCELL de;
+
+	DCELL d[MAXFILES];
+
+	G_percent(row, nrows, 2);
+	/* read input map */
+	for (i = 1; i <= nfiles; i++) {
+	    if ((G_get_raster_row(infd[i], inrast[i], row, in_data_type[i])) <
+		0) {
+		G_fatal_error(_("Could not read from <%s>"), name);
+	    }
+	}
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    for (i = 1; i <= nfiles; i++) {
+		switch (in_data_type[i]) {
+		case CELL_TYPE:
+		    inpix[i - 1] = (double)((CELL *) inrast[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    inpix[i - 1] = (double)((FCELL *) inrast[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    inpix[i - 1] = (double)((DCELL *) inrast[i])[col];
+		    break;
 		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			for(i=1;i<=nfiles;i++)
-			{
-				switch(in_data_type[i])
-				{
-					case CELL_TYPE:
-						inpix[i-1] = (double) ((CELL *) inrast[i])[col];
-						break;
-					case FCELL_TYPE:
-						inpix[i-1] = (double) ((FCELL *) inrast[i])[col];
-						break;
-					case DCELL_TYPE:
-						inpix[i-1] = (double) ((DCELL *) inrast[i])[col];
-						break;
-				}
-			}
-			lmf(nfiles, ndates, inpix, outpix);
-			/* Put the resulting temporal curve 
-			 * in the output layers */
-			for(i=0;i<nfiles;i++){
-				outrast[i][col] = outpix[i];
-			}
-		}
-		for(i=0;i<nfiles;i++){
-			if (G_put_raster_row (outfd[i], outrast[i], out_data_type) < 0)
-				G_fatal_error (_("Cannot write to <%s>"),result[i]);
-		}
+	    }
+	    lmf(nfiles, ndates, inpix, outpix);
+	    /* Put the resulting temporal curve 
+	     * in the output layers */
+	    for (i = 0; i < nfiles; i++) {
+		outrast[i][col] = outpix[i];
+	    }
 	}
-	for (i=1;i<=nfiles;i++)
-	{
-		G_free (inrast[i]);
-		G_close_cell (infd[i]);
-		G_free (outrast[i]);
-		G_close_cell (outfd[i]);
+	for (i = 0; i < nfiles; i++) {
+	    if (G_put_raster_row(outfd[i], outrast[i], out_data_type) < 0)
+		G_fatal_error(_("Cannot write to <%s>"), result[i]);
 	}
-	return 0;
+    }
+    for (i = 1; i <= nfiles; i++) {
+	G_free(inrast[i]);
+	G_close_cell(infd[i]);
+	G_free(outrast[i]);
+	G_close_cell(outfd[i]);
+    }
+    return 0;
 }

Modified: grass-addons/gipe/i.lmf/make_matrix.c
===================================================================
--- grass-addons/gipe/i.lmf/make_matrix.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/make_matrix.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,44 +1,50 @@
-/* Making Triangular Function Matrix/Vector*/
-
-#include<stdio.h>
-#include<stdlib.h>
-#include<math.h>
-
-#define NMAX 200
-#define MAXF 50
-
-#define PI 3.1415926535897932385
-
-int make_matrix(int n,int npoint,int nfunc,int *numk,double f[][MAXF]){
-        int     nn;
-        double  eps, vn, angle, theta;
-        int     i,j,j1,j2;
-        double  di,dk;
-	
-        nn=2*nfunc+2;
-        eps=1.0;
-        /*Set Constants*/
-        vn= (double) npoint;
-        angle=2.0*PI/vn;
-        /*Matrix Clear*/
-        for(i=0;i<n;i++){
-                for(j=0;j<nn;j++){
-                        f[i][j]=0.0;
-               }
-        }
-        /*Making Matrix*/
-        for(i=0;i<n;i++){
-                di= (double) (i+1);
-                for(j=0;j<nfunc;j++){
-                        j1=(j+1)*2;
-                        j2=j1+1;
-                        dk= (double) numk[j];
-                        theta=angle*di*dk;
-                        f[i][j1]=sin(theta);
-                        f[i][j2]=cos(theta);
-                }
-                f[i][0]=1.0;
-                f[i][1]=1.0E-1* (double) (i+1);
-       }
-       return;
-}      
+/* Making Triangular Function Matrix/Vector */  
+    
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+    
+#define NMAX 200
+#define MAXF 50
+    
+#define PI 3.1415926535897932385
+int make_matrix(int n, int npoint, int nfunc, int *numk, double f[][MAXF])
+{
+    int nn;
+
+    double eps, vn, angle, theta;
+
+    int i, j, j1, j2;
+
+    double di, dk;
+
+    nn = 2 * nfunc + 2;
+    eps = 1.0;
+    
+	/*Set Constants */ 
+	vn = (double)npoint;
+    angle = 2.0 * PI / vn;
+    
+	/*Matrix Clear */ 
+	for (i = 0; i < n; i++) {
+	for (j = 0; j < nn; j++) {
+	    f[i][j] = 0.0;
+	}
+    }
+    
+	/*Making Matrix */ 
+	for (i = 0; i < n; i++) {
+	di = (double)(i + 1);
+	for (j = 0; j < nfunc; j++) {
+	    j1 = (j + 1) * 2;
+	    j2 = j1 + 1;
+	    dk = (double)numk[j];
+	    theta = angle * di * dk;
+	    f[i][j1] = sin(theta);
+	    f[i][j2] = cos(theta);
+	} f[i][0] = 1.0;
+	f[i][1] = 1.0E-1 * (double)(i + 1);
+    } return;
+}
+
+

Modified: grass-addons/gipe/i.lmf/maxmin.c
===================================================================
--- grass-addons/gipe/i.lmf/maxmin.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/maxmin.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,45 +1,51 @@
-/* Maxmin Filter */
-#include<stdio.h>
-#include<stdlib.h>
-
-#define NMAX 200
-
-int maxmin(int n,int nwin,double *dat){
-        int i, j, jf, jr;
-        double da1[NMAX];
-        double vmin0=9.9E99;
-        double vminf,vminr;
-
-        for(i=0;i<nwin;i++){
-                da1[i]=dat[0];
-        }
-        for(i=0;i<n;i++){
-                da1[i+nwin]=dat[i];
-        }
-        for(i=0;i<nwin;i++){
-                da1[i+n+nwin]=dat[n];
-        }
-        for(i=0;i<n;i++){
-                vminf=vmin0;
-                vminr=vmin0;
-                for(j=0;j<nwin;j++){
-                        jf=i+nwin+j;
-                        jr=i+nwin-j;
-                        if(da1[jf]<vminf){
-                                vminf=da1[jf];
-                        }
-                        if(da1[jr]<vminr){
-                                vminr=da1[jr];
-                        }
-                }
-                if(vminf>=vminr){
-                        dat[i]=vminf;
-                } else {
-                        dat[i]=vminr;
-                }       
-                if(dat[i]>1.0E10){
-                        dat[i]=0.0;
-                }
-        }
-        return;
-}
+/* Maxmin Filter */  
+#include<stdio.h>
+#include<stdlib.h>
+    
+#define NMAX 200
+int maxmin(int n, int nwin, double *dat)
+{
+    int i, j, jf, jr;
+
+    double da1[NMAX];
+
+    double vmin0 = 9.9E99;
+
+    double vminf, vminr;
+
+    for (i = 0; i < nwin; i++) {
+	da1[i] = dat[0];
+    }
+    for (i = 0; i < n; i++) {
+	da1[i + nwin] = dat[i];
+    }
+    for (i = 0; i < nwin; i++) {
+	da1[i + n + nwin] = dat[n];
+    }
+    for (i = 0; i < n; i++) {
+	vminf = vmin0;
+	vminr = vmin0;
+	for (j = 0; j < nwin; j++) {
+	    jf = i + nwin + j;
+	    jr = i + nwin - j;
+	    if (da1[jf] < vminf) {
+		vminf = da1[jf];
+	    }
+	    if (da1[jr] < vminr) {
+		vminr = da1[jr];
+	    }
+	}
+	if (vminf >= vminr) {
+	    dat[i] = vminf;
+	}
+	else {
+	    dat[i] = vminr;
+	}
+	if (dat[i] > 1.0E10) {
+	    dat[i] = 0.0;
+	}
+    }
+    return;
+}
+
+

Modified: grass-addons/gipe/i.lmf/minmax.c
===================================================================
--- grass-addons/gipe/i.lmf/minmax.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.lmf/minmax.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,45 +1,51 @@
-/* Minmax Filter */
-#include<stdio.h>
-#include<stdlib.h>
-
-#define NMAX 200
-
-int minmax(int n, int nwin, double *dat){
-        int i,j,jf,jr;
-        double da1[NMAX];
-        double vmax0=-9.9E99;
-	double vmaxf,vmaxr;
-
-        for(i=0;i<nwin;i++){
-                da1[i]=dat[0];
-        }
-        for(i=0;i<n;i++){
-                da1[i+nwin]=dat[i];
-        }
-        for(i=0;i<nwin;i++){
-                da1[i+n+nwin]=dat[n];
-        }
-        for(i=0;i<n;i++){
-                vmaxf=vmax0;
-                vmaxr=vmax0;
-                for(j=0;j<nwin;j++){
-                        jf=i+nwin+j;
-                        jr=i+nwin-j;
-                        if(da1[jf]>vmaxf){
-                                vmaxf=da1[jf];
-                        }
-                        if(da1[jr]>vmaxr){
-                                vmaxr=da1[jr];
-                        }
-                }
-                if(vmaxf<=vmaxr){
-                        dat[i]=vmaxf;
-                } else {
-                        dat[i]=vmaxr;
-                }
-                if (dat[i]<=-1.0E10){
-                        dat[i]=0.0;
-                }
-        }
-        return;
-}
+/* Minmax Filter */  
+#include<stdio.h>
+#include<stdlib.h>
+    
+#define NMAX 200
+int minmax(int n, int nwin, double *dat)
+{
+    int i, j, jf, jr;
+
+    double da1[NMAX];
+
+    double vmax0 = -9.9E99;
+
+    double vmaxf, vmaxr;
+
+    for (i = 0; i < nwin; i++) {
+	da1[i] = dat[0];
+    }
+    for (i = 0; i < n; i++) {
+	da1[i + nwin] = dat[i];
+    }
+    for (i = 0; i < nwin; i++) {
+	da1[i + n + nwin] = dat[n];
+    }
+    for (i = 0; i < n; i++) {
+	vmaxf = vmax0;
+	vmaxr = vmax0;
+	for (j = 0; j < nwin; j++) {
+	    jf = i + nwin + j;
+	    jr = i + nwin - j;
+	    if (da1[jf] > vmaxf) {
+		vmaxf = da1[jf];
+	    }
+	    if (da1[jr] > vmaxr) {
+		vmaxr = da1[jr];
+	    }
+	}
+	if (vmaxf <= vmaxr) {
+	    dat[i] = vmaxf;
+	}
+	else {
+	    dat[i] = vmaxr;
+	}
+	if (dat[i] <= -1.0E10) {
+	    dat[i] = 0.0;
+	}
+    }
+    return;
+}
+
+

Modified: grass-addons/gipe/i.longitude/main.c
===================================================================
--- grass-addons/gipe/i.longitude/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.longitude/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,163 +1,187 @@
-/****************************************************************************
- *
- * MODULE:       i.longitude
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the longitude of the pixels in the map. 
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/gprojects.h>
-#include <grass/glocale.h>
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int not_ll=0;/*if proj is not lat/long, it will be 1.*/
-	struct GModule *module;
-	struct Option *input1, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	struct pj_info iproj;
-	struct pj_info oproj;
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd;
-	int outfd1;
-	
-	char *in;
-	int i=0,j=0;
-	double xp, yp;
-	double xmin, ymin;
-	double xmax, ymax;
-	double stepx,stepy;
-	double latitude, longitude;
-
-	void *inrast;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_inrast;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("longitude, projection");
-	module->description = _("creates a longitude map");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->description=_("Name of the input map");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output longitude layer");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	in	 	= input1->answer;
-	result1  	= output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(in, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), in);
-	}
-	data_type_inrast = G_raster_map_type(in,mapset);
-	if ( (infd = G_open_cell_old (in,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), in);
-	if (G_get_cellhd (in, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), in);
-	inrast = G_allocate_raster_buf(data_type_inrast);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
-
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
-
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	
-	/*Shamelessly stolen from r.sun !!!!	*/
-	/* Set up parameters for projection to lat/long if necessary */
-	if ((G_projection() != PROJECTION_LL)) {
-		not_ll=1;
-		struct Key_Value *in_proj_info, *in_unit_info;
-		if ((in_proj_info = G_get_projinfo()) == NULL)
-			G_fatal_error(_("Can't get projection info of current location"));
-		if ((in_unit_info = G_get_projunits()) == NULL)
-			G_fatal_error(_("Can't get projection units of current location"));
-		if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
-			G_fatal_error(_("Can't get projection key values of current location"));
-		G_free_key_value(in_proj_info);
-		G_free_key_value(in_unit_info);
-		/* Set output projection to latlong w/ same ellipsoid */
-		oproj.zone = 0;
-		oproj.meters = 1.;
-		sprintf(oproj.proj, "ll");
-		if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
-			G_fatal_error(_("Unable to set up lat/long projection parameters"));
-	}/* End of stolen from r.sun :P */
-	
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_lon;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd,inrast,row,data_type_inrast)<0)
-			G_fatal_error(_("Could not read from <%s>"),in);
-		for (col=0; col < ncols; col++)
-		{
-			latitude = ymax - ( row * stepy );
-			longitude = xmin + ( col * stepx );
-			if(not_ll){
-				if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
-				    G_fatal_error(_("Error in pj_do_proj"));
-				}
-			}else{
-				/*Do nothing*/
-			}	
-			d_lon = longitude;
-			outrast1[col] = d_lon;
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast);
-	G_close_cell (infd);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.longitude
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the longitude of the pixels in the map. 
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/gprojects.h>
+#include <grass/glocale.h>
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int not_ll = 0;		/*if proj is not lat/long, it will be 1. */
+
+    struct GModule *module;
+
+    struct Option *input1, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    struct pj_info iproj;
+
+    struct pj_info oproj;
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd;
+
+    int outfd1;
+
+    char *in;
+
+    int i = 0, j = 0;
+
+    double xp, yp;
+
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
+    void *inrast;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_inrast;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("longitude, projection");
+    module->description = _("creates a longitude map");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->description = _("Name of the input map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output longitude layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    in = input1->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(in, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), in);
+    }
+    data_type_inrast = G_raster_map_type(in, mapset);
+    if ((infd = G_open_cell_old(in, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), in);
+    if (G_get_cellhd(in, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), in);
+    inrast = G_allocate_raster_buf(data_type_inrast);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    
+	/*Shamelessly stolen from r.sun !!!!    */ 
+	/* Set up parameters for projection to lat/long if necessary */ 
+	if ((G_projection() != PROJECTION_LL)) {
+	not_ll = 1;
+	struct Key_Value *in_proj_info, *in_unit_info;
+
+	if ((in_proj_info = G_get_projinfo()) == NULL)
+	    G_fatal_error(_("Can't get projection info of current location"));
+	if ((in_unit_info = G_get_projunits()) == NULL)
+	    G_fatal_error(_("Can't get projection units of current location"));
+	if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
+	    G_fatal_error(_("Can't get projection key values of current location"));
+	G_free_key_value(in_proj_info);
+	G_free_key_value(in_unit_info);
+	
+	    /* Set output projection to latlong w/ same ellipsoid */ 
+	    oproj.zone = 0;
+	oproj.meters = 1.;
+	sprintf(oproj.proj, "ll");
+	if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
+	    G_fatal_error(_("Unable to set up lat/long projection parameters"));
+    }				/* End of stolen from r.sun :P */
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_lon;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row(infd, inrast, row, data_type_inrast) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), in);
+	for (col = 0; col < ncols; col++)
+	     {
+	    latitude = ymax - (row * stepy);
+	    longitude = xmin + (col * stepx);
+	    if (not_ll) {
+		if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
+		    G_fatal_error(_("Error in pj_do_proj"));
+		}
+	    }
+	    else {
+		
+		    /*Do nothing */ 
+	    }
+	    d_lon = longitude;
+	    outrast1[col] = d_lon;
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast);
+    G_close_cell(infd);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/main.c
===================================================================
--- grass-addons/gipe/i.qc.modis/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,261 +1,326 @@
-/****************************************************************************
- *
- * MODULE:       i.qc.modis
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Converts Quality Control indicators into human readable classes
- * 		 for Modis surface reflectance products 250m/500m
- * 		 (MOD09Q/MOD09A)
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- * 
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-/* 250m Products (MOD09Q) */
-int qc250a( unsigned int pixel );
-int qc250b( unsigned int pixel );
-int qc250c( unsigned int pixel, int bandno );
-int qc250d( unsigned int pixel );
-int qc250e( unsigned int pixel );
-int qc250f( unsigned int pixel );
-
-/* 500m Products (MOD09A) */
-int qc500a( long int pixel );
-int qc500c( long int pixel, int bandno );
-int qc500d( long int pixel );
-int qc500e( long int pixel );
-
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	char *qcflag;/*Switch for particular index*/
-	
-	struct GModule *module;
-	struct Option *input1, *input2, *input_band, *output;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd;
-	int outfd;
-	
-	char  *qcchan;
-	int	bandno;
-	
-	int i=0,j=0;
-	
-	void  *inrast;
-	CELL *outrast;
-	RASTER_MAP_TYPE data_type_output=CELL_TYPE;
-	RASTER_MAP_TYPE data_type_qcchan;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("QC, Quality Control, surface reflectance, Modis");
-	module->description = _("Extract quality control parameters from Modis QC layers");
-
-	/* Define the different options */
-	input1 = G_define_option() ;
-	input1->key        =_("qcname");
-	input1->type       = TYPE_STRING;
-	input1->required   = YES;
-	input1->gisprompt  =_("Name of QC type to extract");
-	input1->description=_("Name of QC: modland_qa_bits, cloud, data_quality, atcorr, adjcorr, diff_orbit_from_500m");
-	input1->answer     =_("modland_qa_bits");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->description=_("Name of the surface reflectance QC layer [bit array]");
-
-	input_band 		= G_define_option();
-	input_band->key		= "band";
-	input_band->type 	= TYPE_INTEGER;
-	input_band->required 	= NO;
-	input_band->gisprompt 	= "old,value";
-	input_band->description = _("Band number of Modis product 250m=[1,2],500m=[1-7]");
-
-	output= G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key        =_("output");
-	output->description=_("Name of the output QC type classification layer");
-	output->answer     =_("qc");
-
-	flag1 = G_define_flag() ;
-	flag1->key         = 'A' ;
-	flag1->description = _("QC for MOD09A product @ 500m instead of default MOD09Q at 250m") ;
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-	qcflag		= input1->answer;
-	qcchan	 	= input2->answer;
-	
-	if(input_band->answer){
-		bandno = atoi(input_band->answer);
-	}
-	result  = output->answer;
-	/***************************************************/
-	if ((!strcoll(qcflag,"cloud")&&flag1->answer)||
-	(!strcoll(qcflag,"diff_orbit_from_500m")&&flag1->answer)){
-		G_fatal_error("Those flags cannot work with MOD09A @ 500m products");
-	}
-	if (!strcoll(qcflag,"data_quality")){
-		if(bandno<1||bandno>7)
-			G_fatal_error("band number out of allowed range [1-7]");
-		if(!flag1->answer&&bandno>2)
-			G_fatal_error("250m band number is out of allowed range [1,2]");
-	}		
-	/***************************************************/
-	mapset = G_find_cell2 (qcchan, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),qcchan);
-	}
-	data_type_qcchan = G_raster_map_type(qcchan,mapset);
-	if ( (infd = G_open_cell_old (qcchan,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), qcchan);
-	if (G_get_cellhd (qcchan, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), qcchan);
-	inrast = G_allocate_raster_buf(data_type_qcchan);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		CELL c;
-		unsigned int qc250chan;
-		CELL qc500chan;
-
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd,inrast,row,data_type_qcchan)<0)
-			G_fatal_error(_("Could not read from <%s>"),qcchan);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{	
-			switch(data_type_qcchan){
-				case CELL_TYPE:
-					c = (int) ((CELL *) inrast)[col];
-					break;
-				case FCELL_TYPE:
-					c = (int) ((FCELL *) inrast)[col];
-					break;
-				case DCELL_TYPE:
-					c = (int) ((DCELL *) inrast)[col];
-					break;
-			}
-			if(flag1->answer){
-				/* This is a MOD09A at 500m product, QC layer is 32-bit */
-				qc500chan = c;
-			} else {
-				/* This is a MOD09A at 250m product, QC layer is 16-bit */
-				qc250chan = (unsigned int) ((CELL *) inrast)[col];
-			}
-			if(G_is_c_null_value(&c)){
-				G_set_c_null_value(&outrast[col],1);
-			} else {
-				/*calculate modland QA bits extraction  */
-				if (!strcoll(qcflag,"modland_qa_bits")){
-					if(flag1->answer){
-						/* 500m product */
-						c =  qc500a( qc500chan );
-					} else {		
-						/* 250m product */
-						c =  qc250a( qc250chan );
-					}
-					outrast[col] = c;
-				} else 
-				/*calculate cloud state  */
-				if (!strcoll(qcflag,"cloud")){
-					/* ONLY 250m product! */
-					c =  qc250b( qc250chan );
-					outrast[col] = c;
-				} else 
-				/*calculate modland QA bits extraction  */
-				if (!strcoll(qcflag,"data_quality")){
-					if(flag1->answer){
-						/* 500m product */
-						c =  qc500c( qc500chan, bandno );
-					} else {		
-						/* 250m product */
-						c =  qc250c( qc250chan, bandno );
-					}
-					outrast[col] = c;
-				} else 
-				/*calculate atmospheric correction flag  */
-				if (!strcoll(qcflag,"atcorr")){
-					if(flag1->answer){
-						/* 500m product */
-						c =  qc500d( qc500chan );
-					} else {		
-						/* 250m product */
-						c =  qc250d( qc250chan );
-					}
-					outrast[col] = c;
-				} else 
-				/*calculate adjacency correction flag  */
-				if (!strcoll(qcflag,"adjcorr")){
-					if(flag1->answer){
-						/* 500m product */
-						c =  qc500e( qc500chan );
-					} else {		
-						/* 250m product */
-						c =  qc250e( qc250chan );
-					}
-					outrast[col] = c;
-				} else 
-				/*calculate different orbit from 500m flag  */
-				if (!strcoll(qcflag,"diff_orbit_from_500m")){
-					/* ONLY 250m product! */
-					c =  qc250f( qc500chan );
-					outrast[col] = c;
-				} else {
-					/* Signal user that the flag name is badly written */
-					/* therefore not understood by the application*/
-					G_fatal_error("Unknown flag name, please check spelling");
-				}
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	
-	G_free(inrast);
-	G_close_cell(infd);
-	G_free(outrast);
-	G_close_cell(outfd);
-
-	/* Color from -1.0 to +1.0 in grey */
-	G_init_colors(&colors);
-	G_add_color_rule(0,0,0,0,10,255,255,255,&colors);
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.qc.modis
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Converts Quality Control indicators into human readable classes
+ * 		 for Modis surface reflectance products 250m/500m
+ * 		 (MOD09Q/MOD09A)
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ * 
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+    
+    /* 250m Products (MOD09Q) */ 
+int qc250a(unsigned int pixel);
+
+int qc250b(unsigned int pixel);
+
+int qc250c(unsigned int pixel, int bandno);
+
+int qc250d(unsigned int pixel);
+
+int qc250e(unsigned int pixel);
+
+int qc250f(unsigned int pixel);
+
+
+    /* 500m Products (MOD09A) */ 
+int qc500a(long int pixel);
+
+int qc500c(long int pixel, int bandno);
+
+int qc500d(long int pixel);
+
+int qc500e(long int pixel);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    char *qcflag;		/*Switch for particular index */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input_band, *output;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd;
+
+    int outfd;
+
+    char *qcchan;
+
+    int bandno;
+
+    int i = 0, j = 0;
+
+    void *inrast;
+
+    CELL * outrast;
+    RASTER_MAP_TYPE data_type_output = CELL_TYPE;
+    RASTER_MAP_TYPE data_type_qcchan;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("QC, Quality Control, surface reflectance, Modis");
+    module->description =
+	_("Extract quality control parameters from Modis QC layers");
+    
+	/* Define the different options */ 
+	input1 = G_define_option();
+    input1->key = _("qcname");
+    input1->type = TYPE_STRING;
+    input1->required = YES;
+    input1->gisprompt = _("Name of QC type to extract");
+    input1->description =
+	_("Name of QC: modland_qa_bits, cloud, data_quality, atcorr, adjcorr, diff_orbit_from_500m");
+    input1->answer = _("modland_qa_bits");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->description =
+	_("Name of the surface reflectance QC layer [bit array]");
+    input_band = G_define_option();
+    input_band->key = "band";
+    input_band->type = TYPE_INTEGER;
+    input_band->required = NO;
+    input_band->gisprompt = "old,value";
+    input_band->description =
+	_("Band number of Modis product 250m=[1,2],500m=[1-7]");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key = _("output");
+    output->description =
+	_("Name of the output QC type classification layer");
+    output->answer = _("qc");
+    flag1 = G_define_flag();
+    flag1->key = 'A';
+    flag1->description =
+	_("QC for MOD09A product @ 500m instead of default MOD09Q at 250m");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    qcflag = input1->answer;
+    qcchan = input2->answer;
+    if (input_band->answer) {
+	bandno = atoi(input_band->answer);
+    }
+    result = output->answer;
+    
+
+	/***************************************************/ 
+	if ((!strcoll(qcflag, "cloud") && flag1->answer) || 
+	    (!strcoll(qcflag, "diff_orbit_from_500m") && flag1->answer)) {
+	G_fatal_error("Those flags cannot work with MOD09A @ 500m products");
+    }
+    if (!strcoll(qcflag, "data_quality")) {
+	if (bandno < 1 || bandno > 7)
+	    G_fatal_error("band number out of allowed range [1-7]");
+	if (!flag1->answer && bandno > 2)
+	    G_fatal_error("250m band number is out of allowed range [1,2]");
+    }
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(qcchan, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), qcchan);
+    }
+    data_type_qcchan = G_raster_map_type(qcchan, mapset);
+    if ((infd = G_open_cell_old(qcchan, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), qcchan);
+    if (G_get_cellhd(qcchan, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), qcchan);
+    inrast = G_allocate_raster_buf(data_type_qcchan);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	CELL c;
+	unsigned int qc250chan;
+
+	CELL qc500chan;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row(infd, inrast, row, data_type_qcchan) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), qcchan);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_qcchan) {
+	    case CELL_TYPE:
+		c = (int)((CELL *) inrast)[col];
+		break;
+	    case FCELL_TYPE:
+		c = (int)((FCELL *) inrast)[col];
+		break;
+	    case DCELL_TYPE:
+		c = (int)((DCELL *) inrast)[col];
+		break;
+	    }
+	    if (flag1->answer) {
+		
+		    /* This is a MOD09A at 500m product, QC layer is 32-bit */ 
+		    qc500chan = c;
+	    }
+	    else {
+		
+		    /* This is a MOD09A at 250m product, QC layer is 16-bit */ 
+		    qc250chan = (unsigned int)((CELL *) inrast)[col];
+	    } if (G_is_c_null_value(&c)) {
+		G_set_c_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+		    /*calculate modland QA bits extraction  */ 
+		    if (!strcoll(qcflag, "modland_qa_bits")) {
+		    if (flag1->answer) {
+			
+			    /* 500m product */ 
+			    c = qc500a(qc500chan);
+		    }
+		    else {
+			
+			    /* 250m product */ 
+			    c = qc250a(qc250chan);
+		    }
+		    outrast[col] = c;
+		}
+		else
+		    
+			/*calculate cloud state  */ 
+		    if (!strcoll(qcflag, "cloud")) {
+		    
+			/* ONLY 250m product! */ 
+			c = qc250b(qc250chan);
+		    outrast[col] = c;
+		}
+		else
+		    
+			/*calculate modland QA bits extraction  */ 
+		    if (!strcoll(qcflag, "data_quality")) {
+		    if (flag1->answer) {
+			
+			    /* 500m product */ 
+			    c = qc500c(qc500chan, bandno);
+		    }
+		    else {
+			
+			    /* 250m product */ 
+			    c = qc250c(qc250chan, bandno);
+		    }
+		    outrast[col] = c;
+		}
+		else
+		    
+			/*calculate atmospheric correction flag  */ 
+		    if (!strcoll(qcflag, "atcorr")) {
+		    if (flag1->answer) {
+			
+			    /* 500m product */ 
+			    c = qc500d(qc500chan);
+		    }
+		    else {
+			
+			    /* 250m product */ 
+			    c = qc250d(qc250chan);
+		    }
+		    outrast[col] = c;
+		}
+		else
+		    
+			/*calculate adjacency correction flag  */ 
+		    if (!strcoll(qcflag, "adjcorr")) {
+		    if (flag1->answer) {
+			
+			    /* 500m product */ 
+			    c = qc500e(qc500chan);
+		    }
+		    else {
+			
+			    /* 250m product */ 
+			    c = qc250e(qc250chan);
+		    }
+		    outrast[col] = c;
+		}
+		else
+		    
+			/*calculate different orbit from 500m flag  */ 
+		    if (!strcoll(qcflag, "diff_orbit_from_500m")) {
+		    
+			/* ONLY 250m product! */ 
+			c = qc250f(qc500chan);
+		    outrast[col] = c;
+		}
+		else {
+		    
+			/* Signal user that the flag name is badly written */ 
+			/* therefore not understood by the application */ 
+			G_fatal_error
+			("Unknown flag name, please check spelling");
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast);
+    G_close_cell(infd);
+    G_free(outrast);
+    G_close_cell(outfd);
+    
+	/* Color from -1.0 to +1.0 in grey */ 
+	G_init_colors(&colors);
+    G_add_color_rule(0, 0, 0, 0, 10, 255, 255, 255, &colors);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250a.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250a.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250a.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,33 +1,39 @@
-/* MODLAND QA Bits 250m Unsigned Int bits[0-1]
- * 00 -> class 1: Corrected product produced at ideal quality -- all bands
- * 01 -> class 2: Corrected product produced at less than idel quality -- some or all bands
- * 10 -> class 3: Corrected product NOT produced due to cloud effect -- all bands
- * 11 -> class 4: Corrected product NOT produced due to other reasons -- some or all bands mayb be fill value (Note that a value of [11] overrides a value of [01])
- */
-
-int qc250a( unsigned int pixel )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x02){
-		/* Non-Corrected product */
-		if(qctemp & 0x01){
-			class=4;/*other reasons*/
-		} else {
-			class=3;/*because of clouds*/
-		}
-	} else {
-		/* Corrected product */
-		if(qctemp & 0x01){
-			class=2;/*less than ideal quality*/
-		} else {
-			class=1;/*ideal quality*/
-		}
-	}
-	return class;
-}
-
+/* MODLAND QA Bits 250m Unsigned Int bits[0-1]
+ * 00 -> class 1: Corrected product produced at ideal quality -- all bands
+ * 01 -> class 2: Corrected product produced at less than idel quality -- some or all bands
+ * 10 -> class 3: Corrected product NOT produced due to cloud effect -- all bands
+ * 11 -> class 4: Corrected product NOT produced due to other reasons -- some or all bands mayb be fill value (Note that a value of [11] overrides a value of [01])
+ */  
+int qc250a(unsigned int pixel) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x02) {
+	
+	    /* Non-Corrected product */ 
+	    if (qctemp & 0x01) {
+	    class = 4;		/*other reasons */
+	}
+	else {
+	    class = 3;		/*because of clouds */
+	}
+    }
+    else {
+	
+	    /* Corrected product */ 
+	    if (qctemp & 0x01) {
+	    class = 2;		/*less than ideal quality */
+	}
+	else {
+	    class = 1;		/*ideal quality */
+	}
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250b.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250b.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250b.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,32 +1,36 @@
-/* Cloud State 250m Unsigned Int bits[2-3]
- * 00 -> class 1: Clear -- No clouds
- * 01 -> class 2: Cloudy
- * 10 -> class 3: Mixed
- * 11 -> class 4: Not Set ; Assumed Clear
- */
-
-int qc250b( unsigned int pixel )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>2;/*bits [2-3] become [0-1]*/
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x02){ /* 1 ? */
-		if(qctemp & 0x01){
-			class=4;/*Not Set ; Assumed Clear*/
-		} else {
-			class=3;/*Mixed clouds*/
-		}
-	} else { /* 0 ? */
-		if(qctemp & 0x01){
-			class=2;/*Cloudy*/
-		} else {
-			class=1;/*Clear -- No Clouds*/
-		}
-	}
-	return class;
-}
-
+/* Cloud State 250m Unsigned Int bits[2-3]
+ * 00 -> class 1: Clear -- No clouds
+ * 01 -> class 2: Cloudy
+ * 10 -> class 3: Mixed
+ * 11 -> class 4: Not Set ; Assumed Clear
+ */  
+int qc250b(unsigned int pixel) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 2;		/*bits [2-3] become [0-1] */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x02) {	/* 1 ? */
+	if (qctemp & 0x01) {
+	    class = 4;		/*Not Set ; Assumed Clear */
+	}
+	else {
+	    class = 3;		/*Mixed clouds */
+	}
+    }
+    else {			/* 0 ? */
+	if (qctemp & 0x01) {
+	    class = 2;		/*Cloudy */
+	}
+	else {
+	    class = 1;		/*Clear -- No Clouds */
+	}
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250c.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250c.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250c.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,88 +1,103 @@
-/* Band-wise Data Quality 250m Unsigned Int bits[0-1]
- * 0000 -> class 1: highest quality
- * 0111 -> class 2: noisy detector
- * 1000 -> class 3: dead detector; data interpolated in L1B
- * 1001 -> class 4: solar zenith >= 86 degrees
- * 1010 -> class 5: solar zenith >= 85 and < 86 degrees
- * 1011 -> class 6: missing input
- * 1100 -> class 7: internal constant used in place of climatological data for at least one atmospheric constant
- * 1101 -> class 8: correction out of bounds, pixel constrained to extreme allowable value
- * 1110 -> class 9: L1B data faulty
- * 1111 -> class 10: not processed due to deep ocean or cloud
- * Class 11: Combination of bits unused
- */
-
-int qc250c( unsigned int pixel , int bandno )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>4*bandno;/* bitshift [4-7] or [8-11] to [0-3] */
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x08){ /* 1??? */
-		if(qctemp & 0x04){ /* 11?? */
-			if(qctemp & 0x02){ /* 111? */
-				if(qctemp & 0x01){ /* 1111 */
-					class=10;/*Not proc.ocean/cloud*/
-				} else { /* 1110 */
-					class=9;/*L1B faulty data*/
-				}
-			} else { /* 110? */
-				if(qctemp & 0x01){ /* 1101 */
-					class=8;/*corr. out of bounds*/
-				} else { /* 1100 */
-					class=7;/*internal constant used*/
-				}
-			}
-		} else {
-			if(qctemp & 0x02){ /* 101? */
-				if(qctemp & 0x01){ /* 1011 */
-					class=6;/*missing input*/
-				} else { /* 1010 */
-					class=5;/*solarzen>=85&&<86*/
-				}
-			}else { /* 100? */
-				if(qctemp & 0x01){ /* 1001 */
-					class=4;/*solar zenith angle>=86*/
-				} else { /* 1000 */
-					class=3;/*Dead detector*/
-				}
-			}
-
-		}
-	} else { /* 0??? */
-		if(qctemp & 0x04){ /* 01?? */
-			if(qctemp & 0x02){ /* 011? */
-				if(qctemp & 0x01){ /* 0111 */
-					class=2;/*Noisy detector*/
-				} else { /* 0110 */
-					class=11;/*Unused*/
-				}
-			} else { /* 010? */
-				if(qctemp & 0x01){ /* 0101 */
-					class=11;/*Unused*/
-				} else { /* 0100 */
-					class=11;/*Unused*/
-				}
-			}
-		} else { /* 00?? */
-			if(qctemp & 0x02){ /* 001? */
-				if(qctemp & 0x01){ /* 0011 */
-					class=11;/*Unused*/
-				} else { /* 0010 */
-					class=11;/*Unused*/
-				}
-			} else { /* 000? */
-				if(qctemp & 0x01){ /* 0001 */
-					class=11;/*Unused*/
-				} else { /* 0000 */
-					class=1;/*Highest quality*/
-				}
-			}
-		}
-	}
-	return class;
-}
-
+/* Band-wise Data Quality 250m Unsigned Int bits[0-1]
+ * 0000 -> class 1: highest quality
+ * 0111 -> class 2: noisy detector
+ * 1000 -> class 3: dead detector; data interpolated in L1B
+ * 1001 -> class 4: solar zenith >= 86 degrees
+ * 1010 -> class 5: solar zenith >= 85 and < 86 degrees
+ * 1011 -> class 6: missing input
+ * 1100 -> class 7: internal constant used in place of climatological data for at least one atmospheric constant
+ * 1101 -> class 8: correction out of bounds, pixel constrained to extreme allowable value
+ * 1110 -> class 9: L1B data faulty
+ * 1111 -> class 10: not processed due to deep ocean or cloud
+ * Class 11: Combination of bits unused
+ */  
+int qc250c(unsigned int pixel, int bandno) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 4 * bandno;	/* bitshift [4-7] or [8-11] to [0-3] */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x08) {	/* 1??? */
+	if (qctemp & 0x04) {	/* 11?? */
+	    if (qctemp & 0x02) {	/* 111? */
+		if (qctemp & 0x01) {	/* 1111 */
+		    class = 10;	/*Not proc.ocean/cloud */
+		}
+		else {		/* 1110 */
+		    class = 9;	/*L1B faulty data */
+		}
+	    }
+	    else {		/* 110? */
+		if (qctemp & 0x01) {	/* 1101 */
+		    class = 8;	/*corr. out of bounds */
+		}
+		else {		/* 1100 */
+		    class = 7;	/*internal constant used */
+		}
+	    }
+	}
+	else {
+	    if (qctemp & 0x02) {	/* 101? */
+		if (qctemp & 0x01) {	/* 1011 */
+		    class = 6;	/*missing input */
+		}
+		else {		/* 1010 */
+		    class = 5;	/*solarzen>=85&&<86 */
+		}
+	    }
+	    else {		/* 100? */
+		if (qctemp & 0x01) {	/* 1001 */
+		    class = 4;	/*solar zenith angle>=86 */
+		}
+		else {		/* 1000 */
+		    class = 3;	/*Dead detector */
+		}
+	    }
+	}
+    }
+    else {			/* 0??? */
+	if (qctemp & 0x04) {	/* 01?? */
+	    if (qctemp & 0x02) {	/* 011? */
+		if (qctemp & 0x01) {	/* 0111 */
+		    class = 2;	/*Noisy detector */
+		}
+		else {		/* 0110 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	    else {		/* 010? */
+		if (qctemp & 0x01) {	/* 0101 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0100 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	}
+	else {			/* 00?? */
+	    if (qctemp & 0x02) {	/* 001? */
+		if (qctemp & 0x01) {	/* 0011 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0010 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	    else {		/* 000? */
+		if (qctemp & 0x01) {	/* 0001 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0000 */
+		    class = 1;	/*Highest quality */
+		}
+	    }
+	}
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250d.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250d.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250d.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-/* Atmospheric correction 250m Unsigned Int bit[12]
- * 00 -> class 1: Not Corrected product
- * 01 -> class 2: Corrected product
- */
-
-int qc250d( unsigned int pixel )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>12; /* bit no 12 becomes 0 */
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x01){
-		class=2;/*Corrected*/
-	} else {
-		class=1;/*Not corrected*/
-	}
-	return class;
-}
-
+/* Atmospheric correction 250m Unsigned Int bit[12]
+ * 00 -> class 1: Not Corrected product
+ * 01 -> class 2: Corrected product
+ */  
+int qc250d(unsigned int pixel) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 12;		/* bit no 12 becomes 0 */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x01) {
+	class = 2;		/*Corrected */
+    }
+    else {
+	class = 1;		/*Not corrected */
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250e.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250e.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250e.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-/* Adjacency correction 250m Unsigned Int bit[13]
- * 00 -> class 1: Not Corrected product
- * 01 -> class 2: Corrected product
- */
-
-int qc250e( unsigned int pixel )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>13; /* bit no 13 becomes 0 */
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x01){
-		class=2;/*Corrected*/
-	} else {
-		class=1;/*Not corrected*/
-	}
-	return class;
-}
-
+/* Adjacency correction 250m Unsigned Int bit[13]
+ * 00 -> class 1: Not Corrected product
+ * 01 -> class 2: Corrected product
+ */  
+int qc250e(unsigned int pixel) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 13;		/* bit no 13 becomes 0 */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x01) {
+	class = 2;		/*Corrected */
+    }
+    else {
+	class = 1;		/*Not corrected */
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc250f.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc250f.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc250f.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-/* Different orbit from 500m product, 250m Unsigned Int bit[14]
- * 00 -> class 1: same orbit as 500m
- * 01 -> class 2: different orbit from 500m
- */
-
-int qc250f( unsigned int pixel )
-{
-	unsigned int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>14; /* bit no 14 becomes 0 */
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x01){
-		class=2;/*different orbit*/
-	} else {
-		class=1;/*same orbit*/
-	}
-	return class;
-}
-
+/* Different orbit from 500m product, 250m Unsigned Int bit[14]
+ * 00 -> class 1: same orbit as 500m
+ * 01 -> class 2: different orbit from 500m
+ */  
+int qc250f(unsigned int pixel) 
+{
+    unsigned int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 14;		/* bit no 14 becomes 0 */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x01) {
+	class = 2;		/*different orbit */
+    }
+    else {
+	class = 1;		/*same orbit */
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc500a.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc500a.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc500a.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,34 +1,39 @@
-/* MODLAND QA Bits 500m long int bits[0-1]
- * 00 -> class 1: Corrected product produced at ideal quality -- all bands
- * 01 -> class 2: Corrected product produced at less than idel quality -- some or all bands
- * 10 -> class 3: Corrected product NOT produced due to cloud effect -- all bands
- * 11 -> class 4: Corrected product NOT produced due to other reasons -- some or all bands mayb be fill value (Note that a value of [11] overrides a value of [01])
- */
-
-int qc500a( long int pixel )
-{
-	long int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x02){
-		/* Non-Corrected product */
-		if(qctemp & 0x01){
-			class=4;/*other reasons*/
-		} else {
-			class=3;/*because of clouds*/
-		}
-	} else {
-		/* Corrected product */
-		if(qctemp & 0x01){
-			class=2;/*less than ideal quality*/
-		} else {
-			class=1;/*ideal quality*/
-		}
-	}
-	return class;
-
-}
-
+/* MODLAND QA Bits 500m long int bits[0-1]
+ * 00 -> class 1: Corrected product produced at ideal quality -- all bands
+ * 01 -> class 2: Corrected product produced at less than idel quality -- some or all bands
+ * 10 -> class 3: Corrected product NOT produced due to cloud effect -- all bands
+ * 11 -> class 4: Corrected product NOT produced due to other reasons -- some or all bands mayb be fill value (Note that a value of [11] overrides a value of [01])
+ */  
+int qc500a(long int pixel) 
+{
+    long int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x02) {
+	
+	    /* Non-Corrected product */ 
+	    if (qctemp & 0x01) {
+	    class = 4;		/*other reasons */
+	}
+	else {
+	    class = 3;		/*because of clouds */
+	}
+    }
+    else {
+	
+	    /* Corrected product */ 
+	    if (qctemp & 0x01) {
+	    class = 2;		/*less than ideal quality */
+	}
+	else {
+	    class = 1;		/*ideal quality */
+	}
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc500c.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc500c.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc500c.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,89 +1,104 @@
-/* Band-wise Data Quality 500m long Int 
- * bits[2-5][6-9][10-13][14-17][18-21][22-25][26-29]
- * 0000 -> class 1: highest quality
- * 0111 -> class 2: noisy detector
- * 1000 -> class 3: dead detector; data interpolated in L1B
- * 1001 -> class 4: solar zenith >= 86 degrees
- * 1010 -> class 5: solar zenith >= 85 and < 86 degrees
- * 1011 -> class 6: missing input
- * 1100 -> class 7: internal constant used in place of climatological data for at least one atmospheric constant
- * 1101 -> class 8: correction out of bounds, pixel constrained to extreme allowable value
- * 1110 -> class 9: L1B data faulty
- * 1111 -> class 10: not processed due to deep ocean or cloud
- * Class 11: Combination of bits unused
- */
-
-int qc500c( long int pixel , int bandno )
-{
-	long int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>2+(4*bandno-1);/* bitshift [] to [0-3] etc. */
-	swab(&swabfrom,&swabto,4);
-	qctemp=swabto;
-	if(qctemp & 0x08){ /* 1??? */
-		if(qctemp & 0x04){ /* 11?? */
-			if(qctemp & 0x02){ /* 111? */
-				if(qctemp & 0x01){ /* 1111 */
-					class=10;/*Not proc.ocean/cloud*/
-				} else { /* 1110 */
-					class=9;/*L1B faulty data*/
-				}
-			} else { /* 110? */
-				if(qctemp & 0x01){ /* 1101 */
-					class=8;/*corr. out of bounds*/
-				} else { /* 1100 */
-					class=7;/*internal constant used*/
-				}
-			}
-		} else {
-			if(qctemp & 0x02){ /* 101? */
-				if(qctemp & 0x01){ /* 1011 */
-					class=6;/*missing input*/
-				} else { /* 1010 */
-					class=5;/*solarzen>=85&&<86*/
-				}
-			}else { /* 100? */
-				if(qctemp & 0x01){ /* 1001 */
-					class=4;/*solar zenith angle>=86*/
-				} else { /* 1000 */
-					class=3;/*Dead detector*/
-				}
-			}
-
-		}
-	} else { /* 0??? */
-		if(qctemp & 0x04){ /* 01?? */
-			if(qctemp & 0x02){ /* 011? */
-				if(qctemp & 0x01){ /* 0111 */
-					class=2;/*Noisy detector*/
-				} else { /* 0110 */
-					class=11;/*Unused*/
-				}
-			} else { /* 010? */
-				if(qctemp & 0x01){ /* 0101 */
-					class=11;/*Unused*/
-				} else { /* 0100 */
-					class=11;/*Unused*/
-				}
-			}
-		} else { /* 00?? */
-			if(qctemp & 0x02){ /* 001? */
-				if(qctemp & 0x01){ /* 0011 */
-					class=11;/*Unused*/
-				} else { /* 0010 */
-					class=11;/*Unused*/
-				}
-			} else { /* 000? */
-				if(qctemp & 0x01){ /* 0001 */
-					class=11;/*Unused*/
-				} else { /* 0000 */
-					class=1;/*Highest quality*/
-				}
-			}
-		}
-	}
-	return class;
-}
-
+/* Band-wise Data Quality 500m long Int 
+ * bits[2-5][6-9][10-13][14-17][18-21][22-25][26-29]
+ * 0000 -> class 1: highest quality
+ * 0111 -> class 2: noisy detector
+ * 1000 -> class 3: dead detector; data interpolated in L1B
+ * 1001 -> class 4: solar zenith >= 86 degrees
+ * 1010 -> class 5: solar zenith >= 85 and < 86 degrees
+ * 1011 -> class 6: missing input
+ * 1100 -> class 7: internal constant used in place of climatological data for at least one atmospheric constant
+ * 1101 -> class 8: correction out of bounds, pixel constrained to extreme allowable value
+ * 1110 -> class 9: L1B data faulty
+ * 1111 -> class 10: not processed due to deep ocean or cloud
+ * Class 11: Combination of bits unused
+ */  
+int qc500c(long int pixel, int bandno) 
+{
+    long int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 2 + (4 * bandno - 1);	/* bitshift [] to [0-3] etc. */
+    swab(&swabfrom, &swabto, 4);
+    qctemp = swabto;
+    if (qctemp & 0x08) {	/* 1??? */
+	if (qctemp & 0x04) {	/* 11?? */
+	    if (qctemp & 0x02) {	/* 111? */
+		if (qctemp & 0x01) {	/* 1111 */
+		    class = 10;	/*Not proc.ocean/cloud */
+		}
+		else {		/* 1110 */
+		    class = 9;	/*L1B faulty data */
+		}
+	    }
+	    else {		/* 110? */
+		if (qctemp & 0x01) {	/* 1101 */
+		    class = 8;	/*corr. out of bounds */
+		}
+		else {		/* 1100 */
+		    class = 7;	/*internal constant used */
+		}
+	    }
+	}
+	else {
+	    if (qctemp & 0x02) {	/* 101? */
+		if (qctemp & 0x01) {	/* 1011 */
+		    class = 6;	/*missing input */
+		}
+		else {		/* 1010 */
+		    class = 5;	/*solarzen>=85&&<86 */
+		}
+	    }
+	    else {		/* 100? */
+		if (qctemp & 0x01) {	/* 1001 */
+		    class = 4;	/*solar zenith angle>=86 */
+		}
+		else {		/* 1000 */
+		    class = 3;	/*Dead detector */
+		}
+	    }
+	}
+    }
+    else {			/* 0??? */
+	if (qctemp & 0x04) {	/* 01?? */
+	    if (qctemp & 0x02) {	/* 011? */
+		if (qctemp & 0x01) {	/* 0111 */
+		    class = 2;	/*Noisy detector */
+		}
+		else {		/* 0110 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	    else {		/* 010? */
+		if (qctemp & 0x01) {	/* 0101 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0100 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	}
+	else {			/* 00?? */
+	    if (qctemp & 0x02) {	/* 001? */
+		if (qctemp & 0x01) {	/* 0011 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0010 */
+		    class = 11;	/*Unused */
+		}
+	    }
+	    else {		/* 000? */
+		if (qctemp & 0x01) {	/* 0001 */
+		    class = 11;	/*Unused */
+		}
+		else {		/* 0000 */
+		    class = 1;	/*Highest quality */
+		}
+	    }
+	}
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc500d.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc500d.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc500d.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-/* Atmospheric correction 500m long Int bit[30]
- * 00 -> class 1: Not Corrected product
- * 01 -> class 2: Corrected product
- */
-
-int qc500d( long int pixel )
-{
-	long int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>30; /* bit no 30 becomes 0 */
-	swab(&swabfrom,&swabto,1);
-	qctemp=swabto;
-	if(qctemp & 0x01){
-		class=2;/*Corrected*/
-	} else {
-		class=1;/*Not corrected*/
-	}
-	return class;
-}
-
+/* Atmospheric correction 500m long Int bit[30]
+ * 00 -> class 1: Not Corrected product
+ * 01 -> class 2: Corrected product
+ */  
+int qc500d(long int pixel) 
+{
+    long int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 30;		/* bit no 30 becomes 0 */
+    swab(&swabfrom, &swabto, 1);
+    qctemp = swabto;
+    if (qctemp & 0x01) {
+	class = 2;		/*Corrected */
+    }
+    else {
+	class = 1;		/*Not corrected */
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.qc.modis/qc500e.c
===================================================================
--- grass-addons/gipe/i.qc.modis/qc500e.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.qc.modis/qc500e.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-/* Adjacency correction 500m long Int bit[31]
- * 00 -> class 1: Not Corrected product
- * 01 -> class 2: Corrected product
- */
-
-int qc500e( long int pixel )
-{
-	long int swabfrom, swabto, qctemp;
-	int class;
-		
-	swabfrom=pixel;
-	swabfrom>>31; /* bit no 31 becomes 0 */
-	swab(&swabfrom,&swabto,1);
-	qctemp=swabto;
-	if(qctemp & 0x01){
-		class=2;/*Corrected*/
-	} else {
-		class=1;/*Not corrected*/
-	}
-	return class;
-}
-
+/* Adjacency correction 500m long Int bit[31]
+ * 00 -> class 1: Not Corrected product
+ * 01 -> class 2: Corrected product
+ */  
+int qc500e(long int pixel) 
+{
+    long int swabfrom, swabto, qctemp;
+
+    int class;
+
+    swabfrom = pixel;
+    swabfrom >> 31;		/* bit no 31 becomes 0 */
+    swab(&swabfrom, &swabto, 1);
+    qctemp = swabto;
+    if (qctemp & 0x01) {
+	class = 2;		/*Corrected */
+    }
+    else {
+	class = 1;		/*Not corrected */
+    }
+    return class;
+}
+
+

Modified: grass-addons/gipe/i.sattime/main.c
===================================================================
--- grass-addons/gipe/i.sattime/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.sattime/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,246 +1,274 @@
-/****************************************************************************
- *
- * MODULE:       i.sattime
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the time of satellite overpass
- * 		 using sun elevation angle, latitude and DOY.
- *
- * COPYRIGHT:    (C) 2007-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#define PI 3.1415927
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_lat, infd_lon, infd_doy, infd_phi;
-	int outfd1;
-	
-	char *lat, *lon, *doy, *phi;
-	int i=0,j=0;
-
-	void *inrast_lat, *inrast_lon, *inrast_doy, *inrast_phi;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_lat;
-	RASTER_MAP_TYPE data_type_lon;
-	RASTER_MAP_TYPE data_type_doy;
-	RASTER_MAP_TYPE data_type_phi;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("satellite time, overpass");
-	module->description = _("creates a satellite overpass time map");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("doy");
-	input1->description=_("Name of the DOY input map");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("lat");
-	input2->description=_("Name of the latitude input map");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key	   = _("long");
-	input3->description=_("Name of the longitude input map");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key	   = _("sun_elev");
-	input4->description=_("Name of the sun elevation angle input map");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output satellite overpass time layer");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	doy	 	= input1->answer;
-	lat	 	= input2->answer;
-	lon	 	= input3->answer;
-	phi	 	= input4->answer;
-		
-	result1  	= output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(doy, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), doy);
-	}
-	data_type_doy = G_raster_map_type(doy,mapset);
-	if ( (infd_doy = G_open_cell_old (doy,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), doy);
-	if (G_get_cellhd (doy, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), doy);
-	inrast_doy = G_allocate_raster_buf(data_type_doy);
-	/***************************************************/
-	mapset = G_find_cell2(lat, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), lat);
-	}
-	data_type_lat = G_raster_map_type(lat,mapset);
-	if ( (infd_lat = G_open_cell_old (lat,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), lat);
-	if (G_get_cellhd (lat, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), lat);
-	inrast_lat = G_allocate_raster_buf(data_type_lat);
-	/***************************************************/
-	mapset = G_find_cell2(lon, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), lon);
-	}
-	data_type_lon = G_raster_map_type(lon,mapset);
-	if ( (infd_lon = G_open_cell_old (lon,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), lon);
-	if (G_get_cellhd (lon, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), lon);
-	inrast_lon = G_allocate_raster_buf(data_type_lon);
-	/***************************************************/
-	mapset = G_find_cell2(phi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), phi);
-	}
-	data_type_phi = G_raster_map_type(phi,mapset);
-	if ( (infd_phi = G_open_cell_old (phi,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), phi);
-	if (G_get_cellhd (phi, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), phi);
-	inrast_phi = G_allocate_raster_buf(data_type_phi);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_frac_year;
-		DCELL d_tc_wa;
-		DCELL d_delta;
-		DCELL d_Wa;
-		DCELL d_Wa1;
-		DCELL d_Wa2;
-		DCELL d_time;
-		DCELL d_lat;
-		DCELL d_lon;
-		DCELL d_doy;
-		DCELL d_phi;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd_doy,inrast_doy,row,data_type_doy)<0)
-			G_fatal_error(_("Could not read from <%s>"),doy);
-		if(G_get_raster_row(infd_lat,inrast_lat,row,data_type_lat)<0)
-			G_fatal_error(_("Could not read from <%s>"),lat);
-		if(G_get_raster_row(infd_lon,inrast_lon,row,data_type_lon)<0)
-			G_fatal_error(_("Could not read from <%s>"),lon);
-		if(G_get_raster_row(infd_phi,inrast_phi,row,data_type_phi)<0)
-			G_fatal_error(_("Could not read from <%s>"),phi);
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_doy){
-				case CELL_TYPE:
-					d_doy = (double) ((CELL *) inrast_doy)[col];
-					break;
-				case FCELL_TYPE:
-					d_doy = (double) ((FCELL *) inrast_doy)[col];
-					break;
-				case DCELL_TYPE:
-					d_doy	= ((DCELL *) inrast_doy)[col];
-					break;
-			}
-			switch(data_type_lat){
-				case CELL_TYPE:
-					d_lat = (double) ((CELL *) inrast_lat)[col];
-					break;
-				case FCELL_TYPE:
-					d_lat = (double) ((FCELL *) inrast_lat)[col];
-					break;
-				case DCELL_TYPE:
-					d_lat	= ((DCELL *) inrast_lat)[col];
-					break;
-			}
-			switch(data_type_lon){
-				case CELL_TYPE:
-					d_lon = (double) ((CELL *) inrast_lon)[col];
-					break;
-				case FCELL_TYPE:
-					d_lon = (double) ((FCELL *) inrast_lon)[col];
-					break;
-				case DCELL_TYPE:
-					d_lon	= ((DCELL *) inrast_lon)[col];
-					break;
-			}
-			switch(data_type_phi){
-				case CELL_TYPE:
-					d_phi = (double) ((CELL *) inrast_phi)[col];
-					break;
-				case FCELL_TYPE:
-					d_phi = (double) ((FCELL *) inrast_phi)[col];
-					break;
-				case DCELL_TYPE:
-					d_phi	= ((DCELL *) inrast_phi)[col];
-					break;
-			}
-			d_frac_year = (360.0/365.25) * d_doy;
-			d_delta = 0.396372-22.91327*cos(d_frac_year*PI/180.0)+4.02543*sin(d_frac_year*PI/180.0)-0.387205*cos(2*d_frac_year*PI/180.0)+0.051967*sin(2*d_frac_year*PI/180.0)-0.154527*cos(3*d_frac_year*PI/180.0)+0.084798*sin(3*d_frac_year*PI/180.0);
-			d_tc_wa = 0.004297+0.107029*cos(d_frac_year*PI/180.0)-1.837877*sin(d_frac_year*PI/180.0)-0.837378*cos(2*d_frac_year*PI/180.0)-2.340475*sin(2*d_frac_year*PI/180.0);
-			d_Wa1=cos((90.0-d_phi)*PI/180.0)-sin(d_delta*PI/180.0)*sin(d_lat*PI/180.0);
-			d_Wa2=cos(d_delta*PI/180.0)*cos(d_lat*PI/180.0);
-			d_Wa=acos(d_Wa1/d_Wa2);
-			d_time = ((d_Wa-(d_lon*PI/180.0)-d_tc_wa)/15.0)+12.0;
-			outrast1[col] = d_time;
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_lat);
-	G_free (inrast_lon);
-	G_free (inrast_doy);
-	G_free (inrast_phi);
-	G_close_cell (infd_lat);
-	G_close_cell (infd_lon);
-	G_close_cell (infd_doy);
-	G_close_cell (infd_phi);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.sattime
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the time of satellite overpass
+ * 		 using sun elevation angle, latitude and DOY.
+ *
+ * COPYRIGHT:    (C) 2007-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+    
+#define PI 3.1415927
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_lat, infd_lon, infd_doy, infd_phi;
+
+    int outfd1;
+
+    char *lat, *lon, *doy, *phi;
+
+    int i = 0, j = 0;
+
+    void *inrast_lat, *inrast_lon, *inrast_doy, *inrast_phi;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_lat;
+    RASTER_MAP_TYPE data_type_lon;
+    RASTER_MAP_TYPE data_type_doy;
+    RASTER_MAP_TYPE data_type_phi;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("satellite time, overpass");
+    module->description = _("creates a satellite overpass time map");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("doy");
+    input1->description = _("Name of the DOY input map");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("lat");
+    input2->description = _("Name of the latitude input map");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("long");
+    input3->description = _("Name of the longitude input map");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("sun_elev");
+    input4->description = _("Name of the sun elevation angle input map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description =
+	_("Name of the output satellite overpass time layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    doy = input1->answer;
+    lat = input2->answer;
+    lon = input3->answer;
+    phi = input4->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(doy, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), doy);
+    }
+    data_type_doy = G_raster_map_type(doy, mapset);
+    if ((infd_doy = G_open_cell_old(doy, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), doy);
+    if (G_get_cellhd(doy, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), doy);
+    inrast_doy = G_allocate_raster_buf(data_type_doy);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lat, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), lat);
+    }
+    data_type_lat = G_raster_map_type(lat, mapset);
+    if ((infd_lat = G_open_cell_old(lat, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lat);
+    if (G_get_cellhd(lat, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), lat);
+    inrast_lat = G_allocate_raster_buf(data_type_lat);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lon, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), lon);
+    }
+    data_type_lon = G_raster_map_type(lon, mapset);
+    if ((infd_lon = G_open_cell_old(lon, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lon);
+    if (G_get_cellhd(lon, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), lon);
+    inrast_lon = G_allocate_raster_buf(data_type_lon);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(phi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), phi);
+    }
+    data_type_phi = G_raster_map_type(phi, mapset);
+    if ((infd_phi = G_open_cell_old(phi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), phi);
+    if (G_get_cellhd(phi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), phi);
+    inrast_phi = G_allocate_raster_buf(data_type_phi);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_frac_year;
+	DCELL d_tc_wa;
+	DCELL d_delta;
+	DCELL d_Wa;
+	DCELL d_Wa1;
+	DCELL d_Wa2;
+	DCELL d_time;
+	DCELL d_lat;
+	DCELL d_lon;
+	DCELL d_doy;
+	DCELL d_phi;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row(infd_doy, inrast_doy, row, data_type_doy) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), doy);
+	if (G_get_raster_row(infd_lat, inrast_lat, row, data_type_lat) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), lat);
+	if (G_get_raster_row(infd_lon, inrast_lon, row, data_type_lon) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), lon);
+	if (G_get_raster_row(infd_phi, inrast_phi, row, data_type_phi) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), phi);
+	for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_doy) {
+	    case CELL_TYPE:
+		d_doy = (double)((CELL *) inrast_doy)[col];
+		break;
+	    case FCELL_TYPE:
+		d_doy = (double)((FCELL *) inrast_doy)[col];
+		break;
+	    case DCELL_TYPE:
+		d_doy = ((DCELL *) inrast_doy)[col];
+		break;
+	    }
+	    switch (data_type_lat) {
+	    case CELL_TYPE:
+		d_lat = (double)((CELL *) inrast_lat)[col];
+		break;
+	    case FCELL_TYPE:
+		d_lat = (double)((FCELL *) inrast_lat)[col];
+		break;
+	    case DCELL_TYPE:
+		d_lat = ((DCELL *) inrast_lat)[col];
+		break;
+	    }
+	    switch (data_type_lon) {
+	    case CELL_TYPE:
+		d_lon = (double)((CELL *) inrast_lon)[col];
+		break;
+	    case FCELL_TYPE:
+		d_lon = (double)((FCELL *) inrast_lon)[col];
+		break;
+	    case DCELL_TYPE:
+		d_lon = ((DCELL *) inrast_lon)[col];
+		break;
+	    }
+	    switch (data_type_phi) {
+	    case CELL_TYPE:
+		d_phi = (double)((CELL *) inrast_phi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_phi = (double)((FCELL *) inrast_phi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_phi = ((DCELL *) inrast_phi)[col];
+		break;
+	    }
+	    d_frac_year = (360.0 / 365.25) * d_doy;
+	    d_delta =
+		0.396372 - 22.91327 * cos(d_frac_year * PI / 180.0) +
+		4.02543 * sin(d_frac_year * PI / 180.0) -
+		0.387205 * cos(2 * d_frac_year * PI / 180.0) +
+		0.051967 * sin(2 * d_frac_year * PI / 180.0) -
+		0.154527 * cos(3 * d_frac_year * PI / 180.0) +
+		0.084798 * sin(3 * d_frac_year * PI / 180.0);
+	    d_tc_wa =
+		0.004297 + 0.107029 * cos(d_frac_year * PI / 180.0) -
+		1.837877 * sin(d_frac_year * PI / 180.0) -
+		0.837378 * cos(2 * d_frac_year * PI / 180.0) -
+		2.340475 * sin(2 * d_frac_year * PI / 180.0);
+	    d_Wa1 =
+		cos((90.0 - d_phi) * PI / 180.0) -
+		sin(d_delta * PI / 180.0) * sin(d_lat * PI / 180.0);
+	    d_Wa2 = cos(d_delta * PI / 180.0) * cos(d_lat * PI / 180.0);
+	    d_Wa = acos(d_Wa1 / d_Wa2);
+	    d_time = ((d_Wa - (d_lon * PI / 180.0) - d_tc_wa) / 15.0) + 12.0;
+	    outrast1[col] = d_time;
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_lat);
+    G_free(inrast_lon);
+    G_free(inrast_doy);
+    G_free(inrast_phi);
+    G_close_cell(infd_lat);
+    G_close_cell(infd_lon);
+    G_close_cell(infd_doy);
+    G_close_cell(infd_phi);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.sunhours/main.c
===================================================================
--- grass-addons/gipe/i.sunhours/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.sunhours/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,175 +1,192 @@
-/****************************************************************************
- *
- * MODULE:       i.sunhours
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates the sunshine hours (also called daytime period)
- * 		 under a perfect clear sky condition.
- * 		 Called generally "N" in meteorology. 
- *
- * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#define PI 3.1415927
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_lat, infd_doy;
-	int outfd1;
-	
-	char *lat, *doy;
-	int i=0,j=0;
-
-	void *inrast_lat, *inrast_doy;
-	DCELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_lat;
-	RASTER_MAP_TYPE data_type_doy;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("sunshine, hours, daytime");
-	module->description = _("creates a sunshine hours map");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("doy");
-	input1->description=_("Name of the latitude input map");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("lat");
-	input2->description=_("Name of the latitude input map");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output sunshine hours layer");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	lat	 	= input1->answer;
-	doy	 	= input2->answer;
-		
-	result1  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(doy, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), doy);
-	}
-	data_type_doy = G_raster_map_type(doy,mapset);
-	if ( (infd_doy = G_open_cell_old (doy,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), doy);
-	if (G_get_cellhd (doy, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), doy);
-	inrast_doy = G_allocate_raster_buf(data_type_doy);
-	/***************************************************/
-	mapset = G_find_cell2(lat, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), lat);
-	}
-	data_type_lat = G_raster_map_type(lat,mapset);
-	if ( (infd_lat = G_open_cell_old (lat,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), lat);
-	if (G_get_cellhd (lat, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), lat);
-	inrast_lat = G_allocate_raster_buf(data_type_lat);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	G_debug(3, "number of rows %d",cellhd.rows);
-
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_da;
-		DCELL d_delta;
-		DCELL d_Ws;
-		DCELL d_N;
-		DCELL d_lat;
-		DCELL d_doy;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd_doy,inrast_doy,row,data_type_doy)<0)
-			G_fatal_error(_("Could not read from <%s>"),doy);
-		if(G_get_raster_row(infd_lat,inrast_lat,row,data_type_lat)<0)
-			G_fatal_error(_("Could not read from <%s>"),lat);
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_doy){
-				case CELL_TYPE:
-					d_doy = (double) ((CELL *) inrast_doy)[col];
-					break;
-				case FCELL_TYPE:
-					d_doy = (double) ((FCELL *) inrast_doy)[col];
-					break;
-				case DCELL_TYPE:
-					d_doy	= ((DCELL *) inrast_doy)[col];
-					break;
-			}
-			switch(data_type_lat){
-				case CELL_TYPE:
-					d_lat = (double) ((CELL *) inrast_lat)[col];
-					break;
-				case FCELL_TYPE:
-					d_lat = (double) ((FCELL *) inrast_lat)[col];
-					break;
-				case DCELL_TYPE:
-					d_lat	= ((DCELL *) inrast_lat)[col];
-					break;
-			}
-			d_da = 2 * PI * ( d_doy - 1 ) / 365.0;
-			d_delta = 0.006918-0.399912*cos(d_da)+0.070257*sin(d_da)-0.006758*cos(2*d_da)+0.000907*sin(2*d_da)-0.002697*cos(3*d_da)+0.00148*sin(3*d_da);
-			d_Ws = acos(-tan(d_lat*PI/180)*tan(d_delta));
-			d_N = ( 360.0 / ( 15.0 * PI ) ) * d_Ws;
-			((DCELL *) outrast1)[col] = d_N;
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_lat);
-	G_free (inrast_doy);
-	G_close_cell (infd_lat);
-	G_close_cell (infd_doy);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.sunhours
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates the sunshine hours (also called daytime period)
+ * 		 under a perfect clear sky condition.
+ * 		 Called generally "N" in meteorology. 
+ *
+ * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+    
+#define PI 3.1415927
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_lat, infd_doy;
+
+    int outfd1;
+
+    char *lat, *doy;
+
+    int i = 0, j = 0;
+
+    void *inrast_lat, *inrast_doy;
+
+    DCELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_lat;
+    RASTER_MAP_TYPE data_type_doy;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("sunshine, hours, daytime");
+    module->description = _("creates a sunshine hours map");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("doy");
+    input1->description = _("Name of the latitude input map");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("lat");
+    input2->description = _("Name of the latitude input map");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output sunshine hours layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    lat = input1->answer;
+    doy = input2->answer;
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(doy, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), doy);
+    }
+    data_type_doy = G_raster_map_type(doy, mapset);
+    if ((infd_doy = G_open_cell_old(doy, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), doy);
+    if (G_get_cellhd(doy, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), doy);
+    inrast_doy = G_allocate_raster_buf(data_type_doy);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(lat, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), lat);
+    }
+    data_type_lat = G_raster_map_type(lat, mapset);
+    if ((infd_lat = G_open_cell_old(lat, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), lat);
+    if (G_get_cellhd(lat, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), lat);
+    inrast_lat = G_allocate_raster_buf(data_type_lat);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_da;
+	DCELL d_delta;
+	DCELL d_Ws;
+	DCELL d_N;
+	DCELL d_lat;
+	DCELL d_doy;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row(infd_doy, inrast_doy, row, data_type_doy) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), doy);
+	if (G_get_raster_row(infd_lat, inrast_lat, row, data_type_lat) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), lat);
+	for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_doy) {
+	    case CELL_TYPE:
+		d_doy = (double)((CELL *) inrast_doy)[col];
+		break;
+	    case FCELL_TYPE:
+		d_doy = (double)((FCELL *) inrast_doy)[col];
+		break;
+	    case DCELL_TYPE:
+		d_doy = ((DCELL *) inrast_doy)[col];
+		break;
+	    }
+	    switch (data_type_lat) {
+	    case CELL_TYPE:
+		d_lat = (double)((CELL *) inrast_lat)[col];
+		break;
+	    case FCELL_TYPE:
+		d_lat = (double)((FCELL *) inrast_lat)[col];
+		break;
+	    case DCELL_TYPE:
+		d_lat = ((DCELL *) inrast_lat)[col];
+		break;
+	    }
+	    d_da = 2 * PI * (d_doy - 1) / 365.0;
+	    d_delta =
+		0.006918 - 0.399912 * cos(d_da) + 0.070257 * sin(d_da) -
+		0.006758 * cos(2 * d_da) + 0.000907 * sin(2 * d_da) -
+		0.002697 * cos(3 * d_da) + 0.00148 * sin(3 * d_da);
+	    d_Ws = acos(-tan(d_lat * PI / 180) * tan(d_delta));
+	    d_N = (360.0 / (15.0 * PI)) * d_Ws;
+	    ((DCELL *) outrast1)[col] = d_N;
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_lat);
+    G_free(inrast_doy);
+    G_close_cell(infd_lat);
+    G_close_cell(infd_doy);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.vi/arvi.c
===================================================================
--- grass-addons/gipe/i.vi/arvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/arvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,26 +1,26 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-/*  ARVI is resistant to atmospheric effects (in comparison to the NDVI) and 
-is accomplished by a self correcting process for the atmospheric effect in the
- red channel, using the difference in the radiance between the blue and the red channels. 
-(Kaufman and Tanre 1996)*/
-
-/* Atmospheric Resistant Vegetation Index */
-
-
-double ar_vi( double redchan, double nirchan, double bluechan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - (2*redchan - bluechan)) / ( nirchan + (2*redchan - bluechan));
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ 
+    /*  ARVI is resistant to atmospheric effects (in comparison to the NDVI) and 
+       is accomplished by a self correcting process for the atmospheric effect in the
+       red channel, using the difference in the radiance between the blue and the red channels. 
+       (Kaufman and Tanre 1996) */ 
+    
+    /* Atmospheric Resistant Vegetation Index */ 
+double ar_vi(double redchan, double nirchan, double bluechan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    (nirchan - (2 * redchan - bluechan)) / (nirchan +
+						    (2 * redchan - bluechan));
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/dvi.c
===================================================================
--- grass-addons/gipe/i.vi/dvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/dvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,19 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* DVI: Difference Vegetation Index */
-
-double d_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - redchan ) ;
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* DVI: Difference Vegetation Index */ 
+double d_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan - redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/evi.c
===================================================================
--- grass-addons/gipe/i.vi/evi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/evi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,25 +1,24 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* EVI: Enhanced Vegetation Index
- * Huete A.R., Liu H.Q., Batchily K., vanLeeuwen W. (1997)
- * A comparison of vegetation indices global set of TM images for EOS-MODIS
- * Remote Sensing of Environment, 59:440-451.
- */
-
-double e_vi( double bluechan, double redchan, double nirchan )
-{
-	double tmp, result;
-	
-	tmp = nirchan + 6.0*redchan - 7.5*bluechan + 1.0 ;
-
-	if( tmp ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = 2.5 * ( nirchan - redchan ) / tmp ;
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* EVI: Enhanced Vegetation Index
+     * Huete A.R., Liu H.Q., Batchily K., vanLeeuwen W. (1997)
+     * A comparison of vegetation indices global set of TM images for EOS-MODIS
+     * Remote Sensing of Environment, 59:440-451.
+     */ 
+double e_vi(double bluechan, double redchan, double nirchan) 
+{
+    double tmp, result;
+
+    tmp = nirchan + 6.0 * redchan - 7.5 * bluechan + 1.0;
+    if (tmp == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = 2.5 * (nirchan - redchan) / tmp;
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/gari.c
===================================================================
--- grass-addons/gipe/i.vi/gari.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/gari.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,17 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*GARI: green atmospherically resistant vegetation index*/
-
-double ga_ri( double redchan, double nirchan, double bluechan, double greenchan )
-{
-	double result;
-	
-	 {
-		result = ( nirchan - (greenchan-(bluechan - redchan))) / ( nirchan - (greenchan-(bluechan - redchan))) ;
-	 }
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*GARI: green atmospherically resistant vegetation index */ 
+double ga_ri(double redchan, double nirchan, double bluechan,
+	      double greenchan) 
+{
+    double result;
+
+     {
+	result =
+	    (nirchan - (greenchan - (bluechan - redchan))) / (nirchan -
+							      (greenchan -
+							       (bluechan -
+								redchan)));
+    } return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/gemi.c
===================================================================
--- grass-addons/gipe/i.vi/gemi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/gemi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,36 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* GEMI: Global Environmental Monitoring Index
-*/
-
-
-double ge_mi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = (( (2*((nirchan * nirchan)-(redchan * redchan))+1.5*nirchan+0.5*redchan) /(nirchan + redchan + 0.5)) * (1 - 0.25 * (2*((nirchan * nirchan)-(redchan * redchan))+1.5*nirchan+0.5*redchan) /(nirchan + redchan + 0.5))) -( (redchan - 0.125) / (1 - redchan)) ;
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* GEMI: Global Environmental Monitoring Index
+     */ 
+double ge_mi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    (((2 * ((nirchan * nirchan) - (redchan * redchan)) +
+	       1.5 * nirchan + 0.5 * redchan) / (nirchan + redchan +
+						 0.5)) * (1 -
+							  0.25 * (2 *
+								  ((nirchan *
+								    nirchan) -
+								   (redchan *
+								    redchan))
+								  +
+								  1.5 *
+								  nirchan +
+								  0.5 *
+								  redchan) /
+							  (nirchan + redchan +
+							   0.5))) -
+	    ((redchan - 0.125) / (1 - redchan));
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/gvi.c
===================================================================
--- grass-addons/gipe/i.vi/gvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/gvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,24 +1,23 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Green Vegetation Index*/
-
-
-double g_vi( double bluechan, double greenchan, double redchan, double nirchan, double chan5chan, double chan7chan)
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else
-
-	{
-		result = (-0.2848*bluechan-0.2435*greenchan-0.5436*redchan+0.7243*nirchan+0.0840*chan5chan- 0.1800*chan7chan);
-
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Green Vegetation Index */ 
+double g_vi(double bluechan, double greenchan, double redchan,
+	      double nirchan, double chan5chan, double chan7chan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else
+	 {
+	result =
+	    (-0.2848 * bluechan - 0.2435 * greenchan - 0.5436 * redchan +
+	     0.7243 * nirchan + 0.0840 * chan5chan - 0.1800 * chan7chan);
+	}
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/ipvi.c
===================================================================
--- grass-addons/gipe/i.vi/ipvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/ipvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,25 +1,25 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*
-IPVI: Infrared Percentage Vegetation Index
-
-			    NIR
-		  IPVI = --------
-			  NIR+red
-*/
-
-double ip_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan ) / ( nirchan + redchan );
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*
+       IPVI: Infrared Percentage Vegetation Index
+       
+       NIR
+       IPVI = --------
+       NIR+red
+     */ 
+double ip_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan) / (nirchan + redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/main.c
===================================================================
--- grass-addons/gipe/i.vi/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.vi
@@ -21,443 +22,885 @@
  *
  * Changelog:	 Added EVI on 20080718 (Yann)
  *
- *****************************************************************************/
-
+ *****************************************************************************/
+
+ 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
+
+double s_r(double redchan, double nirchan);
+
+
+double nd_vi(double redchan, double nirchan);
+
+
+double ip_vi(double redchan, double nirchan);
+
+
+double d_vi(double redchan, double nirchan);
+
+
+double e_vi(double bluechan, double redchan, double nirchan);
+
+
+double p_vi(double redchan, double nirchan);
+
+
+double wd_vi(double redchan, double nirchan);
+
+
+double sa_vi(double redchan, double nirchan);
+
+
+double msa_vi(double redchan, double nirchan);
+
+
+double msa_vi2(double redchan, double nirchan);
+
+
+double ge_mi(double redchan, double nirchan);
+
+
+double ar_vi(double redchan, double nirchan, double bluechan);
+
+
+double g_vi(double bluechan, double greenchan, double redchan,
+	     double nirchan, double chan5chan, double chan7chan);
+
+double ga_ri(double redchan, double nirchan, double bluechan,
+	      double greenchan);
+
+
+int main(int argc, char *argv[]) 
+{
+    
+struct Cell_head cellhd;	/*region+header info */
+
+    
+char *mapset;		/*mapset name */
+
+    
+int nrows, ncols;
+
+    
+int row, col;
+
+    
+
+char *viflag;		/*Switch for particular index */
+
+    
+
+struct GModule *module;
+
+    
+struct Option *input1, *input2, *input3, *input4, *input5, *input6,
+	*input7, *output;
+    
+
+struct Flag *flag1;
+
+    
+struct History history;	/*metadata */
+
+    
+struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    
+char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_redchan, infd_nirchan, infd_greenchan;
+
+    
+int infd_bluechan, infd_chan5chan, infd_chan7chan;
+
+    
+int outfd;
+
+    
+
+char *bluechan, *greenchan, *redchan, *nirchan, *chan5chan, *chan7chan;
+
+    
+
+int i = 0, j = 0;
+
+    
+
+void *inrast_redchan, *inrast_nirchan, *inrast_greenchan;
+
+    
+void *inrast_bluechan, *inrast_chan5chan, *inrast_chan7chan;
+
+    
+DCELL * outrast;
+    
+RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    
+RASTER_MAP_TYPE data_type_redchan;
+    
+RASTER_MAP_TYPE data_type_nirchan;
+    
+RASTER_MAP_TYPE data_type_greenchan;
+    
+RASTER_MAP_TYPE data_type_bluechan;
+    
+RASTER_MAP_TYPE data_type_chan5chan;
+    
+RASTER_MAP_TYPE data_type_chan7chan;
+    
 
-double s_r( double redchan, double nirchan );
-double nd_vi( double redchan, double nirchan );
-double ip_vi( double redchan, double nirchan );
-double d_vi( double redchan, double nirchan );
-double e_vi( double bluechan, double redchan, double nirchan );
-double p_vi( double redchan, double nirchan );
-double wd_vi( double redchan, double nirchan );
-double sa_vi( double redchan, double nirchan );
-double msa_vi( double redchan, double nirchan );
-double msa_vi2( double redchan, double nirchan );
-double ge_mi( double redchan, double nirchan );
-double ar_vi( double redchan, double nirchan, double bluechan );
-double g_vi( double bluechan, double greenchan, double redchan, double nirchan, double chan5chan, double chan7chan);
-double ga_ri( double redchan, double nirchan, double bluechan, double greenchan );
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    
+
+module = G_define_module();
+    
+module->keywords = _("vegetation index, biophysical parameters");
+    
+module->description =
+	_("14 types of vegetation indices from red and nir, and only some requiring additional bands");
+    
 
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
+	/* Define the different options */ 
+	input1 = G_define_option();
+    
+input1->key = _("viname");
+    
+input1->type = TYPE_STRING;
+    
+input1->required = YES;
+    
+input1->gisprompt = _("Name of VI");
+    
+input1->description =
+	_("Name of VI: sr,ndvi,ipvi,dvi,evi,pvi,wdvi,savi,msavi,msavi2,gemi,arvi,gvi,gari.");
+    
+input1->answer = _("ndvi");
+    
+
+input2 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input2->key = _("red");
+    
+input2->description =
+	_("Name of the RED Channel surface reflectance map [0.0;1.0]");
+    
+input2->answer = _("redchan");
+    
+
+input3 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input3->key = _("nir");
+    
+input3->description =
+	_("Name of the NIR Channel surface reflectance map [0.0;1.0]");
+    
+input3->answer = _("nirchan");
+    
+
+input4 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input4->key = _("green");
+    
+input4->required = NO;
+    
+input4->description =
+	_("Name of the GREEN Channel surface reflectance map [0.0;1.0]");
+    
+
+input5 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input5->key = _("blue");
+    
+input5->required = NO;
+    
+input5->description =
+	_("Name of the BLUE Channel surface reflectance map [0.0;1.0]");
+    
+
+input6 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input6->key = _("chan5");
+    
+input6->required = NO;
+    
+input6->description =
+	_("Name of the CHAN5 Channel surface reflectance map [0.0;1.0]");
+    
+
+input7 = G_define_standard_option(G_OPT_R_INPUT);
+    
+input7->key = _("chan7");
+    
+input7->required = NO;
+    
+input7->description =
+	_("Name of the CHAN7 Channel surface reflectance map [0.0;1.0]");
+    
+
+output = G_define_standard_option(G_OPT_R_OUTPUT);
+    
+output->key = _("vi");
+    
+output->required = YES;
+    
+output->description = _("Name of the output vi layer");
+    
+output->answer = _("vi");
+    
 
-	char *viflag;/*Switch for particular index*/
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	
+exit(EXIT_FAILURE);
+    
+viflag = input1->answer;
+    
+redchan = input2->answer;
+    
+nirchan = input3->answer;
+    
+greenchan = input4->answer;
+    
+bluechan = input5->answer;
+    
+chan5chan = input6->answer;
+    
+chan7chan = input7->answer;
+    
+
+result = output->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(redchan, "");
+    
+if (mapset == NULL) {
+	
+G_fatal_error(_("cell file [%s] not found"), redchan);
+    
+}
+    
+data_type_redchan = G_raster_map_type(redchan, mapset);
+    
+if ((infd_redchan = G_open_cell_old(redchan, mapset)) < 0)
+	
+G_fatal_error(_("Cannot open cell file [%s]"), redchan);
+    
+if (G_get_cellhd(redchan, mapset, &cellhd) < 0)
+	
+G_fatal_error(_("Cannot read file header of [%s])"), redchan);
+    
+inrast_redchan = G_allocate_raster_buf(data_type_redchan);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(nirchan, "");
+    
+if (mapset == NULL) {
+	
+G_fatal_error(_("cell file [%s] not found"), nirchan);
+    
+}
+    
+data_type_nirchan = G_raster_map_type(nirchan, mapset);
+    
+if ((infd_nirchan = G_open_cell_old(nirchan, mapset)) < 0)
+	
+G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
+    
+if (G_get_cellhd(nirchan, mapset, &cellhd) < 0)
+	
+G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
+    
+inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
+    
+
+	/***************************************************/ 
+	if (greenchan) {
+	
+mapset = G_find_cell2(greenchan, "");
+	
+if (mapset == NULL) {
+	    
+G_fatal_error(_("cell file [%s] not found"), greenchan);
+	
+}
+	
+data_type_greenchan = G_raster_map_type(greenchan, mapset);
+	
+if ((infd_greenchan = G_open_cell_old(greenchan, mapset)) < 0)
+	    
+G_fatal_error(_("Cannot open cell file [%s]"), greenchan);
+	
+if (G_get_cellhd(greenchan, mapset, &cellhd) < 0)
+	    
+G_fatal_error(_("Cannot read file header of [%s])"), greenchan);
+	
+inrast_greenchan = G_allocate_raster_buf(data_type_greenchan);
+    
+}
+    
+
+	/***************************************************/ 
+	if (bluechan) {
+	
+mapset = G_find_cell2(bluechan, "");
+	
+if (mapset == NULL) {
+	    
+G_fatal_error(_("cell file [%s] not found"), bluechan);
+	
+}
+	
+data_type_bluechan = G_raster_map_type(bluechan, mapset);
+	
+if ((infd_bluechan = G_open_cell_old(bluechan, mapset)) < 0)
+	    
+G_fatal_error(_("Cannot open cell file [%s]"), bluechan);
+	
+if (G_get_cellhd(bluechan, mapset, &cellhd) < 0)
+	    
+G_fatal_error(_("Cannot read file header of [%s])"), bluechan);
+	
+inrast_bluechan = G_allocate_raster_buf(data_type_bluechan);
+    
+}
+    
+
+	/***************************************************/ 
+	if (chan5chan) {
+	
+mapset = G_find_cell2(chan5chan, "");
+	
+if (mapset == NULL) {
+	    
+G_fatal_error(_("cell file [%s] not found"), chan5chan);
+	
+}
+	
+data_type_chan5chan = G_raster_map_type(chan5chan, mapset);
+	
+if ((infd_chan5chan = G_open_cell_old(chan5chan, mapset)) < 0)
+	    
+G_fatal_error(_("Cannot open cell file [%s]"), chan5chan);
+	
+if (G_get_cellhd(chan5chan, mapset, &cellhd) < 0)
+	    
+G_fatal_error(_("Cannot read file header of [%s])"), chan5chan);
+	
+inrast_chan5chan = G_allocate_raster_buf(data_type_chan5chan);
+    
+}
+    
+
+	/***************************************************/ 
+	if (chan7chan) {
+	
+mapset = G_find_cell2(chan7chan, "");
+	
+if (mapset == NULL) {
+	    
+G_fatal_error(_("cell file [%s] not found"), chan7chan);
+	
+}
+	
+data_type_chan7chan = G_raster_map_type(chan7chan, mapset);
+	
+if ((infd_chan7chan = G_open_cell_old(chan7chan, mapset)) < 0)
+	    
+G_fatal_error(_("Cannot open cell file [%s]"), chan7chan);
+	
+if (G_get_cellhd(chan7chan, mapset, &cellhd) < 0)
+	    
+G_fatal_error(_("Cannot read file header of [%s])"), chan7chan);
+	
+inrast_chan7chan = G_allocate_raster_buf(data_type_chan7chan);
+    
+}
+    
+
+	/***************************************************/ 
+	
+G_debug(3, "number of rows %d", cellhd.rows);
+    
+nrows = G_window_rows();
+    
+ncols = G_window_cols();
+    
+outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	
+G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	
+ {
+	
+DCELL d;
+	
+DCELL d_bluechan;
+	
+DCELL d_greenchan;
+	
+DCELL d_redchan;
+	
+DCELL d_nirchan;
+	
+DCELL d_chan5chan;
+	
+DCELL d_chan7chan;
+	
+G_percent(row, nrows, 2);
+	
+if (G_get_raster_row
+	     (infd_redchan, inrast_redchan, row, data_type_redchan) < 0)
+	    
+G_fatal_error(_("Could not read from <%s>"), redchan);
+	
+if (G_get_raster_row
+	     (infd_nirchan, inrast_nirchan, row, data_type_nirchan) < 0)
+	    
+G_fatal_error(_("Could not read from <%s>"), nirchan);
+	
+if (greenchan) {
+	    
+if (G_get_raster_row
+		 (infd_greenchan, inrast_greenchan, row,
+		  data_type_greenchan) < 0)
+		
+G_fatal_error(_("Could not read from <%s>"), greenchan);
+	
+}
+	
+if (bluechan) {
+	    
+if (G_get_raster_row
+		 (infd_bluechan, inrast_bluechan, row,
+		  data_type_bluechan) < 0)
+		
+G_fatal_error(_("Could not read from <%s>"), bluechan);
+	
+}
+	
+if (chan5chan) {
+	    
+if (G_get_raster_row
+		 (infd_chan5chan, inrast_chan5chan, row,
+		  data_type_chan5chan) < 0)
+		
+G_fatal_error(_("Could not read from <%s>"), chan5chan);
+	
+}
+	
+if (chan7chan) {
+	    
+if (G_get_raster_row
+		 (infd_chan7chan, inrast_chan7chan, row,
+		  data_type_chan7chan) < 0)
+		
+G_fatal_error(_("Could not read from <%s>"), chan7chan);
+	
+}
 	
-	struct GModule *module;
-	struct Option *input1, *input2,*input3,*input4,*input5,*input6,*input7, *output;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_redchan, infd_nirchan, infd_greenchan;
-	int infd_bluechan, infd_chan5chan, infd_chan7chan;
-	int outfd;
-	
-	char  *bluechan, *greenchan,*redchan, *nirchan, *chan5chan, *chan7chan;
-	
-	int i=0,j=0;
-	
-	void *inrast_redchan, *inrast_nirchan, *inrast_greenchan;
-	void *inrast_bluechan, *inrast_chan5chan, *inrast_chan7chan;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_redchan;
-	RASTER_MAP_TYPE data_type_nirchan;
-	RASTER_MAP_TYPE data_type_greenchan;
-	RASTER_MAP_TYPE data_type_bluechan;
-	RASTER_MAP_TYPE data_type_chan5chan;
-	RASTER_MAP_TYPE data_type_chan7chan;
-	
-	/************************************/
-	G_gisinit(argv[0]);
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	    
+ {
+	    
+switch (data_type_redchan) {
+	    
+case CELL_TYPE:
+		
+d_redchan = (double)((CELL *) inrast_redchan)[col];
+		
+break;
+	    
+case FCELL_TYPE:
+		
+d_redchan = (double)((FCELL *) inrast_redchan)[col];
+		
+break;
+	    
+case DCELL_TYPE:
+		
+d_redchan = ((DCELL *) inrast_redchan)[col];
+		
+break;
+	    
+}
+	    
+switch (data_type_nirchan) {
+	    
+case CELL_TYPE:
+		
+d_nirchan = (double)((CELL *) inrast_nirchan)[col];
+		
+break;
+	    
+case FCELL_TYPE:
+		
+d_nirchan = (double)((FCELL *) inrast_nirchan)[col];
+		
+break;
+	    
+case DCELL_TYPE:
+		
+d_nirchan = ((DCELL *) inrast_nirchan)[col];
+		
+break;
+	    
+}
+	    
+if (greenchan) {
+		
+switch (data_type_greenchan) {
+		
+case CELL_TYPE:
+		    
+d_greenchan = (double)((CELL *) inrast_greenchan)[col];
+		    
+break;
+		
+case FCELL_TYPE:
+		    
+d_greenchan = (double)((FCELL *) inrast_greenchan)[col];
+		    
+break;
+		
+case DCELL_TYPE:
+		    
+d_greenchan = ((DCELL *) inrast_greenchan)[col];
+		    
+break;
+		
+}
+	    
+}
+	    
+if (bluechan) {
+		
+switch (data_type_bluechan) {
+		
+case CELL_TYPE:
+		    
+d_bluechan = (double)((CELL *) inrast_bluechan)[col];
+		    
+break;
+		
+case FCELL_TYPE:
+		    
+d_bluechan = (double)((FCELL *) inrast_bluechan)[col];
+		    
+break;
+		
+case DCELL_TYPE:
+		    
+d_bluechan = ((DCELL *) inrast_bluechan)[col];
+		    
+break;
+		
+}
+	    
+}
+	    
+if (chan5chan) {
+		
+switch (data_type_chan5chan) {
+		
+case CELL_TYPE:
+		    
+d_chan5chan = (double)((CELL *) inrast_chan5chan)[col];
+		    
+break;
+		
+case FCELL_TYPE:
+		    
+d_chan5chan = (double)((FCELL *) inrast_chan5chan)[col];
+		    
+break;
+		
+case DCELL_TYPE:
+		    
+d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
+		    
+break;
+		
+}
+	    
+}
+	    
+if (chan7chan) {
+		
+switch (data_type_chan7chan) {
+		
+case CELL_TYPE:
+		    
+d_chan7chan = (double)((CELL *) inrast_chan7chan)[col];
+		    
+break;
+		
+case FCELL_TYPE:
+		    
+d_chan7chan = (double)((FCELL *) inrast_chan7chan)[col];
+		    
+break;
+		
+case DCELL_TYPE:
+		    
+d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
+		    
+break;
+		
+}
+	    
+}
+	    
+if (G_is_d_null_value(&d_redchan) ||
+		 
+G_is_d_null_value(&d_nirchan) || 
+((greenchan) &&
+						     G_is_d_null_value
+						     (&d_greenchan)) ||
+		 
+((bluechan) && G_is_d_null_value(&d_bluechan)) ||
+		 
+((chan5chan) && G_is_d_null_value(&d_chan5chan)) ||
+		 
+((chan7chan) && G_is_d_null_value(&d_chan7chan))) {
+		
+G_set_d_null_value(&outrast[col], 1);
+	    
+}
+	    else {
+		
+
+				/************************************/ 
+		    /*calculate simple_ratio        */ 
+		    if (!strcoll(viflag, "sr")) {
+		    
+d = s_r(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate ndvi                    */ 
+		    if (!strcoll(viflag, "ndvi")) {
+		    
+if (d_redchan + d_nirchan < 0.001) {
+			
+G_set_d_null_value(&outrast[col], 1);
+		    
+}
+		    else {
+			
+d = nd_vi(d_redchan, d_nirchan);
+			
+((DCELL *) outrast)[col] = d;
+		    
+}
+		
+}
+		
+		    /*calculate ipvi                    */ 
+		    if (!strcoll(viflag, "ipvi")) {
+		    
+d = ip_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate dvi             */ 
+		    if (!strcoll(viflag, "dvi")) {
+		    
+d = d_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate evi             */ 
+		    if (!strcoll(viflag, "evi")) {
+		    
+d = e_vi(d_bluechan, d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate pvi             */ 
+		    if (!strcoll(viflag, "pvi")) {
+		    
+d = p_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate wdvi                    */ 
+		    if (!strcoll(viflag, "wdvi")) {
+		    
+d = wd_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate savi                    */ 
+		    if (!strcoll(viflag, "savi")) {
+		    
+d = sa_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate msavi                   */ 
+		    if (!strcoll(viflag, "msavi")) {
+		    
+d = msa_vi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate msavi2            */ 
+		    if (!strcoll(viflag, "msavi2")) {
+		    
+d = msa_vi2(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate gemi                    */ 
+		    if (!strcoll(viflag, "gemi")) {
+		    
+d = ge_mi(d_redchan, d_nirchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate arvi                    */ 
+		    if (!strcoll(viflag, "arvi")) {
+		    
+d = ar_vi(d_redchan, d_nirchan, d_bluechan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate gvi            */ 
+		    if (!strcoll(viflag, "gvi")) {
+		    
+d = g_vi(d_bluechan, d_greenchan, d_redchan, d_nirchan,
+			      d_chan5chan, d_chan7chan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+		
+		    /*calculate gari                    */ 
+		    if (!strcoll(viflag, "gari")) {
+		    
+d = ga_ri(d_redchan, d_nirchan, d_bluechan, d_greenchan);
+		    
+((DCELL *) outrast)[col] = d;
+		
+}
+	    
+}
+	    
+}
+	
+if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    
+G_fatal_error(_("Cannot write to output raster file"));
+	
+}
+    
+
+
+G_free(inrast_redchan);
+    
+G_close_cell(infd_redchan);
+    
+G_free(inrast_nirchan);
+    
+G_close_cell(infd_nirchan);
+    
+if (greenchan) {
+	
+G_free(inrast_greenchan);
+	
+G_close_cell(infd_greenchan);
+    
+}
+    
+if (bluechan) {
+	
+G_free(inrast_bluechan);
+	
+G_close_cell(infd_bluechan);
+    
+}
+    
+if (chan5chan) {
+	
+G_free(inrast_chan5chan);
+	
+G_close_cell(infd_chan5chan);
+    
+}
+    
+if (chan7chan) {
+	
+G_free(inrast_chan7chan);
+	
+G_close_cell(infd_chan7chan);
+    
+}
+    
+G_free(outrast);
+    
+G_close_cell(outfd);
+    
 
-	module = G_define_module();
-	module->keywords = _("vegetation index, biophysical parameters");
-	module->description = _("14 types of vegetation indices from red and nir, and only some requiring additional bands");
+	/* Color from -1.0 to +1.0 in grey */ 
+	G_init_colors(&colors);
+    
+G_add_color_rule(-1.0, 0, 0, 0, 1.0, 255, 255, 255, &colors);
+    
+G_short_history(result, "raster", &history);
+    
+G_command_history(&history);
+    
+G_write_history(result, &history);
+    
+
+exit(EXIT_SUCCESS);
+
+}
+
+
 
-	/* Define the different options */
-	input1 = G_define_option() ;
-	input1->key        =_("viname");
-	input1->type       = TYPE_STRING;
-	input1->required   = YES;
-	input1->gisprompt  =_("Name of VI");
-	input1->description=_("Name of VI: sr,ndvi,ipvi,dvi,evi,pvi,wdvi,savi,msavi,msavi2,gemi,arvi,gvi,gari.");
-	input1->answer     =_("ndvi");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("red");
-	input2->description=_("Name of the RED Channel surface reflectance map [0.0;1.0]");
-	input2->answer     =_("redchan");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("nir");
-	input3->description=_("Name of the NIR Channel surface reflectance map [0.0;1.0]");
-	input3->answer     =_("nirchan");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("green");
-	input4->required   = NO;
-	input4->description=_("Name of the GREEN Channel surface reflectance map [0.0;1.0]");
-
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key        =_("blue");
-	input5->required   = NO;
-	input5->description=_("Name of the BLUE Channel surface reflectance map [0.0;1.0]");
-
-	input6 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input6->key        =_("chan5");
-	input6->required   = NO;
-	input6->description=_("Name of the CHAN5 Channel surface reflectance map [0.0;1.0]");
-
-	input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input7->key        =_("chan7");
-	input7->required   = NO;
-	input7->description=_("Name of the CHAN7 Channel surface reflectance map [0.0;1.0]");
-
-	output= G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key        =_("vi");
-	output->required   = YES;
-	output->description=_("Name of the output vi layer");
-	output->answer     =_("vi");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-	viflag		= input1->answer;
-	redchan	 	= input2->answer;
-	nirchan	 	= input3->answer;
-	greenchan	= input4->answer;
-	bluechan	= input5->answer;
-	chan5chan	= input6->answer;
-	chan7chan	= input7->answer;
-
-	result  = output->answer;
-	/***************************************************/
-	mapset = G_find_cell2(redchan, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), redchan);
-	}
-	data_type_redchan = G_raster_map_type(redchan,mapset);
-	if ( (infd_redchan = G_open_cell_old (redchan,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), redchan);
-	if (G_get_cellhd (redchan, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), redchan);
-	inrast_redchan = G_allocate_raster_buf(data_type_redchan);
-	/***************************************************/
-	mapset = G_find_cell2 (nirchan, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),nirchan);
-	}
-	data_type_nirchan = G_raster_map_type(nirchan,mapset);
-	if ( (infd_nirchan = G_open_cell_old (nirchan,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
-	if (G_get_cellhd (nirchan, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
-	inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
-	/***************************************************/
-	if(greenchan){
-		mapset = G_find_cell2(greenchan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"), greenchan);
-		}
-		data_type_greenchan = G_raster_map_type(greenchan,mapset);
-		if ( (infd_greenchan = G_open_cell_old (greenchan,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), greenchan);
-		if (G_get_cellhd (greenchan, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), greenchan);
-		inrast_greenchan = G_allocate_raster_buf(data_type_greenchan);
-	}
-	/***************************************************/
-	if(bluechan){
-		mapset = G_find_cell2(bluechan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"), bluechan);
-		}
-		data_type_bluechan = G_raster_map_type(bluechan,mapset);
-		if ( (infd_bluechan = G_open_cell_old (bluechan,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), bluechan);
-		if (G_get_cellhd (bluechan, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), bluechan);
-		inrast_bluechan = G_allocate_raster_buf(data_type_bluechan);
-	}
-	/***************************************************/
-	if(chan5chan){
-		mapset = G_find_cell2(chan5chan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"), chan5chan);
-		}
-		data_type_chan5chan = G_raster_map_type(chan5chan,mapset);
-		if ( (infd_chan5chan = G_open_cell_old (chan5chan,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), chan5chan);
-		if (G_get_cellhd (chan5chan, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), chan5chan);
-		inrast_chan5chan = G_allocate_raster_buf(data_type_chan5chan);
-	}
-	/***************************************************/
-	if(chan7chan){
-		mapset = G_find_cell2(chan7chan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"), chan7chan);
-		}
-		data_type_chan7chan = G_raster_map_type(chan7chan,mapset);
-		if ( (infd_chan7chan = G_open_cell_old (chan7chan,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), chan7chan);
-		if (G_get_cellhd (chan7chan, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), chan7chan);
-		inrast_chan7chan = G_allocate_raster_buf(data_type_chan7chan);
-	}
-	/***************************************************/
-
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_bluechan;
-		DCELL d_greenchan;
-		DCELL d_redchan;
-		DCELL d_nirchan;
-		DCELL d_chan5chan;
-		DCELL d_chan7chan;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd_redchan,inrast_redchan,row,data_type_redchan)<0)
-			G_fatal_error(_("Could not read from <%s>"),redchan);
-		if(G_get_raster_row(infd_nirchan,inrast_nirchan,row,data_type_nirchan)<0)
-			G_fatal_error(_("Could not read from <%s>"),nirchan);
-		if(greenchan){
-			if(G_get_raster_row(infd_greenchan,inrast_greenchan,row,data_type_greenchan)<0)
-				G_fatal_error(_("Could not read from <%s>"),greenchan);
-		}
-		if(bluechan){
-			if(G_get_raster_row(infd_bluechan,inrast_bluechan,row,data_type_bluechan)<0)
-				G_fatal_error(_("Could not read from <%s>"),bluechan);
-		}
-		if(chan5chan){
-			if(G_get_raster_row(infd_chan5chan,inrast_chan5chan,row,data_type_chan5chan)<0)
-				G_fatal_error(_("Could not read from <%s>"),chan5chan);
-		}
-		if(chan7chan){
-			if(G_get_raster_row(infd_chan7chan,inrast_chan7chan,row,data_type_chan7chan)<0)
-				G_fatal_error(_("Could not read from <%s>"),chan7chan);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_redchan){
-				case CELL_TYPE:
-					d_redchan = (double) ((CELL *) inrast_redchan)[col];
-					break;
-				case FCELL_TYPE:
-					d_redchan = (double) ((FCELL *) inrast_redchan)[col];
-					break;
-				case DCELL_TYPE:
-					d_redchan = ((DCELL *) inrast_redchan)[col];
-					break;
-			}
-			switch(data_type_nirchan){
-				case CELL_TYPE:
-					d_nirchan = (double) ((CELL *) inrast_nirchan)[col];
-					break;
-				case FCELL_TYPE:
-					d_nirchan = (double) ((FCELL *) inrast_nirchan)[col];
-					break;
-				case DCELL_TYPE:
-					d_nirchan = ((DCELL *) inrast_nirchan)[col];
-					break;
-			}
-			if (greenchan){
-				switch(data_type_greenchan){
-					case CELL_TYPE:
-						d_greenchan = (double) ((CELL *) inrast_greenchan)[col];
-						break;
-					case FCELL_TYPE:
-						d_greenchan = (double) ((FCELL *) inrast_greenchan)[col];
-						break;
-					case DCELL_TYPE:
-						d_greenchan = ((DCELL *) inrast_greenchan)[col];
-						break;
-				}
-			}
-			if (bluechan){
-				switch(data_type_bluechan){
-					case CELL_TYPE:
-						d_bluechan = (double) ((CELL *) inrast_bluechan)[col];
-						break;
-					case FCELL_TYPE:
-						d_bluechan = (double) ((FCELL *) inrast_bluechan)[col];
-						break;
-					case DCELL_TYPE:
-						d_bluechan = ((DCELL *) inrast_bluechan)[col];
-						break;
-				}
-			}
-			if (chan5chan){
-				switch(data_type_chan5chan){
-					case CELL_TYPE:
-						d_chan5chan = (double) ((CELL *) inrast_chan5chan)[col];
-						break;
-					case FCELL_TYPE:
-						d_chan5chan = (double) ((FCELL *) inrast_chan5chan)[col];
-						break;
-					case DCELL_TYPE:
-						d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
-						break;
-				}
-			}
-			if (chan7chan){
-				switch(data_type_chan7chan){
-					case CELL_TYPE:
-						d_chan7chan = (double) ((CELL *) inrast_chan7chan)[col];
-						break;
-					case FCELL_TYPE:
-						d_chan7chan = (double) ((FCELL *) inrast_chan7chan)[col];
-						break;
-					case DCELL_TYPE:
-						d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
-						break;
-				}
-			}
-			if(G_is_d_null_value(&d_redchan)||
-			G_is_d_null_value(&d_nirchan)||
-			((greenchan)&&G_is_d_null_value(&d_greenchan))||
-			((bluechan)&&G_is_d_null_value(&d_bluechan))||
-			((chan5chan)&&G_is_d_null_value(&d_chan5chan))||
-			((chan7chan)&&G_is_d_null_value(&d_chan7chan))){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				/************************************/
-				/*calculate simple_ratio        */
-				if (!strcoll(viflag,"sr")){		
-					d =  s_r(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate ndvi	            */
-				if (!strcoll(viflag,"ndvi")){
-					if(d_redchan+d_nirchan<0.001){
-						G_set_d_null_value(&outrast[col],1);
-					} else {
-						d =  nd_vi(d_redchan,d_nirchan );
-						((DCELL *) outrast)[col] = d;
-					}
-				}
-				/*calculate ipvi	            */
-				if (!strcoll(viflag,"ipvi")){
-					d =  ip_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate dvi	            */
-				if (!strcoll(viflag,"dvi")){
-					d =  d_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate evi	            */
-				if (!strcoll(viflag,"evi")){
-					d =  e_vi(d_bluechan,d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate pvi	            */
-				if (!strcoll(viflag,"pvi")){
-					d =  p_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate wdvi	            */
-				if (!strcoll(viflag,"wdvi")){
-					d =  wd_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate savi	            */
-				if (!strcoll(viflag,"savi")){
-					d =  sa_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate msavi	            */
-				if (!strcoll(viflag,"msavi")){
-					d =  msa_vi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate msavi2            */
-				if (!strcoll(viflag,"msavi2")){
-					d =  msa_vi2(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate gemi	            */
-				if (!strcoll(viflag,"gemi")){
-					d =  ge_mi(d_redchan,d_nirchan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate arvi	            */
-				if (!strcoll(viflag,"arvi")){
-					d =  ar_vi(d_redchan,d_nirchan,d_bluechan );
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate gvi            */
-				if (!strcoll(viflag,"gvi")){
-					d =  g_vi(d_bluechan,d_greenchan,d_redchan,d_nirchan,d_chan5chan,d_chan7chan);
-					((DCELL *) outrast)[col] = d;
-				}
-				/*calculate gari	            */
-				if (!strcoll(viflag,"gari")){
-					d =  ga_ri(d_redchan,d_nirchan,d_bluechan,d_greenchan );
-					((DCELL *) outrast)[col] = d;
-				}
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	
-	G_free(inrast_redchan);
-	G_close_cell(infd_redchan);
-	G_free(inrast_nirchan);
-	G_close_cell(infd_nirchan);
-	if(greenchan){
-		G_free(inrast_greenchan);	
-		G_close_cell(infd_greenchan);	
-	}
-	if(bluechan){
-		G_free(inrast_bluechan);	
-		G_close_cell(infd_bluechan);	
-	}
-	if(chan5chan){
-		G_free(inrast_chan5chan);	
-		G_close_cell(infd_chan5chan);	
-	}
-	if(chan7chan){
-		G_free(inrast_chan7chan);	
-		G_close_cell(infd_chan7chan);	
-	}
-	G_free(outrast);
-	G_close_cell(outfd);
-
-	/* Color from -1.0 to +1.0 in grey */
-	G_init_colors(&colors);
-	G_add_color_rule(-1.0,0,0,0,1.0,255,255,255,&colors);
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-

Modified: grass-addons/gipe/i.vi/msavi.c
===================================================================
--- grass-addons/gipe/i.vi/msavi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/msavi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,29 +1,31 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* MSAVI: Modified Soil Adjusted Vegetation Index	
- *							
- *			 s(NIR-s*red-a)			
- *		  MSAVI = ---------------------------	
- *			  (a*NIR+red-a*s+X*(1+s*s))	
- *	where a is the soil line intercept, s is the 	
- *	soil line slope, and X 	is an adjustment factor 
- *	which is set to minimize soil noise (0.08 in 	
- *	original papers).				
- */
-
-double msa_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result =(1/2)*(2 * (nirchan+1)-sqrt((2*nirchan+1)*(2*nirchan+1))-(8 *(nirchan-redchan))) ;
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* MSAVI: Modified Soil Adjusted Vegetation Index       
+     *                                                      
+     *                       s(NIR-s*red-a)                 
+     *                MSAVI = ---------------------------   
+     *                        (a*NIR+red-a*s+X*(1+s*s))     
+     *      where a is the soil line intercept, s is the    
+     *      soil line slope, and X  is an adjustment factor 
+     *      which is set to minimize soil noise (0.08 in    
+     *      original papers).                               
+     */ 
+double msa_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    (1 / 2) * (2 * (nirchan + 1) -
+		       sqrt((2 * nirchan + 1) * (2 * nirchan + 1)) -
+		       (8 * (nirchan - redchan)));
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/msavi2.c
===================================================================
--- grass-addons/gipe/i.vi/msavi2.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/msavi2.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,24 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* MSAVI2: second Modified Soil Adjusted Vegetation Index
- *	MSAVI2 = (1/2)*(2(NIR+1)-sqrt((2*NIR+1)^2-8(NIR-red)))
- */
-
-double msa_vi2( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result =(1/2)*(2 * (nirchan+1)-sqrt((2*nirchan+1)*(2*nirchan+1))-(8 *(nirchan-redchan))) ;
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* MSAVI2: second Modified Soil Adjusted Vegetation Index
+     *      MSAVI2 = (1/2)*(2(NIR+1)-sqrt((2*NIR+1)^2-8(NIR-red)))
+     */ 
+double msa_vi2(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    (1 / 2) * (2 * (nirchan + 1) -
+		       sqrt((2 * nirchan + 1) * (2 * nirchan + 1)) -
+		       (8 * (nirchan - redchan)));
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/ndvi.c
===================================================================
--- grass-addons/gipe/i.vi/ndvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/ndvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,21 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Normalized Difference Vegetation Index*/
-
-
-double nd_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - redchan ) / ( nirchan + redchan );
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Normalized Difference Vegetation Index */ 
+double nd_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan - redchan) / (nirchan + redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/pvi.c
===================================================================
--- grass-addons/gipe/i.vi/pvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/pvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,27 +1,25 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-/*
-PVI: Perpendicular Vegetation Index
-
-		PVI = sin(a)NIR-cos(a)red
-for a  isovegetation lines (lines of equal vegetation) would all be parallel to the soil line therefore a=1
-
-*/
-
-
-double p_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = (sin(1) * nirchan ) / ( cos(1) * redchan ); 
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ 
+    /*
+       PVI: Perpendicular Vegetation Index
+       
+       PVI = sin(a)NIR-cos(a)red
+       for a  isovegetation lines (lines of equal vegetation) would all be parallel to the soil line therefore a=1
+       
+     */ 
+double p_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (sin(1) * nirchan) / (cos(1) * redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/savi.c
===================================================================
--- grass-addons/gipe/i.vi/savi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/savi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,20 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Soil Adjusted Vegetation Index*/
-
-double sa_vi( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ((1+0.5)*( nirchan - redchan )) / ( nirchan + redchan +0.5);
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Soil Adjusted Vegetation Index */ 
+double sa_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result =
+	    ((1 + 0.5) * (nirchan - redchan)) / (nirchan + redchan + 0.5);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/sr.c
===================================================================
--- grass-addons/gipe/i.vi/sr.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/sr.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,19 +1,19 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Simple Vegetation ratio*/
-
-double s_r( double redchan, double nirchan )
-{
-	double result;
-	
-	if( ( redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result =(nirchan/redchan);
-	}
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Simple Vegetation ratio */ 
+double s_r(double redchan, double nirchan) 
+{
+    double result;
+
+    if ((redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan / redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi/wdvi.c
===================================================================
--- grass-addons/gipe/i.vi/wdvi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi/wdvi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,22 +1,21 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Weighted Difference Vegetation Index*/
-
-
-double wd_vi( double redchan, double nirchan )
-{
-	double result;
-	double a=1;/*slope of soil line */
-
-	if( ( nirchan + redchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - a*redchan );
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Weighted Difference Vegetation Index */ 
+double wd_vi(double redchan, double nirchan) 
+{
+    double result;
+
+    double a = 1;		/*slope of soil line */
+
+    if ((nirchan + redchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan - a * redchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.vi.mpi/main.c
===================================================================
--- grass-addons/gipe/i.vi.mpi/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.vi.mpi/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:     i.vi.mpi
@@ -33,868 +34,1021 @@
 
 int main(int argc, char *argv[])
 {
-	int me,host_n,nrows,ncols;
-	int NUM_HOSTS;
-	MPI_Status status;
-	MPI_Init(&argc,&argv);
-	MPI_Comm_size(MPI_COMM_WORLD,&NUM_HOSTS);
-	MPI_Comm_rank(MPI_COMM_WORLD,&me);
- 
-	if(!me)
-	{
-		struct Cell_head cellhd; /*region+header info*/
-		char *mapset; /*mapset name*/
-		int row,col,row_n;
-		char *viflag;/*Switch for particular index*/
-		struct GModule *module;
-		struct Option *input1, *input2,*input3,*input4,*input5,*input6,*input7,*input8, *output;
-	
-		struct Flag *flag1;	
-		struct History history; /*metadata*/
+    int me, host_n, nrows, ncols;
 
+    int NUM_HOSTS;
+
+    MPI_Status status;
+
+    MPI_Init(&argc, &argv);
+    MPI_Comm_size(MPI_COMM_WORLD, &NUM_HOSTS);
+    MPI_Comm_rank(MPI_COMM_WORLD, &me);
+
+    if (!me) {
+	struct Cell_head cellhd;	/*region+header info */
+
+	char *mapset;		/*mapset name */
+
+	int row, col, row_n;
+
+	char *viflag;		/*Switch for particular index */
+
+	struct GModule *module;
+
+	struct Option *input1, *input2, *input3, *input4, *input5, *input6,
+	    *input7, *input8, *output;
+
+	struct Flag *flag1;
+
+	struct History history;	/*metadata */
+
 		/************************************/
-		/* FMEO Declarations*****************/
-		char *name;   /*input raster name*/
-		char *result; /*output raster name*/
-		/*File Descriptors*/
-		int infd_redchan, infd_nirchan, infd_greenchan, infd_bluechan, infd_chan5chan, infd_chan7chan;
-		int outfd;
-	
-		char  *bluechan, *greenchan,*redchan, *nirchan, *chan5chan, *chan7chan;
-	
-		int i=0,j=0,temp;
-	
-		void *inrast_redchan, *inrast_nirchan, *inrast_greenchan, *inrast_bluechan, *inrast_chan5chan, *inrast_chan7chan;
-		DCELL *outrast;
-		
-		RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-		RASTER_MAP_TYPE data_type_redchan;
-		RASTER_MAP_TYPE data_type_nirchan;
-		RASTER_MAP_TYPE data_type_greenchan;
-		RASTER_MAP_TYPE data_type_bluechan;
-		RASTER_MAP_TYPE data_type_chan5chan;
-		RASTER_MAP_TYPE data_type_chan7chan;
-	
+	/* FMEO Declarations**************** */
+	char *name;		/*input raster name */
+
+	char *result;		/*output raster name */
+
+	/*File Descriptors */
+	int infd_redchan, infd_nirchan, infd_greenchan, infd_bluechan,
+	    infd_chan5chan, infd_chan7chan;
+	int outfd;
+
+	char *bluechan, *greenchan, *redchan, *nirchan, *chan5chan,
+	    *chan7chan;
+
+	int i = 0, j = 0, temp;
+
+	void *inrast_redchan, *inrast_nirchan, *inrast_greenchan,
+	    *inrast_bluechan, *inrast_chan5chan, *inrast_chan7chan;
+	DCELL *outrast;
+
+	RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
+	RASTER_MAP_TYPE data_type_redchan;
+
+	RASTER_MAP_TYPE data_type_nirchan;
+
+	RASTER_MAP_TYPE data_type_greenchan;
+
+	RASTER_MAP_TYPE data_type_bluechan;
+
+	RASTER_MAP_TYPE data_type_chan5chan;
+
+	RASTER_MAP_TYPE data_type_chan7chan;
+
 		/************************************/
-		G_gisinit(argv[0]);
+	G_gisinit(argv[0]);
 
-		module = G_define_module();
-		module->keywords = _("vegetation index, biophysical parameters");
-		module->description = _("13 types of vegetation indices from red and nir, and only some requiring additional bands");
+	module = G_define_module();
+	module->keywords = _("vegetation index, biophysical parameters");
+	module->description =
+	    _("13 types of vegetation indices from red and nir, and only some requiring additional bands");
 
-		/* Define the different options */
-		input1 = G_define_option() ;
-		input1->key        =_("viname");
-		input1->type       = TYPE_STRING;
-		input1->required   = YES;
-		input1->gisprompt  =_("Name of VI");
-		input1->description=_("Name of VI: sr,ndvi,ipvi,dvi,pvi,wdvi,savi,msavi,msavi2,gemi,arvi,gvi,gari.");
-		input1->answer     =_("ndvi");
+	/* Define the different options */
+	input1 = G_define_option();
+	input1->key = _("viname");
+	input1->type = TYPE_STRING;
+	input1->required = YES;
+	input1->gisprompt = _("Name of VI");
+	input1->description =
+	    _("Name of VI: sr,ndvi,ipvi,dvi,pvi,wdvi,savi,msavi,msavi2,gemi,arvi,gvi,gari.");
+	input1->answer = _("ndvi");
 
-		input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input2->key	   = _("red");
-		input2->description=_("Name of the RED Channel surface reflectance map [0.0;1.0]");
+	input2 = G_define_standard_option(G_OPT_R_INPUT);
+	input2->key = _("red");
+	input2->description =
+	    _("Name of the RED Channel surface reflectance map [0.0;1.0]");
 
-		input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input3->key        =_("nir");
-		input3->description=_("Name of the NIR Channel surface reflectance map [0.0;1.0]");
+	input3 = G_define_standard_option(G_OPT_R_INPUT);
+	input3->key = _("nir");
+	input3->description =
+	    _("Name of the NIR Channel surface reflectance map [0.0;1.0]");
 
-		input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input4->key        =_("green");
-		input4->required   = NO;
-		input4->description=_("Name of the GREEN Channel surface reflectance map [0.0;1.0]");
-		
-		input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input5->key        =_("blue");
-		input5->required   = NO;
-		input5->description=_("Name of the BLUE Channel surface reflectance map [0.0;1.0]");
+	input4 = G_define_standard_option(G_OPT_R_INPUT);
+	input4->key = _("green");
+	input4->required = NO;
+	input4->description =
+	    _("Name of the GREEN Channel surface reflectance map [0.0;1.0]");
 
-		input6 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input6->key        =_("chan5");
-		input6->required   = NO;
-		input6->description=_("Name of the CHAN5 Channel surface reflectance map [0.0;1.0]");
+	input5 = G_define_standard_option(G_OPT_R_INPUT);
+	input5->key = _("blue");
+	input5->required = NO;
+	input5->description =
+	    _("Name of the BLUE Channel surface reflectance map [0.0;1.0]");
 
-		input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-		input7->key        =_("chan7");
-		input7->required   = NO;
-		input7->description=_("Name of the CHAN7 Channel surface reflectance map [0.0;1.0]");
+	input6 = G_define_standard_option(G_OPT_R_INPUT);
+	input6->key = _("chan5");
+	input6->required = NO;
+	input6->description =
+	    _("Name of the CHAN5 Channel surface reflectance map [0.0;1.0]");
 
-		input8 = G_define_option() ;
-                input8->key        =_("tmp");
-                input8->type       = TYPE_INTEGER;
-                input8->required   = NO;
-                input8->gisprompt  =_("no of operation value");
-                input8->description=_("User input for number of operation");
+	input7 = G_define_standard_option(G_OPT_R_INPUT);
+	input7->key = _("chan7");
+	input7->required = NO;
+	input7->description =
+	    _("Name of the CHAN7 Channel surface reflectance map [0.0;1.0]");
 
-		output= G_define_standard_option(G_OPT_R_OUTPUT) ;
-		output->description=_("Name of the output vi layer");
+	input8 = G_define_option();
+	input8->key = _("tmp");
+	input8->type = TYPE_INTEGER;
+	input8->required = NO;
+	input8->gisprompt = _("no of operation value");
+	input8->description = _("User input for number of operation");
 
+	output = G_define_standard_option(G_OPT_R_OUTPUT);
+	output->description = _("Name of the output vi layer");
+
 		/********************/
-		if (G_parser(argc, argv))
-			exit (EXIT_FAILURE);
-		viflag		= input1->answer;
-		redchan	 = input2->answer;
-		nirchan	 = input3->answer;
-		greenchan	= input4->answer;
-		bluechan	= input5->answer;
-		chan5chan	= input6->answer;
-		chan7chan	= input7->answer;
-		temp            = atoi(input8->answer);
+	if (G_parser(argc, argv))
+	    exit(EXIT_FAILURE);
+	viflag = input1->answer;
+	redchan = input2->answer;
+	nirchan = input3->answer;
+	greenchan = input4->answer;
+	bluechan = input5->answer;
+	chan5chan = input6->answer;
+	chan7chan = input7->answer;
+	temp = atoi(input8->answer);
 
-		result  = output->answer;
+	result = output->answer;
+
 	/***************************************************/
-		mapset = G_find_cell2(redchan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"), redchan);
-		}
-		data_type_redchan = G_raster_map_type(redchan,mapset);
-		if ( (infd_redchan = G_open_cell_old (redchan,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), redchan);
-		if (G_get_cellhd (redchan, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), redchan);
-		inrast_redchan = G_allocate_raster_buf(data_type_redchan);
+	mapset = G_find_cell2(redchan, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), redchan);
+	}
+	data_type_redchan = G_raster_map_type(redchan, mapset);
+	if ((infd_redchan = G_open_cell_old(redchan, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), redchan);
+	if (G_get_cellhd(redchan, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s])"), redchan);
+	inrast_redchan = G_allocate_raster_buf(data_type_redchan);
+
 	/***************************************************/
-		mapset = G_find_cell2 (nirchan, "");
-		if (mapset == NULL) {
-			G_fatal_error(_("cell file [%s] not found"),nirchan);
-		}
-		data_type_nirchan = G_raster_map_type(nirchan,mapset);
-		if ( (infd_nirchan = G_open_cell_old (nirchan,mapset)) < 0)
-			G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
-		if (G_get_cellhd (nirchan, mapset, &cellhd) < 0)
-			G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
-		inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
+	mapset = G_find_cell2(nirchan, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), nirchan);
+	}
+	data_type_nirchan = G_raster_map_type(nirchan, mapset);
+	if ((infd_nirchan = G_open_cell_old(nirchan, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
+	if (G_get_cellhd(nirchan, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
+	inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
+
 	/***************************************************/
-		if(greenchan){
-			mapset = G_find_cell2(greenchan, "");
-			if (mapset == NULL) {
-				G_fatal_error(_("cell file [%s] not found"), greenchan);
-			}
-			data_type_greenchan = G_raster_map_type(greenchan,mapset);
-			if ( (infd_greenchan = G_open_cell_old (greenchan,mapset)) < 0)
-				G_fatal_error (_("Cannot open cell file [%s]"), greenchan);
-			if (G_get_cellhd (greenchan, mapset, &cellhd) < 0)
-				G_fatal_error (_("Cannot read file header of [%s])"), greenchan);
-			inrast_greenchan = G_allocate_raster_buf(data_type_greenchan);
-		}
+	if (greenchan) {
+	    mapset = G_find_cell2(greenchan, "");
+	    if (mapset == NULL) {
+		G_fatal_error(_("cell file [%s] not found"), greenchan);
+	    }
+	    data_type_greenchan = G_raster_map_type(greenchan, mapset);
+	    if ((infd_greenchan = G_open_cell_old(greenchan, mapset)) < 0)
+		G_fatal_error(_("Cannot open cell file [%s]"), greenchan);
+	    if (G_get_cellhd(greenchan, mapset, &cellhd) < 0)
+		G_fatal_error(_("Cannot read file header of [%s])"),
+			      greenchan);
+	    inrast_greenchan = G_allocate_raster_buf(data_type_greenchan);
+	}
+
 	/***************************************************/
-		if(bluechan)
-		{
-			mapset = G_find_cell2(bluechan, "");
-			if (mapset == NULL) {
-				G_fatal_error(_("cell file [%s] not found"), bluechan);
-			}
-			data_type_bluechan = G_raster_map_type(bluechan,mapset);
-			if ( (infd_bluechan = G_open_cell_old (bluechan,mapset)) < 0)
-				G_fatal_error (_("Cannot open cell file [%s]"), bluechan);
-			if (G_get_cellhd (bluechan, mapset, &cellhd) < 0)
-				G_fatal_error (_("Cannot read file header of [%s])"), bluechan);
-			inrast_bluechan = G_allocate_raster_buf(data_type_bluechan);
-		}
+	if (bluechan) {
+	    mapset = G_find_cell2(bluechan, "");
+	    if (mapset == NULL) {
+		G_fatal_error(_("cell file [%s] not found"), bluechan);
+	    }
+	    data_type_bluechan = G_raster_map_type(bluechan, mapset);
+	    if ((infd_bluechan = G_open_cell_old(bluechan, mapset)) < 0)
+		G_fatal_error(_("Cannot open cell file [%s]"), bluechan);
+	    if (G_get_cellhd(bluechan, mapset, &cellhd) < 0)
+		G_fatal_error(_("Cannot read file header of [%s])"),
+			      bluechan);
+	    inrast_bluechan = G_allocate_raster_buf(data_type_bluechan);
+	}
+
 	/***************************************************/
-		if(chan5chan)
-		{
-			mapset = G_find_cell2(chan5chan, "");
-			if (mapset == NULL) {
-				G_fatal_error(_("cell file [%s] not found"), chan5chan);
-			}
-			data_type_chan5chan = G_raster_map_type(chan5chan,mapset);
-			if ( (infd_chan5chan = G_open_cell_old (chan5chan,mapset)) < 0)
-				G_fatal_error (_("Cannot open cell file [%s]"), chan5chan);
-			if (G_get_cellhd (chan5chan, mapset, &cellhd) < 0)
-				G_fatal_error (_("Cannot read file header of [%s])"), chan5chan);
-			inrast_chan5chan = G_allocate_raster_buf(data_type_chan5chan);
-		}
+	if (chan5chan) {
+	    mapset = G_find_cell2(chan5chan, "");
+	    if (mapset == NULL) {
+		G_fatal_error(_("cell file [%s] not found"), chan5chan);
+	    }
+	    data_type_chan5chan = G_raster_map_type(chan5chan, mapset);
+	    if ((infd_chan5chan = G_open_cell_old(chan5chan, mapset)) < 0)
+		G_fatal_error(_("Cannot open cell file [%s]"), chan5chan);
+	    if (G_get_cellhd(chan5chan, mapset, &cellhd) < 0)
+		G_fatal_error(_("Cannot read file header of [%s])"),
+			      chan5chan);
+	    inrast_chan5chan = G_allocate_raster_buf(data_type_chan5chan);
+	}
+
 	/***************************************************/
-		if(chan7chan)
-		{
-			mapset = G_find_cell2(chan7chan, "");
-			if (mapset == NULL) {
-				G_fatal_error(_("cell file [%s] not found"), chan7chan);
-			}
-			data_type_chan7chan = G_raster_map_type(chan7chan,mapset);
-			if ( (infd_chan7chan = G_open_cell_old (chan7chan,mapset)) < 0)
-				G_fatal_error (_("Cannot open cell file [%s]"), chan7chan);
-			if (G_get_cellhd (chan7chan, mapset, &cellhd) < 0)
-				G_fatal_error (_("Cannot read file header of [%s])"), chan7chan);
-			inrast_chan7chan = G_allocate_raster_buf(data_type_chan7chan);
-		}
+	if (chan7chan) {
+	    mapset = G_find_cell2(chan7chan, "");
+	    if (mapset == NULL) {
+		G_fatal_error(_("cell file [%s] not found"), chan7chan);
+	    }
+	    data_type_chan7chan = G_raster_map_type(chan7chan, mapset);
+	    if ((infd_chan7chan = G_open_cell_old(chan7chan, mapset)) < 0)
+		G_fatal_error(_("Cannot open cell file [%s]"), chan7chan);
+	    if (G_get_cellhd(chan7chan, mapset, &cellhd) < 0)
+		G_fatal_error(_("Cannot read file header of [%s])"),
+			      chan7chan);
+	    inrast_chan7chan = G_allocate_raster_buf(data_type_chan7chan);
+	}
+
 	/***************************************************/
-		
-		G_debug(3, "number of rows %d",cellhd.rows);
-		nrows = G_window_rows();
-		ncols = G_window_cols();
-	
-		outrast = G_allocate_raster_buf(data_type_output);
+
+	G_debug(3, "number of rows %d", cellhd.rows);
+	nrows = G_window_rows();
+	ncols = G_window_cols();
+
+	outrast = G_allocate_raster_buf(data_type_output);
 	/* Create New raster files */
-		if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-			G_fatal_error(_("Could not open <%s>"),result);
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	    G_fatal_error(_("Could not open <%s>"), result);
 
-		double db[6][ncols],R[ncols+1],outputImage[NUM_HOSTS][ncols];
-		int I[ncols+1];
-		host_n=1;
-		
-		G_message("tmp=%d", temp);
-		for(i=1;i<NUM_HOSTS;i++){
-			
- 			MPI_Send(&temp,1,MPI_INT,i,1,MPI_COMM_WORLD);
-			MPI_Send(&nrows,1,MPI_INT,i,1,MPI_COMM_WORLD);
-                	MPI_Send(&ncols,1,MPI_INT,i,1,MPI_COMM_WORLD);
+	double db[6][ncols], R[ncols + 1], outputImage[NUM_HOSTS][ncols];
+
+	int I[ncols + 1];
+
+	host_n = 1;
+
+	G_message("tmp=%d", temp);
+	for (i = 1; i < NUM_HOSTS; i++) {
+
+	    MPI_Send(&temp, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
+	    MPI_Send(&nrows, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
+	    MPI_Send(&ncols, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
+	}
+	/* Process pixels */
+	int k, r, nh, cn;
+
+	for (r = 1; r * (NUM_HOSTS - 1) <= nrows; r++) {
+
+	    for (k = 1; k < NUM_HOSTS; k++) {
+
+
+		row = (r - 1) * (NUM_HOSTS - 1) + k - 1;
+
+		DCELL d_bluechan;
+
+		DCELL d_greenchan;
+
+		DCELL d_redchan;
+
+		DCELL d_nirchan;
+
+		DCELL d_chan5chan;
+
+		DCELL d_chan7chan;
+
+		G_percent(row, nrows, 2);
+		/* read input maps */
+		if (G_get_raster_row
+		    (infd_redchan, inrast_redchan, row,
+		     data_type_redchan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), redchan);
+		if (G_get_raster_row
+		    (infd_nirchan, inrast_nirchan, row,
+		     data_type_nirchan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), nirchan);
+		if (greenchan) {
+		    if (G_get_raster_row
+			(infd_greenchan, inrast_greenchan, row,
+			 data_type_greenchan) < 0)
+			G_fatal_error(_("Could not read from <%s>"),
+				      greenchan);
 		}
-		/* Process pixels */
-		int k,r,nh,cn;
-	
-		for (r = 1; r*(NUM_HOSTS-1) <= nrows;r++ )
-		{
-			
-			for(k=1;k<NUM_HOSTS;k++)
-			{
+		if (bluechan) {
+		    if (G_get_raster_row
+			(infd_bluechan, inrast_bluechan, row,
+			 data_type_bluechan) < 0)
+			G_fatal_error(_("Could not read from <%s>"),
+				      bluechan);
+		}
+		if (chan5chan) {
+		    if (G_get_raster_row
+			(infd_chan5chan, inrast_chan5chan, row,
+			 data_type_chan5chan) < 0)
+			G_fatal_error(_("Could not read from <%s>"),
+				      chan5chan);
+		}
+		if (chan7chan) {
+		    if (G_get_raster_row
+			(infd_chan7chan, inrast_chan7chan, row,
+			 data_type_chan7chan) < 0)
+			G_fatal_error(_("Could not read from <%s>"),
+				      chan7chan);
+		}
 
-				
-				row=(r-1)*(NUM_HOSTS-1)+k-1;
+		/*process the data */
+		for (col = 0; col < ncols; col++) {
+		    switch (data_type_redchan) {
+		    case CELL_TYPE:
+			d_redchan = (double)((CELL *) inrast_redchan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_redchan = (double)((FCELL *) inrast_redchan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_redchan = ((DCELL *) inrast_redchan)[col];
+			break;
+		    }
+		    switch (data_type_nirchan) {
+		    case CELL_TYPE:
+			d_nirchan = (double)((CELL *) inrast_nirchan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_nirchan = (double)((FCELL *) inrast_nirchan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_nirchan = ((DCELL *) inrast_nirchan)[col];
+			break;
+		    }
+		    if (greenchan) {
+			switch (data_type_greenchan) {
+			case CELL_TYPE:
+			    d_greenchan =
+				(double)((CELL *) inrast_greenchan)[col];
+			    break;
+			case FCELL_TYPE:
+			    d_greenchan =
+				(double)((FCELL *) inrast_greenchan)[col];
+			    break;
+			case DCELL_TYPE:
+			    d_greenchan = ((DCELL *) inrast_greenchan)[col];
+			    break;
+			}
+		    }
+		    if (bluechan) {
+			switch (data_type_bluechan) {
+			case CELL_TYPE:
+			    d_bluechan =
+				(double)((CELL *) inrast_bluechan)[col];
+			    break;
+			case FCELL_TYPE:
+			    d_bluechan =
+				(double)((FCELL *) inrast_bluechan)[col];
+			    break;
+			case DCELL_TYPE:
+			    d_bluechan = ((DCELL *) inrast_bluechan)[col];
+			    break;
+			}
+		    }
+		    if (chan5chan) {
+			switch (data_type_chan5chan) {
+			case CELL_TYPE:
+			    d_chan5chan =
+				(double)((CELL *) inrast_chan5chan)[col];
+			    break;
+			case FCELL_TYPE:
+			    d_chan5chan =
+				(double)((FCELL *) inrast_chan5chan)[col];
+			    break;
+			case DCELL_TYPE:
+			    d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
+			    break;
+			}
+		    }
+		    if (chan7chan) {
+			switch (data_type_chan7chan) {
+			case CELL_TYPE:
+			    d_chan7chan =
+				(double)((CELL *) inrast_chan7chan)[col];
+			    break;
+			case FCELL_TYPE:
+			    d_chan7chan =
+				(double)((FCELL *) inrast_chan7chan)[col];
+			    break;
+			case DCELL_TYPE:
+			    d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
+			    break;
+			}
+		    }
 
-				DCELL d_bluechan;
-				DCELL d_greenchan;
-				DCELL d_redchan;
-				DCELL d_nirchan;
-				DCELL d_chan5chan;
-				DCELL d_chan7chan;
-				G_percent(row,nrows,2);
-				/* read input maps */	
-				if(G_get_raster_row(infd_redchan,inrast_redchan,row,data_type_redchan)<0)
-					G_fatal_error(_("Could not read from <%s>"),redchan);
-				if(G_get_raster_row(infd_nirchan,inrast_nirchan,row,data_type_nirchan)<0)
-					G_fatal_error(_("Could not read from <%s>"),nirchan);
-				if(greenchan){
-					if(G_get_raster_row(infd_greenchan,inrast_greenchan,row,data_type_greenchan)<0)
-						G_fatal_error(_("Could not read from <%s>"),greenchan);
-				}
-				if(bluechan){
-					if(G_get_raster_row(infd_bluechan,inrast_bluechan,row,data_type_bluechan)<0)
-						G_fatal_error(_("Could not read from <%s>"),bluechan);
-				}
-				if(chan5chan){
-					if(G_get_raster_row(infd_chan5chan,inrast_chan5chan,row,data_type_chan5chan)<0)
-						G_fatal_error(_("Could not read from <%s>"),chan5chan);
-				}
-				if(chan7chan){
-					if(G_get_raster_row(infd_chan7chan,inrast_chan7chan,row,data_type_chan7chan)<0)
-						G_fatal_error(_("Could not read from <%s>"),chan7chan);
-				}
-				
-				/*process the data */
-				for (col=0; col < ncols; col++)
-				{
-					switch(data_type_redchan){
-						case CELL_TYPE:
-							d_redchan = (double) ((CELL *) inrast_redchan)[col];
-						break;
-						case FCELL_TYPE:
-							d_redchan = (double) ((FCELL *) inrast_redchan)[col];
-						break;
-						case DCELL_TYPE:
-							d_redchan = ((DCELL *) inrast_redchan)[col];
-						break;
-					}
-					switch(data_type_nirchan){
-						case CELL_TYPE:
-							d_nirchan = (double) ((CELL *) inrast_nirchan)[col];
-						break;
-						case FCELL_TYPE:
-							d_nirchan = (double) ((FCELL *) inrast_nirchan)[col];
-						break;
-						case DCELL_TYPE:
-							d_nirchan = ((DCELL *) inrast_nirchan)[col];
-						break;
-					}
-					if (greenchan){
-						switch(data_type_greenchan){
-							case CELL_TYPE:
-								d_greenchan = (double) ((CELL *) inrast_greenchan)[col];
-							break;
-							case FCELL_TYPE:
-								d_greenchan = (double) ((FCELL *) inrast_greenchan)[col];
-							break;
-							case DCELL_TYPE:
-								d_greenchan = ((DCELL *) inrast_greenchan)[col];
-							break;
-						}
-					}
-					if (bluechan){
-						switch(data_type_bluechan){
-							case CELL_TYPE:
-								d_bluechan = (double) ((CELL *) inrast_bluechan)[col];
-							break;
-							case FCELL_TYPE:
-								d_bluechan = (double) ((FCELL *) inrast_bluechan)[col];
-							break;
-							case DCELL_TYPE:
-								d_bluechan = ((DCELL *) inrast_bluechan)[col];
-							break;
-						}
-					}
-					if (chan5chan){
-						switch(data_type_chan5chan){
-							case CELL_TYPE:
-								d_chan5chan = (double) ((CELL *) inrast_chan5chan)[col];
-							break;
-							case FCELL_TYPE:
-								d_chan5chan = (double) ((FCELL *) inrast_chan5chan)[col];
-							break;
-							case DCELL_TYPE:
-								d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
-							break;
-						}
-					}
-					if (chan7chan){
-						switch(data_type_chan7chan){
-							case CELL_TYPE:
-								d_chan7chan = (double) ((CELL *) inrast_chan7chan)[col];
-							break;
-							case FCELL_TYPE:
-								d_chan7chan = (double) ((FCELL *) inrast_chan7chan)[col];
-							break;
-							case DCELL_TYPE:
-								d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
-							break;
-						}
-					}
-			
-					db[0][col]= d_redchan;
-					db[1][col]= d_nirchan;
-					db[2][col]= d_greenchan;
-					db[3][col]= d_bluechan;
-					db[4][col]= d_chan5chan;
-					db[5][col]= d_chan7chan;			
+		    db[0][col] = d_redchan;
+		    db[1][col] = d_nirchan;
+		    db[2][col] = d_greenchan;
+		    db[3][col] = d_bluechan;
+		    db[4][col] = d_chan5chan;
+		    db[5][col] = d_chan7chan;
 
-		
-					if(G_is_d_null_value(&d_redchan)){
-						i=0;
-					}else if(G_is_d_null_value(&d_nirchan)){
-						i=0;
-					}else if((greenchan)&&G_is_d_null_value(&d_greenchan)){
-						i=0;
-					}else if((bluechan)&&G_is_d_null_value(&d_bluechan)){
-						i=0;
-					}else if((chan5chan)&&G_is_d_null_value(&d_chan5chan)){
-						i=0;
-					}else if((chan7chan)&&G_is_d_null_value(&d_chan7chan)){
-						i=0;
-					} else {
+
+		    if (G_is_d_null_value(&d_redchan)) {
+			i = 0;
+		    }
+		    else if (G_is_d_null_value(&d_nirchan)) {
+			i = 0;
+		    }
+		    else if ((greenchan) && G_is_d_null_value(&d_greenchan)) {
+			i = 0;
+		    }
+		    else if ((bluechan) && G_is_d_null_value(&d_bluechan)) {
+			i = 0;
+		    }
+		    else if ((chan5chan) && G_is_d_null_value(&d_chan5chan)) {
+			i = 0;
+		    }
+		    else if ((chan7chan) && G_is_d_null_value(&d_chan7chan)) {
+			i = 0;
+		    }
+		    else {
+
 					/************************************/
-					/*calculate simple_ratio        */
-						if (!strcoll(viflag,"sr")){	
-							i=1;
-						}
-					/*calculate ndvi	            */
-						if (!strcoll(viflag,"ndvi")){
-							i=2;
-						}
-					/*calculate ipvi	            */
-						if (!strcoll(viflag,"ipvi")){
-							i=3;
-						}
-					/*calculate dvi	            */
-						if (!strcoll(viflag,"dvi")){
-							i=4;	
-						}
-					/*calculate pvi	            */
-						if (!strcoll(viflag,"pvi")){
-							i=5;	
-						}
-					/*calculate wdvi	            */
-						if (!strcoll(viflag,"wdvi")){
-							i=6;
-						}
-					/*calculate savi	            */
-						if (!strcoll(viflag,"savi")){
-							i=7;
-						}
-					/*calculate msavi	            */
-						if (!strcoll(viflag,"msavi")){
-							i=8;
-						}
-					/*calculate msavi2            */
-						if (!strcoll(viflag,"msavi2")){
-							i=9;
-						}
-					/*calculate gemi	            */
-						if (!strcoll(viflag,"gemi")){
-							i=10;				
-						}
-					/*calculate arvi	            */
-						if (!strcoll(viflag,"arvi")){
-							i=11;
-						}
-					/*calculate gvi            */
-						if (!strcoll(viflag,"gvi")){
-							i=12;
-						}
-					/*calculate gari	            */
-						if (!strcoll(viflag,"gari")){
-							i=13;
-						}
-						I[col]=i;
-
-					}/*else*/
-			
-				}/*col*/
-				/*printf("Row data has genareted\n");*/
-				row_n=k-1;
-				I[ncols]=row_n;
-				/*MPI_Send(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD);*/
-				MPI_Send(I,ncols+1,MPI_INT,k,1,MPI_COMM_WORLD);
-				MPI_Send(db,6*ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				/*MPI_Send(db1,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				MPI_Send(db2,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				MPI_Send(db3,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				MPI_Send(db4,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				MPI_Send(db5,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				*/		
-	
-			}/*k*/				
-			for(k=1;k<NUM_HOSTS;k++){
-				/*MPI_Recv(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD,&status);*/
-				MPI_Recv(R,ncols+1,MPI_DOUBLE,k,1,MPI_COMM_WORLD,&status);
-				row_n=R[ncols];
-				for (cn=0;cn<ncols;cn++)
-					outputImage[row_n][cn]=R[cn];
+			/*calculate simple_ratio        */
+			if (!strcoll(viflag, "sr")) {
+			    i = 1;
 			}
-			
-			for(k=0;k<(NUM_HOSTS-1);k++)
-                	{
-                        	for(j=0;j<ncols;j++)
-                        		outrast[j] = outputImage[k][j];
-                        	if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-                                	G_fatal_error(_("Cannot write to output raster file"));
+			/*calculate ndvi                    */
+			if (!strcoll(viflag, "ndvi")) {
+			    i = 2;
+			}
+			/*calculate ipvi                    */
+			if (!strcoll(viflag, "ipvi")) {
+			    i = 3;
+			}
+			/*calculate dvi             */
+			if (!strcoll(viflag, "dvi")) {
+			    i = 4;
+			}
+			/*calculate pvi             */
+			if (!strcoll(viflag, "pvi")) {
+			    i = 5;
+			}
+			/*calculate wdvi                    */
+			if (!strcoll(viflag, "wdvi")) {
+			    i = 6;
+			}
+			/*calculate savi                    */
+			if (!strcoll(viflag, "savi")) {
+			    i = 7;
+			}
+			/*calculate msavi                   */
+			if (!strcoll(viflag, "msavi")) {
+			    i = 8;
+			}
+			/*calculate msavi2            */
+			if (!strcoll(viflag, "msavi2")) {
+			    i = 9;
+			}
+			/*calculate gemi                    */
+			if (!strcoll(viflag, "gemi")) {
+			    i = 10;
+			}
+			/*calculate arvi                    */
+			if (!strcoll(viflag, "arvi")) {
+			    i = 11;
+			}
+			/*calculate gvi            */
+			if (!strcoll(viflag, "gvi")) {
+			    i = 12;
+			}
+			/*calculate gari                    */
+			if (!strcoll(viflag, "gari")) {
+			    i = 13;
+			}
+			I[col] = i;
 
-                	}
-		}/*r*/
-		k=1;
-		int lm=0;	
-		for(r=row+1;r<nrows;r++)
-		{	
-			      /* printf("row %d, node %d\n",r,k);*/
-                        	DCELL d_bluechan;
-                        	DCELL d_greenchan;
-                                DCELL d_redchan;
-                                DCELL d_nirchan;
-                                DCELL d_chan5chan;
-                                DCELL d_chan7chan;
-                                G_percent(row,nrows,2);
+		    }		/*else */
 
-                                /* read input maps */
-                                if(G_get_raster_row(infd_redchan,inrast_redchan,r,data_type_redchan)<0)
-                                        G_fatal_error(_("Could not read from <%s>"),redchan);
-                                if(G_get_raster_row(infd_nirchan,inrast_nirchan,r,data_type_nirchan)<0)
-                                        G_fatal_error(_("Could not read from <%s>"),nirchan);
-                                if(greenchan){
-                                        if(G_get_raster_row(infd_greenchan,inrast_greenchan,r,data_type_greenchan)<0)
-                                                G_fatal_error(_("Could not read from <%s>"),greenchan);
-                                }
-                                if(bluechan){
-                                        if(G_get_raster_row(infd_bluechan,inrast_bluechan,r,data_type_bluechan)<0)
-                                                G_fatal_error(_("Could not read from <%s>"),bluechan);
-                                }
-                                if(chan5chan){
-                                        if(G_get_raster_row(infd_chan5chan,inrast_chan5chan,r,data_type_chan5chan)<0)
-                                                G_fatal_error(_("Could not read from <%s>"),chan5chan);
-                                }
-                                if(chan7chan){
-                                        if(G_get_raster_row(infd_chan7chan,inrast_chan7chan,r,data_type_chan7chan)<0)
-                                                G_fatal_error(_("Could not read from <%s>"),chan7chan);
-                                }
+		}		/*col */
+		/*printf("Row data has genareted\n"); */
+		row_n = k - 1;
+		I[ncols] = row_n;
+		/*MPI_Send(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD); */
+		MPI_Send(I, ncols + 1, MPI_INT, k, 1, MPI_COMM_WORLD);
+		MPI_Send(db, 6 * ncols, MPI_DOUBLE, k, 1, MPI_COMM_WORLD);
+		/*MPI_Send(db1,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+		   MPI_Send(db2,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+		   MPI_Send(db3,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+		   MPI_Send(db4,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+		   MPI_Send(db5,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+		 */
 
-                                /*process the data */
+	    }			/*k */
+	    for (k = 1; k < NUM_HOSTS; k++) {
+		/*MPI_Recv(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD,&status); */
+		MPI_Recv(R, ncols + 1, MPI_DOUBLE, k, 1, MPI_COMM_WORLD,
+			 &status);
+		row_n = R[ncols];
+		for (cn = 0; cn < ncols; cn++)
+		    outputImage[row_n][cn] = R[cn];
+	    }
 
-                                for (col=0; col < ncols; col++)
-                                {
-                                        switch(data_type_redchan){
-                                                case CELL_TYPE:
-                                                        d_redchan = (double) ((CELL *) inrast_redchan)[col];
-                                                break;
-                                                case FCELL_TYPE:
-                                                        d_redchan = (double) ((FCELL *) inrast_redchan)[col];
-                                                break;
-                                                case DCELL_TYPE:
-                                                        d_redchan = ((DCELL *) inrast_redchan)[col];
-                                                break;
-                                        }
-                                        switch(data_type_nirchan){
-                                                case CELL_TYPE:
-                                                        d_nirchan = (double) ((CELL *) inrast_nirchan)[col];
-                                                break;
-                                                case FCELL_TYPE:
-                                                        d_nirchan = (double) ((FCELL *) inrast_nirchan)[col];
-                                                break;
-                                                case DCELL_TYPE:
-                                                        d_nirchan = ((DCELL *) inrast_nirchan)[col];
-                                                break;
-                                        }
-                                        if (greenchan){
-                                                switch(data_type_greenchan){
-                                                        case CELL_TYPE:
-                                                                d_greenchan = (double) ((CELL *) inrast_greenchan)[col];
-                                                        break;
-                                                        case FCELL_TYPE:
-                                                                d_greenchan = (double) ((FCELL *) inrast_greenchan)[col];
-                                                        break;
-                                                        case DCELL_TYPE:
-                                                                d_greenchan = ((DCELL *) inrast_greenchan)[col];
-                                                        break;
-                                                }
-                                        }
-                                        if (bluechan){
-                                                switch(data_type_bluechan){
-                                                        case CELL_TYPE:
-                                                                d_bluechan = (double) ((CELL *) inrast_bluechan)[col];
-                                                        break;
-                                                        case FCELL_TYPE:
-                                                                d_bluechan = (double) ((FCELL *) inrast_bluechan)[col];
-                                                        break;
-                                                        case DCELL_TYPE:
-                                                                d_bluechan = ((DCELL *) inrast_bluechan)[col];
-                                                        break;
-                                                }
-                                        }
-                                        if (chan5chan){
-                                                
-                                                switch(data_type_chan5chan){
-                                                        case CELL_TYPE:
-                                                                d_chan5chan = (double) ((CELL *) inrast_chan5chan)[col];
-                                                        break;
-                                                        case FCELL_TYPE:
-                                                                d_chan5chan = (double) ((FCELL *) inrast_chan5chan)[col];
-                                                        break;
-                                                        case DCELL_TYPE:
-                                                                d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
-                                                        break;
-                                                }
-                                        }
-                                        if (chan7chan){
-                                                switch(data_type_chan7chan){
-                                                        case CELL_TYPE:
-                                                                d_chan7chan = (double) ((CELL *) inrast_chan7chan)[col];
-                                                        break;
-                                                        case FCELL_TYPE:
-                                                                d_chan7chan = (double) ((FCELL *) inrast_chan7chan)[col];
-                                                        break;
-                                                        case DCELL_TYPE:
-                                                                d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
-                                                        break;
-                                                }
-                                        }
+	    for (k = 0; k < (NUM_HOSTS - 1); k++) {
+		for (j = 0; j < ncols; j++)
+		    outrast[j] = outputImage[k][j];
+		if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+		    G_fatal_error(_("Cannot write to output raster file"));
 
-                                        db[0][col]= d_redchan;
-                                        db[1][col]= d_nirchan;
-                                        db[2][col]= d_greenchan;
-                                        db[3][col]= d_bluechan;
-                                        db[4][col]= d_chan5chan;
-                                        db[5][col]= d_chan7chan;
+	    }
+	}			/*r */
+	k = 1;
+	int lm = 0;
 
-                                        if(G_is_d_null_value(&d_redchan)){
-                                                i=0;
-                                        }else if(G_is_d_null_value(&d_nirchan)){
-                                                i=0;
-                                        }else if((greenchan)&&G_is_d_null_value(&d_greenchan)){
-                                                i=0;
-                                        }else if((bluechan)&&G_is_d_null_value(&d_bluechan)){
-                                                i=0;
-                                        }else if((chan5chan)&&G_is_d_null_value(&d_chan5chan)){
-                                                i=0;
-                                        }else if((chan7chan)&&G_is_d_null_value(&d_chan7chan)){
-                                                i=0;
-                                        } else 
-					{
-                                        /************************************/
-                                        /*calculate simple_ratio        */
-                                                if (!strcoll(viflag,"sr")){
-                                                        i=1;
-                                                }
-                                        /*calculate ndvi                    */
-                                                if (!strcoll(viflag,"ndvi")){
-                                                        i=2;
-                                                }
-                                        /*calculate ipvi                    */
-                                                if (!strcoll(viflag,"ipvi")){
-                                                        i=3;
-                                                }
-                                        /*calculate dvi             */
-                                                if (!strcoll(viflag,"dvi")){
-                                                        i=4;
-                                                }
-                                        /*calculate pvi             */
-                                                if (!strcoll(viflag,"pvi")){
-                                                        i=5;
-                                                }
-                                        /*calculate wdvi                    */
-                                                if (!strcoll(viflag,"wdvi")){
-                                                        i=6;
-                                                }
-                                        /*calculate savi                    */
-                                                if (!strcoll(viflag,"savi")){
-                                                        i=7;
-                                                }
-                                        /*calculate msavi                   */
-                                                if (!strcoll(viflag,"msavi")){
-                                                        i=8;
-                                                }
-                                        /*calculate msavi2            */
-                                                if (!strcoll(viflag,"msavi2")){
-                                                        i=9;
-                                                }
-                                        /*calculate gemi                    */
-                                                if (!strcoll(viflag,"gemi")){
-                                                        i=10;
-                                                }
-                                        /*calculate arvi                    */
-                                                if (!strcoll(viflag,"arvi")){
-                                                        i=11;
-                                                }
-                                        /*calculate gvi            */
-                                                if (!strcoll(viflag,"gvi")){
-                                                        i=12;
-                                                }
-                                        /*calculate gari                    */
-                                                if (!strcoll(viflag,"gari")){
-                                                        i=13;
-                                                }
-					}
-                                                I[col]=i;
-				}/*col*/
-                      		row_n=k-1;
-                                I[ncols]=row_n;
-				/*MPI_Send(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD);*/
-                                MPI_Send(I,ncols+1,MPI_INT,k,1,MPI_COMM_WORLD);
-                                MPI_Send(db,6*ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-                                /*MPI_Send(db1,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-                                MPI_Send(db2,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-                                MPI_Send(db3,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-                                MPI_Send(db4,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-                                MPI_Send(db5,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
-				*/
-				k++;
-				lm=1;		
-			}/*r */
-			if(lm)
-			{	
-				for(nh=1;nh<k;nh++){
-                                /*	MPI_Recv(&row_n,1,MPI_INT,nh,1,MPI_COMM_WORLD,&status);*/
-                                	MPI_Recv(R,ncols+1,MPI_DOUBLE,nh,1,MPI_COMM_WORLD,&status);
-					row_n=R[ncols];
-                                	for (cn=0;cn<ncols;cn++)
-                                        	outputImage[row_n][cn]=R[cn];
-                        	}
+	for (r = row + 1; r < nrows; r++) {
+	    /* printf("row %d, node %d\n",r,k); */
+	    DCELL d_bluechan;
 
-                        	for(nh=0;nh<(k-1);nh++)
-                        	{
-                                	for(j=0;j<ncols;j++)
-                                        	outrast[j] = outputImage[nh][j];
-                                	if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-                                        	G_fatal_error(_("Cannot write to output raster file"));
-                        	}
-			}
+	    DCELL d_greenchan;
 
-        	MPI_Finalize();
-		G_free(inrast_redchan);
-		G_close_cell(infd_redchan);
-		G_free(inrast_nirchan);
-		G_close_cell(infd_nirchan);
-		if(greenchan){
-			G_free(inrast_greenchan);	
-			G_close_cell(infd_greenchan);	
+	    DCELL d_redchan;
+
+	    DCELL d_nirchan;
+
+	    DCELL d_chan5chan;
+
+	    DCELL d_chan7chan;
+
+	    G_percent(row, nrows, 2);
+
+	    /* read input maps */
+	    if (G_get_raster_row
+		(infd_redchan, inrast_redchan, r, data_type_redchan) < 0)
+		G_fatal_error(_("Could not read from <%s>"), redchan);
+	    if (G_get_raster_row
+		(infd_nirchan, inrast_nirchan, r, data_type_nirchan) < 0)
+		G_fatal_error(_("Could not read from <%s>"), nirchan);
+	    if (greenchan) {
+		if (G_get_raster_row
+		    (infd_greenchan, inrast_greenchan, r,
+		     data_type_greenchan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), greenchan);
+	    }
+	    if (bluechan) {
+		if (G_get_raster_row
+		    (infd_bluechan, inrast_bluechan, r,
+		     data_type_bluechan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), bluechan);
+	    }
+	    if (chan5chan) {
+		if (G_get_raster_row
+		    (infd_chan5chan, inrast_chan5chan, r,
+		     data_type_chan5chan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), chan5chan);
+	    }
+	    if (chan7chan) {
+		if (G_get_raster_row
+		    (infd_chan7chan, inrast_chan7chan, r,
+		     data_type_chan7chan) < 0)
+		    G_fatal_error(_("Could not read from <%s>"), chan7chan);
+	    }
+
+	    /*process the data */
+
+	    for (col = 0; col < ncols; col++) {
+		switch (data_type_redchan) {
+		case CELL_TYPE:
+		    d_redchan = (double)((CELL *) inrast_redchan)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_redchan = (double)((FCELL *) inrast_redchan)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_redchan = ((DCELL *) inrast_redchan)[col];
+		    break;
 		}
-		if(bluechan){
-			G_free(inrast_bluechan);	
-			G_close_cell(infd_bluechan);	
+		switch (data_type_nirchan) {
+		case CELL_TYPE:
+		    d_nirchan = (double)((CELL *) inrast_nirchan)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_nirchan = (double)((FCELL *) inrast_nirchan)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_nirchan = ((DCELL *) inrast_nirchan)[col];
+		    break;
 		}
-		if(chan5chan){
-			G_free(inrast_chan5chan);	
-			G_close_cell(infd_chan5chan);	
+		if (greenchan) {
+		    switch (data_type_greenchan) {
+		    case CELL_TYPE:
+			d_greenchan =
+			    (double)((CELL *) inrast_greenchan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_greenchan =
+			    (double)((FCELL *) inrast_greenchan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_greenchan = ((DCELL *) inrast_greenchan)[col];
+			break;
+		    }
 		}
-		if(chan7chan){
-			G_free(inrast_chan7chan);	
-			G_close_cell(infd_chan7chan);	
+		if (bluechan) {
+		    switch (data_type_bluechan) {
+		    case CELL_TYPE:
+			d_bluechan = (double)((CELL *) inrast_bluechan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_bluechan = (double)((FCELL *) inrast_bluechan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_bluechan = ((DCELL *) inrast_bluechan)[col];
+			break;
+		    }
 		}
-		G_free(outrast);
-		G_close_cell(outfd);
-		
-		G_short_history(result, "raster", &history);
-		G_command_history(&history);
-		G_write_history(result,&history);
+		if (chan5chan) {
 
-		exit(EXIT_SUCCESS);
-	}/*if end*/	
-	else if(me){
+		    switch (data_type_chan5chan) {
+		    case CELL_TYPE:
+			d_chan5chan =
+			    (double)((CELL *) inrast_chan5chan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_chan5chan =
+			    (double)((FCELL *) inrast_chan5chan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_chan5chan = ((DCELL *) inrast_chan5chan)[col];
+			break;
+		    }
+		}
+		if (chan7chan) {
+		    switch (data_type_chan7chan) {
+		    case CELL_TYPE:
+			d_chan7chan =
+			    (double)((CELL *) inrast_chan7chan)[col];
+			break;
+		    case FCELL_TYPE:
+			d_chan7chan =
+			    (double)((FCELL *) inrast_chan7chan)[col];
+			break;
+		    case DCELL_TYPE:
+			d_chan7chan = ((DCELL *) inrast_chan7chan)[col];
+			break;
+		    }
+		}
 
-		int col,n_rows,i,row_n,modv,nrows,ncols,t,temp;
-		int *I;
-		double *a, *b, *c, *d, *e, *f, *r;
-		/*double *r;*/
-		
-		MPI_Recv(&temp,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
-		MPI_Recv(&nrows,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
-        	MPI_Recv(&ncols,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
-		/*printf("Slave->%d: nrows=%d, ncols=%d \n",me,nrows,ncols);*/
- 	
-		I=(int *)malloc((ncols+2)*sizeof(int));
-        	a=(double *)malloc((ncols+1)*sizeof(double));
-        	b=(double *)malloc((ncols+1)*sizeof(double));
-        	c=(double *)malloc((ncols+1)*sizeof(double));
-       		d=(double *)malloc((ncols+1)*sizeof(double));
-        	e=(double *)malloc((ncols+1)*sizeof(double));
-        	f=(double *)malloc((ncols+1)*sizeof(double));
-		
-        	r=(double *)malloc((ncols+2)*sizeof(double));
-		double db[6][ncols];
-		n_rows=nrows/(NUM_HOSTS-1);
-		modv=nrows%(NUM_HOSTS-1);
-		/*temp=10;*/
-		/*printf("%d\n",temp);*/	
-		
-		/*int temp;	*/
-		if(modv>=me)
-			n_rows++;
-		for(i=0;i<n_rows;i++)
-		{
-				
-			/*MPI_Recv(&row_n,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);*/
-			MPI_Recv(I,ncols+1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(db,6*ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-		/*	MPI_Recv(a,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(b,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(c,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(d,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(e,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-			MPI_Recv(f,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
-		*/	
-			for (col=0; col<ncols; col++)
-			{
-	
-				a[col]=db[0][col];
-				b[col]=db[1][col];
-				c[col]=db[2][col];
-				d[col]=db[3][col];
-				e[col]=db[4][col];
-				f[col]=db[5][col];
+		db[0][col] = d_redchan;
+		db[1][col] = d_nirchan;
+		db[2][col] = d_greenchan;
+		db[3][col] = d_bluechan;
+		db[4][col] = d_chan5chan;
+		db[5][col] = d_chan7chan;
 
-				for(t=0;t<temp;t++)
-				{		
-			
-				if (I[col]==0) r[col]=-999.99;
-				else if (I[col]==1){
-				/*sr*/
-					if(  a[col] ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = (b[col]/ a[col]);
-					}	
-				}
-				else if (I[col]==2){
-				/*ndvi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ( b[col] - a[col] ) / ( b[col] + a[col] );
-					}
-				}
-				else if (I[col]==3){
-				/*ipvi*/	
-		
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ( b[col] ) / ( b[col] + a[col] );
-					}	
+		if (G_is_d_null_value(&d_redchan)) {
+		    i = 0;
+		}
+		else if (G_is_d_null_value(&d_nirchan)) {
+		    i = 0;
+		}
+		else if ((greenchan) && G_is_d_null_value(&d_greenchan)) {
+		    i = 0;
+		}
+		else if ((bluechan) && G_is_d_null_value(&d_bluechan)) {
+		    i = 0;
+		}
+		else if ((chan5chan) && G_is_d_null_value(&d_chan5chan)) {
+		    i = 0;
+		}
+		else if ((chan7chan) && G_is_d_null_value(&d_chan7chan)) {
+		    i = 0;
+		}
+		else {
 
-				}	
-				else if (I[col]==4){
-				/*dvi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ( b[col] - a[col] ) ;
-					}		
-				}
-				else if (I[col]==5){
-				/*pvi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = (sin(1.0) * b[col] ) / ( cos(1.0) * a[col] ); 
-					}
-				}
-				else if (I[col]==6){
-				/*wdvi*/
-					double slope=1;/*slope of soil line */
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ( b[col] - slope*a[col] );
-					}
-				}
-				else if (I[col]==7){
-				/*savi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ((1+0.5)*( b[col] - a[col] )) / ( b[col] + a[col] +0.5);
-					}
-				}
-				else if (I[col]==8){
-				/*msavi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] =(1/2)*(2 * (b[col]+1)-sqrt((2*b[col]+1)*(2*b[col]+1))-(8 *(b[col]-a[col]))) ;
-					}
-				}
-				else if (I[col]==9){
-				/*msavi2*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] =(1/2)*(2 * (b[col]+1)-sqrt((2*b[col]+1)*(2*b[col]+1))-(8 *(b[col]-a[col]))) ;
-					}
-				}
-				else if (I[col]==10){
-				/*gemi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = (( (2*((b[col] * b[col])-(a[col] * a[col]))+1.5*b[col]+0.5*a[col]) /(b[col]+ a[col] + 0.5)) * (1 - 0.25 * (2*((b[col] * b[col])-(a[col] * a[col]))+1.5*b[col]+0.5*a[col]) /(b[col] + a[col] + 0.5))) -( (a[col] - 0.125) / (1 - a[col])) ;
-					}
-				}
-				else if (I[col]==11){
-				/*arvi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else {
-						r[col] = ( b[col] - (2*a[col] - d[col])) / ( b[col] + (2*a[col] - d[col]));
-					}
-				}
-				else if (I[col]==12){
-				/*gvi*/
-					if( ( b[col] + a[col] ) ==  0.0 ){
-						r[col] = -1.0;
-					} else	{
-						r[col] = (-0.2848*d[col]-0.2435*c[col]-0.5436*a[col]+0.7243*b[col]+0.0840*e[col]- 0.1800*f[col]);
-					}
-				}
-				else if (I[col]==13){
-				/*gari*/
-					r[col] = ( b[col] - (c[col]-(d[col] - a[col]))) / ( b[col] + (c[col]-(d[col] - a[col]))) ;
-				}
+					/************************************/
+		    /*calculate simple_ratio        */
+		    if (!strcoll(viflag, "sr")) {
+			i = 1;
+		    }
+		    /*calculate ndvi                    */
+		    if (!strcoll(viflag, "ndvi")) {
+			i = 2;
+		    }
+		    /*calculate ipvi                    */
+		    if (!strcoll(viflag, "ipvi")) {
+			i = 3;
+		    }
+		    /*calculate dvi             */
+		    if (!strcoll(viflag, "dvi")) {
+			i = 4;
+		    }
+		    /*calculate pvi             */
+		    if (!strcoll(viflag, "pvi")) {
+			i = 5;
+		    }
+		    /*calculate wdvi                    */
+		    if (!strcoll(viflag, "wdvi")) {
+			i = 6;
+		    }
+		    /*calculate savi                    */
+		    if (!strcoll(viflag, "savi")) {
+			i = 7;
+		    }
+		    /*calculate msavi                   */
+		    if (!strcoll(viflag, "msavi")) {
+			i = 8;
+		    }
+		    /*calculate msavi2            */
+		    if (!strcoll(viflag, "msavi2")) {
+			i = 9;
+		    }
+		    /*calculate gemi                    */
+		    if (!strcoll(viflag, "gemi")) {
+			i = 10;
+		    }
+		    /*calculate arvi                    */
+		    if (!strcoll(viflag, "arvi")) {
+			i = 11;
+		    }
+		    /*calculate gvi            */
+		    if (!strcoll(viflag, "gvi")) {
+			i = 12;
+		    }
+		    /*calculate gari                    */
+		    if (!strcoll(viflag, "gari")) {
+			i = 13;
+		    }
+		}
+		I[col] = i;
+	    }			/*col */
+	    row_n = k - 1;
+	    I[ncols] = row_n;
+	    /*MPI_Send(&row_n,1,MPI_INT,k,1,MPI_COMM_WORLD); */
+	    MPI_Send(I, ncols + 1, MPI_INT, k, 1, MPI_COMM_WORLD);
+	    MPI_Send(db, 6 * ncols, MPI_DOUBLE, k, 1, MPI_COMM_WORLD);
+	    /*MPI_Send(db1,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+	       MPI_Send(db2,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+	       MPI_Send(db3,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+	       MPI_Send(db4,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+	       MPI_Send(db5,ncols,MPI_DOUBLE,k,1,MPI_COMM_WORLD);
+	     */
+	    k++;
+	    lm = 1;
+	}			/*r */
+	if (lm) {
+	    for (nh = 1; nh < k; nh++) {
+		/*      MPI_Recv(&row_n,1,MPI_INT,nh,1,MPI_COMM_WORLD,&status); */
+		MPI_Recv(R, ncols + 1, MPI_DOUBLE, nh, 1, MPI_COMM_WORLD,
+			 &status);
+		row_n = R[ncols];
+		for (cn = 0; cn < ncols; cn++)
+		    outputImage[row_n][cn] = R[cn];
+	    }
 
-			} /*for temp*/
+	    for (nh = 0; nh < (k - 1); nh++) {
+		for (j = 0; j < ncols; j++)
+		    outrast[j] = outputImage[nh][j];
+		if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+		    G_fatal_error(_("Cannot write to output raster file"));
+	    }
+	}
 
-		}/*col end*/
-		r[ncols]=I[ncols];
-	
-		/*MPI_Send(&row_n,1,MPI_INT,0,1,MPI_COMM_WORLD);*/
-		MPI_Send(r,ncols+1,MPI_DOUBLE,0,1,MPI_COMM_WORLD);
-	}/*row end*/
+	MPI_Finalize();
+	G_free(inrast_redchan);
+	G_close_cell(infd_redchan);
+	G_free(inrast_nirchan);
+	G_close_cell(infd_nirchan);
+	if (greenchan) {
+	    G_free(inrast_greenchan);
+	    G_close_cell(infd_greenchan);
+	}
+	if (bluechan) {
+	    G_free(inrast_bluechan);
+	    G_close_cell(infd_bluechan);
+	}
+	if (chan5chan) {
+	    G_free(inrast_chan5chan);
+	    G_close_cell(infd_chan5chan);
+	}
+	if (chan7chan) {
+	    G_free(inrast_chan7chan);
+	    G_close_cell(infd_chan7chan);
+	}
+	G_free(outrast);
+	G_close_cell(outfd);
 
+	G_short_history(result, "raster", &history);
+	G_command_history(&history);
+	G_write_history(result, &history);
+
+	exit(EXIT_SUCCESS);
+    }				/*if end */
+    else if (me) {
+
+	int col, n_rows, i, row_n, modv, nrows, ncols, t, temp;
+
+	int *I;
+
+	double *a, *b, *c, *d, *e, *f, *r;
+
+	/*double *r; */
+
+	MPI_Recv(&temp, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
+	MPI_Recv(&nrows, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
+	MPI_Recv(&ncols, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
+	/*printf("Slave->%d: nrows=%d, ncols=%d \n",me,nrows,ncols); */
+
+	I = (int *)malloc((ncols + 2) * sizeof(int));
+	a = (double *)malloc((ncols + 1) * sizeof(double));
+	b = (double *)malloc((ncols + 1) * sizeof(double));
+	c = (double *)malloc((ncols + 1) * sizeof(double));
+	d = (double *)malloc((ncols + 1) * sizeof(double));
+	e = (double *)malloc((ncols + 1) * sizeof(double));
+	f = (double *)malloc((ncols + 1) * sizeof(double));
+
+	r = (double *)malloc((ncols + 2) * sizeof(double));
+	double db[6][ncols];
+
+	n_rows = nrows / (NUM_HOSTS - 1);
+	modv = nrows % (NUM_HOSTS - 1);
+	/*temp=10; */
+	/*printf("%d\n",temp); */
+
+	/*int temp;     */
+	if (modv >= me)
+	    n_rows++;
+	for (i = 0; i < n_rows; i++) {
+
+	    /*MPI_Recv(&row_n,1,MPI_INT,0,1,MPI_COMM_WORLD,&status); */
+	    MPI_Recv(I, ncols + 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
+	    MPI_Recv(db, 6 * ncols, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD,
+		     &status);
+	    /*      MPI_Recv(a,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	       MPI_Recv(b,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	       MPI_Recv(c,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	       MPI_Recv(d,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	       MPI_Recv(e,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	       MPI_Recv(f,ncols,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);
+	     */
+	    for (col = 0; col < ncols; col++) {
+
+		a[col] = db[0][col];
+		b[col] = db[1][col];
+		c[col] = db[2][col];
+		d[col] = db[3][col];
+		e[col] = db[4][col];
+		f[col] = db[5][col];
+
+		for (t = 0; t < temp; t++) {
+
+		    if (I[col] == 0)
+			r[col] = -999.99;
+		    else if (I[col] == 1) {
+			/*sr */
+			if (a[col] == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] = (b[col] / a[col]);
+			}
+		    }
+		    else if (I[col] == 2) {
+			/*ndvi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] = (b[col] - a[col]) / (b[col] + a[col]);
+			}
+		    }
+		    else if (I[col] == 3) {
+			/*ipvi */
+
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] = (b[col]) / (b[col] + a[col]);
+			}
+
+		    }
+		    else if (I[col] == 4) {
+			/*dvi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] = (b[col] - a[col]);
+			}
+		    }
+		    else if (I[col] == 5) {
+			/*pvi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(sin(1.0) * b[col]) / (cos(1.0) * a[col]);
+			}
+		    }
+		    else if (I[col] == 6) {
+			/*wdvi */
+			double slope = 1;	/*slope of soil line */
+
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] = (b[col] - slope * a[col]);
+			}
+		    }
+		    else if (I[col] == 7) {
+			/*savi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				((1 + 0.5) * (b[col] - a[col])) / (b[col] +
+								   a[col] +
+								   0.5);
+			}
+		    }
+		    else if (I[col] == 8) {
+			/*msavi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(1 / 2) * (2 * (b[col] + 1) -
+					   sqrt((2 * b[col] +
+						 1) * (2 * b[col] + 1)) -
+					   (8 * (b[col] - a[col])));
+			}
+		    }
+		    else if (I[col] == 9) {
+			/*msavi2 */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(1 / 2) * (2 * (b[col] + 1) -
+					   sqrt((2 * b[col] +
+						 1) * (2 * b[col] + 1)) -
+					   (8 * (b[col] - a[col])));
+			}
+		    }
+		    else if (I[col] == 10) {
+			/*gemi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(((2 *
+				   ((b[col] * b[col]) - (a[col] * a[col])) +
+				   1.5 * b[col] + 0.5 * a[col]) / (b[col] +
+								   a[col] +
+								   0.5)) *
+				 (1 -
+				  0.25 * (2 *
+					  ((b[col] * b[col]) -
+					   (a[col] * a[col])) + 1.5 * b[col] +
+					  0.5 * a[col]) / (b[col] + a[col] +
+							   0.5))) - ((a[col] -
+								      0.125) /
+								     (1 -
+								      a
+								      [col]));
+			}
+		    }
+		    else if (I[col] == 11) {
+			/*arvi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(b[col] - (2 * a[col] - d[col])) / (b[col] +
+								    (2 *
+								     a[col] -
+								     d[col]));
+			}
+		    }
+		    else if (I[col] == 12) {
+			/*gvi */
+			if ((b[col] + a[col]) == 0.0) {
+			    r[col] = -1.0;
+			}
+			else {
+			    r[col] =
+				(-0.2848 * d[col] - 0.2435 * c[col] -
+				 0.5436 * a[col] + 0.7243 * b[col] +
+				 0.0840 * e[col] - 0.1800 * f[col]);
+			}
+		    }
+		    else if (I[col] == 13) {
+			/*gari */
+			r[col] =
+			    (b[col] -
+			     (c[col] - (d[col] - a[col]))) / (b[col] +
+							      (c[col] -
+							       (d[col] -
+								a[col])));
+		    }
+
+		}		/*for temp */
+
+	    }			/*col end */
+	    r[ncols] = I[ncols];
+
+	    /*MPI_Send(&row_n,1,MPI_INT,0,1,MPI_COMM_WORLD); */
+	    MPI_Send(r, ncols + 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
+	}			/*row end */
+
 	free(I);
 	free(a);
 	free(b);
@@ -903,8 +1057,7 @@
 	free(e);
 	free(f);
 	free(r);
-	MPI_Finalize();	
+	MPI_Finalize();
 
-   	}/*if end*/
-}/*main end*/
-
+    }				/*if end */
+}				/*main end */

Modified: grass-addons/gipe/i.water/main.c
===================================================================
--- grass-addons/gipe/i.water/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.water/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,239 +1,267 @@
-/****************************************************************************
- *
- * MODULE:       i.water
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates if water is there (value=1)
- * 		 two versions, 1) generic (albedo,ndvi)
- * 		 2) Modis (surf_refl_7,ndvi)
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-double water(double albedo, double ndvi);
-double water_modis(double surf_ref_7, double ndvi);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *output1;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output raster name*/
-	/*File Descriptors*/
-	int infd_ndvi, infd_albedo, infd_ref7;
-	int outfd1;
-	
-	char *ndvi,*albedo,*ref7;
-	int i=0,j=0;
-	
-	void *inrast_ndvi, *inrast_albedo, *inrast_ref7;
-	CELL *outrast1;
-	RASTER_MAP_TYPE data_type_output=CELL_TYPE;
-	RASTER_MAP_TYPE data_type_ndvi;
-	RASTER_MAP_TYPE data_type_albedo;
-	RASTER_MAP_TYPE data_type_ref7;
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("water, detection");
-	module->description = _("Water detection, 1 if found, 0 if not");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("ndvi");
-	input1->description=_("Name of the NDVI layer [-]");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("albedo");
-	input2->required   = NO ;
-	input2->description=_("Name of the Albedo layer [-]");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("Modref7");
-	input3->required   = NO ;
-	input3->description=_("Name of the Modis surface reflectance band 7 layer [-]");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->description=_("Name of the output water layer [0/1]");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	ndvi	 	= input1->answer;
-	if(input2->answer)
-	albedo	 	= input2->answer;
-	if(input3->answer)
-	ref7		= input3->answer;
-	
-
-	if(!input2->answer&&!input3->answer){
-		G_fatal_error(_("ERROR: needs either Albedo or Modis surface reflectance in band 7, bailing out."));
-	}
-	
-	result1  = output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(ndvi, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), ndvi);
-	}
-	data_type_ndvi = G_raster_map_type(ndvi,mapset);
-	if ( (infd_ndvi = G_open_cell_old (ndvi,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), ndvi);
-	if (G_get_cellhd (ndvi, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), ndvi);
-	inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
-	/***************************************************/
-	if(input2->answer){
-	mapset = G_find_cell2 (albedo, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),albedo);
-	}
-	data_type_albedo = G_raster_map_type(albedo,mapset);
-	if ( (infd_albedo = G_open_cell_old (albedo,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), albedo);
-	if (G_get_cellhd (albedo, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), albedo);
-	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
-	}
-	/***************************************************/
-	if(input3->answer){
-	mapset = G_find_cell2 (ref7, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), ref7);
-	}
-	data_type_ref7 = G_raster_map_type(ref7,mapset);
-	if ( (infd_ref7 = G_open_cell_old (ref7,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), ref7);
-	if (G_get_cellhd (ref7, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), ref7);
-	inrast_ref7 = G_allocate_raster_buf(data_type_ref7);
-	}
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		CELL d;
-		DCELL d_ndvi;
-		DCELL d_albedo;
-		DCELL d_ref7;
-		G_percent(row,nrows,2);
-		/* read input maps */	
-		if(G_get_raster_row(infd_ndvi,inrast_ndvi,row,data_type_ndvi)<0)
-			G_fatal_error(_("Could not read from <%s>"),ndvi);
-		if(input2->answer){
-		if(G_get_raster_row(infd_albedo,inrast_albedo,row,data_type_albedo)<0)
-			G_fatal_error(_("Could not read from <%s>"),albedo);
-		}
-		if(input3->answer){
-		if(G_get_raster_row(infd_ref7,inrast_ref7,row,data_type_ref7)<0)
-			G_fatal_error(_("Could not read from <%s>"),ref7);
-		}
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_ndvi){
-				case CELL_TYPE:
-					d_ndvi = (double) ((CELL *) inrast_ndvi)[col];
-					break;
-				case FCELL_TYPE:
-					d_ndvi = (double) ((FCELL *) inrast_ndvi)[col];
-					break;
-				case DCELL_TYPE:
-					d_ndvi = ((DCELL *) inrast_ndvi)[col];
-					break;
-			}
-			if(input2->answer){
-			switch(data_type_albedo){
-				case CELL_TYPE:
-					d_albedo = (double) ((CELL *) inrast_albedo)[col];
-					break;
-				case FCELL_TYPE:
-					d_albedo = (double) ((FCELL *) inrast_albedo)[col];
-					break;
-				case DCELL_TYPE:
-					d_albedo = ((DCELL *) inrast_albedo)[col];
-					break;
-			}
-			}
-			if(input3->answer){
-			switch(data_type_ref7){
-				case CELL_TYPE:
-					d_ref7 = (double) ((CELL *) inrast_ref7)[col];
-					break;
-				case FCELL_TYPE:
-					d_ref7 = (double) ((FCELL *) inrast_ref7)[col];
-					break;
-				case DCELL_TYPE:
-					d_ref7 = ((DCELL *) inrast_ref7)[col];
-					break;
-			}
-			}
-			if(G_is_d_null_value(&d_ndvi)||
-			(input2->answer&&G_is_d_null_value(&d_albedo))||
-			(input3->answer&&G_is_d_null_value(&d_ref7))){
-				G_set_c_null_value(&outrast1[col],1);
-			}else {
-				/************************************/
-				/* calculate water detection	    */
-				if(input2->answer){
-					d = water(d_albedo,d_ndvi);
-				} else if (input3->answer){
-					d = water_modis(d_ref7,d_ndvi);
-				}
-				outrast1[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_ndvi);
-	G_close_cell (infd_ndvi);
-	if(input2->answer){
-		G_free (inrast_albedo);
-		G_close_cell (infd_albedo);
-	}
-	if(input3->answer){
-		G_free (inrast_ref7);
-		G_close_cell (infd_ref7);
-	}
-	G_free (outrast1);
-	G_close_cell (outfd1);
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.water
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates if water is there (value=1)
+ * 		 two versions, 1) generic (albedo,ndvi)
+ * 		 2) Modis (surf_refl_7,ndvi)
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double water(double albedo, double ndvi);
+
+double water_modis(double surf_ref_7, double ndvi);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *output1;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_ndvi, infd_albedo, infd_ref7;
+
+    int outfd1;
+
+    char *ndvi, *albedo, *ref7;
+
+    int i = 0, j = 0;
+
+    void *inrast_ndvi, *inrast_albedo, *inrast_ref7;
+
+    CELL * outrast1;
+    RASTER_MAP_TYPE data_type_output = CELL_TYPE;
+    RASTER_MAP_TYPE data_type_ndvi;
+    RASTER_MAP_TYPE data_type_albedo;
+    RASTER_MAP_TYPE data_type_ref7;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("water, detection");
+    module->description = _("Water detection, 1 if found, 0 if not");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("ndvi");
+    input1->description = _("Name of the NDVI layer [-]");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("albedo");
+    input2->required = NO;
+    input2->description = _("Name of the Albedo layer [-]");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("Modref7");
+    input3->required = NO;
+    input3->description =
+	_("Name of the Modis surface reflectance band 7 layer [-]");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->description = _("Name of the output water layer [0/1]");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    ndvi = input1->answer;
+    if (input2->answer)
+	albedo = input2->answer;
+    if (input3->answer)
+	ref7 = input3->answer;
+    if (!input2->answer && !input3->answer) {
+	G_fatal_error(_("ERROR: needs either Albedo or Modis surface reflectance in band 7, bailing out."));
+    }
+    result1 = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(ndvi, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), ndvi);
+    }
+    data_type_ndvi = G_raster_map_type(ndvi, mapset);
+    if ((infd_ndvi = G_open_cell_old(ndvi, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), ndvi);
+    if (G_get_cellhd(ndvi, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), ndvi);
+    inrast_ndvi = G_allocate_raster_buf(data_type_ndvi);
+    
+
+	/***************************************************/ 
+	if (input2->answer) {
+	mapset = G_find_cell2(albedo, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("cell file [%s] not found"), albedo);
+	}
+	data_type_albedo = G_raster_map_type(albedo, mapset);
+	if ((infd_albedo = G_open_cell_old(albedo, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), albedo);
+	if (G_get_cellhd(albedo, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), albedo);
+	inrast_albedo = G_allocate_raster_buf(data_type_albedo);
+    }
+    
+
+	/***************************************************/ 
+	if (input3->answer) {
+	mapset = G_find_cell2(ref7, "");
+	if (mapset == NULL) {
+	    G_fatal_error(_("Cell file [%s] not found"), ref7);
+	}
+	data_type_ref7 = G_raster_map_type(ref7, mapset);
+	if ((infd_ref7 = G_open_cell_old(ref7, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), ref7);
+	if (G_get_cellhd(ref7, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), ref7);
+	inrast_ref7 = G_allocate_raster_buf(data_type_ref7);
+    }
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	CELL d;
+	DCELL d_ndvi;
+	DCELL d_albedo;
+	DCELL d_ref7;
+	G_percent(row, nrows, 2);
+	
+	    /* read input maps */ 
+	    if (G_get_raster_row(infd_ndvi, inrast_ndvi, row, data_type_ndvi)
+		< 0)
+	    G_fatal_error(_("Could not read from <%s>"), ndvi);
+	if (input2->answer) {
+	    if (G_get_raster_row
+		 (infd_albedo, inrast_albedo, row, data_type_albedo) < 0)
+		G_fatal_error(_("Could not read from <%s>"), albedo);
+	}
+	if (input3->answer) {
+	    if (G_get_raster_row(infd_ref7, inrast_ref7, row, data_type_ref7)
+		 < 0)
+		G_fatal_error(_("Could not read from <%s>"), ref7);
+	}
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_ndvi) {
+	    case CELL_TYPE:
+		d_ndvi = (double)((CELL *) inrast_ndvi)[col];
+		break;
+	    case FCELL_TYPE:
+		d_ndvi = (double)((FCELL *) inrast_ndvi)[col];
+		break;
+	    case DCELL_TYPE:
+		d_ndvi = ((DCELL *) inrast_ndvi)[col];
+		break;
+	    }
+	    if (input2->answer) {
+		switch (data_type_albedo) {
+		case CELL_TYPE:
+		    d_albedo = (double)((CELL *) inrast_albedo)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_albedo = (double)((FCELL *) inrast_albedo)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_albedo = ((DCELL *) inrast_albedo)[col];
+		    break;
+		}
+	    }
+	    if (input3->answer) {
+		switch (data_type_ref7) {
+		case CELL_TYPE:
+		    d_ref7 = (double)((CELL *) inrast_ref7)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_ref7 = (double)((FCELL *) inrast_ref7)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_ref7 = ((DCELL *) inrast_ref7)[col];
+		    break;
+		}
+	    }
+	    if (G_is_d_null_value(&d_ndvi) || 
+		 (input2->answer && G_is_d_null_value(&d_albedo)) || 
+		 (input3->answer && G_is_d_null_value(&d_ref7))) {
+		G_set_c_null_value(&outrast1[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* calculate water detection        */ 
+		    if (input2->answer) {
+		    d = water(d_albedo, d_ndvi);
+		}
+		else if (input3->answer) {
+		    d = water_modis(d_ref7, d_ndvi);
+		}
+		outrast1[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_ndvi);
+    G_close_cell(infd_ndvi);
+    if (input2->answer) {
+	G_free(inrast_albedo);
+	G_close_cell(infd_albedo);
+    }
+    if (input3->answer) {
+	G_free(inrast_ref7);
+	G_close_cell(infd_ref7);
+    }
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/i.water/water.c
===================================================================
--- grass-addons/gipe/i.water/water.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.water/water.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,12 +2,13 @@
 
 int water(double albedo, double ndvi)
 {
-	double result;
-	if (albedo<0.1&&ndvi<0.1){
-		result = 1 ;
-	} else {
-		result = 0 ;
-	}
-	return result;
+    double result;
+
+    if (albedo < 0.1 && ndvi < 0.1) {
+	result = 1;
+    }
+    else {
+	result = 0;
+    }
+    return result;
 }
-

Modified: grass-addons/gipe/i.water/water_modis.c
===================================================================
--- grass-addons/gipe/i.water/water_modis.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.water/water_modis.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,12 +2,13 @@
 
 int water_modis(double surf_ref_7, double ndvi)
 {
-	double result;
-	if (surf_ref_7<0.04&&ndvi<0.1){
-		result = 1 ;
-	} else {
-		result = 0 ;
-	}
-	return result;
+    double result;
+
+    if (surf_ref_7 < 0.04 && ndvi < 0.1) {
+	result = 1;
+    }
+    else {
+	result = 0;
+    }
+    return result;
 }
-

Modified: grass-addons/gipe/i.wi/lswi.c
===================================================================
--- grass-addons/gipe/i.wi/lswi.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.wi/lswi.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,25 +1,24 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/* Land Surface Water Index (LWSI)
- * a kind of Normalized Difference Water Index
- * Xiao X., Boles S., Frolking S., Salas W., Moore B., Li C., et al. (2002)
- * Landscape-scale characterization of cropland in China using vegetation and Landsat TM images.
- * International Journal of Remote Sensing, 23:3579-3594.
- */
-
-double ls_wi( double nirchan, double swirchan )
-{
-	double result;
-	
-	if( ( nirchan + swirchan ) ==  0.0 ){
-		result = -1.0;
-	} else {
-		result = ( nirchan - swirchan ) / ( nirchan + swirchan );
-	}
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /* Land Surface Water Index (LWSI)
+     * a kind of Normalized Difference Water Index
+     * Xiao X., Boles S., Frolking S., Salas W., Moore B., Li C., et al. (2002)
+     * Landscape-scale characterization of cropland in China using vegetation and Landsat TM images.
+     * International Journal of Remote Sensing, 23:3579-3594.
+     */ 
+double ls_wi(double nirchan, double swirchan) 
+{
+    double result;
+
+    if ((nirchan + swirchan) == 0.0) {
+	result = -1.0;
+    }
+    else {
+	result = (nirchan - swirchan) / (nirchan + swirchan);
+    }
+    return result;
+}
+
+

Modified: grass-addons/gipe/i.wi/main.c
===================================================================
--- grass-addons/gipe/i.wi/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/i.wi/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,192 +1,220 @@
-/****************************************************************************
- *
- * MODULE:       i.wi
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Calculates water indices 
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- * 
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double ls_wi( double nirchan, double swirchan );
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	char *wiflag;/*Switch for particular index*/
-	
-	struct GModule *module;
-	struct Option *input1,*input2,*input3, *output;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	struct Colors colors; /*Color rules*/	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_nirchan, infd_swirchan;
-	int outfd;
-	
-	char  *nirchan, *swirchan;
-	
-	int i=0,j=0;
-	
-	void  *inrast_nirchan, *inrast_swirchan;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_nirchan;
-	RASTER_MAP_TYPE data_type_swirchan;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("Water index, biophysical parameters");
-	module->description = _("Water indices from nir and swir for the time being");
-
-	/* Define the different options */
-	input1 = G_define_option() ;
-	input1->key        =_("winame");
-	input1->type       = TYPE_STRING;
-	input1->required   = YES;
-	input1->gisprompt  =_("Name of WI");
-	input1->description=_("Name of WI: lswi");
-	input1->answer     =_("lswi");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("nir");
-	input2->description=_("Name of the NIR Channel surface reflectance map [0.0;1.0]");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("swir");
-	input3->description=_("Name of the SWIR Channel surface reflectance map [0.0;1.0]");
-
-	output= G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->description=_("Name of the output wi layer");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-	wiflag		= input1->answer;
-	nirchan	 	= input2->answer;
-	swirchan 	= input3->answer;
-
-	result  = output->answer;
-	/***************************************************/
-	mapset = G_find_cell2 (nirchan, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"),nirchan);
-	}
-	data_type_nirchan = G_raster_map_type(nirchan,mapset);
-	if ( (infd_nirchan = G_open_cell_old (nirchan,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
-	if (G_get_cellhd (nirchan, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
-	inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
-	/***************************************************/
-	mapset = G_find_cell2(swirchan, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), swirchan);
-	}
-	data_type_swirchan = G_raster_map_type(swirchan,mapset);
-	if ( (infd_swirchan = G_open_cell_old (swirchan,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), swirchan);
-	if (G_get_cellhd (swirchan, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), swirchan);
-	inrast_swirchan = G_allocate_raster_buf(data_type_swirchan);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_nirchan;
-		DCELL d_swirchan;
-		G_percent(row,nrows,2);
-		if(G_get_raster_row(infd_nirchan,inrast_nirchan,row,data_type_nirchan)<0)
-			G_fatal_error(_("Could not read from <%s>"),nirchan);
-		if(G_get_raster_row(infd_swirchan,inrast_swirchan,row,data_type_swirchan)<0)
-			G_fatal_error(_("Could not read from <%s>"),swirchan);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_nirchan){
-				case CELL_TYPE:
-					d_nirchan = (double) ((CELL *) inrast_nirchan)[col];
-					break;
-				case FCELL_TYPE:
-					d_nirchan = (double) ((FCELL *) inrast_nirchan)[col];
-					break;
-				case DCELL_TYPE:
-					d_nirchan = ((DCELL *) inrast_nirchan)[col];
-					break;
-			}
-			switch(data_type_swirchan){
-				case CELL_TYPE:
-					d_swirchan = (double) ((CELL *) inrast_swirchan)[col];
-					break;
-				case FCELL_TYPE:
-					d_swirchan = (double) ((FCELL *) inrast_swirchan)[col];
-					break;
-				case DCELL_TYPE:
-					d_swirchan = ((DCELL *) inrast_swirchan)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_nirchan)||
-			G_is_d_null_value(&d_swirchan)){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				/*calculate lswi	            */
-				if (!strcoll(wiflag,"lswi")){
-					if(d_nirchan+d_swirchan<0.001){
-						G_set_d_null_value(&outrast[col],1);
-					} else {
-						d =  ls_wi(d_nirchan,d_swirchan );
-						((DCELL *) outrast)[col] = d;
-					}
-				}
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	
-	G_free(inrast_nirchan);
-	G_close_cell(infd_nirchan);
-	G_free(inrast_swirchan);
-	G_close_cell(infd_swirchan);
-	G_free(outrast);
-	G_close_cell(outfd);
-
-	/* Color from -1.0 to +1.0 in grey */
-	G_init_colors(&colors);
-	G_add_color_rule(-1.0,0,0,0,1.0,255,255,255,&colors);
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       i.wi
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates water indices 
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ * 
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double ls_wi(double nirchan, double swirchan);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    char *wiflag;		/*Switch for particular index */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *output;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    struct Colors colors;	/*Color rules */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_nirchan, infd_swirchan;
+
+    int outfd;
+
+    char *nirchan, *swirchan;
+
+    int i = 0, j = 0;
+
+    void *inrast_nirchan, *inrast_swirchan;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_nirchan;
+    RASTER_MAP_TYPE data_type_swirchan;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("Water index, biophysical parameters");
+    module->description =
+	_("Water indices from nir and swir for the time being");
+    
+	/* Define the different options */ 
+	input1 = G_define_option();
+    input1->key = _("winame");
+    input1->type = TYPE_STRING;
+    input1->required = YES;
+    input1->gisprompt = _("Name of WI");
+    input1->description = _("Name of WI: lswi");
+    input1->answer = _("lswi");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("nir");
+    input2->description =
+	_("Name of the NIR Channel surface reflectance map [0.0;1.0]");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("swir");
+    input3->description =
+	_("Name of the SWIR Channel surface reflectance map [0.0;1.0]");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->description = _("Name of the output wi layer");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    wiflag = input1->answer;
+    nirchan = input2->answer;
+    swirchan = input3->answer;
+    result = output->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(nirchan, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), nirchan);
+    }
+    data_type_nirchan = G_raster_map_type(nirchan, mapset);
+    if ((infd_nirchan = G_open_cell_old(nirchan, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), nirchan);
+    if (G_get_cellhd(nirchan, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), nirchan);
+    inrast_nirchan = G_allocate_raster_buf(data_type_nirchan);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(swirchan, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), swirchan);
+    }
+    data_type_swirchan = G_raster_map_type(swirchan, mapset);
+    if ((infd_swirchan = G_open_cell_old(swirchan, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), swirchan);
+    if (G_get_cellhd(swirchan, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), swirchan);
+    inrast_swirchan = G_allocate_raster_buf(data_type_swirchan);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_nirchan;
+	DCELL d_swirchan;
+	G_percent(row, nrows, 2);
+	if (G_get_raster_row
+	     (infd_nirchan, inrast_nirchan, row, data_type_nirchan) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), nirchan);
+	if (G_get_raster_row
+	     (infd_swirchan, inrast_swirchan, row, data_type_swirchan) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), swirchan);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_nirchan) {
+	    case CELL_TYPE:
+		d_nirchan = (double)((CELL *) inrast_nirchan)[col];
+		break;
+	    case FCELL_TYPE:
+		d_nirchan = (double)((FCELL *) inrast_nirchan)[col];
+		break;
+	    case DCELL_TYPE:
+		d_nirchan = ((DCELL *) inrast_nirchan)[col];
+		break;
+	    }
+	    switch (data_type_swirchan) {
+	    case CELL_TYPE:
+		d_swirchan = (double)((CELL *) inrast_swirchan)[col];
+		break;
+	    case FCELL_TYPE:
+		d_swirchan = (double)((FCELL *) inrast_swirchan)[col];
+		break;
+	    case DCELL_TYPE:
+		d_swirchan = ((DCELL *) inrast_swirchan)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_nirchan) ||
+		 G_is_d_null_value(&d_swirchan)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+		    /*calculate lswi                    */ 
+		    if (!strcoll(wiflag, "lswi")) {
+		    if (d_nirchan + d_swirchan < 0.001) {
+			G_set_d_null_value(&outrast[col], 1);
+		    }
+		    else {
+			d = ls_wi(d_nirchan, d_swirchan);
+			((DCELL *) outrast)[col] = d;
+		    }
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_nirchan);
+    G_close_cell(infd_nirchan);
+    G_free(inrast_swirchan);
+    G_close_cell(infd_swirchan);
+    G_free(outrast);
+    G_close_cell(outfd);
+    
+	/* Color from -1.0 to +1.0 in grey */ 
+	G_init_colors(&colors);
+    G_add_color_rule(-1.0, 0, 0, 0, 1.0, 255, 255, 255, &colors);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.evapo.MH/main.c
===================================================================
--- grass-addons/gipe/r.evapo.MH/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.evapo.MH/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 *
 * MODULE:	r.evapo.MH
@@ -24,329 +25,356 @@
 #include <grass/raster.h>
 #include <grass/glocale.h>
 
-double mh_original(double ra,double tavg,double tmax,double tmin,double p);
-double mh_eto(double ra,double tavg,double tmax,double tmin,double p);
-double mh_samani(double ra,double tavg,double tmax,double tmin);
+double mh_original(double ra, double tavg, double tmax, double tmin,
+		   double p);
+double mh_eto(double ra, double tavg, double tmax, double tmin, double p);
 
+double mh_samani(double ra, double tavg, double tmax, double tmin);
+
 int main(int argc, char *argv[])
-{	
-	/* buffer for input-output rasters */
-	void *inrast_TEMPKAVG,*inrast_TEMPKMIN, *inrast_TEMPKMAX, *inrast_RNET,*inrast_P;
-	
-	DCELL *outrast;
-	
-	/* pointers to input-output raster files */
-	int infd_TEMPKAVG,infd_TEMPKMIN,infd_TEMPKMAX,infd_RNET,infd_P;
-	
-	int outfd;
+{
+    /* buffer for input-output rasters */
+    void *inrast_TEMPKAVG, *inrast_TEMPKMIN, *inrast_TEMPKMAX, *inrast_RNET,
+	*inrast_P;
 
-	/* mapsets for input raster files */
-	char *mapset_TEMPKAVG,*mapset_TEMPKMIN,*mapset_TEMPKMAX,*mapset_RNET,*mapset_P;
+    DCELL *outrast;
 
-	/* names of input-output raster files */
-	char *RNET, *TEMPKAVG, *TEMPKMIN, *TEMPKMAX, *P;
-	
-	char *ETa; 
+    /* pointers to input-output raster files */
+    int infd_TEMPKAVG, infd_TEMPKMIN, infd_TEMPKMAX, infd_RNET, infd_P;
 
-	/* input-output cell values */
-	DCELL d_tempkavg, d_tempkmin, d_tempkmax, d_rnet, d_p;
-	DCELL d_daily_et;
+    int outfd;
 
+    /* mapsets for input raster files */
+    char *mapset_TEMPKAVG, *mapset_TEMPKMIN, *mapset_TEMPKMAX, *mapset_RNET,
+	*mapset_P;
 
-	/* region informations and handler */
-	struct Cell_head cellhd;
-	int nrows, ncols;
-	int row, col;
+    /* names of input-output raster files */
+    char *RNET, *TEMPKAVG, *TEMPKMIN, *TEMPKMAX, *P;
 
-	/* parser stuctures definition */
-	struct GModule *module;
-	struct Option *input_RNET,*input_TEMPKAVG, *input_TEMPKMIN;
-	struct Option *input_TEMPKMAX, *input_P;
-	struct Option *output;
-	struct Flag *zero, *original, *samani;
-	struct Colors color;
-	struct History history;
+    char *ETa;
 
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_tempkavg;
-	RASTER_MAP_TYPE data_type_tempkmin;
-	RASTER_MAP_TYPE data_type_tempkmax;
-	RASTER_MAP_TYPE data_type_rnet;
-	RASTER_MAP_TYPE data_type_p;
-	RASTER_MAP_TYPE data_type_eta;
+    /* input-output cell values */
+    DCELL d_tempkavg, d_tempkmin, d_tempkmax, d_rnet, d_p;
 
-	/* Initialize the GIS calls */
-	G_gisinit(argv[0]);
-	
-	module = G_define_module();
-	module->description =
-		_("Evapotranspiration Calculation "
-		"Modified Hargreaves formulation, 2001."
-		"Flag for Original Hargreaves (1985).");
-	
-	/* Define different options */
-	input_RNET = G_define_standard_option(G_OPT_R_INPUT);
-	input_RNET->key			= "rnetd";
-	input_RNET->key_desc		= "[W/m2/d]";
-	input_RNET->description 	= _("Name of Diurnal Net Radiation raster map");
-	
-	input_TEMPKAVG = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPKAVG->key		= "tempkavg";
-	input_TEMPKAVG->key_desc	= "[C]";
-	input_TEMPKAVG->description	= _("Name of avg air temperature raster map");
-		
-	input_TEMPKMIN = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPKMIN->key		= "tempkmin";
-	input_TEMPKMIN->key_desc	= "[C]";
-	input_TEMPKMIN->description	= _("Name of min air temperature raster map");
-		
-	input_TEMPKMAX = G_define_standard_option(G_OPT_R_INPUT);
-	input_TEMPKMAX->key		= "TEMPKMAX";
-	input_TEMPKMAX->key_desc	= "[C]";
-	input_TEMPKMAX->description	= _("Name of max air temperature raster map");
-		
-	input_P = G_define_standard_option(G_OPT_R_INPUT);
-	input_P->key			= "p";
-	input_P->key_desc		= "[mm/month]";
-	input_P->description		= _("Name of precipitation raster map, disabled if original Hargreaves (1985) is enabled.");
-	
-	output = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key_desc		= "[mm/d]";
-	output->description		= _("Name of output Ref Evapotranspiration layer");
-	
-	/* Define the different flags */
-	zero = G_define_flag() ;
-	zero->key			= 'z' ;
-	zero->description		= _("set negative ETa to zero");
-	
-	original = G_define_flag() ;
-	original->key			= 'h' ;
-	original->description		= _("set to original Hargreaves (1985)");
+    DCELL d_daily_et;
 
-	samani = G_define_flag() ;
-	samani->key			= 's' ;
-	samani->description		= _("set to Hargreaves-Samani (1985)");
 
-	if (G_parser(argc, argv))
-		exit(EXIT_FAILURE);
-	
-	/* get entered parameters */
-	RNET		= input_RNET->answer;
-	TEMPKAVG	= input_TEMPKAVG->answer;
-	TEMPKMIN	= input_TEMPKMIN->answer;
-	TEMPKMAX	= input_TEMPKMAX->answer;
-	P		= input_P->answer;
-	
-	ETa	= output->answer;
-	
-	/* find maps in mapset */
-	mapset_RNET = G_find_cell2 (RNET, "");
-	if (mapset_RNET == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), RNET);
-	mapset_TEMPKAVG = G_find_cell2 (TEMPKAVG, "");
-	if (mapset_TEMPKAVG == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPKAVG);
-	mapset_TEMPKMIN = G_find_cell2 (TEMPKMIN, "");
-	if (mapset_TEMPKMIN == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPKMIN);
-	mapset_TEMPKMAX = G_find_cell2 (TEMPKMAX, "");
-	if (mapset_TEMPKMAX == NULL)
-	        G_fatal_error (_("cell file [%s] not found"), TEMPKMAX);
-	if(!original->answer){
-		mapset_P = G_find_cell2 (P, "");
-		if (mapset_P == NULL)
-	       		G_fatal_error (_("cell file [%s] not found"), P);
+    /* region informations and handler */
+    struct Cell_head cellhd;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    /* parser stuctures definition */
+    struct GModule *module;
+
+    struct Option *input_RNET, *input_TEMPKAVG, *input_TEMPKMIN;
+
+    struct Option *input_TEMPKMAX, *input_P;
+
+    struct Option *output;
+
+    struct Flag *zero, *original, *samani;
+
+    struct Colors color;
+
+    struct History history;
+
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+
+    RASTER_MAP_TYPE data_type_tempkavg;
+
+    RASTER_MAP_TYPE data_type_tempkmin;
+
+    RASTER_MAP_TYPE data_type_tempkmax;
+
+    RASTER_MAP_TYPE data_type_rnet;
+
+    RASTER_MAP_TYPE data_type_p;
+
+    RASTER_MAP_TYPE data_type_eta;
+
+    /* Initialize the GIS calls */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description =
+	_("Evapotranspiration Calculation "
+	  "Modified Hargreaves formulation, 2001."
+	  "Flag for Original Hargreaves (1985).");
+
+    /* Define different options */
+    input_RNET = G_define_standard_option(G_OPT_R_INPUT);
+    input_RNET->key = "rnetd";
+    input_RNET->key_desc = "[W/m2/d]";
+    input_RNET->description = _("Name of Diurnal Net Radiation raster map");
+
+    input_TEMPKAVG = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPKAVG->key = "tempkavg";
+    input_TEMPKAVG->key_desc = "[C]";
+    input_TEMPKAVG->description = _("Name of avg air temperature raster map");
+
+    input_TEMPKMIN = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPKMIN->key = "tempkmin";
+    input_TEMPKMIN->key_desc = "[C]";
+    input_TEMPKMIN->description = _("Name of min air temperature raster map");
+
+    input_TEMPKMAX = G_define_standard_option(G_OPT_R_INPUT);
+    input_TEMPKMAX->key = "TEMPKMAX";
+    input_TEMPKMAX->key_desc = "[C]";
+    input_TEMPKMAX->description = _("Name of max air temperature raster map");
+
+    input_P = G_define_standard_option(G_OPT_R_INPUT);
+    input_P->key = "p";
+    input_P->key_desc = "[mm/month]";
+    input_P->description =
+	_("Name of precipitation raster map, disabled if original Hargreaves (1985) is enabled.");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key_desc = "[mm/d]";
+    output->description = _("Name of output Ref Evapotranspiration layer");
+
+    /* Define the different flags */
+    zero = G_define_flag();
+    zero->key = 'z';
+    zero->description = _("set negative ETa to zero");
+
+    original = G_define_flag();
+    original->key = 'h';
+    original->description = _("set to original Hargreaves (1985)");
+
+    samani = G_define_flag();
+    samani->key = 's';
+    samani->description = _("set to Hargreaves-Samani (1985)");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get entered parameters */
+    RNET = input_RNET->answer;
+    TEMPKAVG = input_TEMPKAVG->answer;
+    TEMPKMIN = input_TEMPKMIN->answer;
+    TEMPKMAX = input_TEMPKMAX->answer;
+    P = input_P->answer;
+
+    ETa = output->answer;
+
+    /* find maps in mapset */
+    mapset_RNET = G_find_cell2(RNET, "");
+    if (mapset_RNET == NULL)
+	G_fatal_error(_("cell file [%s] not found"), RNET);
+    mapset_TEMPKAVG = G_find_cell2(TEMPKAVG, "");
+    if (mapset_TEMPKAVG == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPKAVG);
+    mapset_TEMPKMIN = G_find_cell2(TEMPKMIN, "");
+    if (mapset_TEMPKMIN == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPKMIN);
+    mapset_TEMPKMAX = G_find_cell2(TEMPKMAX, "");
+    if (mapset_TEMPKMAX == NULL)
+	G_fatal_error(_("cell file [%s] not found"), TEMPKMAX);
+    if (!original->answer) {
+	mapset_P = G_find_cell2(P, "");
+	if (mapset_P == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), P);
+    }
+    /* check legal output name */
+    if (G_legal_filename(ETa) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), ETa);
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_rnet = G_raster_map_type(RNET, mapset_RNET);
+    data_type_tempkavg = G_raster_map_type(TEMPKAVG, mapset_TEMPKAVG);
+    data_type_tempkmin = G_raster_map_type(TEMPKMIN, mapset_TEMPKMIN);
+    data_type_tempkmax = G_raster_map_type(TEMPKMAX, mapset_TEMPKMAX);
+    if (!original->answer) {
+	data_type_p = G_raster_map_type(P, mapset_P);
+    }
+    /* open pointers to input raster files */
+    if ((infd_RNET = G_open_cell_old(RNET, mapset_RNET)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), RNET);
+    if ((infd_TEMPKAVG = G_open_cell_old(TEMPKAVG, mapset_TEMPKAVG)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPKAVG);
+    if ((infd_TEMPKMIN = G_open_cell_old(TEMPKMIN, mapset_TEMPKMIN)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPKMIN);
+    if ((infd_TEMPKMAX = G_open_cell_old(TEMPKMAX, mapset_TEMPKMAX)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), TEMPKMAX);
+    if (!original->answer) {
+	if ((infd_P = G_open_cell_old(P, mapset_P)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), P);
+    }
+    /* read headers of raster files */
+    if (G_get_cellhd(RNET, mapset_RNET, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), RNET);
+    if (G_get_cellhd(TEMPKAVG, mapset_TEMPKAVG, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPKAVG);
+    if (G_get_cellhd(TEMPKMIN, mapset_TEMPKMIN, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPKMIN);
+    if (G_get_cellhd(TEMPKMAX, mapset_TEMPKMAX, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), TEMPKMAX);
+    if (!original->answer) {
+	if (G_get_cellhd(P, mapset_P, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s]"), P);
+    }
+    /* Allocate input buffer */
+    inrast_RNET = G_allocate_raster_buf(data_type_rnet);
+    inrast_TEMPKAVG = G_allocate_raster_buf(data_type_tempkavg);
+    inrast_TEMPKMIN = G_allocate_raster_buf(data_type_tempkmin);
+    inrast_TEMPKMAX = G_allocate_raster_buf(data_type_tempkmax);
+    if (!original->answer) {
+	inrast_P = G_allocate_raster_buf(data_type_p);
+    }
+    /* get rows and columns number of the current region */
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
+    /* allocate output buffer */
+    outrast = G_allocate_raster_buf(data_type_output);
+
+    /* open pointers to output raster files */
+    if ((outfd = G_open_raster_new(ETa, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), ETa);
+
+
+    /* start the loop through cells */
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+	/* read input raster row into line buffer */
+	if (G_get_raster_row(infd_RNET, inrast_RNET, row, data_type_rnet) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), RNET);
+	if (G_get_raster_row
+	    (infd_TEMPKAVG, inrast_TEMPKAVG, row, data_type_tempkavg) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPKAVG);
+	if (G_get_raster_row
+	    (infd_TEMPKMIN, inrast_TEMPKMIN, row, data_type_tempkmin) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPKMIN);
+	if (G_get_raster_row
+	    (infd_TEMPKMAX, inrast_TEMPKMAX, row, data_type_tempkmax) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), TEMPKMAX);
+	if (!original->answer) {
+	    if (G_get_raster_row(infd_P, inrast_P, row, data_type_p) < 0)
+		G_fatal_error(_("Could not read from <%s>"), P);
 	}
-	/* check legal output name */ 
-	if (G_legal_filename (ETa) < 0)
-			G_fatal_error (_("[%s] is an illegal name"), ETa);
-		
-	/* determine the input map type (CELL/FCELL/DCELL) */
-	data_type_rnet = G_raster_map_type(RNET,mapset_RNET);
-	data_type_tempkavg = G_raster_map_type(TEMPKAVG,mapset_TEMPKAVG);
-	data_type_tempkmin = G_raster_map_type(TEMPKMIN,mapset_TEMPKMIN);
-	data_type_tempkmax = G_raster_map_type(TEMPKMAX,mapset_TEMPKMAX);
-	if(!original->answer){
-		data_type_p = G_raster_map_type(P,mapset_P);
+	for (col = 0; col < ncols; col++) {
+	    /* read current cell from line buffer */
+	    switch (data_type_rnet) {
+	    case CELL_TYPE:
+		d_rnet = (double)((CELL *) inrast_RNET)[col];
+		break;
+	    case FCELL_TYPE:
+		d_rnet = (double)((FCELL *) inrast_RNET)[col];
+		break;
+	    case DCELL_TYPE:
+		d_rnet = ((DCELL *) inrast_RNET)[col];
+		break;
+	    }
+	    switch (data_type_tempkavg) {
+	    case CELL_TYPE:
+		d_tempkavg = (double)((CELL *) inrast_TEMPKAVG)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempkavg = (double)((FCELL *) inrast_TEMPKAVG)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempkavg = ((DCELL *) inrast_TEMPKAVG)[col];
+		break;
+	    }
+	    switch (data_type_tempkmin) {
+	    case CELL_TYPE:
+		d_tempkmin = (double)((CELL *) inrast_TEMPKMIN)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempkmin = (double)((FCELL *) inrast_TEMPKMIN)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempkmin = ((DCELL *) inrast_TEMPKMIN)[col];
+		break;
+	    }
+	    switch (data_type_tempkmax) {
+	    case CELL_TYPE:
+		d_tempkmax = (double)((CELL *) inrast_TEMPKMAX)[col];
+		break;
+	    case FCELL_TYPE:
+		d_tempkmax = (double)((FCELL *) inrast_TEMPKMAX)[col];
+		break;
+	    case DCELL_TYPE:
+		d_tempkmax = ((DCELL *) inrast_TEMPKMAX)[col];
+		break;
+	    }
+	    if (!original->answer) {
+		switch (data_type_p) {
+		case CELL_TYPE:
+		    d_p = (double)((CELL *) inrast_P)[col];
+		    break;
+		case FCELL_TYPE:
+		    d_p = (double)((FCELL *) inrast_P)[col];
+		    break;
+		case DCELL_TYPE:
+		    d_p = ((DCELL *) inrast_P)[col];
+		    break;
+		}
+	    }
+	    if (G_is_d_null_value(&d_rnet) ||
+		G_is_d_null_value(&d_tempkavg) ||
+		G_is_d_null_value(&d_tempkmin) ||
+		G_is_d_null_value(&d_tempkmax) || G_is_d_null_value(&d_p)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		if (original->answer) {
+		    d_daily_et =
+			mh_original(d_rnet, d_tempkavg, d_tempkmax,
+				    d_tempkmin, d_p);
+		}
+		else if (samani->answer) {
+		    d_daily_et =
+			mh_samani(d_rnet, d_tempkavg, d_tempkmax, d_tempkmin);
+		}
+		else {
+		    d_daily_et =
+			mh_eto(d_rnet, d_tempkavg, d_tempkmax, d_tempkmin,
+			       d_p);
+		}
+		if (zero->answer && d_daily_et < 0)
+		    d_daily_et = 0.0;
+		/* write calculated ETP to output line buffer */
+		outrast[col] = d_daily_et;
+	    }
 	}
-	/* open pointers to input raster files */
-	if ( (infd_RNET = G_open_cell_old (RNET, mapset_RNET)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), RNET);
-	if ( (infd_TEMPKAVG = G_open_cell_old (TEMPKAVG, mapset_TEMPKAVG)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPKAVG);
-	if ( (infd_TEMPKMIN = G_open_cell_old (TEMPKMIN, mapset_TEMPKMIN)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPKMIN);
-	if ( (infd_TEMPKMAX = G_open_cell_old (TEMPKMAX, mapset_TEMPKMAX)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), TEMPKMAX);
-	if(!original->answer){
-		if ( (infd_P = G_open_cell_old (P, mapset_P)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), P);
-	}
-	/* read headers of raster files */
-	if (G_get_cellhd (RNET, mapset_RNET, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), RNET);
-	if (G_get_cellhd (TEMPKAVG, mapset_TEMPKAVG, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPKAVG);
-	if (G_get_cellhd (TEMPKMIN, mapset_TEMPKMIN, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPKMIN);
-	if (G_get_cellhd (TEMPKMAX, mapset_TEMPKMAX, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s]"), TEMPKMAX);
-	if(!original->answer){
-		if (G_get_cellhd (P, mapset_P, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s]"), P);
-	}
-	/* Allocate input buffer */
-	inrast_RNET  = G_allocate_raster_buf(data_type_rnet);
-	inrast_TEMPKAVG = G_allocate_raster_buf(data_type_tempkavg);
-	inrast_TEMPKMIN = G_allocate_raster_buf(data_type_tempkmin);
-	inrast_TEMPKMAX	= G_allocate_raster_buf(data_type_tempkmax);
-	if(!original->answer){
-		inrast_P = G_allocate_raster_buf(data_type_p);
-	}
-	/* get rows and columns number of the current region */
-	nrows = G_window_rows();
-	ncols = G_window_cols();
 
-	/* allocate output buffer */
-	outrast = G_allocate_raster_buf(data_type_output);
 
-	/* open pointers to output raster files */
-	if ( (outfd = G_open_raster_new (ETa,data_type_output)) < 0)
-		G_fatal_error (_("Could not open <%s>"),ETa);
-	
+	/* write output line buffer to output raster file */
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to <%s>"), ETa);
 
-	/* start the loop through cells */
-	for (row = 0; row < nrows; row++)
-	{
-		G_percent(row, nrows, 2);
-		/* read input raster row into line buffer*/	
-		if (G_get_raster_row (infd_RNET, inrast_RNET, row,data_type_rnet) < 0)
-			G_fatal_error (_("Could not read from <%s>"),RNET);
-		if (G_get_raster_row (infd_TEMPKAVG, inrast_TEMPKAVG, row,data_type_tempkavg) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPKAVG);
-		if (G_get_raster_row (infd_TEMPKMIN, inrast_TEMPKMIN, row,data_type_tempkmin) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPKMIN);
-		if (G_get_raster_row (infd_TEMPKMAX, inrast_TEMPKMAX, row,data_type_tempkmax) < 0)
-			G_fatal_error (_("Could not read from <%s>"),TEMPKMAX);
-		if(!original->answer){
-			if (G_get_raster_row (infd_P, inrast_P, row,data_type_p) < 0)
-				G_fatal_error (_("Could not read from <%s>"),P);
-		}	
-		for (col=0; col < ncols; col++)
-		{
-			/* read current cell from line buffer */
-			switch(data_type_rnet){
-				case CELL_TYPE:
-					d_rnet	= (double) ((CELL *) inrast_RNET)[col];
-					break;
-				case FCELL_TYPE:
-					d_rnet	= (double) ((FCELL *) inrast_RNET)[col];
-					break;
-				case DCELL_TYPE:
-					d_rnet	= ((DCELL *) inrast_RNET)[col];
-					break;
-			}
-			switch(data_type_tempkavg){
-				case CELL_TYPE:
-					d_tempkavg = (double) ((CELL *) inrast_TEMPKAVG)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempkavg = (double) ((FCELL *) inrast_TEMPKAVG)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempkavg = ((DCELL *) inrast_TEMPKAVG)[col];
-					break;
-			}
-			switch(data_type_tempkmin){
-				case CELL_TYPE:
-					d_tempkmin = (double) ((CELL *) inrast_TEMPKMIN)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempkmin = (double) ((FCELL *) inrast_TEMPKMIN)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempkmin = ((DCELL *) inrast_TEMPKMIN)[col];
-					break;
-			}
-			switch(data_type_tempkmax){
-				case CELL_TYPE:
-					d_tempkmax = (double) ((CELL *) inrast_TEMPKMAX)[col];
-					break;
-				case FCELL_TYPE:
-					d_tempkmax = (double) ((FCELL *) inrast_TEMPKMAX)[col];
-					break;
-				case DCELL_TYPE:
-					d_tempkmax = ((DCELL *) inrast_TEMPKMAX)[col];
-					break;
-			}
-			if(!original->answer){
-				switch(data_type_p){
-					case CELL_TYPE:
-						d_p = (double) ((CELL *) inrast_P)[col];
-						break;
-					case FCELL_TYPE:
-						d_p = (double) ((FCELL *) inrast_P)[col];
-						break;
-					case DCELL_TYPE:
-						d_p = ((DCELL *) inrast_P)[col];
-						break;
-				}
-			}
-			if(G_is_d_null_value(&d_rnet)||
-				G_is_d_null_value(&d_tempkavg)||
-				G_is_d_null_value(&d_tempkmin)||
-				G_is_d_null_value(&d_tempkmax)||
-				G_is_d_null_value(&d_p)){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				if(original->answer){
-					d_daily_et = mh_original( d_rnet, d_tempkavg, d_tempkmax, d_tempkmin, d_p );
-				} else if(samani->answer){
-					d_daily_et = mh_samani( d_rnet, d_tempkavg, d_tempkmax, d_tempkmin );
-				} else {
-					d_daily_et = mh_eto( d_rnet, d_tempkavg, d_tempkmax, d_tempkmin, d_p );
-				}
-				if (zero->answer && d_daily_et<0)
-					d_daily_et=0.0;
-				/* write calculated ETP to output line buffer */
-				outrast[col] = d_daily_et;
-			}
-		}
-		
+    }
+    /* free buffers and close input maps */
 
-		/* write output line buffer to output raster file */
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error (_("Cannot write to <%s>"), ETa);
-			
-	}
-	/* free buffers and close input maps */
+    G_free(inrast_RNET);
+    G_free(inrast_TEMPKAVG);
+    G_free(inrast_TEMPKMIN);
+    G_free(inrast_TEMPKMAX);
+    if (!original->answer) {
+	G_free(inrast_P);
+    }
+    G_close_cell(infd_RNET);
+    G_close_cell(infd_TEMPKAVG);
+    G_close_cell(infd_TEMPKMIN);
+    G_close_cell(infd_TEMPKMAX);
+    if (!original->answer) {
+	G_close_cell(infd_P);
+    }
+    /* generate color table between -20 and 20 */
+    G_make_rainbow_colors(&color, -20, 20);
+    G_write_colors(ETa, G_mapset(), &color);
 
-	G_free(inrast_RNET);
-	G_free(inrast_TEMPKAVG);
-	G_free(inrast_TEMPKMIN);
-	G_free(inrast_TEMPKMAX);
-	if(!original->answer){
-		G_free(inrast_P);
-	}
-	G_close_cell (infd_RNET);
-	G_close_cell (infd_TEMPKAVG);
-	G_close_cell (infd_TEMPKMIN);
-	G_close_cell (infd_TEMPKMAX);
-	if(!original->answer){
-		G_close_cell (infd_P);
-	}	
-	/* generate color table between -20 and 20 */
-	G_make_rainbow_colors(&color, -20, 20);
-	G_write_colors(ETa,G_mapset(),&color);
+    G_short_history(ETa, "raster", &history);
+    G_command_history(&history);
+    G_write_history(ETa, &history);
 
-	G_short_history(ETa,"raster", &history);
-	G_command_history(&history);
-	G_write_history(ETa, &history);
+    /* free buffers and close output map */
+    G_free(outrast);
+    G_close_cell(outfd);
 
-	/* free buffers and close output map */
-	G_free(outrast);
-	G_close_cell (outfd);
-
-	return 0;
+    return 0;
 }
-

Modified: grass-addons/gipe/r.evapo.MH/mh_eto.c
===================================================================
--- grass-addons/gipe/r.evapo.MH/mh_eto.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.evapo.MH/mh_eto.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,24 +1,21 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*Droogers and Allen, 2001.*/ 
-/*p is [mm/month]*/
-
-double mh_eto(double ra,double tavg,double tmax,double tmin,double p)
-{
-	double 	td, result;
-	
-	td = tmax - tmin;
-
-	if (tavg > 100.0){
-		tavg=tavg-273.15;/*in case temperature is in Kelvin*/
-	}
-
-	ra = ra * (84600.0 * 1000.0); /*convert W -> MJ/d*/
-
-	result = 0.0013 * 0.408 * ra * ( tavg + 17.0 ) * pow((td - 0.0123*p),0.76) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*Droogers and Allen, 2001. */ 
+    /*p is [mm/month] */ 
+double mh_eto(double ra, double tavg, double tmax, double tmin, double p) 
+{
+    double td, result;
+
+    td = tmax - tmin;
+    if (tavg > 100.0) {
+	tavg = tavg - 273.15;	/*in case temperature is in Kelvin */
+    }
+    ra = ra * (84600.0 * 1000.0);	/*convert W -> MJ/d */
+    result =
+	0.0013 * 0.408 * ra * (tavg + 17.0) * pow((td - 0.0123 * p), 0.76);
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.evapo.MH/mh_original.c
===================================================================
--- grass-addons/gipe/r.evapo.MH/mh_original.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.evapo.MH/mh_original.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,23 +1,20 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*Hargreaves et al, 1985.*/ 
-
-double mh_original(double ra,double tavg,double tmax,double tmin,double p)
-{
-	double 	td, result;
-	
-	td = tmax - tmin;
-
-	if (tavg > 100.0){
-		tavg=tavg-273.15;/*in case Temperature is in Kelvin*/
-	}
-
-	ra = ra * (84600.0 * 1000.0); /*convert W -> MJ/d*/
-
-	result = 0.0023 * 0.408 * ra * ( tavg + 17.8 ) * pow(td,0.5) ;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*Hargreaves et al, 1985. */ 
+double mh_original(double ra, double tavg, double tmax, double tmin,
+		    double p) 
+{
+    double td, result;
+
+    td = tmax - tmin;
+    if (tavg > 100.0) {
+	tavg = tavg - 273.15;	/*in case Temperature is in Kelvin */
+    }
+    ra = ra * (84600.0 * 1000.0);	/*convert W -> MJ/d */
+    result = 0.0023 * 0.408 * ra * (tavg + 17.8) * pow(td, 0.5);
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.evapo.MH/mh_samani.c
===================================================================
--- grass-addons/gipe/r.evapo.MH/mh_samani.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.evapo.MH/mh_samani.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,23 +1,21 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-/*Hargreaves-Samani, 1985. */
-
-double mh_samani(double ra,double tavg,double tmax,double tmin)
-{
-	double 	td, result;
-	
-	td = tmax - tmin;
-
-	if (tavg > 100.0){
-		tavg=tavg-273.15;/*in case Temperature is in Kelvin*/
-	}
-
-	ra = ra * (84600.0 * 1000.0); /* convert W -> MJ/d*/
-
-	result = 0.0023 * 0.408 * ra * pow(td,0.5) * ((tmax+tmin)/2+17.8)/2.45;
-	
-	return result;
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+
+    /*Hargreaves-Samani, 1985. */ 
+double mh_samani(double ra, double tavg, double tmax, double tmin) 
+{
+    double td, result;
+
+    td = tmax - tmin;
+    if (tavg > 100.0) {
+	tavg = tavg - 273.15;	/*in case Temperature is in Kelvin */
+    }
+    ra = ra * (84600.0 * 1000.0);	/* convert W -> MJ/d */
+    result =
+	0.0023 * 0.408 * ra * pow(td,
+				  0.5) * ((tmax + tmin) / 2 + 17.8) / 2.45;
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.gaswap.serial/evaluation.c
===================================================================
--- grass-addons/gipe/r.gaswap.serial/evaluation.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.gaswap.serial/evaluation.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /***************************************************************/
 /* This is a simple genetic algorithm implementation where the */
 /* evaluation function takes positive values only and the      */
@@ -3,12 +4,16 @@
 /* fitness of an individual is the same as the value of the    */
 /* objective function (after Michalewicz, 1996)                */
+
 /***************************************************************/
 /* Added SWAP input file creation - finput() - September 2003  */
+
 /***************************************************************/
-/* Added SWAP output ETa file creation - foutput() - October 03*/
+/* Added SWAP output ETa file creation - foutput() - October 03 */
+
 /***************************************************************/
 /* Copyleft LGPL Yann Chemin - yann.chemin at ait.ac.th 2003      */
 /* ychemin at yahoo.com - Code enhancements welcome!              */
-/* Modified by Shamim Akhter - shamimakhter at gmail.com 2005     */ 
+/* Modified by Shamim Akhter - shamimakhter at gmail.com 2005     */
+
 /***************************************************************/
 
@@ -17,78 +22,103 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <string.h> /*foutput()*/
-#include <time.h>   /*foutput()*/
-#include <unistd.h> /*sleep() in evaluate()*/
+#include <string.h>		/*foutput() */
+#include <time.h>		/*foutput() */
+#include <unistd.h>		/*sleep() in evaluate() */
 #include "gaswap.h"
 
 #define TRUE 1
 #define FALSE 0
 
 
-/*int generation;*/		/* current generation number */
-/*int cur_best;*/		/* best individual */
-/*FILE *galog;*/		
+				/*int generation; *//* current generation number */
+				/*int cur_best; *//* best individual */
+/*FILE *galog; */
 
 
 
 /* the SWAP variables to optimize */
-	int DECd1, DECm1, DECy1;
-	int FECd1, FECm1, FECY1;
-	int STSd1, STSm1, STSy1;
-	int DECdloy1, FECdloy1, STSdloy1;
-/*	int doy;*/ /* used in date2doy1() */
-/*	int day, month;*/ /* used in doy2date1() */
-	int TECdoy1; /*cropping season time extent*/
-	int GWJan1, GWDec1; /* GW level values in 01Jan & 31Dec */
+int DECd1, DECm1, DECy1;
 
+int FECd1, FECm1, FECY1;
 
-/*char *dateName[257];*/ 	/*foutput()*/
-/*char *dateNameValue;*/ 	/*foutput()*/
+int STSd1, STSm1, STSy1;
+
+int DECdloy1, FECdloy1, STSdloy1;
+
+		   /*      int doy; *//* used in date2doy1() */
+			  /*      int day, month; *//* used in doy2date1() */
+int TECdoy1;			/*cropping season time extent */
+
+int GWJan1, GWDec1;		/* GW level values in 01Jan & 31Dec */
+
+
+				/*char *dateName[257]; *//*foutput() */
+				/*char *dateNameValue; *//*foutput() */
 char **satDATE1;
-int dateCounter1;		/*foutput()*/
-double swapETA1[367]; 	/*foutput()*/
-double satETA1[367]; 	/*foutput()*/
+
+int dateCounter1;		/*foutput() */
+
+double swapETA1[367];		/*foutput() */
+
+double satETA1[367];		/*foutput() */
+
 double tex1[4];
 
-/*int POPSIZE;*/ 		/* population size */
-/*int MAXGENS;*/ 		/* max. number of generations */
-/*int NVARS;*/        	/* max. number of variables */  
-/*int NSATEL;*/       	/* max. number of satellite images*/ 
-/*int YEAR;*/
-int SSRUN1,ESRUN1;
-double LAT1,ALT1;
+				/*int POPSIZE; *//* population size */
+				/*int MAXGENS; *//* max. number of generations */
+			/*int NVARS; *//* max. number of variables */
+			/*int NSATEL; *//* max. number of satellite images */
+/*int YEAR; */
+int SSRUN1, ESRUN1;
+
+double LAT1, ALT1;
+
 char METFIL1[100];
 
-/*double PXOVER;*/		/* probability of crossover */
-/*double PMUTATION;*/		/* probability of mutation */
-/*double TARGET_FITNESS;*/  /* fitness value that will stop GA */
+				/*double PXOVER; *//* probability of crossover */
+				/*double PMUTATION; *//* probability of mutation */
+			    /*double TARGET_FITNESS; *//* fitness value that will stop GA */
 
 
 /* Declaration of procedures used by this genetic algorithm */
 
-void evaluate(int popsize, int nvar, int * gene, double *fitness, int DECy1, int nsatel, double text[], double ETA[], char **DATE, int ssrun, int esrun, double lat, double alt, char *metfile);
+void evaluate(int popsize, int nvar, int *gene, double *fitness, int DECy1,
+	      int nsatel, double text[], double ETA[], char **DATE, int ssrun,
+	      int esrun, double lat, double alt, char *metfile);
 int doy2date1(int *d, int *m, int *day, int *month);
+
 int date2doy1(int *d, int *m);
 
 
-/*void finput1(void);*/
-/*void finput2(void);*/
+/*void finput1(void); */
+/*void finput2(void); */
 void swapkey1(void);
+
 void calfil1(void);
+
 void capfil1(void);
+
 void swafil1(void);
+
 void bbcfil1(void);
+
 void crpfil1(void);
+
 void sol1fil1(void);
+
 void sol2fil1(void);
+
 void sol3fil1(void);
+
 void sol4fil1(void);
+
 void sol5fil1(void);
 
-/*int foutput(void);*/
-/*int get_sat_eta(void);*/
+/*int foutput(void); */
+/*int get_sat_eta(void); */
 int satellite_dates1(int nsatel);
+
 int get_eta1(char *dateName, char *dateNameValue, int dateCounter1);
 
 /**************************************************************/
@@ -96,225 +126,243 @@
 /* members, performing crossover & mutation and then          */
 /* evaluating the resulting population, until the terminating */
 /* condition is satisfied.                                    */
+
 /**************************************************************/
 
 
 /*************************************************************/
 /* Evaluation function: this takes a user defined function.  */
 /* Each time this is changed, the code has to be recompiled. */
-/* The current function is: square (sat[ETA]-swap[ETA])      */ 
+/* The current function is: square (sat[ETA]-swap[ETA])      */
+
 /*************************************************************/
 
-void evaluate(int popsize, int nvar, int * gene, double *fitness, int DECy1, int nsatel, double text[], double ETA[], char **DATE, int ssrun, int esrun, double lat, double alt, char *metfile)
-/*void evaluate(struct genotype *population)*/
+void evaluate(int popsize, int nvar, int *gene, double *fitness, int DECy1,
+	      int nsatel, double text[], double ETA[], char **DATE, int ssrun,
+	      int esrun, double lat, double alt, char *metfile)
+/*void evaluate(struct genotype *population) */
 {
-	int mem, i;
-/*	int DECdloy1,STSdloy1,TECdoy1,GWJan1,GWDec1,FECdloy1;*/
-/*	int FECd1,FECm1,FECY1;*/
-	double fit_value, dif_value;
-	satDATE1 = (char **)malloc((nsatel+1)*sizeof(char *));
-	for(i=0;i<nsatel+1;i++)
-		satDATE1[i] = (char *)malloc(367*sizeof(char));
-	
-	SSRUN1=ssrun;
-	ESRUN1=esrun;
-	LAT1 = lat;
-	ALT1 = alt;
-	sprintf(METFIL1,"%s",metfile);
-	for(i=0;i<4; i++)
-		tex1[i] = text[i];
-	for(i=1;i<=nsatel;i++) {
-		satETA1[i] = ETA[i];
-		sprintf(satDATE1[i],"%s",DATE[i]);
+    int mem, i;
+
+    /*      int DECdloy1,STSdloy1,TECdoy1,GWJan1,GWDec1,FECdloy1; */
+    /*      int FECd1,FECm1,FECY1; */
+    double fit_value, dif_value;
+
+    satDATE1 = (char **)malloc((nsatel + 1) * sizeof(char *));
+    for (i = 0; i < nsatel + 1; i++)
+	satDATE1[i] = (char *)malloc(367 * sizeof(char));
+
+    SSRUN1 = ssrun;
+    ESRUN1 = esrun;
+    LAT1 = lat;
+    ALT1 = alt;
+    sprintf(METFIL1, "%s", metfile);
+    for (i = 0; i < 4; i++)
+	tex1[i] = text[i];
+    for (i = 1; i <= nsatel; i++) {
+	satETA1[i] = ETA[i];
+	sprintf(satDATE1[i], "%s", DATE[i]);
+    }
+    swapkey1();
+    capfil1();
+    swafil1();
+    sol1fil1();
+    sol2fil1();
+    sol3fil1();
+    sol4fil1();
+    sol5fil1();
+    for (mem = 0; mem < popsize; mem++) {
+	fit_value = 0;
+	DECdloy1 = (int)(*(gene + (mem * nvar) + 0));
+	STSdloy1 = (int)(*(gene + (mem * nvar) + 1));
+	TECdoy1 = (int)(*(gene + (mem * nvar) + 2));
+	GWJan1 = (int)(*(gene + (mem * nvar) + 3));
+	GWDec1 = (int)(*(gene + (mem * nvar) + 4));
+	/* Create the FECd1 and FECm1 from DEC & TEC */
+
+		/*******************************************/
+	/* Get the FECdloy1 = DECdloy1 + TECdoy1 */
+	FECdloy1 = DECdloy1 + TECdoy1;
+	/* leap year */
+	if (DECy1 / 4 * 4 == DECy1 && FECdloy1 > 366) {
+	    FECd1 = 31.0;
+	    FECm1 = 12.0;
+	    FECY1 = DECy1 + 1;
 	}
-	swapkey1();
-	capfil1();
-	swafil1();
-	sol1fil1();
-	sol2fil1();
-	sol3fil1();
-	sol4fil1();
-	sol5fil1();
-	for(mem=0;mem<popsize;mem++)
-	{
-		fit_value=0;
-		DECdloy1 =(int) (*(gene + (mem * nvar) + 0) );
-		STSdloy1 =(int) (*(gene + (mem * nvar) + 1) );
-		TECdoy1 =(int) (*(gene + (mem * nvar) + 2) );
-		GWJan1 =(int) (*(gene + (mem * nvar) + 3) );
-		GWDec1 =(int) (*(gene + (mem * nvar) + 4) );
-		/* Create the FECd1 and FECm1 from DEC & TEC */
-		/*******************************************/
-		/* Get the FECdloy1 = DECdloy1 + TECdoy1 */
-		FECdloy1=DECdloy1+TECdoy1;
-		/* leap year */
-		if (DECy1/4*4 == DECy1 && FECdloy1 > 366){
-			FECd1 = 31.0;
-			FECm1 = 12.0;
-			FECY1 = DECy1 + 1;
-		}
-		/* normal year */
-		if (DECy1/4*4 != DECy1 && FECdloy1 > 365){
-			FECd1 = 31.0;
-			FECm1 = 12.0;
-			FECY1 = DECy1 + 1;
-		}
-		else {
-			/* Get the FECd1 and FECm1 */
-			doy2date1(&FECdloy1,&DECy1,&FECd1,&FECm1);
-		}
-		FECdloy1=0;
+	/* normal year */
+	if (DECy1 / 4 * 4 != DECy1 && FECdloy1 > 365) {
+	    FECd1 = 31.0;
+	    FECm1 = 12.0;
+	    FECY1 = DECy1 + 1;
+	}
+	else {
+	    /* Get the FECd1 and FECm1 */
+	    doy2date1(&FECdloy1, &DECy1, &FECd1, &FECm1);
+	}
+	FECdloy1 = 0;
+
 		/*************************/
-		doy2date1(&DECdloy1,&DECy1,&DECd1,&DECm1);
-		doy2date1(&STSdloy1,&STSy1,&STSd1,&STSm1);
-		calfil1();
-		bbcfil1();
-		crpfil1();
-		system("./swap");
-		satellite_dates1(nsatel);
-		for(i=1;i<=nsatel;i++){
-			dif_value=(int) (1000.0*(satETA1[i]-swapETA1[i]));
-			fit_value=fit_value+( (double) (fabs(dif_value)/1000.0));
-		}
-		double const1, constraint1,lambda1;		
-		constraint1= TECdoy1-90.0;
-		if (constraint1>=0.0)
-			lambda1=0.0;
-		else
-			lambda1=10.0;
+	doy2date1(&DECdloy1, &DECy1, &DECd1, &DECm1);
+	doy2date1(&STSdloy1, &STSy1, &STSd1, &STSm1);
+	calfil1();
+	bbcfil1();
+	crpfil1();
+	system("./swap");
+	satellite_dates1(nsatel);
+	for (i = 1; i <= nsatel; i++) {
+	    dif_value = (int)(1000.0 * (satETA1[i] - swapETA1[i]));
+	    fit_value = fit_value + ((double)(fabs(dif_value) / 1000.0));
+	}
+	double const1, constraint1, lambda1;
 
-		const1 =lambda1*(pow(constraint1,2.0));
-		constraint1=const1;
-		fit_value=fit_value/(double) nsatel;
-		fit_value=1.0/(fit_value*(1.0+constraint1));
-		fitness[mem] = (fit_value);
-	}
-	free(satDATE1);
+	constraint1 = TECdoy1 - 90.0;
+	if (constraint1 >= 0.0)
+	    lambda1 = 0.0;
+	else
+	    lambda1 = 10.0;
+
+	const1 = lambda1 * (pow(constraint1, 2.0));
+	constraint1 = const1;
+	fit_value = fit_value / (double)nsatel;
+	fit_value = 1.0 / (fit_value * (1.0 + constraint1));
+	fitness[mem] = (fit_value);
+    }
+    free(satDATE1);
 }
 
 /*********************************************/
 int doy2date1(int *d, int *m, int *day, int *month)
 {
+
 	/*********************************************/
-	/*This routine converts doy to day/month/year*/
+    /*This routine converts doy to day/month/year */
+
 	/*********************************************/
-	int leap = 0;
-	int year,doy;
-	*day = 0;
-	*month = 0;
-	doy = *d;
-	year = *m;
-	/* Leap year if dividing by 4 leads % 0.0*/
-	if (year/4*4 == year) {
-		leap = 1;
-	}
-	if (doy < 32) {
-		*month = 1;
-		*day = doy;
-	}
-	else if (doy > 31 && doy < (60+leap)) {
-		*month = 2;
-		*day = doy - 31;
-	}
-	else if (doy > (59+leap) && doy < (91+leap)) {
-		*month = 3;
-		*day = doy - (59+leap);
-	}
-	else if (doy > (90+leap) && doy < (121+leap)) {
-		*month = 4;
-		*day = doy - (90+leap);
-	} 
-	else if (doy > (120+leap) && doy < (152+leap)) {
-		*month = 5;
-		*day = doy - (120+leap);
-	}
-	else if (doy > (151+leap) && doy < (182+leap)) {
-		*month = 6;
-		*day = doy - (151+leap);
-	}
-	else if (doy > (181+leap) && doy < (213+leap)) {
-		*month = 7;
-		*day = doy - (181+leap);
-	}
-	else if (doy > (212+leap) && doy < (244+leap)) {
-		*month = 8;
-		*day = doy - (212+leap);
-	}
-	else if (doy > (243+leap) && doy < (274+leap)) {
-		*month = 9;
-		*day = doy - (243+leap);
-	}
-	else if (doy > (273+leap) && doy < (305+leap)) {
-		*month = 10;
-		*day = doy - (273+leap);
-	}
-	else if (doy > (304+leap) && doy < (335+leap)) {
-		*month = 11;
-		*day = doy - (304+leap);
-	}
-	else if (doy > 334) {
-		*month = 12;
-		*day = doy - (334+leap);
-	}
-return 1;
+    int leap = 0;
 
+    int year, doy;
+
+    *day = 0;
+    *month = 0;
+    doy = *d;
+    year = *m;
+    /* Leap year if dividing by 4 leads % 0.0 */
+    if (year / 4 * 4 == year) {
+	leap = 1;
+    }
+    if (doy < 32) {
+	*month = 1;
+	*day = doy;
+    }
+    else if (doy > 31 && doy < (60 + leap)) {
+	*month = 2;
+	*day = doy - 31;
+    }
+    else if (doy > (59 + leap) && doy < (91 + leap)) {
+	*month = 3;
+	*day = doy - (59 + leap);
+    }
+    else if (doy > (90 + leap) && doy < (121 + leap)) {
+	*month = 4;
+	*day = doy - (90 + leap);
+    }
+    else if (doy > (120 + leap) && doy < (152 + leap)) {
+	*month = 5;
+	*day = doy - (120 + leap);
+    }
+    else if (doy > (151 + leap) && doy < (182 + leap)) {
+	*month = 6;
+	*day = doy - (151 + leap);
+    }
+    else if (doy > (181 + leap) && doy < (213 + leap)) {
+	*month = 7;
+	*day = doy - (181 + leap);
+    }
+    else if (doy > (212 + leap) && doy < (244 + leap)) {
+	*month = 8;
+	*day = doy - (212 + leap);
+    }
+    else if (doy > (243 + leap) && doy < (274 + leap)) {
+	*month = 9;
+	*day = doy - (243 + leap);
+    }
+    else if (doy > (273 + leap) && doy < (305 + leap)) {
+	*month = 10;
+	*day = doy - (273 + leap);
+    }
+    else if (doy > (304 + leap) && doy < (335 + leap)) {
+	*month = 11;
+	*day = doy - (304 + leap);
+    }
+    else if (doy > 334) {
+	*month = 12;
+	*day = doy - (334 + leap);
+    }
+    return 1;
+
 }
 
 /*********************************************/
-/*This program converts day/month/year to doy*/
+/*This program converts day/month/year to doy */
+
 /*********************************************/
+
 /*********************************************/
 int date2doy1(int *d, int *m)
 {
-	int leap=0;
-	int day_month_tot=0;
-	int day, month;
-	int doy;
-	doy=0;
-	day = *d;
-	month = *m;
-	if (month == 1) {
-		day_month_tot = 0;
-	}
-	else if (month == 2) {
-		day_month_tot = 31;
-	}
-	else if (month == 3) {
-		day_month_tot = 59;
-	}
-	else if (month == 4) {
-		day_month_tot = 90;
-	}
-	else if (month == 5) {
-		day_month_tot = 120;
-	}
-	else if (month == 6) {
-		day_month_tot = 151;
-	}
-	else if (month == 7) {
-		day_month_tot = 181;
-	}
-	else if (month == 8) {
-		day_month_tot = 212;
-	}
-	else if (month == 9) {
-		day_month_tot = 243;
-	}
-	else if (month == 10) {
-		day_month_tot = 273;
-	}
-	else if (month == 11) {
-		day_month_tot = 304;
-	}
-	else if (month == 12) {
-		day_month_tot = 334;
-	}
-	/* Leap year if dividing by 4 leads % 0.0*/
-	if (DECy1/4*4 == DECy1) {
-		leap = 1;
-	}
-	doy=day_month_tot+day+leap;
-	return(doy);
+    int leap = 0;
+
+    int day_month_tot = 0;
+
+    int day, month;
+
+    int doy;
+
+    doy = 0;
+    day = *d;
+    month = *m;
+    if (month == 1) {
+	day_month_tot = 0;
+    }
+    else if (month == 2) {
+	day_month_tot = 31;
+    }
+    else if (month == 3) {
+	day_month_tot = 59;
+    }
+    else if (month == 4) {
+	day_month_tot = 90;
+    }
+    else if (month == 5) {
+	day_month_tot = 120;
+    }
+    else if (month == 6) {
+	day_month_tot = 151;
+    }
+    else if (month == 7) {
+	day_month_tot = 181;
+    }
+    else if (month == 8) {
+	day_month_tot = 212;
+    }
+    else if (month == 9) {
+	day_month_tot = 243;
+    }
+    else if (month == 10) {
+	day_month_tot = 273;
+    }
+    else if (month == 11) {
+	day_month_tot = 304;
+    }
+    else if (month == 12) {
+	day_month_tot = 334;
+    }
+    /* Leap year if dividing by 4 leads % 0.0 */
+    if (DECy1 / 4 * 4 == DECy1) {
+	leap = 1;
+    }
+    doy = day_month_tot + day + leap;
+    return (doy);
 }
 
 /*******************************************************/
@@ -322,518 +370,610 @@
 /* Getting ETa from SWAP INC.file => swap.eta          */
 /*         and merging it with satellite.eta file      */
 /*         Resulting file => eval.eta                  */
-/*	   Format of eval.eta: ETaRS ETaSWAP\n         */
+/*         Format of eval.eta: ETaRS ETaSWAP\n         */
+
 /*******************************************************/
+
 /********************************************************/
+
 /********************************************************/
+
 /********************************************************/
 int get_eta1(char *dateName, char *dateNameValue, int dateCounter1)
 {
     FILE *f;
+
     char s[1000], *ptr;
-    int i=0;
+
+    int i = 0;
+
     char *p;
-    int TRA1=0, TRA2, EVS1=0, EVS2;
-    double TRA=0.0, EVS;
-    f=fopen("out.INC","r");
+
+    int TRA1 = 0, TRA2, EVS1 = 0, EVS2;
+
+    double TRA = 0.0, EVS;
+
+    f = fopen("out.INC", "r");
     if (!f)
-        return 1;
+	return 1;
 
-    while (fgets(s,1000,f)!=NULL) {
-	    ptr = strstr(s, dateNameValue);
-	    if (ptr != NULL) {
-		    p = strtok(ptr, " .");
-		    while (i<17) {
-			    p = strtok(NULL, " .");
-			    if ((i==11 || i==12 || i==15 || i==16) && p) {
-				    if (i==11 && p) {
-					    TRA1 = atoi(p);
-				    } else if (i==12 && p) {
-					    TRA2 = atoi(p);
-					    TRA = (double) (TRA1) + (double) (TRA2/1000.0);
-				    } else if (i==15 && p) {
-					    EVS1 = atoi(p);
-				    } else if (i==16 && p) {
-					    EVS2 = atoi(p);
-					    EVS = (double) (EVS1) + (double) (EVS2/1000.0);
-					    swapETA1[dateCounter1] = (TRA + EVS);
-				    }
-			    }
-			    i=i+1;
+    while (fgets(s, 1000, f) != NULL) {
+	ptr = strstr(s, dateNameValue);
+	if (ptr != NULL) {
+	    p = strtok(ptr, " .");
+	    while (i < 17) {
+		p = strtok(NULL, " .");
+		if ((i == 11 || i == 12 || i == 15 || i == 16) && p) {
+		    if (i == 11 && p) {
+			TRA1 = atoi(p);
 		    }
+		    else if (i == 12 && p) {
+			TRA2 = atoi(p);
+			TRA = (double)(TRA1) + (double)(TRA2 / 1000.0);
+		    }
+		    else if (i == 15 && p) {
+			EVS1 = atoi(p);
+		    }
+		    else if (i == 16 && p) {
+			EVS2 = atoi(p);
+			EVS = (double)(EVS1) + (double)(EVS2 / 1000.0);
+			swapETA1[dateCounter1] = (TRA + EVS);
+		    }
+		}
+		i = i + 1;
 	    }
+	}
     }
     (void)fclose(f);
-   return 1;
+    return 1;
 }
 
 /*******************************************************/
 /*                                                     */
 /* Getting dates from "satellite.dates" file           */
 /*                                                     */
+
 /*******************************************************/
-/* Thanks Dr Honda honda at ait.ac.th!		       */
-/*						       */
+/* Thanks Dr Honda honda at ait.ac.th!                    */
+/*                                                     */
 /* Limited to 99 images so far...                      */
+
 /*******************************************************/
 int satellite_dates1(int nsatel)
 {
-	struct tm t;
-	#define	BUFLEN	1000	
-	int	iline=0;
-       	int	i;
-	char	dateName[256];
-	char	*dateNameFormat="%s%2.2d";
-	char	*date ="date";
-	char	dateNameValue[256];
-	char	*dateFormat1="%1.1d/%2.2d/%4.4d";
-	char	*dateFormat2="%2.2d/%2.2d/%4.4d";
-	int 	dateCounter1=0;
-      char buf[BUFLEN];
+    struct tm t;
 
-	for(i=1;i<=nsatel;i++){
-		if(sscanf( satDATE1[i], "%d/%d/%d", &t.tm_mday, &t.tm_mon, &t.tm_year) != 3 ){
-			fprintf(stderr, "reading problem; line : %d: %s\n", iline, buf);
-			return 1;
-		}
-		t.tm_mon--;
-		t.tm_year -= 1900;
-	    	sprintf( dateName, dateNameFormat, date, i);
-		i++;
-		if (t.tm_mday < 10.0) {
-			sprintf (dateNameValue,dateFormat1, t.tm_mday, t.tm_mon+1, t.tm_year+1900);
-		} else {
-		sprintf(dateNameValue,dateFormat2, t.tm_mday, t.tm_mon+1,t.tm_year+1900);
-		}
-		get_eta1(dateName, dateNameValue, dateCounter1);/*send the sat.date to get_eta1()*/
-		dateCounter1++;
+#define	BUFLEN	1000
+    int iline = 0;
+
+    int i;
+
+    char dateName[256];
+
+    char *dateNameFormat = "%s%2.2d";
+
+    char *date = "date";
+
+    char dateNameValue[256];
+
+    char *dateFormat1 = "%1.1d/%2.2d/%4.4d";
+
+    char *dateFormat2 = "%2.2d/%2.2d/%4.4d";
+
+    int dateCounter1 = 0;
+
+    char buf[BUFLEN];
+
+    for (i = 1; i <= nsatel; i++) {
+	if (sscanf(satDATE1[i], "%d/%d/%d", &t.tm_mday, &t.tm_mon, &t.tm_year)
+	    != 3) {
+	    fprintf(stderr, "reading problem; line : %d: %s\n", iline, buf);
+	    return 1;
+	}
+	t.tm_mon--;
+	t.tm_year -= 1900;
+	sprintf(dateName, dateNameFormat, date, i);
+	i++;
+	if (t.tm_mday < 10.0) {
+	    sprintf(dateNameValue, dateFormat1, t.tm_mday, t.tm_mon + 1,
+		    t.tm_year + 1900);
+	}
+	else {
+	    sprintf(dateNameValue, dateFormat2, t.tm_mday, t.tm_mon + 1,
+		    t.tm_year + 1900);
+	}
+	get_eta1(dateName, dateNameValue, dateCounter1);	/*send the sat.date to get_eta1() */
+	dateCounter1++;
     }
-return 1;
+    return 1;
 }
 
 /*******************************************************/
 /*                                                     */
 /*                File Input for SWAP                  */
 /*                                                     */
+
 /*******************************************************/
 /* Date: 20030901                                      */
+
 /*******************************************************/
+
 /********************************************************/
 /* main program                                         */
+
 /********************************************************/
+
 /********************************************************/
 void swapkey1(void)
 {
-	FILE *swapkey1; /* SWAP.KEY file-variable */
-	swapkey1 = fopen("SWAP.KEY", "w");
-	fputs(" Project = 'Rood'\n", swapkey1);
-	fputs(" Path = ''\n", swapkey1);
-	fputs(" CType = 1\n", swapkey1);
-	fputs(" SWSCRE = 0\n\n", swapkey1);
-	fprintf(swapkey1, " SSRUN1 = 01 01 %d\n", SSRUN1);		
-	fprintf(swapkey1, " ESRUN1 = 31 12 %d\n", ESRUN1);	
-	fputs(" FMAY = 1\n", swapkey1);
-	fputs(" Period = 1\n", swapkey1);
-	fputs(" SWRES = 1\n\n", swapkey1);
-	fputs(" SWODAT = 0\n", swapkey1);
-	fputs("*\n", swapkey1);
-	fputs("* DD MM YYYY\n", swapkey1);
-	fputs("* End_of_table\n\n", swapkey1);
-	fprintf(swapkey1, " METFIL1 = \'%s\'\n", METFIL1);	
-	fprintf(swapkey1, " LAT1 = %lf\n", LAT1);	
-	fprintf(swapkey1, " ALT1 = %lf\n", ALT1);	
-	fputs(" SWETR = 0\n", swapkey1);
-	fputs(" SWRAI = 0\n\n", swapkey1);
-	fputs("* IRGFIL CALFIL   DRFIL   BBCFIL  OUTFIL\n", swapkey1);
-	fputs("  ''     'RoodC'  'none'  'Rood'  'out'\n", swapkey1);
-	fputs("* End_of_table\n\n", swapkey1);
-	fputs(" SWDRA = 0\n\n", swapkey1);
-	fputs(" SWSOLU = 0\n\n", swapkey1);
-	fputs(" SWHEA = 0\n\n", swapkey1);
-	fputs(" SWVAP = 0\n\n", swapkey1);
-	fputs(" SWDRF = 0\n\n", swapkey1);
-	fputs(" SWSWB = 0\n\n", swapkey1);
-	fputs(" SWAFO = 0\n",swapkey1);
-	fputs(" AFONAM = 'testA'\n\n", swapkey1);
-	fputs(" SWAUN = 0\n", swapkey1);
-	fputs(" AUNNAM = 'testA'\n\n", swapkey1);
-	fputs(" SWATE = 0\n", swapkey1);
-	fputs(" ATENAM = 'testA'\n\n", swapkey1);
-	fputs(" SWAIR = 0\n", swapkey1);
-	fputs(" AIRNAM = 'testA'\n\n", swapkey1);
-	(void)fclose(swapkey1);
-	return;
+    FILE *swapkey1;		/* SWAP.KEY file-variable */
+
+    swapkey1 = fopen("SWAP.KEY", "w");
+    fputs(" Project = 'Rood'\n", swapkey1);
+    fputs(" Path = ''\n", swapkey1);
+    fputs(" CType = 1\n", swapkey1);
+    fputs(" SWSCRE = 0\n\n", swapkey1);
+    fprintf(swapkey1, " SSRUN1 = 01 01 %d\n", SSRUN1);
+    fprintf(swapkey1, " ESRUN1 = 31 12 %d\n", ESRUN1);
+    fputs(" FMAY = 1\n", swapkey1);
+    fputs(" Period = 1\n", swapkey1);
+    fputs(" SWRES = 1\n\n", swapkey1);
+    fputs(" SWODAT = 0\n", swapkey1);
+    fputs("*\n", swapkey1);
+    fputs("* DD MM YYYY\n", swapkey1);
+    fputs("* End_of_table\n\n", swapkey1);
+    fprintf(swapkey1, " METFIL1 = \'%s\'\n", METFIL1);
+    fprintf(swapkey1, " LAT1 = %lf\n", LAT1);
+    fprintf(swapkey1, " ALT1 = %lf\n", ALT1);
+    fputs(" SWETR = 0\n", swapkey1);
+    fputs(" SWRAI = 0\n\n", swapkey1);
+    fputs("* IRGFIL CALFIL   DRFIL   BBCFIL  OUTFIL\n", swapkey1);
+    fputs("  ''     'RoodC'  'none'  'Rood'  'out'\n", swapkey1);
+    fputs("* End_of_table\n\n", swapkey1);
+    fputs(" SWDRA = 0\n\n", swapkey1);
+    fputs(" SWSOLU = 0\n\n", swapkey1);
+    fputs(" SWHEA = 0\n\n", swapkey1);
+    fputs(" SWVAP = 0\n\n", swapkey1);
+    fputs(" SWDRF = 0\n\n", swapkey1);
+    fputs(" SWSWB = 0\n\n", swapkey1);
+    fputs(" SWAFO = 0\n", swapkey1);
+    fputs(" AFONAM = 'testA'\n\n", swapkey1);
+    fputs(" SWAUN = 0\n", swapkey1);
+    fputs(" AUNNAM = 'testA'\n\n", swapkey1);
+    fputs(" SWATE = 0\n", swapkey1);
+    fputs(" ATENAM = 'testA'\n\n", swapkey1);
+    fputs(" SWAIR = 0\n", swapkey1);
+    fputs(" AIRNAM = 'testA'\n\n", swapkey1);
+    (void)fclose(swapkey1);
+    return;
 }
 
 /********************************************************/
 /* CAL-file                                             */
+
 /********************************************************/
 
 void calfil1(void)
+{
+    FILE *calfil1;		/* RoodC.CAL file_variable */
 
-{
-	FILE *calfil1; /* RoodC.CAL file_variable */
-	calfil1 = fopen("RoodC.CAL", "w");
-	fputs("* CRPFIL Type CAPFIL  EMERGENCE END_crop START_sch\n", calfil1);
-	fputs("*                     d1 m1     d2 m2    d3 m3\n", calfil1);
-	fprintf(calfil1, " 'Crop'  1    'RoodC' %i %i     %i %i    %i %i\n", DECd1, DECm1, FECd1, FECm1, STSd1, STSm1);
-	fputs("* End_of_table\n\n", calfil1);
-	(void)fclose(calfil1);
-	return ;
+    calfil1 = fopen("RoodC.CAL", "w");
+    fputs("* CRPFIL Type CAPFIL  EMERGENCE END_crop START_sch\n", calfil1);
+    fputs("*                     d1 m1     d2 m2    d3 m3\n", calfil1);
+    fprintf(calfil1, " 'Crop'  1    'RoodC' %i %i     %i %i    %i %i\n",
+	    DECd1, DECm1, FECd1, FECm1, STSd1, STSm1);
+    fputs("* End_of_table\n\n", calfil1);
+    (void)fclose(calfil1);
+    return;
 }
 
 /*******************************************************/
 /* CAP-file                                            */
+
 /*******************************************************/
 
 void capfil1(void)
 {
-	FILE *capfil1; /* RoodC.CAP file_variable */
-	capfil1 = fopen("RoodC.CAP", "w");
-	fputs(" ISUAS = 1\n\n", capfil1);
-	fputs(" CIRRS = 2.56\n\n", capfil1);
-	fputs(" TCS1 = 0\n", capfil1);
-	fputs("* DVS  Ta/Tp\n", capfil1);
-	fputs("0.0 0.75\n", capfil1);
-	fputs("2.0 0.75\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs("TCS2 = 0\n", capfil1);
-	fputs("* DVS  RAW\n", capfil1);
-	fputs("  0.0  0.95\n", capfil1);
-	fputs("  2.0  0.95\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs("TCS3 = 0\n", capfil1);
-	fputs("* DVS  TAW\n", capfil1);
-	fputs("  0.0  0.50\n", capfil1);
-	fputs("  2.0  0.50\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs("TCS4 = 0\n", capfil1);
-	fputs("* DVS  DWA\n", capfil1);
-	fputs("  0.0  40.0\n", capfil1);
-	fputs("  2.0  40.0\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs(" TCS5 = 1\n", capfil1);
-	fputs(" PHORMC = 0\n", capfil1);
-	fputs(" DCRIT = 0.0\n", capfil1);
-	fputs("* DVS  VALUE\n", capfil1);
-	fputs("  0.0  1.0\n", capfil1);
-	fputs("  2.0  1.0\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs(" DCS1 = 0\n", capfil1);
-	fputs("* DVS  dI\n", capfil1);
-	fputs("  0.0  0.0\n", capfil1);
-	fputs("  2.0  0.0\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	fputs(" DCS2 = 1\n", capfil1);
-	fputs("* DVS  FID\n", capfil1);
-	fputs("  0.0  100.0\n", capfil1);
-	fputs("  2.0  100.0\n", capfil1);
-	fputs("* End of table\n\n", capfil1);
-	(void)fclose(capfil1);
-	return;
+    FILE *capfil1;		/* RoodC.CAP file_variable */
+
+    capfil1 = fopen("RoodC.CAP", "w");
+    fputs(" ISUAS = 1\n\n", capfil1);
+    fputs(" CIRRS = 2.56\n\n", capfil1);
+    fputs(" TCS1 = 0\n", capfil1);
+    fputs("* DVS  Ta/Tp\n", capfil1);
+    fputs("0.0 0.75\n", capfil1);
+    fputs("2.0 0.75\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs("TCS2 = 0\n", capfil1);
+    fputs("* DVS  RAW\n", capfil1);
+    fputs("  0.0  0.95\n", capfil1);
+    fputs("  2.0  0.95\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs("TCS3 = 0\n", capfil1);
+    fputs("* DVS  TAW\n", capfil1);
+    fputs("  0.0  0.50\n", capfil1);
+    fputs("  2.0  0.50\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs("TCS4 = 0\n", capfil1);
+    fputs("* DVS  DWA\n", capfil1);
+    fputs("  0.0  40.0\n", capfil1);
+    fputs("  2.0  40.0\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs(" TCS5 = 1\n", capfil1);
+    fputs(" PHORMC = 0\n", capfil1);
+    fputs(" DCRIT = 0.0\n", capfil1);
+    fputs("* DVS  VALUE\n", capfil1);
+    fputs("  0.0  1.0\n", capfil1);
+    fputs("  2.0  1.0\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs(" DCS1 = 0\n", capfil1);
+    fputs("* DVS  dI\n", capfil1);
+    fputs("  0.0  0.0\n", capfil1);
+    fputs("  2.0  0.0\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    fputs(" DCS2 = 1\n", capfil1);
+    fputs("* DVS  FID\n", capfil1);
+    fputs("  0.0  100.0\n", capfil1);
+    fputs("  2.0  100.0\n", capfil1);
+    fputs("* End of table\n\n", capfil1);
+    (void)fclose(capfil1);
+    return;
 }
 
 /*******************************************************/
 /* SWA-file                                            */
+
 /*******************************************************/
 
 void swafil1(void)
 {
-	FILE *swafil1; /* Rood.SWA file_variable */
-	swafil1 = fopen("Rood.SWA", "w");
-	fputs("*******************************************************************************\n", swafil1);
-	fputs(" PONDMX = 10.0\n\n", swafil1);
-	fputs("*******************************************************************************\n\n", swafil1);
+    FILE *swafil1;		/* Rood.SWA file_variable */
 
-	fputs("*******************************************************************************\n", swafil1);
-	fputs(" SWCFBS = 0\n", swafil1);
-	fputs(" CFBS = 0.9\n", swafil1);
-	fputs(" SWREDU = 2\n", swafil1);
-	fputs(" COFRED = 0.35\n", swafil1);
-	fputs(" RSIGNI = 0.5\n\n", swafil1);
-	fputs("*******************************************************************************\n\n", swafil1);
+    swafil1 = fopen("Rood.SWA", "w");
+    fputs
+	("*******************************************************************************\n",
+	 swafil1);
+    fputs(" PONDMX = 10.0\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil1);
 
-	fputs("*******************************************************************************\n", swafil1);
-	fputs(" DTMIN = 1.0E-4\n", swafil1);
-	fputs(" DTMAX = 0.1\n", swafil1);
-	fputs(" SWNUMS = 2\n", swafil1);
-	fputs(" THETOL = 0.01\n\n", swafil1);
-	fputs("*******************************************************************************\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n",
+	 swafil1);
+    fputs(" SWCFBS = 0\n", swafil1);
+    fputs(" CFBS = 0.9\n", swafil1);
+    fputs(" SWREDU = 2\n", swafil1);
+    fputs(" COFRED = 0.35\n", swafil1);
+    fputs(" RSIGNI = 0.5\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil1);
 
-	fputs("*******************************************************************************\n", swafil1);
-	fputs(" NUMLAY = 5\n", swafil1);
-	fputs(" NUMNOD = 50\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n",
+	 swafil1);
+    fputs(" DTMIN = 1.0E-4\n", swafil1);
+    fputs(" DTMAX = 0.1\n", swafil1);
+    fputs(" SWNUMS = 2\n", swafil1);
+    fputs(" THETOL = 0.01\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil1);
 
-	fputs(" BOTCOM = 14 19 22 26 50\n", swafil1);
-	fputs("*\n", swafil1);
-	fputs("* thickness of soil compartments [cm]:\n", swafil1);
-	fputs("* total 500 cm\n", swafil1);
-	fputs("*\n", swafil1);
-	fputs(" DZ =\n", swafil1);
-	fputs("     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0\n", swafil1);
-	fputs("     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0\n", swafil1);
-	fputs("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n", swafil1);
-	fputs("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n", swafil1);
-	fputs("    20.0    20.0    20.0    20.0    20.0    20.0    20.0    20.0    40.0    40.0\n", swafil1);
-	fputs("*******************************************************************************\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n",
+	 swafil1);
+    fputs(" NUMLAY = 5\n", swafil1);
+    fputs(" NUMNOD = 50\n\n", swafil1);
 
-	fputs("*******************************************************************************\n", swafil1);
-	fputs(" SOLFIL = 'Rood_1' 'Rood_2' 'Rood_3' 'Rood_4' 'Rood_5'\n\n", swafil1);
-	fputs("* PSAND  PSILT  PCLAY  ORGMAT\n", swafil1);
-	fprintf(swafil1,"  %lf   %lf   %lf   %lf\n",tex1[0],tex1[1],tex1[2],tex1[3]);
-	fprintf(swafil1,"  %lf   %lf   %lf   %lf\n",tex1[0],tex1[1],tex1[2],tex1[3]);
-	fprintf(swafil1,"  %lf   %lf   %lf   %lf\n",tex1[0],tex1[1],tex1[2],tex1[3]);
-	fprintf(swafil1,"  %lf   %lf   %lf   %lf\n",tex1[0],tex1[1],tex1[2],tex1[3]);
-	fprintf(swafil1,"  %lf   %lf   %lf   %lf\n\n",tex1[0],tex1[1],tex1[2],tex1[3]);
-	
-	fputs("*******************************************************************************\n\n", swafil1);
+    fputs(" BOTCOM = 14 19 22 26 50\n", swafil1);
+    fputs("*\n", swafil1);
+    fputs("* thickness of soil compartments [cm]:\n", swafil1);
+    fputs("* total 500 cm\n", swafil1);
+    fputs("*\n", swafil1);
+    fputs(" DZ =\n", swafil1);
+    fputs
+	("     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0\n",
+	 swafil1);
+    fputs
+	("     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0\n",
+	 swafil1);
+    fputs
+	("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n",
+	 swafil1);
+    fputs
+	("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n",
+	 swafil1);
+    fputs
+	("    20.0    20.0    20.0    20.0    20.0    20.0    20.0    20.0    40.0    40.0\n",
+	 swafil1);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil1);
 
-	fputs(" RDS = 300.0\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n",
+	 swafil1);
+    fputs(" SOLFIL = 'Rood_1' 'Rood_2' 'Rood_3' 'Rood_4' 'Rood_5'\n\n",
+	  swafil1);
+    fputs("* PSAND  PSILT  PCLAY  ORGMAT\n", swafil1);
+    fprintf(swafil1, "  %lf   %lf   %lf   %lf\n", tex1[0], tex1[1], tex1[2],
+	    tex1[3]);
+    fprintf(swafil1, "  %lf   %lf   %lf   %lf\n", tex1[0], tex1[1], tex1[2],
+	    tex1[3]);
+    fprintf(swafil1, "  %lf   %lf   %lf   %lf\n", tex1[0], tex1[1], tex1[2],
+	    tex1[3]);
+    fprintf(swafil1, "  %lf   %lf   %lf   %lf\n", tex1[0], tex1[1], tex1[2],
+	    tex1[3]);
+    fprintf(swafil1, "  %lf   %lf   %lf   %lf\n\n", tex1[0], tex1[1], tex1[2],
+	    tex1[3]);
 
-	fputs(" SWHYST = 0\n", swafil1);
-	fputs(" TAU = 0.2\n\n", swafil1);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil1);
 
-	fputs(" SWSCAL = 0\n", swafil1);
-	fputs(" NSCALE = 0\n", swafil1);
-	fputs(" ISCLAY = 0\n", swafil1);
-	fputs(" SWMOBI = 0\n", swafil1);
-	fputs("* PF1  FM1  PF2  FM2  THETIM\n", swafil1);
-	fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil1);
-	fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil1);
-	fputs("* End_of_table\n\n", swafil1);
+    fputs(" RDS = 300.0\n\n", swafil1);
 
-	fputs(" SWCRACK = 0\n", swafil1);
-	fputs(" SHRINA = 0.53\n", swafil1);
-	fputs(" MOISR1 = 1.0\n", swafil1);
-	fputs(" MOISRD = 0.05\n", swafil1);
-	fputs(" ZNCRACK = -5.0\n", swafil1);
-	fputs(" GEOMF = 3.0\n", swafil1);
-	fputs(" DIAMPOL = 40.0\n", swafil1);
-	fputs(" RAPCOEF = 0.0\n", swafil1);
-	fputs(" DIFDES = 0.2\n", swafil1);
-	fputs(" THETCR = 0.50  0.50  0.50  0.50  0.50\n\n", swafil1);
-	fputs(" SWDIVD = 0\n", swafil1);
-	fputs(" COFANI =    1.0    1.0    1.0    1.0    1.0\n\n", swafil1);
-	fputs(" SWINCO = 2\n", swafil1);
-	fputs(" GWLI = -200.0\n", swafil1);
-	fputs(" OSGWLM = 30.0\n\n", swafil1);
-	(void)fclose(swafil1);
-	return;
+    fputs(" SWHYST = 0\n", swafil1);
+    fputs(" TAU = 0.2\n\n", swafil1);
+
+    fputs(" SWSCAL = 0\n", swafil1);
+    fputs(" NSCALE = 0\n", swafil1);
+    fputs(" ISCLAY = 0\n", swafil1);
+    fputs(" SWMOBI = 0\n", swafil1);
+    fputs("* PF1  FM1  PF2  FM2  THETIM\n", swafil1);
+    fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil1);
+    fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil1);
+    fputs("* End_of_table\n\n", swafil1);
+
+    fputs(" SWCRACK = 0\n", swafil1);
+    fputs(" SHRINA = 0.53\n", swafil1);
+    fputs(" MOISR1 = 1.0\n", swafil1);
+    fputs(" MOISRD = 0.05\n", swafil1);
+    fputs(" ZNCRACK = -5.0\n", swafil1);
+    fputs(" GEOMF = 3.0\n", swafil1);
+    fputs(" DIAMPOL = 40.0\n", swafil1);
+    fputs(" RAPCOEF = 0.0\n", swafil1);
+    fputs(" DIFDES = 0.2\n", swafil1);
+    fputs(" THETCR = 0.50  0.50  0.50  0.50  0.50\n\n", swafil1);
+    fputs(" SWDIVD = 0\n", swafil1);
+    fputs(" COFANI =    1.0    1.0    1.0    1.0    1.0\n\n", swafil1);
+    fputs(" SWINCO = 2\n", swafil1);
+    fputs(" GWLI = -200.0\n", swafil1);
+    fputs(" OSGWLM = 30.0\n\n", swafil1);
+    (void)fclose(swafil1);
+    return;
 }
 
 /*******************************************************/
 /* BBC-file                                            */
+
 /*******************************************************/
 
 void bbcfil1(void)
 {
-	FILE *bbcfil1; /* Rood.BBC file_variable */
-	bbcfil1 = fopen("Rood.BBC", "w");
-	fputs(" SWOPT1 = 1\n", bbcfil1);
-	fputs("* d1 m1 GWlevel\n", bbcfil1);
-	fprintf(bbcfil1,"  1  1 -%i.0\n",GWJan1);
-	fprintf(bbcfil1," 31 12 -%i.0\n",GWDec1);
-	fputs("* End of table\n\n", bbcfil1);
-	fputs(" SWOPT2 = 0\n", bbcfil1);
-	fputs(" SWC2 = 2\n", bbcfil1);
-	fputs(" C2AVE = 0.1\n", bbcfil1);
-	fputs(" C2AMP = 0.05\n", bbcfil1);
-	fputs(" C2STA = 91\n", bbcfil1);
-	fputs("* d2 m2 Qbot\n", bbcfil1);
-	fputs("   1  1 0.10\n", bbcfil1);
-	fputs("   5  6 0.20\n", bbcfil1);
-	fputs("  31 12 0.10\n", bbcfil1);
-	fputs("* End of table\n\n", bbcfil1);
-	fputs(" SWOPT3 = 0\n", bbcfil1);
-	fputs(" SHAPE = 0.79\n", bbcfil1);
-	fputs(" RIMLAY = 500\n", bbcfil1);
-	fputs(" AQAVE = 50.0\n", bbcfil1);
-	fputs(" AQAMP = 20.0\n", bbcfil1);
-	fputs(" AQTAMX = 120\n", bbcfil1);
-	fputs(" AQPER = 365\n\n", bbcfil1);
-	fputs(" SWOPT4 = 0\n", bbcfil1);
-	fputs(" COFQHA = -0.3\n", bbcfil1);
-	fputs(" COFQHB = -0.01\n\n", bbcfil1);
-	fputs(" SWOPT5 = 0\n", bbcfil1);
-	fputs("* d5 m5 GWlevel\n", bbcfil1);
-	fputs("   1  1  50.0\n", bbcfil1);
-	fputs("  31 12  20.0\n", bbcfil1);
-	fputs("* End of table\n\n", bbcfil1);
-	fputs(" SWOPT6 = 0\n", bbcfil1);
-	fputs(" SWOPT7 = 0\n", bbcfil1);
-	fputs(" SWOPT8 = 0\n", bbcfil1);
-	(void)fclose(bbcfil1);
-	return;
+    FILE *bbcfil1;		/* Rood.BBC file_variable */
+
+    bbcfil1 = fopen("Rood.BBC", "w");
+    fputs(" SWOPT1 = 1\n", bbcfil1);
+    fputs("* d1 m1 GWlevel\n", bbcfil1);
+    fprintf(bbcfil1, "  1  1 -%i.0\n", GWJan1);
+    fprintf(bbcfil1, " 31 12 -%i.0\n", GWDec1);
+    fputs("* End of table\n\n", bbcfil1);
+    fputs(" SWOPT2 = 0\n", bbcfil1);
+    fputs(" SWC2 = 2\n", bbcfil1);
+    fputs(" C2AVE = 0.1\n", bbcfil1);
+    fputs(" C2AMP = 0.05\n", bbcfil1);
+    fputs(" C2STA = 91\n", bbcfil1);
+    fputs("* d2 m2 Qbot\n", bbcfil1);
+    fputs("   1  1 0.10\n", bbcfil1);
+    fputs("   5  6 0.20\n", bbcfil1);
+    fputs("  31 12 0.10\n", bbcfil1);
+    fputs("* End of table\n\n", bbcfil1);
+    fputs(" SWOPT3 = 0\n", bbcfil1);
+    fputs(" SHAPE = 0.79\n", bbcfil1);
+    fputs(" RIMLAY = 500\n", bbcfil1);
+    fputs(" AQAVE = 50.0\n", bbcfil1);
+    fputs(" AQAMP = 20.0\n", bbcfil1);
+    fputs(" AQTAMX = 120\n", bbcfil1);
+    fputs(" AQPER = 365\n\n", bbcfil1);
+    fputs(" SWOPT4 = 0\n", bbcfil1);
+    fputs(" COFQHA = -0.3\n", bbcfil1);
+    fputs(" COFQHB = -0.01\n\n", bbcfil1);
+    fputs(" SWOPT5 = 0\n", bbcfil1);
+    fputs("* d5 m5 GWlevel\n", bbcfil1);
+    fputs("   1  1  50.0\n", bbcfil1);
+    fputs("  31 12  20.0\n", bbcfil1);
+    fputs("* End of table\n\n", bbcfil1);
+    fputs(" SWOPT6 = 0\n", bbcfil1);
+    fputs(" SWOPT7 = 0\n", bbcfil1);
+    fputs(" SWOPT8 = 0\n", bbcfil1);
+    (void)fclose(bbcfil1);
+    return;
 }
 
 /********************************************************/
 /* CRP-file                                             */
+
 /********************************************************/
 
 void crpfil1(void)
 {
-	FILE *crpfil1; /* Crop.CRP file_variable */
-	crpfil1 = fopen("Crop.CRP", "w");
-	fputs(" IDEV = 1\n", crpfil1);
-	fprintf(crpfil1,"LCC = %i\n",TECdoy1);
-	fputs(" TSUMEA = 1050.0\n", crpfil1);
-	fputs(" TSUMAM = 1000.0\n", crpfil1);
-	fputs(" TBASE = 0.0\n\n", crpfil1);
-	fputs(" KDIF = 0.6\n", crpfil1);
-	fputs(" KDIR = 0.75\n\n", crpfil1);
-	fputs(" SWGC = 1\n", crpfil1);
-	fputs("*    DVS   LAI or SCF\n", crpfil1);
-	fputs(" GCTB =\n", crpfil1);
-	fputs("     0.0    0.00\n", crpfil1);
-	fputs("     0.2    0.07\n", crpfil1);
-	fputs("     0.4    0.16\n", crpfil1);
-	fputs("     0.6    0.61\n", crpfil1);
-	fputs("     0.8    1.25\n", crpfil1);
-	fputs("     1.0    1.73\n", crpfil1);
-	fputs("     1.2    1.75\n", crpfil1);
-	fputs("     1.4    1.73\n", crpfil1);
-	fputs("     1.6    1.71\n", crpfil1);
-	fputs("     1.8    1.04\n", crpfil1);
-	fputs("     2.0    0.58\n", crpfil1);
-	fputs("**************\n\n", crpfil1);
-	fputs(" SWCF = 2\n", crpfil1);
-	fputs("*        DVS   CF or CH\n", crpfil1);
-	fputs(" CFTB = 0.00   0.0\n", crpfil1);
-	fputs("        1.00  70.0\n", crpfil1);
-	fputs("        2.00  70.0\n\n", crpfil1);
-	fputs("*         DVS   RD\n", crpfil1);
-	fputs(" RDTB = 0.00   0.0\n", crpfil1);
-	fputs("        1.00 100.0\n", crpfil1);
-	fputs("        2.00 100.0\n\n", crpfil1);
-	fputs("*        DVS   KY\n", crpfil1);
-	fputs(" KYTB = 0.0   0.2\n", crpfil1);
-	fputs("        0.4   0.2\n", crpfil1);
-	fputs("        0.9   0.5\n", crpfil1);
-	fputs("        1.5   0.5\n", crpfil1);
-	fputs("        2.0   0.3\n\n", crpfil1);
-	fputs("**************\n\n", crpfil1);
-	fputs(" HLIM1 = -0.1\n", crpfil1);
-	fputs(" HLIM2U = -30.0\n", crpfil1);
-	fputs(" HLIM2L = -30.0\n", crpfil1);
-	fputs(" HLIM3H = -100.0\n", crpfil1);
-	fputs(" HLIM3L = -200.0\n", crpfil1);
-	fputs(" HLIM4 = -16000.0\n", crpfil1);
-	fputs(" RSC = 70.0\n", crpfil1);
-	fputs(" ADCRH = 0.5\n", crpfil1);
-	fputs(" ADCRL = 0.1\n\n", crpfil1);
-	fputs(" ECMAX = 7.7\n", crpfil1);
-	fputs(" ECSLOP = 5.0\n\n", crpfil1);
-	fputs(" COFAB = 0.25\n\n", crpfil1);
-	fputs("*        Rdepth Rdensity\n", crpfil1);
-	fputs(" RDCTB = 0.0    1.0\n", crpfil1);
-	fputs("         1.0    0.0\n", crpfil1);
-	fputs("**************\n", crpfil1);
-	(void)fclose(crpfil1);
-	return;
+    FILE *crpfil1;		/* Crop.CRP file_variable */
+
+    crpfil1 = fopen("Crop.CRP", "w");
+    fputs(" IDEV = 1\n", crpfil1);
+    fprintf(crpfil1, "LCC = %i\n", TECdoy1);
+    fputs(" TSUMEA = 1050.0\n", crpfil1);
+    fputs(" TSUMAM = 1000.0\n", crpfil1);
+    fputs(" TBASE = 0.0\n\n", crpfil1);
+    fputs(" KDIF = 0.6\n", crpfil1);
+    fputs(" KDIR = 0.75\n\n", crpfil1);
+    fputs(" SWGC = 1\n", crpfil1);
+    fputs("*    DVS   LAI or SCF\n", crpfil1);
+    fputs(" GCTB =\n", crpfil1);
+    fputs("     0.0    0.00\n", crpfil1);
+    fputs("     0.2    0.07\n", crpfil1);
+    fputs("     0.4    0.16\n", crpfil1);
+    fputs("     0.6    0.61\n", crpfil1);
+    fputs("     0.8    1.25\n", crpfil1);
+    fputs("     1.0    1.73\n", crpfil1);
+    fputs("     1.2    1.75\n", crpfil1);
+    fputs("     1.4    1.73\n", crpfil1);
+    fputs("     1.6    1.71\n", crpfil1);
+    fputs("     1.8    1.04\n", crpfil1);
+    fputs("     2.0    0.58\n", crpfil1);
+    fputs("**************\n\n", crpfil1);
+    fputs(" SWCF = 2\n", crpfil1);
+    fputs("*        DVS   CF or CH\n", crpfil1);
+    fputs(" CFTB = 0.00   0.0\n", crpfil1);
+    fputs("        1.00  70.0\n", crpfil1);
+    fputs("        2.00  70.0\n\n", crpfil1);
+    fputs("*         DVS   RD\n", crpfil1);
+    fputs(" RDTB = 0.00   0.0\n", crpfil1);
+    fputs("        1.00 100.0\n", crpfil1);
+    fputs("        2.00 100.0\n\n", crpfil1);
+    fputs("*        DVS   KY\n", crpfil1);
+    fputs(" KYTB = 0.0   0.2\n", crpfil1);
+    fputs("        0.4   0.2\n", crpfil1);
+    fputs("        0.9   0.5\n", crpfil1);
+    fputs("        1.5   0.5\n", crpfil1);
+    fputs("        2.0   0.3\n\n", crpfil1);
+    fputs("**************\n\n", crpfil1);
+    fputs(" HLIM1 = -0.1\n", crpfil1);
+    fputs(" HLIM2U = -30.0\n", crpfil1);
+    fputs(" HLIM2L = -30.0\n", crpfil1);
+    fputs(" HLIM3H = -100.0\n", crpfil1);
+    fputs(" HLIM3L = -200.0\n", crpfil1);
+    fputs(" HLIM4 = -16000.0\n", crpfil1);
+    fputs(" RSC = 70.0\n", crpfil1);
+    fputs(" ADCRH = 0.5\n", crpfil1);
+    fputs(" ADCRL = 0.1\n\n", crpfil1);
+    fputs(" ECMAX = 7.7\n", crpfil1);
+    fputs(" ECSLOP = 5.0\n\n", crpfil1);
+    fputs(" COFAB = 0.25\n\n", crpfil1);
+    fputs("*        Rdepth Rdensity\n", crpfil1);
+    fputs(" RDCTB = 0.0    1.0\n", crpfil1);
+    fputs("         1.0    0.0\n", crpfil1);
+    fputs("**************\n", crpfil1);
+    (void)fclose(crpfil1);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 1                                   */
+
 /*******************************************************/
 
 void sol1fil1(void)
 {
-	FILE *sol1fil1; /* Rood_1.SOL file_variable */
-	sol1fil1 = fopen("Rood_1.SOL", "w");
-	fputs(" SWPHYS = 1\n\n", sol1fil1);
-	fputs(" COFGEN1 = 0.000\n", sol1fil1);
-	fputs(" COFGEN2 = 0.492\n", sol1fil1);
-	fputs(" COFGEN3 = 0.500\n", sol1fil1);
-	fputs(" COFGEN4 = 0.02638\n", sol1fil1);
-	fputs(" COFGEN5 = -2.196\n", sol1fil1);
-	fputs(" COFGEN6 = 1.1775\n", sol1fil1);
-	fputs(" COFGEN8 = 0.029\n", sol1fil1);
-	fputs("* End of file\n", sol1fil1);
-	(void)fclose(sol1fil1);
-	return;
+    FILE *sol1fil1;		/* Rood_1.SOL file_variable */
+
+    sol1fil1 = fopen("Rood_1.SOL", "w");
+    fputs(" SWPHYS = 1\n\n", sol1fil1);
+    fputs(" COFGEN1 = 0.000\n", sol1fil1);
+    fputs(" COFGEN2 = 0.492\n", sol1fil1);
+    fputs(" COFGEN3 = 0.500\n", sol1fil1);
+    fputs(" COFGEN4 = 0.02638\n", sol1fil1);
+    fputs(" COFGEN5 = -2.196\n", sol1fil1);
+    fputs(" COFGEN6 = 1.1775\n", sol1fil1);
+    fputs(" COFGEN8 = 0.029\n", sol1fil1);
+    fputs("* End of file\n", sol1fil1);
+    (void)fclose(sol1fil1);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 2                                   */
+
 /*******************************************************/
 
 void sol2fil1(void)
 {
-	FILE *sol2fil1; /* Rood_2.SOL file_variable */
-	sol2fil1 = fopen("Rood_2.SOL", "w");
-	fputs(" SWPHYS = 1\n\n", sol2fil1);
-	fputs(" COFGEN1 = 0.000\n", sol2fil1);
-	fputs(" COFGEN2 = 0.516\n", sol2fil1);
-	fputs(" COFGEN3 = 30.206\n", sol2fil1);
-	fputs(" COFGEN4 = 0.01216\n", sol2fil1);
-	fputs(" COFGEN5 = 0.046\n", sol2fil1);
-	fputs(" COFGEN6 = 1.1471\n", sol2fil1);
-	fputs(" COFGEN8 = 0.013\n", sol2fil1);
-	fputs("* End of file\n", sol2fil1);
-	(void)fclose(sol2fil1);
-	return;
+    FILE *sol2fil1;		/* Rood_2.SOL file_variable */
+
+    sol2fil1 = fopen("Rood_2.SOL", "w");
+    fputs(" SWPHYS = 1\n\n", sol2fil1);
+    fputs(" COFGEN1 = 0.000\n", sol2fil1);
+    fputs(" COFGEN2 = 0.516\n", sol2fil1);
+    fputs(" COFGEN3 = 30.206\n", sol2fil1);
+    fputs(" COFGEN4 = 0.01216\n", sol2fil1);
+    fputs(" COFGEN5 = 0.046\n", sol2fil1);
+    fputs(" COFGEN6 = 1.1471\n", sol2fil1);
+    fputs(" COFGEN8 = 0.013\n", sol2fil1);
+    fputs("* End of file\n", sol2fil1);
+    (void)fclose(sol2fil1);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 3                                   */
+
 /*******************************************************/
 
 void sol3fil1(void)
 {
-	FILE *sol3fil1; /* Rood_3.SOL file_variable */
-	sol3fil1 = fopen("Rood_3.SOL", "w");
-	fputs(" SWPHYS = 1\n\n", sol3fil1);
-	fputs(" COFGEN1 = 0.000\n", sol3fil1);
-	fputs(" COFGEN2 = 0.501\n", sol3fil1);
-	fputs(" COFGEN3 = 2.295\n", sol3fil1);
-	fputs(" COFGEN4 = 0.00778\n", sol3fil1);
-	fputs(" COFGEN5 = 0.251\n", sol3fil1);
-	fputs(" COFGEN6 = 1.0829\n", sol3fil1);
-	fputs(" COFGEN8 = 0.009\n", sol3fil1);
-	fputs("* End of file\n", sol3fil1);
-	(void)fclose(sol3fil1);
-	return;
+    FILE *sol3fil1;		/* Rood_3.SOL file_variable */
+
+    sol3fil1 = fopen("Rood_3.SOL", "w");
+    fputs(" SWPHYS = 1\n\n", sol3fil1);
+    fputs(" COFGEN1 = 0.000\n", sol3fil1);
+    fputs(" COFGEN2 = 0.501\n", sol3fil1);
+    fputs(" COFGEN3 = 2.295\n", sol3fil1);
+    fputs(" COFGEN4 = 0.00778\n", sol3fil1);
+    fputs(" COFGEN5 = 0.251\n", sol3fil1);
+    fputs(" COFGEN6 = 1.0829\n", sol3fil1);
+    fputs(" COFGEN8 = 0.009\n", sol3fil1);
+    fputs("* End of file\n", sol3fil1);
+    (void)fclose(sol3fil1);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 4                                   */
+
 /*******************************************************/
 
 void sol4fil1(void)
 {
-	FILE *sol4fil1; /* Rood_4.SOL file_variable */
-	sol4fil1 = fopen("Rood_4.SOL", "w");
-	fputs(" SWPHYS = 1\n\n", sol4fil1);
-	fputs(" COFGEN1 = 0.000\n", sol4fil1);
-	fputs(" COFGEN2 = 0.431\n", sol4fil1);
-	fputs(" COFGEN3 = 4.331\n", sol4fil1);
-	fputs(" COFGEN4 = 0.01379\n", sol4fil1);
-	fputs(" COFGEN5 = -2.743\n", sol4fil1);
-	fputs(" COFGEN6 = 1.0818\n", sol4fil1);
-	fputs(" COFGEN8 = 0.015\n", sol4fil1);
-	fputs("* End of file\n", sol4fil1);
-	(void)fclose(sol4fil1);
-	return;
+    FILE *sol4fil1;		/* Rood_4.SOL file_variable */
+
+    sol4fil1 = fopen("Rood_4.SOL", "w");
+    fputs(" SWPHYS = 1\n\n", sol4fil1);
+    fputs(" COFGEN1 = 0.000\n", sol4fil1);
+    fputs(" COFGEN2 = 0.431\n", sol4fil1);
+    fputs(" COFGEN3 = 4.331\n", sol4fil1);
+    fputs(" COFGEN4 = 0.01379\n", sol4fil1);
+    fputs(" COFGEN5 = -2.743\n", sol4fil1);
+    fputs(" COFGEN6 = 1.0818\n", sol4fil1);
+    fputs(" COFGEN8 = 0.015\n", sol4fil1);
+    fputs("* End of file\n", sol4fil1);
+    (void)fclose(sol4fil1);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 5                                   */
+
 /*******************************************************/
 
 void sol5fil1(void)
 {
-	FILE *sol5fil1; /* Rood_5.SOL file_variable */
-	sol5fil1 = fopen("Rood_5.SOL", "w");
-	fputs(" SWPHYS = 1\n\n", sol5fil1);
-	fputs(" COFGEN1 = 0.000\n", sol5fil1);
-	fputs(" COFGEN2 = 0.424\n", sol5fil1);
-	fputs(" COFGEN3 = 9.843\n", sol5fil1);
-	fputs(" COFGEN4 = 0.01667\n", sol5fil1);
-	fputs(" COFGEN5 = -1.982\n", sol5fil1);
-	fputs(" COFGEN6 = 1.1307\n", sol5fil1);
-	fputs(" COFGEN8 = 0.018\n", sol5fil1);
-	fputs("* End of file\n", sol5fil1);
-	(void)fclose(sol5fil1);
-	return;
+    FILE *sol5fil1;		/* Rood_5.SOL file_variable */
+
+    sol5fil1 = fopen("Rood_5.SOL", "w");
+    fputs(" SWPHYS = 1\n\n", sol5fil1);
+    fputs(" COFGEN1 = 0.000\n", sol5fil1);
+    fputs(" COFGEN2 = 0.424\n", sol5fil1);
+    fputs(" COFGEN3 = 9.843\n", sol5fil1);
+    fputs(" COFGEN4 = 0.01667\n", sol5fil1);
+    fputs(" COFGEN5 = -1.982\n", sol5fil1);
+    fputs(" COFGEN6 = 1.1307\n", sol5fil1);
+    fputs(" COFGEN8 = 0.018\n", sol5fil1);
+    fputs("* End of file\n", sol5fil1);
+    (void)fclose(sol5fil1);
+    return;
 }
+
 /********************************************************/

Modified: grass-addons/gipe/r.gaswap.serial/gaswap.c
===================================================================
--- grass-addons/gipe/r.gaswap.serial/gaswap.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.gaswap.serial/gaswap.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /***************************************************************/
 /* This is a simple genetic algorithm implementation where the */
 /* evaluation function takes positive values only and the      */
@@ -3,12 +4,16 @@
 /* fitness of an individual is the same as the value of the    */
 /* objective function (after Michalewicz, 1996)                */
+
 /***************************************************************/
 /* Added SWAP input file creation - finput() - September 2003  */
+
 /***************************************************************/
-/* Added SWAP output ETa file creation - foutput() - October 03*/
+/* Added SWAP output ETa file creation - foutput() - October 03 */
+
 /***************************************************************/
 /* Copyleft LGPL Yann Chemin - yann.chemin at ait.ac.th 2003      */
 /* ychemin at yahoo.com - Code enhancements welcome!              */
-/* Modified by Shamim Akhter - shamimakhter at gmail.com 2005     */ 
+/* Modified by Shamim Akhter - shamimakhter at gmail.com 2005     */
+
 /***************************************************************/
 
@@ -17,91 +22,139 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <string.h> /*foutput()*/
-#include <time.h>   /*foutput()*/
-#include <unistd.h> /*sleep() in evaluate()*/
+#include <string.h>		/*foutput() */
+#include <time.h>		/*foutput() */
+#include <unistd.h>		/*sleep() in evaluate() */
 #include "gaswap.h"
 
 #define TRUE 1
 #define FALSE 0
 
 
-int generation;		/* current generation number */
-int cur_best;		/* best individual */
-FILE *galog;		
+int generation;			/* current generation number */
 
+int cur_best;			/* best individual */
 
+FILE *galog;
 
+
+
 /* the SWAP variables to optimize */
-	int DECd, DECm, DECy;
-	int FECd, FECm, FECy;
-	int STSd, STSm, STSy;
-	int DECdoy, FECdoy, STSdoy;
-	int doy; /* used in date2doy() */
-	int day, month; /* used in doy2date() */
-	int TECdoy; /*cropping season time extent*/
-	int GWJan, GWDec; /* GW level values in 01Jan & 31Dec */
+int DECd, DECm, DECy;
 
+int FECd, FECm, FECy;
 
-//char *dateName[257]; 	/*foutput()*/
-//char *dateNameValue; 	/*foutput()*/
+int STSd, STSm, STSy;
+
+int DECdoy, FECdoy, STSdoy;
+
+int doy;			/* used in date2doy() */
+
+int day, month;			/* used in doy2date() */
+
+int TECdoy;			/*cropping season time extent */
+
+int GWJan, GWDec;		/* GW level values in 01Jan & 31Dec */
+
+
+//char *dateName[257];  /*foutput()*/
+//char *dateNameValue;  /*foutput()*/
 char **satDATE;
-int dateCounter;		/*foutput()*/
-double swapETA[367]; 	/*foutput()*/
-double satETA[367]; 	/*foutput()*/
+
+int dateCounter;		/*foutput() */
+
+double swapETA[367];		/*foutput() */
+
+double satETA[367];		/*foutput() */
+
 double tex[4];
 
-int * gene_tmp;
-double * fitness_tmp;
+int *gene_tmp;
 
-int POPSIZE; 		/* population size */
-int MAXGENS; 		/* max. number of generations */
-int NVARS;        	/* max. number of variables */  
-int NSATEL;       	/* max. number of satellite images*/ 
+double *fitness_tmp;
+
+int POPSIZE;			/* population size */
+
+int MAXGENS;			/* max. number of generations */
+
+int NVARS;			/* max. number of variables */
+
+int NSATEL;			/* max. number of satellite images */
+
 int YEAR;
-int SSRUN,ESRUN;
-double LAT,ALT;
+
+int SSRUN, ESRUN;
+
+double LAT, ALT;
+
 char METFIL[100];
 
-double PXOVER;		/* probability of crossover */
+double PXOVER;			/* probability of crossover */
+
 double PMUTATION;		/* probability of mutation */
-double TARGET_FITNESS;  /* fitness value that will stop GA */
 
+double TARGET_FITNESS;		/* fitness value that will stop GA */
 
+
 /* Declaration of procedures used by this genetic algorithm */
 void initialize(struct genotype *population);
+
 void evaluate_init(struct genotype *population);
-void evaluate(int popsize, int nvar, int *gene, double *fitness, int DECy, int nsatel, double text[], double ETA[], char **DATE, int ssrun, int esrun, double lat, double alt, char *metfile);
 
+void evaluate(int popsize, int nvar, int *gene, double *fitness, int DECy,
+	      int nsatel, double text[], double ETA[], char **DATE, int ssrun,
+	      int esrun, double lat, double alt, char *metfile);
+
 void keep_the_best(struct genotype *population);
+
 void elitist(struct genotype *population);
+
 void selection(struct genotype *population, struct genotype *newpopulation);
+
 void crossover(struct genotype *population);
+
 void mutate(struct genotype *population);
+
 void report(struct genotype *population, int generation);
 
 void makegrasscell(struct genotype **population);
 
 int doy2date(int *d, int *m);
+
 int date2doy(int *d, int *m);
 
 void finput1(void);
+
 void finput2(void);
+
 void swapkey(void);
+
 void calfil(void);
+
 void capfil(void);
+
 void swafil(void);
+
 void bbcfil(void);
+
 void crpfil(void);
+
 void sol1fil(void);
+
 void sol2fil(void);
+
 void sol3fil(void);
+
 void sol4fil(void);
+
 void sol5fil(void);
 
 int foutput(void);
+
 int get_sat_eta(void);
+
 int satellite_dates(void);
+
 int get_eta(char *dateName, char *dateNameValue, int dateCounter);
 
 /**************************************************************/
@@ -109,433 +162,461 @@
 /* members, performing crossover & mutation and then          */
 /* evaluating the resulting population, until the terminating */
 /* condition is satisfied.                                    */
+
 /**************************************************************/
 
 
-void gaswap(double texture[], double sat[], double satDOY[],int NVAR,int NSAT,int year, int pop,int maxgen,double pmute, double pxover,double target_fitness,int syear,int eyear,char *METRO, double Lati,double Alt, double *grass_c){
+void gaswap(double texture[], double sat[], double satDOY[], int NVAR,
+	    int NSAT, int year, int pop, int maxgen, double pmute,
+	    double pxover, double target_fitness, int syear, int eyear,
+	    char *METRO, double Lati, double Alt, double *grass_c)
+{
 
-	int i;
+    int i;
 
-	POPSIZE=pop;
-	MAXGENS=maxgen;
-	PXOVER=pxover;
-	PMUTATION=pmute;
-	TARGET_FITNESS=target_fitness;
-	NVARS=NVAR;
-	NSATEL=NSAT;
-	YEAR=year;
-	SSRUN=syear;
-	ESRUN=eyear;
-	LAT =Lati;
-	ALT=Alt;
-	//////////////////////
-	i=0;
-	while(!(METRO[i]==0)){ 
-		METFIL[i]=METRO[i];
-		i++;
-	} 
-	METFIL[i]=0;
-	generation=0;
-	DECy=YEAR;
-	FECy=YEAR;
-	STSy=YEAR;
-	struct genotype *population,*newpopulation;
-	//printf("start MALLOC\n");
-	/* malloc the population[number][size] and newpopulation[number][size] */
-	if((population = malloc((POPSIZE+1)*sizeof(struct genotype)))==NULL){
-		printf("Ooops malloc1.1 == null...\n");
-		exit(1);
+    POPSIZE = pop;
+    MAXGENS = maxgen;
+    PXOVER = pxover;
+    PMUTATION = pmute;
+    TARGET_FITNESS = target_fitness;
+    NVARS = NVAR;
+    NSATEL = NSAT;
+    YEAR = year;
+    SSRUN = syear;
+    ESRUN = eyear;
+    LAT = Lati;
+    ALT = Alt;
+    //////////////////////
+    i = 0;
+    while (!(METRO[i] == 0)) {
+	METFIL[i] = METRO[i];
+	i++;
+    }
+    METFIL[i] = 0;
+    generation = 0;
+    DECy = YEAR;
+    FECy = YEAR;
+    STSy = YEAR;
+    struct genotype *population, *newpopulation;
+
+    //printf("start MALLOC\n");
+    /* malloc the population[number][size] and newpopulation[number][size] */
+    if ((population =
+	 malloc((POPSIZE + 1) * sizeof(struct genotype))) == NULL) {
+	printf("Ooops malloc1.1 == null...\n");
+	exit(1);
+    }
+
+    for (i = 0; i <= POPSIZE; i++) {
+	population[i].gene = malloc((NVARS) * sizeof(int));
+	population[i].upper = malloc((NVARS) * sizeof(double));
+	population[i].lower = malloc((NVARS) * sizeof(double));
+    }
+
+    if ((newpopulation =
+	 malloc((POPSIZE + 1) * sizeof(struct genotype))) == NULL) {
+	printf("Ooops malloc2.1 == null...\n");
+	//exit(1);
+    }
+    for (i = 0; i <= POPSIZE; i++) {
+	newpopulation[i].gene = malloc((NVARS) * sizeof(int));
+	newpopulation[i].upper = malloc((NVARS) * sizeof(double));
+	newpopulation[i].lower = malloc((NVARS) * sizeof(double));
+    }
+
+    satDATE = (char **)malloc((NSATEL + 1) * sizeof(char *));
+
+    for (i = 0; i < NSATEL + 1; i++)
+	satDATE[i] = (char *)malloc(367 * sizeof(char));
+
+    //printf("converting satDOY to satDATES\n");
+    /* Converting the SATdoy to SATdates */
+    int satdoy_var;
+
+    for (i = 1; i <= NSATEL; i++) {
+	//printf("converting satDOY to satDATES 1.%i\n",i);
+	satETA[i] = sat[i];
+	//printf("converting satDOY to satDATES 2.%i\n",i);
+	satdoy_var = satDOY[i];
+	//printf("converting satDOY to satDATES 3.%i\n",i);
+	doy2date(&satdoy_var, &DECy);
+	//printf("converting satDOY to satDATES 4.%i\n",i);
+	satDOY[i] = satdoy_var;
+	//printf("converting satDOY to satDATES 5.%i\n",i);
+	//printf("day=%d month=%d DEcy=%d\n",day,month,DECy);
+	sprintf(satDATE[i], "%d/%d/%d", day, month, DECy);
+	//printf("converte satDATES %s\n",satDATE[i]);
+	day = 0;
+	month = 0;
+
+    }
+    //printf("End of converting satDOY to satDATES\n");
+    for (i = 1; i <= NSATEL; i++) {
+	satETA[i] = sat[i];
+	//printf("satETA %f\n",satETA[i]);
+    }
+    //printf("End of converting NSATEL\n");
+    for (i = 0; i < 4; i++)
+	tex[i] = texture[i];
+
+    generation = 0;
+    srand(getuid() * getpid() % getppid());
+    initialize(population);
+    evaluate_init(population);
+    keep_the_best(population);
+    gene_tmp = (int *)malloc(sizeof(int) * ((POPSIZE + 1) * NVARS));
+    fitness_tmp = (double *)malloc(sizeof(double) * (POPSIZE + 1));
+    generation = 0;
+
+    while (generation < MAXGENS) {
+	generation++;
+	selection(population, newpopulation);
+	crossover(population);
+	mutate(population);
+	int c1, c2;
+
+	for (c1 = 0; c1 < POPSIZE + 1; c1++)
+	    for (c2 = 0; c2 < NVARS; c2++) {
+		*(gene_tmp + (c1 * NVARS) + c2) = population[c1].gene[c2];
+		//printf("population[%d].gene[%d]=%d ",c1,c2,population[c1].gene[c2]);
+	    }
+	for (c1 = 0; c1 < POPSIZE + 1; c1++) {
+	    fitness_tmp[c1] = population[c1].fitness;
+	    printf("population[%d].fitness=%f  ", c1, population[c1].fitness);
 	}
-	
-	for(i=0;i<=POPSIZE;i++)
-	{
-		population[i].gene=malloc((NVARS)*sizeof(int ));
-		population[i].upper=malloc((NVARS)*sizeof(double ));
-		population[i].lower=malloc((NVARS)*sizeof(double ));
-	}
-	
-	if((newpopulation = malloc((POPSIZE+1)*sizeof(struct genotype)))==NULL){
-		printf("Ooops malloc2.1 == null...\n");
-		//exit(1);
-	}
-	for(i=0;i<=POPSIZE;i++)
-	{
-		newpopulation[i].gene=malloc((NVARS)*sizeof(int));
-		newpopulation[i].upper=malloc((NVARS)*sizeof(double));
-		newpopulation[i].lower=malloc((NVARS)*sizeof(double));
-	}
-	
-	satDATE = (char **)malloc((NSATEL+1)*sizeof(char *));
+	evaluate(POPSIZE, NVARS, gene_tmp, fitness_tmp, DECy, NSATEL, tex,
+		 satETA, satDATE, SSRUN, ESRUN, LAT, ALT, METFIL);
 
-        for(i=0;i<NSATEL+1;i++)
-		satDATE[i] = (char *)malloc(367*sizeof(char));
-	
-	//printf("converting satDOY to satDATES\n");
-	/* Converting the SATdoy to SATdates*/
-	int satdoy_var;
-	for(i=1; i<=NSATEL;i++){
-		//printf("converting satDOY to satDATES 1.%i\n",i);
-		satETA[i]=sat[i];
-		//printf("converting satDOY to satDATES 2.%i\n",i);
-		satdoy_var=satDOY[i];
-		//printf("converting satDOY to satDATES 3.%i\n",i);
-		doy2date(&satdoy_var,&DECy);
-		//printf("converting satDOY to satDATES 4.%i\n",i);
-		satDOY[i]=satdoy_var;
-		//printf("converting satDOY to satDATES 5.%i\n",i);
-		//printf("day=%d month=%d DEcy=%d\n",day,month,DECy);
-		sprintf(satDATE[i],"%d/%d/%d",day,month,DECy);	
-		//printf("converte satDATES %s\n",satDATE[i]);
-		day=0;
-		month=0;		
-		
+	for (c1 = 0; c1 < POPSIZE + 1; c1++) {
+	    population[c1].fitness = fitness_tmp[c1];
+	    printf("population[%d].fitness=%f  ", c1, population[c1].fitness);
 	}
-	//printf("End of converting satDOY to satDATES\n");
-	for(i=1; i<=NSATEL;i++){
-		satETA[i]=sat[i];
-		//printf("satETA %f\n",satETA[i]);
-	}
-	//printf("End of converting NSATEL\n");
-	for(i=0;i<4;i++)
-		tex[i]=texture[i];
-	
-	generation=0;
-	srand(getuid()*getpid()%getppid());
-	initialize(population);
-	evaluate_init(population);
-	keep_the_best(population);
-	gene_tmp = (int *) malloc(sizeof(int) * ((POPSIZE + 1) * NVARS ));
-	fitness_tmp = (double *) malloc(sizeof(double) * (POPSIZE +1));
-	generation=0;
+	elitist(population);
+    }
+    int k;
 
-	while(generation < MAXGENS)
-	{
-		generation++;
-		selection(population,newpopulation);
-		crossover(population);
-		mutate(population);
-		int c1,c2;
-		
-		for (c1=0; c1<POPSIZE+1; c1++)
-			for (c2=0; c2<NVARS; c2++) {
-				*(gene_tmp + (c1 * NVARS) + c2) = population[c1].gene[c2];
-				//printf("population[%d].gene[%d]=%d ",c1,c2,population[c1].gene[c2]);
-			}
-		for (c1=0; c1<POPSIZE+1; c1++) {
-			fitness_tmp[c1] = population[c1].fitness;
-			printf("population[%d].fitness=%f  ",c1,population[c1].fitness);
-		}
-		evaluate(POPSIZE, NVARS, gene_tmp, fitness_tmp, DECy, NSATEL, tex, satETA, satDATE, SSRUN, ESRUN, LAT, ALT, METFIL);
+    for (k = 1; k < NVARS; k++) {
+	grass_c[k] = (population[POPSIZE].gene[k - 1]);
+	//printf("Inside GASWAP %f\n", grass_c[k]);
+    }
+    grass_c[k] = population[POPSIZE].fitness;
 
-		for (c1=0; c1<POPSIZE+1; c1++){
-			population[c1].fitness = fitness_tmp[c1];
-			printf("population[%d].fitness=%f  ",c1,population[c1].fitness);
-		}
-		elitist(population);
-	}
-	int k;
-	for(k=1;k<NVARS;k++){
-		grass_c[k]= (population[POPSIZE].gene[k-1]);
-		//printf("Inside GASWAP %f\n", grass_c[k]);
-	}
-	grass_c[k]=population[POPSIZE].fitness; 
-	
-	free(gene_tmp);
-	free(fitness_tmp);
-	free(satDATE);
-	free(population);
-	free(newpopulation);
-	return;
+    free(gene_tmp);
+    free(fitness_tmp);
+    free(satDATE);
+    free(population);
+    free(newpopulation);
+    return;
 }
 
 void initialize(struct genotype *population)
 {
-	int i,j;
-	double lbound[NVARS], ubound[NVARS];
+    int i, j;
 
-	population[POPSIZE].fitness = 0.0;/*init best member fitness*/
+    double lbound[NVARS], ubound[NVARS];
 
-	for(i=0;i<POPSIZE;i++)
-	{
-		for(j=0;j<NVARS;j++)
-		{
-			
-			if (j==0){
-				lbound[j] = 60.0;
-				ubound[j] = 120.0;
-			} if (j==1){
-				lbound[j] = 90.0;
-				ubound[j] = 150.0;
-			} if (j==2) {
-				lbound[j] = 160.0;
-				ubound[j] = 200.0;
-			} if (j==3 || j==4){
-				lbound[j] = 140.0;
-				ubound[j] = 160.0;
-			} if (j==7 || j==8){
-				lbound[j] = 10.0;		
-				ubound[j] = 200.0;	
-			} if (j==9 || j==10){
-	
+    population[POPSIZE].fitness = 0.0;	/*init best member fitness */
 
-			}
-			
-			population[i].lower[j]=0.0;
-			population[i].upper[j]=0.0;
-			population[i].fitness=0.0;
-			population[i].rfitness=0.0;
-			population[i].cfitness=0.0;
-			population[i].lower[j]=lbound[j];
-			population[i].upper[j]=ubound[j];
-			population[i].gene[j]=(int) ((rand()%1000/1000.0)*(population[i].upper[j]-population[i].lower[j])+population[i].lower[j]);
+    for (i = 0; i < POPSIZE; i++) {
+	for (j = 0; j < NVARS; j++) {
 
-		}
+	    if (j == 0) {
+		lbound[j] = 60.0;
+		ubound[j] = 120.0;
+	    }
+	    if (j == 1) {
+		lbound[j] = 90.0;
+		ubound[j] = 150.0;
+	    }
+	    if (j == 2) {
+		lbound[j] = 160.0;
+		ubound[j] = 200.0;
+	    }
+	    if (j == 3 || j == 4) {
+		lbound[j] = 140.0;
+		ubound[j] = 160.0;
+	    }
+	    if (j == 7 || j == 8) {
+		lbound[j] = 10.0;
+		ubound[j] = 200.0;
+	    }
+	    if (j == 9 || j == 10) {
+
+
+	    }
+
+	    population[i].lower[j] = 0.0;
+	    population[i].upper[j] = 0.0;
+	    population[i].fitness = 0.0;
+	    population[i].rfitness = 0.0;
+	    population[i].cfitness = 0.0;
+	    population[i].lower[j] = lbound[j];
+	    population[i].upper[j] = ubound[j];
+	    population[i].gene[j] =
+		(int)((rand() % 1000 / 1000.0) *
+		      (population[i].upper[j] - population[i].lower[j]) +
+		      population[i].lower[j]);
+
 	}
+    }
 }
 
 
 /*************************************************************/
 /* Evaluation function: this takes a user defined function.  */
 /* Each time this is changed, the code has to be recompiled. */
-/* The current function is: square (sat[ETA]-swap[ETA])      */ 
+/* The current function is: square (sat[ETA]-swap[ETA])      */
+
 /*************************************************************/
 
 void evaluate_init(struct genotype *population)
 {
-	int mem, i;
-	double fit_value, dif_value;
+    int mem, i;
 
-	for(mem=0;mem<POPSIZE;mem++)
-	{
-		fit_value=0;
-		DECdoy =(int) (population[mem].gene[0]);
-		STSdoy =(int) (population[mem].gene[1]);
-		TECdoy =(int) (population[mem].gene[2]);
-		GWJan =(int) (population[mem].gene[3]);
-		GWDec =(int) (population[mem].gene[4]);	
-			
-		/* Create the FECd and FECm from DEC & TEC */
+    double fit_value, dif_value;
+
+    for (mem = 0; mem < POPSIZE; mem++) {
+	fit_value = 0;
+	DECdoy = (int)(population[mem].gene[0]);
+	STSdoy = (int)(population[mem].gene[1]);
+	TECdoy = (int)(population[mem].gene[2]);
+	GWJan = (int)(population[mem].gene[3]);
+	GWDec = (int)(population[mem].gene[4]);
+
+	/* Create the FECd and FECm from DEC & TEC */
+
 		/*******************************************/
-		/* Get the FECdoy = DECdoy + TECdoy */
-		FECdoy=DECdoy+TECdoy;
-		/* leap year */
-		if (DECy/4*4 == DECy && FECdoy > 366){
-			FECd = 31.0;
-			FECm = 12.0;
-			FECy = DECy + 1;
-		}
-		/* normal year */
-		if (DECy/4*4 != DECy && FECdoy > 365){
-			FECd = 31.0;
-			FECm = 12.0;
-			FECy = DECy + 1;
-		}
-		else {
-			/* Get the FECd and FECm */
-			doy2date(&FECdoy,&DECy);
-			FECd=day;
-			FECm=month;
-			day=0;
-			month=0;
-		}
-		FECdoy=0;
+	/* Get the FECdoy = DECdoy + TECdoy */
+	FECdoy = DECdoy + TECdoy;
+	/* leap year */
+	if (DECy / 4 * 4 == DECy && FECdoy > 366) {
+	    FECd = 31.0;
+	    FECm = 12.0;
+	    FECy = DECy + 1;
+	}
+	/* normal year */
+	if (DECy / 4 * 4 != DECy && FECdoy > 365) {
+	    FECd = 31.0;
+	    FECm = 12.0;
+	    FECy = DECy + 1;
+	}
+	else {
+	    /* Get the FECd and FECm */
+	    doy2date(&FECdoy, &DECy);
+	    FECd = day;
+	    FECm = month;
+	    day = 0;
+	    month = 0;
+	}
+	FECdoy = 0;
+
 		/*************************/
-			
-		doy2date(&DECdoy,&DECy);
-		
-		DECd=day;
-		DECm=month;
-		day=0;
-		month=0;
-	
-		doy2date(&STSdoy,&STSy);
-		
-		STSd=day;
-		STSm=month;
-		day=0;
-		month=0;
-		
-		if (DECd==0||DECm==0||FECd==0||FECm==0||STSd==0||STSm==0||GWJan==0||GWDec==0) {
-			population[mem].fitness=0.0;
-			initialize(population);
-		} else {
-			if(generation<2){
-				finput1();
-			} 
-			else {
-				finput2();
-			}
-			system("./swap");
-			foutput();
-			for(i=1;i<=NSATEL;i++) {
-				dif_value=(int) (1000.0*(satETA[i]-swapETA[i]));
-				fit_value=fit_value+( (double) (fabs(dif_value)/1000.0));// *(double) (fabs(dif_value)/1000.0));
-			}
-			fit_value=fit_value/(double) NSATEL;
-			population[mem].fitness=(fit_value);
-		}
+
+	doy2date(&DECdoy, &DECy);
+
+	DECd = day;
+	DECm = month;
+	day = 0;
+	month = 0;
+
+	doy2date(&STSdoy, &STSy);
+
+	STSd = day;
+	STSm = month;
+	day = 0;
+	month = 0;
+
+	if (DECd == 0 || DECm == 0 || FECd == 0 || FECm == 0 || STSd == 0 ||
+	    STSm == 0 || GWJan == 0 || GWDec == 0) {
+	    population[mem].fitness = 0.0;
+	    initialize(population);
 	}
+	else {
+	    if (generation < 2) {
+		finput1();
+	    }
+	    else {
+		finput2();
+	    }
+	    system("./swap");
+	    foutput();
+	    for (i = 1; i <= NSATEL; i++) {
+		dif_value = (int)(1000.0 * (satETA[i] - swapETA[i]));
+		fit_value = fit_value + ((double)(fabs(dif_value) / 1000.0));	// *(double) (fabs(dif_value)/1000.0));
+	    }
+	    fit_value = fit_value / (double)NSATEL;
+	    population[mem].fitness = (fit_value);
+	}
+    }
 }
 
 /*********************************************/
 int doy2date(int *d, int *m)
 {
+
 	/*********************************************/
-	/*This routine converts doy to day/month/year*/
+    /*This routine converts doy to day/month/year */
+
 	/*********************************************/
 
-	int leap = 0;
-	int year;
-	
-	day = 0;
-	month = 0;
-	doy = *d;
-	year = *m;
-	
-	/* Leap year if dividing by 4 leads % 0.0*/
-	if (year/4*4 == year) {
-		leap = 1;
-	}
+    int leap = 0;
 
-	if (doy < 32) {
-		month = 1;
-		day = doy;
-	}
-	else if (doy > 31 && doy < (60+leap)) {
-		month = 2;
-		day = doy - 31;
-	}
-	else if (doy > (59+leap) && doy < (91+leap)) {
-		month = 3;
-		day = doy - (59+leap);
-	}
-	else if (doy > (90+leap) && doy < (121+leap)) {
-		month = 4;
-		day = doy - (90+leap);
-	} 
-	else if (doy > (120+leap) && doy < (152+leap)) {
-		month = 5;
-		day = doy - (120+leap);
-	}
-	else if (doy > (151+leap) && doy < (182+leap)) {
-		month = 6;
-		day = doy - (151+leap);
-	}
-	else if (doy > (181+leap) && doy < (213+leap)) {
-		month = 7;
-		day = doy - (181+leap);
-	}
-	else if (doy > (212+leap) && doy < (244+leap)) {
-		month = 8;
-		day = doy - (212+leap);
-	}
-	else if (doy > (243+leap) && doy < (274+leap)) {
-		month = 9;
-		day = doy - (243+leap);
-	}
-	else if (doy > (273+leap) && doy < (305+leap)) {
-		month = 10;
-		day = doy - (273+leap);
-	}
-	else if (doy > (304+leap) && doy < (335+leap)) {
-		month = 11;
-		day = doy - (304+leap);
-	}
-	else if (doy > 334) {
-		month = 12;
-		day = doy - (334+leap);
-	}
-return 1;
+    int year;
 
+    day = 0;
+    month = 0;
+    doy = *d;
+    year = *m;
+
+    /* Leap year if dividing by 4 leads % 0.0 */
+    if (year / 4 * 4 == year) {
+	leap = 1;
+    }
+
+    if (doy < 32) {
+	month = 1;
+	day = doy;
+    }
+    else if (doy > 31 && doy < (60 + leap)) {
+	month = 2;
+	day = doy - 31;
+    }
+    else if (doy > (59 + leap) && doy < (91 + leap)) {
+	month = 3;
+	day = doy - (59 + leap);
+    }
+    else if (doy > (90 + leap) && doy < (121 + leap)) {
+	month = 4;
+	day = doy - (90 + leap);
+    }
+    else if (doy > (120 + leap) && doy < (152 + leap)) {
+	month = 5;
+	day = doy - (120 + leap);
+    }
+    else if (doy > (151 + leap) && doy < (182 + leap)) {
+	month = 6;
+	day = doy - (151 + leap);
+    }
+    else if (doy > (181 + leap) && doy < (213 + leap)) {
+	month = 7;
+	day = doy - (181 + leap);
+    }
+    else if (doy > (212 + leap) && doy < (244 + leap)) {
+	month = 8;
+	day = doy - (212 + leap);
+    }
+    else if (doy > (243 + leap) && doy < (274 + leap)) {
+	month = 9;
+	day = doy - (243 + leap);
+    }
+    else if (doy > (273 + leap) && doy < (305 + leap)) {
+	month = 10;
+	day = doy - (273 + leap);
+    }
+    else if (doy > (304 + leap) && doy < (335 + leap)) {
+	month = 11;
+	day = doy - (304 + leap);
+    }
+    else if (doy > 334) {
+	month = 12;
+	day = doy - (334 + leap);
+    }
+    return 1;
+
 }
 
 /*********************************************/
-/*This program converts day/month/year to doy*/
+/*This program converts day/month/year to doy */
+
 /*********************************************/
+
 /*********************************************/
 
 int date2doy(int *d, int *m)
 {
-	int leap=0;
-	int day_month_tot=0;
-	int day, month;
+    int leap = 0;
 
-	doy=0;
-	day = *d;
-	month = *m;
+    int day_month_tot = 0;
 
-	if (month == 1) {
-		day_month_tot = 0;
-	}
-	else if (month == 2) {
-		day_month_tot = 31;
-	}
-	else if (month == 3) {
-		day_month_tot = 59;
-	}
-	else if (month == 4) {
-		day_month_tot = 90;
-	}
-	else if (month == 5) {
-		day_month_tot = 120;
-	}
-	else if (month == 6) {
-		day_month_tot = 151;
-	}
-	else if (month == 7) {
-		day_month_tot = 181;
-	}
-	else if (month == 8) {
-		day_month_tot = 212;
-	}
-	else if (month == 9) {
-		day_month_tot = 243;
-	}
-	else if (month == 10) {
-		day_month_tot = 273;
-	}
-	else if (month == 11) {
-		day_month_tot = 304;
-	}
-	else if (month == 12) {
-		day_month_tot = 334;
-	}
+    int day, month;
 
-	/* Leap year if dividing by 4 leads % 0.0*/
-	if (DECy/4*4 == DECy) {
-		leap = 1;
-	}
+    doy = 0;
+    day = *d;
+    month = *m;
 
-	doy=day_month_tot+day+leap;
+    if (month == 1) {
+	day_month_tot = 0;
+    }
+    else if (month == 2) {
+	day_month_tot = 31;
+    }
+    else if (month == 3) {
+	day_month_tot = 59;
+    }
+    else if (month == 4) {
+	day_month_tot = 90;
+    }
+    else if (month == 5) {
+	day_month_tot = 120;
+    }
+    else if (month == 6) {
+	day_month_tot = 151;
+    }
+    else if (month == 7) {
+	day_month_tot = 181;
+    }
+    else if (month == 8) {
+	day_month_tot = 212;
+    }
+    else if (month == 9) {
+	day_month_tot = 243;
+    }
+    else if (month == 10) {
+	day_month_tot = 273;
+    }
+    else if (month == 11) {
+	day_month_tot = 304;
+    }
+    else if (month == 12) {
+	day_month_tot = 334;
+    }
 
-	return(doy);
+    /* Leap year if dividing by 4 leads % 0.0 */
+    if (DECy / 4 * 4 == DECy) {
+	leap = 1;
+    }
+
+    doy = day_month_tot + day + leap;
+
+    return (doy);
 }
 
 /**************************************************************/
 /* Keep_the_best function: This function keeps track of the   */
 /* best member of the population. Note that the last entry in */
 /* the array Population holds a copy of the best individual.  */
+
 /**************************************************************/
 
 void keep_the_best(struct genotype *population)
 {
-	int mem;
-	int i;
-	cur_best=0; /* stores the index of the best individual */
+    int mem;
 
-	for (mem=0;mem<POPSIZE;mem++)
-	{	//yann you need to change here...
-		if (population[mem].fitness > population[POPSIZE].fitness)
-		{
-			cur_best=mem;
-			population[POPSIZE].fitness=population[mem].fitness;
-		}
+    int i;
+
+    cur_best = 0;		/* stores the index of the best individual */
+
+    for (mem = 0; mem < POPSIZE; mem++) {	//yann you need to change here...
+	if (population[mem].fitness > population[POPSIZE].fitness) {
+	    cur_best = mem;
+	    population[POPSIZE].fitness = population[mem].fitness;
 	}
+    }
 
-	/* once the best member of the population is found, copy the genes */
+    /* once the best member of the population is found, copy the genes */
 
-	for (i=0;i<NVARS;i++)
-		population[POPSIZE].gene[i]=population[cur_best].gene[i];
+    for (i = 0; i < NVARS; i++)
+	population[POPSIZE].gene[i] = population[cur_best].gene[i];
 }
 
 
@@ -545,74 +626,69 @@
 /* the current generation is worse then the best member of the  */
 /* previous generation, the latter one would replace the worst  */
 /* member in the new population.                                */
+
 /****************************************************************/
 
 void elitist(struct genotype *population)
 {
-	int i;
-	double best, worst;	/* best and worst fitness values */
-	int best_mem=0, worst_mem=0; /* indexes of the best and worst member */
+    int i;
 
-	best=population[0].fitness;
-	worst=population[0].fitness;
-	for(i=0;i<POPSIZE-1;i++)
-	{
-		if(population[i].fitness>population[i+1].fitness)
-		{
-			if(population[i].fitness>=best)
-			{
-				best=population[i].fitness;
-				best_mem=i;
-			}
-			if(population[i+1].fitness<=worst)
-			{
-				worst=population[i+1].fitness;
-				worst_mem=i+1;
-			}
-		}
-		else
-		{
-			if(population[i].fitness<=worst)
-			{
-				worst=population[i].fitness;
-				worst_mem=i;
-			}
-			if(population[i+1].fitness>=best)
-			{
-				best=population[i+1].fitness;
-				best_mem=i+1;
-			}
-		}
+    double best, worst;		/* best and worst fitness values */
+
+    int best_mem = 0, worst_mem = 0;	/* indexes of the best and worst member */
+
+    best = population[0].fitness;
+    worst = population[0].fitness;
+    for (i = 0; i < POPSIZE - 1; i++) {
+	if (population[i].fitness > population[i + 1].fitness) {
+	    if (population[i].fitness >= best) {
+		best = population[i].fitness;
+		best_mem = i;
+	    }
+	    if (population[i + 1].fitness <= worst) {
+		worst = population[i + 1].fitness;
+		worst_mem = i + 1;
+	    }
 	}
+	else {
+	    if (population[i].fitness <= worst) {
+		worst = population[i].fitness;
+		worst_mem = i;
+	    }
+	    if (population[i + 1].fitness >= best) {
+		best = population[i + 1].fitness;
+		best_mem = i + 1;
+	    }
+	}
+    }
 
-	for(i=0;i<POPSIZE;i++) {
-		if(population[i].fitness >= best) {
-			best = population[i].fitness;
-			best_mem = i;
-		} else if (population[i].fitness <= worst) {
-			worst = population[i].fitness;
-			worst_mem = i;
-		}
+    for (i = 0; i < POPSIZE; i++) {
+	if (population[i].fitness >= best) {
+	    best = population[i].fitness;
+	    best_mem = i;
 	}
+	else if (population[i].fitness <= worst) {
+	    worst = population[i].fitness;
+	    worst_mem = i;
+	}
+    }
 
-	/* If best individual from the new populationis better than */
-	/* the best individual from the previous population, then   */
-	/* copy the best from the new population; else replace the  */
-	/* worst individual from the current population with the    */
-	/* best one from the previous generation                    */
+    /* If best individual from the new populationis better than */
+    /* the best individual from the previous population, then   */
+    /* copy the best from the new population; else replace the  */
+    /* worst individual from the current population with the    */
+    /* best one from the previous generation                    */
 
-	if (best>=population[POPSIZE].fitness)
-	{
-		for(i=0;i<NVARS;i++)
-			population[POPSIZE].gene[i]=population[best_mem].gene[i];
-		population[POPSIZE].fitness=population[best_mem].fitness;
-	}
-	else
-	{
-		for(i=0;i<NVARS;i++)
-		population[worst_mem].gene[i]=population[POPSIZE].gene[i];
-		population[worst_mem].fitness=population[POPSIZE].fitness;
-	}
+    if (best >= population[POPSIZE].fitness) {
+	for (i = 0; i < NVARS; i++)
+	    population[POPSIZE].gene[i] = population[best_mem].gene[i];
+	population[POPSIZE].fitness = population[best_mem].fitness;
+    }
+    else {
+	for (i = 0; i < NVARS; i++)
+	    population[worst_mem].gene[i] = population[POPSIZE].gene[i];
+	population[worst_mem].fitness = population[POPSIZE].fitness;
+    }
 }
 
 
@@ -620,201 +696,216 @@
 /* Selection function: standard proportional selection for   */
 /* maximization problems incorporating elitist model - makes */
 /* sure that the best member survives.                       */
+
 /*************************************************************/
 void selection(struct genotype *population, struct genotype *newpopulation)
 {
-	int mem, i, j;
-	double sum=0;
-	double p;
+    int mem, i, j;
 
-	/* find total fitness of the population */
-	for (mem=0;mem<POPSIZE;mem++)
-	{
-		sum+=population[mem].fitness;
-	}
+    double sum = 0;
 
-	/* calculate relative fitness */
-	for(mem=0;mem<POPSIZE;mem++)
-	{
-		population[mem].rfitness=population[mem].fitness/sum;
-	}
-	population[0].cfitness=population[0].rfitness;
+    double p;
 
-	/* Calculate cumulative fitness */
-	for(mem=1;mem<POPSIZE;mem++)
-	{
-		population[mem].cfitness=population[mem-1].cfitness+population[mem].rfitness;
-	}
+    /* find total fitness of the population */
+    for (mem = 0; mem < POPSIZE; mem++) {
+	sum += population[mem].fitness;
+    }
 
-	/* finally select survivors using cumulative fitness. */
-	for(i=0;i<POPSIZE;i++)
-	{
-		p=rand()%1000/1000.0;
-		if(p<population[0].cfitness)
-			newpopulation[i]= population[0];
-		else
-		{
-			for(j=0;j<POPSIZE;j++)
-				if(p>=population[j].cfitness && p<=population[j+1].cfitness) 
-					newpopulation[i]=population[j+1];
-		}
+    /* calculate relative fitness */
+    for (mem = 0; mem < POPSIZE; mem++) {
+	population[mem].rfitness = population[mem].fitness / sum;
+    }
+    population[0].cfitness = population[0].rfitness;
+
+    /* Calculate cumulative fitness */
+    for (mem = 1; mem < POPSIZE; mem++) {
+	population[mem].cfitness =
+	    population[mem - 1].cfitness + population[mem].rfitness;
+    }
+
+    /* finally select survivors using cumulative fitness. */
+    for (i = 0; i < POPSIZE; i++) {
+	p = rand() % 1000 / 1000.0;
+	if (p < population[0].cfitness)
+	    newpopulation[i] = population[0];
+	else {
+	    for (j = 0; j < POPSIZE; j++)
+		if (p >= population[j].cfitness &&
+		    p <= population[j + 1].cfitness)
+		    newpopulation[i] = population[j + 1];
 	}
+    }
 
-	/* Once a new population is created, copy it back */
+    /* Once a new population is created, copy it back */
 
-	for(i=0;i<POPSIZE;i++)
-		population[i]=newpopulation[i];
+    for (i = 0; i < POPSIZE; i++)
+	population[i] = newpopulation[i];
 }
 
 /**************************************************************/
 /* Crossover selection: selects two parents that take part in */
 /* the crossover. Implements a single point crossover.        */
+
 /**************************************************************/
 
 void crossover(struct genotype *population)
 {
-	int  mem, one=0;
-	int first=0; /* Count of the numbers of members chosen */
-	int ii;
-	int point; /* crossover point */
-	int temp;
-	double x;
-	
-	for(mem=0;mem<POPSIZE;mem++)
-	{
-		x=rand()%1000/1000.0;
-		if(x<PXOVER)
-		{
-			++first;
-			if(first%2==0) 
-			{
-				//Xover(one,mem);
-				if(NVARS>1)
-				{
-					if(NVARS==2)
-						point=1;
-					else
-						point=(rand()%(NVARS-1))+1;
+    int mem, one = 0;
 
-					for(ii=0;ii<point;ii++)
-					{
-			
-						temp=population[one].gene[ii];
-						population[one].gene[ii]=population[mem].gene[ii];
-						population[mem].gene[ii]=temp;
-						
-					}
-				}
+    int first = 0;		/* Count of the numbers of members chosen */
 
-				
-				else
-					one=mem;
-			}//if nested
-		}//if
-	}//for
+    int ii;
 
+    int point;			/* crossover point */
+
+    int temp;
+
+    double x;
+
+    for (mem = 0; mem < POPSIZE; mem++) {
+	x = rand() % 1000 / 1000.0;
+	if (x < PXOVER) {
+	    ++first;
+	    if (first % 2 == 0) {
+		//Xover(one,mem);
+		if (NVARS > 1) {
+		    if (NVARS == 2)
+			point = 1;
+		    else
+			point = (rand() % (NVARS - 1)) + 1;
+
+		    for (ii = 0; ii < point; ii++) {
+
+			temp = population[one].gene[ii];
+			population[one].gene[ii] = population[mem].gene[ii];
+			population[mem].gene[ii] = temp;
+
+		    }
+		}
+
+
+		else
+		    one = mem;
+	    }			//if nested
+	}			//if
+    }				//for
+
 }
+
 /**************************************************************/
 /* Mutation: random uniform mutation. A variable selected for */
 /* mutation is replaced by a random value between lower and   */
 /* upper bounds of this variable.                             */
+
 /**************************************************************/
 
 void mutate(struct genotype *population)
 {
-	int i,j;
-	double x;
-	double lbound[NVARS], ubound[NVARS];
+    int i, j;
 
-	for(i=0;i<POPSIZE;i++)
-	{
-		for(j=0;j<NVARS;j++)
-		{
-			if (j==0){
-				lbound[j] = 60.0;
-				ubound[j] = 120.0;
-			} if (j==1){
-				lbound[j] = 90.0;
-				ubound[j] = 150.0;
-			} if (j==2){
-				lbound[j] = 160.0;
-				ubound[j] = 200.0;
-			} if (j==3 || j==4){
-				lbound[j] = 140.0;
-				ubound[j] = 160.0;
-			}
+    double x;
 
-			population[i].lower[j]=0.0;
-			population[i].upper[j]=0.0;
-			population[i].lower[j]=lbound[j];
-			population[i].upper[j]=ubound[j];
-		}
+    double lbound[NVARS], ubound[NVARS];
+
+    for (i = 0; i < POPSIZE; i++) {
+	for (j = 0; j < NVARS; j++) {
+	    if (j == 0) {
+		lbound[j] = 60.0;
+		ubound[j] = 120.0;
+	    }
+	    if (j == 1) {
+		lbound[j] = 90.0;
+		ubound[j] = 150.0;
+	    }
+	    if (j == 2) {
+		lbound[j] = 160.0;
+		ubound[j] = 200.0;
+	    }
+	    if (j == 3 || j == 4) {
+		lbound[j] = 140.0;
+		ubound[j] = 160.0;
+	    }
+
+	    population[i].lower[j] = 0.0;
+	    population[i].upper[j] = 0.0;
+	    population[i].lower[j] = lbound[j];
+	    population[i].upper[j] = ubound[j];
 	}
+    }
 
 
-	for(i=0;i<POPSIZE;i++)
-	{
-		for(j=0;j<NVARS;j++)
-		{
-			x=rand()%1000/1000.0;
-			if(x<PMUTATION)
-			{
-			/* Find the bounds on the variable to be mutated */
+    for (i = 0; i < POPSIZE; i++) {
+	for (j = 0; j < NVARS; j++) {
+	    x = rand() % 1000 / 1000.0;
+	    if (x < PMUTATION) {
+		/* Find the bounds on the variable to be mutated */
 
-	population[i].gene[j]=(rand()%1000/1000.0)*(population[i].upper[j]-population[i].lower[j])+population[i].lower[j];
-			}
-		}
+		population[i].gene[j] =
+		    (rand() % 1000 / 1000.0) * (population[i].upper[j] -
+						population[i].lower[j]) +
+		    population[i].lower[j];
+	    }
 	}
+    }
 }
 
 /**************************************************************/
 /* Report function: reports progress of the simulation. Data  */
 /* dumped into the output file are separated by commas        */
+
 /**************************************************************/
 
 void report(struct genotype *population, int generation)
 {
-	int i;
-	double best_val;	/* best population fitness */
-	double avg;		/* avg population fitness */
-	double stddev;		/* std. deviation of population fitness */
-	double sum_square;	/* sum of square for std. calc */
-	double square_sum;	/* square of sum for std. calc */
-	double sum;		/* total population fitness */
+    int i;
 
-	sum=0.0;
-	sum_square=0.0;
+    double best_val;		/* best population fitness */
 
-	for(i=0;i<POPSIZE;i++)
-	{
-		sum+=population[i].fitness;
-		sum_square+=population[i].fitness*population[i].fitness;
-	}
+    double avg;			/* avg population fitness */
 
-	avg=sum/(double)POPSIZE;
-	square_sum=avg*avg*(double)POPSIZE;
-	stddev=sqrt((sum_square-square_sum)/(POPSIZE-1));FECd=day;
-		FECm=month;
-	
-	best_val=population[POPSIZE].fitness;
+    double stddev;		/* std. deviation of population fitness */
 
-	fprintf(galog, "%5d\t	%6.3f\t %6.3f\t %6.3f \n", generation, best_val, avg, stddev);
+    double sum_square;		/* sum of square for std. calc */
+
+    double square_sum;		/* square of sum for std. calc */
+
+    double sum;			/* total population fitness */
+
+    sum = 0.0;
+    sum_square = 0.0;
+
+    for (i = 0; i < POPSIZE; i++) {
+	sum += population[i].fitness;
+	sum_square += population[i].fitness * population[i].fitness;
+    }
+
+    avg = sum / (double)POPSIZE;
+    square_sum = avg * avg * (double)POPSIZE;
+    stddev = sqrt((sum_square - square_sum) / (POPSIZE - 1));
+    FECd = day;
+    FECm = month;
+
+    best_val = population[POPSIZE].fitness;
+
+    fprintf(galog, "%5d\t	%6.3f\t %6.3f\t %6.3f \n", generation,
+	    best_val, avg, stddev);
 }
 
 /**************************************************************/
 /* MakeGrassCell function: output final result to a file that */
 /* will be used by the GRASS module to give values to cell    */
+
 /**************************************************************/
 
 //void makegrasscell(struct genotype *population)
 //{
-//	FILE *gc;
-//	int temp_doy;
+//      FILE *gc;
+//      int temp_doy;
 
-//	if((gc=fopen("grass_cell.txt","w"))==NULL)
-//	{
-//		exit(1);
-//	}
+//      if((gc=fopen("grass_cell.txt","w"))==NULL)
+//      {
+//              exit(1);
+//      }
 
 
 	/* Date of Emergence of Crop */
@@ -824,13 +915,13 @@
 	//grass_cell[0]=doy;
 
 	//fprintf(gc,"%i\n",doy);
-	
+
 	/* Irrigation Scheduling Start */
 	//date2doy(&population[POPSIZE].gene[2], &population[POPSIZE].gene[3]);
 
 	//grass_cell[1]=doy;
 	//fprintf(gc,"%i\n",doy);
-	
+
 	/* Date of Emergence of Crop + Time Extent of Cropping */
 	//doy = temp_doy + population[POPSIZE].gene[4];
 
@@ -841,17 +932,17 @@
 
 	//grass_cell[3]=population[POPSIZE].gene[5];
 	//fprintf(gc,"%i\n",population[POPSIZE].gene[5]);
-	
+
 	/* GW Level 31st Dec */
 	//grass_cell[4]=population[POPSIZE].gene[6];
 	//fprintf(gc,"%i\n",population[POPSIZE].gene[6]);
 
 	/* fitness of the best member */
-	
+
 	//grass_cell[5]=population[POPSIZE].fitness;
 	//fprintf(gc,"%5.3f\n",population[POPSIZE].fitness);
 	//fclose(gc);
-	
+
 //}
 
 
@@ -860,58 +951,60 @@
 /*******************************************************/
 /*                                                     */
 /* SWAP forward with best fitness individual values    */
+
 /*******************************************************/
 /*
-void swap(void)
-{
-	int i;
-	FILE *f;
-	
-	if((f=fopen("ET.csv","w"))==NULL)
-	{
-		exit(1);
-	}
-		
-	DECdoy =(int) (population[POPSIZE].gene[0]);
-	STSdoy =(int) (population[POPSIZE].gene[1]);
-	TECdoy =(int) (population[POPSIZE].gene[2]);
-	GWJan =(int) (population[POPSIZE].gene[3]);
-	GWDec =(int) (population[POPSIZE].gene[4]);	
+   void swap(void)
+   {
+   int i;
+   FILE *f;
 
-	doy2date(&DECdoy,&DECy);
-	DECd=day;
-	DECm=month;
-	day=0;
-	month=0;
+   if((f=fopen("ET.csv","w"))==NULL)
+   {
+   exit(1);
+   }
 
-	doy2date(&STSdoy,&STSy);
-	STSd=day;
-	STSm=month;
-	day=0;
-	month=0;	
-	
-	printf("Making the SWAP_ET data\n");
+   DECdoy =(int) (population[POPSIZE].gene[0]);
+   STSdoy =(int) (population[POPSIZE].gene[1]);
+   TECdoy =(int) (population[POPSIZE].gene[2]);
+   GWJan =(int) (population[POPSIZE].gene[3]);
+   GWDec =(int) (population[POPSIZE].gene[4]);  
 
-	
-	finput2();
-	system("./swap");
-	foutput();
+   doy2date(&DECdoy,&DECy);
+   DECd=day;
+   DECm=month;
+   day=0;
+   month=0;
 
-	fprintf(f,"satETa,swapETa\n");	
-	for(i=0;i<NSATEL;i++) {
-		fprintf(f,"%2.3f,%2.3f\n",satETA[i],swapETA[i]);
-	}
-	fclose(f);
-}
+   doy2date(&STSdoy,&STSy);
+   STSd=day;
+   STSm=month;
+   day=0;
+   month=0;     
 
-*/
+   printf("Making the SWAP_ET data\n");
 
+
+   finput2();
+   system("./swap");
+   foutput();
+
+   fprintf(f,"satETa,swapETa\n");       
+   for(i=0;i<NSATEL;i++) {
+   fprintf(f,"%2.3f,%2.3f\n",satETA[i],swapETA[i]);
+   }
+   fclose(f);
+   }
+
+ */
+
 /*******************************************************/
 /*                                                     */
 /* Getting ETa from SWAP INC.file => swap.eta          */
 /*         and merging it with satellite.eta file      */
 /*         Resulting file => eval.eta                  */
-/*	   Format of eval.eta: ETaRS ETaSWAP\n         */
+/*         Format of eval.eta: ETaRS ETaSWAP\n         */
+
 /*******************************************************/
 
 
@@ -919,8 +1012,8 @@
 
 int foutput(void)
 {
-	satellite_dates();
- 	return 1;
+    satellite_dates();
+    return 1;
 }
 
 /********************************************************/
@@ -931,99 +1024,123 @@
 int get_eta(char *dateName, char *dateNameValue, int dateCounter)
 {
     FILE *f;
+
     char s[1000], *ptr;
-    int i=0;
+
+    int i = 0;
+
     char *p;
-    int TRA1=0, TRA2, EVS1=0, EVS2;
-    double TRA=0.0, EVS;
 
-    f=fopen("out.INC","r");
+    int TRA1 = 0, TRA2, EVS1 = 0, EVS2;
 
+    double TRA = 0.0, EVS;
+
+    f = fopen("out.INC", "r");
+
     if (!f)
-        return 1;
+	return 1;
 
-    while (fgets(s,1000,f)!=NULL) {
-	    ptr = strstr(s, dateNameValue);
-	    if (ptr != NULL) {
-		    p = strtok(ptr, " .");
-		    while (i<17) {
-			    p = strtok(NULL, " .");
-			    if ((i==11 || i==12 || i==15 || i==16) && p) {
-				    if (i==11 && p) {
-					    TRA1 = atoi(p);
-				    } else if (i==12 && p) {
-					    TRA2 = atoi(p);
-					    TRA = (double) (TRA1) + (double) (TRA2/1000.0);
-				    } else if (i==15 && p) {
-					    EVS1 = atoi(p);
-				    } else if (i==16 && p) {
-					    EVS2 = atoi(p);
-					    EVS = (double) (EVS1) + (double) (EVS2/1000.0);
-					    swapETA[dateCounter] = (TRA + EVS);
-				    }
-			    }
-			    i=i+1;
+    while (fgets(s, 1000, f) != NULL) {
+	ptr = strstr(s, dateNameValue);
+	if (ptr != NULL) {
+	    p = strtok(ptr, " .");
+	    while (i < 17) {
+		p = strtok(NULL, " .");
+		if ((i == 11 || i == 12 || i == 15 || i == 16) && p) {
+		    if (i == 11 && p) {
+			TRA1 = atoi(p);
 		    }
+		    else if (i == 12 && p) {
+			TRA2 = atoi(p);
+			TRA = (double)(TRA1) + (double)(TRA2 / 1000.0);
+		    }
+		    else if (i == 15 && p) {
+			EVS1 = atoi(p);
+		    }
+		    else if (i == 16 && p) {
+			EVS2 = atoi(p);
+			EVS = (double)(EVS1) + (double)(EVS2 / 1000.0);
+			swapETA[dateCounter] = (TRA + EVS);
+		    }
+		}
+		i = i + 1;
 	    }
+	}
     }
     (void)fclose(f);
-   return 1;
+    return 1;
 }
 
 /*******************************************************/
 /*                                                     */
 /* Getting dates from "satellite.dates" file           */
 /*                                                     */
+
 /*******************************************************/
-/* Thanks Dr Honda!				       */
-/*						       */
+/* Thanks Dr Honda!                                    */
+/*                                                     */
 /* Limited to 99 images so far...                      */
+
 /*******************************************************/
 
 int satellite_dates()
 {
-	struct tm t;
-	#define	BUFLEN	1000	
-	int	iline=0;
-       	int	i;
-	char	dateName[256];
-	char	*dateNameFormat="%s%2.2d";
-	char	*date ="date";
-	char	dateNameValue[256];
-	char	*dateFormat1="%1.1d/%2.2d/%4.4d";
-	char	*dateFormat2="%2.2d/%2.2d/%4.4d";
-	int 	dateCounter=0;
-      char buf[BUFLEN];
+    struct tm t;
 
+#define	BUFLEN	1000
+    int iline = 0;
+
+    int i;
+
+    char dateName[256];
+
+    char *dateNameFormat = "%s%2.2d";
+
+    char *date = "date";
+
+    char dateNameValue[256];
+
+    char *dateFormat1 = "%1.1d/%2.2d/%4.4d";
+
+    char *dateFormat2 = "%2.2d/%2.2d/%4.4d";
+
+    int dateCounter = 0;
+
+    char buf[BUFLEN];
+
     //fp=fopen("satellite.dates","rt");
     //if (!fp)
     //    return 1;
-    
-	//iline = 1;
-	//i = 1;
 
-   // while (fgets( buf, BUFLEN, fp) != NULL) {
-	for(i=1;i<=NSATEL;i++){
-		if(sscanf( satDATE[i], "%d/%d/%d", &t.tm_mday, &t.tm_mon, &t.tm_year) != 3 ){
-			fprintf(stderr, "reading problem; line : %d: %s\n", iline, buf);
-			//fclose(fp);
-			return 1;
-		}
-		t.tm_mon--;
-		t.tm_year -= 1900;
-	    	sprintf( dateName, dateNameFormat, date, i);
-		i++;
-		if (t.tm_mday < 10.0) {
-			sprintf (dateNameValue,dateFormat1, t.tm_mday, t.tm_mon+1, t.tm_year+1900);
-		} else {
-		sprintf(dateNameValue,dateFormat2, t.tm_mday, t.tm_mon+1,t.tm_year+1900);
-		}
-		get_eta(dateName, dateNameValue, dateCounter);	/* send the sat.date to get_eta() */
-		dateCounter++;
-		//iline++;
+    //iline = 1;
+    //i = 1;
+
+    // while (fgets( buf, BUFLEN, fp) != NULL) {
+    for (i = 1; i <= NSATEL; i++) {
+	if (sscanf(satDATE[i], "%d/%d/%d", &t.tm_mday, &t.tm_mon, &t.tm_year)
+	    != 3) {
+	    fprintf(stderr, "reading problem; line : %d: %s\n", iline, buf);
+	    //fclose(fp);
+	    return 1;
+	}
+	t.tm_mon--;
+	t.tm_year -= 1900;
+	sprintf(dateName, dateNameFormat, date, i);
+	i++;
+	if (t.tm_mday < 10.0) {
+	    sprintf(dateNameValue, dateFormat1, t.tm_mday, t.tm_mon + 1,
+		    t.tm_year + 1900);
+	}
+	else {
+	    sprintf(dateNameValue, dateFormat2, t.tm_mday, t.tm_mon + 1,
+		    t.tm_year + 1900);
+	}
+	get_eta(dateName, dateNameValue, dateCounter);	/* send the sat.date to get_eta() */
+	dateCounter++;
+	//iline++;
     }
-//(void)fclose(fp);
-return 1;
+    //(void)fclose(fp);
+    return 1;
 }
 
 
@@ -1035,582 +1152,629 @@
 /*                                                     */
 /*                File Input for SWAP                  */
 /*                                                     */
+
 /*******************************************************/
 /* Date: 20030901                                      */
+
 /*******************************************************/
 
 
 
 /********************************************************/
 /* main program                                         */
+
 /********************************************************/
 
 void finput1(void)
 {
-	swapkey();
-	calfil();
-	capfil();
-	swafil();
-	bbcfil();
-	crpfil();
-	sol1fil();
-	sol2fil();
-	sol3fil();
-	sol4fil();
-	sol5fil();
+    swapkey();
+    calfil();
+    capfil();
+    swafil();
+    bbcfil();
+    crpfil();
+    sol1fil();
+    sol2fil();
+    sol3fil();
+    sol4fil();
+    sol5fil();
 }
 
 void finput2(void)
 {
-	calfil();
-	bbcfil();
-	crpfil();
+    calfil();
+    bbcfil();
+    crpfil();
 }
 
 /********************************************************/
 /* FILE *swapkey; an input to SWAP                      */
+
 /********************************************************/
 
 void swapkey(void)
 {
-	FILE *swapkey; /* SWAP.KEY file-variable */
+    FILE *swapkey;		/* SWAP.KEY file-variable */
 
-	swapkey = fopen("SWAP.KEY", "w");
+    swapkey = fopen("SWAP.KEY", "w");
 
-	fputs(" Project = 'Rood'\n", swapkey);
-	fputs(" Path = ''\n", swapkey);
-	fputs(" CType = 1\n", swapkey);
-	fputs(" SWSCRE = 0\n\n", swapkey);
-	
-	fprintf(swapkey, " SSRUN = 01 01 %d\n", SSRUN);		
-	//fputs(" SSRUN = 01 01 1991\n", swapkey); /*variable only year*/
-	fprintf(swapkey, " ESRUN = 31 12 %d\n", ESRUN);	
-	//fputs(" ESRUN = 31 12 1991\n", swapkey); /*variable only year*/
-	fputs(" FMAY = 1\n", swapkey);
-	fputs(" Period = 1\n", swapkey);
-	fputs(" SWRES = 1\n\n", swapkey);
+    fputs(" Project = 'Rood'\n", swapkey);
+    fputs(" Path = ''\n", swapkey);
+    fputs(" CType = 1\n", swapkey);
+    fputs(" SWSCRE = 0\n\n", swapkey);
 
-	fputs(" SWODAT = 0\n", swapkey);
-	fputs("*\n", swapkey);
-	fputs("* DD MM YYYY\n", swapkey);
-	fputs("* End_of_table\n\n", swapkey);
-	
-	fprintf(swapkey, " METFIL = \'%s\'\n", METFIL);	
-	//fputs(" METFIL = 'ESF'\n", swapkey); /*variable*/
-	fprintf(swapkey, " LAT = %lf\n", LAT);	
-	//fputs(" LAT = 32.4\n", swapkey);   /* variable*/
-	fprintf(swapkey, " ALT = %lf\n", ALT);	
-	//fputs(" ALT = 1600.0\n", swapkey); /*variable*/
-	fputs(" SWETR = 0\n", swapkey);
-	fputs(" SWRAI = 0\n\n", swapkey);
+    fprintf(swapkey, " SSRUN = 01 01 %d\n", SSRUN);
+    //fputs(" SSRUN = 01 01 1991\n", swapkey); /*variable only year*/
+    fprintf(swapkey, " ESRUN = 31 12 %d\n", ESRUN);
+    //fputs(" ESRUN = 31 12 1991\n", swapkey); /*variable only year*/
+    fputs(" FMAY = 1\n", swapkey);
+    fputs(" Period = 1\n", swapkey);
+    fputs(" SWRES = 1\n\n", swapkey);
 
-	fputs("* IRGFIL CALFIL   DRFIL   BBCFIL  OUTFIL\n", swapkey);
-	fputs("  ''     'RoodC'  'none'  'Rood'  'out'\n", swapkey);
-	fputs("* End_of_table\n\n", swapkey);
+    fputs(" SWODAT = 0\n", swapkey);
+    fputs("*\n", swapkey);
+    fputs("* DD MM YYYY\n", swapkey);
+    fputs("* End_of_table\n\n", swapkey);
 
-	fputs(" SWDRA = 0\n\n", swapkey);
+    fprintf(swapkey, " METFIL = \'%s\'\n", METFIL);
+    //fputs(" METFIL = 'ESF'\n", swapkey); /*variable*/
+    fprintf(swapkey, " LAT = %lf\n", LAT);
+    //fputs(" LAT = 32.4\n", swapkey);   /* variable*/
+    fprintf(swapkey, " ALT = %lf\n", ALT);
+    //fputs(" ALT = 1600.0\n", swapkey); /*variable*/
+    fputs(" SWETR = 0\n", swapkey);
+    fputs(" SWRAI = 0\n\n", swapkey);
 
-	fputs(" SWSOLU = 0\n\n", swapkey);
+    fputs("* IRGFIL CALFIL   DRFIL   BBCFIL  OUTFIL\n", swapkey);
+    fputs("  ''     'RoodC'  'none'  'Rood'  'out'\n", swapkey);
+    fputs("* End_of_table\n\n", swapkey);
 
-	fputs(" SWHEA = 0\n\n", swapkey);
+    fputs(" SWDRA = 0\n\n", swapkey);
 
-	fputs(" SWVAP = 0\n\n", swapkey);
+    fputs(" SWSOLU = 0\n\n", swapkey);
 
-	fputs(" SWDRF = 0\n\n", swapkey);
+    fputs(" SWHEA = 0\n\n", swapkey);
 
-	fputs(" SWSWB = 0\n\n", swapkey);
+    fputs(" SWVAP = 0\n\n", swapkey);
 
-	fputs(" SWAFO = 0\n",swapkey);
-	fputs(" AFONAM = 'testA'\n\n", swapkey);
+    fputs(" SWDRF = 0\n\n", swapkey);
 
-	fputs(" SWAUN = 0\n", swapkey);
-	fputs(" AUNNAM = 'testA'\n\n", swapkey);
+    fputs(" SWSWB = 0\n\n", swapkey);
 
-	fputs(" SWATE = 0\n", swapkey);
-	fputs(" ATENAM = 'testA'\n\n", swapkey);
+    fputs(" SWAFO = 0\n", swapkey);
+    fputs(" AFONAM = 'testA'\n\n", swapkey);
 
-	fputs(" SWAIR = 0\n", swapkey);
-	fputs(" AIRNAM = 'testA'\n\n", swapkey);
+    fputs(" SWAUN = 0\n", swapkey);
+    fputs(" AUNNAM = 'testA'\n\n", swapkey);
 
-	(void)fclose(swapkey);
-	return;
+    fputs(" SWATE = 0\n", swapkey);
+    fputs(" ATENAM = 'testA'\n\n", swapkey);
+
+    fputs(" SWAIR = 0\n", swapkey);
+    fputs(" AIRNAM = 'testA'\n\n", swapkey);
+
+    (void)fclose(swapkey);
+    return;
 }
 
 /********************************************************/
 /* CAL-file                                             */
+
 /********************************************************/
 
 void calfil(void)
-
 {
-	FILE *calfil; /* RoodC.CAL file_variable */
+    FILE *calfil;		/* RoodC.CAL file_variable */
 
-	calfil = fopen("RoodC.CAL", "w");
+    calfil = fopen("RoodC.CAL", "w");
 
-	fputs("* CRPFIL Type CAPFIL  EMERGENCE END_crop START_sch\n", calfil);
-	fputs("*                     d1 m1     d2 m2    d3 m3\n", calfil);
-	fprintf(calfil, " 'Crop'  1    'RoodC' %i %i     %i %i    %i %i\n", DECd, DECm, FECd, FECm, STSd, STSm);
-	fputs("* End_of_table\n\n", calfil);
-	
-	(void)fclose(calfil);
-	return ;
+    fputs("* CRPFIL Type CAPFIL  EMERGENCE END_crop START_sch\n", calfil);
+    fputs("*                     d1 m1     d2 m2    d3 m3\n", calfil);
+    fprintf(calfil, " 'Crop'  1    'RoodC' %i %i     %i %i    %i %i\n", DECd,
+	    DECm, FECd, FECm, STSd, STSm);
+    fputs("* End_of_table\n\n", calfil);
+
+    (void)fclose(calfil);
+    return;
 }
 
 /*******************************************************/
 /* CAP-file                                            */
+
 /*******************************************************/
 
 void capfil(void)
 {
-	FILE *capfil; /* RoodC.CAP file_variable */
+    FILE *capfil;		/* RoodC.CAP file_variable */
 
-	capfil = fopen("RoodC.CAP", "w");
+    capfil = fopen("RoodC.CAP", "w");
 
-	fputs(" ISUAS = 1\n\n", capfil);
-	fputs(" CIRRS = 2.56\n\n", capfil);
+    fputs(" ISUAS = 1\n\n", capfil);
+    fputs(" CIRRS = 2.56\n\n", capfil);
 
-	fputs(" TCS1 = 0\n", capfil);
-	fputs("* DVS  Ta/Tp\n", capfil);
-	fputs("0.0 0.75\n", capfil);
-	fputs("2.0 0.75\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs(" TCS1 = 0\n", capfil);
+    fputs("* DVS  Ta/Tp\n", capfil);
+    fputs("0.0 0.75\n", capfil);
+    fputs("2.0 0.75\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs("TCS2 = 0\n", capfil);
-	fputs("* DVS  RAW\n", capfil);
-	fputs("  0.0  0.95\n", capfil);
-	fputs("  2.0  0.95\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs("TCS2 = 0\n", capfil);
+    fputs("* DVS  RAW\n", capfil);
+    fputs("  0.0  0.95\n", capfil);
+    fputs("  2.0  0.95\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs("TCS3 = 0\n", capfil);
-	fputs("* DVS  TAW\n", capfil);
-	fputs("  0.0  0.50\n", capfil);
-	fputs("  2.0  0.50\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs("TCS3 = 0\n", capfil);
+    fputs("* DVS  TAW\n", capfil);
+    fputs("  0.0  0.50\n", capfil);
+    fputs("  2.0  0.50\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs("TCS4 = 0\n", capfil);
-	fputs("* DVS  DWA\n", capfil);
-	fputs("  0.0  40.0\n", capfil);
-	fputs("  2.0  40.0\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs("TCS4 = 0\n", capfil);
+    fputs("* DVS  DWA\n", capfil);
+    fputs("  0.0  40.0\n", capfil);
+    fputs("  2.0  40.0\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs(" TCS5 = 1\n", capfil);
-	fputs(" PHORMC = 0\n", capfil);
-	fputs(" DCRIT = 0.0\n", capfil);
-	fputs("* DVS  VALUE\n", capfil);
-	fputs("  0.0  1.0\n", capfil);
-	fputs("  2.0  1.0\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs(" TCS5 = 1\n", capfil);
+    fputs(" PHORMC = 0\n", capfil);
+    fputs(" DCRIT = 0.0\n", capfil);
+    fputs("* DVS  VALUE\n", capfil);
+    fputs("  0.0  1.0\n", capfil);
+    fputs("  2.0  1.0\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs(" DCS1 = 0\n", capfil);
-	fputs("* DVS  dI\n", capfil);
-	fputs("  0.0  0.0\n", capfil);
-	fputs("  2.0  0.0\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs(" DCS1 = 0\n", capfil);
+    fputs("* DVS  dI\n", capfil);
+    fputs("  0.0  0.0\n", capfil);
+    fputs("  2.0  0.0\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	fputs(" DCS2 = 1\n", capfil);
-	fputs("* DVS  FID\n", capfil);
-	fputs("  0.0  100.0\n", capfil);
-	fputs("  2.0  100.0\n", capfil);
-	fputs("* End of table\n\n", capfil);
+    fputs(" DCS2 = 1\n", capfil);
+    fputs("* DVS  FID\n", capfil);
+    fputs("  0.0  100.0\n", capfil);
+    fputs("  2.0  100.0\n", capfil);
+    fputs("* End of table\n\n", capfil);
 
-	(void)fclose(capfil);
-	return;
+    (void)fclose(capfil);
+    return;
 }
 
 /*******************************************************/
 /* SWA-file                                            */
+
 /*******************************************************/
 
 void swafil(void)
 {
-	FILE *swafil; /* Rood.SWA file_variable */
-	
-	//double  tex[4];
-	
-	/* Get soil texture percentages */
-/*	if ((soil = fopen("soil.txt","r")) == NULL)
-	{
-		printf("Cannot open soil texture data file!\n");
-		exit(1);
-	}
-	
-	for (i=0;i<4;i++)
-	{
-		fscanf(soil, "%lf", &texture);
-		tex[i]=texture;
-	}
-	/
-	fclose(soil);
-*/	/********************************/
-	
-	swafil = fopen("Rood.SWA", "w");
+    FILE *swafil;		/* Rood.SWA file_variable */
 
-	fputs("*******************************************************************************\n", swafil);
-	fputs(" PONDMX = 10.0\n\n", swafil);
-	fputs("*******************************************************************************\n\n", swafil);
+    //double  tex[4];
 
-	fputs("*******************************************************************************\n", swafil);
-	fputs(" SWCFBS = 0\n", swafil);
-	fputs(" CFBS = 0.9\n", swafil);
-	fputs(" SWREDU = 2\n", swafil);
-	fputs(" COFRED = 0.35\n", swafil);
-	fputs(" RSIGNI = 0.5\n\n", swafil);
-	fputs("*******************************************************************************\n\n", swafil);
+    /* Get soil texture percentages */
+    /*      if ((soil = fopen("soil.txt","r")) == NULL)
+       {
+       printf("Cannot open soil texture data file!\n");
+       exit(1);
+       }
 
-	fputs("*******************************************************************************\n", swafil);
-	fputs(" DTMIN = 1.0E-4\n", swafil);
-	fputs(" DTMAX = 0.1\n", swafil);
-	fputs(" SWNUMS = 2\n", swafil);
-	fputs(" THETOL = 0.01\n\n", swafil);
-	fputs("*******************************************************************************\n\n", swafil);
+       for (i=0;i<4;i++)
+       {
+       fscanf(soil, "%lf", &texture);
+       tex[i]=texture;
+       }
+       /
+       fclose(soil);
 
-	fputs("*******************************************************************************\n", swafil);
-	fputs(" NUMLAY = 5\n", swafil);
-	fputs(" NUMNOD = 50\n\n", swafil);
+	 *//********************************/
 
-	fputs(" BOTCOM = 14 19 22 26 50\n", swafil);
-	fputs("*\n", swafil);
-	fputs("* thickness of soil compartments [cm]:\n", swafil);
-	fputs("* total 500 cm\n", swafil);
-	fputs("*\n", swafil);
-	fputs(" DZ =\n", swafil);
-	fputs("     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0\n", swafil);
-	fputs("     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0\n", swafil);
-	fputs("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n", swafil);
-	fputs("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n", swafil);
-	fputs("    20.0    20.0    20.0    20.0    20.0    20.0    20.0    20.0    40.0    40.0\n", swafil);
-	fputs("*******************************************************************************\n\n", swafil);
+    swafil = fopen("Rood.SWA", "w");
 
-	fputs("*******************************************************************************\n", swafil);
-	fputs(" SOLFIL = 'Rood_1' 'Rood_2' 'Rood_3' 'Rood_4' 'Rood_5'\n\n", swafil);
-	fputs("* PSAND  PSILT  PCLAY  ORGMAT\n", swafil);
-	//fputs("  0.21   0.44   0.34   0.0051\n",swafil);
-	//fputs("  0.10   0.26   0.64   0.0039\n",swafil);
-	//fputs("  0.04   0.28   0.68   0.0031\n",swafil);
-	//fputs("  0.02   0.50   0.48   0.0023\n",swafil);
-	//fputs("  0.08   0.60   0.32   0.0020\n",swafil);
-	
-	fprintf(swafil,"  %lf   %lf   %lf   %lf\n",tex[0],tex[1],tex[2],tex[3]);
-	fprintf(swafil,"  %lf   %lf   %lf   %lf\n",tex[0],tex[1],tex[2],tex[3]);
-	fprintf(swafil,"  %lf   %lf   %lf   %lf\n",tex[0],tex[1],tex[2],tex[3]);
-	fprintf(swafil,"  %lf   %lf   %lf   %lf\n",tex[0],tex[1],tex[2],tex[3]);
-	fprintf(swafil,"  %lf   %lf   %lf   %lf\n\n",tex[0],tex[1],tex[2],tex[3]);
-	
-	fputs("*******************************************************************************\n\n", swafil);
+    fputs
+	("*******************************************************************************\n",
+	 swafil);
+    fputs(" PONDMX = 10.0\n\n", swafil);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil);
 
-	fputs(" RDS = 300.0\n\n", swafil);
+    fputs
+	("*******************************************************************************\n",
+	 swafil);
+    fputs(" SWCFBS = 0\n", swafil);
+    fputs(" CFBS = 0.9\n", swafil);
+    fputs(" SWREDU = 2\n", swafil);
+    fputs(" COFRED = 0.35\n", swafil);
+    fputs(" RSIGNI = 0.5\n\n", swafil);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil);
 
-	fputs(" SWHYST = 0\n", swafil);
-	fputs(" TAU = 0.2\n\n", swafil);
+    fputs
+	("*******************************************************************************\n",
+	 swafil);
+    fputs(" DTMIN = 1.0E-4\n", swafil);
+    fputs(" DTMAX = 0.1\n", swafil);
+    fputs(" SWNUMS = 2\n", swafil);
+    fputs(" THETOL = 0.01\n\n", swafil);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil);
 
-	fputs(" SWSCAL = 0\n", swafil);
-	fputs(" NSCALE = 0\n", swafil);
-	fputs(" ISCLAY = 0\n", swafil);
-/*	fputs(" FSCALE =\n", swafil);
-	fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n", swafil);
-	fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n", swafil);
-	fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n\n", swafil); */
+    fputs
+	("*******************************************************************************\n",
+	 swafil);
+    fputs(" NUMLAY = 5\n", swafil);
+    fputs(" NUMNOD = 50\n\n", swafil);
 
-	fputs(" SWMOBI = 0\n", swafil);
-	fputs("* PF1  FM1  PF2  FM2  THETIM\n", swafil);
-	fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil);
-	fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil);
-	fputs("* End_of_table\n\n", swafil);
+    fputs(" BOTCOM = 14 19 22 26 50\n", swafil);
+    fputs("*\n", swafil);
+    fputs("* thickness of soil compartments [cm]:\n", swafil);
+    fputs("* total 500 cm\n", swafil);
+    fputs("*\n", swafil);
+    fputs(" DZ =\n", swafil);
+    fputs
+	("     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0\n",
+	 swafil);
+    fputs
+	("     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0\n",
+	 swafil);
+    fputs
+	("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n",
+	 swafil);
+    fputs
+	("    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0    10.0\n",
+	 swafil);
+    fputs
+	("    20.0    20.0    20.0    20.0    20.0    20.0    20.0    20.0    40.0    40.0\n",
+	 swafil);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil);
 
-	fputs(" SWCRACK = 0\n", swafil);
-	fputs(" SHRINA = 0.53\n", swafil);
-	fputs(" MOISR1 = 1.0\n", swafil);
-	fputs(" MOISRD = 0.05\n", swafil);
-	fputs(" ZNCRACK = -5.0\n", swafil);
-	fputs(" GEOMF = 3.0\n", swafil);
-	fputs(" DIAMPOL = 40.0\n", swafil);
-	fputs(" RAPCOEF = 0.0\n", swafil);
-	fputs(" DIFDES = 0.2\n", swafil);
-	fputs(" THETCR = 0.50  0.50  0.50  0.50  0.50\n\n", swafil);
+    fputs
+	("*******************************************************************************\n",
+	 swafil);
+    fputs(" SOLFIL = 'Rood_1' 'Rood_2' 'Rood_3' 'Rood_4' 'Rood_5'\n\n",
+	  swafil);
+    fputs("* PSAND  PSILT  PCLAY  ORGMAT\n", swafil);
+    //fputs("  0.21   0.44   0.34   0.0051\n",swafil);
+    //fputs("  0.10   0.26   0.64   0.0039\n",swafil);
+    //fputs("  0.04   0.28   0.68   0.0031\n",swafil);
+    //fputs("  0.02   0.50   0.48   0.0023\n",swafil);
+    //fputs("  0.08   0.60   0.32   0.0020\n",swafil);
 
-	fputs(" SWDIVD = 0\n", swafil);
-	fputs(" COFANI =    1.0    1.0    1.0    1.0    1.0\n\n", swafil);
+    fprintf(swafil, "  %lf   %lf   %lf   %lf\n", tex[0], tex[1], tex[2],
+	    tex[3]);
+    fprintf(swafil, "  %lf   %lf   %lf   %lf\n", tex[0], tex[1], tex[2],
+	    tex[3]);
+    fprintf(swafil, "  %lf   %lf   %lf   %lf\n", tex[0], tex[1], tex[2],
+	    tex[3]);
+    fprintf(swafil, "  %lf   %lf   %lf   %lf\n", tex[0], tex[1], tex[2],
+	    tex[3]);
+    fprintf(swafil, "  %lf   %lf   %lf   %lf\n\n", tex[0], tex[1], tex[2],
+	    tex[3]);
 
-	fputs(" SWINCO = 2\n", swafil);
-/*	fputs(" HI =\n", swafil);
-	fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
-	fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
-	fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
-	fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil); */
-	fputs(" GWLI = -200.0\n", swafil);
-	fputs(" OSGWLM = 30.0\n\n", swafil);
+    fputs
+	("*******************************************************************************\n\n",
+	 swafil);
 
-	(void)fclose(swafil);
-	return;
+    fputs(" RDS = 300.0\n\n", swafil);
+
+    fputs(" SWHYST = 0\n", swafil);
+    fputs(" TAU = 0.2\n\n", swafil);
+
+    fputs(" SWSCAL = 0\n", swafil);
+    fputs(" NSCALE = 0\n", swafil);
+    fputs(" ISCLAY = 0\n", swafil);
+    /*      fputs(" FSCALE =\n", swafil);
+       fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n", swafil);
+       fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n", swafil);
+       fputs("   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00\n\n", swafil); */
+
+    fputs(" SWMOBI = 0\n", swafil);
+    fputs("* PF1  FM1  PF2  FM2  THETIM\n", swafil);
+    fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil);
+    fputs("  0.0  0.0  0.0  0.0  0.0\n", swafil);
+    fputs("* End_of_table\n\n", swafil);
+
+    fputs(" SWCRACK = 0\n", swafil);
+    fputs(" SHRINA = 0.53\n", swafil);
+    fputs(" MOISR1 = 1.0\n", swafil);
+    fputs(" MOISRD = 0.05\n", swafil);
+    fputs(" ZNCRACK = -5.0\n", swafil);
+    fputs(" GEOMF = 3.0\n", swafil);
+    fputs(" DIAMPOL = 40.0\n", swafil);
+    fputs(" RAPCOEF = 0.0\n", swafil);
+    fputs(" DIFDES = 0.2\n", swafil);
+    fputs(" THETCR = 0.50  0.50  0.50  0.50  0.50\n\n", swafil);
+
+    fputs(" SWDIVD = 0\n", swafil);
+    fputs(" COFANI =    1.0    1.0    1.0    1.0    1.0\n\n", swafil);
+
+    fputs(" SWINCO = 2\n", swafil);
+    /*      fputs(" HI =\n", swafil);
+       fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
+       fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
+       fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil);
+       fputs("    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0\n", swafil); */
+    fputs(" GWLI = -200.0\n", swafil);
+    fputs(" OSGWLM = 30.0\n\n", swafil);
+
+    (void)fclose(swafil);
+    return;
 }
 
 /*******************************************************/
 /* BBC-file                                            */
+
 /*******************************************************/
 
 void bbcfil(void)
 {
-	FILE *bbcfil; /* Rood.BBC file_variable */
+    FILE *bbcfil;		/* Rood.BBC file_variable */
 
-	bbcfil = fopen("Rood.BBC", "w");
+    bbcfil = fopen("Rood.BBC", "w");
 
-	fputs(" SWOPT1 = 1\n", bbcfil);
-	fputs("* d1 m1 GWlevel\n", bbcfil);
-	fprintf(bbcfil,"  1  1 -%i.0\n",GWJan);
-	fprintf(bbcfil," 31 12 -%i.0\n",GWDec);
-/*	fputs("   1  1 -150.0\n", bbcfil);
-	fputs("  31 12 -150.0\n", bbcfil);
-*/	fputs("* End of table\n\n", bbcfil);
+    fputs(" SWOPT1 = 1\n", bbcfil);
+    fputs("* d1 m1 GWlevel\n", bbcfil);
+    fprintf(bbcfil, "  1  1 -%i.0\n", GWJan);
+    fprintf(bbcfil, " 31 12 -%i.0\n", GWDec);
+    /*      fputs("   1  1 -150.0\n", bbcfil);
+       fputs("  31 12 -150.0\n", bbcfil);
+     */ fputs("* End of table\n\n", bbcfil);
 
-	fputs(" SWOPT2 = 0\n", bbcfil);
-	fputs(" SWC2 = 2\n", bbcfil);
-	fputs(" C2AVE = 0.1\n", bbcfil);
-	fputs(" C2AMP = 0.05\n", bbcfil);
-	fputs(" C2STA = 91\n", bbcfil);
-	fputs("* d2 m2 Qbot\n", bbcfil);
-	fputs("   1  1 0.10\n", bbcfil);
-	fputs("   5  6 0.20\n", bbcfil);
-	fputs("  31 12 0.10\n", bbcfil);
-	fputs("* End of table\n\n", bbcfil);
+    fputs(" SWOPT2 = 0\n", bbcfil);
+    fputs(" SWC2 = 2\n", bbcfil);
+    fputs(" C2AVE = 0.1\n", bbcfil);
+    fputs(" C2AMP = 0.05\n", bbcfil);
+    fputs(" C2STA = 91\n", bbcfil);
+    fputs("* d2 m2 Qbot\n", bbcfil);
+    fputs("   1  1 0.10\n", bbcfil);
+    fputs("   5  6 0.20\n", bbcfil);
+    fputs("  31 12 0.10\n", bbcfil);
+    fputs("* End of table\n\n", bbcfil);
 
-	fputs(" SWOPT3 = 0\n", bbcfil);
-	fputs(" SHAPE = 0.79\n", bbcfil);
-	fputs(" RIMLAY = 500\n", bbcfil);
-	fputs(" AQAVE = 50.0\n", bbcfil);
-	fputs(" AQAMP = 20.0\n", bbcfil);
-	fputs(" AQTAMX = 120\n", bbcfil);
-	fputs(" AQPER = 365\n\n", bbcfil);
+    fputs(" SWOPT3 = 0\n", bbcfil);
+    fputs(" SHAPE = 0.79\n", bbcfil);
+    fputs(" RIMLAY = 500\n", bbcfil);
+    fputs(" AQAVE = 50.0\n", bbcfil);
+    fputs(" AQAMP = 20.0\n", bbcfil);
+    fputs(" AQTAMX = 120\n", bbcfil);
+    fputs(" AQPER = 365\n\n", bbcfil);
 
-	fputs(" SWOPT4 = 0\n", bbcfil);
-	fputs(" COFQHA = -0.3\n", bbcfil);
-	fputs(" COFQHB = -0.01\n\n", bbcfil);
+    fputs(" SWOPT4 = 0\n", bbcfil);
+    fputs(" COFQHA = -0.3\n", bbcfil);
+    fputs(" COFQHB = -0.01\n\n", bbcfil);
 
-	fputs(" SWOPT5 = 0\n", bbcfil);
-	fputs("* d5 m5 GWlevel\n", bbcfil);
-	fputs("   1  1  50.0\n", bbcfil);
-	fputs("  31 12  20.0\n", bbcfil);
-	fputs("* End of table\n\n", bbcfil);
+    fputs(" SWOPT5 = 0\n", bbcfil);
+    fputs("* d5 m5 GWlevel\n", bbcfil);
+    fputs("   1  1  50.0\n", bbcfil);
+    fputs("  31 12  20.0\n", bbcfil);
+    fputs("* End of table\n\n", bbcfil);
 
-	fputs(" SWOPT6 = 0\n", bbcfil);
-	fputs(" SWOPT7 = 0\n", bbcfil);
-	fputs(" SWOPT8 = 0\n", bbcfil);
+    fputs(" SWOPT6 = 0\n", bbcfil);
+    fputs(" SWOPT7 = 0\n", bbcfil);
+    fputs(" SWOPT8 = 0\n", bbcfil);
 
 
-	(void)fclose(bbcfil);
-	return;
+    (void)fclose(bbcfil);
+    return;
 }
 
 /********************************************************/
 /* CRP-file                                             */
+
 /********************************************************/
 
 void crpfil(void)
 {
-	FILE *crpfil; /* Crop.CRP file_variable */
+    FILE *crpfil;		/* Crop.CRP file_variable */
 
-	crpfil = fopen("Crop.CRP", "w");
+    crpfil = fopen("Crop.CRP", "w");
 
-	fputs(" IDEV = 1\n", crpfil);
-//	fputs(" LCC = 180\n", crpfil);
-	fprintf(crpfil,"LCC = %i\n",TECdoy);
-	fputs(" TSUMEA = 1050.0\n", crpfil);
-	fputs(" TSUMAM = 1000.0\n", crpfil);
-	fputs(" TBASE = 0.0\n\n", crpfil);
+    fputs(" IDEV = 1\n", crpfil);
+    //      fputs(" LCC = 180\n", crpfil);
+    fprintf(crpfil, "LCC = %i\n", TECdoy);
+    fputs(" TSUMEA = 1050.0\n", crpfil);
+    fputs(" TSUMAM = 1000.0\n", crpfil);
+    fputs(" TBASE = 0.0\n\n", crpfil);
 
-	fputs(" KDIF = 0.6\n", crpfil);
-	fputs(" KDIR = 0.75\n\n", crpfil);
+    fputs(" KDIF = 0.6\n", crpfil);
+    fputs(" KDIR = 0.75\n\n", crpfil);
 
-	fputs(" SWGC = 1\n", crpfil);
-	fputs("*    DVS   LAI or SCF\n", crpfil);
-	fputs(" GCTB =\n", crpfil);
-	fputs("     0.0    0.00\n", crpfil);
-	fputs("     0.2    0.07\n", crpfil);
-	fputs("     0.4    0.16\n", crpfil);
-	fputs("     0.6    0.61\n", crpfil);
-	fputs("     0.8    1.25\n", crpfil);
-	fputs("     1.0    1.73\n", crpfil);
-	fputs("     1.2    1.75\n", crpfil);
-	fputs("     1.4    1.73\n", crpfil);
-	fputs("     1.6    1.71\n", crpfil);
-	fputs("     1.8    1.04\n", crpfil);
-	fputs("     2.0    0.58\n", crpfil);
-	fputs("**************\n\n", crpfil);
+    fputs(" SWGC = 1\n", crpfil);
+    fputs("*    DVS   LAI or SCF\n", crpfil);
+    fputs(" GCTB =\n", crpfil);
+    fputs("     0.0    0.00\n", crpfil);
+    fputs("     0.2    0.07\n", crpfil);
+    fputs("     0.4    0.16\n", crpfil);
+    fputs("     0.6    0.61\n", crpfil);
+    fputs("     0.8    1.25\n", crpfil);
+    fputs("     1.0    1.73\n", crpfil);
+    fputs("     1.2    1.75\n", crpfil);
+    fputs("     1.4    1.73\n", crpfil);
+    fputs("     1.6    1.71\n", crpfil);
+    fputs("     1.8    1.04\n", crpfil);
+    fputs("     2.0    0.58\n", crpfil);
+    fputs("**************\n\n", crpfil);
 
-	fputs(" SWCF = 2\n", crpfil);
-	fputs("*        DVS   CF or CH\n", crpfil);
-	fputs(" CFTB = 0.00   0.0\n", crpfil);
-	fputs("        1.00  70.0\n", crpfil);
-	fputs("        2.00  70.0\n\n", crpfil);
+    fputs(" SWCF = 2\n", crpfil);
+    fputs("*        DVS   CF or CH\n", crpfil);
+    fputs(" CFTB = 0.00   0.0\n", crpfil);
+    fputs("        1.00  70.0\n", crpfil);
+    fputs("        2.00  70.0\n\n", crpfil);
 
-	fputs("*         DVS   RD\n", crpfil);
-	fputs(" RDTB = 0.00   0.0\n", crpfil);
-	fputs("        1.00 100.0\n", crpfil);
-	fputs("        2.00 100.0\n\n", crpfil);
+    fputs("*         DVS   RD\n", crpfil);
+    fputs(" RDTB = 0.00   0.0\n", crpfil);
+    fputs("        1.00 100.0\n", crpfil);
+    fputs("        2.00 100.0\n\n", crpfil);
 
-	fputs("*        DVS   KY\n", crpfil);
-	fputs(" KYTB = 0.0   0.2\n", crpfil);
-	fputs("        0.4   0.2\n", crpfil);
-	fputs("        0.9   0.5\n", crpfil);
-	fputs("        1.5   0.5\n", crpfil);
-	fputs("        2.0   0.3\n\n", crpfil);
-	fputs("**************\n\n", crpfil);
+    fputs("*        DVS   KY\n", crpfil);
+    fputs(" KYTB = 0.0   0.2\n", crpfil);
+    fputs("        0.4   0.2\n", crpfil);
+    fputs("        0.9   0.5\n", crpfil);
+    fputs("        1.5   0.5\n", crpfil);
+    fputs("        2.0   0.3\n\n", crpfil);
+    fputs("**************\n\n", crpfil);
 
-	fputs(" HLIM1 = -0.1\n", crpfil);
-	fputs(" HLIM2U = -30.0\n", crpfil);
-	fputs(" HLIM2L = -30.0\n", crpfil);
-	fputs(" HLIM3H = -100.0\n", crpfil);
-	fputs(" HLIM3L = -200.0\n", crpfil);
-	fputs(" HLIM4 = -16000.0\n", crpfil);
-	fputs(" RSC = 70.0\n", crpfil);
-	fputs(" ADCRH = 0.5\n", crpfil);
-	fputs(" ADCRL = 0.1\n\n", crpfil);
+    fputs(" HLIM1 = -0.1\n", crpfil);
+    fputs(" HLIM2U = -30.0\n", crpfil);
+    fputs(" HLIM2L = -30.0\n", crpfil);
+    fputs(" HLIM3H = -100.0\n", crpfil);
+    fputs(" HLIM3L = -200.0\n", crpfil);
+    fputs(" HLIM4 = -16000.0\n", crpfil);
+    fputs(" RSC = 70.0\n", crpfil);
+    fputs(" ADCRH = 0.5\n", crpfil);
+    fputs(" ADCRL = 0.1\n\n", crpfil);
 
-	fputs(" ECMAX = 7.7\n", crpfil);
-	fputs(" ECSLOP = 5.0\n\n", crpfil);
+    fputs(" ECMAX = 7.7\n", crpfil);
+    fputs(" ECSLOP = 5.0\n\n", crpfil);
 
-	fputs(" COFAB = 0.25\n\n", crpfil);
+    fputs(" COFAB = 0.25\n\n", crpfil);
 
-	fputs("*        Rdepth Rdensity\n", crpfil);
-	fputs(" RDCTB = 0.0    1.0\n", crpfil);
-	fputs("         1.0    0.0\n", crpfil);
-	fputs("**************\n", crpfil);
+    fputs("*        Rdepth Rdensity\n", crpfil);
+    fputs(" RDCTB = 0.0    1.0\n", crpfil);
+    fputs("         1.0    0.0\n", crpfil);
+    fputs("**************\n", crpfil);
 
-	(void)fclose(crpfil);
-	return;
+    (void)fclose(crpfil);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 1                                   */
+
 /*******************************************************/
 
 void sol1fil(void)
 {
 
-	FILE *sol1fil; /* Rood_1.SOL file_variable */
+    FILE *sol1fil;		/* Rood_1.SOL file_variable */
 
-	sol1fil = fopen("Rood_1.SOL", "w");
+    sol1fil = fopen("Rood_1.SOL", "w");
 
-	fputs(" SWPHYS = 1\n\n", sol1fil);
+    fputs(" SWPHYS = 1\n\n", sol1fil);
 
-	fputs(" COFGEN1 = 0.000\n", sol1fil);
-	fputs(" COFGEN2 = 0.492\n", sol1fil);
-	fputs(" COFGEN3 = 0.500\n", sol1fil);
-	fputs(" COFGEN4 = 0.02638\n", sol1fil);
-	fputs(" COFGEN5 = -2.196\n", sol1fil);
-	fputs(" COFGEN6 = 1.1775\n", sol1fil);
-	fputs(" COFGEN8 = 0.029\n", sol1fil);
-	fputs("* End of file\n", sol1fil);
+    fputs(" COFGEN1 = 0.000\n", sol1fil);
+    fputs(" COFGEN2 = 0.492\n", sol1fil);
+    fputs(" COFGEN3 = 0.500\n", sol1fil);
+    fputs(" COFGEN4 = 0.02638\n", sol1fil);
+    fputs(" COFGEN5 = -2.196\n", sol1fil);
+    fputs(" COFGEN6 = 1.1775\n", sol1fil);
+    fputs(" COFGEN8 = 0.029\n", sol1fil);
+    fputs("* End of file\n", sol1fil);
 
-	(void)fclose(sol1fil);
-	return;
+    (void)fclose(sol1fil);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 2                                   */
+
 /*******************************************************/
 
 void sol2fil(void)
 {
 
-	FILE *sol2fil; /* Rood_2.SOL file_variable */
+    FILE *sol2fil;		/* Rood_2.SOL file_variable */
 
-	sol2fil = fopen("Rood_2.SOL", "w");
+    sol2fil = fopen("Rood_2.SOL", "w");
 
-	fputs(" SWPHYS = 1\n\n", sol2fil);
+    fputs(" SWPHYS = 1\n\n", sol2fil);
 
-	fputs(" COFGEN1 = 0.000\n", sol2fil);
-	fputs(" COFGEN2 = 0.516\n", sol2fil);
-	fputs(" COFGEN3 = 30.206\n", sol2fil);
-	fputs(" COFGEN4 = 0.01216\n", sol2fil);
-	fputs(" COFGEN5 = 0.046\n", sol2fil);
-	fputs(" COFGEN6 = 1.1471\n", sol2fil);
-	fputs(" COFGEN8 = 0.013\n", sol2fil);
-	fputs("* End of file\n", sol2fil);
+    fputs(" COFGEN1 = 0.000\n", sol2fil);
+    fputs(" COFGEN2 = 0.516\n", sol2fil);
+    fputs(" COFGEN3 = 30.206\n", sol2fil);
+    fputs(" COFGEN4 = 0.01216\n", sol2fil);
+    fputs(" COFGEN5 = 0.046\n", sol2fil);
+    fputs(" COFGEN6 = 1.1471\n", sol2fil);
+    fputs(" COFGEN8 = 0.013\n", sol2fil);
+    fputs("* End of file\n", sol2fil);
 
-	(void)fclose(sol2fil);
-	return;
+    (void)fclose(sol2fil);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 3                                   */
+
 /******************************10194*************************/
 
 void sol3fil(void)
 {
 
-	FILE *sol3fil; /* Rood_3.SOL file_variable */
+    FILE *sol3fil;		/* Rood_3.SOL file_variable */
 
-	sol3fil = fopen("Rood_3.SOL", "w");
+    sol3fil = fopen("Rood_3.SOL", "w");
 
-	fputs(" SWPHYS = 1\n\n", sol3fil);
+    fputs(" SWPHYS = 1\n\n", sol3fil);
 
-	fputs(" COFGEN1 = 0.000\n", sol3fil);
-	fputs(" COFGEN2 = 0.501\n", sol3fil);
-	fputs(" COFGEN3 = 2.295\n", sol3fil);
-	fputs(" COFGEN4 = 0.00778\n", sol3fil);
-	fputs(" COFGEN5 = 0.251\n", sol3fil);
-	fputs(" COFGEN6 = 1.0829\n", sol3fil);
-	fputs(" COFGEN8 = 0.009\n", sol3fil);
-	fputs("* End of file\n", sol3fil);
+    fputs(" COFGEN1 = 0.000\n", sol3fil);
+    fputs(" COFGEN2 = 0.501\n", sol3fil);
+    fputs(" COFGEN3 = 2.295\n", sol3fil);
+    fputs(" COFGEN4 = 0.00778\n", sol3fil);
+    fputs(" COFGEN5 = 0.251\n", sol3fil);
+    fputs(" COFGEN6 = 1.0829\n", sol3fil);
+    fputs(" COFGEN8 = 0.009\n", sol3fil);
+    fputs("* End of file\n", sol3fil);
 
-	(void)fclose(sol3fil);
-	return;
+    (void)fclose(sol3fil);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 4                                   */
+
 /*******************************************************/
 
 void sol4fil(void)
 {
 
-	FILE *sol4fil; /* Rood_4.SOL file_variable */
+    FILE *sol4fil;		/* Rood_4.SOL file_variable */
 
-	sol4fil = fopen("Rood_4.SOL", "w");
+    sol4fil = fopen("Rood_4.SOL", "w");
 
-	fputs(" SWPHYS = 1\n\n", sol4fil);
+    fputs(" SWPHYS = 1\n\n", sol4fil);
 
-	fputs(" COFGEN1 = 0.000\n", sol4fil);
-	fputs(" COFGEN2 = 0.431\n", sol4fil);
-	fputs(" COFGEN3 = 4.331\n", sol4fil);
-	fputs(" COFGEN4 = 0.01379\n", sol4fil);
-	fputs(" COFGEN5 = -2.743\n", sol4fil);
-	fputs(" COFGEN6 = 1.0818\n", sol4fil);
-	fputs(" COFGEN8 = 0.015\n", sol4fil);
-	fputs("* End of file\n", sol4fil);
+    fputs(" COFGEN1 = 0.000\n", sol4fil);
+    fputs(" COFGEN2 = 0.431\n", sol4fil);
+    fputs(" COFGEN3 = 4.331\n", sol4fil);
+    fputs(" COFGEN4 = 0.01379\n", sol4fil);
+    fputs(" COFGEN5 = -2.743\n", sol4fil);
+    fputs(" COFGEN6 = 1.0818\n", sol4fil);
+    fputs(" COFGEN8 = 0.015\n", sol4fil);
+    fputs("* End of file\n", sol4fil);
 
-	(void)fclose(sol4fil);
-	return;
+    (void)fclose(sol4fil);
+    return;
 }
 
 /*******************************************************/
 /* SOL-file number 5                                   */
+
 /*******************************************************/
 
 void sol5fil(void)
 {
 
-	FILE *sol5fil; /* Rood_5.SOL file_variable */
+    FILE *sol5fil;		/* Rood_5.SOL file_variable */
 
-	sol5fil = fopen("Rood_5.SOL", "w");
+    sol5fil = fopen("Rood_5.SOL", "w");
 
-	fputs(" SWPHYS = 1\n\n", sol5fil);
+    fputs(" SWPHYS = 1\n\n", sol5fil);
 
-	fputs(" COFGEN1 = 0.000\n", sol5fil);
-	fputs(" COFGEN2 = 0.424\n", sol5fil);
-	fputs(" COFGEN3 = 9.843\n", sol5fil);
-	fputs(" COFGEN4 = 0.01667\n", sol5fil);
-	fputs(" COFGEN5 = -1.982\n", sol5fil);
-	fputs(" COFGEN6 = 1.1307\n", sol5fil);
-	fputs(" COFGEN8 = 0.018\n", sol5fil);
-	fputs("* End of file\n", sol5fil);
+    fputs(" COFGEN1 = 0.000\n", sol5fil);
+    fputs(" COFGEN2 = 0.424\n", sol5fil);
+    fputs(" COFGEN3 = 9.843\n", sol5fil);
+    fputs(" COFGEN4 = 0.01667\n", sol5fil);
+    fputs(" COFGEN5 = -1.982\n", sol5fil);
+    fputs(" COFGEN6 = 1.1307\n", sol5fil);
+    fputs(" COFGEN8 = 0.018\n", sol5fil);
+    fputs("* End of file\n", sol5fil);
 
-	(void)fclose(sol5fil);
-	return;
+    (void)fclose(sol5fil);
+    return;
 }
 
 /********************************************************/
-
-
-
-

Modified: grass-addons/gipe/r.gaswap.serial/gaswap.h
===================================================================
--- grass-addons/gipe/r.gaswap.serial/gaswap.h	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.gaswap.serial/gaswap.h	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,15 @@
 
-void gaswap(double texture[],double sat[],double satDOY[],int NVAR,int NSAT,int year, int pop,int maxgen,double pmute, double pxover,double target_fitness,int syear,int eyear,char *METRO, double Lati,double Alt, double *grass_c);
-// void gaswap(double texture[],double sat[],double satDOY[],int NVAR,int NSAT,int year, int pop,int maxgen,double pmute, double pxover,double target_fitness,int syear,int eyear,char *METRO, double Lati,double Alt);
+void gaswap(double texture[], double sat[], double satDOY[], int NVAR,
+	    int NSAT, int year, int pop, int maxgen, double pmute,
+	    double pxover, double target_fitness, int syear, int eyear,
+	    char *METRO, double Lati, double Alt, double *grass_c);
 
-struct genotype /* genotype (GT), a member of the population */
+struct genotype			/* genotype (GT), a member of the population */
 {
-	        int *gene;
-	        double fitness;         /* GT's fitness */
-	        double *upper;  /* GT's variables upper bound */
-	        double *lower;  /* GT's variables lower bound */
-	        double rfitness;        /* relative fitness */
-	        double cfitness;        /* cumulative fitness */
+    int *gene;
+    double fitness;		/* GT's fitness */
+    double *upper;		/* GT's variables upper bound */
+    double *lower;		/* GT's variables lower bound */
+    double rfitness;		/* relative fitness */
+    double cfitness;		/* cumulative fitness */
 };
-
-

Modified: grass-addons/gipe/r.gaswap.serial/main.c
===================================================================
--- grass-addons/gipe/r.gaswap.serial/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.gaswap.serial/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       r.gaswap
@@ -21,551 +22,600 @@
 #include <grass/glocale.h>
 #include "gaswap.h"
 
-#define MAXFILES 180 /* input ETa max layers */
-#define NVAR 6 /* output file number */
+#define MAXFILES 180		/* input ETa max layers */
+#define NVAR 6			/* output file number */
 
 extern DCELL f_f(DCELL);
+
 /* satellite ETa is in mm/day, swapETa in cm/day */
 /* This converts sat ETa to cm/day */
-DCELL f_calc1(DCELL x){
-	return (x*0.1);
+DCELL f_calc1(DCELL x)
+{
+    return (x * 0.1);
 }
 
 int main(int argc, char *argv[])
 {
-	struct GModule *module;
-	struct{
-	struct Option *input1, *input2, *input3, *input4, *input5, *input6,*input7,*input8,*input9,*input10,*input11,*input12,*input13,*input14,*input15,*input16,*input17,*output1, *output2, *output3, *output4, *output5, *output6;
-	} param;
-	struct Cell_head cellhd;
-	char *name, *mapset;
-	int nrows, ncols;
-	int row,col;
-	name="";
+    struct GModule *module;
+
+    struct
+    {
+	struct Option *input1, *input2, *input3, *input4, *input5, *input6,
+	    *input7, *input8, *input9, *input10, *input11, *input12, *input13,
+	    *input14, *input15, *input16, *input17, *output1, *output2,
+	    *output3, *output4, *output5, *output6;
+    } param;
+
+    struct Cell_head cellhd;
+
+    char *name, *mapset;
+
+    int nrows, ncols;
+
+    int row, col;
+
+    name = "";
+
 	/************************************/
-	/* FMEO Declarations*****************/
-	int nfiles=MAXFILES;
-	int infd[nfiles],infd_doy[nfiles], infd_psand, infd_psilt, infd_pclay, infd_pomat;
-	int outfd[NVAR+1];
-	int SSRUN_Year,ESRUN_Year,year;
-	char *METFILE;
-	double Lat,Alt;
-	char **names;
-	char **ptr;
-	char **doy_names;
-	char **doy_ptr;
-	char *psand,*psilt,*pclay,*pomat;
-	char *result[NVAR+1];
-	double pmute,pxover,target_fitness;
-	/*char *pmute, *pxover, *target_fitness;*/
-	int pop, maxgen;
-	
-	int ok;
-	int i=0,j=0;
-	
-	void *inrast[nfiles], *inrast_doy[nfiles];
-	void *inrast_psand, *inrast_psilt, *inrast_pclay, *inrast_pomat;
-	FCELL *outrast[NVAR+2];
-	RASTER_MAP_TYPE data_type[nfiles];
-	RASTER_MAP_TYPE data_type_doy[nfiles];
-	RASTER_MAP_TYPE data_type_psand;
-	RASTER_MAP_TYPE data_type_psilt;
-	RASTER_MAP_TYPE data_type_pclay;
-	RASTER_MAP_TYPE data_type_pomat;
-	
-	double sat[nfiles];
-	double sat_doy[nfiles];
-	double grass_cell[NVAR+2];
+    /* FMEO Declarations**************** */
+    int nfiles = MAXFILES;
+
+    int infd[nfiles], infd_doy[nfiles], infd_psand, infd_psilt, infd_pclay,
+	infd_pomat;
+    int outfd[NVAR + 1];
+
+    int SSRUN_Year, ESRUN_Year, year;
+
+    char *METFILE;
+
+    double Lat, Alt;
+
+    char **names;
+
+    char **ptr;
+
+    char **doy_names;
+
+    char **doy_ptr;
+
+    char *psand, *psilt, *pclay, *pomat;
+
+    char *result[NVAR + 1];
+
+    double pmute, pxover, target_fitness;
+
+    /*char *pmute, *pxover, *target_fitness; */
+    int pop, maxgen;
+
+    int ok;
+
+    int i = 0, j = 0;
+
+    void *inrast[nfiles], *inrast_doy[nfiles];
+
+    void *inrast_psand, *inrast_psilt, *inrast_pclay, *inrast_pomat;
+
+    FCELL *outrast[NVAR + 2];
+
+    RASTER_MAP_TYPE data_type[nfiles];
+
+    RASTER_MAP_TYPE data_type_doy[nfiles];
+
+    RASTER_MAP_TYPE data_type_psand;
+
+    RASTER_MAP_TYPE data_type_psilt;
+
+    RASTER_MAP_TYPE data_type_pclay;
+
+    RASTER_MAP_TYPE data_type_pomat;
+
+    double sat[nfiles];
+
+    double sat_doy[nfiles];
+
+    double grass_cell[NVAR + 2];
+
 	/************************************/
 
-	G_gisinit(argv[0]);
+    G_gisinit(argv[0]);
 
-	module = G_define_module();
-	module->description =
-		"Genetic Algorithms and Soil-Water-Air-Plant [GA+SWAP]";
+    module = G_define_module();
+    module->description =
+	"Genetic Algorithms and Soil-Water-Air-Plant [GA+SWAP]";
 
-	/* Define the different options */
+    /* Define the different options */
 
-	param.input1 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	param.input1->key        = "input_eta";
-	param.input1->description= "Names of satellite ETa layers" ;
-	param.input1->guisection = "IN";
-	
-	param.input2 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	param.input2->key        = "input_doy";
-	param.input2->description= "Names of satellite Doy layers" ;
-	param.input2->guisection = "IN";
+    param.input1 = G_define_standard_option(G_OPT_R_INPUTS);
+    param.input1->key = "input_eta";
+    param.input1->description = "Names of satellite ETa layers";
+    param.input1->guisection = "IN";
 
-	param.input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	param.input3->key	 = "psand";
-	param.input3->description= "Name of the Soil sand fraction map [0.0-1.0]" ;
-	param.input3->answer     = "psand";
-	param.input3->guisection = "IN";
+    param.input2 = G_define_standard_option(G_OPT_R_INPUTS);
+    param.input2->key = "input_doy";
+    param.input2->description = "Names of satellite Doy layers";
+    param.input2->guisection = "IN";
 
-	param.input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	param.input4->key        = "psilt";
-	param.input4->description= "Name of the Soil silt fraction map [0.0-1.0]" ;
-	param.input4->answer     = "psilt";
-	param.input4->guisection = "IN";
+    param.input3 = G_define_standard_option(G_OPT_R_INPUT);
+    param.input3->key = "psand";
+    param.input3->description =
+	"Name of the Soil sand fraction map [0.0-1.0]";
+    param.input3->answer = "psand";
+    param.input3->guisection = "IN";
 
-	param.input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	param.input5->key        = "pclay";
-	param.input5->description= "Name of the Soil clay fraction map [0.0-1.0]" ;
-	param.input5->answer     = "pclay";
-	param.input5->guisection = "IN";
+    param.input4 = G_define_standard_option(G_OPT_R_INPUT);
+    param.input4->key = "psilt";
+    param.input4->description =
+	"Name of the Soil silt fraction map [0.0-1.0]";
+    param.input4->answer = "psilt";
+    param.input4->guisection = "IN";
 
-	param.input6 = G_define_standard_option(G_OPT_R_INPUT) ;
-	param.input6->key        = "pomat";
-	param.input6->description= "Name of the Soil Organic Matter map [0.0-1.0]" ;
-	param.input6->answer     = "pomat";
-	param.input6->guisection = "IN";
+    param.input5 = G_define_standard_option(G_OPT_R_INPUT);
+    param.input5->key = "pclay";
+    param.input5->description =
+	"Name of the Soil clay fraction map [0.0-1.0]";
+    param.input5->answer = "pclay";
+    param.input5->guisection = "IN";
 
-	param.input7 = G_define_option() ;
-	param.input7->key        = "pop";
-	param.input7->key_desc   = "count";
-	param.input7->type       = TYPE_INTEGER;
-	param.input7->required   = YES;
-	param.input7->description= "Number of Populations in GAreal";
-	param.input7->answer     = "20";
-	param.input7->guisection = "param_GA";
+    param.input6 = G_define_standard_option(G_OPT_R_INPUT);
+    param.input6->key = "pomat";
+    param.input6->description =
+	"Name of the Soil Organic Matter map [0.0-1.0]";
+    param.input6->answer = "pomat";
+    param.input6->guisection = "IN";
 
-	param.input8 = G_define_option() ;
-	param.input8->key        = "max_gen";
-	param.input8->key_desc   = "count";
-	param.input8->type       = TYPE_INTEGER;
-	param.input8->required   = YES;
-	param.input8->description= "Number of Generations in GAreal";
-	param.input8->answer     = "200";
-	param.input8->guisection = "param_GA";
-	
-	param.input9 = G_define_option() ;
-	param.input9->key        = "pmute";
-	param.input9->key_desc   = "count";
-	param.input9->type       = TYPE_DOUBLE;
-	param.input9->required   = YES;
-	param.input9->description= "Probability of Mutation in GAreal";
-	param.input9->answer     = "0.3";
-	param.input9->guisection = "param_GA";
-	
-	param.input10 = G_define_option() ;
-	param.input10->key        = "pxover";
-	param.input10->key_desc   = "count";
-	param.input10->type       = TYPE_DOUBLE;
-	param.input10->required   = YES;
-	param.input10->description= "Probability of Crossover in GAreal";
-	param.input10->answer     = "0.8";
-	param.input10->guisection = "param_GA";
-	
-	param.input11 = G_define_option() ;
-	param.input11->key        = "target_fitness";
-	param.input11->key_desc   = "count";
-	param.input11->type       = TYPE_DOUBLE;
-	param.input11->required   = YES;
-	param.input11->description= "Target Fitness in GAreal";
-	param.input11->answer     = "20.0";
-	param.input11->guisection = "param_GA";
-	
-	param.input12 = G_define_option() ;
-	param.input12->key        = "year";
-	param.input12->key_desc   = "count";
-	param.input12->type       = TYPE_INTEGER;
-	param.input12->required   = NO;
-	param.input12->description= "Data Processing Year";
-	param.input12->answer     = "2002";
-	param.input12->guisection = "param_SWAP";
+    param.input7 = G_define_option();
+    param.input7->key = "pop";
+    param.input7->key_desc = "count";
+    param.input7->type = TYPE_INTEGER;
+    param.input7->required = YES;
+    param.input7->description = "Number of Populations in GAreal";
+    param.input7->answer = "20";
+    param.input7->guisection = "param_GA";
 
-	param.input13 = G_define_option() ;
-	param.input13->key        = "SSRUN_Year";
-	param.input13->key_desc   = "count";
-	param.input13->type       = TYPE_INTEGER;
-	param.input13->required   = NO;
-	param.input13->description= "Starting Crop Year";
-	param.input13->answer     = "2002";
-	param.input13->guisection = "param_SWAP";
+    param.input8 = G_define_option();
+    param.input8->key = "max_gen";
+    param.input8->key_desc = "count";
+    param.input8->type = TYPE_INTEGER;
+    param.input8->required = YES;
+    param.input8->description = "Number of Generations in GAreal";
+    param.input8->answer = "200";
+    param.input8->guisection = "param_GA";
 
-	param.input14 = G_define_option() ;
-	param.input14->key        = "ESRUN_Year";
-	param.input14->key_desc   = "count";
-	param.input14->type       = TYPE_INTEGER;
-	param.input14->required   = NO;
-	param.input14->description= "Ending Crop Year";
-	param.input14->answer     = "2002";
-	param.input14->guisection = "param_SWAP";
+    param.input9 = G_define_option();
+    param.input9->key = "pmute";
+    param.input9->key_desc = "count";
+    param.input9->type = TYPE_DOUBLE;
+    param.input9->required = YES;
+    param.input9->description = "Probability of Mutation in GAreal";
+    param.input9->answer = "0.3";
+    param.input9->guisection = "param_GA";
 
-	param.input15 = G_define_option() ;
-	param.input15->key        = "METFILE";
-	param.input15->key_desc   = "count";
-	param.input15->type       = TYPE_STRING;
-	param.input15->required   = NO;
-	param.input15->description= "Meteorological File Name";
-	param.input15->answer     = "U-T";
-	param.input15->guisection = "param_SWAP";
+    param.input10 = G_define_option();
+    param.input10->key = "pxover";
+    param.input10->key_desc = "count";
+    param.input10->type = TYPE_DOUBLE;
+    param.input10->required = YES;
+    param.input10->description = "Probability of Crossover in GAreal";
+    param.input10->answer = "0.8";
+    param.input10->guisection = "param_GA";
 
-	
-	param.input16 = G_define_option() ;
-	param.input16->key        = "Lat";
-	param.input16->key_desc   = "count";
-	param.input16->type       = TYPE_DOUBLE;
-	param.input16->required   = NO;
-	param.input16->description= "Latitude of MET Station";
-	param.input16->answer     = "14.338";
-	param.input16->guisection = "param_SWAP";
+    param.input11 = G_define_option();
+    param.input11->key = "target_fitness";
+    param.input11->key_desc = "count";
+    param.input11->type = TYPE_DOUBLE;
+    param.input11->required = YES;
+    param.input11->description = "Target Fitness in GAreal";
+    param.input11->answer = "20.0";
+    param.input11->guisection = "param_GA";
 
+    param.input12 = G_define_option();
+    param.input12->key = "year";
+    param.input12->key_desc = "count";
+    param.input12->type = TYPE_INTEGER;
+    param.input12->required = NO;
+    param.input12->description = "Data Processing Year";
+    param.input12->answer = "2002";
+    param.input12->guisection = "param_SWAP";
 
-	param.input17 = G_define_option() ;
-	param.input17->key        = "Alt";
-	param.input17->key_desc   = "count";
-	param.input17->type       = TYPE_DOUBLE;
-	param.input17->required   = NO;
-	param.input17->description= "Altitude of MET Station";
-	param.input17->answer     = "10.0";
-	param.input17->guisection = "param_SWAP";
-	
-	
-	
-	param.output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output1->key        = "DEC";
-	param.output1->description= "Name of an output Date of Emergence of Crop layer" ;
-	param.output1->answer     = "gaswap_DEC";
-	param.output1->guisection = "OUT";
-	
-	param.output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output2->key        = "FEC";
-	param.output2->description= "Name of an output Forced End of Crop layer" ;
-	param.output2->answer     = "gaswap_FEC";
-	param.output2->guisection = "OUT";
+    param.input13 = G_define_option();
+    param.input13->key = "SSRUN_Year";
+    param.input13->key_desc = "count";
+    param.input13->type = TYPE_INTEGER;
+    param.input13->required = NO;
+    param.input13->description = "Starting Crop Year";
+    param.input13->answer = "2002";
+    param.input13->guisection = "param_SWAP";
 
-	param.output3 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output3->key        = "STS";
-	param.output3->description= "Name of an output Scheduled Time of Irrigation layer" ;
-	param.output3->answer     = "gaswap_STS";
-	param.output3->guisection = "OUT";
+    param.input14 = G_define_option();
+    param.input14->key = "ESRUN_Year";
+    param.input14->key_desc = "count";
+    param.input14->type = TYPE_INTEGER;
+    param.input14->required = NO;
+    param.input14->description = "Ending Crop Year";
+    param.input14->answer = "2002";
+    param.input14->guisection = "param_SWAP";
 
-	param.output4 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output4->key        = "GWJan";
-	param.output4->description= "Name of the Ground Water Depth in January output layer" ;
-	param.output4->answer     = "gaswap_GWJan";
-	param.output4->guisection = "OUT";
+    param.input15 = G_define_option();
+    param.input15->key = "METFILE";
+    param.input15->key_desc = "count";
+    param.input15->type = TYPE_STRING;
+    param.input15->required = NO;
+    param.input15->description = "Meteorological File Name";
+    param.input15->answer = "U-T";
+    param.input15->guisection = "param_SWAP";
 
-	param.output5 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output5->key        = "GWDec";
-	param.output5->description= "Name of the Ground Water Depth in December output layer" ;
-	param.output5->answer     = "gaswap_GWDec";
-	param.output5->guisection = "OUT";
 
-	param.output6 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	param.output6->key        = "fitness";
-	param.output6->description= "Name of the fitness output layer" ;
-	param.output6->answer     = "gaswap_fitness";
-	param.output6->guisection = "OUT";
+    param.input16 = G_define_option();
+    param.input16->key = "Lat";
+    param.input16->key_desc = "count";
+    param.input16->type = TYPE_DOUBLE;
+    param.input16->required = NO;
+    param.input16->description = "Latitude of MET Station";
+    param.input16->answer = "14.338";
+    param.input16->guisection = "param_SWAP";
 
-	/* FMEO init nfiles */
-	nfiles = 1;
+
+    param.input17 = G_define_option();
+    param.input17->key = "Alt";
+    param.input17->key_desc = "count";
+    param.input17->type = TYPE_DOUBLE;
+    param.input17->required = NO;
+    param.input17->description = "Altitude of MET Station";
+    param.input17->answer = "10.0";
+    param.input17->guisection = "param_SWAP";
+
+
+
+    param.output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output1->key = "DEC";
+    param.output1->description =
+	"Name of an output Date of Emergence of Crop layer";
+    param.output1->answer = "gaswap_DEC";
+    param.output1->guisection = "OUT";
+
+    param.output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output2->key = "FEC";
+    param.output2->description = "Name of an output Forced End of Crop layer";
+    param.output2->answer = "gaswap_FEC";
+    param.output2->guisection = "OUT";
+
+    param.output3 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output3->key = "STS";
+    param.output3->description =
+	"Name of an output Scheduled Time of Irrigation layer";
+    param.output3->answer = "gaswap_STS";
+    param.output3->guisection = "OUT";
+
+    param.output4 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output4->key = "GWJan";
+    param.output4->description =
+	"Name of the Ground Water Depth in January output layer";
+    param.output4->answer = "gaswap_GWJan";
+    param.output4->guisection = "OUT";
+
+    param.output5 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output5->key = "GWDec";
+    param.output5->description =
+	"Name of the Ground Water Depth in December output layer";
+    param.output5->answer = "gaswap_GWDec";
+    param.output5->guisection = "OUT";
+
+    param.output6 = G_define_standard_option(G_OPT_R_OUTPUT);
+    param.output6->key = "fitness";
+    param.output6->description = "Name of the fitness output layer";
+    param.output6->answer = "gaswap_fitness";
+    param.output6->guisection = "OUT";
+
+    /* FMEO init nfiles */
+    nfiles = 1;
+
 	/********************/
+
 	/********************/
-	if (G_parser(argc, argv))
-		exit (-1);
+    if (G_parser(argc, argv))
+	exit(-1);
 
-	ok = 1;
-	names    = param.input1->answers;
-	ptr      = param.input1->answers;
-	doy_names    = param.input2->answers;
-	doy_ptr      = param.input2->answers;
-	psand	 = param.input3->answer;
-	psilt	 = param.input4->answer;
-	pclay	 = param.input5->answer;
-	pomat	 = param.input6->answer;
-	pop	 	= atoi(param.input7->answer);
-	maxgen   	= atof(param.input8->answer);
-	pmute    	= atof(param.input9->answer);
-	pxover   	= atof(param.input10->answer); 
-	target_fitness 	= atof(param.input11->answer);
-	year		= atoi(param.input12->answer);
-	SSRUN_Year	= atoi(param.input13->answer);
-	ESRUN_Year	= atoi(param.input14->answer);
-	METFILE		= param.input15->answer;
-	Lat		= atof(param.input16->answer);
-	Alt		= atof(param.input17->answer);
-	
-	result[1]  = param.output1->answer;
-	result[2]  = param.output2->answer;
-	result[3]  = param.output3->answer;
-	result[4]  = param.output4->answer;
-	result[5]  = param.output5->answer;
-	result[6]  = param.output6->answer;
+    ok = 1;
+    names = param.input1->answers;
+    ptr = param.input1->answers;
+    doy_names = param.input2->answers;
+    doy_ptr = param.input2->answers;
+    psand = param.input3->answer;
+    psilt = param.input4->answer;
+    pclay = param.input5->answer;
+    pomat = param.input6->answer;
+    pop = atoi(param.input7->answer);
+    maxgen = atof(param.input8->answer);
+    pmute = atof(param.input9->answer);
+    pxover = atof(param.input10->answer);
+    target_fitness = atof(param.input11->answer);
+    year = atoi(param.input12->answer);
+    SSRUN_Year = atoi(param.input13->answer);
+    ESRUN_Year = atoi(param.input14->answer);
+    METFILE = param.input15->answer;
+    Lat = atof(param.input16->answer);
+    Alt = atof(param.input17->answer);
 
-	/*Satellite dates...*/
-	for (; *doy_ptr != NULL; doy_ptr++)
-	{
-		if (nfiles >= MAXFILES)
-			G_fatal_error ("%s - too many ETa files. Only %d allowed", G_program_name(), MAXFILES);
-		name = *doy_ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error ("cell file [%s] not found", name);
-			sleep(3);
-			ok = 0;
-		}
-	        for (i=1;i<NVAR+1;i++)
-		{
-			if (G_legal_filename (result[i]) < 0){
-				G_fatal_error ("[%s] is an illegal name", result[i]);
-				sleep(3);
-				ok = 0;
-			}
-		}
-		if (!ok)
-			continue;
-		infd_doy[nfiles] = G_open_cell_old (name, mapset);
-		if (infd_doy[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		data_type_doy[nfiles] = G_raster_map_type(name, mapset);
-		if((infd_doy[nfiles] = G_open_cell_old(name,mapset))<0)
-			G_fatal_error("Cannot open cell file [%s]", name);
-		if(G_get_cellhd(name,mapset,&cellhd)<0)
-			G_fatal_error("Cannot read file header of [%s]", name);
-		inrast_doy[nfiles] = G_allocate_raster_buf(data_type_doy[nfiles]);
-		nfiles++;
-	}
-	nfiles--;
-	if (nfiles <= 1)
-		G_fatal_error("The min specified input map is two");
+    result[1] = param.output1->answer;
+    result[2] = param.output2->answer;
+    result[3] = param.output3->answer;
+    result[4] = param.output4->answer;
+    result[5] = param.output5->answer;
+    result[6] = param.output6->answer;
 
-	nfiles=1;
-	/*****************************************************/
-	/*satellite eta*/
-	for (; *ptr != NULL; ptr++)
-	{
-		if (nfiles >= MAXFILES)
-			G_fatal_error ("%s - too many ETa files. Only %d allowed", G_program_name(), MAXFILES);
-		name = *ptr;
-		/* find map in mapset */
-		mapset = G_find_cell2 (name, "");
-	        if (mapset == NULL)
-		{
-			G_fatal_error ("cell file [%s] not found", name);
-			sleep(3);
-			ok = 0;
-		}
-	        for (i=1;i<NVAR+1;i++)
-		{
-			if (G_legal_filename (result[i]) < 0){
-				G_fatal_error ("[%s] is an illegal name", result[i]);
-				sleep(3);
-				ok = 0;
-			}
-		}
-		if (!ok)
-			continue;
-		infd[nfiles] = G_open_cell_old (name, mapset);
-		if (infd[nfiles] < 0)
-		{
-			ok = 0;
-			continue;
-		}
-		/* Allocate input buffer */
-		data_type[nfiles] = G_raster_map_type(name, mapset);
-		if((infd[nfiles] = G_open_cell_old(name,mapset))<0)
-			G_fatal_error("Cannot open cell file [%s]", name);
-		if(G_get_cellhd(name,mapset,&cellhd)<0)
-			G_fatal_error("Cannot read file header of [%s]", name);
-		inrast[nfiles] = G_allocate_raster_buf(data_type[nfiles]);
-		nfiles++;
-	}
-	nfiles--;
-	if (nfiles <= 1)
-		G_fatal_error("The min specified input map is two");
-	
-	/*Load soil files now*/
-	/***************************************************/
-	mapset = G_find_cell2 (psand, "");
+    /*Satellite dates... */
+    for (; *doy_ptr != NULL; doy_ptr++) {
+	if (nfiles >= MAXFILES)
+	    G_fatal_error("%s - too many ETa files. Only %d allowed",
+			  G_program_name(), MAXFILES);
+	name = *doy_ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
 	if (mapset == NULL) {
-		G_fatal_error ("cell file [%s] not found", psand);
-		sleep(3);
-		ok=0;
+	    G_fatal_error("cell file [%s] not found", name);
+	    sleep(3);
+	    ok = 0;
 	}
-	data_type_psand = G_raster_map_type(psand,mapset);
-	if ( (infd_psand = G_open_cell_old (psand,mapset)) < 0)
-		G_fatal_error ("Cannot open cell file [%s]", psand);
-	if (G_get_cellhd (psand, mapset, &cellhd) < 0)
-		G_fatal_error ("Cannot read file header of [%s]", psand);
-	inrast_psand = G_allocate_raster_buf(data_type_psand);
-	/***************************************************/
-	mapset = G_find_cell2 (psilt, "");
-	if (mapset == NULL) {
-		G_fatal_error ("cell file [%s] not found", psilt);
+	for (i = 1; i < NVAR + 1; i++) {
+	    if (G_legal_filename(result[i]) < 0) {
+		G_fatal_error("[%s] is an illegal name", result[i]);
 		sleep(3);
-		ok=0;
+		ok = 0;
+	    }
 	}
-	data_type_psilt = G_raster_map_type(psilt,mapset);
-	if ( (infd_psilt = G_open_cell_old (psilt,mapset)) < 0)
-		G_fatal_error ("Cannot open cell file [%s]", psilt);
-	if (G_get_cellhd (psilt, mapset, &cellhd) < 0)
-		G_fatal_error ("Cannot read file header of [%s]", psilt);
-	inrast_psilt = G_allocate_raster_buf(data_type_psilt);
-	/***************************************************/
-	mapset = G_find_cell2 (pclay, "");
+	if (!ok)
+	    continue;
+	infd_doy[nfiles] = G_open_cell_old(name, mapset);
+	if (infd_doy[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	data_type_doy[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd_doy[nfiles] = G_open_cell_old(name, mapset)) < 0)
+	    G_fatal_error("Cannot open cell file [%s]", name);
+	if (G_get_cellhd(name, mapset, &cellhd) < 0)
+	    G_fatal_error("Cannot read file header of [%s]", name);
+	inrast_doy[nfiles] = G_allocate_raster_buf(data_type_doy[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 1)
+	G_fatal_error("The min specified input map is two");
+
+    nfiles = 1;
+
+	/*****************************************************/
+    /*satellite eta */
+    for (; *ptr != NULL; ptr++) {
+	if (nfiles >= MAXFILES)
+	    G_fatal_error("%s - too many ETa files. Only %d allowed",
+			  G_program_name(), MAXFILES);
+	name = *ptr;
+	/* find map in mapset */
+	mapset = G_find_cell2(name, "");
 	if (mapset == NULL) {
-		G_fatal_error ("Cell file [%s] not found", pclay);
-		sleep(3);
-		ok=0;
+	    G_fatal_error("cell file [%s] not found", name);
+	    sleep(3);
+	    ok = 0;
 	}
-	data_type_pclay = G_raster_map_type(pclay,mapset);
-	if ( (infd_pclay = G_open_cell_old (psilt,mapset)) < 0)
-		G_fatal_error ("Cannot open cell file [%s]", pclay);
-	if (G_get_cellhd (pclay, mapset, &cellhd) < 0)
-		G_fatal_error ("Cannot read file header of [%s]", pclay);
-	inrast_pclay = G_allocate_raster_buf(data_type_pclay);
-	/***************************************************/
-	mapset = G_find_cell2 (pomat, "");
-	if (mapset == NULL) {
-		G_fatal_error ("Cell file [%s] not found", pomat);
+	for (i = 1; i < NVAR + 1; i++) {
+	    if (G_legal_filename(result[i]) < 0) {
+		G_fatal_error("[%s] is an illegal name", result[i]);
 		sleep(3);
-		ok=0;
+		ok = 0;
+	    }
 	}
-	data_type_pomat = G_raster_map_type(pomat,mapset);
-	if ( (infd_pomat = G_open_cell_old (pomat,mapset)) < 0)
-		G_fatal_error ("Cannot open cell file [%s]", pomat);
-	if (G_get_cellhd (pomat, mapset, &cellhd) < 0)
-		G_fatal_error ("Cannot read file header of [%s]", pomat);
-	inrast_pomat = G_allocate_raster_buf(data_type_pomat);
+	if (!ok)
+	    continue;
+	infd[nfiles] = G_open_cell_old(name, mapset);
+	if (infd[nfiles] < 0) {
+	    ok = 0;
+	    continue;
+	}
+	/* Allocate input buffer */
+	data_type[nfiles] = G_raster_map_type(name, mapset);
+	if ((infd[nfiles] = G_open_cell_old(name, mapset)) < 0)
+	    G_fatal_error("Cannot open cell file [%s]", name);
+	if (G_get_cellhd(name, mapset, &cellhd) < 0)
+	    G_fatal_error("Cannot read file header of [%s]", name);
+	inrast[nfiles] = G_allocate_raster_buf(data_type[nfiles]);
+	nfiles++;
+    }
+    nfiles--;
+    if (nfiles <= 1)
+	G_fatal_error("The min specified input map is two");
+
+    /*Load soil files now */
+
 	/***************************************************/
-	//printf("here1\n");
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	
-	//printf("No of rows=%d\nNo of Col=%d\n",nrows,ncols);
-	for (i=1;i<NVAR+2;i++)
-	{
+    mapset = G_find_cell2(psand, "");
+    if (mapset == NULL) {
+	G_fatal_error("cell file [%s] not found", psand);
+	sleep(3);
+	ok = 0;
+    }
+    data_type_psand = G_raster_map_type(psand, mapset);
+    if ((infd_psand = G_open_cell_old(psand, mapset)) < 0)
+	G_fatal_error("Cannot open cell file [%s]", psand);
+    if (G_get_cellhd(psand, mapset, &cellhd) < 0)
+	G_fatal_error("Cannot read file header of [%s]", psand);
+    inrast_psand = G_allocate_raster_buf(data_type_psand);
+
+	/***************************************************/
+    mapset = G_find_cell2(psilt, "");
+    if (mapset == NULL) {
+	G_fatal_error("cell file [%s] not found", psilt);
+	sleep(3);
+	ok = 0;
+    }
+    data_type_psilt = G_raster_map_type(psilt, mapset);
+    if ((infd_psilt = G_open_cell_old(psilt, mapset)) < 0)
+	G_fatal_error("Cannot open cell file [%s]", psilt);
+    if (G_get_cellhd(psilt, mapset, &cellhd) < 0)
+	G_fatal_error("Cannot read file header of [%s]", psilt);
+    inrast_psilt = G_allocate_raster_buf(data_type_psilt);
+
+	/***************************************************/
+    mapset = G_find_cell2(pclay, "");
+    if (mapset == NULL) {
+	G_fatal_error("Cell file [%s] not found", pclay);
+	sleep(3);
+	ok = 0;
+    }
+    data_type_pclay = G_raster_map_type(pclay, mapset);
+    if ((infd_pclay = G_open_cell_old(psilt, mapset)) < 0)
+	G_fatal_error("Cannot open cell file [%s]", pclay);
+    if (G_get_cellhd(pclay, mapset, &cellhd) < 0)
+	G_fatal_error("Cannot read file header of [%s]", pclay);
+    inrast_pclay = G_allocate_raster_buf(data_type_pclay);
+
+	/***************************************************/
+    mapset = G_find_cell2(pomat, "");
+    if (mapset == NULL) {
+	G_fatal_error("Cell file [%s] not found", pomat);
+	sleep(3);
+	ok = 0;
+    }
+    data_type_pomat = G_raster_map_type(pomat, mapset);
+    if ((infd_pomat = G_open_cell_old(pomat, mapset)) < 0)
+	G_fatal_error("Cannot open cell file [%s]", pomat);
+    if (G_get_cellhd(pomat, mapset, &cellhd) < 0)
+	G_fatal_error("Cannot read file header of [%s]", pomat);
+    inrast_pomat = G_allocate_raster_buf(data_type_pomat);
+
+	/***************************************************/
+    //printf("here1\n");
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+
+    //printf("No of rows=%d\nNo of Col=%d\n",nrows,ncols);
+    for (i = 1; i < NVAR + 2; i++) {
 	outrast[i] = G_allocate_raster_buf(1);
+    }
+    /* Create New raster files */
+    for (i = 1; i < NVAR + 1; i++) {
+	if ((outfd[i] = G_open_raster_new(result[i], 1)) < 0)
+	    G_fatal_error("Could not open <%s>", result[i]);
+    }
+    /* Process pixels */
+    for (row = 0; row < nrows; row++) {
+	FCELL f;
+
+	FCELL fe[NVAR + 2];
+	double tex[4] = { 0.0, 0.0, 0.0, 0.0 };
+	G_percent(row, nrows, 2);
+	/* read input map */
+	for (i = 1; i < nfiles + 1; i++) {
+	    sat[i] = 0.0;
+	    sat_doy[i] = 0.0;
+	    if (G_get_raster_row(infd[i], inrast[i], row, data_type[i]) < 0)
+		G_fatal_error("Could not read from <%s>", name);
+	    if (G_get_raster_row
+		(infd_doy[i], inrast_doy[i], row, data_type[i]) < 0)
+		G_fatal_error("Could not read from <%s>", name);
 	}
-	/* Create New raster files */
-	for (i=1;i<NVAR+1;i++){
-		if ( (outfd[i] = G_open_raster_new (result[i],1)) < 0)
-		G_fatal_error ("Could not open <%s>",result[i]);
-	}
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		FCELL f;
-		FCELL fe[NVAR+2];
-		double tex[4] = {0.0,0.0,0.0,0.0};
-		G_percent (row, nrows, 2);
-		/* read input map */
-		for (i=1;i<nfiles+1;i++){
-			sat[i]=0.0;
-			sat_doy[i]=0.0;
-			if(G_get_raster_row(infd[i],inrast[i],row,data_type[i])<0)
-			G_fatal_error ("Could not read from <%s>",name);
-			if(G_get_raster_row(infd_doy[i],inrast_doy[i],row,data_type[i])<0)
-			G_fatal_error ("Could not read from <%s>",name);
-		}
-		/* read soil input maps */	
-		if(G_get_raster_row(infd_psand,inrast_psand,row,1)<0)
-			G_fatal_error ("Could not read from <%s>",name);
-		if(G_get_raster_row(infd_psilt,inrast_psilt,row,1)<0)
-			G_fatal_error ("Could not read from <%s>",name);
-		if(G_get_raster_row(infd_pclay,inrast_pclay,row,1)<0)
-			G_fatal_error ("Could not read from <%s>",name);
-		if(G_get_raster_row(infd_pomat,inrast_pomat,row,1)<0)
-			G_fatal_error ("Could not read from <%s>",name);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_psand){
-				case CELL_TYPE:
-					f = (float) ((CELL *) inrast_psand)[col];
-					break;
-				case FCELL_TYPE:
-					f = ((FCELL *) inrast_psand)[col];
-					break;
-				case DCELL_TYPE:
-					f = (float) ((DCELL *) inrast_psand)[col];
-					break;
-			}
-			tex[0] = f;
-			switch(data_type_psilt){
-				case CELL_TYPE:
-					f = (float) ((CELL *) inrast_psilt)[col];
-					break;
-				case FCELL_TYPE:
-					f = ((FCELL *) inrast_psilt)[col];
-					break;
-				case DCELL_TYPE:
-					f = (float) ((DCELL *) inrast_psilt)[col];
-					break;
-			}
-			tex[1] = f;
-			switch(data_type_pclay){
-				case CELL_TYPE:
-					f = (float) ((CELL *) inrast_pclay)[col];
-					break;
-				case FCELL_TYPE:
-					f = ((FCELL *) inrast_pclay)[col];
-					break;
-				case DCELL_TYPE:
-					f = (float) ((DCELL *) inrast_pclay)[col];
-					break;
-			}
-			tex[2] = f;
-			switch(data_type_pomat){
-				case CELL_TYPE:
-					f = (float) ((CELL *) inrast_pomat)[col];
-					break;
-				case FCELL_TYPE:
-					f = ((FCELL *) inrast_pomat)[col];
-					break;
-				case DCELL_TYPE:
-					f = (float) ((DCELL *) inrast_pomat)[col];
-					break;
-			}
-			tex[3] = f;
+	/* read soil input maps */
+	if (G_get_raster_row(infd_psand, inrast_psand, row, 1) < 0)
+	    G_fatal_error("Could not read from <%s>", name);
+	if (G_get_raster_row(infd_psilt, inrast_psilt, row, 1) < 0)
+	    G_fatal_error("Could not read from <%s>", name);
+	if (G_get_raster_row(infd_pclay, inrast_pclay, row, 1) < 0)
+	    G_fatal_error("Could not read from <%s>", name);
+	if (G_get_raster_row(infd_pomat, inrast_pomat, row, 1) < 0)
+	    G_fatal_error("Could not read from <%s>", name);
+	/*process the data */
+	for (col = 0; col < ncols; col++) {
+	    switch (data_type_psand) {
+	    case CELL_TYPE:
+		f = (float)((CELL *) inrast_psand)[col];
+		break;
+	    case FCELL_TYPE:
+		f = ((FCELL *) inrast_psand)[col];
+		break;
+	    case DCELL_TYPE:
+		f = (float)((DCELL *) inrast_psand)[col];
+		break;
+	    }
+	    tex[0] = f;
+	    switch (data_type_psilt) {
+	    case CELL_TYPE:
+		f = (float)((CELL *) inrast_psilt)[col];
+		break;
+	    case FCELL_TYPE:
+		f = ((FCELL *) inrast_psilt)[col];
+		break;
+	    case DCELL_TYPE:
+		f = (float)((DCELL *) inrast_psilt)[col];
+		break;
+	    }
+	    tex[1] = f;
+	    switch (data_type_pclay) {
+	    case CELL_TYPE:
+		f = (float)((CELL *) inrast_pclay)[col];
+		break;
+	    case FCELL_TYPE:
+		f = ((FCELL *) inrast_pclay)[col];
+		break;
+	    case DCELL_TYPE:
+		f = (float)((DCELL *) inrast_pclay)[col];
+		break;
+	    }
+	    tex[2] = f;
+	    switch (data_type_pomat) {
+	    case CELL_TYPE:
+		f = (float)((CELL *) inrast_pomat)[col];
+		break;
+	    case FCELL_TYPE:
+		f = ((FCELL *) inrast_pomat)[col];
+		break;
+	    case DCELL_TYPE:
+		f = (float)((DCELL *) inrast_pomat)[col];
+		break;
+	    }
+	    tex[3] = f;
+
 			/************************************/
-			for(j=1;j<nfiles+1;j++){
-				f = ((FCELL *) inrast[j])[col];
-				f = f_calc1(f); /* convert to cm/day */
-				sat[j]=f;
-				f = ((FCELL *) inrast_doy[j])[col];
-				sat_doy[j]=f;
-			}
-			gaswap(tex,sat,sat_doy,NVAR,nfiles,year,pop,maxgen, pmute, pxover,target_fitness,SSRUN_Year,ESRUN_Year,METFILE,Lat,Alt,grass_cell);
-				
-			for (j=1;j<NVAR+2;j++)
-			{
- 				fe[j] = grass_cell[j];
-				outrast[j][col] = fe[j];
-			}
-		}
-		for (i=1;i<NVAR+1;i++){
-			if (G_put_raster_row (outfd[i], outrast[i], data_type[i]) < 0)
-				G_fatal_error ("Cannot write to output raster file");
-		}
+	    for (j = 1; j < nfiles + 1; j++) {
+		f = ((FCELL *) inrast[j])[col];
+		f = f_calc1(f);	/* convert to cm/day */
+		sat[j] = f;
+		f = ((FCELL *) inrast_doy[j])[col];
+		sat_doy[j] = f;
+	    }
+	    gaswap(tex, sat, sat_doy, NVAR, nfiles, year, pop, maxgen, pmute,
+		   pxover, target_fitness, SSRUN_Year, ESRUN_Year, METFILE,
+		   Lat, Alt, grass_cell);
+
+	    for (j = 1; j < NVAR + 2; j++) {
+		fe[j] = grass_cell[j];
+		outrast[j][col] = fe[j];
+	    }
 	}
-	for (i=1;i<nfiles+1;i++){
-		G_free (inrast[i]);
-		G_close_cell (infd[i]);
-		G_free (inrast_doy[i]);
-		G_close_cell (infd_doy[i]);
+	for (i = 1; i < NVAR + 1; i++) {
+	    if (G_put_raster_row(outfd[i], outrast[i], data_type[i]) < 0)
+		G_fatal_error("Cannot write to output raster file");
 	}
+    }
+    for (i = 1; i < nfiles + 1; i++) {
+	G_free(inrast[i]);
+	G_close_cell(infd[i]);
+	G_free(inrast_doy[i]);
+	G_close_cell(infd_doy[i]);
+    }
 
-	G_free (inrast_psand);
-	G_free (inrast_psilt);
-	G_free (inrast_pclay);
-	G_free (inrast_pomat);
-	G_close_cell (infd_psand);
-	G_close_cell (infd_psilt);
-	G_close_cell (infd_pclay);
-	G_close_cell (infd_pomat);
-	
-	for (i=1;i<NVAR+2;i++){
-		G_free (outrast[i]);
-		G_close_cell (outfd[i]);
-	}
-	return 0;
-}	
+    G_free(inrast_psand);
+    G_free(inrast_psilt);
+    G_free(inrast_pclay);
+    G_free(inrast_pomat);
+    G_close_cell(infd_psand);
+    G_close_cell(infd_psilt);
+    G_close_cell(infd_pclay);
+    G_close_cell(infd_pomat);
+
+    for (i = 1; i < NVAR + 2; i++) {
+	G_free(outrast[i]);
+	G_close_cell(outfd[i]);
+    }
+    return 0;
+}

Modified: grass-addons/gipe/r.out.vic/main.c
===================================================================
--- grass-addons/gipe/r.out.vic/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.out.vic/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,713 +1,875 @@
-/****************************************************************************
- *
- * MODULE:       r.out.vic
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Creates VIC input files:
- * 		 1 - Meteorological: 
- * 			Three time series of GIS data are needed:
- * 		 	Precipitation (mm/d), Tmax(C) and Tmin(C)
- * 		 2 - Vegetation:
- * 		 	Filling only Land cover class, with only one class,
- * 			 and one standard root system.
- * 			 Option to add monthly LAI (12 maps).
- * 		 3 - Soil:
- *		 	Barely complete, it takes elevation data & lat/long,
- *		 	the rest is filled up with dummy soil data.
- *		 4 - Routing file from GRASS flow direction file:
- *		 	Direct AGNPS or flag for r.watershed format
- *		 	Recoding to rout input file for VIC post-processing
- *		 	of hydrological surface runoff flow.
- *		 	http://www.hydro.washington.edu/Lettenmaier/ 
- *		 	Models/VIC/Documentation/Bernt/
- *		 	rout/mainframe_rout1.htm
- *  
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/gprojects.h>
-#include <grass/glocale.h>
-
-/************************/
-/* daily one year is: 	*/
-#define MAXFILES 366
-
-/************************/
-/* for vegeation: 	*/
-int veglib(char *filename);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	int not_ll=0;/*if proj is not lat/long, it will be 1.*/
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *input5, *input6, *input7;
-	struct Option *output1, *output2, *output3, *output4, *output5;
-	
-	struct Flag *flag1, *flag2;	
-	struct History history; /*metadata*/
-	
-	struct pj_info iproj;
-	struct pj_info oproj;
-	/************************************/
-	/* FMEO Declarations*****************/
-	char 	*prcp_name;/* input first time-series raster files names*/
-	char 	*tmax_name;/* input second time_series raster files names*/
-	char 	*tmin_name;/* input third time_series raster files names*/
-	char 	*landcover_name;/* input raster name*/
-	char 	lai_name[12]; 	/* input monthly raster files name*/
-	char 	*fdir_name;/* input flow direction raster name*/
-	char 	*result1; 	/*output file base name*/
-	char 	*result2; 	/*output Soil file name*/
-	char	*result3; 	/*output Veg file name*/
-	char	*result4; 	/*output flow direction file name*/
-	char 	result_lat_long[50]; 	/*output file name*/
-	/*File Descriptors*/
-	int 	infd_prcp[MAXFILES], infd_tmax[MAXFILES], infd_tmin[MAXFILES];
-	int	infd;
-	int 	infd_landcover, infd_lai[12], infd_fdir;
-	
-	int 	i=0,j=0;
-	double 	xp, yp;
-	double 	xmin, ymin;
-	double 	xmax, ymax;
-	double 	stepx,stepy;
-	double 	latitude, longitude;
-
-	void 			*inrast_prcp[MAXFILES];
-	void 			*inrast_tmax[MAXFILES];
-	void 			*inrast_tmin[MAXFILES];
-	void 			*inrast_elevation;
-	void 			*inrast_landcover;
-	void 			*inrast_lai[12];
-	void 			*inrast_fdir;
-	RASTER_MAP_TYPE 	data_type_inrast_prcp[MAXFILES];
-	RASTER_MAP_TYPE 	data_type_inrast_tmax[MAXFILES];
-	RASTER_MAP_TYPE 	data_type_inrast_tmin[MAXFILES];
-	RASTER_MAP_TYPE 	data_type_inrast_elevation;
-	RASTER_MAP_TYPE 	data_type_inrast_landcover;
-	RASTER_MAP_TYPE 	data_type_inrast_lai[12];
-	RASTER_MAP_TYPE 	data_type_inrast_fdir;
-
-	/****************************************/
-	/* Meteorological maps 			*/
-	FILE	*f; 		/* output ascii file*/
-	int 	grid_count; 	/* grid cell count*/
-	double	prcp[MAXFILES];	/* Precipitation data*/
-	double	tmax[MAXFILES];	/* Tmax data*/
-	double	tmin[MAXFILES];	/* Tmin data*/
-	char 	**prcp_ptr;	/* pointer to get the input1->answers*/
-	char 	**tmax_ptr;	/* pointer to get the input2->answers*/
-	char 	**tmin_ptr;	/* pointer to get the input3->answers*/
-	int	nfiles1, nfiles2, nfiles3, nfiles_shortest;/* count no. input files*/
-	char	**test1, **test2, **test3; /* test number of input files*/
-	char	**ptr1, **ptr2, **ptr3;	/* test number of input files*/
-	/****************************************/
-	/* Elevation map 			*/
-	char	*in;
-	FILE	*g; 		/* output soil ascii file*/
-	int 	process; 	/* process grid cell switch*/
-	char 	*dummy_data1; 	/* dummy data part 1*/
-	char 	*dummy_data2; 	/* dummy data part 2*/
-	double	elevation;	/* average evevation of grid cell*/
-	/****************************************/
-	/* Vegetation map 			*/
-	FILE	*h; 		/* output ascii file*/
-	char 	*dummy_data3; 	/* dummy data part 1*/
-	char 	*dummy_data4; 	/* dummy data part 2*/
-	double	*lai[12];	/* lAI data for each month*/
-	char 	**lai_ptr;	/* pointer to get the input2->answers*/
-	int	nfiles; 	/* count LAI input files*/
-	char	**test, **ptr;	/* test number of LAI input files*/
-	/****************************************/
-	/* Flow Direction			*/
-	FILE	*ef;		/* output flow direction ascii file*/
-	/****************************************/
-
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("VIC, hydrology, precipitation, Tmax, Tmin, soil");
-	module->description = _("* PURPOSE:      Creates VIC input files:\n \
- * 		 1 - Meteorological:\n \
- * 			Three time series of GIS data are needed:\n \
- * 		 	Precipitation (mm/d), Tmax(C) and Tmin(C)\n \
- * 		 2 - Vegetation:\n \
- * 		 	Filling only Land cover class, with only one class,\n \
- * 			 and one standard root system.\n \
- * 			 Option to add monthly LAI (12 maps).\n \
- * 		 3 - Soil:\n \
- *		 	Barely complete, it takes elevation data & lat/long,\n \
- *		 	the rest is filled up with dummy soil data.\n \
- *		 4 - Routing file from GRASS flow direction file:\n \
- *		 	Recoding to rout input file for VIC post-processing\n \
- *		 	of hydrological surface runoff flow.\n \
- *		 	http://www.hydro.washington.edu/Lettenmaier/ \n \
- *		 	Models/VIC/Documentation/Bernt/ \n \
- *		 	rout/mainframe_rout1.htm");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input1->key	   = _("prcp");
-	input1->description=_("Names of the precipitation input maps");
-	input1->guisection=_("Required");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input2->key	   = _("tmax");
-	input2->description=_("Names of the Tmax input maps");
-	input2->guisection=_("Required");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input3->key	   = _("tmin");
-	input3->description=_("Names of the Tmin input maps");
-	input3->guisection=_("Required");
-	
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key	   = _("dem");
-	input4->description=_("Name of the elevation input map");
-	input4->guisection=_("Required");
-	
-	input5 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input5->key	   = _("landcover");
-	input5->description=_("Name of the land cover input map");
-	input5->guisection=_("Required");
-
-	input6 = G_define_standard_option(G_OPT_R_INPUTS) ;
-	input6->key	   = _("LAI");
-	input6->required   = NO;
-	input6->description=_("Name of the LAI input maps (12 of them!)");	
-
-	input7 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input7->key	   = _("flow_dir");
-	input7->description=_("Name of the flow direction input map (GRASS format)");
-	input7->guisection=_("Required");
-
-	output1 = G_define_option();
-	output1->key      	=_("output");
-	output1->description	=_("Base Name of the output vic meteorological ascii files");
-	output1->answer     	=_("data_");
-
-	output2 = G_define_option() ;
-	output2->key        =_("output_soil");
-	output2->description=_("Name of the output vic soil ascii file");
-	output2->answer     =_("vic_soil.asc");
-
-	output3 = G_define_option() ;
-	output3->key        =_("output_veg");
-	output3->description=_("Name of the output vic vegetation ascii file");
-	output3->answer     =_("vic_veg.asc");
-	
-	output4 = G_define_option() ;
-	output4->key        =_("veglib");
-	output4->required   = NO;
-	output4->description=_("Name of a standard vegetation library file");
-	output4->answer     =_("veglib");
-
-	output5 = G_define_option() ;
-	output5->key        =_("output_fdir");
-	output5->description=_("Name of the output flow direction ascii file");
-	output5->answer     =_("vic_fdir.asc");
-
-	flag1 = G_define_flag() ;
-	flag1->key		= 'a';
-	flag1->description	=_("append meteorological data if file already exists (useful if adding additional year of data)");
-
-	flag2 = G_define_flag() ;
-	flag2->key		= 'b';
-	flag2->description	=_("Convert flow direction from r.watershed to AGNPS");
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	in			= input4->answer;
-	landcover_name	 	= input5->answer;
-	fdir_name	 	= input7->answer;
-	result1 		= output1->answer;
-	result2 		= output2->answer;
-	result3 	 	= output3->answer;
-	result4 	 	= output5->answer;
-	/************************************************/
-	/* STANDARD VEGLIB CREATION HERE 		*/
-	if(output4->answer)
-		veglib(output4->answer);
-
-	/************************************************/
-	/* LOADING TMAX TIME SERIES MAPS 		*/
-	test1 = input1->answers;
-	for (ptr1 = test1, nfiles1 = 0; *ptr1 != NULL; ptr1++, nfiles1++)
-		;
-	if (nfiles1 > MAXFILES){
-		G_fatal_error(_("Too many inputs1, change MAXFILES, recompile."));
-	}
-	prcp_ptr = input1->answers;
-	i=0;
-	for(; *prcp_ptr != NULL; prcp_ptr++){
-		prcp_name= *prcp_ptr;
-		mapset = G_find_cell2(prcp_name, "");
-		if (mapset == NULL) 
-			G_fatal_error(_("cell file [%s] not found"), prcp_name);
-		data_type_inrast_prcp[i] = G_raster_map_type(prcp_name,mapset);
-		if ((infd_prcp[i] = G_open_cell_old (prcp_name,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), prcp_name);
-		if (G_get_cellhd (prcp_name, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), prcp_name);
-		inrast_prcp[i] = G_allocate_raster_buf(data_type_inrast_prcp[i]);
-		i++;
-	}
-	nfiles1=i;
-	/************************************************/
-	/* LOADING TMAX TIME SERIES MAPS 		*/
-	test2 = input2->answers;
-	for (ptr2 = test2, nfiles2 = 0; *ptr2 != NULL; ptr2++, nfiles2++)
-		;
-	if (nfiles2 > MAXFILES){
-		G_fatal_error(_("Too many inputs2, change MAXFILES, recompile."));
-	}
-	tmax_ptr = input2->answers;
-	i=0;
-	for(; *tmax_ptr != NULL; tmax_ptr++){
-		tmax_name = *tmax_ptr;
-		mapset = G_find_cell2(tmax_name, "");
-		if (mapset == NULL)
-			G_fatal_error(_("cell file [%s] not found"), tmax_name);
-		data_type_inrast_tmax[i] = G_raster_map_type(tmax_name,mapset);
-		if ((infd_tmax[i] = G_open_cell_old (tmax_name,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"),tmax_name);
-		if (G_get_cellhd (tmax_name, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), tmax_name);
-		inrast_tmax[i] = G_allocate_raster_buf(data_type_inrast_tmax[i]);
-		i++;
-	}
-	nfiles2=i;
-	/************************************************/
-	/* LOADING TMIN TIME SERIES MAPS 		*/
-	test3 = input3->answers;
-	for (ptr3 = test3, nfiles3 = 0; *ptr3 != NULL; ptr3++, nfiles3++)
-		;
-	if (nfiles3 > MAXFILES){
-		G_fatal_error(_("Too many inputs3, change MAXFILES, recompile."));
-	}
-	tmin_ptr = input3->answers;
-	i=0;
-	for(; *tmin_ptr != NULL; tmin_ptr++){
-		tmin_name = *tmin_ptr;
-		mapset = G_find_cell2(tmin_name, "");
-		if (mapset == NULL)
-			G_fatal_error(_("cell file [%s] not found"), tmin_name);
-		data_type_inrast_tmin[i] = G_raster_map_type(tmin_name,mapset);
-		if ((infd_tmin[i] = G_open_cell_old (tmin_name,mapset)) < 0)
-			G_fatal_error (_("Cannot open cell file [%s]"), tmin_name);
-		if (G_get_cellhd (tmin_name, mapset, &cellhd) < 0)
-			G_fatal_error (_("Cannot read file header of [%s])"), tmin_name);
-		inrast_tmin[i] = G_allocate_raster_buf(data_type_inrast_tmin[i]);
-		i++;
-	}
-	nfiles3=i;
-	/************************************************/
-	if(nfiles1<=nfiles2||nfiles1<=nfiles3){
-		nfiles_shortest = nfiles1;
-	} else if (nfiles2<=nfiles1||nfiles2<=nfiles3){
-		nfiles_shortest = nfiles2;
-	} else {
-		nfiles_shortest = nfiles3;
-	}
-	/************************************************/
-	/* Load Elevation file				*/
-	mapset = G_find_cell2(in, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), in);
-	}
-	data_type_inrast_elevation = G_raster_map_type(in,mapset);
-	if ( (infd = G_open_cell_old (in,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), in);
-	if (G_get_cellhd (in, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), in);
-	inrast_elevation = G_allocate_raster_buf(data_type_inrast_elevation);
-	/***************************************************/
-	/************************************************/
-	/* LOADING OPTIONAL LAI MONTHLY MAPS 		*/
-	if (input6->answer){
-		test = input6->answers;
-		for (ptr = test, nfiles = 0; *ptr != NULL; ptr++, nfiles++)
-			;
-		if (nfiles < 12)
-			G_fatal_error(_("The exact number of LAI input maps is 12"));
-		lai_ptr = input6->answers;
-		for(; *lai_ptr != NULL; lai_ptr++){
-			strcpy(lai_name,*lai_ptr);
-		}
-		for(i=0;i<12;i++){
-			mapset = G_find_cell2(&lai_name[i], "");
-			if (mapset == NULL) {
-				G_fatal_error(_("cell file [%s] not found"), lai_name[i]);
-			}
-			data_type_inrast_lai[i] = G_raster_map_type(&lai_name[i],mapset);
-			if ((infd_lai[i] = G_open_cell_old (&lai_name[i],mapset)) < 0)
-				G_fatal_error (_("Cannot open cell file [%s]"), lai_name[i]);
-			if (G_get_cellhd (&lai_name[i], mapset, &cellhd) < 0)
-				G_fatal_error (_("Cannot read file header of [%s])"), lai_name[i]);
-			inrast_lai[i] = G_allocate_raster_buf(data_type_inrast_lai[i]);
-		}
-	}
-	/************************************************/
-	/* LOADING REQUIRED LAND COVER MAP		*/
-	mapset = G_find_cell2(landcover_name, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), landcover_name);
-	}
-	data_type_inrast_landcover = G_raster_map_type(landcover_name,mapset);
-	if ( (infd_landcover = G_open_cell_old (landcover_name,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), landcover_name);
-	if (G_get_cellhd (landcover_name, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), landcover_name);
-	inrast_landcover = G_allocate_raster_buf(data_type_inrast_landcover);
-	/************************************************/
-	/* LOADING REQUIRED BINARY MAP			*/
-	mapset = G_find_cell2(fdir_name, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), fdir_name);
-	}
-	data_type_inrast_fdir = G_raster_map_type(fdir_name,mapset);
-	if ( (infd_fdir = G_open_cell_old (fdir_name,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), fdir_name);
-	if (G_get_cellhd (fdir_name, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), fdir_name);
-	inrast_fdir = G_allocate_raster_buf(data_type_inrast_fdir);
-	/***************************************************/
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-
-	G_get_window(&cellhd);
-
-	stepx=cellhd.ew_res;
-	stepy=cellhd.ns_res;
-
-	xmin=cellhd.west;
-	xmax=cellhd.east;
-	ymin=cellhd.south;
-	ymax=cellhd.north;
-
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-
-	/*Shamelessly stolen from r.sun !!!!	*/
-	/* Set up parameters for projection to lat/long if necessary */
-	if ((G_projection() != PROJECTION_LL)) {
-		not_ll=1;
-		struct Key_Value *in_proj_info, *in_unit_info;
-		if ((in_proj_info = G_get_projinfo()) == NULL)
-			G_fatal_error(_("Can't get projection info of current location"));
-		if ((in_unit_info = G_get_projunits()) == NULL)
-			G_fatal_error(_("Can't get projection units of current location"));
-		if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
-			G_fatal_error(_("Can't get projection key values of current location"));
-		G_free_key_value(in_proj_info);
-		G_free_key_value(in_unit_info);
-		/* Set output projection to latlong w/ same ellipsoid */
-		oproj.zone = 0;
-		oproj.meters = 1.;
-		sprintf(oproj.proj, "ll");
-		if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
-			G_fatal_error(_("Unable to set up lat/long projection parameters"));
-	}/*End of stolen from r.sun */
-
-	/*Initialize soil grid cell process switch*/
-	g=fopen(result2,"w");
-	/*Initialize grid cell process switch*/
-	/*If =1 then process, if =0 then skip*/
-	process = 1; 
-
-	/*Initialize grid cell count*/
-	grid_count = 1;
-	/*Initialize output file */
-	fprintf(g,"#RUN\tGRID\tLAT\tLNG\tINFILT\tDs\tDs_MAX\tWs\tC\tEXPT_1\tEXPT_2\tEXPT_3\tKsat_1\tKsat_2\tKsat_3\tPHI_1\tPHI_2\tPHI_3\tMOIST_1\tMOIST_2\tMOIST_3\tELEV\tDEPTH_1\tDEPTH_2\tDEPTH_3\tAVG_T\tDP\tBUBLE1\tBUBLE2\tBUBLE3\tQUARZ1\tQUARZ2\tQUARZ3\tBULKDN1\tBULKDN2\tBULKDN3\tPARTDN1\tPARTDN2\tPARTDN3\tOFF_GMT\tWcrFT1\tWcrFT2\tWcrFT3\tWpFT1\tWpFT2\tWpFT3\tZ0_SOIL\tZ0_SNOW\tPRCP\tRESM1\tRESM2\tRESM3\tFS_ACTV\tJULY_TAVG\n");
-
-	/*Initialize dummy data*/
-	dummy_data1 = "0.010\t1.e-4\t3.05\t0.93\t2\t4.0\t4.0\t4.0\t250.0\t250.0\t250.0\t-999\t-999\t-999\t0.4\t0.4\t0.4";
-	dummy_data2 = "0.1\t6.90\t2.000\t14.0\t4.0\t75.0\t75.0\t75.0\t0.24\t0.24\t0.24\t1306\t1367\t1367\t2650\t2650\t2650\t-6\t0.330\t0.330\t0.330\t0.133\t0.133\t0.133\t0.001\t0.010\t500\t0.02\t0.02\t0.02\t1\t18.665\n";
-
-	/*Initialize grid cell process switch*/
-	h=fopen(result3,"w");
-
-	/*Initialize dummy data*/
-	dummy_data3 = "0.10 0.1 1.00 0.65 0.50 0.25";
-	dummy_data4 = "0.312 0.413 0.413 0.413 0.413 0.488 0.975 1.150 0.625 0.312 0.312 0.312";
-		
-	/***********************/
-	/* Flow Direction File */
-	/*Initialize grid cell process switch*/
-	ef=fopen(result4,"w");
-	/*Print to ascii file*/
-	G_message("Your ncols,nrows for flow direction map are (%d,%d)\n",ncols,nrows);
-	fprintf(ef,"ncols\t%d\n", ncols);
-	fprintf(ef,"nrows\t%d\n", nrows);
-	/*NORTHWEST LONGITUDE*/
-	G_message("Your xllcorner for flow direction map is %f\n",xmin);
-	fprintf(ef,"xllcorner\t%f\n", xmin);
-	/*NORTHWEST LATITUDE*/
-	G_message("Your yllcorner for flow direction map is %f\n",ymin);
-	fprintf(ef,"yllcorner\t%f\n", ymin);
-	/*CELLSIZE*/
-	G_message("Your cellsize for flow direction map is %f\n",stepx);
-	fprintf(ef,"cellsize\t%f\n", stepx);
-	/*NODATA_value*/
-	G_message("Your NODATA_value for flow direction map is 0\n");
-	fprintf(ef,"NODATA_value\t0\n");
-	/***********************/
-	/***********************/
-	for (row = 0; row < nrows; row++){
-		DCELL 	d_prcp[MAXFILES];
-		DCELL 	d_tmax[MAXFILES];
-		DCELL	d_tmin[MAXFILES];
-		DCELL 	d_elevation;
-		CELL 	c_landcover;
-		CELL 	c_fdir;
-		DCELL 	d_lai[12];
-		G_percent(row,nrows,2);
-		for(i=0;i<nfiles_shortest;i++){
-			if(G_get_raster_row(infd_prcp[i],inrast_prcp[i],row,data_type_inrast_prcp[i])<0)
-				G_fatal_error(_("Could not read from prcp<%d>"),i+1);
-			if(G_get_raster_row(infd_tmax[i],inrast_tmax[i],row,data_type_inrast_tmax[i])<0)
-				G_fatal_error(_("Could not read from tmax<%d>"),i+1);
-			if(G_get_raster_row(infd_tmin[i],inrast_tmin[i],row,data_type_inrast_tmin[i])<0)
-				G_fatal_error(_("Could not read from tmin<%d>"),i+1);
-		}
-		if(G_get_raster_row(infd,inrast_elevation,row,data_type_inrast_elevation)<0)
-			G_fatal_error(_("Could not read from <%s>"),in);
-		if(G_get_raster_row(infd_landcover,inrast_landcover,row,data_type_inrast_landcover)<0)
-			G_fatal_error(_("Could not read from <%s>"),landcover_name);
-		if(input6->answer){
-			for(i=0;i<12;i++){
-				if(G_get_raster_row(infd_lai[i],inrast_lai[i],row,data_type_inrast_lai[i])<0)
-					G_fatal_error(_("Could not read from <%s>"),lai_name[i]);
-				}
-		}
-		if(G_get_raster_row(infd_fdir,inrast_fdir,row,data_type_inrast_fdir)<0)
-			G_fatal_error(_("Could not read from <%s>"),fdir_name);
-		for (col=0; col < ncols; col++){
-			for(i=0;i<nfiles_shortest;i++){
-				/*Extract prcp time series data*/
-				switch(data_type_inrast_prcp[i]){
-					case CELL_TYPE:
-						d_prcp[i]= (double) ((CELL *) inrast_prcp[i])[col];
-						break;
-					case FCELL_TYPE:
-						d_prcp[i]= (double) ((FCELL *) inrast_prcp[i])[col];
-						break;
-					case DCELL_TYPE:
-						d_prcp[i]= (double) ((DCELL *) inrast_prcp[i])[col];
-						break;
-				}
-				/*Extract tmax time series data*/
-				switch(data_type_inrast_tmax[i]){
-					case CELL_TYPE:
-						d_tmax[i]= (double) ((CELL *) inrast_tmax[i])[col];
-						break;
-					case FCELL_TYPE:
-						d_tmax[i]= (double) ((FCELL *) inrast_tmax[i])[col];
-						break;
-					case DCELL_TYPE:
-						d_tmax[i]= (double) ((DCELL *) inrast_tmax[i])[col];
-						break;
-				}
-				/*Extract tmin time series data*/
-				switch(data_type_inrast_tmin[i]){
-					case CELL_TYPE:
-						d_tmin[i]= (double) ((CELL *) inrast_tmin[i])[col];
-						break;
-					case FCELL_TYPE:
-						d_tmin[i]= (double) ((FCELL *) inrast_tmin[i])[col];
-						break;
-					case DCELL_TYPE:
-						d_tmin[i]= (double) ((DCELL *) inrast_tmin[i])[col];
-						break;
-				}
-			}
-			/*Extract elevation data*/
-			switch(data_type_inrast_elevation){
-				case CELL_TYPE:
-					d_elevation = (double) ((CELL *) inrast_elevation)[col];
-					break;
-				case FCELL_TYPE:
-					d_elevation = (double) ((FCELL *) inrast_elevation)[col];
-					break;
-				case DCELL_TYPE:
-					d_elevation = ((DCELL *) inrast_elevation)[col];
-					break;
-			}
-			/*Extract landcover data*/
-			switch(data_type_inrast_landcover){
-				case CELL_TYPE:
-					c_landcover= (int) ((CELL *) inrast_landcover)[col];
-					break;
-				case FCELL_TYPE:
-					c_landcover= (int) ((FCELL *) inrast_landcover)[col];
-					break;
-				case DCELL_TYPE:
-					c_landcover= (int) ((DCELL *) inrast_landcover)[col];
-					break;
-			}
-			/*Extract lai monthly data*/
-			if(input6->answer){
-				for(i=0;i<12;i++){
-					switch(data_type_inrast_lai[i]){
-					case CELL_TYPE:
-						d_lai[i]= (double) ((CELL *) inrast_lai[i])[col];
-						break;
-					case FCELL_TYPE:
-						d_lai[i]= (double) ((FCELL *) inrast_lai[i])[col];
-						break;
-					case DCELL_TYPE:
-						d_lai[i]= (double) ((DCELL *) inrast_lai[i])[col];
-						break;
-					}
-				}
-			}
-			switch(data_type_inrast_fdir){
-				case CELL_TYPE:
-					c_fdir=  ((CELL *) inrast_fdir)[col];
-					break;
-				case FCELL_TYPE:
-					c_fdir= (int) ((FCELL *) inrast_fdir)[col];
-					break;
-				case DCELL_TYPE:
-					c_fdir= (int) ((DCELL *) inrast_fdir)[col];
-					break;
-			}
-			/*Extract lat/long data*/
-			latitude = ymax - ( row * stepy );
-			longitude = xmin + ( col * stepx );
-			if(not_ll){
-				if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
-				    G_fatal_error(_("Error in pj_do_proj"));
-				}
-			}else{
-				/*Do nothing*/
-			}
-			if(G_is_d_null_value(&prcp[0])||
-			G_is_d_null_value(&d_tmax[0])||
-			G_is_d_null_value(&d_tmin[0])||
-			G_is_d_null_value(&d_elevation)||
-			G_is_c_null_value(&c_landcover)||
-			G_is_c_null_value(&c_fdir)){
-				/* Do nothing */
-				c_fdir = -1;
-			} else {
-				/* Make the output .dat file name */
-				sprintf(result_lat_long,"%s%.4f%s%.4f",result1,latitude-stepy/2.0,"_",longitude-stepx/2);	
-				/*Open new ascii file*/
-				if (flag1->answer){
-					/*Initialize grid cell in append mode*/
-					f=fopen(result_lat_long,"a");
-				} else {
-					/*Initialize grid cell in new file mode*/
-					f=fopen(result_lat_long,"w");
-				}
-				/*Print data into the file maps data if available*/
-				for(i=0;i<nfiles_shortest;i++){
-					fprintf(f,"%.2f  %.2f  %.2f\n", d_prcp[i], d_tmax[i], d_tmin[i]);
-				}
-				fclose(f);
-				/*Print to soil ascii file*/
-				fprintf(g,"%d\t%d\t%7.4f\t%8.4f\t%s\t%7.2f\t%s\n", process, grid_count, latitude-stepy/2, longitude-stepx/2, dummy_data1, d_elevation, dummy_data2);
-				/*Print to vegetation ascii file*/
-				/*Grid cell count and number of classes in that grid cell (=1)*/
-				fprintf(h,"%d 1\n", grid_count);
-				/*Class number, percentage that this class covers in the
-				 * grid cell(=1.0, full grid cell)
-				 * 3 root zones with depths of 10cm, 10cm and 1.0m
-				 * for those 3 root zone depths, how much root in each (%)
-				 * here we have 0.65, 0.50 and 0.25
-				 * */
-				fprintf(h,"%d 1.0 %s\n", c_landcover, dummy_data3);
-				/*Load monthly LAI maps data if available*/
-				if(input6->answer){
-					fprintf(h,"%5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f\n", lai[0], lai[1], lai[2], lai[3], lai[4], lai[5], lai[6], lai[7], lai[8], lai[9], lai[10], lai[11]);
-				} else {
-				/*	fprintf(h,"%s\n", dummy_data4);*/
-				}
-				grid_count=grid_count+1;
-			}
-			/*Print to ascii file*/
-			/*Grid cell flow direction value in that grid cell*/
-			if(flag2->answer){
-				/* Convert r.watershed flow dir to AGNPS */
-				if(c_fdir==0||c_fdir==8){
-					fprintf(ef,"3 ");
-				} else if(c_fdir==1){
-					fprintf(ef,"2 ");
-				} else if(c_fdir==2){
-					fprintf(ef,"1 ");
-				} else if(c_fdir==3){
-					fprintf(ef,"8 ");
-				} else if(c_fdir==4){
-					fprintf(ef,"7 ");
-				} else if(c_fdir==5){
-					fprintf(ef,"6 ");
-				} else if(c_fdir==6){
-					fprintf(ef,"5 ");
-				} else if(c_fdir==7){
-					fprintf(ef,"4 ");
-				} else if(c_fdir==-1){
-					fprintf(ef,"0 ");
-				} else {
-					fprintf(ef,"0 ");
-				}
-			} else {
-				if(c_fdir==-1){
-					/* Flow direction NODATA_value=0 */
-					fprintf(ef,"0 ");
-				} else {
-					/* Flow direction format is AGNPS */
-					fprintf(ef,"%d ",c_fdir);
-				}
-			}
-		}
-		fprintf(ef,"\n");
-	}
-	G_message(_("Created %d VIC meteorological files"),grid_count);
-	G_message(_("Created %d VIC grid cells soil/vegetation definitions"),grid_count);
-	G_message(_("Created a routing flow direction input file"));
-	for(i=0;i<nfiles1;i++){
-		G_free (inrast_prcp[i]);
-		G_close_cell (infd_prcp[i]);
-	}
-	for(i=0;i<nfiles2;i++){
-		G_free (inrast_tmax[i]);
-		G_close_cell (infd_tmax[i]);
-	}
-	for(i=0;i<nfiles3;i++){
-		G_free (inrast_tmin[i]);
-		G_close_cell (infd_tmin[i]);
-	}
-	G_free (inrast_elevation);
-	G_close_cell (infd);
-	G_free (inrast_landcover);
-	G_close_cell (infd_landcover);
-	if(input6->answer){
-		for(i=0;i<12;i++){
-			G_free (inrast_lai[i]);
-			G_close_cell (infd_lai[i]);
-		}
-	}
-	G_free (inrast_fdir);
-	G_close_cell (infd_fdir);
-	fclose(h);/* Vegetation ascii file*/
-	fclose(g);/* Soil ascii file*/
-	fclose(ef);/* Flow Direction ascii file*/
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       r.out.vic
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Creates VIC input files:
+ * 		 1 - Meteorological: 
+ * 			Three time series of GIS data are needed:
+ * 		 	Precipitation (mm/d), Tmax(C) and Tmin(C)
+ * 		 2 - Vegetation:
+ * 		 	Filling only Land cover class, with only one class,
+ * 			 and one standard root system.
+ * 			 Option to add monthly LAI (12 maps).
+ * 		 3 - Soil:
+ *		 	Barely complete, it takes elevation data & lat/long,
+ *		 	the rest is filled up with dummy soil data.
+ *		 4 - Routing file from GRASS flow direction file:
+ *		 	Direct AGNPS or flag for r.watershed format
+ *		 	Recoding to rout input file for VIC post-processing
+ *		 	of hydrological surface runoff flow.
+ *		 	http://www.hydro.washington.edu/Lettenmaier/ 
+ *		 	Models/VIC/Documentation/Bernt/
+ *		 	rout/mainframe_rout1.htm
+ *  
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/gprojects.h>
+#include <grass/glocale.h>
+    
+
+/************************/ 
+    /* daily one year is:   */ 
+#define MAXFILES 366
+    
+
+/************************/ 
+    /* for vegeation:       */ 
+int veglib(char *filename);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    int not_ll = 0;		/*if proj is not lat/long, it will be 1. */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *input5, *input6, *input7;
+
+    struct Option *output1, *output2, *output3, *output4, *output5;
+
+    struct Flag *flag1, *flag2;
+
+    struct History history;	/*metadata */
+
+    struct pj_info iproj;
+
+    struct pj_info oproj;
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *prcp_name;		/* input first time-series raster files names */
+
+    char *tmax_name;		/* input second time_series raster files names */
+
+    char *tmin_name;		/* input third time_series raster files names */
+
+    char *landcover_name;	/* input raster name */
+
+    char lai_name[12];		/* input monthly raster files name */
+
+    char *fdir_name;		/* input flow direction raster name */
+
+    char *result1;		/*output file base name */
+
+    char *result2;		/*output Soil file name */
+
+    char *result3;		/*output Veg file name */
+
+    char *result4;		/*output flow direction file name */
+
+    char result_lat_long[50];	/*output file name */
+
+    
+	/*File Descriptors */ 
+    int infd_prcp[MAXFILES], infd_tmax[MAXFILES], infd_tmin[MAXFILES];
+
+    int infd;
+
+    int infd_landcover, infd_lai[12], infd_fdir;
+
+    int i = 0, j = 0;
+
+    double xp, yp;
+
+    double xmin, ymin;
+
+    double xmax, ymax;
+
+    double stepx, stepy;
+
+    double latitude, longitude;
+
+    void *inrast_prcp[MAXFILES];
+
+    void *inrast_tmax[MAXFILES];
+
+    void *inrast_tmin[MAXFILES];
+
+    void *inrast_elevation;
+
+    void *inrast_landcover;
+
+    void *inrast_lai[12];
+
+    void *inrast_fdir;
+
+    RASTER_MAP_TYPE data_type_inrast_prcp[MAXFILES];
+    RASTER_MAP_TYPE data_type_inrast_tmax[MAXFILES];
+    RASTER_MAP_TYPE data_type_inrast_tmin[MAXFILES];
+    RASTER_MAP_TYPE data_type_inrast_elevation;
+    RASTER_MAP_TYPE data_type_inrast_landcover;
+    RASTER_MAP_TYPE data_type_inrast_lai[12];
+    RASTER_MAP_TYPE data_type_inrast_fdir;
+    
+
+	/****************************************/ 
+	/* Meteorological maps                  */ 
+	FILE * f;		/* output ascii file */
+    int grid_count;		/* grid cell count */
+
+    double prcp[MAXFILES];	/* Precipitation data */
+
+    double tmax[MAXFILES];	/* Tmax data */
+
+    double tmin[MAXFILES];	/* Tmin data */
+
+    char **prcp_ptr;		/* pointer to get the input1->answers */
+
+    char **tmax_ptr;		/* pointer to get the input2->answers */
+
+    char **tmin_ptr;		/* pointer to get the input3->answers */
+
+    int nfiles1, nfiles2, nfiles3, nfiles_shortest;	/* count no. input files */
+
+    char **test1, **test2, **test3;	/* test number of input files */
+
+    char **ptr1, **ptr2, **ptr3;	/* test number of input files */
+
+    
+
+	/****************************************/ 
+	/* Elevation map                        */ 
+    char *in;
+
+    FILE * g;			/* output soil ascii file */
+    int process;		/* process grid cell switch */
+
+    char *dummy_data1;		/* dummy data part 1 */
+
+    char *dummy_data2;		/* dummy data part 2 */
+
+    double elevation;		/* average evevation of grid cell */
+
+    
+
+	/****************************************/ 
+	/* Vegetation map                       */ 
+	FILE * h;		/* output ascii file */
+    char *dummy_data3;		/* dummy data part 1 */
+
+    char *dummy_data4;		/* dummy data part 2 */
+
+    double *lai[12];		/* lAI data for each month */
+
+    char **lai_ptr;		/* pointer to get the input2->answers */
+
+    int nfiles;		/* count LAI input files */
+
+    char **test, **ptr;	/* test number of LAI input files */
+
+    
+
+	/****************************************/ 
+	/* Flow Direction                       */ 
+	FILE * ef;		/* output flow direction ascii file */
+    
+
+	/****************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("VIC, hydrology, precipitation, Tmax, Tmin, soil");
+    module->description = _("* PURPOSE:      Creates VIC input files:\n \
+ * 		 1 - Meteorological:\n \
+ * 			Three time series of GIS data are needed:\n \
+ * 		 	Precipitation (mm/d), Tmax(C) and Tmin(C)\n \
+ * 		 2 - Vegetation:\n \
+ * 		 	Filling only Land cover class, with only one class,\n \
+ * 			 and one standard root system.\n \
+ * 			 Option to add monthly LAI (12 maps).\n \
+ * 		 3 - Soil:\n \
+ *		 	Barely complete, it takes elevation data & lat/long,\n \
+ *		 	the rest is filled up with dummy soil data.\n \
+ *		 4 - Routing file from GRASS flow direction file:\n \
+ *		 	Recoding to rout input file for VIC post-processing\n \
+ *		 	of hydrological surface runoff flow.\n \
+ *		 	http://www.hydro.washington.edu/Lettenmaier/ \n \
+ *		 	Models/VIC/Documentation/Bernt/ \n \
+ *		 	rout/mainframe_rout1.htm");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUTS);
+    input1->key = _("prcp");
+    input1->description = _("Names of the precipitation input maps");
+    input1->guisection = _("Required");
+    input2 = G_define_standard_option(G_OPT_R_INPUTS);
+    input2->key = _("tmax");
+    input2->description = _("Names of the Tmax input maps");
+    input2->guisection = _("Required");
+    input3 = G_define_standard_option(G_OPT_R_INPUTS);
+    input3->key = _("tmin");
+    input3->description = _("Names of the Tmin input maps");
+    input3->guisection = _("Required");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("dem");
+    input4->description = _("Name of the elevation input map");
+    input4->guisection = _("Required");
+    input5 = G_define_standard_option(G_OPT_R_INPUT);
+    input5->key = _("landcover");
+    input5->description = _("Name of the land cover input map");
+    input5->guisection = _("Required");
+    input6 = G_define_standard_option(G_OPT_R_INPUTS);
+    input6->key = _("LAI");
+    input6->required = NO;
+    input6->description = _("Name of the LAI input maps (12 of them!)");
+    input7 = G_define_standard_option(G_OPT_R_INPUT);
+    input7->key = _("flow_dir");
+    input7->description =
+	_("Name of the flow direction input map (GRASS format)");
+    input7->guisection = _("Required");
+    output1 = G_define_option();
+    output1->key = _("output");
+    output1->description =
+	_("Base Name of the output vic meteorological ascii files");
+    output1->answer = _("data_");
+    output2 = G_define_option();
+    output2->key = _("output_soil");
+    output2->description = _("Name of the output vic soil ascii file");
+    output2->answer = _("vic_soil.asc");
+    output3 = G_define_option();
+    output3->key = _("output_veg");
+    output3->description = _("Name of the output vic vegetation ascii file");
+    output3->answer = _("vic_veg.asc");
+    output4 = G_define_option();
+    output4->key = _("veglib");
+    output4->required = NO;
+    output4->description = _("Name of a standard vegetation library file");
+    output4->answer = _("veglib");
+    output5 = G_define_option();
+    output5->key = _("output_fdir");
+    output5->description = _("Name of the output flow direction ascii file");
+    output5->answer = _("vic_fdir.asc");
+    flag1 = G_define_flag();
+    flag1->key = 'a';
+    flag1->description =
+	_("append meteorological data if file already exists (useful if adding additional year of data)");
+    flag2 = G_define_flag();
+    flag2->key = 'b';
+    flag2->description =
+	_("Convert flow direction from r.watershed to AGNPS");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    in = input4->answer;
+    landcover_name = input5->answer;
+    fdir_name = input7->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    result3 = output3->answer;
+    result4 = output5->answer;
+    
+
+	/************************************************/ 
+	/* STANDARD VEGLIB CREATION HERE                */ 
+	if (output4->answer)
+	veglib(output4->answer);
+    
+
+	/************************************************/ 
+	/* LOADING TMAX TIME SERIES MAPS                */ 
+	test1 = input1->answers;
+    for (ptr1 = test1, nfiles1 = 0; *ptr1 != NULL; ptr1++, nfiles1++)
+	;
+    if (nfiles1 > MAXFILES) {
+	G_fatal_error(_("Too many inputs1, change MAXFILES, recompile."));
+    }
+    prcp_ptr = input1->answers;
+    i = 0;
+    for (; *prcp_ptr != NULL; prcp_ptr++) {
+	prcp_name = *prcp_ptr;
+	mapset = G_find_cell2(prcp_name, "");
+	if (mapset == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), prcp_name);
+	data_type_inrast_prcp[i] = G_raster_map_type(prcp_name, mapset);
+	if ((infd_prcp[i] = G_open_cell_old(prcp_name, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), prcp_name);
+	if (G_get_cellhd(prcp_name, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s])"), prcp_name);
+	inrast_prcp[i] = G_allocate_raster_buf(data_type_inrast_prcp[i]);
+	i++;
+    }
+    nfiles1 = i;
+    
+
+	/************************************************/ 
+	/* LOADING TMAX TIME SERIES MAPS                */ 
+	test2 = input2->answers;
+    for (ptr2 = test2, nfiles2 = 0; *ptr2 != NULL; ptr2++, nfiles2++)
+	;
+    if (nfiles2 > MAXFILES) {
+	G_fatal_error(_("Too many inputs2, change MAXFILES, recompile."));
+    }
+    tmax_ptr = input2->answers;
+    i = 0;
+    for (; *tmax_ptr != NULL; tmax_ptr++) {
+	tmax_name = *tmax_ptr;
+	mapset = G_find_cell2(tmax_name, "");
+	if (mapset == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), tmax_name);
+	data_type_inrast_tmax[i] = G_raster_map_type(tmax_name, mapset);
+	if ((infd_tmax[i] = G_open_cell_old(tmax_name, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), tmax_name);
+	if (G_get_cellhd(tmax_name, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s])"), tmax_name);
+	inrast_tmax[i] = G_allocate_raster_buf(data_type_inrast_tmax[i]);
+	i++;
+    }
+    nfiles2 = i;
+    
+
+	/************************************************/ 
+	/* LOADING TMIN TIME SERIES MAPS                */ 
+	test3 = input3->answers;
+    for (ptr3 = test3, nfiles3 = 0; *ptr3 != NULL; ptr3++, nfiles3++)
+	;
+    if (nfiles3 > MAXFILES) {
+	G_fatal_error(_("Too many inputs3, change MAXFILES, recompile."));
+    }
+    tmin_ptr = input3->answers;
+    i = 0;
+    for (; *tmin_ptr != NULL; tmin_ptr++) {
+	tmin_name = *tmin_ptr;
+	mapset = G_find_cell2(tmin_name, "");
+	if (mapset == NULL)
+	    G_fatal_error(_("cell file [%s] not found"), tmin_name);
+	data_type_inrast_tmin[i] = G_raster_map_type(tmin_name, mapset);
+	if ((infd_tmin[i] = G_open_cell_old(tmin_name, mapset)) < 0)
+	    G_fatal_error(_("Cannot open cell file [%s]"), tmin_name);
+	if (G_get_cellhd(tmin_name, mapset, &cellhd) < 0)
+	    G_fatal_error(_("Cannot read file header of [%s])"), tmin_name);
+	inrast_tmin[i] = G_allocate_raster_buf(data_type_inrast_tmin[i]);
+	i++;
+    }
+    nfiles3 = i;
+    
+
+	/************************************************/ 
+	if (nfiles1 <= nfiles2 || nfiles1 <= nfiles3) {
+	nfiles_shortest = nfiles1;
+    }
+    else if (nfiles2 <= nfiles1 || nfiles2 <= nfiles3) {
+	nfiles_shortest = nfiles2;
+    }
+    else {
+	nfiles_shortest = nfiles3;
+    }
+    
+
+	/************************************************/ 
+	/* Load Elevation file                          */ 
+	mapset = G_find_cell2(in, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), in);
+    }
+    data_type_inrast_elevation = G_raster_map_type(in, mapset);
+    if ((infd = G_open_cell_old(in, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), in);
+    if (G_get_cellhd(in, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), in);
+    inrast_elevation = G_allocate_raster_buf(data_type_inrast_elevation);
+    
+
+	/***************************************************/ 
+
+	/************************************************/ 
+	/* LOADING OPTIONAL LAI MONTHLY MAPS            */ 
+	if (input6->answer) {
+	test = input6->answers;
+	for (ptr = test, nfiles = 0; *ptr != NULL; ptr++, nfiles++)
+	    ;
+	if (nfiles < 12)
+	    G_fatal_error(_("The exact number of LAI input maps is 12"));
+	lai_ptr = input6->answers;
+	for (; *lai_ptr != NULL; lai_ptr++) {
+	    strcpy(lai_name, *lai_ptr);
+	}
+	for (i = 0; i < 12; i++) {
+	    mapset = G_find_cell2(&lai_name[i], "");
+	    if (mapset == NULL) {
+		G_fatal_error(_("cell file [%s] not found"), lai_name[i]);
+	    }
+	    data_type_inrast_lai[i] =
+		G_raster_map_type(&lai_name[i], mapset);
+	    if ((infd_lai[i] = G_open_cell_old(&lai_name[i], mapset)) < 0)
+		G_fatal_error(_("Cannot open cell file [%s]"), lai_name[i]);
+	    if (G_get_cellhd(&lai_name[i], mapset, &cellhd) < 0)
+		G_fatal_error(_("Cannot read file header of [%s])"),
+			       lai_name[i]);
+	    inrast_lai[i] = G_allocate_raster_buf(data_type_inrast_lai[i]);
+	}
+    }
+    
+
+	/************************************************/ 
+	/* LOADING REQUIRED LAND COVER MAP              */ 
+	mapset = G_find_cell2(landcover_name, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), landcover_name);
+    }
+    data_type_inrast_landcover = G_raster_map_type(landcover_name, mapset);
+    if ((infd_landcover = G_open_cell_old(landcover_name, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), landcover_name);
+    if (G_get_cellhd(landcover_name, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), landcover_name);
+    inrast_landcover = G_allocate_raster_buf(data_type_inrast_landcover);
+    
+
+	/************************************************/ 
+	/* LOADING REQUIRED BINARY MAP                  */ 
+	mapset = G_find_cell2(fdir_name, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), fdir_name);
+    }
+    data_type_inrast_fdir = G_raster_map_type(fdir_name, mapset);
+    if ((infd_fdir = G_open_cell_old(fdir_name, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), fdir_name);
+    if (G_get_cellhd(fdir_name, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), fdir_name);
+    inrast_fdir = G_allocate_raster_buf(data_type_inrast_fdir);
+    
+
+	/***************************************************/ 
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    G_get_window(&cellhd);
+    stepx = cellhd.ew_res;
+    stepy = cellhd.ns_res;
+    xmin = cellhd.west;
+    xmax = cellhd.east;
+    ymin = cellhd.south;
+    ymax = cellhd.north;
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    
+	/*Shamelessly stolen from r.sun !!!!    */ 
+	/* Set up parameters for projection to lat/long if necessary */ 
+	if ((G_projection() != PROJECTION_LL)) {
+	not_ll = 1;
+	struct Key_Value *in_proj_info, *in_unit_info;
+
+	if ((in_proj_info = G_get_projinfo()) == NULL)
+	    G_fatal_error(_("Can't get projection info of current location"));
+	if ((in_unit_info = G_get_projunits()) == NULL)
+	    G_fatal_error(_("Can't get projection units of current location"));
+	if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
+	    G_fatal_error(_("Can't get projection key values of current location"));
+	G_free_key_value(in_proj_info);
+	G_free_key_value(in_unit_info);
+	
+	    /* Set output projection to latlong w/ same ellipsoid */ 
+	    oproj.zone = 0;
+	oproj.meters = 1.;
+	sprintf(oproj.proj, "ll");
+	if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
+	    G_fatal_error(_("Unable to set up lat/long projection parameters"));
+    }				/*End of stolen from r.sun */
+    
+	/*Initialize soil grid cell process switch */ 
+	g = fopen(result2, "w");
+    
+	/*Initialize grid cell process switch */ 
+	/*If =1 then process, if =0 then skip */ 
+	process = 1;
+    
+	/*Initialize grid cell count */ 
+	grid_count = 1;
+    
+	/*Initialize output file */ 
+	fprintf(g,
+		"#RUN\tGRID\tLAT\tLNG\tINFILT\tDs\tDs_MAX\tWs\tC\tEXPT_1\tEXPT_2\tEXPT_3\tKsat_1\tKsat_2\tKsat_3\tPHI_1\tPHI_2\tPHI_3\tMOIST_1\tMOIST_2\tMOIST_3\tELEV\tDEPTH_1\tDEPTH_2\tDEPTH_3\tAVG_T\tDP\tBUBLE1\tBUBLE2\tBUBLE3\tQUARZ1\tQUARZ2\tQUARZ3\tBULKDN1\tBULKDN2\tBULKDN3\tPARTDN1\tPARTDN2\tPARTDN3\tOFF_GMT\tWcrFT1\tWcrFT2\tWcrFT3\tWpFT1\tWpFT2\tWpFT3\tZ0_SOIL\tZ0_SNOW\tPRCP\tRESM1\tRESM2\tRESM3\tFS_ACTV\tJULY_TAVG\n");
+    
+	/*Initialize dummy data */ 
+	dummy_data1 =
+	"0.010\t1.e-4\t3.05\t0.93\t2\t4.0\t4.0\t4.0\t250.0\t250.0\t250.0\t-999\t-999\t-999\t0.4\t0.4\t0.4";
+    dummy_data2 =
+	"0.1\t6.90\t2.000\t14.0\t4.0\t75.0\t75.0\t75.0\t0.24\t0.24\t0.24\t1306\t1367\t1367\t2650\t2650\t2650\t-6\t0.330\t0.330\t0.330\t0.133\t0.133\t0.133\t0.001\t0.010\t500\t0.02\t0.02\t0.02\t1\t18.665\n";
+    
+	/*Initialize grid cell process switch */ 
+	h = fopen(result3, "w");
+    
+	/*Initialize dummy data */ 
+	dummy_data3 = "0.10 0.1 1.00 0.65 0.50 0.25";
+    dummy_data4 =
+	"0.312 0.413 0.413 0.413 0.413 0.488 0.975 1.150 0.625 0.312 0.312 0.312";
+    
+
+	/***********************/ 
+	/* Flow Direction File */ 
+	/*Initialize grid cell process switch */ 
+	ef = fopen(result4, "w");
+    
+	/*Print to ascii file */ 
+	G_message("Your ncols,nrows for flow direction map are (%d,%d)\n",
+		  ncols, nrows);
+    fprintf(ef, "ncols\t%d\n", ncols);
+    fprintf(ef, "nrows\t%d\n", nrows);
+    
+	/*NORTHWEST LONGITUDE */ 
+	G_message("Your xllcorner for flow direction map is %f\n", xmin);
+    fprintf(ef, "xllcorner\t%f\n", xmin);
+    
+	/*NORTHWEST LATITUDE */ 
+	G_message("Your yllcorner for flow direction map is %f\n", ymin);
+    fprintf(ef, "yllcorner\t%f\n", ymin);
+     /*CELLSIZE*/ G_message("Your cellsize for flow direction map is %f\n",
+			      stepx);
+    fprintf(ef, "cellsize\t%f\n", stepx);
+    
+	/*NODATA_value */ 
+	G_message("Your NODATA_value for flow direction map is 0\n");
+    fprintf(ef, "NODATA_value\t0\n");
+    
+
+	/***********************/ 
+
+	/***********************/ 
+	for (row = 0; row < nrows; row++) {
+	DCELL d_prcp[MAXFILES];
+	DCELL d_tmax[MAXFILES];
+	DCELL d_tmin[MAXFILES];
+	DCELL d_elevation;
+	CELL c_landcover;
+	CELL c_fdir;
+	DCELL d_lai[12];
+	G_percent(row, nrows, 2);
+	for (i = 0; i < nfiles_shortest; i++) {
+	    if (G_get_raster_row
+		 (infd_prcp[i], inrast_prcp[i], row,
+		  data_type_inrast_prcp[i]) < 0)
+		G_fatal_error(_("Could not read from prcp<%d>"), i + 1);
+	    if (G_get_raster_row
+		 (infd_tmax[i], inrast_tmax[i], row,
+		  data_type_inrast_tmax[i]) < 0)
+		G_fatal_error(_("Could not read from tmax<%d>"), i + 1);
+	    if (G_get_raster_row
+		 (infd_tmin[i], inrast_tmin[i], row,
+		  data_type_inrast_tmin[i]) < 0)
+		G_fatal_error(_("Could not read from tmin<%d>"), i + 1);
+	}
+	if (G_get_raster_row
+	     (infd, inrast_elevation, row, data_type_inrast_elevation) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), in);
+	if (G_get_raster_row
+	     (infd_landcover, inrast_landcover, row,
+	      data_type_inrast_landcover) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), landcover_name);
+	if (input6->answer) {
+	    for (i = 0; i < 12; i++) {
+		if (G_get_raster_row
+		     (infd_lai[i], inrast_lai[i], row,
+		      data_type_inrast_lai[i]) < 0)
+		    G_fatal_error(_("Could not read from <%s>"),
+				   lai_name[i]);
+	    }
+	}
+	if (G_get_raster_row
+	     (infd_fdir, inrast_fdir, row, data_type_inrast_fdir) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), fdir_name);
+	for (col = 0; col < ncols; col++) {
+	    for (i = 0; i < nfiles_shortest; i++) {
+		
+		    /*Extract prcp time series data */ 
+		    switch (data_type_inrast_prcp[i]) {
+		case CELL_TYPE:
+		    d_prcp[i] = (double)((CELL *) inrast_prcp[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d_prcp[i] = (double)((FCELL *) inrast_prcp[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d_prcp[i] = (double)((DCELL *) inrast_prcp[i])[col];
+		    break;
+		}
+		
+		    /*Extract tmax time series data */ 
+		    switch (data_type_inrast_tmax[i]) {
+		case CELL_TYPE:
+		    d_tmax[i] = (double)((CELL *) inrast_tmax[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d_tmax[i] = (double)((FCELL *) inrast_tmax[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d_tmax[i] = (double)((DCELL *) inrast_tmax[i])[col];
+		    break;
+		}
+		
+		    /*Extract tmin time series data */ 
+		    switch (data_type_inrast_tmin[i]) {
+		case CELL_TYPE:
+		    d_tmin[i] = (double)((CELL *) inrast_tmin[i])[col];
+		    break;
+		case FCELL_TYPE:
+		    d_tmin[i] = (double)((FCELL *) inrast_tmin[i])[col];
+		    break;
+		case DCELL_TYPE:
+		    d_tmin[i] = (double)((DCELL *) inrast_tmin[i])[col];
+		    break;
+		}
+	    }
+	    
+		/*Extract elevation data */ 
+		switch (data_type_inrast_elevation) {
+	    case CELL_TYPE:
+		d_elevation = (double)((CELL *) inrast_elevation)[col];
+		break;
+	    case FCELL_TYPE:
+		d_elevation = (double)((FCELL *) inrast_elevation)[col];
+		break;
+	    case DCELL_TYPE:
+		d_elevation = ((DCELL *) inrast_elevation)[col];
+		break;
+	    }
+	    
+		/*Extract landcover data */ 
+		switch (data_type_inrast_landcover) {
+	    case CELL_TYPE:
+		c_landcover = (int)((CELL *) inrast_landcover)[col];
+		break;
+	    case FCELL_TYPE:
+		c_landcover = (int)((FCELL *) inrast_landcover)[col];
+		break;
+	    case DCELL_TYPE:
+		c_landcover = (int)((DCELL *) inrast_landcover)[col];
+		break;
+	    }
+	    
+		/*Extract lai monthly data */ 
+		if (input6->answer) {
+		for (i = 0; i < 12; i++) {
+		    switch (data_type_inrast_lai[i]) {
+		    case CELL_TYPE:
+			d_lai[i] = (double)((CELL *) inrast_lai[i])[col];
+			break;
+		    case FCELL_TYPE:
+			d_lai[i] = (double)((FCELL *) inrast_lai[i])[col];
+			break;
+		    case DCELL_TYPE:
+			d_lai[i] = (double)((DCELL *) inrast_lai[i])[col];
+			break;
+		    }
+		}
+	    }
+	    switch (data_type_inrast_fdir) {
+	    case CELL_TYPE:
+		c_fdir = ((CELL *) inrast_fdir)[col];
+		break;
+	    case FCELL_TYPE:
+		c_fdir = (int)((FCELL *) inrast_fdir)[col];
+		break;
+	    case DCELL_TYPE:
+		c_fdir = (int)((DCELL *) inrast_fdir)[col];
+		break;
+	    }
+	    
+		/*Extract lat/long data */ 
+		latitude = ymax - (row * stepy);
+	    longitude = xmin + (col * stepx);
+	    if (not_ll) {
+		if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
+		    G_fatal_error(_("Error in pj_do_proj"));
+		}
+	    }
+	    else {
+		
+		    /*Do nothing */ 
+	    }
+	    if (G_is_d_null_value(&prcp[0]) || G_is_d_null_value(&d_tmax[0])
+		 || G_is_d_null_value(&d_tmin[0]) ||
+		 G_is_d_null_value(&d_elevation) ||
+		 G_is_c_null_value(&c_landcover) ||
+		 G_is_c_null_value(&c_fdir)) {
+		
+		    /* Do nothing */ 
+		    c_fdir = -1;
+	    }
+	    else {
+		
+		    /* Make the output .dat file name */ 
+		    sprintf(result_lat_long, "%s%.4f%s%.4f", result1,
+			    latitude - stepy / 2.0, "_",
+			    longitude - stepx / 2);
+		
+		    /*Open new ascii file */ 
+		    if (flag1->answer) {
+		    
+			/*Initialize grid cell in append mode */ 
+			f = fopen(result_lat_long, "a");
+		}
+		else {
+		    
+			/*Initialize grid cell in new file mode */ 
+			f = fopen(result_lat_long, "w");
+		}
+		
+		    /*Print data into the file maps data if available */ 
+		    for (i = 0; i < nfiles_shortest; i++) {
+		    fprintf(f, "%.2f  %.2f  %.2f\n", d_prcp[i], d_tmax[i],
+			     d_tmin[i]);
+		}
+		fclose(f);
+		
+		    /*Print to soil ascii file */ 
+		    fprintf(g, "%d\t%d\t%7.4f\t%8.4f\t%s\t%7.2f\t%s\n",
+			    process, grid_count, latitude - stepy / 2,
+			    longitude - stepx / 2, dummy_data1, d_elevation,
+			    dummy_data2);
+		
+		    /*Print to vegetation ascii file */ 
+		    /*Grid cell count and number of classes in that grid cell (=1) */ 
+		    fprintf(h, "%d 1\n", grid_count);
+		
+		    /*Class number, percentage that this class covers in the
+		     * grid cell(=1.0, full grid cell)
+		     * 3 root zones with depths of 10cm, 10cm and 1.0m
+		     * for those 3 root zone depths, how much root in each (%)
+		     * here we have 0.65, 0.50 and 0.25
+		     * */ 
+		    fprintf(h, "%d 1.0 %s\n", c_landcover, dummy_data3);
+		
+		    /*Load monthly LAI maps data if available */ 
+		    if (input6->answer) {
+		    fprintf(h,
+			     "%5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f\n",
+			     lai[0], lai[1], lai[2], lai[3], lai[4], lai[5],
+			     lai[6], lai[7], lai[8], lai[9], lai[10],
+			     lai[11]);
+		}
+		else {
+		    
+			/*      fprintf(h,"%s\n", dummy_data4); */ 
+		}
+		grid_count = grid_count + 1;
+	    }
+	    
+		/*Print to ascii file */ 
+		/*Grid cell flow direction value in that grid cell */ 
+		if (flag2->answer) {
+		
+		    /* Convert r.watershed flow dir to AGNPS */ 
+		    if (c_fdir == 0 || c_fdir == 8) {
+		    fprintf(ef, "3 ");
+		}
+		else if (c_fdir == 1) {
+		    fprintf(ef, "2 ");
+		}
+		else if (c_fdir == 2) {
+		    fprintf(ef, "1 ");
+		}
+		else if (c_fdir == 3) {
+		    fprintf(ef, "8 ");
+		}
+		else if (c_fdir == 4) {
+		    fprintf(ef, "7 ");
+		}
+		else if (c_fdir == 5) {
+		    fprintf(ef, "6 ");
+		}
+		else if (c_fdir == 6) {
+		    fprintf(ef, "5 ");
+		}
+		else if (c_fdir == 7) {
+		    fprintf(ef, "4 ");
+		}
+		else if (c_fdir == -1) {
+		    fprintf(ef, "0 ");
+		}
+		else {
+		    fprintf(ef, "0 ");
+		}
+	    }
+	    else {
+		if (c_fdir == -1) {
+		    
+			/* Flow direction NODATA_value=0 */ 
+			fprintf(ef, "0 ");
+		}
+		else {
+		    
+			/* Flow direction format is AGNPS */ 
+			fprintf(ef, "%d ", c_fdir);
+		}
+	    }
+	}
+	fprintf(ef, "\n");
+    }
+    G_message(_("Created %d VIC meteorological files"), grid_count);
+    G_message(_("Created %d VIC grid cells soil/vegetation definitions"),
+	       grid_count);
+    G_message(_("Created a routing flow direction input file"));
+    for (i = 0; i < nfiles1; i++) {
+	G_free(inrast_prcp[i]);
+	G_close_cell(infd_prcp[i]);
+    }
+    for (i = 0; i < nfiles2; i++) {
+	G_free(inrast_tmax[i]);
+	G_close_cell(infd_tmax[i]);
+    }
+    for (i = 0; i < nfiles3; i++) {
+	G_free(inrast_tmin[i]);
+	G_close_cell(infd_tmin[i]);
+    }
+    G_free(inrast_elevation);
+    G_close_cell(infd);
+    G_free(inrast_landcover);
+    G_close_cell(infd_landcover);
+    if (input6->answer) {
+	for (i = 0; i < 12; i++) {
+	    G_free(inrast_lai[i]);
+	    G_close_cell(infd_lai[i]);
+	}
+    }
+    G_free(inrast_fdir);
+    G_close_cell(infd_fdir);
+    fclose(h);			/* Vegetation ascii file */
+    fclose(g);			/* Soil ascii file */
+    fclose(ef);		/* Flow Direction ascii file */
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.out.vic/veg_lib.c
===================================================================
--- grass-addons/gipe/r.out.vic/veg_lib.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.out.vic/veg_lib.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,12 +1,15 @@
 #include<stdio.h>
 #include<stdlib.h>
 
-int veglib(char *filename){
+int veglib(char *filename)
+{
 
-	FILE *f;
-	f=fopen(filename,"w");
+    FILE *f;
 
-	fprintf(f,"#Class	OvrStry	Rarc	Rmin	JAN-LAI	FEB-LAI	MAR-LAI	APR-LAI	MAY-LAI	JUN-LAI	JUL-LAI	AUG-LAI	SEP-LAI	OCT-LAI	NOV-LAI	DEC-LAI	JAN-ALB	FEB_ALB	MAR-ALB	APR-ALB	MAY-ALB	JUN-ALB	JUL-ALB	AUG-ALB	SEP-ALB	OCT-ALB	NOV-ALB	DEC-ALB	JAN-ROU	FEB-ROU	MAR-ROU	APR-ROU	MAY-ROU	JUN-ROU	JUL-ROU	AUG-ROU	SEP-ROU	OCT-ROU	NOV-ROU	DEC-ROU	JAN-DIS	FEB-DIS	MAR-DIS	APR-DIS	MAY-DIS	JUN-DIS	JUL-DIS	AUG-DIS	SEP-DIS	OCT-DIS	NOV-DIS	DEC-DIS	WIND_H	RGL	SolAtn	WndAtn	Trunk	COMMENT\n\
+    f = fopen(filename, "w");
+
+    fprintf(f,
+	    "#Class	OvrStry	Rarc	Rmin	JAN-LAI	FEB-LAI	MAR-LAI	APR-LAI	MAY-LAI	JUN-LAI	JUL-LAI	AUG-LAI	SEP-LAI	OCT-LAI	NOV-LAI	DEC-LAI	JAN-ALB	FEB_ALB	MAR-ALB	APR-ALB	MAY-ALB	JUN-ALB	JUL-ALB	AUG-ALB	SEP-ALB	OCT-ALB	NOV-ALB	DEC-ALB	JAN-ROU	FEB-ROU	MAR-ROU	APR-ROU	MAY-ROU	JUN-ROU	JUL-ROU	AUG-ROU	SEP-ROU	OCT-ROU	NOV-ROU	DEC-ROU	JAN-DIS	FEB-DIS	MAR-DIS	APR-DIS	MAY-DIS	JUN-DIS	JUL-DIS	AUG-DIS	SEP-DIS	OCT-DIS	NOV-DIS	DEC-DIS	WIND_H	RGL	SolAtn	WndAtn	Trunk	COMMENT\n\
 1	1	60.0	250.	3.400	3.400	3.500	3.700	4.000	4.400	4.400	4.300	4.200	3.700	3.500	3.400	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	50.0	30	0.5	0.5	0.2	Evergreen Needleleaf\n\
 2	1	60.0	250.	3.400	3.400	3.500	3.700	4.000	4.400	4.400	4.300	4.200	3.700	3.500	3.400	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	0.12	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	1.476	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	8.04	50.0	30	0.5	0.5	0.2	Evergreen Broadleaf\n\
 3	1	60.0	125.	1.680	1.520	1.680	2.900	4.900	5.000	5.000	4.600	3.440	3.040	2.160	2.000	0.18	0.18	0.18	0.18	0.18	0.18	0.18	0.18	0.18	0.18	0.18	0.18	1.230	1.230	1.230	1.230	1.230	1.230	1.230	1.230	1.230	1.230	1.230	1.230	6.70	6.70	6.70	6.70	6.70	6.70	6.70	6.70	6.70	6.70	6.70	6.70	50.0	30	0.5	0.5	0.2	Deciduous Needleleaf\n\
@@ -19,6 +22,6 @@
 10	0	25.0	120.	2.000	2.250	2.950	3.850	3.750	3.500	3.550	3.200	3.300	2.850	2.600	2.200	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.20	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.0738	0.402	0.402	0.402	0.402	0.402	0.402	0.402	0.402	0.402	0.402	0.402	0.402	10	100	0.5	0.5	0.2	Grasslands\n\
 11	0	25.0	120.	0.050	0.020	0.050	0.250	1.500	3.0000	4.500	5.0000	2.5000	0.500	0.050	0.020	0.10	0.10	0.10	0.10	0.20	0.20	0.20	0.20	0.20	0.10	0.10	0.10	0.006	0.006	0.006	0.006	0.012	0.062	0.123	0.185	0.215	0.215	0.006	0.006	0.034	0.034	0.034	0.034	0.067	0.335	0.670	1.005	1.173	1.173	0.034	0.034	10	100	0.5	0.5	0.2	Crop land (corn)\n");
 
-	fclose(f);
-	return;
+    fclose(f);
+    return;
 }

Modified: grass-addons/gipe/r.soiltex2prop/main.c
===================================================================
--- grass-addons/gipe/r.soiltex2prop/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soiltex2prop/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,208 +1,237 @@
-/****************************************************************************
- *
- * MODULE:       r.soiltex2prop
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Transforms percentage of texture (sand/clay)
- *               into USDA 1951 (p209) soil texture classes and then
- *               into USLE soil erodibility factor (K) as an output
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#define POLYGON_DIMENSION 50
-int point_in_triangle(double point_x, double point_y, double point_z, double t1_x, double t1_y, double t1_z, double t2_x, double t2_y, double t2_z, double t3_x, double t3_y, double t3_z);
-double prct2porosity(double sand_input, double clay_input);
-double prct2ksat(double sand_input, double clay_input);
-double prct2hf(double sand_input, double clay_input);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4;
-	struct Option *output1, *output2, *output3;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result1; /*output porosity raster name*/
-	char *result2; /*output KSat raster name*/
-	char *result3; /*output Hf raster name*/
-	/*File Descriptors*/
-	int infd_psand, infd_pclay;
-	int outfd1, outfd2, outfd3;
-	
-	char *psand,*pclay;
-	
-	int i=0,j=0;
-	
-	void *inrast_psand, *inrast_pclay;
-	DCELL *outrast1, *outrast2, *outrast3;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_psand;
-	RASTER_MAP_TYPE data_type_pclay;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("soil, texture, porosity, Ksat, Hf");
-	module->description = _("Texture to soil properties (porosity, Ksat,Hf)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("psand");
-	input1->description=_("Name of the Soil sand fraction map [0.0-1.0]");
-	input1->answer     =_("psand");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("pclay");
-	input2->description=_("Name of the Soil clay fraction map [0.0-1.0]");
-	input2->answer     =_("pclay");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("porosity");
-	output1->description=_("Name of the output porosity layer");
-	output1->answer     =_("porosity");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("ksat");
-	output2->description=_("Name of the output Ksat layer");
-	output2->answer     =_("ksat");
-
-	output3 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output3->key        =_("hf");
-	output3->description=_("Name of the output hf layer");
-	output3->answer     =_("hf");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	psand	 	= input1->answer;
-	pclay	 	= input2->answer;
-	
-	result1  = output1->answer;
-	result2  = output2->answer;
-	result3  = output3->answer;
-	/***************************************************/
-	mapset = G_find_cell2(psand, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), psand);
-	}
-	data_type_psand = G_raster_map_type(psand,mapset);
-	if ( (infd_psand = G_open_cell_old (psand,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), psand);
-	if (G_get_cellhd (psand, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), psand);
-	inrast_psand = G_allocate_raster_buf(data_type_psand);
-	/***************************************************/
-	mapset = G_find_cell2 (pclay, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), pclay);
-	}
-	data_type_pclay = G_raster_map_type(pclay,mapset);
-	if ( (infd_pclay = G_open_cell_old (pclay,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), pclay);
-	if (G_get_cellhd (pclay, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), pclay);
-	inrast_pclay = G_allocate_raster_buf(data_type_pclay);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	outrast2 = G_allocate_raster_buf(data_type_output);
-	outrast3 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new(result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if ( (outfd2 = G_open_raster_new(result2,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result2);
-	if ( (outfd3 = G_open_raster_new(result3,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result3);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_sand;
-		DCELL d_clay;
-		double tex[4] = {0.0,0.0,0.0,0.0};
-		G_percent(row,nrows,2);
-		/* read soil input maps */	
-		if(G_get_raster_row(infd_psand,inrast_psand,row,data_type_psand)<0)
-			G_fatal_error(_("Could not read from <%s>"),psand);
-		if(G_get_raster_row(infd_pclay,inrast_pclay,row,data_type_pclay)<0)
-			G_fatal_error(_("Could not read from <%s>"),pclay);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			d_sand = ((DCELL *) inrast_psand)[col];
-			d_clay = ((DCELL *) inrast_pclay)[col];
-			if(G_is_d_null_value(&d_sand)||
-			G_is_d_null_value(&d_clay)){
-				G_set_d_null_value(&outrast1[col],1);
-				G_set_d_null_value(&outrast2[col],1);
-				G_set_d_null_value(&outrast3[col],1);
-			} else {
-				/************************************/
-				/* convert to porosity		    */
-				d = prct2porosity(d_sand*100.0,d_clay*100.0);
-				outrast1[col] = d;
-				d = prct2ksat(d_sand*100.0,d_clay*100.0);
-				outrast2[col] = d;
-				d = prct2hf(d_sand*100.0, d_clay*100.0);
-				outrast3[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output)<0)
-			G_fatal_error(_("Cannot write output raster file1"));
-		if (G_put_raster_row (outfd2, outrast2, data_type_output)<0)
-			G_fatal_error(_("Cannot write output raster file2"));
-		if (G_put_raster_row (outfd3, outrast3, data_type_output)<0)
-			G_fatal_error(_("Cannot write output raster file3"));
-	}
-
-	G_free (inrast_psand);
-	G_free (inrast_pclay);
-	G_close_cell (infd_psand);
-	G_close_cell (infd_pclay);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	G_free (outrast2);
-	G_close_cell (outfd2);
-	G_free (outrast3);
-	G_close_cell (outfd3);
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-	G_short_history(result2, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result2,&history);
-	G_short_history(result3, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result3,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       r.soiltex2prop
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Transforms percentage of texture (sand/clay)
+ *               into USDA 1951 (p209) soil texture classes and then
+ *               into USLE soil erodibility factor (K) as an output
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+    
+#define POLYGON_DIMENSION 50
+int point_in_triangle(double point_x, double point_y, double point_z,
+		      double t1_x, double t1_y, double t1_z, double t2_x,
+		      double t2_y, double t2_z, double t3_x, double t3_y,
+		      double t3_z);
+double prct2porosity(double sand_input, double clay_input);
+
+double prct2ksat(double sand_input, double clay_input);
+
+double prct2hf(double sand_input, double clay_input);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4;
+
+    struct Option *output1, *output2, *output3;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result1;		/*output porosity raster name */
+
+    char *result2;		/*output KSat raster name */
+
+    char *result3;		/*output Hf raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_psand, infd_pclay;
+
+    int outfd1, outfd2, outfd3;
+
+    char *psand, *pclay;
+
+    int i = 0, j = 0;
+
+    void *inrast_psand, *inrast_pclay;
+
+    DCELL * outrast1, *outrast2, *outrast3;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_psand;
+    RASTER_MAP_TYPE data_type_pclay;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("soil, texture, porosity, Ksat, Hf");
+    module->description =
+	_("Texture to soil properties (porosity, Ksat,Hf)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("psand");
+    input1->description = _("Name of the Soil sand fraction map [0.0-1.0]");
+    input1->answer = _("psand");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("pclay");
+    input2->description = _("Name of the Soil clay fraction map [0.0-1.0]");
+    input2->answer = _("pclay");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("porosity");
+    output1->description = _("Name of the output porosity layer");
+    output1->answer = _("porosity");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("ksat");
+    output2->description = _("Name of the output Ksat layer");
+    output2->answer = _("ksat");
+    output3 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output3->key = _("hf");
+    output3->description = _("Name of the output hf layer");
+    output3->answer = _("hf");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    psand = input1->answer;
+    pclay = input2->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    result3 = output3->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(psand, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), psand);
+    }
+    data_type_psand = G_raster_map_type(psand, mapset);
+    if ((infd_psand = G_open_cell_old(psand, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), psand);
+    if (G_get_cellhd(psand, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), psand);
+    inrast_psand = G_allocate_raster_buf(data_type_psand);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(pclay, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), pclay);
+    }
+    data_type_pclay = G_raster_map_type(pclay, mapset);
+    if ((infd_pclay = G_open_cell_old(pclay, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), pclay);
+    if (G_get_cellhd(pclay, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), pclay);
+    inrast_pclay = G_allocate_raster_buf(data_type_pclay);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    outrast2 = G_allocate_raster_buf(data_type_output);
+    outrast3 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result2);
+    if ((outfd3 = G_open_raster_new(result3, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result3);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_sand;
+	DCELL d_clay;
+	double tex[4] = { 0.0, 0.0, 0.0, 0.0 };
+	G_percent(row, nrows, 2);
+	
+	    /* read soil input maps */ 
+	    if (G_get_raster_row
+		(infd_psand, inrast_psand, row, data_type_psand) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), psand);
+	if (G_get_raster_row(infd_pclay, inrast_pclay, row, data_type_pclay)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), pclay);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    d_sand = ((DCELL *) inrast_psand)[col];
+	    d_clay = ((DCELL *) inrast_pclay)[col];
+	    if (G_is_d_null_value(&d_sand) || G_is_d_null_value(&d_clay)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		G_set_d_null_value(&outrast2[col], 1);
+		G_set_d_null_value(&outrast3[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* convert to porosity              */ 
+		    d = prct2porosity(d_sand * 100.0, d_clay * 100.0);
+		outrast1[col] = d;
+		d = prct2ksat(d_sand * 100.0, d_clay * 100.0);
+		outrast2[col] = d;
+		d = prct2hf(d_sand * 100.0, d_clay * 100.0);
+		outrast3[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write output raster file1"));
+	if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write output raster file2"));
+	if (G_put_raster_row(outfd3, outrast3, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write output raster file3"));
+	}
+    G_free(inrast_psand);
+    G_free(inrast_pclay);
+    G_close_cell(infd_psand);
+    G_close_cell(infd_pclay);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_free(outrast2);
+    G_close_cell(outfd2);
+    G_free(outrast3);
+    G_close_cell(outfd3);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    G_short_history(result2, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result2, &history);
+    G_short_history(result3, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result3, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.soiltex2prop/prct2hf.c
===================================================================
--- grass-addons/gipe/r.soiltex2prop/prct2hf.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soiltex2prop/prct2hf.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,714 +2,914 @@
 
 #define POLYGON_DIMENSION 50
 
-struct vector{
-	double sand;
-	double clay;
-	double silt;
+struct vector
+{
+    double sand;
+    double clay;
+    double silt;
 };
 
 
-/* HF*/
+/* HF */
 
-double prct2hf(double sand_input, double clay_input){
-	int i,index;
-	double temp, hf;
-	double silt_input=0.0; 	/*Rawls et al (1990)*/
-				/*does not have silt input*/
-	/*printf("in prct2hf(), cm\n");*/
-	/*setup the 3Dvectors and initialize them*/
-	struct vector cls[POLYGON_DIMENSION] = {0.0};
-	/*In case silt is not == 0.0, fill up explicitly*/
-	for(i=0;i<POLYGON_DIMENSION;i++){
-		cls[i].sand=0.0;
-		cls[i].clay=0.0;
-		cls[i].silt=0.0;
+double prct2hf(double sand_input, double clay_input)
+{
+    int i, index;
+
+    double temp, hf;
+
+    double silt_input = 0.0;	/*Rawls et al (1990) */
+
+    /*does not have silt input */
+    /*printf("in prct2hf(), cm\n"); */
+    /*setup the 3Dvectors and initialize them */
+    struct vector cls[POLYGON_DIMENSION] = { 0.0 };
+    /*In case silt is not == 0.0, fill up explicitly */
+    for (i = 0; i < POLYGON_DIMENSION; i++) {
+	cls[i].sand = 0.0;
+	cls[i].clay = 0.0;
+	cls[i].silt = 0.0;
+    }
+    /*fill up initial polygon points */
+    cls[0].sand = 0.0;
+    cls[0].clay = 100.0;
+    cls[1].sand = 0.0;
+    cls[1].clay = 54.0;
+    cls[2].sand = 17.0;
+    cls[2].clay = 55.0;
+    /*Get started */
+    index =
+	point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			  cls[0].clay, cls[0].silt, cls[1].sand, cls[1].clay,
+			  cls[1].silt, cls[2].sand, cls[2].clay, cls[2].silt);
+    if (index == 1) {
+	hf = 175.0;
+	index = 1;
+	/*printf("hf=175.0\n"); */
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 100.0;
+	cls[1].sand = 17.0;
+	cls[1].clay = 55.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 60.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=175.0\n"); */
+	    hf = 175.0;
 	}
-	/*fill up initial polygon points*/
-	cls[0].sand=0.0;
-	cls[0].clay=100.0;
-	cls[1].sand=0.0;
-	cls[1].clay=54.0;
-	cls[2].sand=17.0;
-	cls[2].clay=55.0;
-	/*Get started*/
-	index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-	if(index==1){
-		hf=175.0;
-		index=1;
-		/*printf("hf=175.0\n");*/
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 100.0;
+	cls[1].sand = 34.0;
+	cls[1].clay = 66.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 60.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=175.0\n"); */
+	    hf = 175.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=100.0;
-		cls[1].sand=17.0;
-		cls[1].clay=55.0;
-		cls[2].sand=30.0;
-		cls[2].clay=60.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=175.0\n");*/
-			hf=175.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 72.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 65.0;
+	cls[2].clay = 15.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=5.0\n"); */
+	    hf = 5.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=100.0;
-		cls[1].sand=34.0;
-		cls[1].clay=66.0;
-		cls[2].sand=30.0;
-		cls[2].clay=60.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=175.0\n");*/
-			hf=175.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 65.0;
+	cls[2].clay = 15.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=5.0\n"); */
+	    hf = 5.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=72.0;
-		cls[1].clay=0.0;
-		cls[2].sand=65.0;
-		cls[2].clay=15.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=5.0\n");*/
-			hf=5.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 67.0;
+	cls[2].clay = 33.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=5.0\n"); */
+	    hf = 5.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=65.0;
-		cls[1].clay=30.0;
-		cls[2].sand=65.0;
-		cls[2].clay=15.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=5.0\n");*/
-			hf=5.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 58.0;
+	cls[0].clay = 42.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 67.0;
+	cls[2].clay = 33.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=15.0\n"); */
+	    hf = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=65.0;
-		cls[1].clay=30.0;
-		cls[2].sand=67.0;
-		cls[2].clay=33.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=5.0\n");*/
-			hf=5.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 58.0;
+	cls[0].clay = 42.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=15.0\n"); */
+	    hf = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=58.0;
-		cls[0].clay=42.0;
-		cls[1].sand=65.0;
-		cls[1].clay=30.0;
-		cls[2].sand=67.0;
-		cls[2].clay=33.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=15.0\n");*/
-			hf=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 65.0;
+	cls[0].clay = 15.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=15.0\n"); */
+	    hf = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=58.0;
-		cls[0].clay=42.0;
-		cls[1].sand=65.0;
-		cls[1].clay=30.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=15.0\n");*/
-			hf=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 65.0;
+	cls[0].clay = 15.0;
+	cls[1].sand = 72.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=15.0\n"); */
+	    hf = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=65.0;
-		cls[0].clay=15.0;
-		cls[1].sand=65.0;
-		cls[1].clay=30.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=15.0\n");*/
-			hf=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 14.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=25.0\n"); */
+	    hf = 25.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=65.0;
-		cls[0].clay=15.0;
-		cls[1].sand=72.0;
-		cls[1].clay=0.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=15.0\n");*/
-			hf=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 28.0;
+	cls[0].clay = 24.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 14.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=25.0\n"); */
+	    hf = 25.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=0.0;
-		cls[1].sand=20.0;
-		cls[1].clay=14.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=25.0\n");*/
-			hf=25.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 28.0;
+	cls[0].clay = 24.0;
+	cls[1].sand = 58.0;
+	cls[1].clay = 42.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=25.0\n"); */
+	    hf = 25.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=28.0;
-		cls[0].clay=24.0;
-		cls[1].sand=20.0;
-		cls[1].clay=14.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=25.0\n");*/
-			hf=25.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 28.0;
+	cls[0].clay = 24.0;
+	cls[1].sand = 58.0;
+	cls[1].clay = 42.0;
+	cls[2].sand = 55.0;
+	cls[2].clay = 45.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=25.0\n"); */
+	    hf = 25.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=28.0;
-		cls[0].clay=24.0;
-		cls[1].sand=58.0;
-		cls[1].clay=42.0;
-		cls[2].sand=30.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=25.0\n");*/
-			hf=25.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 55.0;
+	cls[0].clay = 45.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 50.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=28.0;
-		cls[0].clay=24.0;
-		cls[1].sand=58.0;
-		cls[1].clay=42.0;
-		cls[2].sand=55.0;
-		cls[2].clay=45.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=25.0\n");*/
-			hf=25.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 55.0;
+	cls[0].clay = 45.0;
+	cls[1].sand = 28.0;
+	cls[1].clay = 24.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=55.0;
-		cls[0].clay=45.0;
-		cls[1].sand=50.0;
-		cls[1].clay=50.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 28.0;
+	cls[1].sand = 28.0;
+	cls[1].clay = 24.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=55.0;
-		cls[0].clay=45.0;
-		cls[1].sand=28.0;
-		cls[1].clay=24.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 28.0;
+	cls[1].sand = 28.0;
+	cls[1].clay = 24.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 14.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=28.0;
-		cls[1].sand=28.0;
-		cls[1].clay=24.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 28.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 14.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 14.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=28.0;
-		cls[1].sand=28.0;
-		cls[1].clay=24.0;
-		cls[2].sand=20.0;
-		cls[2].clay=14.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 14.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 14.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=28.0;
-		cls[1].sand=10.0;
-		cls[1].clay=14.0;
-		cls[2].sand=20.0;
-		cls[2].clay=14.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/*if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 14.0;
+	cls[2].sand = 7.0;
+	cls[2].clay = 3.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=0.0;
-		cls[1].sand=10.0;
-		cls[1].clay=14.0;
-		cls[2].sand=20.0;
-		cls[2].clay=14.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 7.0;
+	cls[2].clay = 3.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/*if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=0.0;
-		cls[1].sand=10.0;
-		cls[1].clay=14.0;
-		cls[2].sand=7.0;
-		cls[2].clay=3.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 3.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 7.0;
+	cls[2].clay = 3.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=0.0;
-		cls[1].sand=0.0;
-		cls[1].clay=0.0;
-		cls[2].sand=7.0;
-		cls[2].clay=3.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 3.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 9.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=35.0\n"); */
+	    hf = 35.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=3.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=0.0;
-		cls[2].sand=7.0;
-		cls[2].clay=3.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 3.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 9.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=3.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=0.0;
-		cls[2].sand=0.0;
-		cls[2].clay=9.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=35.0\n");*/
-			hf=35.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 3.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 7.0;
+	cls[2].clay = 3.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=3.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=28.0;
-		cls[2].sand=0.0;
-		cls[2].clay=9.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 10.0;
+	cls[0].clay = 14.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 7.0;
+	cls[2].clay = 3.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=3.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=28.0;
-		cls[2].sand=7.0;
-		cls[2].clay=3.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 10.0;
+	cls[0].clay = 14.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 10.0;
+	cls[2].clay = 27.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=10.0;
-		cls[0].clay=14.0;
-		cls[1].sand=0.0;
-		cls[1].clay=28.0;
-		cls[2].sand=7.0;
-		cls[2].clay=3.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 10.0;
+	cls[0].clay = 14.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 10.0;
+	cls[2].clay = 27.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=10.0;
-		cls[0].clay=14.0;
-		cls[1].sand=0.0;
-		cls[1].clay=28.0;
-		cls[2].sand=10.0;
-		cls[2].clay=27.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 14.0;
+	cls[0].clay = 30.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 10.0;
+	cls[2].clay = 27.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=10.0;
-		cls[0].clay=14.0;
-		cls[1].sand=20.0;
-		cls[1].clay=28.0;
-		cls[2].sand=10.0;
-		cls[2].clay=27.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 14.0;
+	cls[0].clay = 30.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 16.0;
+	cls[2].clay = 35.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=14.0;
-		cls[0].clay=30.0;
-		cls[1].sand=20.0;
-		cls[1].clay=28.0;
-		cls[2].sand=10.0;
-		cls[2].clay=27.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 23.0;
+	cls[0].clay = 42.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 16.0;
+	cls[2].clay = 35.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=14.0;
-		cls[0].clay=30.0;
-		cls[1].sand=20.0;
-		cls[1].clay=28.0;
-		cls[2].sand=16.0;
-		cls[2].clay=35.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/*if index not found then continue */
+	cls[0].sand = 23.0;
+	cls[0].clay = 42.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 28.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=23.0;
-		cls[0].clay=42.0;
-		cls[1].sand=20.0;
-		cls[1].clay=28.0;
-		cls[2].sand=16.0;
-		cls[2].clay=35.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 23.0;
+	cls[0].clay = 42.0;
+	cls[1].sand = 36.0;
+	cls[1].clay = 46.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/*if index not found then continue*/
-		cls[0].sand=23.0;
-		cls[0].clay=42.0;
-		cls[1].sand=20.0;
-		cls[1].clay=28.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 50.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 36.0;
+	cls[1].clay = 46.0;
+	cls[2].sand = 35.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=23.0;
-		cls[0].clay=42.0;
-		cls[1].sand=36.0;
-		cls[1].clay=46.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 50.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 36.0;
+	cls[1].clay = 46.0;
+	cls[2].sand = 45.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=50.0\n"); */
+	    hf = 50.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=50.0;
-		cls[0].clay=50.0;
-		cls[1].sand=36.0;
-		cls[1].clay=46.0;
-		cls[2].sand=35.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 36.0;
+	cls[1].clay = 46.0;
+	cls[2].sand = 45.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=50.0;
-		cls[0].clay=50.0;
-		cls[1].sand=36.0;
-		cls[1].clay=46.0;
-		cls[2].sand=45.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=50.0\n");*/
-			hf=50.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 42.0;
+	cls[1].clay = 58.0;
+	cls[2].sand = 45.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=55.0;
-		cls[1].sand=36.0;
-		cls[1].clay=46.0;
-		cls[2].sand=45.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 36.0;
+	cls[1].clay = 46.0;
+	cls[2].sand = 23.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=55.0;
-		cls[1].sand=42.0;
-		cls[1].clay=58.0;
-		cls[2].sand=45.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 23.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=55.0;
-		cls[1].sand=36.0;
-		cls[1].clay=46.0;
-		cls[2].sand=23.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 35.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 23.0;
+	cls[2].clay = 42.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=55.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=23.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 16.0;
+	cls[0].clay = 35.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 14.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=35.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=23.0;
-		cls[2].clay=42.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 10.0;
+	cls[0].clay = 27.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 14.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=16.0;
-		cls[0].clay=35.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=14.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 10.0;
+	cls[0].clay = 27.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 28.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=80.0\n"); */
+	    hf = 80.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=10.0;
-		cls[0].clay=27.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=14.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 17.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 54.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=125.0\n"); */
+	    hf = 125.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=10.0;
-		cls[0].clay=27.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=0.0;
-		cls[2].clay=28.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=80.0\n");*/
-			hf=80.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 17.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 44.0;
+	cls[2].sand = 38.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=125.0\n"); */
+	    hf = 125.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=17.0;
-		cls[0].clay=55.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=0.0;
-		cls[2].clay=54.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=125.0\n");*/
-			hf=125.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 17.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 60.0;
+	cls[2].sand = 38.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=125.0\n"); */
+	    hf = 125.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=17.0;
-		cls[0].clay=55.0;
-		cls[1].sand=0.0;
-		cls[1].clay=44.0;
-		cls[2].sand=38.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=125.0\n");*/
-			hf=125.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 42.0;
+	cls[0].clay = 58.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 60.0;
+	cls[2].sand = 38.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=125.0\n"); */
+	    hf = 125.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=17.0;
-		cls[0].clay=55.0;
-		cls[1].sand=30.0;
-		cls[1].clay=60.0;
-		cls[2].sand=38.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=125.0\n");*/
-			hf=125.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 42.0;
+	cls[0].clay = 58.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 60.0;
+	cls[2].sand = 34.0;
+	cls[2].clay = 66.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("hf=125.0\n"); */
+	    hf = 125.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=42.0;
-		cls[0].clay=58.0;
-		cls[1].sand=30.0;
-		cls[1].clay=60.0;
-		cls[2].sand=38.0;
-		cls[2].clay=55.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=125.0\n");*/
-			hf=125.0;
-		}
-	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=42.0;
-		cls[0].clay=58.0;
-		cls[1].sand=30.0;
-		cls[1].clay=60.0;
-		cls[2].sand=34.0;
-		cls[2].clay=66.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("hf=125.0\n");*/
-			hf=125.0;
-		}
-	}
-	return hf;
+    }
+    return hf;
 }

Modified: grass-addons/gipe/r.soiltex2prop/prct2ksat.c
===================================================================
--- grass-addons/gipe/r.soiltex2prop/prct2ksat.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soiltex2prop/prct2ksat.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,1036 +2,1328 @@
 
 #define POLYGON_DIMENSION 50
 
-struct vector{
-	double sand;
-	double clay;
-	double silt;
+struct vector
+{
+    double sand;
+    double clay;
+    double silt;
 };
 
 
-/* KSAT*/
+/* KSAT */
 
-double prct2ksat(double sand_input, double clay_input){
-	int i,index;
-	double temp,ksat;
-	double silt_input=0.0; 	/*Rawls et al (1990)*/
-				/*do not have silt input*/
-	/*printf("in prct2ksat(), cm/h\n");*/
-	/*setup the 3Dvectors and initialize them*/
-	struct vector cls[POLYGON_DIMENSION] = {0.0};
-	/*In case silt is not == 0.0, fill up explicitly*/
-	for(i=0;i<POLYGON_DIMENSION;i++){
-		cls[i].sand=0.0;
-		cls[i].clay=0.0;
-		cls[i].silt=0.0;
+double prct2ksat(double sand_input, double clay_input)
+{
+    int i, index;
+
+    double temp, ksat;
+
+    double silt_input = 0.0;	/*Rawls et al (1990) */
+
+    /*do not have silt input */
+    /*printf("in prct2ksat(), cm/h\n"); */
+    /*setup the 3Dvectors and initialize them */
+    struct vector cls[POLYGON_DIMENSION] = { 0.0 };
+    /*In case silt is not == 0.0, fill up explicitly */
+    for (i = 0; i < POLYGON_DIMENSION; i++) {
+	cls[i].sand = 0.0;
+	cls[i].clay = 0.0;
+	cls[i].silt = 0.0;
+    }
+    /*fill up initial polygon points */
+    cls[0].sand = 0.0;
+    cls[0].clay = 100.0;
+    cls[1].sand = 0.0;
+    cls[1].clay = 60.0;
+    cls[2].sand = 7.0;
+    cls[2].clay = 56.0;
+    /*Get started */
+    index =
+	point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			  cls[0].clay, cls[0].silt, cls[1].sand, cls[1].clay,
+			  cls[1].silt, cls[2].sand, cls[2].clay, cls[2].silt);
+    if (index == 1) {
+	ksat = 0.0025;
+	index = 1;
+	/*printf("Ksat=0.0025\n"); */
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 100.0;
+	cls[1].sand = 7.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 30.0;
+	cls[2].clay = 56.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0025\n"); */
+	    ksat = 0.0025;
 	}
-	/*fill up initial polygon points*/
-	cls[0].sand=0.0;
-	cls[0].clay=100.0;
-	cls[1].sand=0.0;
-	cls[1].clay=60.0;
-	cls[2].sand=7.0;
-	cls[2].clay=56.0;
-	/*Get started*/
-	index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-	if(index==1){
-		ksat=0.0025;
-		index=1;
-		/*printf("Ksat=0.0025\n");*/
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 100.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 40.0;
+	cls[2].clay = 63.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0025\n"); */
+	    ksat = 0.0025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=100.0;
-		cls[1].sand=7.0;
-		cls[1].clay=56.0;
-		cls[2].sand=30.0;
-		cls[2].clay=56.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0025\n");*/
-			ksat=0.0025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 85.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 90.0;
+	cls[2].clay = 10.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=25.0\n"); */
+	    ksat = 25.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=100.0;
-		cls[1].sand=30.0;
-		cls[1].clay=56.0;
-		cls[2].sand=40.0;
-		cls[2].clay=63.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0025\n");*/
-			ksat=0.0025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 85.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 90.0;
+	cls[1].clay = 10.0;
+	cls[2].sand = 80.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=15.0\n"); */
+	    ksat = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=85.0;
-		cls[1].clay=0.0;
-		cls[2].sand=90.0;
-		cls[2].clay=10.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=25.0\n");*/
-			ksat=25.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 80.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 90.0;
+	cls[1].clay = 10.0;
+	cls[2].sand = 85.0;
+	cls[2].clay = 15.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=15.0\n"); */
+	    ksat = 15.0;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=85.0;
-		cls[0].clay=0.0;
-		cls[1].sand=90.0;
-		cls[1].clay=10.0;
-		cls[2].sand=80.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=15.0\n");*/
-			ksat=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 85.0;
+	cls[0].clay = 15.0;
+	cls[1].sand = 80.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 70.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=7.5\n"); */
+	    ksat = 7.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=80.0;
-		cls[0].clay=0.0;
-		cls[1].sand=90.0;
-		cls[1].clay=10.0;
-		cls[2].sand=85.0;
-		cls[2].clay=15.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=15.0\n");*/
-			ksat=15.0;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 70.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 85.0;
+	cls[1].clay = 15.0;
+	cls[2].sand = 75.0;
+	cls[2].clay = 25.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=7.5\n"); */
+	    ksat = 7.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=85.0;
-		cls[0].clay=15.0;
-		cls[1].sand=80.0;
-		cls[1].clay=0.0;
-		cls[2].sand=70.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=7.5\n");*/
-			ksat=7.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 70.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 75.0;
+	cls[1].clay = 25.0;
+	cls[2].sand = 68.0;
+	cls[2].clay = 23.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=2.5\n"); */
+	    ksat = 2.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=70.0;
-		cls[0].clay=0.0;
-		cls[1].sand=85.0;
-		cls[1].clay=15.0;
-		cls[2].sand=75.0;
-		cls[2].clay=25.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=7.5\n");*/
-			ksat=7.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 75.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 68.0;
+	cls[1].clay = 23.0;
+	cls[2].sand = 70.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=2.5\n"); */
+	    ksat = 2.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=70.0;
-		cls[0].clay=0.0;
-		cls[1].sand=75.0;
-		cls[1].clay=25.0;
-		cls[2].sand=68.0;
-		cls[2].clay=23.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=2.5\n");*/
-			ksat=2.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 70.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 35.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 68.0;
+	cls[2].clay = 23.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=2.5\n"); */
+	    ksat = 2.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=75.0;
-		cls[0].clay=25.0;
-		cls[1].sand=68.0;
-		cls[1].clay=23.0;
-		cls[2].sand=70.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=2.5\n");*/
-			ksat=2.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 17.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 35.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 22.0;
+	cls[2].clay = 5.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=1.5\n"); */
+	    ksat = 1.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=70.0;
-		cls[0].clay=0.0;
-		cls[1].sand=35.0;
-		cls[1].clay=0.0;
-		cls[2].sand=68.0;
-		cls[2].clay=23.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=2.5\n");*/
-			ksat=2.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 5.0;
+	cls[1].sand = 35.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 68.0;
+	cls[2].clay = 23.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=1.5\n"); */
+	    ksat = 1.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=17.0;
-		cls[0].clay=0.0;
-		cls[1].sand=35.0;
-		cls[1].clay=0.0;
-		cls[2].sand=22.0;
-		cls[2].clay=5.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=1.5\n");*/
-			ksat=1.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 5.0;
+	cls[1].sand = 68.0;
+	cls[1].clay = 23.0;
+	cls[2].sand = 60.0;
+	cls[2].clay = 25.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=1.5\n"); */
+	    ksat = 1.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=5.0;
-		cls[1].sand=35.0;
-		cls[1].clay=0.0;
-		cls[2].sand=68.0;
-		cls[2].clay=23.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=1.5\n");*/
-			ksat=1.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 68.0;
+	cls[1].clay = 23.0;
+	cls[2].sand = 70.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=1.5\n"); */
+	    ksat = 1.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=5.0;
-		cls[1].sand=68.0;
-		cls[1].clay=23.0;
-		cls[2].sand=60.0;
-		cls[2].clay=25.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=1.5\n");*/
-			ksat=1.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 70.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=1.5\n"); */
+	    ksat = 1.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=25.0;
-		cls[1].sand=68.0;
-		cls[1].clay=23.0;
-		cls[2].sand=70.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=1.5\n");*/
-			ksat=1.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 17.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 22.0;
+	cls[2].clay = 5.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=25.0;
-		cls[1].sand=65.0;
-		cls[1].clay=35.0;
-		cls[2].sand=70.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=1.5\n");*/
-			ksat=1.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 12.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 22.0;
+	cls[2].clay = 5.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=17.0;
-		cls[0].clay=0.0;
-		cls[1].sand=10.0;
-		cls[1].clay=0.0;
-		cls[2].sand=22.0;
-		cls[2].clay=5.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 12.0;
+	cls[1].sand = 42.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 22.0;
+	cls[2].clay = 5.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=12.0;
-		cls[1].sand=10.0;
-		cls[1].clay=0.0;
-		cls[2].sand=22.0;
-		cls[2].clay=5.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 42.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 22.0;
+	cls[2].clay = 5.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=12.0;
-		cls[1].sand=42.0;
-		cls[1].clay=20.0;
-		cls[2].sand=22.0;
-		cls[2].clay=5.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 42.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=25.0;
-		cls[1].sand=42.0;
-		cls[1].clay=20.0;
-		cls[2].sand=22.0;
-		cls[2].clay=5.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 25.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=25.0;
-		cls[1].sand=42.0;
-		cls[1].clay=20.0;
-		cls[2].sand=57.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 63.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 65.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 30.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.8\n"); */
+	    ksat = 0.8;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=25.0;
-		cls[1].sand=65.0;
-		cls[1].clay=35.0;
-		cls[2].sand=57.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 55.0;
+	cls[0].clay = 35.0;
+	cls[1].sand = 60.0;
+	cls[1].clay = 40.0;
+	cls[2].sand = 63.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=63.0;
-		cls[0].clay=38.0;
-		cls[1].sand=65.0;
-		cls[1].clay=35.0;
-		cls[2].sand=57.0;
-		cls[2].clay=30.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.8\n");*/
-			ksat=0.8;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 55.0;
+	cls[0].clay = 35.0;
+	cls[1].sand = 57.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 63.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=55.0;
-		cls[0].clay=35.0;
-		cls[1].sand=60.0;
-		cls[1].clay=40.0;
-		cls[2].sand=63.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 55.0;
+	cls[0].clay = 35.0;
+	cls[1].sand = 57.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 38.0;
+	cls[2].clay = 23.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=55.0;
-		cls[0].clay=35.0;
-		cls[1].sand=57.0;
-		cls[1].clay=30.0;
-		cls[2].sand=63.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 42.0;
+	cls[0].clay = 20.0;
+	cls[1].sand = 57.0;
+	cls[1].clay = 30.0;
+	cls[2].sand = 38.0;
+	cls[2].clay = 23.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=55.0;
-		cls[0].clay=35.0;
-		cls[1].sand=57.0;
-		cls[1].clay=30.0;
-		cls[2].sand=38.0;
-		cls[2].clay=23.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 42.0;
+	cls[0].clay = 20.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 12.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=42.0;
-		cls[0].clay=20.0;
-		cls[1].sand=57.0;
-		cls[1].clay=30.0;
-		cls[2].sand=38.0;
-		cls[2].clay=23.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 12.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=42.0;
-		cls[0].clay=20.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=20.0;
-		cls[2].clay=12.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 12.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=20.0;
-		cls[2].clay=12.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 10.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=10.0;
-		cls[1].clay=0.0;
-		cls[2].sand=20.0;
-		cls[2].clay=12.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 3.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.5\n"); */
+	    ksat = 0.5;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=10.0;
-		cls[1].clay=0.0;
-		cls[2].sand=0.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 3.0;
+	cls[2].sand = 9.0;
+	cls[2].clay = 18.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=3.0;
-		cls[2].sand=0.0;
-		cls[2].clay=0.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.5\n");*/
-			ksat=0.5;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 16.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 3.0;
+	cls[2].sand = 9.0;
+	cls[2].clay = 18.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=0.0;
-		cls[1].clay=3.0;
-		cls[2].sand=9.0;
-		cls[2].clay=18.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 7.0;
+	cls[0].clay = 3.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 9.0;
+	cls[2].clay = 18.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=16.0;
-		cls[1].sand=0.0;
-		cls[1].clay=3.0;
-		cls[2].sand=9.0;
-		cls[2].clay=18.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 9.0;
+	cls[2].clay = 18.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=7.0;
-		cls[0].clay=3.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=9.0;
-		cls[2].clay=18.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=29.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=9.0;
-		cls[2].clay=18.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 23.0;
+	cls[1].sand = 23.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=29.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 23.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=23.0;
-		cls[1].sand=23.0;
-		cls[1].clay=20.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 38.0;
+	cls[0].clay = 23.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 55.0;
+	cls[2].clay = 35.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=23.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 40.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 55.0;
+	cls[2].clay = 35.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=38.0;
-		cls[0].clay=23.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=55.0;
-		cls[2].clay=35.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 60.0;
+	cls[0].clay = 40.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 44.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.3\n"); */
+	    ksat = 0.3;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=40.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=55.0;
-		cls[2].clay=35.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 44.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=60.0;
-		cls[0].clay=40.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=57.0;
-		cls[2].clay=44.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.3\n");*/
-			ksat=0.3;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 55.0;
+	cls[1].clay = 45.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 44.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=57.0;
-		cls[2].clay=44.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 35.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=55.0;
-		cls[1].clay=45.0;
-		cls[2].sand=57.0;
-		cls[2].clay=44.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=50.0;
-		cls[1].clay=35.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 33.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 13.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=29.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=33.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 22.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 9.0;
+	cls[1].clay = 18.0;
+	cls[2].sand = 13.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=29.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=13.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 16.0;
+	cls[1].sand = 9.0;
+	cls[1].clay = 18.0;
+	cls[2].sand = 13.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=22.0;
-		cls[0].clay=29.0;
-		cls[1].sand=9.0;
-		cls[1].clay=18.0;
-		cls[2].sand=13.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 16.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 25.0;
+	cls[2].sand = 13.0;
+	cls[2].clay = 29.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.15\n"); */
+	    ksat = 0.15;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=16.0;
-		cls[1].sand=9.0;
-		cls[1].clay=18.0;
-		cls[2].sand=13.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 0.0;
+	cls[0].clay = 33.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 25.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=16.0;
-		cls[1].sand=0.0;
-		cls[1].clay=25.0;
-		cls[2].sand=13.0;
-		cls[2].clay=29.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.15\n");*/
-			ksat=0.15;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 25.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=0.0;
-		cls[0].clay=33.0;
-		cls[1].sand=0.0;
-		cls[1].clay=25.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 29.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=29.0;
-		cls[1].sand=0.0;
-		cls[1].clay=25.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 41.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=29.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 20.0;
+	cls[0].clay = 41.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=41.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 38.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=20.0;
-		cls[0].clay=41.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 38.0;
+	cls[1].sand = 55.0;
+	cls[1].clay = 45.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.075\n"); */
+	    ksat = 0.075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=30.0;
-		cls[1].clay=38.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 41.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=38.0;
-		cls[1].sand=55.0;
-		cls[1].clay=45.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.075\n");*/
-			ksat=0.075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 18.0;
+	cls[1].clay = 53.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=50.0;
-		cls[1].sand=20.0;
-		cls[1].clay=41.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 35.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 18.0;
+	cls[1].clay = 53.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=50.0;
-		cls[1].sand=18.0;
-		cls[1].clay=53.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 35.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 43.0;
+	cls[1].clay = 58.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 50.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=35.0;
-		cls[0].clay=55.0;
-		cls[1].sand=18.0;
-		cls[1].clay=53.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 41.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=35.0;
-		cls[0].clay=55.0;
-		cls[1].sand=43.0;
-		cls[1].clay=58.0;
-		cls[2].sand=50.0;
-		cls[2].clay=50.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 33.0;
+	cls[2].sand = 15.0;
+	cls[2].clay = 38.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=50.0;
-		cls[1].sand=20.0;
-		cls[1].clay=41.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 13.0;
+	cls[0].clay = 50.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 33.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 51.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.025\n"); */
+	    ksat = 0.025;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=50.0;
-		cls[1].sand=0.0;
-		cls[1].clay=33.0;
-		cls[2].sand=15.0;
-		cls[2].clay=38.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 8.0;
+	cls[0].clay = 56.0;
+	cls[1].sand = 0.0;
+	cls[1].clay = 60.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 51.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=13.0;
-		cls[0].clay=50.0;
-		cls[1].sand=0.0;
-		cls[1].clay=33.0;
-		cls[2].sand=0.0;
-		cls[2].clay=51.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.025\n");*/
-			ksat=0.025;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 8.0;
+	cls[0].clay = 56.0;
+	cls[1].sand = 13.0;
+	cls[1].clay = 50.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 51.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=8.0;
-		cls[0].clay=56.0;
-		cls[1].sand=0.0;
-		cls[1].clay=60.0;
-		cls[2].sand=0.0;
-		cls[2].clay=51.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 8.0;
+	cls[0].clay = 56.0;
+	cls[1].sand = 13.0;
+	cls[1].clay = 50.0;
+	cls[2].sand = 18.0;
+	cls[2].clay = 53.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=8.0;
-		cls[0].clay=56.0;
-		cls[1].sand=13.0;
-		cls[1].clay=50.0;
-		cls[2].sand=0.0;
-		cls[2].clay=51.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 8.0;
+	cls[0].clay = 56.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 18.0;
+	cls[2].clay = 53.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=8.0;
-		cls[0].clay=56.0;
-		cls[1].sand=13.0;
-		cls[1].clay=50.0;
-		cls[2].sand=18.0;
-		cls[2].clay=53.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 35.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 18.0;
+	cls[2].clay = 53.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=8.0;
-		cls[0].clay=56.0;
-		cls[1].sand=30.0;
-		cls[1].clay=56.0;
-		cls[2].sand=18.0;
-		cls[2].clay=53.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 35.0;
+	cls[0].clay = 55.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 43.0;
+	cls[2].clay = 58.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=35.0;
-		cls[0].clay=55.0;
-		cls[1].sand=30.0;
-		cls[1].clay=56.0;
-		cls[2].sand=18.0;
-		cls[2].clay=53.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
+    }
+    if (index == 0) {		/* if index not found then continue */
+	cls[0].sand = 40.0;
+	cls[0].clay = 63.0;
+	cls[1].sand = 30.0;
+	cls[1].clay = 56.0;
+	cls[2].sand = 43.0;
+	cls[2].clay = 58.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    index = 1;
+	    /*printf("Ksat=0.0075\n"); */
+	    ksat = 0.0075;
 	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=35.0;
-		cls[0].clay=55.0;
-		cls[1].sand=30.0;
-		cls[1].clay=56.0;
-		cls[2].sand=43.0;
-		cls[2].clay=58.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
-	}
-	if (index==0){/* if index not found then continue*/
-		cls[0].sand=40.0;
-		cls[0].clay=63.0;
-		cls[1].sand=30.0;
-		cls[1].clay=56.0;
-		cls[2].sand=43.0;
-		cls[2].clay=58.0;
-		index=point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			index=1;
-			/*printf("Ksat=0.0075\n");*/
-			ksat=0.0075;
-		}
-	}
-	return ksat;
+    }
+    return ksat;
 }

Modified: grass-addons/gipe/r.soiltex2prop/prct2porosity.c
===================================================================
--- grass-addons/gipe/r.soiltex2prop/prct2porosity.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soiltex2prop/prct2porosity.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,241 +2,303 @@
 
 #define POLYGON_DIMENSION 50
 
-struct vector{
-	double sand;
-	double clay;
-	double silt;
+struct vector
+{
+    double sand;
+    double clay;
+    double silt;
 };
 
-double prct2porosity(double sand_input, double clay_input){
-	int i,index;
-	double temp,porosity;
-	double silt_input=0.0; 	//Rawls et al (1990)
-				//do not have silt input
-	//printf("in prct2poros(), Volume Fraction\n");
-	//setup the 3Dvectors and initialize them
-	struct vector cls[POLYGON_DIMENSION] = {0.0};
-	//In case silt is not == 0.0, fill up explicitly
-	for(i=0;i<POLYGON_DIMENSION;i++){
-		cls[i].sand=0.0;
-		cls[i].clay=0.0;
-		cls[i].silt=0.0;
+double prct2porosity(double sand_input, double clay_input)
+{
+    int i, index;
+
+    double temp, porosity;
+
+    double silt_input = 0.0;	//Rawls et al (1990)
+
+    //do not have silt input
+    //printf("in prct2poros(), Volume Fraction\n");
+    //setup the 3Dvectors and initialize them
+    struct vector cls[POLYGON_DIMENSION] = { 0.0 };
+    //In case silt is not == 0.0, fill up explicitly
+    for (i = 0; i < POLYGON_DIMENSION; i++) {
+	cls[i].sand = 0.0;
+	cls[i].clay = 0.0;
+	cls[i].silt = 0.0;
+    }
+    //printf("0=>sand:%.2f\tclay:%.2f\n", sand_input,clay_input);
+    cls[0].sand = 0.0;
+    cls[0].clay = 100.0;
+    cls[1].sand = 10.0;
+    cls[1].clay = 90.0;
+    cls[2].sand = 25.0;
+    cls[2].clay = 75.0;
+    //printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
+    //printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
+    //printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
+    //Get started
+    index =
+	point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			  cls[0].clay, cls[0].silt, cls[1].sand, cls[1].clay,
+			  cls[1].silt, cls[2].sand, cls[2].clay, cls[2].silt);
+    if (index == 1) {
+	porosity = 0.575;
+	//printf("Poros=0.575\n");
+    }
+    if (index == 0) {		// if index not found then continue
+	//printf("1=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
+	cls[0].sand = 10.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 20.0;
+	cls[1].clay = 20.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 0.0;
+	//printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
+	//printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
+	//printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.575\n");
+	    porosity = 0.575;
 	}
-	//printf("0=>sand:%.2f\tclay:%.2f\n", sand_input,clay_input);
-	cls[0].sand=0.0;
-	cls[0].clay=100.0;
-	cls[1].sand=10.0;
-	cls[1].clay=90.0;
-	cls[2].sand=25.0;
-	cls[2].clay=75.0;
+    }
+    if (index == 0) {		// if index not found then continue
+	//printf("2=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 50.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 43.0;
 	//printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
 	//printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
 	//printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
-	//Get started
-	index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-	if(index==1){
-		porosity=0.575;
-		//printf("Poros=0.575\n");
-	} 
-	if (index==0){// if index not found then continue
-		//printf("1=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
-		cls[0].sand=10.0;
-		cls[0].clay=0.0;
-		cls[1].sand=20.0;
-		cls[1].clay=20.0;
-		cls[2].sand=50.0;
-		cls[2].clay=0.0;
-		//printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
-		//printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
-		//printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.575\n");
-			porosity=0.575;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		//printf("2=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=50.0;
-		cls[1].clay=50.0;
-		cls[2].sand=50.0;
-		cls[2].clay=43.0;
-		//printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
-		//printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
-		//printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.425\n");
-			porosity=0.425;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		//printf("3=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=50.0;
-		cls[1].clay=43.0;
-		cls[2].sand=52.0;
-		cls[2].clay=33.0;
-		////printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
-		////printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
-		////printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.425\n");
-			porosity=0.425;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=52.0;
-		cls[1].clay=33.0;
-		cls[2].sand=57.0;
-		cls[2].clay=25.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.425\n");
-			porosity=0.425;
-		}
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.425\n");
+	    porosity = 0.425;
 	}
-	if (index==0){// if index not found then continue
-		cls[0].sand=100.0;
-		cls[0].clay=0.0;
-		cls[1].sand=57.0;
-		cls[1].clay=25.0;
-		cls[2].sand=87.0;
-		cls[2].clay=0.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.425\n");
-			porosity=0.425;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=0.0;
-		cls[0].clay=0.0;
-		cls[1].sand=25.0;
-		cls[1].clay=75.0;
-		cls[2].sand=0.0;
-		cls[2].clay=90.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index == 1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=0.0;
-		cls[0].clay=0.0;
-		cls[1].sand=25.0;
-		cls[1].clay=75.0;
-		cls[2].sand=10.0;
-		cls[2].clay=0.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=10.0;
-		cls[0].clay=0.0;
-		cls[1].sand=25.0;
-		cls[1].clay=75.0;
-		cls[2].sand=20.0;
-		cls[2].clay=20.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
+    }
+    if (index == 0) {		// if index not found then continue
+	//printf("3=>sand:%.2f\tclay:%.2f\n",sand_input,clay_input);
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 50.0;
+	cls[1].clay = 43.0;
+	cls[2].sand = 52.0;
+	cls[2].clay = 33.0;
+	////printf("sand0:%.2f\tclay0:%.2f\tsilt0:%.2f\n",cls[0].sand,cls[0].clay,cls[0].silt);
+	////printf("sand1:%.2f\tclay1:%.2f\tsilt1:%.2f\n",cls[1].sand,cls[1].clay,cls[1].silt);
+	////printf("sand2:%.2f\tclay2:%.2f\tsilt2:%.2f\n",cls[2].sand,cls[2].clay,cls[2].silt);
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.425\n");
+	    porosity = 0.425;
 	}
-	if (index==0){// if index not found then continue
-		cls[0].sand=20.0;
-		cls[0].clay=20.0;
-		cls[1].sand=25.0;
-		cls[1].clay=75.0;
-		cls[2].sand=25.0;
-		cls[2].clay=55.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 52.0;
+	cls[1].clay = 33.0;
+	cls[2].sand = 57.0;
+	cls[2].clay = 25.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.425\n");
+	    porosity = 0.425;
 	}
-	if (index==0){// if index not found then continue
-		cls[0].sand=20.0;
-		cls[0].clay=20.0;
-		cls[1].sand=25.0;
-		cls[1].clay=55.0;
-		cls[2].sand=27.0;
-		cls[2].clay=45.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=20.0;
-		cls[0].clay=20.0;
-		cls[1].sand=27.0;
-		cls[1].clay=45.0;
-		cls[2].sand=50.0;
-		cls[2].clay=0.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
- 	if (index==0){// if index not found then continue
-		cls[0].sand=50.0;
-		cls[0].clay=0.0;
-		cls[1].sand=70.0;
-		cls[1].clay=0.0;
-		cls[2].sand=37.0;
-		cls[2].clay=25.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=25.0;
-		cls[0].clay=75.0;
-		cls[1].sand=25.0;
-		cls[1].clay=55.0;
-		cls[2].sand=28.0;
-		cls[2].clay=61.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		cls[0].sand=25.0;
-		cls[0].clay=75.0;
-		cls[1].sand=37.0;
-		cls[1].clay=65.0;
-		cls[2].sand=28.0;
-		cls[2].clay=61.0;
-		index = point_in_triangle(sand_input,clay_input,silt_input,cls[0].sand,cls[0].clay,cls[0].silt,cls[1].sand,cls[1].clay,cls[1].silt,cls[2].sand,cls[2].clay,cls[2].silt);
-		if(index==1){
-			//printf("Poros=0.525\n");
-			porosity=0.525;
-		}
-	} 
-	if (index==0){// if index not found then continue
-		//printf("Poros=0.475 (final choice...)\n");
-		porosity=0.475;
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 100.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 57.0;
+	cls[1].clay = 25.0;
+	cls[2].sand = 87.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.425\n");
+	    porosity = 0.425;
 	}
-	return porosity;
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 0.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 75.0;
+	cls[2].sand = 0.0;
+	cls[2].clay = 90.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 0.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 75.0;
+	cls[2].sand = 10.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 10.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 75.0;
+	cls[2].sand = 20.0;
+	cls[2].clay = 20.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 20.0;
+	cls[0].clay = 20.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 75.0;
+	cls[2].sand = 25.0;
+	cls[2].clay = 55.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 20.0;
+	cls[0].clay = 20.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 55.0;
+	cls[2].sand = 27.0;
+	cls[2].clay = 45.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 20.0;
+	cls[0].clay = 20.0;
+	cls[1].sand = 27.0;
+	cls[1].clay = 45.0;
+	cls[2].sand = 50.0;
+	cls[2].clay = 0.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 50.0;
+	cls[0].clay = 0.0;
+	cls[1].sand = 70.0;
+	cls[1].clay = 0.0;
+	cls[2].sand = 37.0;
+	cls[2].clay = 25.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 25.0;
+	cls[0].clay = 75.0;
+	cls[1].sand = 25.0;
+	cls[1].clay = 55.0;
+	cls[2].sand = 28.0;
+	cls[2].clay = 61.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	cls[0].sand = 25.0;
+	cls[0].clay = 75.0;
+	cls[1].sand = 37.0;
+	cls[1].clay = 65.0;
+	cls[2].sand = 28.0;
+	cls[2].clay = 61.0;
+	index =
+	    point_in_triangle(sand_input, clay_input, silt_input, cls[0].sand,
+			      cls[0].clay, cls[0].silt, cls[1].sand,
+			      cls[1].clay, cls[1].silt, cls[2].sand,
+			      cls[2].clay, cls[2].silt);
+	if (index == 1) {
+	    //printf("Poros=0.525\n");
+	    porosity = 0.525;
+	}
+    }
+    if (index == 0) {		// if index not found then continue
+	//printf("Poros=0.475 (final choice...)\n");
+	porosity = 0.475;
+    }
+    return porosity;
 }
-
-

Modified: grass-addons/gipe/r.soiltex2prop/vector_multiplication.c
===================================================================
--- grass-addons/gipe/r.soiltex2prop/vector_multiplication.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soiltex2prop/vector_multiplication.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -2,91 +2,112 @@
 
 #define POLYGON_DIMENSION 50
 
-struct vector{
-	double sand;
-	double clay;
-	double silt;
+struct vector
+{
+    double sand;
+    double clay;
+    double silt;
 };
 
-int point_in_triangle(double point_x, double point_y, double point_z, double t1_x, double t1_y, double t1_z, double t2_x, double t2_y, double t2_z, double t3_x, double t3_y, double t3_z){
-	/*printf("in function: sand=%5.3f clay=%5.3f silt=%5.3f\n",point_x,point_y,point_z);*/
-	int index=0;
-	double answer;
-	double answer1_x, answer1_y, answer1_z;
-	double answer2_x, answer2_y, answer2_z;
-	double answer3_x, answer3_y, answer3_z;
-	/* Consider three points forming a trinagle from a given soil class boundary ABC */
-	/* Consider F an additional point in space */
-	double af1, af2, af3; /*Points for vector AF*/
-	double bf1, bf2, bf3; /*Points for vector BF*/
-	double cf1, cf2, cf3; /*Points for vector CF*/
-	double ab1, ab2, ab3; /*Points for vector AB*/ 
-	double bc1, bc2, bc3; /*Points for vector BC*/
-	double ca1, ca2, ca3; /*Points for vector CA*/
-	/* Create vectors AB, BC and CA*/
-	ab1=(t2_x-t1_x);
-	ab2=(t2_y-t1_y);
-	ab3=(t2_z-t1_z);
-	bc1=(t3_x-t2_x);
-	bc2=(t3_y-t2_y);
-	bc3=(t3_z-t2_z);
-	ca1=(t1_x-t3_x);
-	ca2=(t1_y-t3_y);
-	ca3=(t1_z-t3_z);
-	/* Create vectors AF, BF and CF*/
-	af1=(point_x-t1_x);
-	af2=(point_y-t1_y);
-	af3=(point_z-t1_z);
-	bf1=(point_x-t2_x);
-	bf2=(point_y-t2_y);
-	bf3=(point_z-t2_z);
-	cf1=(point_x-t3_x);
-	cf2=(point_y-t3_y);
-	cf3=(point_z-t3_z);
-	/* Calculate the following CrossProducts:*/
-	/* AFxAB*/
-	answer1_x=(af2*ab3)-(af3*ab2);
-	answer1_y=(af3*ab1)-(af1*ab3);
-	answer1_z=(af1*ab2)-(af2*ab1);
-/*	printf("answer(AFxAB)= %f %f %f\n",answer1_x, answer1_y, answer1_z);*/
-	/*BFxBC*/
-	answer2_x=(bf2*bc3)-(bf3*bc2);
-	answer2_y=(bf3*bc1)-(bf1*bc3);
-	answer2_z=(bf1*bc2)-(bf2*bc1);
-/*	printf("answer(BFxBC)= %f %f %f\n",answer2_x, answer2_y, answer2_z);*/
-	/*CFxCA*/
-	answer3_x=(cf2*ca3)-(cf3*ca2);
-	answer3_y=(cf3*ca1)-(cf1*ca3);
-	answer3_z=(cf1*ca2)-(cf2*ca1);
-/*	printf("answer(CFxCA)= %f %f %f\n",answer3_x, answer3_y, answer3_z);*/
-	answer=0.0;/*initialize value*/
-	if(answer1_x>0&&answer2_x>0&&answer3_x>0){
-		answer+=1.0;
-	} else if(answer1_x<0&&answer2_x<0&&answer3_x<0){
-		answer-=1.0;
-	}
-	if(answer1_y>0&&answer2_y>0&&answer3_y>0){
-		answer+=1.0;
-	} else if(answer1_y<0&&answer2_y<0&&answer3_y<0){
-		answer-=1.0;
-	}
-	if(answer1_z>0&&answer2_z>0&&answer3_z>0){
-		answer+=1.0;
-	} else if(answer1_z<0&&answer2_z<0&&answer3_z<0){
-		answer-=1.0;
-	}
-	if(answer==3||answer==-3){
-		index = 1;
-	} else if(point_x==t1_x&&point_y==t1_y&&point_z==t1_z){
-		index = 1;
-	} else if(point_x==t2_x&&point_y==t2_y&&point_z==t2_z){
-		index = 1;
-	} else if(point_x==t3_x&&point_y==t3_y&&point_z==t3_z){
-		index = 1;
-	} else {
-		index = 0 ;
-	}
-	return index;
-}
+int point_in_triangle(double point_x, double point_y, double point_z,
+		      double t1_x, double t1_y, double t1_z, double t2_x,
+		      double t2_y, double t2_z, double t3_x, double t3_y,
+		      double t3_z)
+{
+    /*printf("in function: sand=%5.3f clay=%5.3f silt=%5.3f\n",point_x,point_y,point_z); */
+    int index = 0;
 
+    double answer;
 
+    double answer1_x, answer1_y, answer1_z;
+
+    double answer2_x, answer2_y, answer2_z;
+
+    double answer3_x, answer3_y, answer3_z;
+
+    /* Consider three points forming a trinagle from a given soil class boundary ABC */
+    /* Consider F an additional point in space */
+    double af1, af2, af3;	/*Points for vector AF */
+
+    double bf1, bf2, bf3;	/*Points for vector BF */
+
+    double cf1, cf2, cf3;	/*Points for vector CF */
+
+    double ab1, ab2, ab3;	/*Points for vector AB */
+
+    double bc1, bc2, bc3;	/*Points for vector BC */
+
+    double ca1, ca2, ca3;	/*Points for vector CA */
+
+    /* Create vectors AB, BC and CA */
+    ab1 = (t2_x - t1_x);
+    ab2 = (t2_y - t1_y);
+    ab3 = (t2_z - t1_z);
+    bc1 = (t3_x - t2_x);
+    bc2 = (t3_y - t2_y);
+    bc3 = (t3_z - t2_z);
+    ca1 = (t1_x - t3_x);
+    ca2 = (t1_y - t3_y);
+    ca3 = (t1_z - t3_z);
+    /* Create vectors AF, BF and CF */
+    af1 = (point_x - t1_x);
+    af2 = (point_y - t1_y);
+    af3 = (point_z - t1_z);
+    bf1 = (point_x - t2_x);
+    bf2 = (point_y - t2_y);
+    bf3 = (point_z - t2_z);
+    cf1 = (point_x - t3_x);
+    cf2 = (point_y - t3_y);
+    cf3 = (point_z - t3_z);
+    /* Calculate the following CrossProducts: */
+    /* AFxAB */
+    answer1_x = (af2 * ab3) - (af3 * ab2);
+    answer1_y = (af3 * ab1) - (af1 * ab3);
+    answer1_z = (af1 * ab2) - (af2 * ab1);
+    /*      printf("answer(AFxAB)= %f %f %f\n",answer1_x, answer1_y, answer1_z); */
+    /*BFxBC */
+    answer2_x = (bf2 * bc3) - (bf3 * bc2);
+    answer2_y = (bf3 * bc1) - (bf1 * bc3);
+    answer2_z = (bf1 * bc2) - (bf2 * bc1);
+    /*      printf("answer(BFxBC)= %f %f %f\n",answer2_x, answer2_y, answer2_z); */
+    /*CFxCA */
+    answer3_x = (cf2 * ca3) - (cf3 * ca2);
+    answer3_y = (cf3 * ca1) - (cf1 * ca3);
+    answer3_z = (cf1 * ca2) - (cf2 * ca1);
+    /*      printf("answer(CFxCA)= %f %f %f\n",answer3_x, answer3_y, answer3_z); */
+    answer = 0.0;		/*initialize value */
+    if (answer1_x > 0 && answer2_x > 0 && answer3_x > 0) {
+	answer += 1.0;
+    }
+    else if (answer1_x < 0 && answer2_x < 0 && answer3_x < 0) {
+	answer -= 1.0;
+    }
+    if (answer1_y > 0 && answer2_y > 0 && answer3_y > 0) {
+	answer += 1.0;
+    }
+    else if (answer1_y < 0 && answer2_y < 0 && answer3_y < 0) {
+	answer -= 1.0;
+    }
+    if (answer1_z > 0 && answer2_z > 0 && answer3_z > 0) {
+	answer += 1.0;
+    }
+    else if (answer1_z < 0 && answer2_z < 0 && answer3_z < 0) {
+	answer -= 1.0;
+    }
+    if (answer == 3 || answer == -3) {
+	index = 1;
+    }
+    else if (point_x == t1_x && point_y == t1_y && point_z == t1_z) {
+	index = 1;
+    }
+    else if (point_x == t2_x && point_y == t2_y && point_z == t2_z) {
+	index = 1;
+    }
+    else if (point_x == t3_x && point_y == t3_y && point_z == t3_z) {
+	index = 1;
+    }
+    else {
+	index = 0;
+    }
+    return index;
+}

Modified: grass-addons/gipe/r.soilusda2tex/main.c
===================================================================
--- grass-addons/gipe/r.soilusda2tex/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soilusda2tex/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,192 +1,218 @@
-/****************************************************************************
- *
- * MODULE:       r.soilusda2tex
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Transforms USDA 1951 (p209) soil classes into
- * 		 maps of percentage of texture (sand/clay/silt).
- *
- * COPYRIGHT:    (C) 2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#define POLYGON_DIMENSION 20
-double usda2psand(int texture);
-double usda2psilt(int texture);
-double usda2pclay(int texture);
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /* mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1,*output1,*output2,*output3,*output4;
-	
-	struct Flag *flag1;	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /* input raster name*/
-	char *result1,*result2,*result3; /*output raster name*/
-	/*File Descriptors*/
-	int infd_soilusda;
-	int outfd1, outfd2, outfd3;
-	
-	char *soilusda;
-	
-	int i=0,j=0;
-	
-	void *inrast_soilusda;
-	DCELL *outrast1, *outrast2, *outrast3;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_soilusda;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("soil, USDA, classification");
-	module->description = _("USDA Soil classes conversion to texture fractions");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("soilusda");
-	input1->description=_("Name of the USDA Soil classes reclassified [1-12]");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("pclay");
-	output1->description=_("Name of the Soil clay fraction layer [0.0-1.0]");
-	output1->answer     =_("pclay");
-
-	output2 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output2->key        =_("psilt");
-	output2->description=_("Name of the Soil silt fraction layer [0.0-1.0]");
-	output2->answer     =_("psilt");
-
-	output3 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output3->key        =_("psand");
-	output3->description=_("Name of the Soil sand fraction layer [0.0-1.0]");
-	output3->answer     =_("psand");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	soilusda  	= input1->answer;
-	result1	 	= output1->answer;
-	result2		= output2->answer;
-	result3	 	= output3->answer;
-	
-	/***************************************************/
-	mapset = G_find_cell2(soilusda, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), soilusda);
-	}
-	data_type_soilusda = G_raster_map_type(soilusda,mapset);
-	if ( (infd_soilusda = G_open_cell_old (soilusda,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), soilusda);
-	if (G_get_cellhd (soilusda, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), soilusda);
-	inrast_soilusda = G_allocate_raster_buf(data_type_soilusda);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast1 = G_allocate_raster_buf(data_type_output);
-	outrast2 = G_allocate_raster_buf(data_type_output);
-	outrast3 = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd1 = G_open_raster_new (result1,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result1);
-	if ( (outfd2 = G_open_raster_new (result2,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result2);
-	if ( (outfd3 = G_open_raster_new (result3,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result3);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_sand;
-		DCELL d_clay;
-		DCELL d_silt;
-		DCELL d_soilusda;
-		G_percent(row,nrows,2);
-		/* read soil classes [0-11] input map */	
-		if(G_get_raster_row(infd_soilusda,inrast_soilusda,row,data_type_soilusda)<0)
-			G_fatal_error(_("Could not read from <%s>"),soilusda);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_soilusda){
-				case CELL_TYPE:
-					d_soilusda = (double) ((CELL *) inrast_soilusda)[col];
-					break;
-				case FCELL_TYPE:
-					d_soilusda = (double) ((FCELL *) inrast_soilusda)[col];
-					break;
-				case DCELL_TYPE:
-					d_soilusda = ((DCELL *) inrast_soilusda)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_soilusda)){
-				G_set_d_null_value(&outrast1[col],1);
-				G_set_d_null_value(&outrast2[col],1);
-				G_set_d_null_value(&outrast3[col],1);
-			}else {
-				/************************************/
-				/* convert to prct 		    */
-				d_clay = usda2pclay(d_soilusda);
-				d_silt = usda2psilt(d_soilusda);
-				d_sand = usda2psand(d_soilusda);
-				/************************************/
-				outrast1[col] = d_clay;
-				outrast2[col] = d_silt;
-				outrast3[col] = d_sand;
-			}
-		}
-		if (G_put_raster_row (outfd1, outrast1, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file clay"));
-		if (G_put_raster_row (outfd2, outrast2, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file silt"));
-		if (G_put_raster_row (outfd3, outrast3, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file sand"));
-	}
-
-	G_free (inrast_soilusda);
-	G_close_cell (infd_soilusda);
-	
-	G_free (outrast1);
-	G_close_cell (outfd1);
-	G_free (outrast2);
-	G_close_cell (outfd2);
-	G_free (outrast3);
-	G_close_cell (outfd3);
-
-	G_short_history(result1, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result1,&history);
-	
-	G_short_history(result2, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result2,&history);
-	
-	G_short_history(result3, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result3,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       r.soilusda2tex
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Transforms USDA 1951 (p209) soil classes into
+ * 		 maps of percentage of texture (sand/clay/silt).
+ *
+ * COPYRIGHT:    (C) 2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+    
+#define POLYGON_DIMENSION 20
+double usda2psand(int texture);
+
+double usda2psilt(int texture);
+
+double usda2pclay(int texture);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/* mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *output1, *output2, *output3, *output4;
+
+    struct Flag *flag1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/* input raster name */
+
+    char *result1, *result2, *result3;	/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_soilusda;
+
+    int outfd1, outfd2, outfd3;
+
+    char *soilusda;
+
+    int i = 0, j = 0;
+
+    void *inrast_soilusda;
+
+    DCELL * outrast1, *outrast2, *outrast3;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_soilusda;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("soil, USDA, classification");
+    module->description =
+	_("USDA Soil classes conversion to texture fractions");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("soilusda");
+    input1->description =
+	_("Name of the USDA Soil classes reclassified [1-12]");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("pclay");
+    output1->description =
+	_("Name of the Soil clay fraction layer [0.0-1.0]");
+    output1->answer = _("pclay");
+    output2 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output2->key = _("psilt");
+    output2->description =
+	_("Name of the Soil silt fraction layer [0.0-1.0]");
+    output2->answer = _("psilt");
+    output3 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output3->key = _("psand");
+    output3->description =
+	_("Name of the Soil sand fraction layer [0.0-1.0]");
+    output3->answer = _("psand");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    soilusda = input1->answer;
+    result1 = output1->answer;
+    result2 = output2->answer;
+    result3 = output3->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(soilusda, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), soilusda);
+    }
+    data_type_soilusda = G_raster_map_type(soilusda, mapset);
+    if ((infd_soilusda = G_open_cell_old(soilusda, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), soilusda);
+    if (G_get_cellhd(soilusda, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), soilusda);
+    inrast_soilusda = G_allocate_raster_buf(data_type_soilusda);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast1 = G_allocate_raster_buf(data_type_output);
+    outrast2 = G_allocate_raster_buf(data_type_output);
+    outrast3 = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd1 = G_open_raster_new(result1, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result1);
+    if ((outfd2 = G_open_raster_new(result2, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result2);
+    if ((outfd3 = G_open_raster_new(result3, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result3);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_sand;
+	DCELL d_clay;
+	DCELL d_silt;
+	DCELL d_soilusda;
+	G_percent(row, nrows, 2);
+	
+	    /* read soil classes [0-11] input map */ 
+	    if (G_get_raster_row
+		(infd_soilusda, inrast_soilusda, row, data_type_soilusda) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), soilusda);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_soilusda) {
+	    case CELL_TYPE:
+		d_soilusda = (double)((CELL *) inrast_soilusda)[col];
+		break;
+	    case FCELL_TYPE:
+		d_soilusda = (double)((FCELL *) inrast_soilusda)[col];
+		break;
+	    case DCELL_TYPE:
+		d_soilusda = ((DCELL *) inrast_soilusda)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_soilusda)) {
+		G_set_d_null_value(&outrast1[col], 1);
+		G_set_d_null_value(&outrast2[col], 1);
+		G_set_d_null_value(&outrast3[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* convert to prct                  */ 
+		    d_clay = usda2pclay(d_soilusda);
+		d_silt = usda2psilt(d_soilusda);
+		d_sand = usda2psand(d_soilusda);
+		
+
+				/************************************/ 
+		    outrast1[col] = d_clay;
+		outrast2[col] = d_silt;
+		outrast3[col] = d_sand;
+	    }
+	    }
+	if (G_put_raster_row(outfd1, outrast1, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file clay"));
+	if (G_put_raster_row(outfd2, outrast2, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file silt"));
+	if (G_put_raster_row(outfd3, outrast3, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file sand"));
+	}
+    G_free(inrast_soilusda);
+    G_close_cell(infd_soilusda);
+    G_free(outrast1);
+    G_close_cell(outfd1);
+    G_free(outrast2);
+    G_close_cell(outfd2);
+    G_free(outrast3);
+    G_close_cell(outfd3);
+    G_short_history(result1, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result1, &history);
+    G_short_history(result2, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result2, &history);
+    G_short_history(result3, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result3, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.soilusda2tex/usda2pclay.c
===================================================================
--- grass-addons/gipe/r.soilusda2tex/usda2pclay.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soilusda2tex/usda2pclay.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,74 +1,89 @@
 #include<stdio.h>
 
-/* From FAOSOIL CD, after USDA 1951, p209*/
+/* From FAOSOIL CD, after USDA 1951, p209 */
 
-double usda2pclay(int texture){
-	double psand;
-	double psilt;
-	double pclay;
-	if(texture==0){
-/*		printf("clay\n");*/
-		psand=0.2;
-		psilt=0.2;
-		pclay=0.8;
-	} else if (texture==1){
-/*		printf("sandy clay\n");*/
-		psand=0.5;
-		psilt=0.1;
-		pclay=0.4;
-	} else if (texture==2){
-/*		printf("silty clay\n");*/
-		psand=0.05;
-		psilt=0.5;
-		pclay=0.45;
-	} else if (texture==3){
-/*		printf("sandy clay loam\n");*/
-		psand=0.6;
-		psilt=0.15;
-		pclay=0.25;
-	} else if (texture==4){
-/*		printf("clay loam\n");*/
-		psand=0.3;
-		psilt=0.35;
-		pclay=0.35;
-	} else if (texture==5){
-/*		printf("silty clay loam\n");*/
-		psand=0.1;
-		psilt=0.55;
-		pclay=0.35;
-	} else if (texture==6){
-/*		printf("sand\n");*/
-		psand=0.9;
-		psilt=0.05;
-		pclay=0.05;
-	} else if (texture==7){
-/*		printf("loamy sand\n");*/
-		psand=0.85;
-		psilt=0.05;
-		pclay=0.1;
-	} else if (texture==8){
-/*		printf("sandy loam\n");*/
-		psand=0.65;
-		psilt=0.25;
-		pclay=0.1;
-	} else if (texture==9){
-/*		printf("loam\n");*/
-		psand=0.4;
-		psilt=0.4;
-		pclay=0.2;
-	} else if (texture==10){
-/*		printf("silt loam\n");*/
-		psand=0.2;
-		psilt=0.65;
-		pclay=0.15;
-	} else if (texture==11){
-/*		printf("silt\n");*/
-		psand=0.05;
-		psilt=0.9;
-		pclay=0.05;
-	} else {
-		/*printf("i am confused here...Can you do it yourself please?\n");*/
-	}
-	return pclay;
+double usda2pclay(int texture)
+{
+    double psand;
+
+    double psilt;
+
+    double pclay;
+
+    if (texture == 0) {
+	/*              printf("clay\n"); */
+	psand = 0.2;
+	psilt = 0.2;
+	pclay = 0.8;
+    }
+    else if (texture == 1) {
+	/*              printf("sandy clay\n"); */
+	psand = 0.5;
+	psilt = 0.1;
+	pclay = 0.4;
+    }
+    else if (texture == 2) {
+	/*              printf("silty clay\n"); */
+	psand = 0.05;
+	psilt = 0.5;
+	pclay = 0.45;
+    }
+    else if (texture == 3) {
+	/*              printf("sandy clay loam\n"); */
+	psand = 0.6;
+	psilt = 0.15;
+	pclay = 0.25;
+    }
+    else if (texture == 4) {
+	/*              printf("clay loam\n"); */
+	psand = 0.3;
+	psilt = 0.35;
+	pclay = 0.35;
+    }
+    else if (texture == 5) {
+	/*              printf("silty clay loam\n"); */
+	psand = 0.1;
+	psilt = 0.55;
+	pclay = 0.35;
+    }
+    else if (texture == 6) {
+	/*              printf("sand\n"); */
+	psand = 0.9;
+	psilt = 0.05;
+	pclay = 0.05;
+    }
+    else if (texture == 7) {
+	/*              printf("loamy sand\n"); */
+	psand = 0.85;
+	psilt = 0.05;
+	pclay = 0.1;
+    }
+    else if (texture == 8) {
+	/*              printf("sandy loam\n"); */
+	psand = 0.65;
+	psilt = 0.25;
+	pclay = 0.1;
+    }
+    else if (texture == 9) {
+	/*              printf("loam\n"); */
+	psand = 0.4;
+	psilt = 0.4;
+	pclay = 0.2;
+    }
+    else if (texture == 10) {
+	/*              printf("silt loam\n"); */
+	psand = 0.2;
+	psilt = 0.65;
+	pclay = 0.15;
+    }
+    else if (texture == 11) {
+	/*              printf("silt\n"); */
+	psand = 0.05;
+	psilt = 0.9;
+	pclay = 0.05;
+    }
+    else {
+	/*printf("i am confused here...Can you do it yourself please?\n"); */
+    }
+    return pclay;
 }
-

Modified: grass-addons/gipe/r.soilusda2tex/usda2psand.c
===================================================================
--- grass-addons/gipe/r.soilusda2tex/usda2psand.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soilusda2tex/usda2psand.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,74 +1,89 @@
 #include<stdio.h>
 
-/* From FAOSOIL CD, after USDA 1951, p209*/
+/* From FAOSOIL CD, after USDA 1951, p209 */
 
-double usda2psand(int texture){
-	double psand;
-	double psilt;
-	double pclay;
-	if(texture==0){
-/*		printf("clay\n");*/
-		psand=0.2;
-		psilt=0.2;
-		pclay=0.8;
-	} else if (texture==1){
-/*		printf("sandy clay\n");*/
-		psand=0.5;
-		psilt=0.1;
-		pclay=0.4;
-	} else if (texture==2){
-/*		printf("silty clay\n");*/
-		psand=0.05;
-		psilt=0.5;
-		pclay=0.45;
-	} else if (texture==3){
-/*		printf("sandy clay loam\n");*/
-		psand=0.6;
-		psilt=0.15;
-		pclay=0.25;
-	} else if (texture==4){
-/*		printf("clay loam\n");*/
-		psand=0.3;
-		psilt=0.35;
-		pclay=0.35;
-	} else if (texture==5){
-/*		printf("silty clay loam\n");*/
-		psand=0.1;
-		psilt=0.55;
-		pclay=0.35;
-	} else if (texture==6){
-/*		printf("sand\n");*/
-		psand=0.9;
-		psilt=0.05;
-		pclay=0.05;
-	} else if (texture==7){
-/*		printf("loamy sand\n");*/
-		psand=0.85;
-		psilt=0.05;
-		pclay=0.1;
-	} else if (texture==8){
-/*		printf("sandy loam\n");*/
-		psand=0.65;
-		psilt=0.25;
-		pclay=0.1;
-	} else if (texture==9){
-/*		printf("loam\n");*/
-		psand=0.4;
-		psilt=0.4;
-		pclay=0.2;
-	} else if (texture==10){
-/*		printf("silt loam\n");*/
-		psand=0.2;
-		psilt=0.65;
-		pclay=0.15;
-	} else if (texture==11){
-/*		printf("silt\n");*/
-		psand=0.05;
-		psilt=0.9;
-		pclay=0.05;
-	} else {
-		/*printf("i am confused here...Can you do it yourself please?\n");*/
-	}
-	return psand;
+double usda2psand(int texture)
+{
+    double psand;
+
+    double psilt;
+
+    double pclay;
+
+    if (texture == 0) {
+	/*              printf("clay\n"); */
+	psand = 0.2;
+	psilt = 0.2;
+	pclay = 0.8;
+    }
+    else if (texture == 1) {
+	/*              printf("sandy clay\n"); */
+	psand = 0.5;
+	psilt = 0.1;
+	pclay = 0.4;
+    }
+    else if (texture == 2) {
+	/*              printf("silty clay\n"); */
+	psand = 0.05;
+	psilt = 0.5;
+	pclay = 0.45;
+    }
+    else if (texture == 3) {
+	/*              printf("sandy clay loam\n"); */
+	psand = 0.6;
+	psilt = 0.15;
+	pclay = 0.25;
+    }
+    else if (texture == 4) {
+	/*              printf("clay loam\n"); */
+	psand = 0.3;
+	psilt = 0.35;
+	pclay = 0.35;
+    }
+    else if (texture == 5) {
+	/*              printf("silty clay loam\n"); */
+	psand = 0.1;
+	psilt = 0.55;
+	pclay = 0.35;
+    }
+    else if (texture == 6) {
+	/*              printf("sand\n"); */
+	psand = 0.9;
+	psilt = 0.05;
+	pclay = 0.05;
+    }
+    else if (texture == 7) {
+	/*              printf("loamy sand\n"); */
+	psand = 0.85;
+	psilt = 0.05;
+	pclay = 0.1;
+    }
+    else if (texture == 8) {
+	/*              printf("sandy loam\n"); */
+	psand = 0.65;
+	psilt = 0.25;
+	pclay = 0.1;
+    }
+    else if (texture == 9) {
+	/*              printf("loam\n"); */
+	psand = 0.4;
+	psilt = 0.4;
+	pclay = 0.2;
+    }
+    else if (texture == 10) {
+	/*              printf("silt loam\n"); */
+	psand = 0.2;
+	psilt = 0.65;
+	pclay = 0.15;
+    }
+    else if (texture == 11) {
+	/*              printf("silt\n"); */
+	psand = 0.05;
+	psilt = 0.9;
+	pclay = 0.05;
+    }
+    else {
+	/*printf("i am confused here...Can you do it yourself please?\n"); */
+    }
+    return psand;
 }
-

Modified: grass-addons/gipe/r.soilusda2tex/usda2psilt.c
===================================================================
--- grass-addons/gipe/r.soilusda2tex/usda2psilt.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.soilusda2tex/usda2psilt.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,74 +1,89 @@
 #include<stdio.h>
 
-/* From FAOSOIL CD, after USDA 1951, p209*/
+/* From FAOSOIL CD, after USDA 1951, p209 */
 
-double usda2psilt(int texture){
-	double psand;
-	double psilt;
-	double pclay;
-	if(texture==0){
-/*		printf("clay\n");*/
-		psand=0.2;
-		psilt=0.2;
-		pclay=0.8;
-	} else if (texture==1){
-/*		printf("sandy clay\n");*/
-		psand=0.5;
-		psilt=0.1;
-		pclay=0.4;
-	} else if (texture==2){
-/*		printf("silty clay\n");*/
-		psand=0.05;
-		psilt=0.5;
-		pclay=0.45;
-	} else if (texture==3){
-/*		printf("sandy clay loam\n");*/
-		psand=0.6;
-		psilt=0.15;
-		pclay=0.25;
-	} else if (texture==4){
-/*		printf("clay loam\n");*/
-		psand=0.3;
-		psilt=0.35;
-		pclay=0.35;
-	} else if (texture==5){
-/*		printf("silty clay loam\n");*/
-		psand=0.1;
-		psilt=0.55;
-		pclay=0.35;
-	} else if (texture==6){
-/*		printf("sand\n");*/
-		psand=0.9;
-		psilt=0.05;
-		pclay=0.05;
-	} else if (texture==7){
-/*		printf("loamy sand\n");*/
-		psand=0.85;
-		psilt=0.05;
-		pclay=0.1;
-	} else if (texture==8){
-/*		printf("sandy loam\n");*/
-		psand=0.65;
-		psilt=0.25;
-		pclay=0.1;
-	} else if (texture==9){
-/*		printf("loam\n");*/
-		psand=0.4;
-		psilt=0.4;
-		pclay=0.2;
-	} else if (texture==10){
-/*		printf("silt loam\n");*/
-		psand=0.2;
-		psilt=0.65;
-		pclay=0.15;
-	} else if (texture==11){
-/*		printf("silt\n");*/
-		psand=0.05;
-		psilt=0.9;
-		pclay=0.05;
-	} else {
-		/*printf("i am confused here...Can you do it yourself please?\n");*/
-	}
-	return psilt;
+double usda2psilt(int texture)
+{
+    double psand;
+
+    double psilt;
+
+    double pclay;
+
+    if (texture == 0) {
+	/*              printf("clay\n"); */
+	psand = 0.2;
+	psilt = 0.2;
+	pclay = 0.8;
+    }
+    else if (texture == 1) {
+	/*              printf("sandy clay\n"); */
+	psand = 0.5;
+	psilt = 0.1;
+	pclay = 0.4;
+    }
+    else if (texture == 2) {
+	/*              printf("silty clay\n"); */
+	psand = 0.05;
+	psilt = 0.5;
+	pclay = 0.45;
+    }
+    else if (texture == 3) {
+	/*              printf("sandy clay loam\n"); */
+	psand = 0.6;
+	psilt = 0.15;
+	pclay = 0.25;
+    }
+    else if (texture == 4) {
+	/*              printf("clay loam\n"); */
+	psand = 0.3;
+	psilt = 0.35;
+	pclay = 0.35;
+    }
+    else if (texture == 5) {
+	/*              printf("silty clay loam\n"); */
+	psand = 0.1;
+	psilt = 0.55;
+	pclay = 0.35;
+    }
+    else if (texture == 6) {
+	/*              printf("sand\n"); */
+	psand = 0.9;
+	psilt = 0.05;
+	pclay = 0.05;
+    }
+    else if (texture == 7) {
+	/*              printf("loamy sand\n"); */
+	psand = 0.85;
+	psilt = 0.05;
+	pclay = 0.1;
+    }
+    else if (texture == 8) {
+	/*              printf("sandy loam\n"); */
+	psand = 0.65;
+	psilt = 0.25;
+	pclay = 0.1;
+    }
+    else if (texture == 9) {
+	/*              printf("loam\n"); */
+	psand = 0.4;
+	psilt = 0.4;
+	pclay = 0.2;
+    }
+    else if (texture == 10) {
+	/*              printf("silt loam\n"); */
+	psand = 0.2;
+	psilt = 0.65;
+	pclay = 0.15;
+    }
+    else if (texture == 11) {
+	/*              printf("silt\n"); */
+	psand = 0.05;
+	psilt = 0.9;
+	pclay = 0.05;
+    }
+    else {
+	/*printf("i am confused here...Can you do it yourself please?\n"); */
+    }
+    return psilt;
 }
-

Modified: grass-addons/gipe/r.uslek/main.c
===================================================================
--- grass-addons/gipe/r.uslek/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.uslek/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,264 +1,292 @@
-/****************************************************************************
- *
- * MODULE:       r.uslek
- * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
- * PURPOSE:      Transforms percentage of texture (sand/clay/silt)
- *               into USDA 1951 (p209) soil texture classes and then
- *               into USLE soil erodibility factor (K) as an output
- *
- * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-#include "prct2tex.h"
-#include "tex2usle_k.h"
-
-#define POLYGON_DIMENSION 20
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	struct GModule *module;
-	struct Option *input1, *input2, *input3, *input4,*output1;
-	
-	struct History history; /*metadata*/
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /* input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_psand, infd_psilt, infd_pclay, infd_pomat;
-	int outfd;
-	
-	char *psand,*psilt,*pclay,*pomat;
-	
-	int i=0,j=0;
-	
-	void *inrast_psand, *inrast_psilt, *inrast_pclay, *inrast_pomat;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_psand;
-	RASTER_MAP_TYPE data_type_psilt;
-	RASTER_MAP_TYPE data_type_pclay;
-	RASTER_MAP_TYPE data_type_pomat;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("USLE, soil, erosion");
-	module->description = _("USLE Soil Erodibility Factor (K)");
-
-	/* Define the different options */
-	input1 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input1->key	   = _("psand");
-	input1->description=_("Name of the Soil sand fraction map [0.0-1.0]");
-	input1->answer     =_("psand");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key        =_("pclay");
-	input2->description=_("Name of the Soil clay fraction map [0.0-1.0]");
-	input2->answer     =_("pclay");
-
-	input3 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input3->key        =_("psilt");
-	input3->description=_("Name of the Soil silt fraction map [0.0-1.0]");
-	input3->answer     =_("psilt");
-
-	input4 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input4->key        =_("pomat");
-	input4->description=_("Name of the Soil Organic Matter map [0.0-1.0]");
-	input4->answer     =_("pomat");
-
-	output1 = G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output1->key        =_("usle_k");
-	output1->description=_("Name of the output USLE K factor layer");
-	output1->answer     =_("usle_k");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-
-	psand	 	= input1->answer;
-	pclay	 	= input2->answer;
-	psilt		= input3->answer;
-	pomat	 	= input4->answer;
-	
-	result  	= output1->answer;
-	/***************************************************/
-	mapset = G_find_cell2(psand, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), psand);
-	}
-	data_type_psand = G_raster_map_type(psand,mapset);
-	if ( (infd_psand = G_open_cell_old (psand,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), psand);
-	if (G_get_cellhd (psand, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), psand);
-	inrast_psand = G_allocate_raster_buf(data_type_psand);
-	/***************************************************/
-	mapset = G_find_cell2 (psilt, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), psilt);
-	}
-	data_type_psilt = G_raster_map_type(psilt,mapset);
-	if ( (infd_psilt = G_open_cell_old (psilt,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), psilt);
-	if (G_get_cellhd (psilt, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), psilt);
-	inrast_psilt = G_allocate_raster_buf(data_type_psilt);
-	/***************************************************/
-	mapset = G_find_cell2 (pclay, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), pclay);
-	}
-	data_type_pclay = G_raster_map_type(pclay,mapset);
-	if ( (infd_pclay = G_open_cell_old (pclay,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), pclay);
-	if (G_get_cellhd (pclay, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), pclay);
-	inrast_pclay = G_allocate_raster_buf(data_type_pclay);
-	/***************************************************/
-	mapset = G_find_cell2 (pomat, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("Cell file [%s] not found"), pomat);
-	}
-	data_type_pomat = G_raster_map_type(pomat,mapset);
-	if ( (infd_pomat = G_open_cell_old (pomat,mapset)) < 0)
-		G_fatal_error(_("Cannot open cell file [%s]"), pomat);
-	if (G_get_cellhd (pomat, mapset, &cellhd) < 0)
-		G_fatal_error(_("Cannot read file header of [%s]"), pomat);
-	inrast_pomat = G_allocate_raster_buf(data_type_pomat);
-	/***************************************************/
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_sand;
-		DCELL d_clay;
-		DCELL d_silt;
-		DCELL d_om;
-		double tex[4] = {0.0,0.0,0.0,0.0};
-		G_percent(row,nrows,2);
-		/* read soil input maps */	
-		if(G_get_raster_row(infd_psand,inrast_psand,row,data_type_psand)<0)
-			G_fatal_error(_("Could not read from <%s>"),psand);
-		if(G_get_raster_row(infd_psilt,inrast_psilt,row,data_type_psilt)<0)
-			G_fatal_error(_("Could not read from <%s>"),psilt);
-		if(G_get_raster_row(infd_pclay,inrast_pclay,row,data_type_pclay)<0)
-			G_fatal_error(_("Could not read from <%s>"),pclay);
-		if(G_get_raster_row(infd_pomat,inrast_pomat,row,data_type_pomat)<0)
-			G_fatal_error(_("Could not read from <%s>"),pomat);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			switch(data_type_psand){
-				case CELL_TYPE:
-					d_sand = (double) ((CELL *) inrast_psand)[col];
-					break;
-				case FCELL_TYPE:
-					d_sand = (double) ((FCELL *) inrast_psand)[col];
-					break;
-				case DCELL_TYPE:
-					d_sand = ((DCELL *) inrast_psand)[col];
-					break;
-			}
-			switch(data_type_psilt){
-				case CELL_TYPE:
-					d_silt = (double) ((CELL *) inrast_psilt)[col];
-					break;
-				case FCELL_TYPE:
-					d_silt = (double) ((FCELL *) inrast_psilt)[col];
-					break;
-				case DCELL_TYPE:
-					d_silt = ((DCELL *) inrast_psilt)[col];
-					break;
-			}
-			switch(data_type_pclay){
-				case CELL_TYPE:
-					d_clay = (double) ((CELL *) inrast_pclay)[col];
-					break;
-				case FCELL_TYPE:
-					d_clay = (double) ((FCELL *) inrast_pclay)[col];
-					break;
-				case DCELL_TYPE:
-					d_clay = ((DCELL *) inrast_pclay)[col];
-					break;
-			}
-			switch(data_type_pomat){
-				case CELL_TYPE:
-					d_om = (double) ((CELL *) inrast_pomat)[col];
-					break;
-				case FCELL_TYPE:
-					d_om = (double) ((FCELL *) inrast_pomat)[col];
-					break;
-				case DCELL_TYPE:
-					d_om = ((DCELL *) inrast_pomat)[col];
-					break;
-			}
-			if(G_is_d_null_value(&d_sand)||
-			G_is_d_null_value(&d_clay)||
-			G_is_d_null_value(&d_silt)){
-				G_set_d_null_value(&outrast[col],1);
-			}else {
-				/************************************/
-				/* convert to usle_k 		    */
-				d = (double) prct2tex(d_sand, d_clay, d_silt);
-				if((d_sand+d_clay+d_silt)!=1.0){
-					G_message("d=%icol=%i||%5.3f%5.3f%5.3f\n",d,col,d_sand,d_clay,d_silt);
-				}
-				if(G_is_d_null_value(&d_om)){
-					d_om=0.0;/*if OM==NULL then make it 0.0*/
-				}
-				d = tex2usle_k((int) d, d_om);	
-				/*G_message(_(" -> %5.3f",d));*/
-				/************************************/
-				if(d>1.0){/*in case some map input not standard*/
-					d=0.0;
-				}
-				outrast[col] = d;
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	G_free (inrast_psand);
-	G_free (inrast_psilt);
-	G_free (inrast_pclay);
-	G_free (inrast_pomat);
-	G_close_cell (infd_psand);
-	G_close_cell (infd_psilt);
-	G_close_cell (infd_pclay);
-	G_close_cell (infd_pomat);
-	
-	G_free (outrast);
-	G_close_cell (outfd);
-
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       r.uslek
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Transforms percentage of texture (sand/clay/silt)
+ *               into USDA 1951 (p209) soil texture classes and then
+ *               into USLE soil erodibility factor (K) as an output
+ *
+ * COPYRIGHT:    (C) 2002-2008 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ *
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "prct2tex.h"
+#include "tex2usle_k.h"
+    
+#define POLYGON_DIMENSION 20
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *input3, *input4, *output1;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/* input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_psand, infd_psilt, infd_pclay, infd_pomat;
+
+    int outfd;
+
+    char *psand, *psilt, *pclay, *pomat;
+
+    int i = 0, j = 0;
+
+    void *inrast_psand, *inrast_psilt, *inrast_pclay, *inrast_pomat;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_psand;
+    RASTER_MAP_TYPE data_type_psilt;
+    RASTER_MAP_TYPE data_type_pclay;
+    RASTER_MAP_TYPE data_type_pomat;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("USLE, soil, erosion");
+    module->description = _("USLE Soil Erodibility Factor (K)");
+    
+	/* Define the different options */ 
+	input1 = G_define_standard_option(G_OPT_R_INPUT);
+    input1->key = _("psand");
+    input1->description = _("Name of the Soil sand fraction map [0.0-1.0]");
+    input1->answer = _("psand");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("pclay");
+    input2->description = _("Name of the Soil clay fraction map [0.0-1.0]");
+    input2->answer = _("pclay");
+    input3 = G_define_standard_option(G_OPT_R_INPUT);
+    input3->key = _("psilt");
+    input3->description = _("Name of the Soil silt fraction map [0.0-1.0]");
+    input3->answer = _("psilt");
+    input4 = G_define_standard_option(G_OPT_R_INPUT);
+    input4->key = _("pomat");
+    input4->description = _("Name of the Soil Organic Matter map [0.0-1.0]");
+    input4->answer = _("pomat");
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    output1->key = _("usle_k");
+    output1->description = _("Name of the output USLE K factor layer");
+    output1->answer = _("usle_k");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    psand = input1->answer;
+    pclay = input2->answer;
+    psilt = input3->answer;
+    pomat = input4->answer;
+    result = output1->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(psand, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), psand);
+    }
+    data_type_psand = G_raster_map_type(psand, mapset);
+    if ((infd_psand = G_open_cell_old(psand, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), psand);
+    if (G_get_cellhd(psand, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), psand);
+    inrast_psand = G_allocate_raster_buf(data_type_psand);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(psilt, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), psilt);
+    }
+    data_type_psilt = G_raster_map_type(psilt, mapset);
+    if ((infd_psilt = G_open_cell_old(psilt, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), psilt);
+    if (G_get_cellhd(psilt, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), psilt);
+    inrast_psilt = G_allocate_raster_buf(data_type_psilt);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(pclay, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), pclay);
+    }
+    data_type_pclay = G_raster_map_type(pclay, mapset);
+    if ((infd_pclay = G_open_cell_old(pclay, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), pclay);
+    if (G_get_cellhd(pclay, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), pclay);
+    inrast_pclay = G_allocate_raster_buf(data_type_pclay);
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(pomat, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("Cell file [%s] not found"), pomat);
+    }
+    data_type_pomat = G_raster_map_type(pomat, mapset);
+    if ((infd_pomat = G_open_cell_old(pomat, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), pomat);
+    if (G_get_cellhd(pomat, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s]"), pomat);
+    inrast_pomat = G_allocate_raster_buf(data_type_pomat);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_sand;
+	DCELL d_clay;
+	DCELL d_silt;
+	DCELL d_om;
+	double tex[4] = { 0.0, 0.0, 0.0, 0.0 };
+	G_percent(row, nrows, 2);
+	
+	    /* read soil input maps */ 
+	    if (G_get_raster_row
+		(infd_psand, inrast_psand, row, data_type_psand) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), psand);
+	if (G_get_raster_row(infd_psilt, inrast_psilt, row, data_type_psilt)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), psilt);
+	if (G_get_raster_row(infd_pclay, inrast_pclay, row, data_type_pclay)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), pclay);
+	if (G_get_raster_row(infd_pomat, inrast_pomat, row, data_type_pomat)
+	     < 0)
+	    G_fatal_error(_("Could not read from <%s>"), pomat);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    switch (data_type_psand) {
+	    case CELL_TYPE:
+		d_sand = (double)((CELL *) inrast_psand)[col];
+		break;
+	    case FCELL_TYPE:
+		d_sand = (double)((FCELL *) inrast_psand)[col];
+		break;
+	    case DCELL_TYPE:
+		d_sand = ((DCELL *) inrast_psand)[col];
+		break;
+	    }
+	    switch (data_type_psilt) {
+	    case CELL_TYPE:
+		d_silt = (double)((CELL *) inrast_psilt)[col];
+		break;
+	    case FCELL_TYPE:
+		d_silt = (double)((FCELL *) inrast_psilt)[col];
+		break;
+	    case DCELL_TYPE:
+		d_silt = ((DCELL *) inrast_psilt)[col];
+		break;
+	    }
+	    switch (data_type_pclay) {
+	    case CELL_TYPE:
+		d_clay = (double)((CELL *) inrast_pclay)[col];
+		break;
+	    case FCELL_TYPE:
+		d_clay = (double)((FCELL *) inrast_pclay)[col];
+		break;
+	    case DCELL_TYPE:
+		d_clay = ((DCELL *) inrast_pclay)[col];
+		break;
+	    }
+	    switch (data_type_pomat) {
+	    case CELL_TYPE:
+		d_om = (double)((CELL *) inrast_pomat)[col];
+		break;
+	    case FCELL_TYPE:
+		d_om = (double)((FCELL *) inrast_pomat)[col];
+		break;
+	    case DCELL_TYPE:
+		d_om = ((DCELL *) inrast_pomat)[col];
+		break;
+	    }
+	    if (G_is_d_null_value(&d_sand) || G_is_d_null_value(&d_clay) ||
+		 G_is_d_null_value(&d_silt)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /* convert to usle_k                */ 
+		    d = (double)prct2tex(d_sand, d_clay, d_silt);
+		if ((d_sand + d_clay + d_silt) != 1.0) {
+		    G_message("d=%icol=%i||%5.3f%5.3f%5.3f\n", d, col,
+			       d_sand, d_clay, d_silt);
+		}
+		if (G_is_d_null_value(&d_om)) {
+		    d_om = 0.0;	/*if OM==NULL then make it 0.0 */
+		}
+		d = tex2usle_k((int)d, d_om);
+		
+		    /*G_message(_(" -> %5.3f",d)); */ 
+
+				/************************************/ 
+		    if (d > 1.0) {	/*in case some map input not standard */
+		    d = 0.0;
+		}
+		outrast[col] = d;
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_psand);
+    G_free(inrast_psilt);
+    G_free(inrast_pclay);
+    G_free(inrast_pomat);
+    G_close_cell(infd_psand);
+    G_close_cell(infd_psilt);
+    G_close_cell(infd_pclay);
+    G_close_cell(infd_pomat);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.uslek/prct2tex.c
===================================================================
--- grass-addons/gipe/r.uslek/prct2tex.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.uslek/prct2tex.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,480 +1,700 @@
 #include<stdio.h>
 
 #define POLYGON_DIMENSION 20
-/* From FAOSOIL CD, after USDA 1951, p209*/
+/* From FAOSOIL CD, after USDA 1951, p209 */
 
-struct vector{
-	double sand;
-	double clay;
-	double silt;
+struct vector
+{
+    double sand;
+    double clay;
+    double silt;
 };
 
-double point_in_triangle(double point_x, double point_y, double point_z, double t1_x, double t1_y, double t1_z, double t2_x, double t2_y, double t2_z, double t3_x, double t3_y, double t3_z){
-	/*printf("in function: sand=%5.3f clay=%5.3f silt=%5.3f\n",point_x,point_y,point_z);*/
-	double answer;
-	double answer1_x, answer1_y, answer1_z;
-	double answer2_x, answer2_y, answer2_z;
-	double answer3_x, answer3_y, answer3_z;
-	/* Consider three points forming a trinagle from a given soil class boundary ABC*/
-	/* Consider F an additional point in space*/
-	double af1, af2, af3; /*Points for vector AF*/
-	double bf1, bf2, bf3; /*Points for vector BF*/
-	double cf1, cf2, cf3; /*Points for vector CF*/
-	double ab1, ab2, ab3; /*Points for vector AB*/ 
-	double bc1, bc2, bc3; /*Points for vector BC*/
-	double ca1, ca2, ca3; /*Points for vector CA*/
-	/* Create vectors AB, BC and CA*/
-	ab1=(t2_x-t1_x);
-	ab2=(t2_y-t1_y);
-	ab3=(t2_z-t1_z);
-	bc1=(t3_x-t2_x);
-	bc2=(t3_y-t2_y);
-	bc3=(t3_z-t2_z);
-	ca1=(t1_x-t3_x);
-	ca2=(t1_y-t3_y);
-	ca3=(t1_z-t3_z);
-	/* Create vectors AF, BF and CF*/
-	af1=(point_x-t1_x);
-	af2=(point_y-t1_y);
-	af3=(point_z-t1_z);
-	bf1=(point_x-t2_x);
-	bf2=(point_y-t2_y);
-	bf3=(point_z-t2_z);
-	cf1=(point_x-t3_x);
-	cf2=(point_y-t3_y);
-	cf3=(point_z-t3_z);
-	/* Calculate the following CrossProducts:*/
-	/* AFxAB*/
-	answer1_x=(af2*ab3)-(af3*ab2);
-	answer1_y=(af3*ab1)-(af1*ab3);
-	answer1_z=(af1*ab2)-(af2*ab1);
-/*	printf("answer(AFxAB)= %f %f %f\n",answer1_x, answer1_y, answer1_z);*/
-	/*BFxBC*/
-	answer2_x=(bf2*bc3)-(bf3*bc2);
-	answer2_y=(bf3*bc1)-(bf1*bc3);
-	answer2_z=(bf1*bc2)-(bf2*bc1);
-/*	printf("answer(BFxBC)= %f %f %f\n",answer2_x, answer2_y, answer2_z);*/
-	/*CFxCA*/
-	answer3_x=(cf2*ca3)-(cf3*ca2);
-	answer3_y=(cf3*ca1)-(cf1*ca3);
-	answer3_z=(cf1*ca2)-(cf2*ca1);
-/*	printf("answer(CFxCA)= %f %f %f\n",answer3_x, answer3_y, answer3_z);*/
-	answer=0.0;/*initialize value*/
-	if((int)answer1_x>=0&&(int)answer2_x>=0&&(int)answer3_x>=0){
-		answer+=1.0;
-	} else if((int)answer1_x<=0&&(int)answer2_x<=0&&(int)answer3_x<=0){
-		answer-=1.0;
-	}
-	if((int)answer1_y>=0&&(int)answer2_y>=0&&(int)answer3_y>=0){
-		answer+=1.0;
-	} else if((int)answer1_y<=0&&(int)answer2_y<=0&&(int)answer3_y<=0){
-		answer-=1.0;
-	}
-	if((int)answer1_z>=0&&(int)answer2_z>=0&&(int)answer3_z>=0){
-		answer+=1.0;
-	} else if((int)answer1_z<=0&&(int)answer2_z<=0&&(int)answer3_z<=0){
-		answer-=1.0;
-	}
-	if(answer==3||answer==-3){
-		answer=1;
-	} else {
-		answer=0;
-	}
-	return answer;
+double point_in_triangle(double point_x, double point_y, double point_z,
+			 double t1_x, double t1_y, double t1_z, double t2_x,
+			 double t2_y, double t2_z, double t3_x, double t3_y,
+			 double t3_z)
+{
+    /*printf("in function: sand=%5.3f clay=%5.3f silt=%5.3f\n",point_x,point_y,point_z); */
+    double answer;
+
+    double answer1_x, answer1_y, answer1_z;
+
+    double answer2_x, answer2_y, answer2_z;
+
+    double answer3_x, answer3_y, answer3_z;
+
+    /* Consider three points forming a trinagle from a given soil class boundary ABC */
+    /* Consider F an additional point in space */
+    double af1, af2, af3;	/*Points for vector AF */
+
+    double bf1, bf2, bf3;	/*Points for vector BF */
+
+    double cf1, cf2, cf3;	/*Points for vector CF */
+
+    double ab1, ab2, ab3;	/*Points for vector AB */
+
+    double bc1, bc2, bc3;	/*Points for vector BC */
+
+    double ca1, ca2, ca3;	/*Points for vector CA */
+
+    /* Create vectors AB, BC and CA */
+    ab1 = (t2_x - t1_x);
+    ab2 = (t2_y - t1_y);
+    ab3 = (t2_z - t1_z);
+    bc1 = (t3_x - t2_x);
+    bc2 = (t3_y - t2_y);
+    bc3 = (t3_z - t2_z);
+    ca1 = (t1_x - t3_x);
+    ca2 = (t1_y - t3_y);
+    ca3 = (t1_z - t3_z);
+    /* Create vectors AF, BF and CF */
+    af1 = (point_x - t1_x);
+    af2 = (point_y - t1_y);
+    af3 = (point_z - t1_z);
+    bf1 = (point_x - t2_x);
+    bf2 = (point_y - t2_y);
+    bf3 = (point_z - t2_z);
+    cf1 = (point_x - t3_x);
+    cf2 = (point_y - t3_y);
+    cf3 = (point_z - t3_z);
+    /* Calculate the following CrossProducts: */
+    /* AFxAB */
+    answer1_x = (af2 * ab3) - (af3 * ab2);
+    answer1_y = (af3 * ab1) - (af1 * ab3);
+    answer1_z = (af1 * ab2) - (af2 * ab1);
+    /*      printf("answer(AFxAB)= %f %f %f\n",answer1_x, answer1_y, answer1_z); */
+    /*BFxBC */
+    answer2_x = (bf2 * bc3) - (bf3 * bc2);
+    answer2_y = (bf3 * bc1) - (bf1 * bc3);
+    answer2_z = (bf1 * bc2) - (bf2 * bc1);
+    /*      printf("answer(BFxBC)= %f %f %f\n",answer2_x, answer2_y, answer2_z); */
+    /*CFxCA */
+    answer3_x = (cf2 * ca3) - (cf3 * ca2);
+    answer3_y = (cf3 * ca1) - (cf1 * ca3);
+    answer3_z = (cf1 * ca2) - (cf2 * ca1);
+    /*      printf("answer(CFxCA)= %f %f %f\n",answer3_x, answer3_y, answer3_z); */
+    answer = 0.0;		/*initialize value */
+    if ((int)answer1_x >= 0 && (int)answer2_x >= 0 && (int)answer3_x >= 0) {
+	answer += 1.0;
+    }
+    else if ((int)answer1_x <= 0 && (int)answer2_x <= 0 &&
+	     (int)answer3_x <= 0) {
+	answer -= 1.0;
+    }
+    if ((int)answer1_y >= 0 && (int)answer2_y >= 0 && (int)answer3_y >= 0) {
+	answer += 1.0;
+    }
+    else if ((int)answer1_y <= 0 && (int)answer2_y <= 0 &&
+	     (int)answer3_y <= 0) {
+	answer -= 1.0;
+    }
+    if ((int)answer1_z >= 0 && (int)answer2_z >= 0 && (int)answer3_z >= 0) {
+	answer += 1.0;
+    }
+    else if ((int)answer1_z <= 0 && (int)answer2_z <= 0 &&
+	     (int)answer3_z <= 0) {
+	answer -= 1.0;
+    }
+    if (answer == 3 || answer == -3) {
+	answer = 1;
+    }
+    else {
+	answer = 0;
+    }
+    return answer;
 }
 
-int prct2tex(double sand_input, double clay_input, double silt_input){
-	/*printf("%5.3f||%5.3f||%5.3f",sand_input,clay_input,silt_input);*/
-	int i;
-	double temp;
-	/* set up index for soil texture classes*/
-	int index=20;
-	/* set up mark index for inside/outside polygon check*/
-	double mark[POLYGON_DIMENSION]={0.0};
-	/*printf("in prct2tex()\n");*/
-	/*setup the 3Dvectors and initialize them*/
-	/* index 0*/
-	struct vector cls_clay[POLYGON_DIMENSION] = {0.0};
-	/* index 1*/
-	struct vector cls_sandy_clay[POLYGON_DIMENSION] = {0.0};
-	/* index 2*/
-	struct vector cls_silty_clay[POLYGON_DIMENSION] = {0.0};
-	/* index 3*/
-	struct vector cls_sandy_clay_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 4*/
-	struct vector cls_clay_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 5*/
-	struct vector cls_silty_clay_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 6*/
-	struct vector cls_sand[POLYGON_DIMENSION] = {0.0};
-	/* index 7*/
-	struct vector cls_loamy_sand[POLYGON_DIMENSION] = {0.0};
-	/* index 8*/
-	struct vector cls_sandy_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 9*/
-	struct vector cls_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 10*/
-	struct vector cls_silt_loam[POLYGON_DIMENSION] = {0.0};
-	/* index 11*/
-	struct vector cls_silt[POLYGON_DIMENSION] = {0.0};
+int prct2tex(double sand_input, double clay_input, double silt_input)
+{
+    /*printf("%5.3f||%5.3f||%5.3f",sand_input,clay_input,silt_input); */
+    int i;
 
-	if((sand_input+clay_input+silt_input)<=10.0){
-		sand_input=sand_input*100.0;
-		clay_input=clay_input*100.0;
-		silt_input=silt_input*100.0;
-	}
-	/*printf("%5.3f||%5.3f||%5.3f|",sand_input,clay_input,silt_input);*/
-	
-	/*Feed the polygons for index 0*/
-	cls_clay[0].sand=0.0;
-	cls_clay[0].clay=100.0;
-	cls_clay[0].silt=0.0;
-	cls_clay[1].sand=0.0;
-	cls_clay[1].clay=60.0;
-	cls_clay[1].silt=40.0;
-	cls_clay[2].sand=20.0;
-	cls_clay[2].clay=40.0;
-	cls_clay[2].silt=40.0;
-	cls_clay[3].sand=50.0;
-	cls_clay[3].clay=40.0;
-	cls_clay[3].silt=10.0;
-	cls_clay[4].sand=50.0;
-	cls_clay[4].clay=50.0;
-	cls_clay[4].silt=0.0;
-	/* Check for index 0*/
-/*	printf("in prct2tex(): check for index 0\n");*/
-	mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_clay[0].sand,cls_clay[0].clay,cls_clay[0].silt,cls_clay[1].sand,cls_clay[1].clay,cls_clay[1].silt,cls_clay[2].sand,cls_clay[2].clay,cls_clay[2].silt);
-	mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_clay[0].sand,cls_clay[0].clay,cls_clay[0].silt,cls_clay[2].sand,cls_clay[2].clay,cls_clay[2].silt,cls_clay[3].sand,cls_clay[3].clay,cls_clay[3].silt);
-	mark[2]=point_in_triangle(sand_input,clay_input,silt_input,cls_clay[0].sand,cls_clay[0].clay,cls_clay[0].silt,cls_clay[3].sand,cls_clay[3].clay,cls_clay[3].silt,cls_clay[4].sand,cls_clay[4].clay,cls_clay[4].silt);
-/*	printf("Clay: mark[0]=%f\n",mark[0]);*/
-/*	printf("Clay: mark[1]=%f\n",mark[1]);*/
-/*	printf("Clay: mark[2]=%f\n",mark[2]);*/
-	if(mark[0]==1||mark[1]==1||mark[2]==1){
-		index=0;
-/*		printf("Clay: index labelled as 0\n");*/
-	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 1*/
-		cls_sandy_clay[0].sand=50.0;
-		cls_sandy_clay[0].clay=50.0;
-		cls_sandy_clay[0].silt=0.0;
-		cls_sandy_clay[1].sand=50.0;
-		cls_sandy_clay[1].clay=35.0;
-		cls_sandy_clay[1].silt=15.0;
-		cls_sandy_clay[2].sand=65.0;
-		cls_sandy_clay[2].clay=35.0;
-		cls_sandy_clay[2].silt=0.0;
-		/* Check for index 1*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_clay[0].sand,cls_sandy_clay[0].clay,cls_sandy_clay[0].silt,cls_sandy_clay[1].sand,cls_sandy_clay[1].clay,cls_sandy_clay[1].silt,cls_sandy_clay[2].sand,cls_sandy_clay[2].clay,cls_sandy_clay[2].silt);
+    double temp;
 
-/*		printf("Sandy Clay: mark[0]=%f\n",mark[0]);*/
-		if(mark[0]==1){
-			index=1;
-/*			printf("Sandy Clay: index labelled as 1\n");*/
-		}
+    /* set up index for soil texture classes */
+    int index = 20;
+
+    /* set up mark index for inside/outside polygon check */
+    double mark[POLYGON_DIMENSION] = { 0.0 };
+    /*printf("in prct2tex()\n"); */
+    /*setup the 3Dvectors and initialize them */
+    /* index 0 */
+    struct vector cls_clay[POLYGON_DIMENSION] = { 0.0 };
+    /* index 1 */
+    struct vector cls_sandy_clay[POLYGON_DIMENSION] = { 0.0 };
+    /* index 2 */
+    struct vector cls_silty_clay[POLYGON_DIMENSION] = { 0.0 };
+    /* index 3 */
+    struct vector cls_sandy_clay_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 4 */
+    struct vector cls_clay_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 5 */
+    struct vector cls_silty_clay_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 6 */
+    struct vector cls_sand[POLYGON_DIMENSION] = { 0.0 };
+    /* index 7 */
+    struct vector cls_loamy_sand[POLYGON_DIMENSION] = { 0.0 };
+    /* index 8 */
+    struct vector cls_sandy_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 9 */
+    struct vector cls_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 10 */
+    struct vector cls_silt_loam[POLYGON_DIMENSION] = { 0.0 };
+    /* index 11 */
+    struct vector cls_silt[POLYGON_DIMENSION] = { 0.0 };
+
+    if ((sand_input + clay_input + silt_input) <= 10.0) {
+	sand_input = sand_input * 100.0;
+	clay_input = clay_input * 100.0;
+	silt_input = silt_input * 100.0;
+    }
+    /*printf("%5.3f||%5.3f||%5.3f|",sand_input,clay_input,silt_input); */
+
+    /*Feed the polygons for index 0 */
+    cls_clay[0].sand = 0.0;
+    cls_clay[0].clay = 100.0;
+    cls_clay[0].silt = 0.0;
+    cls_clay[1].sand = 0.0;
+    cls_clay[1].clay = 60.0;
+    cls_clay[1].silt = 40.0;
+    cls_clay[2].sand = 20.0;
+    cls_clay[2].clay = 40.0;
+    cls_clay[2].silt = 40.0;
+    cls_clay[3].sand = 50.0;
+    cls_clay[3].clay = 40.0;
+    cls_clay[3].silt = 10.0;
+    cls_clay[4].sand = 50.0;
+    cls_clay[4].clay = 50.0;
+    cls_clay[4].silt = 0.0;
+    /* Check for index 0 */
+    /*      printf("in prct2tex(): check for index 0\n"); */
+    mark[0] =
+	point_in_triangle(sand_input, clay_input, silt_input,
+			  cls_clay[0].sand, cls_clay[0].clay,
+			  cls_clay[0].silt, cls_clay[1].sand,
+			  cls_clay[1].clay, cls_clay[1].silt,
+			  cls_clay[2].sand, cls_clay[2].clay,
+			  cls_clay[2].silt);
+    mark[1] =
+	point_in_triangle(sand_input, clay_input, silt_input,
+			  cls_clay[0].sand, cls_clay[0].clay,
+			  cls_clay[0].silt, cls_clay[2].sand,
+			  cls_clay[2].clay, cls_clay[2].silt,
+			  cls_clay[3].sand, cls_clay[3].clay,
+			  cls_clay[3].silt);
+    mark[2] =
+	point_in_triangle(sand_input, clay_input, silt_input,
+			  cls_clay[0].sand, cls_clay[0].clay,
+			  cls_clay[0].silt, cls_clay[3].sand,
+			  cls_clay[3].clay, cls_clay[3].silt,
+			  cls_clay[4].sand, cls_clay[4].clay,
+			  cls_clay[4].silt);
+    /*      printf("Clay: mark[0]=%f\n",mark[0]); */
+    /*      printf("Clay: mark[1]=%f\n",mark[1]); */
+    /*      printf("Clay: mark[2]=%f\n",mark[2]); */
+    if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
+	index = 0;
+	/*              printf("Clay: index labelled as 0\n"); */
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 1 */
+	cls_sandy_clay[0].sand = 50.0;
+	cls_sandy_clay[0].clay = 50.0;
+	cls_sandy_clay[0].silt = 0.0;
+	cls_sandy_clay[1].sand = 50.0;
+	cls_sandy_clay[1].clay = 35.0;
+	cls_sandy_clay[1].silt = 15.0;
+	cls_sandy_clay[2].sand = 65.0;
+	cls_sandy_clay[2].clay = 35.0;
+	cls_sandy_clay[2].silt = 0.0;
+	/* Check for index 1 */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_clay[0].sand, cls_sandy_clay[0].clay,
+			      cls_sandy_clay[0].silt, cls_sandy_clay[1].sand,
+			      cls_sandy_clay[1].clay, cls_sandy_clay[1].silt,
+			      cls_sandy_clay[2].sand, cls_sandy_clay[2].clay,
+			      cls_sandy_clay[2].silt);
+
+	/*              printf("Sandy Clay: mark[0]=%f\n",mark[0]); */
+	if (mark[0] == 1) {
+	    index = 1;
+	    /*                      printf("Sandy Clay: index labelled as 1\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 2*/
-		cls_silty_clay[0].sand=0.0;
-		cls_silty_clay[0].clay=60.0;
-		cls_silty_clay[0].silt=40.0;
-		cls_silty_clay[1].sand=0.0;
-		cls_silty_clay[1].clay=40.0;
-		cls_silty_clay[1].silt=60.0;
-		cls_silty_clay[2].sand=20.0;
-		cls_silty_clay[2].clay=40.0;
-		cls_silty_clay[2].silt=40.0;
-		/* Check for index 2*/
-	/*	printf("sand=%5.3f||clay=%5.3f||silt=%5.3f\n",sand_input,clay_input,silt_input); */
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_silty_clay[0].sand,cls_silty_clay[0].clay,cls_silty_clay[0].silt,cls_silty_clay[1].sand,cls_silty_clay[1].clay,cls_silty_clay[1].silt,cls_silty_clay[2].sand,cls_silty_clay[2].clay,cls_silty_clay[2].silt);
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 2 */
+	cls_silty_clay[0].sand = 0.0;
+	cls_silty_clay[0].clay = 60.0;
+	cls_silty_clay[0].silt = 40.0;
+	cls_silty_clay[1].sand = 0.0;
+	cls_silty_clay[1].clay = 40.0;
+	cls_silty_clay[1].silt = 60.0;
+	cls_silty_clay[2].sand = 20.0;
+	cls_silty_clay[2].clay = 40.0;
+	cls_silty_clay[2].silt = 40.0;
+	/* Check for index 2 */
+	/*      printf("sand=%5.3f||clay=%5.3f||silt=%5.3f\n",sand_input,clay_input,silt_input); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silty_clay[0].sand, cls_silty_clay[0].clay,
+			      cls_silty_clay[0].silt, cls_silty_clay[1].sand,
+			      cls_silty_clay[1].clay, cls_silty_clay[1].silt,
+			      cls_silty_clay[2].sand, cls_silty_clay[2].clay,
+			      cls_silty_clay[2].silt);
 
-	/*	printf("Silty Clay: mark[0]=%f\n",mark[0]);*/
-		if(mark[0]==1){
-			index=2;
-	/*		printf("Silty Clay: index labelled as 2\n");*/
-		}
+	/*      printf("Silty Clay: mark[0]=%f\n",mark[0]); */
+	if (mark[0] == 1) {
+	    index = 2;
+	    /*              printf("Silty Clay: index labelled as 2\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 3*/
-		cls_sandy_clay_loam[0].sand=65.0;
-		cls_sandy_clay_loam[0].clay=35.0;
-		cls_sandy_clay_loam[0].silt=0.0;
-		cls_sandy_clay_loam[1].sand=50.0;
-		cls_sandy_clay_loam[1].clay=35.0;
-		cls_sandy_clay_loam[1].silt=15.0;
-		cls_sandy_clay_loam[2].sand=50.0;
-		cls_sandy_clay_loam[2].clay=30.0;
-		cls_sandy_clay_loam[2].silt=20.0;
-		cls_sandy_clay_loam[3].sand=55.0;
-		cls_sandy_clay_loam[3].clay=25.0;
-		cls_sandy_clay_loam[3].silt=20.0;
-		cls_sandy_clay_loam[4].sand=75.0;
-		cls_sandy_clay_loam[4].clay=25.0;
-		cls_sandy_clay_loam[4].silt=0.0;
-		/* Check for index 0*/
-/*		printf("in prct2tex(): check for index 3\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_clay_loam[0].sand,cls_sandy_clay_loam[0].clay,cls_sandy_clay_loam[0].silt,cls_sandy_clay_loam[1].sand,cls_sandy_clay_loam[1].clay,cls_sandy_clay_loam[1].silt,cls_sandy_clay_loam[2].sand,cls_sandy_clay_loam[2].clay,cls_sandy_clay_loam[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_clay_loam[0].sand,cls_sandy_clay_loam[0].clay,cls_sandy_clay_loam[0].silt,cls_sandy_clay_loam[2].sand,cls_sandy_clay_loam[2].clay,cls_sandy_clay_loam[2].silt,cls_sandy_clay_loam[3].sand,cls_sandy_clay_loam[3].clay,cls_sandy_clay_loam[3].silt);
-		mark[2]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_clay_loam[0].sand,cls_sandy_clay_loam[0].clay,cls_sandy_clay_loam[0].silt,cls_sandy_clay_loam[3].sand,cls_sandy_clay_loam[3].clay,cls_sandy_clay_loam[3].silt,cls_sandy_clay_loam[4].sand,cls_sandy_clay_loam[4].clay,cls_sandy_clay_loam[4].silt);
-/*		printf("Sandy Clay Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Sandy Clay Loam: mark[1]=%f\n",mark[1]);*/
-/*		printf("Sandy Clay Loam: mark[2]=%f\n",mark[2]);*/
-		if(mark[0]==1||mark[1]==1||mark[2]==1){
-			index=3;
-/*			printf("Sandy Clay Loam: index labelled as 3\n");*/
-		}
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 3 */
+	cls_sandy_clay_loam[0].sand = 65.0;
+	cls_sandy_clay_loam[0].clay = 35.0;
+	cls_sandy_clay_loam[0].silt = 0.0;
+	cls_sandy_clay_loam[1].sand = 50.0;
+	cls_sandy_clay_loam[1].clay = 35.0;
+	cls_sandy_clay_loam[1].silt = 15.0;
+	cls_sandy_clay_loam[2].sand = 50.0;
+	cls_sandy_clay_loam[2].clay = 30.0;
+	cls_sandy_clay_loam[2].silt = 20.0;
+	cls_sandy_clay_loam[3].sand = 55.0;
+	cls_sandy_clay_loam[3].clay = 25.0;
+	cls_sandy_clay_loam[3].silt = 20.0;
+	cls_sandy_clay_loam[4].sand = 75.0;
+	cls_sandy_clay_loam[4].clay = 25.0;
+	cls_sandy_clay_loam[4].silt = 0.0;
+	/* Check for index 0 */
+	/*              printf("in prct2tex(): check for index 3\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_clay_loam[0].sand,
+			      cls_sandy_clay_loam[0].clay,
+			      cls_sandy_clay_loam[0].silt,
+			      cls_sandy_clay_loam[1].sand,
+			      cls_sandy_clay_loam[1].clay,
+			      cls_sandy_clay_loam[1].silt,
+			      cls_sandy_clay_loam[2].sand,
+			      cls_sandy_clay_loam[2].clay,
+			      cls_sandy_clay_loam[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_clay_loam[0].sand,
+			      cls_sandy_clay_loam[0].clay,
+			      cls_sandy_clay_loam[0].silt,
+			      cls_sandy_clay_loam[2].sand,
+			      cls_sandy_clay_loam[2].clay,
+			      cls_sandy_clay_loam[2].silt,
+			      cls_sandy_clay_loam[3].sand,
+			      cls_sandy_clay_loam[3].clay,
+			      cls_sandy_clay_loam[3].silt);
+	mark[2] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_clay_loam[0].sand,
+			      cls_sandy_clay_loam[0].clay,
+			      cls_sandy_clay_loam[0].silt,
+			      cls_sandy_clay_loam[3].sand,
+			      cls_sandy_clay_loam[3].clay,
+			      cls_sandy_clay_loam[3].silt,
+			      cls_sandy_clay_loam[4].sand,
+			      cls_sandy_clay_loam[4].clay,
+			      cls_sandy_clay_loam[4].silt);
+	/*              printf("Sandy Clay Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Sandy Clay Loam: mark[1]=%f\n",mark[1]); */
+	/*              printf("Sandy Clay Loam: mark[2]=%f\n",mark[2]); */
+	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
+	    index = 3;
+	    /*                      printf("Sandy Clay Loam: index labelled as 3\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 4*/
-		cls_clay_loam[0].sand=20.0;
-		cls_clay_loam[0].clay=40.0;
-		cls_clay_loam[0].silt=40.0;
-		cls_clay_loam[1].sand=20.0;
-		cls_clay_loam[1].clay=30.0;
-		cls_clay_loam[1].silt=50.0;
-		cls_clay_loam[2].sand=50.0;
-		cls_clay_loam[2].clay=30.0;
-		cls_clay_loam[2].silt=20.0;
-		cls_clay_loam[3].sand=10.0;
-		cls_clay_loam[3].clay=50.0;
-		cls_clay_loam[3].silt=40.0;
-		/* Check for index 4*/
-/*		printf("in prct2tex(): check for index 4\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_clay_loam[0].sand,cls_clay_loam[0].clay,cls_clay_loam[0].silt,cls_clay_loam[1].sand,cls_clay_loam[1].clay,cls_clay_loam[1].silt,cls_clay_loam[2].sand,cls_clay_loam[2].clay,cls_clay_loam[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_clay_loam[0].sand,cls_clay_loam[0].clay,cls_clay_loam[0].silt,cls_clay_loam[2].sand,cls_clay_loam[2].clay,cls_clay_loam[2].silt,cls_clay_loam[3].sand,cls_clay_loam[3].clay,cls_clay_loam[3].silt);
-/*		printf("Clay Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Clay Loam: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1){
-			index=4;
-/*			printf("Clay Loam: index labelled as 4\n");*/
-		}
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 4 */
+	cls_clay_loam[0].sand = 20.0;
+	cls_clay_loam[0].clay = 40.0;
+	cls_clay_loam[0].silt = 40.0;
+	cls_clay_loam[1].sand = 20.0;
+	cls_clay_loam[1].clay = 30.0;
+	cls_clay_loam[1].silt = 50.0;
+	cls_clay_loam[2].sand = 50.0;
+	cls_clay_loam[2].clay = 30.0;
+	cls_clay_loam[2].silt = 20.0;
+	cls_clay_loam[3].sand = 10.0;
+	cls_clay_loam[3].clay = 50.0;
+	cls_clay_loam[3].silt = 40.0;
+	/* Check for index 4 */
+	/*              printf("in prct2tex(): check for index 4\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_clay_loam[0].sand, cls_clay_loam[0].clay,
+			      cls_clay_loam[0].silt, cls_clay_loam[1].sand,
+			      cls_clay_loam[1].clay, cls_clay_loam[1].silt,
+			      cls_clay_loam[2].sand, cls_clay_loam[2].clay,
+			      cls_clay_loam[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_clay_loam[0].sand, cls_clay_loam[0].clay,
+			      cls_clay_loam[0].silt, cls_clay_loam[2].sand,
+			      cls_clay_loam[2].clay, cls_clay_loam[2].silt,
+			      cls_clay_loam[3].sand, cls_clay_loam[3].clay,
+			      cls_clay_loam[3].silt);
+	/*              printf("Clay Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Clay Loam: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1) {
+	    index = 4;
+	    /*                      printf("Clay Loam: index labelled as 4\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 5*/
-		cls_silty_clay_loam[0].sand=0.0;
-		cls_silty_clay_loam[0].clay=40.0;
-		cls_silty_clay_loam[0].silt=60.0;
-		cls_silty_clay_loam[1].sand=0.0;
-		cls_silty_clay_loam[1].clay=30.0;
-		cls_silty_clay_loam[1].silt=70.0;
-		cls_silty_clay_loam[2].sand=20.0;
-		cls_silty_clay_loam[2].clay=30.0;
-		cls_silty_clay_loam[2].silt=50.0;
-		cls_silty_clay_loam[3].sand=20.0;
-		cls_silty_clay_loam[3].clay=40.0;
-		cls_silty_clay_loam[3].silt=40.0;
-		/* Check for index 5*/
-/*		printf("in prct2tex(): check for index 5\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_silty_clay_loam[0].sand,cls_silty_clay_loam[0].clay,cls_silty_clay_loam[0].silt,cls_silty_clay_loam[1].sand,cls_silty_clay_loam[1].clay,cls_silty_clay_loam[1].silt,cls_silty_clay_loam[2].sand,cls_silty_clay_loam[2].clay,cls_silty_clay_loam[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_silty_clay_loam[0].sand,cls_silty_clay_loam[0].clay,cls_silty_clay_loam[0].silt,cls_silty_clay_loam[2].sand,cls_silty_clay_loam[2].clay,cls_silty_clay_loam[2].silt,cls_silty_clay_loam[3].sand,cls_silty_clay_loam[3].clay,cls_silty_clay_loam[3].silt);
-/*		printf("Silty Clay Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Silty Clay Loam: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1){
-			index=5;
-/*			printf("Silty Clay Loam: index labelled as 5\n");*/
-		}
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 5 */
+	cls_silty_clay_loam[0].sand = 0.0;
+	cls_silty_clay_loam[0].clay = 40.0;
+	cls_silty_clay_loam[0].silt = 60.0;
+	cls_silty_clay_loam[1].sand = 0.0;
+	cls_silty_clay_loam[1].clay = 30.0;
+	cls_silty_clay_loam[1].silt = 70.0;
+	cls_silty_clay_loam[2].sand = 20.0;
+	cls_silty_clay_loam[2].clay = 30.0;
+	cls_silty_clay_loam[2].silt = 50.0;
+	cls_silty_clay_loam[3].sand = 20.0;
+	cls_silty_clay_loam[3].clay = 40.0;
+	cls_silty_clay_loam[3].silt = 40.0;
+	/* Check for index 5 */
+	/*              printf("in prct2tex(): check for index 5\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silty_clay_loam[0].sand,
+			      cls_silty_clay_loam[0].clay,
+			      cls_silty_clay_loam[0].silt,
+			      cls_silty_clay_loam[1].sand,
+			      cls_silty_clay_loam[1].clay,
+			      cls_silty_clay_loam[1].silt,
+			      cls_silty_clay_loam[2].sand,
+			      cls_silty_clay_loam[2].clay,
+			      cls_silty_clay_loam[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silty_clay_loam[0].sand,
+			      cls_silty_clay_loam[0].clay,
+			      cls_silty_clay_loam[0].silt,
+			      cls_silty_clay_loam[2].sand,
+			      cls_silty_clay_loam[2].clay,
+			      cls_silty_clay_loam[2].silt,
+			      cls_silty_clay_loam[3].sand,
+			      cls_silty_clay_loam[3].clay,
+			      cls_silty_clay_loam[3].silt);
+	/*              printf("Silty Clay Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Silty Clay Loam: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1) {
+	    index = 5;
+	    /*                      printf("Silty Clay Loam: index labelled as 5\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 6*/
-		cls_sand[0].sand=85.0;
-		cls_sand[0].clay=15.0;
-		cls_sand[0].silt=0.0;
-		cls_sand[1].sand=85.0;
-		cls_sand[1].clay=0.0;
-		cls_sand[1].silt=15.0;
-		cls_sand[2].sand=100.0;
-		cls_sand[2].clay=0.0;
-		cls_sand[2].silt=0.0;
-		/* Check for index 6*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_sand[0].sand,cls_sand[0].clay,cls_sand[0].silt,cls_sand[1].sand,cls_sand[1].clay,cls_sand[1].silt,cls_sand[2].sand,cls_sand[2].clay,cls_sand[2].silt);
-/*		printf("Sand: mark[0]=%f\n",mark[0]);*/
-		if(mark[0]==1){
-			index=6;
-/*			printf("Sand: index labelled as 6\n");*/
-		}
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 6 */
+	cls_sand[0].sand = 85.0;
+	cls_sand[0].clay = 15.0;
+	cls_sand[0].silt = 0.0;
+	cls_sand[1].sand = 85.0;
+	cls_sand[1].clay = 0.0;
+	cls_sand[1].silt = 15.0;
+	cls_sand[2].sand = 100.0;
+	cls_sand[2].clay = 0.0;
+	cls_sand[2].silt = 0.0;
+	/* Check for index 6 */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sand[0].sand, cls_sand[0].clay,
+			      cls_sand[0].silt, cls_sand[1].sand,
+			      cls_sand[1].clay, cls_sand[1].silt,
+			      cls_sand[2].sand, cls_sand[2].clay,
+			      cls_sand[2].silt);
+	/*              printf("Sand: mark[0]=%f\n",mark[0]); */
+	if (mark[0] == 1) {
+	    index = 6;
+	    /*                      printf("Sand: index labelled as 6\n"); */
 	}
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 7*/
-		cls_loamy_sand[0].sand=80.0;
-		cls_loamy_sand[0].clay=20.0;
-		cls_loamy_sand[0].silt=0.0;
-		cls_loamy_sand[1].sand=70.0;
-		cls_loamy_sand[1].clay=0.0;
-		cls_loamy_sand[1].silt=30.0;
-		cls_loamy_sand[2].sand=85.0;
-		cls_loamy_sand[2].clay=0.0;
-		cls_loamy_sand[2].silt=15.0;
-		cls_loamy_sand[3].sand=85.0;
-		cls_loamy_sand[3].clay=15.0;
-		cls_loamy_sand[3].silt=0.0;
-		/* Check for index 7*/
-/*		printf("in prct2tex(): check for index 7\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_loamy_sand[0].sand,cls_loamy_sand[0].clay,cls_loamy_sand[0].silt,cls_loamy_sand[1].sand,cls_loamy_sand[1].clay,cls_loamy_sand[1].silt,cls_loamy_sand[2].sand,cls_loamy_sand[2].clay,cls_loamy_sand[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_loamy_sand[0].sand,cls_loamy_sand[0].clay,cls_loamy_sand[0].silt,cls_loamy_sand[2].sand,cls_loamy_sand[2].clay,cls_loamy_sand[2].silt,cls_loamy_sand[3].sand,cls_loamy_sand[3].clay,cls_loamy_sand[3].silt);
-/*		printf("Loamy Sand: mark[0]=%f\n",mark[0]);*/
-/*		printf("Loamy Sand: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1){
-			index=7;
-/*			printf("Loamy Sand: index labelled as 7\n");*/
-		}
+    }
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 7 */
+	cls_loamy_sand[0].sand = 80.0;
+	cls_loamy_sand[0].clay = 20.0;
+	cls_loamy_sand[0].silt = 0.0;
+	cls_loamy_sand[1].sand = 70.0;
+	cls_loamy_sand[1].clay = 0.0;
+	cls_loamy_sand[1].silt = 30.0;
+	cls_loamy_sand[2].sand = 85.0;
+	cls_loamy_sand[2].clay = 0.0;
+	cls_loamy_sand[2].silt = 15.0;
+	cls_loamy_sand[3].sand = 85.0;
+	cls_loamy_sand[3].clay = 15.0;
+	cls_loamy_sand[3].silt = 0.0;
+	/* Check for index 7 */
+	/*              printf("in prct2tex(): check for index 7\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_loamy_sand[0].sand, cls_loamy_sand[0].clay,
+			      cls_loamy_sand[0].silt, cls_loamy_sand[1].sand,
+			      cls_loamy_sand[1].clay, cls_loamy_sand[1].silt,
+			      cls_loamy_sand[2].sand, cls_loamy_sand[2].clay,
+			      cls_loamy_sand[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_loamy_sand[0].sand, cls_loamy_sand[0].clay,
+			      cls_loamy_sand[0].silt, cls_loamy_sand[2].sand,
+			      cls_loamy_sand[2].clay, cls_loamy_sand[2].silt,
+			      cls_loamy_sand[3].sand, cls_loamy_sand[3].clay,
+			      cls_loamy_sand[3].silt);
+	/*              printf("Loamy Sand: mark[0]=%f\n",mark[0]); */
+	/*              printf("Loamy Sand: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1) {
+	    index = 7;
+	    /*                      printf("Loamy Sand: index labelled as 7\n"); */
 	}
+    }
 
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 8*/
-		cls_sandy_loam[0].sand=75.0;
-		cls_sandy_loam[0].clay=25.0;
-		cls_sandy_loam[0].silt=0.0;
-		cls_sandy_loam[1].sand=55.0;
-		cls_sandy_loam[1].clay=25.0;
-		cls_sandy_loam[1].silt=20.0;
-		cls_sandy_loam[2].sand=55.0;
-		cls_sandy_loam[2].clay=10.0;
-		cls_sandy_loam[2].silt=35.0;
-		cls_sandy_loam[3].sand=40.0;
-		cls_sandy_loam[3].clay=10.0;
-		cls_sandy_loam[3].silt=50.0;
-		cls_sandy_loam[4].sand=50.0;
-		cls_sandy_loam[4].clay=0.0;
-		cls_sandy_loam[4].silt=50.0;
-		cls_sandy_loam[5].sand=70.0;
-		cls_sandy_loam[5].clay=0.0;
-		cls_sandy_loam[5].silt=30.0;
-		cls_sandy_loam[6].sand=80.0;
-		cls_sandy_loam[6].clay=20.0;
-		cls_sandy_loam[6].silt=0.0;
-		/* Check for index 8*/
-/*		printf("in prct2tex(): check for index 8\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_loam[2].sand,cls_sandy_loam[2].clay,cls_sandy_loam[2].silt,cls_sandy_loam[3].sand,cls_sandy_loam[3].clay,cls_sandy_loam[3].silt,cls_sandy_loam[4].sand,cls_sandy_loam[4].clay,cls_sandy_loam[4].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_loam[2].sand,cls_sandy_loam[2].clay,cls_sandy_loam[2].silt,cls_sandy_loam[4].sand,cls_sandy_loam[4].clay,cls_sandy_loam[4].silt,cls_sandy_loam[5].sand,cls_sandy_loam[5].clay,cls_sandy_loam[5].silt);
-		mark[2]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_loam[2].sand,cls_sandy_loam[2].clay,cls_sandy_loam[2].silt,cls_sandy_loam[5].sand,cls_sandy_loam[5].clay,cls_sandy_loam[5].silt,cls_sandy_loam[6].sand,cls_sandy_loam[6].clay,cls_sandy_loam[6].silt);
-		mark[3]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_loam[2].sand,cls_sandy_loam[2].clay,cls_sandy_loam[2].silt,cls_sandy_loam[6].sand,cls_sandy_loam[6].clay,cls_sandy_loam[6].silt,cls_sandy_loam[0].sand,cls_sandy_loam[0].clay,cls_sandy_loam[0].silt);
-		mark[4]=point_in_triangle(sand_input,clay_input,silt_input,cls_sandy_loam[2].sand,cls_sandy_loam[2].clay,cls_sandy_loam[2].silt,cls_sandy_loam[0].sand,cls_sandy_loam[0].clay,cls_sandy_loam[0].silt,cls_sandy_loam[1].sand,cls_sandy_loam[1].clay,cls_sandy_loam[1].silt);
-/*		printf("Sandy Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Sandy Loam: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1||mark[2]==1||mark[3]==1||mark[4]==1){
-			index=8;
-/*			printf("Sandy Loam: index labelled as 8\n");*/
-		}
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 8 */
+	cls_sandy_loam[0].sand = 75.0;
+	cls_sandy_loam[0].clay = 25.0;
+	cls_sandy_loam[0].silt = 0.0;
+	cls_sandy_loam[1].sand = 55.0;
+	cls_sandy_loam[1].clay = 25.0;
+	cls_sandy_loam[1].silt = 20.0;
+	cls_sandy_loam[2].sand = 55.0;
+	cls_sandy_loam[2].clay = 10.0;
+	cls_sandy_loam[2].silt = 35.0;
+	cls_sandy_loam[3].sand = 40.0;
+	cls_sandy_loam[3].clay = 10.0;
+	cls_sandy_loam[3].silt = 50.0;
+	cls_sandy_loam[4].sand = 50.0;
+	cls_sandy_loam[4].clay = 0.0;
+	cls_sandy_loam[4].silt = 50.0;
+	cls_sandy_loam[5].sand = 70.0;
+	cls_sandy_loam[5].clay = 0.0;
+	cls_sandy_loam[5].silt = 30.0;
+	cls_sandy_loam[6].sand = 80.0;
+	cls_sandy_loam[6].clay = 20.0;
+	cls_sandy_loam[6].silt = 0.0;
+	/* Check for index 8 */
+	/*              printf("in prct2tex(): check for index 8\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
+			      cls_sandy_loam[2].silt, cls_sandy_loam[3].sand,
+			      cls_sandy_loam[3].clay, cls_sandy_loam[3].silt,
+			      cls_sandy_loam[4].sand, cls_sandy_loam[4].clay,
+			      cls_sandy_loam[4].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
+			      cls_sandy_loam[2].silt, cls_sandy_loam[4].sand,
+			      cls_sandy_loam[4].clay, cls_sandy_loam[4].silt,
+			      cls_sandy_loam[5].sand, cls_sandy_loam[5].clay,
+			      cls_sandy_loam[5].silt);
+	mark[2] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
+			      cls_sandy_loam[2].silt, cls_sandy_loam[5].sand,
+			      cls_sandy_loam[5].clay, cls_sandy_loam[5].silt,
+			      cls_sandy_loam[6].sand, cls_sandy_loam[6].clay,
+			      cls_sandy_loam[6].silt);
+	mark[3] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
+			      cls_sandy_loam[2].silt, cls_sandy_loam[6].sand,
+			      cls_sandy_loam[6].clay, cls_sandy_loam[6].silt,
+			      cls_sandy_loam[0].sand, cls_sandy_loam[0].clay,
+			      cls_sandy_loam[0].silt);
+	mark[4] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
+			      cls_sandy_loam[2].silt, cls_sandy_loam[0].sand,
+			      cls_sandy_loam[0].clay, cls_sandy_loam[0].silt,
+			      cls_sandy_loam[1].sand, cls_sandy_loam[1].clay,
+			      cls_sandy_loam[1].silt);
+	/*              printf("Sandy Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Sandy Loam: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1 || mark[3] == 1 ||
+	    mark[4] == 1) {
+	    index = 8;
+	    /*                      printf("Sandy Loam: index labelled as 8\n"); */
 	}
+    }
 
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 9*/
-		cls_loam[0].sand=50.0;
-		cls_loam[0].clay=30.0;
-		cls_loam[0].silt=20.0;
-		cls_loam[1].sand=20.0;
-		cls_loam[1].clay=30.0;
-		cls_loam[1].silt=50.0;
-		cls_loam[2].sand=40.0;
-		cls_loam[2].clay=10.0;
-		cls_loam[2].silt=50.0;
-		cls_loam[3].sand=55.0;
-		cls_loam[3].clay=10.0;
-		cls_loam[3].silt=35.0;
-		cls_loam[4].sand=55.0;
-		cls_loam[4].clay=25.0;
-		cls_loam[4].silt=15.0;
-		/* Check for index 9*/
-/*		printf("in prct2tex(): check for index 9\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_loam[0].sand,cls_loam[0].clay,cls_loam[0].silt,cls_loam[1].sand,cls_loam[1].clay,cls_loam[1].silt,cls_loam[2].sand,cls_loam[2].clay,cls_loam[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_loam[0].sand,cls_loam[0].clay,cls_loam[0].silt,cls_loam[2].sand,cls_loam[2].clay,cls_loam[2].silt,cls_loam[3].sand,cls_loam[3].clay,cls_loam[3].silt);
-		mark[2]=point_in_triangle(sand_input,clay_input,silt_input,cls_loam[0].sand,cls_loam[0].clay,cls_loam[0].silt,cls_loam[3].sand,cls_loam[3].clay,cls_loam[3].silt,cls_loam[4].sand,cls_loam[4].clay,cls_loam[4].silt);
-/*		printf("Sandy Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Sandy Loam: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1||mark[2]==1){
-			index=9;
-/*			printf("Loam: index labelled as 9\n");*/
-		}
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 9 */
+	cls_loam[0].sand = 50.0;
+	cls_loam[0].clay = 30.0;
+	cls_loam[0].silt = 20.0;
+	cls_loam[1].sand = 20.0;
+	cls_loam[1].clay = 30.0;
+	cls_loam[1].silt = 50.0;
+	cls_loam[2].sand = 40.0;
+	cls_loam[2].clay = 10.0;
+	cls_loam[2].silt = 50.0;
+	cls_loam[3].sand = 55.0;
+	cls_loam[3].clay = 10.0;
+	cls_loam[3].silt = 35.0;
+	cls_loam[4].sand = 55.0;
+	cls_loam[4].clay = 25.0;
+	cls_loam[4].silt = 15.0;
+	/* Check for index 9 */
+	/*              printf("in prct2tex(): check for index 9\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_loam[0].sand, cls_loam[0].clay,
+			      cls_loam[0].silt, cls_loam[1].sand,
+			      cls_loam[1].clay, cls_loam[1].silt,
+			      cls_loam[2].sand, cls_loam[2].clay,
+			      cls_loam[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_loam[0].sand, cls_loam[0].clay,
+			      cls_loam[0].silt, cls_loam[2].sand,
+			      cls_loam[2].clay, cls_loam[2].silt,
+			      cls_loam[3].sand, cls_loam[3].clay,
+			      cls_loam[3].silt);
+	mark[2] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_loam[0].sand, cls_loam[0].clay,
+			      cls_loam[0].silt, cls_loam[3].sand,
+			      cls_loam[3].clay, cls_loam[3].silt,
+			      cls_loam[4].sand, cls_loam[4].clay,
+			      cls_loam[4].silt);
+	/*              printf("Sandy Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Sandy Loam: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
+	    index = 9;
+	    /*                      printf("Loam: index labelled as 9\n"); */
 	}
+    }
 
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 10*/
-		cls_silt_loam[0].sand=20.0;
-		cls_silt_loam[0].clay=30.0;
-		cls_silt_loam[0].silt=50.0;
-		cls_silt_loam[1].sand=0.0;
-		cls_silt_loam[1].clay=30.0;
-		cls_silt_loam[1].silt=70.0;
-		cls_silt_loam[2].sand=0.0;
-		cls_silt_loam[2].clay=10.0;
-		cls_silt_loam[2].silt=90.0;
-		cls_silt_loam[3].sand=15.0;
-		cls_silt_loam[3].clay=10.0;
-		cls_silt_loam[3].silt=75.0;
-		cls_silt_loam[4].sand=25.0;
-		cls_silt_loam[4].clay=0.0;
-		cls_silt_loam[4].silt=75.0;
-		cls_silt_loam[5].sand=50.0;
-		cls_silt_loam[5].clay=0.0;
-		cls_silt_loam[5].silt=50.0;
-		/* Check for index 10*/
-/*		printf("in prct2tex(): check for index 10\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt_loam[3].sand,cls_silt_loam[3].clay,cls_silt_loam[3].silt,cls_silt_loam[4].sand,cls_silt_loam[4].clay,cls_silt_loam[4].silt,cls_silt_loam[5].sand,cls_silt_loam[5].clay,cls_silt_loam[5].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt_loam[3].sand,cls_silt_loam[3].clay,cls_silt_loam[3].silt,cls_silt_loam[5].sand,cls_silt_loam[5].clay,cls_silt_loam[5].silt,cls_silt_loam[0].sand,cls_silt_loam[0].clay,cls_silt_loam[0].silt);
-		mark[2]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt_loam[3].sand,cls_silt_loam[3].clay,cls_silt_loam[3].silt,cls_silt_loam[0].sand,cls_silt_loam[0].clay,cls_silt_loam[0].silt,cls_silt_loam[1].sand,cls_silt_loam[1].clay,cls_silt_loam[1].silt);
-		mark[3]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt_loam[3].sand,cls_silt_loam[3].clay,cls_silt_loam[3].silt,cls_silt_loam[1].sand,cls_silt_loam[1].clay,cls_silt_loam[1].silt,cls_silt_loam[2].sand,cls_silt_loam[2].clay,cls_silt_loam[2].silt);
-/*		printf("Silt Loam: mark[0]=%f\n",mark[0]);*/
-/*		printf("Silt Loam: mark[1]=%f\n",mark[1]);*/
-/*		printf("Silt Loam: mark[2]=%f\n",mark[2]);*/
-/*		printf("Silt Loam: mark[3]=%f\n",mark[3]);*/
-		if(mark[0]==1||mark[1]==1||mark[2]==1||mark[3]==1){
-			index=10;
-/*			printf("Silt Loam: index labelled as 10\n");*/
-		}
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 10 */
+	cls_silt_loam[0].sand = 20.0;
+	cls_silt_loam[0].clay = 30.0;
+	cls_silt_loam[0].silt = 50.0;
+	cls_silt_loam[1].sand = 0.0;
+	cls_silt_loam[1].clay = 30.0;
+	cls_silt_loam[1].silt = 70.0;
+	cls_silt_loam[2].sand = 0.0;
+	cls_silt_loam[2].clay = 10.0;
+	cls_silt_loam[2].silt = 90.0;
+	cls_silt_loam[3].sand = 15.0;
+	cls_silt_loam[3].clay = 10.0;
+	cls_silt_loam[3].silt = 75.0;
+	cls_silt_loam[4].sand = 25.0;
+	cls_silt_loam[4].clay = 0.0;
+	cls_silt_loam[4].silt = 75.0;
+	cls_silt_loam[5].sand = 50.0;
+	cls_silt_loam[5].clay = 0.0;
+	cls_silt_loam[5].silt = 50.0;
+	/* Check for index 10 */
+	/*              printf("in prct2tex(): check for index 10\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
+			      cls_silt_loam[3].silt, cls_silt_loam[4].sand,
+			      cls_silt_loam[4].clay, cls_silt_loam[4].silt,
+			      cls_silt_loam[5].sand, cls_silt_loam[5].clay,
+			      cls_silt_loam[5].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
+			      cls_silt_loam[3].silt, cls_silt_loam[5].sand,
+			      cls_silt_loam[5].clay, cls_silt_loam[5].silt,
+			      cls_silt_loam[0].sand, cls_silt_loam[0].clay,
+			      cls_silt_loam[0].silt);
+	mark[2] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
+			      cls_silt_loam[3].silt, cls_silt_loam[0].sand,
+			      cls_silt_loam[0].clay, cls_silt_loam[0].silt,
+			      cls_silt_loam[1].sand, cls_silt_loam[1].clay,
+			      cls_silt_loam[1].silt);
+	mark[3] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
+			      cls_silt_loam[3].silt, cls_silt_loam[1].sand,
+			      cls_silt_loam[1].clay, cls_silt_loam[1].silt,
+			      cls_silt_loam[2].sand, cls_silt_loam[2].clay,
+			      cls_silt_loam[2].silt);
+	/*              printf("Silt Loam: mark[0]=%f\n",mark[0]); */
+	/*              printf("Silt Loam: mark[1]=%f\n",mark[1]); */
+	/*              printf("Silt Loam: mark[2]=%f\n",mark[2]); */
+	/*              printf("Silt Loam: mark[3]=%f\n",mark[3]); */
+	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1 || mark[3] == 1) {
+	    index = 10;
+	    /*                      printf("Silt Loam: index labelled as 10\n"); */
 	}
+    }
 
-	if (index==20){/* if index not found then continue*/
-		/*Feed the polygons for index 11*/
-		cls_silt[0].sand=15.0;
-		cls_silt[0].clay=10.0;
-		cls_silt[0].silt=75.0;
-		cls_silt[1].sand=0.0;
-		cls_silt[1].clay=10.0;
-		cls_silt[1].silt=90.0;
-		cls_silt[2].sand=0.0;
-		cls_silt[2].clay=0.0;
-		cls_silt[2].silt=100.0;
-		cls_silt[3].sand=25.0;
-		cls_silt[3].clay=0.0;
-		cls_silt[3].silt=75.0;
-		/* Check for index 11*/
-/*		printf("in prct2tex(): check for index 11\n");*/
-		mark[0]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt[0].sand,cls_silt[0].clay,cls_silt[0].silt,cls_silt[1].sand,cls_silt[1].clay,cls_silt[1].silt,cls_silt[2].sand,cls_silt[2].clay,cls_silt[2].silt);
-		mark[1]=point_in_triangle(sand_input,clay_input,silt_input,cls_silt[0].sand,cls_silt[0].clay,cls_silt[0].silt,cls_silt[2].sand,cls_silt[2].clay,cls_silt[2].silt,cls_silt[3].sand,cls_silt[3].clay,cls_silt[3].silt);
-/*		printf("Silt: mark[0]=%f\n",mark[0]);*/
-/*		printf("Silt: mark[1]=%f\n",mark[1]);*/
-		if(mark[0]==1||mark[1]==1){
-			index=11;
-/*			printf("Silt: index labelled as 11\n");*/
-		}
+    if (index == 20) {		/* if index not found then continue */
+	/*Feed the polygons for index 11 */
+	cls_silt[0].sand = 15.0;
+	cls_silt[0].clay = 10.0;
+	cls_silt[0].silt = 75.0;
+	cls_silt[1].sand = 0.0;
+	cls_silt[1].clay = 10.0;
+	cls_silt[1].silt = 90.0;
+	cls_silt[2].sand = 0.0;
+	cls_silt[2].clay = 0.0;
+	cls_silt[2].silt = 100.0;
+	cls_silt[3].sand = 25.0;
+	cls_silt[3].clay = 0.0;
+	cls_silt[3].silt = 75.0;
+	/* Check for index 11 */
+	/*              printf("in prct2tex(): check for index 11\n"); */
+	mark[0] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt[0].sand, cls_silt[0].clay,
+			      cls_silt[0].silt, cls_silt[1].sand,
+			      cls_silt[1].clay, cls_silt[1].silt,
+			      cls_silt[2].sand, cls_silt[2].clay,
+			      cls_silt[2].silt);
+	mark[1] =
+	    point_in_triangle(sand_input, clay_input, silt_input,
+			      cls_silt[0].sand, cls_silt[0].clay,
+			      cls_silt[0].silt, cls_silt[2].sand,
+			      cls_silt[2].clay, cls_silt[2].silt,
+			      cls_silt[3].sand, cls_silt[3].clay,
+			      cls_silt[3].silt);
+	/*              printf("Silt: mark[0]=%f\n",mark[0]); */
+	/*              printf("Silt: mark[1]=%f\n",mark[1]); */
+	if (mark[0] == 1 || mark[1] == 1) {
+	    index = 11;
+	    /*                      printf("Silt: index labelled as 11\n"); */
 	}
-/*	if(index==0){
-		printf("clay\n");
-	} else if (index==1){
-		printf("sandy clay\n");
-	} else if (index==2){
-		printf("silty clay\n");
-	} else if (index==3){
-		printf("sandy clay loam\n");
-	} else if (index==4){
-		printf("clay loam\n");
-	} else if (index==5){
-		printf("silty clay loam\n");
-	} else if (index==6){
-		printf("sand\n");
-	} else if (index==7){
-		printf("loamy sand\n");
-	} else if (index==8){
-		printf("sandy loam\n");
-	} else if (index==9){
-		printf("loam\n");
-	} else if (index==10){
-		printf("silt loam\n");
-	} else if (index==11){
-		printf("silt\n");
-	} else {
-		printf("i am confused here...Can you do it yourself please?\n");
-	}*/
-	return index;
+    }
+    /*      if(index==0){
+       printf("clay\n");
+       } else if (index==1){
+       printf("sandy clay\n");
+       } else if (index==2){
+       printf("silty clay\n");
+       } else if (index==3){
+       printf("sandy clay loam\n");
+       } else if (index==4){
+       printf("clay loam\n");
+       } else if (index==5){
+       printf("silty clay loam\n");
+       } else if (index==6){
+       printf("sand\n");
+       } else if (index==7){
+       printf("loamy sand\n");
+       } else if (index==8){
+       printf("sandy loam\n");
+       } else if (index==9){
+       printf("loam\n");
+       } else if (index==10){
+       printf("silt loam\n");
+       } else if (index==11){
+       printf("silt\n");
+       } else {
+       printf("i am confused here...Can you do it yourself please?\n");
+       } */
+    return index;
 }
-

Modified: grass-addons/gipe/r.uslek/prct2tex.h
===================================================================
--- grass-addons/gipe/r.uslek/prct2tex.h	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.uslek/prct2tex.h	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,3 +1,7 @@
 
 int prct2tex(double sand_input, double clay_input, double silt_input);
-double point_in_triangle(double point_x, double point_y, double point_z, double t1_x, double t1_y, double t1_z, double t2_x, double t2_y, double t2_z, double t3_x, double t3_y, double t3_z);
+
+double point_in_triangle(double point_x, double point_y, double point_z,
+			 double t1_x, double t1_y, double t1_z, double t2_x,
+			 double t2_y, double t2_z, double t3_x, double t3_y,
+			 double t3_z);

Modified: grass-addons/gipe/r.uslek/tex2usle_k.c
===================================================================
--- grass-addons/gipe/r.uslek/tex2usle_k.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.uslek/tex2usle_k.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,216 +1,281 @@
 #include<stdio.h>
 
-/* From FAOSOIL CD, after USDA 1951, p209*/
+/* From FAOSOIL CD, after USDA 1951, p209 */
 
-double tex2usle_k(int texture, double om_in){
-	double usle_k=200.0;
-	/*printf("texture=%i, om=%5.3f",texture, om_in);*/
-	if(om_in<0.5){
-		if(texture==0){
-/*			printf("clay\n");*/
-			usle_k=0.29;/*Took max value @0.2*/
-		} else if (texture==1){
-/*			printf("sandy clay\n");*/
-			usle_k=0.14;
-		} else if (texture==2){
-/*			printf("silty clay\n");*/
-			usle_k=0.25;
-		} else if (texture==3){
-/*			printf("sandy clay loam\n");*/
-			usle_k=0.27;
-		} else if (texture==4){
-/*			printf("clay loam\n");*/
-			usle_k=0.28;
-		} else if (texture==5){
-/*			printf("silty clay loam\n");*/
-			usle_k=0.37;
-		} else if (texture==6){
-/*			printf("sand\n");*/
-			usle_k=0.05;
-		} else if (texture==7){
-/*			printf("loamy sand\n");*/
-			usle_k=0.12;
-		} else if (texture==8){
-/*			printf("sandy loam\n");*/
-			usle_k=0.27;
-		} else if (texture==9){
-/*			printf("loam\n");*/
-			usle_k=0.38;
-		} else if (texture==10){
-/*			printf("silt loam\n");*/
-			usle_k=0.48;
-		} else if (texture==11){
-/*			printf("silt\n");*/
-			usle_k=0.60;
-		} else {
-			/*printf("i am confused here...Can you do it yourself please?\n");*/
-			usle_k=500.0;/*change value to show it was processed*/
-		}
-	} else if(om_in>=0.5&&om_in<0.2){
-		if(texture==0){
-/*			printf("clay\n");*/
-			usle_k=0.29;/*Range=[0.13-0.29]@0.2, took max*/
-		} else if (texture==1){
-/*			printf("sandy clay\n");*/
-			usle_k=0.135;
-		} else if (texture==2){
-/*			printf("silty clay\n");*/
-			usle_k=0.24;
-		} else if (texture==3){
-/*			printf("sandy clay loam\n");*/
-			usle_k=0.26;
-		} else if (texture==4){
-/*			printf("clay loam\n");*/
-			usle_k=0.265;
-		} else if (texture==5){
-/*			printf("silty clay loam\n");*/
-			usle_k=0.345;
-		} else if (texture==6){
-/*			printf("sand\n");*/
-			usle_k=0.04;
-		} else if (texture==7){
-/*			printf("loamy sand\n");*/
-			usle_k=0.11;
-		} else if (texture==8){
-/*			printf("sandy loam\n");*/
-			usle_k=0.255;
-		} else if (texture==9){
-/*			printf("loam\n");*/
-			usle_k=0.36;
-		} else if (texture==10){
-/*			printf("silt loam\n");*/
-			usle_k=0.45;
-		} else if (texture==11){
-/*			printf("silt\n");*/
-			usle_k=0.56;
-		} else {
-			/*printf("i am confused here...Can you do it yourself please?\n");*/
-			usle_k=500.0;/*change value to show it was processed*/
-		}
-	} else if(om_in==0.2){
-		if(texture==0){
-/*			printf("clay\n");*/
-			usle_k=0.22;/*Range=[0.13-0.29]@0.2, took average*/
-		} else if (texture==1){
-/*			printf("sandy clay\n");*/
-			usle_k=0.13;
-		} else if (texture==2){
-/*			printf("silty clay\n");*/
-			usle_k=0.23;
-		} else if (texture==3){
-/*			printf("sandy clay loam\n");*/
-			usle_k=0.25;
-		} else if (texture==4){
-/*			printf("clay loam\n");*/
-			usle_k=0.25;
-		} else if (texture==5){
-/*			printf("silty clay loam\n");*/
-			usle_k=0.32;
-		} else if (texture==6){
-/*			printf("sand\n");*/
-			usle_k=0.03;
-		} else if (texture==7){
-/*			printf("loamy sand\n");*/
-			usle_k=0.10;
-		} else if (texture==8){
-/*			printf("sandy loam\n");*/
-			usle_k=0.24;
-		} else if (texture==9){
-/*			printf("loam\n");*/
-			usle_k=0.34;
-		} else if (texture==10){
-/*			printf("silt loam\n");*/
-			usle_k=0.42;
-		} else if (texture==11){
-/*			printf("silt\n");*/
-			usle_k=0.52;
-		} else {
-			/*printf("i am confused here...Can you do it yourself please?\n");*/
-			usle_k=500.0;/*change value to show it was processed*/
-		}
-	} else if(om_in>0.2&&om_in<0.4){
-		if(texture==0){
-/*			printf("clay\n");*/
-			usle_k=0.13;/*Range=[0.13-0.29]@0.2, took min*/
-		} else if (texture==1){
-/*			printf("sandy clay\n");*/
-			usle_k=0.125;
-		} else if (texture==2){
-/*			printf("silty clay\n");*/
-			usle_k=0.21;
-		} else if (texture==3){
-/*			printf("sandy clay loam\n");*/
-			usle_k=0.23;
-		} else if (texture==4){
-/*			printf("clay loam\n");*/
-			usle_k=0.23;
-		} else if (texture==5){
-/*			printf("silty clay loam\n");*/
-			usle_k=0.29;
-		} else if (texture==6){
-/*			printf("sand\n");*/
-			usle_k=0.025;
-		} else if (texture==7){
-/*			printf("loamy sand\n");*/
-			usle_k=0.09;
-		} else if (texture==8){
-/*			printf("sandy loam\n");*/
-			usle_k=0.215;
-		} else if (texture==9){
-/*			printf("loam\n");*/
-			usle_k=0.325;
-		} else if (texture==10){
-/*			printf("silt loam\n");*/
-			usle_k=0.375;
-		} else if (texture==11){
-/*			printf("silt\n");*/
-			usle_k=0.47;
-		} else {
-			/*printf("i am confused here...Can you do it yourself please?\n");*/
-			usle_k=500.0;/*change value to show it was processed*/
-		}
-	} else if(om_in>=0.4){ /*May not be right (>4), but i have no other data*/
-		if(texture==0){
-/*			printf("clay\n");*/
-			usle_k=0.13;/*took from value min @0.2 (table empty here)*/
-		} else if (texture==1){
-/*			printf("sandy clay\n");*/
-			usle_k=0.12;
-		} else if (texture==2){
-/*			printf("silty clay\n");*/
-			usle_k=0.19;
-		} else if (texture==3){
-/*			printf("sandy clay loam\n");*/
-			usle_k=0.21;
-		} else if (texture==4){
-/*			printf("clay loam\n");*/
-			usle_k=0.21;
-		} else if (texture==5){
-/*			printf("silty clay loam\n");*/
-			usle_k=0.26;
-		} else if (texture==6){
-/*			printf("sand\n");*/
-			usle_k=0.02;
-		} else if (texture==7){
-/*			printf("loamy sand\n");*/
-			usle_k=0.08;
-		} else if (texture==8){
-/*			printf("sandy loam\n");*/
-			usle_k=0.19;
-		} else if (texture==9){
-/*			printf("loam\n");*/
-			usle_k=0.29;
-		} else if (texture==10){
-/*			printf("silt loam\n");*/
-			usle_k=0.33;
-		} else if (texture==11){
-/*			printf("silt\n");*/
-			usle_k=0.42;
-		} else {
-			/*printf("i am confused here...Can you do it yourself please?\n");*/
-			usle_k=500.0;/*change value to show it was processed*/
-		}
+double tex2usle_k(int texture, double om_in)
+{
+    double usle_k = 200.0;
+
+    /*printf("texture=%i, om=%5.3f",texture, om_in); */
+    if (om_in < 0.5) {
+	if (texture == 0) {
+	    /*                      printf("clay\n"); */
+	    usle_k = 0.29;	/*Took max value @0.2 */
 	}
-	return usle_k;
+	else if (texture == 1) {
+	    /*                      printf("sandy clay\n"); */
+	    usle_k = 0.14;
+	}
+	else if (texture == 2) {
+	    /*                      printf("silty clay\n"); */
+	    usle_k = 0.25;
+	}
+	else if (texture == 3) {
+	    /*                      printf("sandy clay loam\n"); */
+	    usle_k = 0.27;
+	}
+	else if (texture == 4) {
+	    /*                      printf("clay loam\n"); */
+	    usle_k = 0.28;
+	}
+	else if (texture == 5) {
+	    /*                      printf("silty clay loam\n"); */
+	    usle_k = 0.37;
+	}
+	else if (texture == 6) {
+	    /*                      printf("sand\n"); */
+	    usle_k = 0.05;
+	}
+	else if (texture == 7) {
+	    /*                      printf("loamy sand\n"); */
+	    usle_k = 0.12;
+	}
+	else if (texture == 8) {
+	    /*                      printf("sandy loam\n"); */
+	    usle_k = 0.27;
+	}
+	else if (texture == 9) {
+	    /*                      printf("loam\n"); */
+	    usle_k = 0.38;
+	}
+	else if (texture == 10) {
+	    /*                      printf("silt loam\n"); */
+	    usle_k = 0.48;
+	}
+	else if (texture == 11) {
+	    /*                      printf("silt\n"); */
+	    usle_k = 0.60;
+	}
+	else {
+	    /*printf("i am confused here...Can you do it yourself please?\n"); */
+	    usle_k = 500.0;	/*change value to show it was processed */
+	}
+    }
+    else if (om_in >= 0.5 && om_in < 0.2) {
+	if (texture == 0) {
+	    /*                      printf("clay\n"); */
+	    usle_k = 0.29;	/*Range=[0.13-0.29]@0.2, took max */
+	}
+	else if (texture == 1) {
+	    /*                      printf("sandy clay\n"); */
+	    usle_k = 0.135;
+	}
+	else if (texture == 2) {
+	    /*                      printf("silty clay\n"); */
+	    usle_k = 0.24;
+	}
+	else if (texture == 3) {
+	    /*                      printf("sandy clay loam\n"); */
+	    usle_k = 0.26;
+	}
+	else if (texture == 4) {
+	    /*                      printf("clay loam\n"); */
+	    usle_k = 0.265;
+	}
+	else if (texture == 5) {
+	    /*                      printf("silty clay loam\n"); */
+	    usle_k = 0.345;
+	}
+	else if (texture == 6) {
+	    /*                      printf("sand\n"); */
+	    usle_k = 0.04;
+	}
+	else if (texture == 7) {
+	    /*                      printf("loamy sand\n"); */
+	    usle_k = 0.11;
+	}
+	else if (texture == 8) {
+	    /*                      printf("sandy loam\n"); */
+	    usle_k = 0.255;
+	}
+	else if (texture == 9) {
+	    /*                      printf("loam\n"); */
+	    usle_k = 0.36;
+	}
+	else if (texture == 10) {
+	    /*                      printf("silt loam\n"); */
+	    usle_k = 0.45;
+	}
+	else if (texture == 11) {
+	    /*                      printf("silt\n"); */
+	    usle_k = 0.56;
+	}
+	else {
+	    /*printf("i am confused here...Can you do it yourself please?\n"); */
+	    usle_k = 500.0;	/*change value to show it was processed */
+	}
+    }
+    else if (om_in == 0.2) {
+	if (texture == 0) {
+	    /*                      printf("clay\n"); */
+	    usle_k = 0.22;	/*Range=[0.13-0.29]@0.2, took average */
+	}
+	else if (texture == 1) {
+	    /*                      printf("sandy clay\n"); */
+	    usle_k = 0.13;
+	}
+	else if (texture == 2) {
+	    /*                      printf("silty clay\n"); */
+	    usle_k = 0.23;
+	}
+	else if (texture == 3) {
+	    /*                      printf("sandy clay loam\n"); */
+	    usle_k = 0.25;
+	}
+	else if (texture == 4) {
+	    /*                      printf("clay loam\n"); */
+	    usle_k = 0.25;
+	}
+	else if (texture == 5) {
+	    /*                      printf("silty clay loam\n"); */
+	    usle_k = 0.32;
+	}
+	else if (texture == 6) {
+	    /*                      printf("sand\n"); */
+	    usle_k = 0.03;
+	}
+	else if (texture == 7) {
+	    /*                      printf("loamy sand\n"); */
+	    usle_k = 0.10;
+	}
+	else if (texture == 8) {
+	    /*                      printf("sandy loam\n"); */
+	    usle_k = 0.24;
+	}
+	else if (texture == 9) {
+	    /*                      printf("loam\n"); */
+	    usle_k = 0.34;
+	}
+	else if (texture == 10) {
+	    /*                      printf("silt loam\n"); */
+	    usle_k = 0.42;
+	}
+	else if (texture == 11) {
+	    /*                      printf("silt\n"); */
+	    usle_k = 0.52;
+	}
+	else {
+	    /*printf("i am confused here...Can you do it yourself please?\n"); */
+	    usle_k = 500.0;	/*change value to show it was processed */
+	}
+    }
+    else if (om_in > 0.2 && om_in < 0.4) {
+	if (texture == 0) {
+	    /*                      printf("clay\n"); */
+	    usle_k = 0.13;	/*Range=[0.13-0.29]@0.2, took min */
+	}
+	else if (texture == 1) {
+	    /*                      printf("sandy clay\n"); */
+	    usle_k = 0.125;
+	}
+	else if (texture == 2) {
+	    /*                      printf("silty clay\n"); */
+	    usle_k = 0.21;
+	}
+	else if (texture == 3) {
+	    /*                      printf("sandy clay loam\n"); */
+	    usle_k = 0.23;
+	}
+	else if (texture == 4) {
+	    /*                      printf("clay loam\n"); */
+	    usle_k = 0.23;
+	}
+	else if (texture == 5) {
+	    /*                      printf("silty clay loam\n"); */
+	    usle_k = 0.29;
+	}
+	else if (texture == 6) {
+	    /*                      printf("sand\n"); */
+	    usle_k = 0.025;
+	}
+	else if (texture == 7) {
+	    /*                      printf("loamy sand\n"); */
+	    usle_k = 0.09;
+	}
+	else if (texture == 8) {
+	    /*                      printf("sandy loam\n"); */
+	    usle_k = 0.215;
+	}
+	else if (texture == 9) {
+	    /*                      printf("loam\n"); */
+	    usle_k = 0.325;
+	}
+	else if (texture == 10) {
+	    /*                      printf("silt loam\n"); */
+	    usle_k = 0.375;
+	}
+	else if (texture == 11) {
+	    /*                      printf("silt\n"); */
+	    usle_k = 0.47;
+	}
+	else {
+	    /*printf("i am confused here...Can you do it yourself please?\n"); */
+	    usle_k = 500.0;	/*change value to show it was processed */
+	}
+    }
+    else if (om_in >= 0.4) {	/*May not be right (>4), but i have no other data */
+	if (texture == 0) {
+	    /*                      printf("clay\n"); */
+	    usle_k = 0.13;	/*took from value min @0.2 (table empty here) */
+	}
+	else if (texture == 1) {
+	    /*                      printf("sandy clay\n"); */
+	    usle_k = 0.12;
+	}
+	else if (texture == 2) {
+	    /*                      printf("silty clay\n"); */
+	    usle_k = 0.19;
+	}
+	else if (texture == 3) {
+	    /*                      printf("sandy clay loam\n"); */
+	    usle_k = 0.21;
+	}
+	else if (texture == 4) {
+	    /*                      printf("clay loam\n"); */
+	    usle_k = 0.21;
+	}
+	else if (texture == 5) {
+	    /*                      printf("silty clay loam\n"); */
+	    usle_k = 0.26;
+	}
+	else if (texture == 6) {
+	    /*                      printf("sand\n"); */
+	    usle_k = 0.02;
+	}
+	else if (texture == 7) {
+	    /*                      printf("loamy sand\n"); */
+	    usle_k = 0.08;
+	}
+	else if (texture == 8) {
+	    /*                      printf("sandy loam\n"); */
+	    usle_k = 0.19;
+	}
+	else if (texture == 9) {
+	    /*                      printf("loam\n"); */
+	    usle_k = 0.29;
+	}
+	else if (texture == 10) {
+	    /*                      printf("silt loam\n"); */
+	    usle_k = 0.33;
+	}
+	else if (texture == 11) {
+	    /*                      printf("silt\n"); */
+	    usle_k = 0.42;
+	}
+	else {
+	    /*printf("i am confused here...Can you do it yourself please?\n"); */
+	    usle_k = 500.0;	/*change value to show it was processed */
+	}
+    }
+    return usle_k;
 }
-

Modified: grass-addons/gipe/r.usler/elswaify85.c
===================================================================
--- grass-addons/gipe/r.usler/elswaify85.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.usler/elswaify85.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,12 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-double elswaify_1985( double annual_pmm )
-{
-	double result;
-
-	result = ((annual_pmm*0.35)+38.5);
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ double elswaify_1985(double annual_pmm) 
+{
+    double result;
+
+    result = ((annual_pmm * 0.35) + 38.5);
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.usler/foster81.c
===================================================================
--- grass-addons/gipe/r.usler/foster81.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.usler/foster81.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,14 +1,12 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-double foster_1981( double annual_pmm)
-{
-	double result;
-	
-	result = ((annual_pmm*0.276*75.0)/100.0); 
-	
-	return result;
-
-}
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ double foster_1981(double annual_pmm) 
+{
+    double result;
+
+    result = ((annual_pmm * 0.276 * 75.0) / 100.0);
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.usler/main.c
===================================================================
--- grass-addons/gipe/r.usler/main.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.usler/main.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,168 +1,193 @@
-/****************************************************************************
- *
- * MODULE:       r.usler
- * AUTHOR(S):    Natalia Medvedeva - natmead at gmail.com
- *		 Yann Chemin - ychemin at gmail.com
- * PURPOSE:      Calculates USLE R factor 
- * 		 Rainfall Erosion index according to four methods 
- *
- * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- * 
- *****************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-double elswaify_1985( double annaul_pmm );
-double morgan_1974( double annual_pmm );
-double foster_1981( double annual_pmm);
-double roose_1975( double annual_pmm );
-
-int main(int argc, char *argv[])
-{
-	struct Cell_head cellhd; /*region+header info*/
-	char *mapset; /*mapset name*/
-	int nrows, ncols;
-	int row,col;
-
-	char *nameflag;/*Switch for particular method*/
-	
-	struct GModule *module;
-	struct Option *input1, *input2, *output;
-	
-	struct History history; /*metadata*/
-	
-	/************************************/
-	/* FMEO Declarations*****************/
-	char *name;   /*input raster name*/
-	char *result; /*output raster name*/
-	/*File Descriptors*/
-	int infd_annual_pmm;
-	int outfd;
-	
-	char  *annual_pmm;
-	
-	int i=0,j=0;
-	
-	void *inrast_annual_pmm;
-	DCELL *outrast;
-	RASTER_MAP_TYPE data_type_output=DCELL_TYPE;
-	RASTER_MAP_TYPE data_type_annual_pmm;
-	
-	/************************************/
-	G_gisinit(argv[0]);
-
-	module = G_define_module();
-	module->keywords = _("rainfall, erosion, USLE");
-	module->description = _("USLE R factor, Rainfall erosivity index.");
-
-	/* Define the different options */
-	input1 = G_define_option() ;
-	input1->key        =_("name");
-	input1->type       = TYPE_STRING;
-	input1->required   = YES;
-	input1->gisprompt  =_("Name of method to use");
-	input1->description=_("Name of USLE R equation: roose, morgan, foster, elswaify.");
-	input1->answer     =_("morgan");
-
-	input2 = G_define_standard_option(G_OPT_R_INPUT) ;
-	input2->key	   = _("annual_precip");
-	input2->description=_("Name of the annual precipitation map");
-	input2->answer     =_("annual_pmm");
-
-	output= G_define_standard_option(G_OPT_R_OUTPUT) ;
-	output->key        =_("usler");
-	output->description=_("Name of the output usler layer");
-	output->answer     =_("usler");
-
-	/********************/
-	if (G_parser(argc, argv))
-		exit (EXIT_FAILURE);
-	nameflag	= input1->answer;
-	annual_pmm 	= input2->answer;
-
-	result  = output->answer;
-	/***************************************************/
-	mapset = G_find_cell2(annual_pmm, "");
-	if (mapset == NULL) {
-		G_fatal_error(_("cell file [%s] not found"), annual_pmm);
-	}
-	data_type_annual_pmm = G_raster_map_type(annual_pmm,mapset);
-	if ( (infd_annual_pmm = G_open_cell_old (annual_pmm,mapset)) < 0)
-		G_fatal_error (_("Cannot open cell file [%s]"), annual_pmm);
-	if (G_get_cellhd (annual_pmm, mapset, &cellhd) < 0)
-		G_fatal_error (_("Cannot read file header of [%s])"), annual_pmm);
-	inrast_annual_pmm = G_allocate_raster_buf(data_type_annual_pmm);
-	/***************************************************/
-
-	G_debug(3, "number of rows %d",cellhd.rows);
-	nrows = G_window_rows();
-	ncols = G_window_cols();
-	outrast = G_allocate_raster_buf(data_type_output);
-	/* Create New raster files */
-	if ( (outfd = G_open_raster_new (result,data_type_output)) < 0)
-		G_fatal_error(_("Could not open <%s>"),result);
-	/* Process pixels */
-	for (row = 0; row < nrows; row++)
-	{
-		DCELL d;
-		DCELL d_annual_pmm;
-		G_percent(row,nrows,2);
-		/* read input map */	
-		if(G_get_raster_row(infd_annual_pmm,inrast_annual_pmm,row,data_type_annual_pmm)<0)
-			G_fatal_error(_("Could not read from <%s>"),annual_pmm);
-		/*process the data */
-		for (col=0; col < ncols; col++)
-		{
-			d_annual_pmm = ((DCELL *) inrast_annual_pmm)[col];
-			if(G_is_d_null_value(&d_annual_pmm)){
-				G_set_d_null_value(&outrast[col],1);
-			} else {
-				/************************************/
-				/*calculate morgan       */
-				if (!strcoll(nameflag,"morgan")){		
-					d =  morgan_1974(d_annual_pmm);
-					outrast[col] = d;
-				}
-				/*calculate roose	            */
-				if (!strcoll(nameflag,"roose")){
-					d =  roose_1975(d_annual_pmm);
-					outrast[col] = d;
-				}
-				/*calculate foster	            */
-				if (!strcoll(nameflag,"foster")){
-					d =  foster_1981(d_annual_pmm);
-					outrast[col] = d;
-				}
-				/*calculate elswaify	            */
-				if (!strcoll(nameflag,"elswaify")){
-					d =  elswaify_1985(d_annual_pmm);
-					outrast[col] = d;
-				}
-			}
-		}
-		if (G_put_raster_row (outfd, outrast, data_type_output) < 0)
-			G_fatal_error(_("Cannot write to output raster file"));
-	}
-
-	
-	G_free(inrast_annual_pmm);
-	G_close_cell(infd_annual_pmm);
-	
-	G_free(outrast);
-	G_close_cell(outfd);
-	
-	G_short_history(result, "raster", &history);
-	G_command_history(&history);
-	G_write_history(result,&history);
-
-	exit(EXIT_SUCCESS);
-}
-
+
+/****************************************************************************
+ *
+ * MODULE:       r.usler
+ * AUTHOR(S):    Natalia Medvedeva - natmead at gmail.com
+ *		 Yann Chemin - ychemin at gmail.com
+ * PURPOSE:      Calculates USLE R factor 
+ * 		 Rainfall Erosion index according to four methods 
+ *
+ * COPYRIGHT:    (C) 2002-2006 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
+ *   	    	 for details.
+ * 
+ *****************************************************************************/
+     
+    
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+double elswaify_1985(double annaul_pmm);
+
+double morgan_1974(double annual_pmm);
+
+double foster_1981(double annual_pmm);
+
+double roose_1975(double annual_pmm);
+
+int main(int argc, char *argv[]) 
+{
+    struct Cell_head cellhd;	/*region+header info */
+
+    char *mapset;		/*mapset name */
+
+    int nrows, ncols;
+
+    int row, col;
+
+    char *nameflag;		/*Switch for particular method */
+
+    struct GModule *module;
+
+    struct Option *input1, *input2, *output;
+
+    struct History history;	/*metadata */
+
+    
+
+	/************************************/ 
+	/* FMEO Declarations**************** */ 
+    char *name;			/*input raster name */
+
+    char *result;		/*output raster name */
+
+    
+	/*File Descriptors */ 
+    int infd_annual_pmm;
+
+    int outfd;
+
+    char *annual_pmm;
+
+    int i = 0, j = 0;
+
+    void *inrast_annual_pmm;
+
+    DCELL * outrast;
+    RASTER_MAP_TYPE data_type_output = DCELL_TYPE;
+    RASTER_MAP_TYPE data_type_annual_pmm;
+    
+
+	/************************************/ 
+	G_gisinit(argv[0]);
+    module = G_define_module();
+    module->keywords = _("rainfall, erosion, USLE");
+    module->description = _("USLE R factor, Rainfall erosivity index.");
+    
+	/* Define the different options */ 
+	input1 = G_define_option();
+    input1->key = _("name");
+    input1->type = TYPE_STRING;
+    input1->required = YES;
+    input1->gisprompt = _("Name of method to use");
+    input1->description =
+	_("Name of USLE R equation: roose, morgan, foster, elswaify.");
+    input1->answer = _("morgan");
+    input2 = G_define_standard_option(G_OPT_R_INPUT);
+    input2->key = _("annual_precip");
+    input2->description = _("Name of the annual precipitation map");
+    input2->answer = _("annual_pmm");
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+    output->key = _("usler");
+    output->description = _("Name of the output usler layer");
+    output->answer = _("usler");
+    
+
+	/********************/ 
+	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    nameflag = input1->answer;
+    annual_pmm = input2->answer;
+    result = output->answer;
+    
+
+	/***************************************************/ 
+	mapset = G_find_cell2(annual_pmm, "");
+    if (mapset == NULL) {
+	G_fatal_error(_("cell file [%s] not found"), annual_pmm);
+    }
+    data_type_annual_pmm = G_raster_map_type(annual_pmm, mapset);
+    if ((infd_annual_pmm = G_open_cell_old(annual_pmm, mapset)) < 0)
+	G_fatal_error(_("Cannot open cell file [%s]"), annual_pmm);
+    if (G_get_cellhd(annual_pmm, mapset, &cellhd) < 0)
+	G_fatal_error(_("Cannot read file header of [%s])"), annual_pmm);
+    inrast_annual_pmm = G_allocate_raster_buf(data_type_annual_pmm);
+    
+
+	/***************************************************/ 
+	G_debug(3, "number of rows %d", cellhd.rows);
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    outrast = G_allocate_raster_buf(data_type_output);
+    
+	/* Create New raster files */ 
+	if ((outfd = G_open_raster_new(result, data_type_output)) < 0)
+	G_fatal_error(_("Could not open <%s>"), result);
+    
+	/* Process pixels */ 
+	for (row = 0; row < nrows; row++)
+	 {
+	DCELL d;
+	DCELL d_annual_pmm;
+	G_percent(row, nrows, 2);
+	
+	    /* read input map */ 
+	    if (G_get_raster_row
+		(infd_annual_pmm, inrast_annual_pmm, row,
+		 data_type_annual_pmm) < 0)
+	    G_fatal_error(_("Could not read from <%s>"), annual_pmm);
+	
+	    /*process the data */ 
+	    for (col = 0; col < ncols; col++)
+	     {
+	    d_annual_pmm = ((DCELL *) inrast_annual_pmm)[col];
+	    if (G_is_d_null_value(&d_annual_pmm)) {
+		G_set_d_null_value(&outrast[col], 1);
+	    }
+	    else {
+		
+
+				/************************************/ 
+		    /*calculate morgan       */ 
+		    if (!strcoll(nameflag, "morgan")) {
+		    d = morgan_1974(d_annual_pmm);
+		    outrast[col] = d;
+		}
+		
+		    /*calculate roose                   */ 
+		    if (!strcoll(nameflag, "roose")) {
+		    d = roose_1975(d_annual_pmm);
+		    outrast[col] = d;
+		}
+		
+		    /*calculate foster                  */ 
+		    if (!strcoll(nameflag, "foster")) {
+		    d = foster_1981(d_annual_pmm);
+		    outrast[col] = d;
+		}
+		
+		    /*calculate elswaify                */ 
+		    if (!strcoll(nameflag, "elswaify")) {
+		    d = elswaify_1985(d_annual_pmm);
+		    outrast[col] = d;
+		}
+	    }
+	    }
+	if (G_put_raster_row(outfd, outrast, data_type_output) < 0)
+	    G_fatal_error(_("Cannot write to output raster file"));
+	}
+    G_free(inrast_annual_pmm);
+    G_close_cell(infd_annual_pmm);
+    G_free(outrast);
+    G_close_cell(outfd);
+    G_short_history(result, "raster", &history);
+    G_command_history(&history);
+    G_write_history(result, &history);
+    exit(EXIT_SUCCESS);
+}
+
+

Modified: grass-addons/gipe/r.usler/morgan74.c
===================================================================
--- grass-addons/gipe/r.usler/morgan74.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.usler/morgan74.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,12 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-double morgan_1974( double annual_pmm )
-{
-	double result;
-	
-	result = (((annual_pmm*9.28)-8838.0)*75.0)/1000.0;
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ double morgan_1974(double annual_pmm) 
+{
+    double result;
+
+    result = (((annual_pmm * 9.28) - 8838.0) * 75.0) / 1000.0;
+    return result;
+}
+
+

Modified: grass-addons/gipe/r.usler/roose75.c
===================================================================
--- grass-addons/gipe/r.usler/roose75.c	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/r.usler/roose75.c	2008-08-22 02:44:27 UTC (rev 32991)
@@ -1,15 +1,12 @@
-#include<stdio.h>
-#include<math.h>
-#include<stdlib.h>
-
-
-double roose_1975( double annual_pmm )
-{
-	double result;
-	
-	result = annual_pmm*0.5*1.73 ;
-	
-	return result;
-
-}
-
+#include<stdio.h>
+#include<math.h>
+#include<stdlib.h>
+ double roose_1975(double annual_pmm) 
+{
+    double result;
+
+    result = annual_pmm * 0.5 * 1.73;
+    return result;
+}
+
+

Modified: grass-addons/gipe/readme.gipe
===================================================================
--- grass-addons/gipe/readme.gipe	2008-08-22 02:02:44 UTC (rev 32990)
+++ grass-addons/gipe/readme.gipe	2008-08-22 02:44:27 UTC (rev 32991)
@@ -55,7 +55,7 @@
 i.vi:		Calculates any of 13 types of vegetation indices
 i.vi.mpi:	Educational version in MPICH for HPC
 i.vi.grid:	Educational version in Ninf/G for grid computing
-i.water:	Identifies water (from Terra-MODIS)
+i.water:	Identifies water (from Terra-MODIS and generic method)
 i.wi:		Calculates a water index
 
 
@@ -69,4 +69,4 @@
 r.soiltex2prop: Converts soil texture to porosity, saturated hydraulic conductivity, wetting front suction after Rawls et al (1990)
 r.soilusda2tex: Converts USDA soil classes to soil texture layers (sand, silt, clay)
 r.uslek: 	USLE K factor calculation from soil texture layers
-r.usler: 	USLE R factor calculation (4 methods)
\ No newline at end of file
+r.usler: 	USLE R factor calculation (4 methods)



More information about the grass-commit mailing list