#!/usr/bin/env python

############################################################################
#
# MODULE:      r.convert.aspect
# AUTHOR(S):   Margherita Di Leo
# PURPOSE:     Script for converting degrees clockwise from North to GRASS convention
# COPYRIGHT:   (C) 2013 by Margherita Di Leo 
#              dileomargherita@gmail.com
#
#              This program is free software under the GNU General Public
#              License (>=v3.0) and comes with ABSOLUTELY NO WARRANTY.
#              See the file COPYING that comes with GRASS
#              for details.
#
#############################################################################

#%module
#% description: Script for converting degrees clockwise from North to GRASS convention
#% keywords: raster, aspect
#%end

#%option
#% key: r_wind
#% type: string
#% gisprompt: old,raster,raster
#% key_desc: 
#% description: Name of raster map containing degrees from North clockwise (e.g. wind map)
#% required: yes
#%end

#%option
#% key: r_grass
#% type: string
#% gisprompt: old,raster,raster
#% key_desc:  
#% description: Name of raster map containing degrees from East counterclockwise 
#% required : yes
##%END

import sys
import os
import grass.script as grass

if not os.environ.has_key("GISBASE"):
    grass.message( "You must be in GRASS GIS to run this program." )
    sys.exit(1)   
    
def main():
    r_wind = options["r_wind"]
    r_grass = options["r_grass"]
    
    grass.mapcalc("$r_grass = if( $r_wind >= 0 && $r_wind < 270, 270 - $r_wind, if($r_wind == 270, 360, if($r_wind > 270 && $r_wind < 360, 630 - $r_wind, if(isnull($r_wind), 0, null()))))",
                  r_grass = r_grass,
                  r_wind = r_wind)
       
    
if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())   
