def main(argv):

    try:
        opts,argv = getopt.getopt(argv,":h:i:e:s:o:c:E:t:p:g:P:m:l:",['help','[outFile]','code','[shapeFile]','start','end','[tr]'])
    except getopt.GetoptError:
        print 'error in parameter for GFSDownload. type GFSDownload.py -help for more detail on use '
        sys.exit(2)
    
    for opt, arg in opts:
        if opt == '-h':
            print 'GFSDownload.py  '
            print '    [mandatory] : ',
            print '        --code <GFSCode>'
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YYYY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --level <GFS Level> (default surface)'
            print '        --step <GFS Step> (default 3,6,9,12)'
            print '        --grid <GFS Grid> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/GFS)'
            print '        --proxy <proxy : True/False> (default False)'
            print '        --mode <mode : analyse/forcast/cycleforecast> (default analyse)'
            print ''
            print 'EXAMPLES'
            print '--temperature on a shapefile'
            print 'python GFSDownload.py -c PRES -i 2014-01-01 -e 2014-01-02 -s PATH_TO_SHAPE'
            print '--pressure on a area'
            print 'python GFSDownload.py -c 134 -i 2014-01-01 -e 2014-01-02 -E xmin,ymax,xmax,ymin'
            print ''
            print ' CODE PARAMETERS'
            print 'total precipitation  :  APCP [m of water]'
            print '2 metre temperature  :  TMP [K]'
            print 'maximum 2m temperature since last post-processing step : TMAX [K]'
            print 'minimum 2m temperature since last post-processing step : TMIN [K]'
            print 'surface pressure : PRES [Pa]'
            print '2 metre dewpoint : DPT [K]'
            print '10 metre eastward wind component UGRD [m s-1]'
            print '10 metre northward wind component VGRD [m s-1]'
            print '...'
            print ''
            print ' LEVEL PARAMETERS'
            print '2_m_above_ground'
            print '10_m_above_ground'
            print '3000-0_m_above_ground'
            print '300_mb'
            print '...'
            print 'see http://www.nco.ncep.noaa.gov/pmb/products/gfs/ for product description'
            sys.exit() 
        elif opt in ('-o','--outFolder'):
            oFolder = arg
        elif opt in ('-c','--code'):
            codeGFS = arg.split(',')
        elif opt in ('-i','--start'):
            startDate = arg
        elif opt in ('-e','--end'):
            endDate = arg
        elif opt in ('-s','--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E','--tr'):
            extend = arg.split(',')
        elif opt in ('-g','--grid'):
            grid = arg
        elif opt in ('-p','--step'):
            step = arg.split(',')
        elif opt in ('-P','--proxy'):
            proxy = arg
        elif opt in ('-m','--mode'):
            mode = arg
        elif opt in ('-l','--level'):
            levelList = arg.split(',')
    
    if len(sys.argv) < 8:
        print 'GFSDownload.py'
        print '    -c <GFSCode> -list possible-'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-g <size of grid in 0.25/0.5/1/2.5> (default 0.25)]'
        print '    [-p <GFS step parameter in 0,6,12,18> default 0,6,12,18] -list possible-'
        print '    [-o <outfolder> (default /home/user/GFS)]'
        print '    [-P <proxy> (default False)]'
        print '    [-l <level> (default 2_m_above_ground)]'
        print '    [-m <mode> (default analyse)]'
        print ''
        print 'For help on paramCode -help'
        sys.exit(2)
        
    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/GFS'
        print "output folder not precised : downloaded GFF images on "+oFolder
    
    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder) 
    
    try:
        codeGFS
    except NameError:
        exit ('parameter(s) needed not precise. Please give the GFS parameter you wish')
    utils.checkForParams(codeGFS)
    
    
    try:
        startDate
    except NameError:
        exit ('init Date not precised')
    # verification si sartDate est une date
    startDate=utils.checkForDate(startDate) 
    
    try:
        endDate
    except NameError:
        exit ('end Date not specified')
    # verification si sartDate est une date
    endDate=utils.checkForDate(endDate) 
    
    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit ('no Area of interest have been specified. please use -shp or -tr to declare it')
    
    if 'pathToShapefile' in locals():
        extendArea=utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea=extend
    extendArea=utils.checkForExtendValidity(extendArea)
    
    try:
        levelList
    except NameError:
        levelList=['surface']
    levelList=utils.checkForLevelValidity(levelList)
    
    try:
        grid
    except NameError:
        grid='0.25'
    grid=utils.checkForGridValidity(grid)
        
    try:
        step
    except NameError:
        step=[0,6,12,18]
    step=utils.checkForStepValidity(step)
        
    try:
        proxy
    except NameError:
        proxy=False
        
    try:
        mode
    except NameError:
        mode='analyse'
    
    #Proxy parameteres needed
    if(proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
        
    
    #Download GFS
    struct=utils.create_request_gfs(startDate, endDate, step, levelList, grid, extendArea, codeGFS, mode)    
    listeFile=[]
    
    if len(struct)==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                outpath=oFolder+'/'+",".join(codeGFS)+'_'+i.rsplit('.',1)[1]+'.grb'
                listeFile.append(outpath)
                result=utils.GFSDownload(i,outpath)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            utils.convertGribToTiff(listeFile,codeGFS,levelList,step,grid,startDate,endDate,oFolder)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    if struct[1] is not None:
        print ("")
        print ("--------------------------------------------------")
        print ("")
        print ("Some parameters couldn't been downloaded in %s mode :" % mode )
        print ("They have been downloaded in %s mode due to var %s" % (struct[1],','.join(struct[2])) )
def main(argv):

    try:
        opts,argv = getopt.getopt(argv,":h:i:e:s:E:o:g:P:t:f:r:",['help','[outFile]','code','[shapeFile]','start','end','[tr]'])
    except getopt.GetoptError:
        print 'error in parameter for eraInterimDownload. type eraInterimDownload.py -help for more detail on use '
        sys.exit(2)
    
    for opt, arg in opts:
        if opt == '-h':
            print 'eraInterimDownload.py  '
            print '    [mandatory] : '
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --typeData  < analyse , forcast> (default forcast)'
            print '        --grid <EraInterim Time> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/eraInterim)'
            print '        --proxy <True/False> (default False)'
            print '        --temporaryFile <True/False> (default False)'
            print '        --result < TxtFile / RasterFile> default RasterFile'
            print ''
            sys.exit() 
        elif opt in ('-o','--outFolder'):
            oFolder = arg
        elif opt in ('-i','--start'):
            startDate = arg
        elif opt in ('-e','--end'):
            endDate = arg
        elif opt in ('-s','--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E','--tr'):
            extend = arg.split(',')
        elif opt in ('-g','--grid'):
            grid = arg
        elif opt in ('-P','--proxy'):
            proxy = arg
        elif opt in ('-t','--typeData'):
            typeData = arg
        elif opt in ('-f','--temporaryFile'):
            temporaryFile = arg
        elif opt in ('-r','--result'):
            typeOutput = arg
    
    if len(sys.argv) < 7:
        print 'eraInterimDownload.py'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-t < analyse , forcast> (default analyse)]'
        print '    [-g <size of grid in 0.125/0.25/0.5/0.75/1.125/1.5/2/2.5/3> (default0.75)]'
        print '    [-o <outfolder> (default /home/user/eraInterim)]'
        print '    [-P <proxy : True/False> (default False)]'
        print '    [-f <temporaryFile : True/False> (default False)]'
        print '    [-r <resultOutput : TxtFile/RasterFile> (default RasterFile)]'
        print ''
        print 'For help on interimCode -help'
        sys.exit(2)
        
    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/eraInterim'
        print "output folder not precised : downloaded eraInterim images on "+oFolder
    
    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder) 
        
    try:
        startDate
    except NameError:
        exit ('init Date not precised')
    # verification si sartDate est une date
    startDate=utils.checkForDate(startDate) 
    
    try:
        endDate
    except NameError:
        exit ('end Date not specified')
    # verification si sartDate est une date
    endDate=utils.checkForDate(endDate) 
    
    if (startDate>endDate):
        exit('startDate could not be greater than endDate')

    today=date.today()
    limitDate = today - timedelta(days=31*3)
    limitDate=date(limitDate.year,limitDate.month,calendar.monthrange(limitDate.year,limitDate.month)[1])
    if (startDate>limitDate or endDate>limitDate ):
        exit('date could not exceed 2014-12-31')
    
    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit ('no Area of interest have been specified. please use -shp or -tr to declare it')
    
    if 'pathToShapefile' in locals():
        extendArea=utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea=extend

    extendArea=utils.checkForExtendValidity(extendArea)
      
    try:
        typeData
    except NameError:
        typeData='analyse'

    try:
        grid
    except NameError:
        grid='0.75'
    grid=utils.checkForGridValidity(grid)
            
    try:
        proxy
    except NameError:
        proxy=False

    #Proxy parameteres needed
    if(proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
    
    try:
        temporaryFile
    except NameError:
        temporaryFile=False
    
    try:
        typeOutput
    except NameError:
        typeOutput='RasterFile'
    """----------------------------------------"""
    
    
    #Create param if first Time
    if (not utils.checkForFile(os.path.expanduser('~')+'/.ecmwfapirc')):
        print ('for first connexion you have to define yout key and password on ecmwf')
        print ('cf  https://apps.ecmwf.int/auth/login/')
        print ('')
        u = raw_input('user (mail) : ')
        k = raw_input('keys : ')
        utils.createParamFile(os.path.expanduser('~')+'/.ecmwfapirc',u,k)
        
    delta = endDate - startDate
    nbDays = delta.days + float(delta.seconds) / 86400 + 1

    #--------------------------On charge les rasters
    if typeData == "analyse":
        time = ['00',"12","06","18"]
        step = []
        nbBandByDay=len(time)
    else:
        time = ['00',"12"]
        step = [3,6,9,12]
        nbBandByDay=(12*len(time))/(len(step))+1
    server = ECMWFDataServer()
    
    
    """ altitude de la grille EraInterim """
    # Only Forcast possible
    codeGeopot= [129]
    GeoFile = oFolder+"/129"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeGeopot, GeoFile,typeData)
    server.retrieve(struct)
    Geo = utils.convertNETCDFtoDicArray(GeoFile)
    Geo = utils.convertGeoToAlt(Geo)
    #un peu inutile car ne change pas ... mais bon! 
    Geo = utils.computeDailyMax(Geo, nbBandByDay, typeData)
    
    """ Vitesse du vent """
    codeVent= [165,166]
    vent={}
    ventFile=[]
    for i in codeVent:
        ventFile.append(oFolder+"/"+str(i)+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc')
        struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, [i], ventFile[-1],typeData)
        server.retrieve(struct)
        vent[i]=utils.convertNETCDFtoDicArray(ventFile[-1])
    
     
    vent = utils.fusVentFromDict(vent,nbBandByDay)
    vent=utils.computeDailyMean(vent,nbBandByDay,typeData)

    """ Humidité relative """
    
    codePressure= [134]
    pressureFile = oFolder+"/134"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'

    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codePressure, pressureFile,typeData)
    server.retrieve(struct)
    pressure = utils.convertNETCDFtoDicArray(pressureFile)
    #oulalal c'est moche
    pressureMean = utils.convertPaToKgPa(pressure)
    pressure = utils.convertToHectoPascal(pressure)
    pressureMean = utils.computeDailyMean(pressureMean,nbBandByDay,typeData)
    
    codeT2m= [167]
    T2mFile = oFolder+"/167"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'

    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeT2m, T2mFile,typeData)
    server.retrieve(struct)
    T2m = utils.convertNETCDFtoDicArray(T2mFile)

    Tmean = utils.computeDailyMean(T2m, nbBandByDay, typeData)
    Tmax = utils.computeDailyMax(T2m,nbBandByDay)
    Tmin = utils.computeDailyMin(T2m,nbBandByDay)
    
    T2m = utils.convertKToD(T2m)
    #T2m = utils.computeDailyMean(T2m,nbBandByDay,typeData)
    
    codeDewP= [168]
    DewPFile = oFolder+"/168"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'

    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeDewP, DewPFile,typeData)
    server.retrieve(struct)
    DewP = utils.convertNETCDFtoDicArray(DewPFile)
    DewP = utils.convertKToD(DewP)
    #DewP = utils.computeDailyMean(DewP,nbBandByDay,typeData)

    humidity = utils.ComputeHumidityFromPT(pressure,T2m,DewP)
    #humidity = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    Hmax = utils.computeDailyMax(humidity,nbBandByDay)
    Hmin = utils.computeDailyMin(humidity,nbBandByDay)
    Hmean = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    

    """ ONLY FORCAST FOR THESE VAR"""
    typeData="forcast"
    time = ['00',"12"]
    step = [3,6,9,12]
    nbBandByDay=(12*len(time))/(len(step))+1
    
    """ Rayonnement global incident journalier """
    # Only Forcast possiblet
    codeRay= [176]
    RayFile = oFolder+"/176"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeRay, RayFile,typeData)
    server.retrieve(struct)
    Ray = utils.convertNETCDFtoDicArray(RayFile)
    Ray = utils.computeDailyMean(Ray,nbBandByDay,typeData)
    Ray = utils.convertWToMJ(Ray)
    
    """ downward surface solar radiation """
    # Only Forcast possiblet
    codeRay= [169]
    RayFileDownShort = oFolder+"/169"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeRay, RayFileDownShort,typeData)
    server.retrieve(struct)
    RayDownShort = utils.convertNETCDFtoDicArray(RayFileDownShort)
    RayDownShort = utils.computeDailyMean(RayDownShort,nbBandByDay,typeData)
    RayDownShort = utils.convertWToMJ(RayDownShort)
    
    """ downward surface thermal radiation """
    # Only Forcast possiblet
    codeRay= [175]
    RayFileDownLong = oFolder+"/175"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeRay, RayFileDownLong,typeData)
    server.retrieve(struct)
    RayDownLong = utils.convertNETCDFtoDicArray(RayFileDownLong)
    RayDownLong = utils.computeDailyMean(RayDownLong,nbBandByDay,typeData)
    RayDownLong = utils.convertWToMJ(RayDownLong)

    """ Evaporation """
    codeEvap= [182]
    EvapFile = oFolder+"/182"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeEvap, EvapFile,typeData)
    server.retrieve(struct)
    Evap = utils.convertNETCDFtoDicArray(EvapFile)
    Evap = utils.computeDailyMean(Evap,nbBandByDay,typeData)
    #Evap = utils.convertMToMm(Evap)
    
    
    """ Precipitation """
    #NOT NEEDED FOR ETO BUT Exported
    
    utils.checkForTimeValidity(time)
    utils.checkForStepValidity(step,typeData)
    codePrecipitation= [228]
    precipitationFile = oFolder+"/228"+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'

    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codePrecipitation, precipitationFile)
    server.retrieve(struct)
    precipitation = utils.convertNETCDFtoDicArray(precipitationFile)
    precipitation=utils.computeDailyAccumulation(precipitation,nbBandByDay,typeData)
    
    
    """ Grid of latitude [0],longitude[1] in WGS84"""
    geoTransform=utils.getGeoTransform(RayFile)
    shape=Ray[0].shape
    latlon = utils.getCentroidLatFromArray(shape,geoTransform,grid)

    """ --------------------- Compute ET0---------------------- """
    
    ET0_0={}
    ET0_1={}
    ET0_2={}
    DoyList=[]
    DateList=[]
    
    for i in range(0,int(nbDays)):
        #jour Julien
        J = utils.doy(startDate,i)
        dateEnCours=startDate+ timedelta(days=i)
        DateList.append(dateEnCours)
        DoyList.append(J)
        Hmax[i] = np.where(Hmax[i]>100,100,Hmax[i])
        
        # --- Constants ---# 
        #Solar constant
        Gsc = 0.0820 # [MJ.m-2.min-1]
        #Albedo - grass reference crop
        a = 0.23
        #Ratio of molecular weight of water vapor/dry air
        epsilon=0.622 
        #Latente heat of vaporisation
        Lv=2.45 # [MJ.kg-1]
        # Specific heat at constant pressure
        Cp= 1.013e-3; # [MJ.kg-1.°C-1]
        # Stefan-Boltzmann constant [MJ.K-4.m-2.day-1]
        StefBoltz=4.903e-9; #FAO
        
        # --- Equations ---# 
        # Psychometric constant [kPa.°C-1]
        cte_psy = (Cp*pressureMean[i])/(epsilon*Lv) # Equation 8 Chap 3 FAO
        #Mean sturation vapor presure [kPa]
        #es = (utils.esat(pressureMean[i],Tmax[i]) + utils.esat(pressureMean[i],Tmin[i]))/2;    #Equation 12 Chap 3
        es = (utils.eocalc(Tmax[i]-273.16)+utils.eocalc(Tmin[i]-273.16))/2    #Equation 12 Chap 3
        # Slope of saturation vapour pressure curve at air temperature [kPa.°C-1]
        delta = utils.delta_calc(Tmean[i]);                    # Equation 13 Chap 3
        # Actual vapour pressure derived from relative humidity [kPa]
        #ea = (utils.esat(pressureMean[i]/100,Tmax[i]-273.16)*(Hmax[i]/100) + utils.esat(pressureMean[i]/100,Tmin[i]-273.16)*(Hmin[i]/100))/2;      # Equation 17 Chap 3
        ea = (utils.eocalc(Tmax[i]-273.16)*(Hmax[i]/100)+utils.eocalc(Tmin[i]-273.16)*(Hmin[i]/100))/2
        # Conversion of latitude from degrees to radians
        phi = (np.pi/180)* latlon[1];     
        # Relative distance Earth-Sun
        dr = 1 + 0.033*math.cos(2*math.pi*J/365);         # Equation 23 Chap 3
        # Solar declination
        d = 0.4093*math.sin(2*math.pi*J/365 - 1.39);      # Equation 24 Chap 3
        # sunset hour angle
        ws = np.arccos(-np.tan(phi)*math.tan(d));                # Equation 25 Chap 3
        
        """Classical calculation FAO """
        
        # Extraterestrial radiation for daily periods
        Ra = (24.*60/np.pi)*Gsc*dr*(ws*np.sin(phi)*np.sin(d) + np.cos(phi)*np.cos(d)*np.sin(ws))    # Equation 21 Chap 3
        # Clear sky solar radiation [MJ.m-2.day-1]
        Rso = (0.75 + 2e-5*Geo[i])*Ra;                 # Equation 37 Chap 3
        # Net solar radiation [MJ.m-2.day-1]
        Rns = (1 - a)*RayDownShort[i];                          # Equation 38 Chap 3
        #
        f=(1.35*(np.fmin(RayDownShort[i]/Rso,1)) - 0.35);
        # Net longwave radiation [MJ.m-2.day-1]
        Rnl = StefBoltz*((Tmax[i]**4 + Tmin[i]**4)/2)*(0.34 - 0.14*np.sqrt(ea))*f; # Equation 39 Chap 3
        # Net Radiation [MJ.m-2.day-1]
        Rn =Rns - Rnl;                              # Equation 40 Chap 3
        G = 0;                                      # Equation 42 Chap 3
        ET0_0[i] = ( 0.408*delta*( Rn-G )+ cte_psy*( 900/(Tmean[i] + 273) )*(es - ea)*vent[i] )/( delta + cte_psy*(1 + 0.34*vent[i]) );  # Equation 6 Chap 4
        
        
        """ Considering product 176 = RN these equations are not needed """
        Rn = Ray[i]
        # Soil heat flux at daily scale
        G = 0;                                      # Equation 42 Chap 3
        ET0_1[i] = ( 0.408*delta*( Rn-G )+ cte_psy*( 900/(Tmean[i] + 273) )*(es - ea)*vent[i] )/( delta + cte_psy*(1 + 0.34*vent[i]) );  # Equation 6 Chap 4
    
        """ Considering product 176 Evaporation """
        ET0_2[i] = Evap[i]
    
    if typeOutput=='RasterFile':
        #On ecrit le fichier ET0 
        geoTransform=utils.getGeoTransform(RayFile)
        shape=Ray[0].shape
        utils.writeTiffFromDicoArray(ET0_0,oFolder+"/tmp.tif",shape,geoTransform)
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/ET0.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/ET0.tif")

        #On écrit le fichier Precipitation
        geoTransform=utils.getGeoTransform(precipitationFile)
        shape=precipitation[0].shape
        utils.writeTiffFromDicoArray(precipitation,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/precipitationAcc.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/precipitationAcc.tif")
        
    else:
        #On ecrit le fichier au format Txt
        proj=utils.getProj(pathToShapefile)
        utils.WriteTxtFileForEachPixel(oFolder,ET0_0,ET0_1,ET0_2,DateList,DoyList,Ray,RayDownShort,RayDownLong,Tmean,Tmax,Tmin,Hmean,Hmax,Hmin,vent,precipitation,pressureMean,Geo,latlon,proj)
        utils.WritePointList(oFolder,latlon,proj)
    
    """ ------------------------------------------- """
    if(temporaryFile):
        #On ecrit le fichier latlon 
        geoTransform=utils.getGeoTransform(GeoFile)
        shape=Geo[0].shape
        utils.writeTiffFromDicoArray(Geo,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/altitude.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/altitude.tif")
    
        #On ecrit le fichier latlon 
        geoTransform=utils.getGeoTransform(RayFile)
        shape=Ray[0].shape
        utils.writeTiffFromDicoArray(latlon,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/latLon.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/latLon.tif")
        
        
        #On ecrit le fichier vent --> a enlever
        geoTransform=utils.getGeoTransform(ventFile[-1])
        shape=vent[0].shape
        utils.writeTiffFromDicoArray(vent,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/ventMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/ventMean.tif")
        
        #On ecrit le fichier Rhmin
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=pressureMean[0].shape
        utils.writeTiffFromDicoArray(pressureMean,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/pressureMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/pressureMean.tif")
        
        #On ecrit le fichier Rhmax
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=Hmax[0].shape
        utils.writeTiffFromDicoArray(Hmax,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/humidityMax.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/humidityMax.tif")
        
        #On ecrit le fichier Rhmin
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=Hmin[0].shape
        utils.writeTiffFromDicoArray(Hmin,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/humidityMin.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/humidityMin.tif")
    
        #On ecrit le fichier Tmax
        geoTransform=utils.getGeoTransform(T2mFile)
        shape=Tmax[0].shape
        utils.writeTiffFromDicoArray(Tmax,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/TemperatureMax.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/TemperatureMax.tif")
    
        #On ecrit le fichier Tmin
        geoTransform=utils.getGeoTransform(T2mFile)
        shape=Tmin[0].shape
        utils.writeTiffFromDicoArray(Tmin,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/TemperatureMin.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/TemperatureMin.tif")
        
        #On ecrit le fichier Rayonnement
        geoTransform=utils.getGeoTransform(RayFile)
        shape=Ray[0].shape
        utils.writeTiffFromDicoArray(Ray,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/RayonnementMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/RayonnementMean.tif")
    
    #on supprime les fichier intermédiare !
    os.remove(pressureFile)
    os.remove(T2mFile)
    os.remove(DewPFile)
    os.remove(RayFile)
    os.remove(GeoFile)
    for i in ventFile:
        os.remove(i)
    os.remove(precipitationFile)
    os.remove(EvapFile)
    os.remove(RayFileDownLong)
    os.remove(RayFileDownShort)
Exemple #3
0
def main(argv):

    try:
        opts, argv = getopt.getopt(argv, ":h:i:e:s:o:c:E:t:p:g:P:m:l:", [
            'help', '[outFile]', 'code', '[shapeFile]', 'start', 'end', '[tr]'
        ])
    except getopt.GetoptError:
        print 'error in parameter for GFSDownload. type GFSDownload.py -help for more detail on use '
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print 'GFSDownload.py  '
            print '    [mandatory] : ',
            print '        --code <GFSCode>'
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YYYY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --level <GFS Level> (default surface)'
            print '        --step <GFS Step> (default 3,6,9,12)'
            print '        --grid <GFS Grid> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/GFS)'
            print '        --proxy <proxy : True/False> (default False)'
            print '        --mode <mode : analyse/forcast/cycleforecast> (default analyse)'
            print ''
            print 'EXAMPLES'
            print '--temperature on a shapefile'
            print 'python GFSDownload.py -c PRES -i 2014-01-01 -e 2014-01-02 -s PATH_TO_SHAPE'
            print '--pressure on a area'
            print 'python GFSDownload.py -c 134 -i 2014-01-01 -e 2014-01-02 -E xmin,ymax,xmax,ymin'
            print ''
            print ' CODE PARAMETERS'
            print 'total precipitation  :  APCP [m of water]'
            print '2 metre temperature  :  TMP [K]'
            print 'maximum 2m temperature since last post-processing step : TMAX [K]'
            print 'minimum 2m temperature since last post-processing step : TMIN [K]'
            print 'surface pressure : PRES [Pa]'
            print '2 metre dewpoint : DPT [K]'
            print '10 metre eastward wind component UGRD [m s-1]'
            print '10 metre northward wind component VGRD [m s-1]'
            print '...'
            print ''
            print ' LEVEL PARAMETERS'
            print '2_m_above_ground'
            print '10_m_above_ground'
            print '3000-0_m_above_ground'
            print '300_mb'
            print '...'
            print 'see http://www.nco.ncep.noaa.gov/pmb/products/gfs/ for product description'
            sys.exit()
        elif opt in ('-o', '--outFolder'):
            oFolder = arg
        elif opt in ('-c', '--code'):
            codeGFS = arg.split(',')
        elif opt in ('-i', '--start'):
            startDate = arg
        elif opt in ('-e', '--end'):
            endDate = arg
        elif opt in ('-s', '--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E', '--tr'):
            extend = arg.split(',')
        elif opt in ('-g', '--grid'):
            grid = arg
        elif opt in ('-p', '--step'):
            step = arg.split(',')
        elif opt in ('-P', '--proxy'):
            proxy = arg
        elif opt in ('-m', '--mode'):
            mode = arg
        elif opt in ('-l', '--level'):
            levelList = arg.split(',')

    if len(sys.argv) < 8:
        print 'GFSDownload.py'
        print '    -c <GFSCode> -list possible-'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-g <size of grid in 0.25/0.5/1/2.5> (default 0.25)]'
        print '    [-p <GFS step parameter in 0,6,12,18> default 0,6,12,18] -list possible-'
        print '    [-o <outfolder> (default /home/user/GFS)]'
        print '    [-P <proxy> (default False)]'
        print '    [-l <level> (default 2_m_above_ground)]'
        print '    [-m <mode> (default analyse)]'
        print ''
        print 'For help on paramCode -help'
        sys.exit(2)

    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/GFS'
        print "output folder not precised : downloaded GFF images on " + oFolder

    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder)

    try:
        codeGFS
    except NameError:
        exit(
            'parameter(s) needed not precise. Please give the GFS parameter you wish'
        )
    utils.checkForParams(codeGFS)

    try:
        startDate
    except NameError:
        exit('init Date not precised')
    # verification si sartDate est une date
    startDate = utils.checkForDate(startDate)

    try:
        endDate
    except NameError:
        exit('end Date not specified')
    # verification si sartDate est une date
    endDate = utils.checkForDate(endDate)

    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit(
                'no Area of interest have been specified. please use -shp or -tr to declare it'
            )

    if 'pathToShapefile' in locals():
        extendArea = utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea = extend
    extendArea = utils.checkForExtendValidity(extendArea)

    try:
        levelList
    except NameError:
        levelList = ['surface']
    levelList = utils.checkForLevelValidity(levelList)

    try:
        grid
    except NameError:
        grid = '0.25'
    grid = utils.checkForGridValidity(grid)

    try:
        step
    except NameError:
        step = [0, 6, 12, 18]
    step = utils.checkForStepValidity(step)

    try:
        proxy
    except NameError:
        proxy = False

    try:
        mode
    except NameError:
        mode = 'analyse'

    #Proxy parameteres needed
    if (proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)

    #Download GFS
    struct = utils.create_request_gfs(startDate, endDate, step, levelList,
                                      grid, extendArea, codeGFS, mode)
    listeFile = []

    if len(struct) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                outpath = oFolder + '/' + ",".join(codeGFS) + '_' + i.rsplit(
                    '.', 1)[1] + '.grb'
                listeFile.append(outpath)
                result = utils.GFSDownload(i, outpath)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            utils.convertGribToTiff(listeFile, codeGFS, levelList, step, grid,
                                    startDate, endDate, oFolder)
        else:
            exit("PARAM needed is not compatible with level selected")

    if struct[1] is not None:
        print("")
        print("--------------------------------------------------")
        print("")
        print("Some parameters couldn't been downloaded in %s mode :" % mode)
        print("They have been downloaded in %s mode due to var %s" %
              (struct[1], ','.join(struct[2])))
def main(argv):

    try:
        opts, argv = getopt.getopt(argv, ":h:i:e:s:E:o:g:P:t:f:r:", [
            'help', '[outFile]', 'code', '[shapeFile]', 'start', 'end', '[tr]'
        ])
    except getopt.GetoptError:
        print 'error in parameter for evapo_GFS. type evapo_GFS.py -help for more detail on use '
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print 'evapo_GFS.py  '
            print '    [mandatory] : '
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --typeData  < analyse , forecast, cycleforecast> (default forecast)'
            print '        --grid <EraInterim Time> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/eraInterim)'
            print '        --proxy <True/False> (default False)'
            print '        --temporaryFile <True/False> (default False)'
            print '        --result < TxtFile / RasterFile> default RasterFile'
            print ''
            sys.exit()
        elif opt in ('-o', '--outFolder'):
            oFolder = arg
        elif opt in ('-i', '--start'):
            startDate = arg
        elif opt in ('-e', '--end'):
            endDate = arg
        elif opt in ('-s', '--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E', '--tr'):
            extend = arg.split(',')
        elif opt in ('-g', '--grid'):
            grid = arg
        elif opt in ('-P', '--proxy'):
            proxy = arg
        elif opt in ('-t', '--typeData'):
            typeData = arg
        elif opt in ('-f', '--temporaryFile'):
            temporaryFile = arg
        elif opt in ('-r', '--result'):
            typeOutput = arg

    if len(sys.argv) < 7:
        print 'evapo_GFS.py'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-t < analyse , forecast,cycleforecast> (default analyse)]'
        print '    [-g <size of grid in 0.25/0.5/1/2.5> (default 0.25)]'
        print '    [-o <outfolder> (default /home/user/eraInterim)]'
        print '    [-P <proxy : True/False> (default False)]'
        print '    [-f <temporaryFile : True/False> (default False)]'
        print '    [-r <resultOutput : TxtFile/RasterFile> (default RasterFile)]'
        print ''
        sys.exit(2)

    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/GFS'
        print "output folder not precised : downloaded GFS images on " + oFolder

    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder)

    try:
        startDate
    except NameError:
        exit('init Date not precised')
    # verification si sartDate est une date
    startDate = utils.checkForDate(startDate)

    try:
        endDate
    except NameError:
        exit('end Date not specified')
    # verification si sartDate est une date
    endDate = utils.checkForDate(endDate)

    if (startDate > endDate):
        exit('startDate could not be greater than endDate')

    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit(
                'no Area of interest have been specified. please use -shp or -tr to declare it'
            )

    if 'pathToShapefile' in locals():
        extendArea = utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea = extend

    extendArea = utils.checkForExtendValidity(extendArea)

    try:
        typeData
    except NameError:
        typeData = 'analyse'

    try:
        grid
    except NameError:
        grid = '0.25'
    grid = utils.checkForGridValidity(grid)

    try:
        proxy
    except NameError:
        proxy = False

    #Proxy parameteres needed
    if (proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)

    try:
        temporaryFile
    except NameError:
        temporaryFile = False

    try:
        typeOutput
    except NameError:
        typeOutput = 'RasterFile'
    """----------------------------------------"""

    delta = endDate - startDate
    nbDays = delta.days + float(delta.seconds) / 86400 + 1

    #--------------------------On charge les rasters
    step = [0, 6, 12, 18]
    levelList = ["surface"]
    nbBandByDay = len(step)
    """ altitude de la grille GFS """
    print('downloading altitude grid')
    # Only Forecast possible
    codeGeopot = ['HGT']
    struct = utils.create_request_gfs(startDate, endDate, step, levelList,
                                      grid, extendArea, codeGeopot, typeData)
    listeGeoFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                GeoFile = oFolder + '/' + ",".join(
                    codeGeopot) + '_' + i.rsplit('.', 1)[1] + '.grb'
                listeGeoFile.append(GeoFile)
                result = utils.GFSDownload(i, GeoFile)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            Geo = utils.convertGribToDicoArray(listeGeoFile, codeGeopot,
                                               levelList, step, grid,
                                               startDate, endDate)
            Geo = utils.convertGeoToAlt(Geo)
            #un peu inutile car ne change pas ... mais bon!
            Geo = utils.computeDailyMax(Geo, nbBandByDay, typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    """ Vitesse du vent """
    print('downloading wind velocity')
    codeVent = ['UGRD', 'VGRD']
    levelListVent = ["80_m_above_ground"]
    vent = {}
    ventFile = []
    for v in codeVent:
        struct = utils.create_request_gfs(startDate, endDate, step,
                                          levelListVent, grid, extendArea,
                                          codeVent, typeData)
        listeFile = []
        if len(struct[0]) == 0:
            exit("No data founded")
        else:
            for i in struct[0]:
                try:
                    tmpVent = oFolder + '/' + v + '_' + i.rsplit('.',
                                                                 1)[1] + '.grb'
                    ventFile.append(tmpVent)
                    result = utils.GFSDownload(i, tmpVent)
                except:
                    print("---")
                    exit('Error in GFS server')

            if result:
                vent[v] = utils.convertGribToDicoArray(ventFile, codeVent,
                                                       levelList, step, grid,
                                                       startDate, endDate)
            else:
                exit("PARAM needed is not compatible with level selected")

    vent = utils.fusVentFromDict(vent, nbBandByDay)
    vent = utils.computeDailyMean(vent, nbBandByDay, typeData)
    exit
    """ Humidité relative """
    print('downloading relative humidity')
    codePressure = ['PRES']
    struct = utils.create_request_gfs(startDate, endDate, step, levelList,
                                      grid, extendArea, codePressure, typeData)
    listePressureFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                pressureFile = oFolder + '/' + ",".join(
                    codePressure) + '_' + i.rsplit('.', 1)[1] + '.grb'
                listePressureFile.append(pressureFile)
                result = utils.GFSDownload(i, pressureFile)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            pressure = utils.convertGribToDicoArray(listePressureFile,
                                                    codePressure, levelList,
                                                    step, grid, startDate,
                                                    endDate)
            pressureMean = utils.convertPaToKgPa(pressure)
            pressure = utils.convertToHectoPascal(pressure)
            pressureMean = utils.computeDailyMean(pressureMean, nbBandByDay,
                                                  typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    """ Temperature 2m """
    print('downloading Temperature')
    levelListTmp = ["2_m_above_ground"]
    codeT2m = ['TMP']
    struct = utils.create_request_gfs(startDate, endDate, step, levelListTmp,
                                      grid, extendArea, codeT2m, typeData)
    listeTmpFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                T2mFile = oFolder + '/' + ",".join(codeT2m) + '_' + i.rsplit(
                    '.', 1)[1] + '.grb'
                listeTmpFile.append(T2mFile)
                result = utils.GFSDownload(i, T2mFile)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            T2m = utils.convertGribToDicoArray(listeTmpFile, codeT2m,
                                               levelList, step, grid,
                                               startDate, endDate)
            Tmean = utils.computeDailyMean(T2m, nbBandByDay, typeData)
            Tmax = utils.computeDailyMax(T2m, nbBandByDay)
            Tmin = utils.computeDailyMin(T2m, nbBandByDay)
            T2m = utils.convertKToD(T2m)
        else:
            exit("PARAM needed is not compatible with level selected")
    """ DewPoint """
    print('downloading DewPoint')
    levelListdew = ["2_m_above_ground"]
    codeDewP = ['DPT']
    struct = utils.create_request_gfs(startDate, endDate, step, levelListdew,
                                      grid, extendArea, codeDewP, typeData)
    listeDewFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                DewPFile = oFolder + '/' + ",".join(codeDewP) + '_' + i.rsplit(
                    '.', 1)[1] + '.grb'
                listeDewFile.append(DewPFile)
                result = utils.GFSDownload(i, DewPFile)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            DewP = utils.convertGribToDicoArray(listeDewFile, codeDewP,
                                                levelList, step, grid,
                                                startDate, endDate)
            DewP = utils.convertKToD(DewP)
        else:
            exit("PARAM needed is not compatible with level selected")

    humidity = utils.ComputeHumidityFromPT(pressure, T2m, DewP)
    #humidity = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    Hmax = utils.computeDailyMax(humidity, nbBandByDay)
    Hmin = utils.computeDailyMin(humidity, nbBandByDay)
    Hmean = utils.computeDailyMean(humidity, nbBandByDay, typeData)
    """ Rayonnement global incident journalier """
    print('downloading downscale shortwave radiation')
    # Only Forecast possible
    levelListRay = ["surface"]
    codeRay = ["DSWRF"]
    typeData = "cycleforecast"
    struct = utils.create_request_gfs(startDate, endDate, step, levelListRay,
                                      grid, extendArea, codeRay, typeData)
    listeRayFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                RayFileDownShort = oFolder + '/' + ",".join(
                    codeRay) + '_' + i.rsplit('.', 1)[1] + '.grb'
                listeRayFile.append(RayFileDownShort)
                result = utils.GFSDownload(i, RayFileDownShort)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            RayDownShort = utils.convertGribToDicoArray(
                listeRayFile, codeRay, levelList, step, grid, startDate,
                endDate)
            RayDownShort = utils.computeDailyMean(RayDownShort, nbBandByDay,
                                                  typeData)
            RayDownShort = utils.convertWToMJ(RayDownShort)
        else:
            exit("PARAM needed is not compatible with level selected")
    """ Precipitation """
    #NOT NEEDED FOR ETO BUT Exported
    print('downloading Precipitation')
    codePrecipitation = ["APCP"]
    struct = utils.create_request_gfs(startDate, endDate, step, levelList,
                                      grid, extendArea, codePrecipitation,
                                      typeData)
    listePrecFile = []
    if len(struct[0]) == 0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try:
                precipitationFile = oFolder + '/' + ",".join(
                    codePrecipitation) + '_' + i.rsplit('.', 1)[1] + '.grb'
                listePrecFile.append(precipitationFile)
                result = utils.GFSDownload(i, precipitationFile)
            except:
                print("---")
                exit('Error in GFS server')

        if result:
            precipitation = utils.convertGribToDicoArray(
                listePrecFile, codePrecipitation, levelList, step, grid,
                startDate, endDate)
            precipitation = utils.computeDailyAccumulation(
                precipitation, nbBandByDay, typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    """ Grid of latitude [0],longitude[1] in WGS84"""
    geoTransform = utils.getGeoTransform(RayFileDownShort)
    shape = RayDownShort[0].shape
    latlon = utils.getCentroidLatFromArray(shape, geoTransform, grid)
    """ --------------------- Compute ET0---------------------- """

    ET0_0 = {}
    DoyList = []
    DateList = []

    for i in range(0, int(nbDays)):
        #jour Julien
        J = utils.doy(startDate, i)
        dateEnCours = startDate + timedelta(days=i)
        DateList.append(dateEnCours)
        DoyList.append(J)
        Hmax[i] = np.where(Hmax[i] > 100, 100, Hmax[i])

        # --- Constants ---#
        #Solar constant
        Gsc = 0.0820  # [MJ.m-2.min-1]
        #Albedo - grass reference crop
        a = 0.23
        #Ratio of molecular weight of water vapor/dry air
        epsilon = 0.622
        #Latente heat of vaporisation
        Lv = 2.45  # [MJ.kg-1]
        # Specific heat at constant pressure
        Cp = 1.013e-3
        # [MJ.kg-1.°C-1]
        # Stefan-Boltzmann constant [MJ.K-4.m-2.day-1]
        StefBoltz = 4.903e-9
        #FAO

        # --- Equations ---#
        # Psychometric constant [kPa.°C-1]
        cte_psy = (Cp * pressureMean[i]) / (epsilon * Lv
                                            )  # Equation 8 Chap 3 FAO
        #Mean sturation vapor presure [kPa]
        #es = (utils.esat(pressureMean[i],Tmax[i]) + utils.esat(pressureMean[i],Tmin[i]))/2;    #Equation 12 Chap 3
        es = (utils.eocalc(Tmax[i] - 273.16) +
              utils.eocalc(Tmin[i] - 273.16)) / 2  #Equation 12 Chap 3
        # Slope of saturation vapour pressure curve at air temperature [kPa.°C-1]
        delta = utils.delta_calc(Tmean[i])
        # Equation 13 Chap 3
        # Actual vapour pressure derived from relative humidity [kPa]
        #ea = (utils.esat(pressureMean[i]/100,Tmax[i]-273.16)*(Hmax[i]/100) + utils.esat(pressureMean[i]/100,Tmin[i]-273.16)*(Hmin[i]/100))/2;      # Equation 17 Chap 3
        ea = (utils.eocalc(Tmax[i] - 273.16) *
              (Hmax[i] / 100) + utils.eocalc(Tmin[i] - 273.16) *
              (Hmin[i] / 100)) / 2
        # Conversion of latitude from degrees to radians
        phi = (np.pi / 180) * latlon[1]
        # Relative distance Earth-Sun
        dr = 1 + 0.033 * math.cos(2 * math.pi * J / 365)
        # Equation 23 Chap 3
        # Solar declination
        d = 0.4093 * math.sin(2 * math.pi * J / 365 - 1.39)
        # Equation 24 Chap 3
        # sunset hour angle
        ws = np.arccos(-np.tan(phi) * math.tan(d))
        # Equation 25 Chap 3
        """Classical calculation FAO """

        # Extraterestrial radiation for daily periods
        Ra = (24. * 60 / np.pi) * Gsc * dr * (
            ws * np.sin(phi) * np.sin(d) + np.cos(phi) * np.cos(d) * np.sin(ws)
        )  # Equation 21 Chap 3
        # Clear sky solar radiation [MJ.m-2.day-1]
        Rso = (0.75 + 2e-5 * Geo[i]) * Ra
        # Equation 37 Chap 3
        # Net solar radiation [MJ.m-2.day-1]
        Rns = (1 - a) * RayDownShort[i]
        # Equation 38 Chap 3
        #
        f = (1.35 * (np.fmin(RayDownShort[i] / Rso, 1)) - 0.35)
        # Net longwave radiation [MJ.m-2.day-1]
        Rnl = StefBoltz * (
            (Tmax[i]**4 + Tmin[i]**4) / 2) * (0.34 - 0.14 * np.sqrt(ea)) * f
        # Equation 39 Chap 3
        # Net Radiation [MJ.m-2.day-1]
        Rn = Rns - Rnl
        # Equation 40 Chap 3
        G = 0
        # Equation 42 Chap 3
        ET0_0[i] = (0.408 * delta * (Rn - G) + cte_psy * (900 /
                                                          (Tmean[i] + 273)) *
                    (es - ea) * vent[i]) / (delta + cte_psy *
                                            (1 + 0.34 * vent[i]))
        # Equation 6 Chap 4

    if typeOutput == 'RasterFile':
        #On ecrit le fichier ET0
        geoTransform = utils.getGeoTransform(RayFileDownShort)
        shape = RayDownShort[0].shape
        utils.writeTiffFromDicoArray(ET0_0, oFolder + "/tmp.tif", shape,
                                     geoTransform)
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/ET0.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/ET0.tif")

        #On écrit le fichier Precipitation
        geoTransform = utils.getGeoTransform(precipitationFile)
        shape = precipitation[0].shape
        utils.writeTiffFromDicoArray(precipitation, oFolder + "/tmp.tif",
                                     shape, geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/precipitationAcc.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/precipitationAcc.tif")

    else:
        #On ecrit le fichier au format Txt
        proj = utils.getProj(pathToShapefile)
        utils.WriteTxtFileForEachPixel(oFolder, ET0_0, DateList, DoyList,
                                       RayDownShort, Tmean, Tmax, Tmin, Hmean,
                                       Hmax, Hmin, vent, precipitation,
                                       pressureMean, Geo, latlon, proj)
        utils.WritePointList(oFolder, latlon, proj)
    """ ------------------------------------------- """
    if (temporaryFile):
        #On ecrit le fichier latlon
        geoTransform = utils.getGeoTransform(GeoFile)
        shape = Geo[0].shape
        utils.writeTiffFromDicoArray(Geo, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/altitude.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/altitude.tif")

        #On ecrit le fichier latlon
        geoTransform = utils.getGeoTransform(RayFileDownShort)
        shape = RayDownShort[0].shape
        utils.writeTiffFromDicoArray(latlon, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/latLon.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/latLon.tif")

        #On ecrit le fichier vent --> a enlever
        geoTransform = utils.getGeoTransform(ventFile[-1])
        shape = vent[0].shape
        utils.writeTiffFromDicoArray(vent, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/ventMean.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/ventMean.tif")

        #On ecrit le fichier Rhmin
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = pressureMean[0].shape
        utils.writeTiffFromDicoArray(pressureMean, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/pressureMean.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/pressureMean.tif")

        #On ecrit le fichier Rhmax
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = Hmax[0].shape
        utils.writeTiffFromDicoArray(Hmax, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/humidityMax.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/humidityMax.tif")

        #On ecrit le fichier Rhmin
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = Hmin[0].shape
        utils.writeTiffFromDicoArray(Hmin, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/humidityMin.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/humidityMin.tif")

        #On ecrit le fichier Tmax
        geoTransform = utils.getGeoTransform(T2mFile)
        shape = Tmax[0].shape
        utils.writeTiffFromDicoArray(Tmax, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/TemperatureMax.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/TemperatureMax.tif")

        #On ecrit le fichier Tmin
        geoTransform = utils.getGeoTransform(T2mFile)
        shape = Tmin[0].shape
        utils.writeTiffFromDicoArray(Tmin, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/TemperatureMin.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/TemperatureMin.tif")

        #On ecrit le fichier Rayonnement
        geoTransform = utils.getGeoTransform(RayFileDownShort)
        shape = RayDownShort[0].shape
        utils.writeTiffFromDicoArray(RayDownShort, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/RayonnementMean.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/RayonnementMean.tif")

    #on supprime les fichier intermédiare !
    for i in listePressureFile:
        os.remove(i)
    for i in listeTmpFile:
        os.remove(i)
    for i in listeDewFile:
        os.remove(i)
    for i in listeRayFile:
        os.remove(i)
    for i in listeGeoFile:
        os.remove(i)
    for i in ventFile:
        os.remove(i)
    for i in listePrecFile:
        os.remove(i)
Exemple #5
0
def main(argv):

    try:
        opts, argv = getopt.getopt(argv, ":h:i:e:s:o:c:E:t:p:g:P:m:", [
            'help', '[outFile]', 'code', '[shapeFile]', 'start', 'end', '[tr]'
        ])
    except getopt.GetoptError:
        print(
            'error in parameter for eraInterimDownload. type eraInterimDownload.py -help for more detail on use '
        )
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print('eraInterimDownload.py  ')
            print('    [mandatory] : ', end=' ')
            print('        --code <EraInterimCode>')
            print('        --init <dateStart YYYY-MM-DD>')
            print('        --end <dateEnd YY-MM-DD>')
            print(
                '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            )
            print('    [optional] :')
            print('        --time <EraInterim Time> (default 00)')
            print('        --step <EraInterim Step> (default 3,6,9,12)')
            print('        --grid <EraInterim Time> (default 0.75)')
            print(
                '        --outfile <outfolder> (default /home/user/eraInterim)'
            )
            print('        --proxy <proxy : True/False> (default False)')
            print('        --mode <mode : analyse/forcast> (default analyse)')
            print('')
            print('EXAMPLES')
            print('--temperature on a shapefile')
            print(
                'python eraInterimDownload.py -c 167 -i 2014-01-01 -e 2014-01-02 -s PATH_TO_SHAPE'
            )
            print('--pressure on a area')
            print(
                'python eraInterimDownload.py -c 134 -i 2014-01-01 -e 2014-01-02 -E xmin,ymax,xmax,ymin'
            )
            print('')
            print(' CODE PARAMETERS')
            print('')
            print('total precipitation  :  228 [m of water]')
            print('2 metre temperature  :  167 [K]')
            print(
                'maximum 2m temperature since last post-processing step : 201 [K]'
            )
            print(
                'minimum 2m temperature since last post-processing step : 202 [K]'
            )
            print('surface pressure : 134 [Pa]')
            print('2 metre dewpoint : 168 [K]')
            print('10 metre eastward wind component X X 165 [m s-1]')
            print('10 metre northward wind component X X 166 [m s-1]')
            print('...')
            print(
                'see http://old.ecmwf.int/publications/library/ecpublications/_pdf/era/era_report_series/RS_1_v2.pdf for more references'
            )
            sys.exit()
        elif opt in ('-o', '--outFolder'):
            oFolder = arg
        elif opt in ('-c', '--code'):
            codeEra = arg.split(',')
        elif opt in ('-i', '--start'):
            startDate = arg
        elif opt in ('-e', '--end'):
            endDate = arg
        elif opt in ('-s', '--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E', '--tr'):
            extend = arg.split(',')
        elif opt in ('-t', '--time'):
            time = arg.split(',')
        elif opt in ('-g', '--grid'):
            grid = arg
        elif opt in ('-p', '--step'):
            step = arg.split(',')
        elif opt in ('-P', '--proxy'):
            proxy = arg
        elif opt in ('-m', '--mode'):
            mode = arg

    if len(sys.argv) < 8:
        print('eraInterimDownload.py')
        print('    -c <EraInterimCode> -list possible-')
        print('    -i <dateStart YYYY-MM-DD> ')
        print('    -e <dateEnd YY-MM-DD>')
        print('    -s <shapefile> ')
        print('  or')
        print('    -E < xmin,ymax,xmax,ymin>]')
        print('')
        print(
            '    [-t <eraInterim time parameters in 00/06/12/18> (default 00,12)] -list possible-'
        )
        print(
            '    [-g <size of grid in 0.125/0.25/0.5/0.75/1.125/1.5/2/2.5/3> (default 0.75)]'
        )
        print(
            '    [-p <eraInterim step parameter in 00/03/06/12> default 3,6,9,12] -list possible-'
        )
        print('    [-o <outfolder> (default /home/user/eraInterim)]')
        print('    [-P <proxy> (default False)]')
        print('')
        print('For help on interimCode -help')
        sys.exit(2)

    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/eraInterim'
        print("output folder not precised : downloaded eraInterim images on " +
              oFolder)

    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder)

    try:
        codeEra
    except NameError:
        exit(
            'parameters need not precise. Please give the era Interim parameter you wish'
        )

    try:
        startDate
    except NameError:
        exit('init Date not precised')
    # verification si sartDate est une date
    startDate = utils.checkForDate(startDate)

    try:
        endDate
    except NameError:
        exit('end Date not specified')
    # verification si sartDate est une date
    endDate = utils.checkForDate(endDate)

    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit(
                'no Area of interest have been specified. please use -shp or -tr to declare it'
            )

    if 'pathToShapefile' in locals():
        extendArea = utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea = extend

    extendArea = utils.checkForExtendValidity(extendArea)

    try:
        time
    except NameError:
        time = ['00', '12']
    time = utils.checkForTimeValidity(time)

    try:
        grid
    except NameError:
        grid = '0.75'
    grid = utils.checkForGridValidity(grid)

    try:
        step
    except NameError:
        step = [3, 6, 9, 12]
    step = utils.checkForStepValidity(step)

    try:
        proxy
    except NameError:
        proxy = False

    try:
        mode
    except NameError:
        mode = 'analyse'

    #Proxy parameteres needed
    if (proxy):
        login = input('login proxy : ')
        pwd = input('password proxy :  : ')
        site = input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)

    #Create param if first Time
    if (not utils.checkForFile(os.path.expanduser('~') + '/.ecmwfapirc')):
        print(
            'for first connexion you have to define yout key and password on ecmwf'
        )
        print('cf  https://apps.ecmwf.int/auth/login/')
        print('')
        u = input('user (mail) : ')
        k = input('keys : ')
        utils.createParamFile(os.path.expanduser('~') + '/.ecmwfapirc', u, k)

    #Download NETCDF
    server = ECMWFDataServer()
    outNETCDFFile = oFolder + '/' + "/".join(
        [str(x) for x in codeEra]) + '_' + startDate.strftime(
            '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeEra, outNETCDFFile, mode)

    if len(struct[0]) == 0:
        exit()
    else:
        for i in struct[0]:
            try:
                server.retrieve(i)
            except:
                print("---")
                exit('Error in EraInterim server')

    if struct[1] is not None:
        print("")
        print("--------------------------------------------------")
        print("")
        print(("Some parameters couldn't been downloaded in %s mode :" % mode +
               ' ' + struct[1]))
        print(("They have been downloaded in %s mode" % struct[2]))

    utils.convertNETCDFtoTIF(outNETCDFFile, oFolder + '/tmp.tif')
    shape = utils.getShape(outNETCDFFile)
    if ('pathToShapefile' in locals()):
        utils.reprojRaster(oFolder + '/tmp.tif',
                           outNETCDFFile.rsplit('.')[0] + '.tif', shape,
                           pathToShapefile)
    else:
        utils.reprojRaster(oFolder + '/tmp.tif',
                           outNETCDFFile.rsplit('.')[0] + '.tif', shape)

    os.remove(oFolder + '/tmp.tif')
    os.remove(outNETCDFFile)
def main(argv):

    try:
        opts,argv = getopt.getopt(argv,":h:i:e:s:o:c:E:t:p:g:P:m:",['help','[outFile]','code','[shapeFile]','start','end','[tr]'])
    except getopt.GetoptError:
        print 'error in parameter for eraInterimDownload. type eraInterimDownload.py -help for more detail on use '
        sys.exit(2)
    
    for opt, arg in opts:
        if opt == '-h':
            print 'eraInterimDownload.py  '
            print '    [mandatory] : ',
            print '        --code <EraInterimCode>'
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --time <EraInterim Time> (default 00)'
            print '        --step <EraInterim Step> (default 3,6,9,12)'
            print '        --grid <EraInterim Time> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/eraInterim)'
            print '        --proxy <proxy : True/False> (default False)'
            print '        --mode <mode : analyse/forcast> (default analyse)'
            print ''
            print 'EXAMPLES'
            print '--temperature on a shapefile'
            print 'python eraInterimDownload.py -c 167 -i 2014-01-01 -e 2014-01-02 -s PATH_TO_SHAPE'
            print '--pressure on a area'
            print 'python eraInterimDownload.py -c 134 -i 2014-01-01 -e 2014-01-02 -E xmin,ymax,xmax,ymin'
            print ''
            print ' CODE PARAMETERS'
            print ''
            print 'total precipitation  :  228 [m of water]'
            print '2 metre temperature  :  167 [K]'
            print 'maximum 2m temperature since last post-processing step : 201 [K]'
            print 'minimum 2m temperature since last post-processing step : 202 [K]'
            print 'surface pressure : 134 [Pa]'
            print '2 metre dewpoint : 168 [K]'
            print '10 metre eastward wind component X X 165 [m s-1]'
            print '10 metre northward wind component X X 166 [m s-1]'
            print '...'
            print 'see http://old.ecmwf.int/publications/library/ecpublications/_pdf/era/era_report_series/RS_1_v2.pdf for more references'
            sys.exit() 
        elif opt in ('-o','--outFolder'):
            oFolder = arg
        elif opt in ('-c','--code'):
            codeEra = arg.split(',')
        elif opt in ('-i','--start'):
            startDate = arg
        elif opt in ('-e','--end'):
            endDate = arg
        elif opt in ('-s','--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E','--tr'):
            extend = arg.split(',')
        elif opt in ('-t','--time'):
            time = arg.split(',')
        elif opt in ('-g','--grid'):
            grid = arg
        elif opt in ('-p','--step'):
            step = arg.split(',')
        elif opt in ('-P','--proxy'):
            proxy = arg
        elif opt in ('-m','--mode'):
            mode = arg
    
    if len(sys.argv) < 8:
        print 'eraInterimDownload.py'
        print '    -c <EraInterimCode> -list possible-'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-t <eraInterim time parameters in 00/06/12/18> (default 00,12)] -list possible-'
        print '    [-g <size of grid in 0.125/0.25/0.5/0.75/1.125/1.5/2/2.5/3> (default 0.75)]'
        print '    [-p <eraInterim step parameter in 00/03/06/12> default 3,6,9,12] -list possible-'
        print '    [-o <outfolder> (default /home/user/eraInterim)]'
        print '    [-P <proxy> (default False)]'
        print ''
        print 'For help on interimCode -help'
        sys.exit(2)
        
    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/eraInterim'
        print "output folder not precised : downloaded eraInterim images on "+oFolder
    
    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder) 
    
    try:
        codeEra
    except NameError:
        exit ('parameters need not precise. Please give the era Interim parameter you wish')
    
    try:
        startDate
    except NameError:
        exit ('init Date not precised')
    # verification si sartDate est une date
    startDate=utils.checkForDate(startDate) 
    
    try:
        endDate
    except NameError:
        exit ('end Date not specified')
    # verification si sartDate est une date
    endDate=utils.checkForDate(endDate) 
    
    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit ('no Area of interest have been specified. please use -shp or -tr to declare it')
    
    if 'pathToShapefile' in locals():
        extendArea=utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea=extend

    extendArea=utils.checkForExtendValidity(extendArea)
    
    try:
        time
    except NameError:
        time=['00','12']
    time=utils.checkForTimeValidity(time)
    
    try:
        grid
    except NameError:
        grid='0.75'
    grid=utils.checkForGridValidity(grid)
        
    try:
        step
    except NameError:
        step=[3,6,9,12]
    step=utils.checkForStepValidity(step)
    
    try:
        proxy
    except NameError:
        proxy=False
        
    try:
        mode
    except NameError:
        mode='analyse'
    
    #Proxy parameteres needed
    if(proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
    
    
    #Create param if first Time
    if (not utils.checkForFile(os.path.expanduser('~')+'/.ecmwfapirc')):
        print ('for first connexion you have to define yout key and password on ecmwf')
        print ('cf  https://apps.ecmwf.int/auth/login/')
        print ('')
        u = raw_input('user (mail) : ')
        k = raw_input('keys : ')
        utils.createParamFile(os.path.expanduser('~')+'/.ecmwfapirc',u,k)
        
    
    #Download NETCDF
    server = ECMWFDataServer()
    outNETCDFFile=oFolder+'/'+"/".join([str(x) for x in codeEra])+'_'+startDate.strftime('%Y%m%d')+'_'+endDate.strftime('%Y%m%d')+'.nc'
    struct=utils.create_request_sfc(startDate, endDate, time, step, grid, extendArea, codeEra,outNETCDFFile,mode)
    
    if len(struct[0])==0:
        exit()
    else:
        for i in struct[0]:
            try :
                server.retrieve(i)
            except:
                print("---")
                exit('Error in EraInterim server')
    

    
    if struct[1] is not None:
        print ("")
        print ("--------------------------------------------------")
        print ("")
        print ("Some parameters couldn't been downloaded in %s mode :" % mode + ' '+ struct[1]  )
        print ("They have been downloaded in %s mode" % struct[2] )

    
    utils.convertNETCDFtoTIF(outNETCDFFile, oFolder+'/tmp.tif')
    shape=utils.getShape(outNETCDFFile)
    if ('pathToShapefile' in locals()):
        utils.reprojRaster(oFolder+'/tmp.tif',outNETCDFFile.rsplit('.')[0]+'.tif',shape,pathToShapefile)
    else:
        utils.reprojRaster(oFolder+'/tmp.tif',outNETCDFFile.rsplit('.')[0]+'.tif',shape)
    
    os.remove(oFolder+'/tmp.tif')
    os.remove(outNETCDFFile)
def main(argv):

    try:
        opts, argv = getopt.getopt(argv, ":h:i:e:s:E:o:g:P:t:f:r:", [
            'help', '[outFile]', 'code', '[shapeFile]', 'start', 'end', '[tr]'
        ])
    except getopt.GetoptError:
        print 'error in parameter for eraInterimDownload. type eraInterimDownload.py -help for more detail on use '
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print 'eraInterimDownload.py  '
            print '    [mandatory] : '
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --typeData  < analyse , forcast> (default forcast)'
            print '        --grid <EraInterim Time> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/eraInterim)'
            print '        --proxy <True/False> (default False)'
            print '        --temporaryFile <True/False> (default False)'
            print '        --result < TxtFile / RasterFile> default RasterFile'
            print ''
            sys.exit()
        elif opt in ('-o', '--outFolder'):
            oFolder = arg
        elif opt in ('-i', '--start'):
            startDate = arg
        elif opt in ('-e', '--end'):
            endDate = arg
        elif opt in ('-s', '--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E', '--tr'):
            extend = arg.split(',')
        elif opt in ('-g', '--grid'):
            grid = arg
        elif opt in ('-P', '--proxy'):
            proxy = arg
        elif opt in ('-t', '--typeData'):
            typeData = arg
        elif opt in ('-f', '--temporaryFile'):
            temporaryFile = arg
        elif opt in ('-r', '--result'):
            typeOutput = arg

    if len(sys.argv) < 7:
        print 'eraInterimDownload.py'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-t < analyse , forcast> (default analyse)]'
        print '    [-g <size of grid in 0.125/0.25/0.5/0.75/1.125/1.5/2/2.5/3> (default0.75)]'
        print '    [-o <outfolder> (default /home/user/eraInterim)]'
        print '    [-P <proxy : True/False> (default False)]'
        print '    [-f <temporaryFile : True/False> (default False)]'
        print '    [-r <resultOutput : TxtFile/RasterFile> (default RasterFile)]'
        print ''
        print 'For help on interimCode -help'
        sys.exit(2)

    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/eraInterim'
        print "output folder not precised : downloaded eraInterim images on " + oFolder

    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder)

    try:
        startDate
    except NameError:
        exit('init Date not precised')
    # verification si sartDate est une date
    startDate = utils.checkForDate(startDate)

    try:
        endDate
    except NameError:
        exit('end Date not specified')
    # verification si sartDate est une date
    endDate = utils.checkForDate(endDate)

    if (startDate > endDate):
        exit('startDate could not be greater than endDate')

    today = date.today()
    limitDate = today - timedelta(days=31 * 3)
    limitDate = date(limitDate.year, limitDate.month,
                     calendar.monthrange(limitDate.year, limitDate.month)[1])
    if (startDate > limitDate or endDate > limitDate):
        exit('date could not exceed 2014-12-31')

    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit(
                'no Area of interest have been specified. please use -shp or -tr to declare it'
            )

    if 'pathToShapefile' in locals():
        extendArea = utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea = extend

    extendArea = utils.checkForExtendValidity(extendArea)

    try:
        typeData
    except NameError:
        typeData = 'analyse'

    try:
        grid
    except NameError:
        grid = '0.75'
    grid = utils.checkForGridValidity(grid)

    try:
        proxy
    except NameError:
        proxy = False

    #Proxy parameteres needed
    if (proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050" % (login, pwd, site)

    try:
        temporaryFile
    except NameError:
        temporaryFile = False

    try:
        typeOutput
    except NameError:
        typeOutput = 'RasterFile'
    """----------------------------------------"""

    #Create param if first Time
    if (not utils.checkForFile(os.path.expanduser('~') + '/.ecmwfapirc')):
        print(
            'for first connexion you have to define yout key and password on ecmwf'
        )
        print('cf  https://apps.ecmwf.int/auth/login/')
        print('')
        u = raw_input('user (mail) : ')
        k = raw_input('keys : ')
        utils.createParamFile(os.path.expanduser('~') + '/.ecmwfapirc', u, k)

    delta = endDate - startDate
    nbDays = delta.days + float(delta.seconds) / 86400 + 1

    #--------------------------On charge les rasters
    if typeData == "analyse":
        time = ['00', "12", "06", "18"]
        step = []
        nbBandByDay = len(time)
    else:
        time = ['00', "12"]
        step = [3, 6, 9, 12]
        nbBandByDay = (12 * len(time)) / (len(step)) + 1
    server = ECMWFDataServer()
    """ altitude de la grille EraInterim """
    # Only Forcast possible
    codeGeopot = [129]
    GeoFile = oFolder + "/129" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeGeopot, GeoFile,
                                      typeData)
    server.retrieve(struct)
    Geo = utils.convertNETCDFtoDicArray(GeoFile)
    Geo = utils.convertGeoToAlt(Geo)
    #un peu inutile car ne change pas ... mais bon!
    Geo = utils.computeDailyMax(Geo, nbBandByDay, typeData)
    """ Vitesse du vent """
    codeVent = [165, 166]
    vent = {}
    ventFile = []
    for i in codeVent:
        ventFile.append(oFolder + "/" + str(i) + '_' +
                        startDate.strftime('%Y%m%d') + '_' +
                        endDate.strftime('%Y%m%d') + '.nc')
        struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                          extendArea, [i], ventFile[-1],
                                          typeData)
        server.retrieve(struct)
        vent[i] = utils.convertNETCDFtoDicArray(ventFile[-1])

    vent = utils.fusVentFromDict(vent, nbBandByDay)
    vent = utils.computeDailyMean(vent, nbBandByDay, typeData)
    """ Humidité relative """

    codePressure = [134]
    pressureFile = oFolder + "/134" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'

    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codePressure, pressureFile,
                                      typeData)
    server.retrieve(struct)
    pressure = utils.convertNETCDFtoDicArray(pressureFile)
    #oulalal c'est moche
    pressureMean = utils.convertPaToKgPa(pressure)
    pressure = utils.convertToHectoPascal(pressure)
    pressureMean = utils.computeDailyMean(pressureMean, nbBandByDay, typeData)

    codeT2m = [167]
    T2mFile = oFolder + "/167" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'

    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeT2m, T2mFile, typeData)
    server.retrieve(struct)
    T2m = utils.convertNETCDFtoDicArray(T2mFile)

    Tmean = utils.computeDailyMean(T2m, nbBandByDay, typeData)
    Tmax = utils.computeDailyMax(T2m, nbBandByDay)
    Tmin = utils.computeDailyMin(T2m, nbBandByDay)

    T2m = utils.convertKToD(T2m)
    #T2m = utils.computeDailyMean(T2m,nbBandByDay,typeData)

    codeDewP = [168]
    DewPFile = oFolder + "/168" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'

    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeDewP, DewPFile, typeData)
    server.retrieve(struct)
    DewP = utils.convertNETCDFtoDicArray(DewPFile)
    DewP = utils.convertKToD(DewP)
    #DewP = utils.computeDailyMean(DewP,nbBandByDay,typeData)

    humidity = utils.ComputeHumidityFromPT(pressure, T2m, DewP)
    #humidity = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    Hmax = utils.computeDailyMax(humidity, nbBandByDay)
    Hmin = utils.computeDailyMin(humidity, nbBandByDay)
    Hmean = utils.computeDailyMean(humidity, nbBandByDay, typeData)
    """ ONLY FORCAST FOR THESE VAR"""
    typeData = "forcast"
    time = ['00', "12"]
    step = [3, 6, 9, 12]
    nbBandByDay = (12 * len(time)) / (len(step)) + 1
    """ Rayonnement global incident journalier """
    # Only Forcast possiblet
    codeRay = [176]
    RayFile = oFolder + "/176" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeRay, RayFile, typeData)
    server.retrieve(struct)
    Ray = utils.convertNETCDFtoDicArray(RayFile)
    Ray = utils.computeDailyMean(Ray, nbBandByDay, typeData)
    Ray = utils.convertWToMJ(Ray)
    """ downward surface solar radiation """
    # Only Forcast possiblet
    codeRay = [169]
    RayFileDownShort = oFolder + "/169" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeRay, RayFileDownShort,
                                      typeData)
    server.retrieve(struct)
    RayDownShort = utils.convertNETCDFtoDicArray(RayFileDownShort)
    RayDownShort = utils.computeDailyMean(RayDownShort, nbBandByDay, typeData)
    RayDownShort = utils.convertWToMJ(RayDownShort)
    """ downward surface thermal radiation """
    # Only Forcast possiblet
    codeRay = [175]
    RayFileDownLong = oFolder + "/175" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeRay, RayFileDownLong,
                                      typeData)
    server.retrieve(struct)
    RayDownLong = utils.convertNETCDFtoDicArray(RayFileDownLong)
    RayDownLong = utils.computeDailyMean(RayDownLong, nbBandByDay, typeData)
    RayDownLong = utils.convertWToMJ(RayDownLong)
    """ Evaporation """
    codeEvap = [182]
    EvapFile = oFolder + "/182" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'
    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codeEvap, EvapFile, typeData)
    server.retrieve(struct)
    Evap = utils.convertNETCDFtoDicArray(EvapFile)
    Evap = utils.computeDailyMean(Evap, nbBandByDay, typeData)
    #Evap = utils.convertMToMm(Evap)
    """ Precipitation """
    #NOT NEEDED FOR ETO BUT Exported

    utils.checkForTimeValidity(time)
    utils.checkForStepValidity(step, typeData)
    codePrecipitation = [228]
    precipitationFile = oFolder + "/228" + '_' + startDate.strftime(
        '%Y%m%d') + '_' + endDate.strftime('%Y%m%d') + '.nc'

    struct = utils.create_request_sfc(startDate, endDate, time, step, grid,
                                      extendArea, codePrecipitation,
                                      precipitationFile)
    server.retrieve(struct)
    precipitation = utils.convertNETCDFtoDicArray(precipitationFile)
    precipitation = utils.computeDailyAccumulation(precipitation, nbBandByDay,
                                                   typeData)
    """ Grid of latitude [0],longitude[1] in WGS84"""
    geoTransform = utils.getGeoTransform(RayFile)
    shape = Ray[0].shape
    latlon = utils.getCentroidLatFromArray(shape, geoTransform, grid)
    """ --------------------- Compute ET0---------------------- """

    ET0_0 = {}
    ET0_1 = {}
    ET0_2 = {}
    DoyList = []
    DateList = []

    for i in range(0, int(nbDays)):
        #jour Julien
        J = utils.doy(startDate, i)
        dateEnCours = startDate + timedelta(days=i)
        DateList.append(dateEnCours)
        DoyList.append(J)
        Hmax[i] = np.where(Hmax[i] > 100, 100, Hmax[i])

        # --- Constants ---#
        #Solar constant
        Gsc = 0.0820  # [MJ.m-2.min-1]
        #Albedo - grass reference crop
        a = 0.23
        #Ratio of molecular weight of water vapor/dry air
        epsilon = 0.622
        #Latente heat of vaporisation
        Lv = 2.45  # [MJ.kg-1]
        # Specific heat at constant pressure
        Cp = 1.013e-3
        # [MJ.kg-1.°C-1]
        # Stefan-Boltzmann constant [MJ.K-4.m-2.day-1]
        StefBoltz = 4.903e-9
        #FAO

        # --- Equations ---#
        # Psychometric constant [kPa.°C-1]
        cte_psy = (Cp * pressureMean[i]) / (epsilon * Lv
                                            )  # Equation 8 Chap 3 FAO
        #Mean sturation vapor presure [kPa]
        #es = (utils.esat(pressureMean[i],Tmax[i]) + utils.esat(pressureMean[i],Tmin[i]))/2;    #Equation 12 Chap 3
        es = (utils.eocalc(Tmax[i] - 273.16) +
              utils.eocalc(Tmin[i] - 273.16)) / 2  #Equation 12 Chap 3
        # Slope of saturation vapour pressure curve at air temperature [kPa.°C-1]
        delta = utils.delta_calc(Tmean[i])
        # Equation 13 Chap 3
        # Actual vapour pressure derived from relative humidity [kPa]
        #ea = (utils.esat(pressureMean[i]/100,Tmax[i]-273.16)*(Hmax[i]/100) + utils.esat(pressureMean[i]/100,Tmin[i]-273.16)*(Hmin[i]/100))/2;      # Equation 17 Chap 3
        ea = (utils.eocalc(Tmax[i] - 273.16) *
              (Hmax[i] / 100) + utils.eocalc(Tmin[i] - 273.16) *
              (Hmin[i] / 100)) / 2
        # Conversion of latitude from degrees to radians
        phi = (np.pi / 180) * latlon[1]
        # Relative distance Earth-Sun
        dr = 1 + 0.033 * math.cos(2 * math.pi * J / 365)
        # Equation 23 Chap 3
        # Solar declination
        d = 0.4093 * math.sin(2 * math.pi * J / 365 - 1.39)
        # Equation 24 Chap 3
        # sunset hour angle
        ws = np.arccos(-np.tan(phi) * math.tan(d))
        # Equation 25 Chap 3
        """Classical calculation FAO """

        # Extraterestrial radiation for daily periods
        Ra = (24. * 60 / np.pi) * Gsc * dr * (
            ws * np.sin(phi) * np.sin(d) + np.cos(phi) * np.cos(d) * np.sin(ws)
        )  # Equation 21 Chap 3
        # Clear sky solar radiation [MJ.m-2.day-1]
        Rso = (0.75 + 2e-5 * Geo[i]) * Ra
        # Equation 37 Chap 3
        # Net solar radiation [MJ.m-2.day-1]
        Rns = (1 - a) * RayDownShort[i]
        # Equation 38 Chap 3
        #
        f = (1.35 * (np.fmin(RayDownShort[i] / Rso, 1)) - 0.35)
        # Net longwave radiation [MJ.m-2.day-1]
        Rnl = StefBoltz * (
            (Tmax[i]**4 + Tmin[i]**4) / 2) * (0.34 - 0.14 * np.sqrt(ea)) * f
        # Equation 39 Chap 3
        # Net Radiation [MJ.m-2.day-1]
        Rn = Rns - Rnl
        # Equation 40 Chap 3
        G = 0
        # Equation 42 Chap 3
        ET0_0[i] = (0.408 * delta * (Rn - G) + cte_psy * (900 /
                                                          (Tmean[i] + 273)) *
                    (es - ea) * vent[i]) / (delta + cte_psy *
                                            (1 + 0.34 * vent[i]))
        # Equation 6 Chap 4
        """ Considering product 176 = RN these equations are not needed """
        Rn = Ray[i]
        # Soil heat flux at daily scale
        G = 0
        # Equation 42 Chap 3
        ET0_1[i] = (0.408 * delta * (Rn - G) + cte_psy * (900 /
                                                          (Tmean[i] + 273)) *
                    (es - ea) * vent[i]) / (delta + cte_psy *
                                            (1 + 0.34 * vent[i]))
        # Equation 6 Chap 4
        """ Considering product 176 Evaporation """
        ET0_2[i] = Evap[i]

    if typeOutput == 'RasterFile':
        #On ecrit le fichier ET0
        geoTransform = utils.getGeoTransform(RayFile)
        shape = Ray[0].shape
        utils.writeTiffFromDicoArray(ET0_0, oFolder + "/tmp.tif", shape,
                                     geoTransform)
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/ET0.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/ET0.tif")

        #On écrit le fichier Precipitation
        geoTransform = utils.getGeoTransform(precipitationFile)
        shape = precipitation[0].shape
        utils.writeTiffFromDicoArray(precipitation, oFolder + "/tmp.tif",
                                     shape, geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/precipitationAcc.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/precipitationAcc.tif")

    else:
        #On ecrit le fichier au format Txt
        proj = utils.getProj(pathToShapefile)
        utils.WriteTxtFileForEachPixel(oFolder, ET0_0, ET0_1, ET0_2, DateList,
                                       DoyList, Ray, RayDownShort, RayDownLong,
                                       Tmean, Tmax, Tmin, Hmean, Hmax, Hmin,
                                       vent, precipitation, pressureMean, Geo,
                                       latlon, proj)
        utils.WritePointList(oFolder, latlon, proj)
    """ ------------------------------------------- """
    if (temporaryFile):
        #On ecrit le fichier latlon
        geoTransform = utils.getGeoTransform(GeoFile)
        shape = Geo[0].shape
        utils.writeTiffFromDicoArray(Geo, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/altitude.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/altitude.tif")

        #On ecrit le fichier latlon
        geoTransform = utils.getGeoTransform(RayFile)
        shape = Ray[0].shape
        utils.writeTiffFromDicoArray(latlon, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/latLon.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/latLon.tif")

        #On ecrit le fichier vent --> a enlever
        geoTransform = utils.getGeoTransform(ventFile[-1])
        shape = vent[0].shape
        utils.writeTiffFromDicoArray(vent, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif", oFolder + "/ventMean.tif",
                               shape, pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/ventMean.tif")

        #On ecrit le fichier Rhmin
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = pressureMean[0].shape
        utils.writeTiffFromDicoArray(pressureMean, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/pressureMean.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/pressureMean.tif")

        #On ecrit le fichier Rhmax
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = Hmax[0].shape
        utils.writeTiffFromDicoArray(Hmax, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/humidityMax.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/humidityMax.tif")

        #On ecrit le fichier Rhmin
        geoTransform = utils.getGeoTransform(pressureFile)
        shape = Hmin[0].shape
        utils.writeTiffFromDicoArray(Hmin, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/humidityMin.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif", oFolder + "/humidityMin.tif")

        #On ecrit le fichier Tmax
        geoTransform = utils.getGeoTransform(T2mFile)
        shape = Tmax[0].shape
        utils.writeTiffFromDicoArray(Tmax, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/TemperatureMax.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/TemperatureMax.tif")

        #On ecrit le fichier Tmin
        geoTransform = utils.getGeoTransform(T2mFile)
        shape = Tmin[0].shape
        utils.writeTiffFromDicoArray(Tmin, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/TemperatureMin.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/TemperatureMin.tif")

        #On ecrit le fichier Rayonnement
        geoTransform = utils.getGeoTransform(RayFile)
        shape = Ray[0].shape
        utils.writeTiffFromDicoArray(Ray, oFolder + "/tmp.tif", shape,
                                     geoTransform)

        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder + "/tmp.tif",
                               oFolder + "/RayonnementMean.tif", shape,
                               pathToShapefile)
            os.remove(oFolder + "/tmp.tif")
        else:
            utils.moveFile(oFolder + "/tmp.tif",
                           oFolder + "/RayonnementMean.tif")

    #on supprime les fichier intermédiare !
    os.remove(pressureFile)
    os.remove(T2mFile)
    os.remove(DewPFile)
    os.remove(RayFile)
    os.remove(GeoFile)
    for i in ventFile:
        os.remove(i)
    os.remove(precipitationFile)
    os.remove(EvapFile)
    os.remove(RayFileDownLong)
    os.remove(RayFileDownShort)
def main(argv):

    try:
        opts,argv = getopt.getopt(argv,":h:i:e:s:E:o:g:P:t:f:r:",['help','[outFile]','code','[shapeFile]','start','end','[tr]'])
    except getopt.GetoptError:
        print 'error in parameter for evapo_GFS. type evapo_GFS.py -help for more detail on use '
        sys.exit(2)
    
    for opt, arg in opts:
        if opt == '-h':
            print 'evapo_GFS.py  '
            print '    [mandatory] : '
            print '        --init <dateStart YYYY-MM-DD>'
            print '        --end <dateEnd YY-MM-DD>'
            print '        --shapefile <shapefile> OU -Extend < xmin,ymax,xmax,ymin>'
            print '    [optional] :'
            print '        --typeData  < analyse , forecast, cycleforecast> (default forecast)'
            print '        --grid <EraInterim Time> (default 0.75)'
            print '        --outfile <outfolder> (default /home/user/eraInterim)'
            print '        --proxy <True/False> (default False)'
            print '        --temporaryFile <True/False> (default False)'
            print '        --result < TxtFile / RasterFile> default RasterFile'
            print ''
            sys.exit() 
        elif opt in ('-o','--outFolder'):
            oFolder = arg
        elif opt in ('-i','--start'):
            startDate = arg
        elif opt in ('-e','--end'):
            endDate = arg
        elif opt in ('-s','--shapefile'):
            pathToShapefile = arg
        elif opt in ('-E','--tr'):
            extend = arg.split(',')
        elif opt in ('-g','--grid'):
            grid = arg
        elif opt in ('-P','--proxy'):
            proxy = arg
        elif opt in ('-t','--typeData'):
            typeData = arg
        elif opt in ('-f','--temporaryFile'):
            temporaryFile = arg
        elif opt in ('-r','--result'):
            typeOutput = arg
    
    if len(sys.argv) < 7:
        print 'evapo_GFS.py'
        print '    -i <dateStart YYYY-MM-DD> '
        print '    -e <dateEnd YY-MM-DD>'
        print '    -s <shapefile> '
        print '  or'
        print '    -E < xmin,ymax,xmax,ymin>]'
        print ''
        print '    [-t < analyse , forecast,cycleforecast> (default analyse)]'
        print '    [-g <size of grid in 0.25/0.5/1/2.5> (default 0.25)]'
        print '    [-o <outfolder> (default /home/user/eraInterim)]'
        print '    [-P <proxy : True/False> (default False)]'
        print '    [-f <temporaryFile : True/False> (default False)]'
        print '    [-r <resultOutput : TxtFile/RasterFile> (default RasterFile)]'
        print ''
        sys.exit(2)
        
    try:
        oFolder
    except NameError:
        oFolder = os.path.expanduser('~')
        oFolder = oFolder + '/GFS'
        print "output folder not precised : downloaded GFS images on "+oFolder
    
    # verification du folder/or creation if not exists
    utils.checkForFolder(oFolder) 
        
    try:
        startDate
    except NameError:
        exit ('init Date not precised')
    # verification si sartDate est une date
    startDate=utils.checkForDate(startDate) 
    
    try:
        endDate
    except NameError:
        exit ('end Date not specified')
    # verification si sartDate est une date
    endDate=utils.checkForDate(endDate) 
    
    if (startDate>endDate):
        exit('startDate could not be greater than endDate')

    try:
        pathToShapefile
    except NameError:
        try:
            extend
        except NameError:
            exit ('no Area of interest have been specified. please use -shp or -tr to declare it')
    
    if 'pathToShapefile' in locals():
        extendArea=utils.convertShpToExtend(pathToShapefile)
    else:
        extendArea=extend

    extendArea=utils.checkForExtendValidity(extendArea)
      
    try:
        typeData
    except NameError:
        typeData='analyse'

    try:
        grid
    except NameError:
        grid='0.25'
    grid=utils.checkForGridValidity(grid)
            
    try:
        proxy
    except NameError:
        proxy=False

    #Proxy parameteres needed
    if(proxy):
        login = raw_input('login proxy : ')
        pwd = raw_input('password proxy :  : ')
        site = raw_input('site (surf.cnes.fr) : ')
        os.environ["http_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
        os.environ["https_proxy"] = "http://%s:%s@%s:8050"%(login,pwd,site)
    
    try:
        temporaryFile
    except NameError:
        temporaryFile=False
    
    try:
        typeOutput
    except NameError:
        typeOutput='RasterFile'
    """----------------------------------------"""
        
    delta = endDate - startDate
    nbDays = delta.days + float(delta.seconds) / 86400 + 1

    #--------------------------On charge les rasters
    step = [0,6,12,18]
    levelList = ["surface"]
    nbBandByDay=len(step)
    
    """ altitude de la grille GFS """
    print('downloading altitude grid')
    # Only Forecast possible
    codeGeopot= ['HGT']
    struct=utils.create_request_gfs(startDate, endDate, step, levelList, grid, extendArea, codeGeopot, typeData)        
    listeGeoFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                GeoFile = oFolder+'/'+",".join(codeGeopot)+'_'+i.rsplit('.',1)[1]+'.grb'
                listeGeoFile.append(GeoFile)
                result=utils.GFSDownload(i,GeoFile)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            Geo = utils.convertGribToDicoArray(listeGeoFile, codeGeopot, levelList, step, grid, startDate, endDate)
            Geo = utils.convertGeoToAlt(Geo)
            #un peu inutile car ne change pas ... mais bon! 
            Geo = utils.computeDailyMax(Geo, nbBandByDay, typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    """ Vitesse du vent """
    print('downloading wind velocity')
    codeVent= ['UGRD','VGRD']
    levelListVent = ["80_m_above_ground"]
    vent={}
    ventFile=[]
    for v in codeVent:
        struct=utils.create_request_gfs(startDate, endDate, step, levelListVent, grid, extendArea, codeVent, typeData)        
        listeFile=[]
        if len(struct[0])==0:
            exit("No data founded")
        else:
            for i in struct[0]:
                try :
                    tmpVent = oFolder+'/'+v+'_'+i.rsplit('.',1)[1]+'.grb'
                    ventFile.append(tmpVent)
                    result=utils.GFSDownload(i,tmpVent)
                except:
                    print("---")
                    exit('Error in GFS server')
            
            if result:
                vent[v] = utils.convertGribToDicoArray(ventFile, codeVent, levelList, step, grid, startDate, endDate)
            else:
                exit("PARAM needed is not compatible with level selected")
    
    vent = utils.fusVentFromDict(vent,nbBandByDay)
    vent=utils.computeDailyMean(vent,nbBandByDay,typeData)
    exit
    """ Humidité relative """
    print('downloading relative humidity')
    codePressure= ['PRES']
    struct=utils.create_request_gfs(startDate, endDate, step, levelList, grid, extendArea, codePressure, typeData)        
    listePressureFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                pressureFile = oFolder+'/'+",".join(codePressure)+'_'+i.rsplit('.',1)[1]+'.grb'
                listePressureFile.append(pressureFile)
                result=utils.GFSDownload(i,pressureFile)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            pressure = utils.convertGribToDicoArray(listePressureFile, codePressure, levelList, step, grid, startDate, endDate)
            pressureMean = utils.convertPaToKgPa(pressure)
            pressure = utils.convertToHectoPascal(pressure)
            pressureMean = utils.computeDailyMean(pressureMean,nbBandByDay,typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    """ Temperature 2m """
    print('downloading Temperature')
    levelListTmp = ["2_m_above_ground"]
    codeT2m= ['TMP']
    struct=utils.create_request_gfs(startDate, endDate, step, levelListTmp, grid, extendArea, codeT2m, typeData)        
    listeTmpFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                T2mFile = oFolder+'/'+",".join(codeT2m)+'_'+i.rsplit('.',1)[1]+'.grb'
                listeTmpFile.append(T2mFile)
                result=utils.GFSDownload(i,T2mFile)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            T2m = utils.convertGribToDicoArray(listeTmpFile, codeT2m, levelList, step, grid, startDate, endDate)
            Tmean = utils.computeDailyMean(T2m, nbBandByDay, typeData)
            Tmax = utils.computeDailyMax(T2m,nbBandByDay)
            Tmin = utils.computeDailyMin(T2m,nbBandByDay)
            T2m = utils.convertKToD(T2m)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    """ DewPoint """
    print('downloading DewPoint')
    levelListdew = ["2_m_above_ground"]
    codeDewP= ['DPT']
    struct=utils.create_request_gfs(startDate, endDate, step, levelListdew, grid, extendArea, codeDewP, typeData)        
    listeDewFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                DewPFile = oFolder+'/'+",".join(codeDewP)+'_'+i.rsplit('.',1)[1]+'.grb'
                listeDewFile.append(DewPFile)
                result=utils.GFSDownload(i,DewPFile)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            DewP = utils.convertGribToDicoArray(listeDewFile, codeDewP, levelList, step, grid, startDate, endDate)
            DewP = utils.convertKToD(DewP)
        else:
            exit("PARAM needed is not compatible with level selected")

    
    humidity = utils.ComputeHumidityFromPT(pressure,T2m,DewP)
    #humidity = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    Hmax = utils.computeDailyMax(humidity,nbBandByDay)
    Hmin = utils.computeDailyMin(humidity,nbBandByDay)
    Hmean = utils.computeDailyMean(humidity,nbBandByDay,typeData)
    

    """ Rayonnement global incident journalier """
    print('downloading downscale shortwave radiation')
    # Only Forecast possible
    levelListRay = ["surface"]
    codeRay= ["DSWRF"]
    typeData="cycleforecast"
    struct=utils.create_request_gfs(startDate, endDate, step, levelListRay, grid, extendArea, codeRay, typeData)        
    listeRayFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                RayFileDownShort = oFolder+'/'+",".join(codeRay)+'_'+i.rsplit('.',1)[1]+'.grb'
                listeRayFile.append(RayFileDownShort)
                result=utils.GFSDownload(i,RayFileDownShort)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            RayDownShort = utils.convertGribToDicoArray(listeRayFile, codeRay, levelList, step, grid, startDate, endDate)
            RayDownShort = utils.computeDailyMean(RayDownShort,nbBandByDay,typeData)
            RayDownShort = utils.convertWToMJ(RayDownShort)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    """ Precipitation """
    #NOT NEEDED FOR ETO BUT Exported
    print('downloading Precipitation')
    codePrecipitation= ["APCP"]
    struct=utils.create_request_gfs(startDate, endDate, step, levelList, grid, extendArea, codePrecipitation, typeData)        
    listePrecFile=[]
    if len(struct[0])==0:
        exit("No data founded")
    else:
        for i in struct[0]:
            try :
                precipitationFile = oFolder+'/'+",".join(codePrecipitation)+'_'+i.rsplit('.',1)[1]+'.grb'
                listePrecFile.append(precipitationFile)
                result=utils.GFSDownload(i,precipitationFile)
            except:
                print("---")
                exit('Error in GFS server')
        
        if result:
            precipitation = utils.convertGribToDicoArray(listePrecFile, codePrecipitation, levelList, step, grid, startDate, endDate)
            precipitation=utils.computeDailyAccumulation(precipitation,nbBandByDay,typeData)
        else:
            exit("PARAM needed is not compatible with level selected")
    
    """ Grid of latitude [0],longitude[1] in WGS84"""
    geoTransform=utils.getGeoTransform(RayFileDownShort)
    shape=RayDownShort[0].shape
    latlon = utils.getCentroidLatFromArray(shape,geoTransform,grid)

    """ --------------------- Compute ET0---------------------- """
    
    ET0_0={}
    DoyList=[]
    DateList=[]
    
    for i in range(0,int(nbDays)):
        #jour Julien
        J = utils.doy(startDate,i)
        dateEnCours=startDate+ timedelta(days=i)
        DateList.append(dateEnCours)
        DoyList.append(J)
        Hmax[i] = np.where(Hmax[i]>100,100,Hmax[i])
        
        # --- Constants ---# 
        #Solar constant
        Gsc = 0.0820 # [MJ.m-2.min-1]
        #Albedo - grass reference crop
        a = 0.23
        #Ratio of molecular weight of water vapor/dry air
        epsilon=0.622 
        #Latente heat of vaporisation
        Lv=2.45 # [MJ.kg-1]
        # Specific heat at constant pressure
        Cp= 1.013e-3; # [MJ.kg-1.°C-1]
        # Stefan-Boltzmann constant [MJ.K-4.m-2.day-1]
        StefBoltz=4.903e-9; #FAO
        
        # --- Equations ---# 
        # Psychometric constant [kPa.°C-1]
        cte_psy = (Cp*pressureMean[i])/(epsilon*Lv) # Equation 8 Chap 3 FAO
        #Mean sturation vapor presure [kPa]
        #es = (utils.esat(pressureMean[i],Tmax[i]) + utils.esat(pressureMean[i],Tmin[i]))/2;    #Equation 12 Chap 3
        es = (utils.eocalc(Tmax[i]-273.16)+utils.eocalc(Tmin[i]-273.16))/2    #Equation 12 Chap 3
        # Slope of saturation vapour pressure curve at air temperature [kPa.°C-1]
        delta = utils.delta_calc(Tmean[i]);                    # Equation 13 Chap 3
        # Actual vapour pressure derived from relative humidity [kPa]
        #ea = (utils.esat(pressureMean[i]/100,Tmax[i]-273.16)*(Hmax[i]/100) + utils.esat(pressureMean[i]/100,Tmin[i]-273.16)*(Hmin[i]/100))/2;      # Equation 17 Chap 3
        ea = (utils.eocalc(Tmax[i]-273.16)*(Hmax[i]/100)+utils.eocalc(Tmin[i]-273.16)*(Hmin[i]/100))/2
        # Conversion of latitude from degrees to radians
        phi = (np.pi/180)* latlon[1];     
        # Relative distance Earth-Sun
        dr = 1 + 0.033*math.cos(2*math.pi*J/365);         # Equation 23 Chap 3
        # Solar declination
        d = 0.4093*math.sin(2*math.pi*J/365 - 1.39);      # Equation 24 Chap 3
        # sunset hour angle
        ws = np.arccos(-np.tan(phi)*math.tan(d));                # Equation 25 Chap 3
        
        """Classical calculation FAO """
        
        # Extraterestrial radiation for daily periods
        Ra = (24.*60/np.pi)*Gsc*dr*(ws*np.sin(phi)*np.sin(d) + np.cos(phi)*np.cos(d)*np.sin(ws))    # Equation 21 Chap 3
        # Clear sky solar radiation [MJ.m-2.day-1]
        Rso = (0.75 + 2e-5*Geo[i])*Ra;                 # Equation 37 Chap 3
        # Net solar radiation [MJ.m-2.day-1]
        Rns = (1 - a)*RayDownShort[i];                          # Equation 38 Chap 3
        #
        f=(1.35*(np.fmin(RayDownShort[i]/Rso,1)) - 0.35);
        # Net longwave radiation [MJ.m-2.day-1]
        Rnl = StefBoltz*((Tmax[i]**4 + Tmin[i]**4)/2)*(0.34 - 0.14*np.sqrt(ea))*f; # Equation 39 Chap 3
        # Net Radiation [MJ.m-2.day-1]
        Rn =Rns - Rnl;                              # Equation 40 Chap 3
        G = 0;                                      # Equation 42 Chap 3
        ET0_0[i] = ( 0.408*delta*( Rn-G )+ cte_psy*( 900/(Tmean[i] + 273) )*(es - ea)*vent[i] )/( delta + cte_psy*(1 + 0.34*vent[i]) );  # Equation 6 Chap 4
    
    if typeOutput=='RasterFile':
        #On ecrit le fichier ET0 
        geoTransform=utils.getGeoTransform(RayFileDownShort)
        shape=RayDownShort[0].shape
        utils.writeTiffFromDicoArray(ET0_0,oFolder+"/tmp.tif",shape,geoTransform)
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/ET0.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/ET0.tif")

        #On écrit le fichier Precipitation
        geoTransform=utils.getGeoTransform(precipitationFile)
        shape=precipitation[0].shape
        utils.writeTiffFromDicoArray(precipitation,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/precipitationAcc.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/precipitationAcc.tif")
        
    else:
        #On ecrit le fichier au format Txt
        proj=utils.getProj(pathToShapefile)
        utils.WriteTxtFileForEachPixel(oFolder,ET0_0,DateList,DoyList,RayDownShort,Tmean,Tmax,Tmin,Hmean,Hmax,Hmin,vent,precipitation,pressureMean,Geo,latlon,proj)
        utils.WritePointList(oFolder,latlon,proj)
    
    """ ------------------------------------------- """
    if(temporaryFile):
        #On ecrit le fichier latlon 
        geoTransform=utils.getGeoTransform(GeoFile)
        shape=Geo[0].shape
        utils.writeTiffFromDicoArray(Geo,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/altitude.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/altitude.tif")
    
        #On ecrit le fichier latlon 
        geoTransform=utils.getGeoTransform(RayFileDownShort)
        shape=RayDownShort[0].shape
        utils.writeTiffFromDicoArray(latlon,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/latLon.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/latLon.tif")
        
        
        #On ecrit le fichier vent --> a enlever
        geoTransform=utils.getGeoTransform(ventFile[-1])
        shape=vent[0].shape
        utils.writeTiffFromDicoArray(vent,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/ventMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/ventMean.tif")
        
        #On ecrit le fichier Rhmin
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=pressureMean[0].shape
        utils.writeTiffFromDicoArray(pressureMean,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/pressureMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/pressureMean.tif")
        
        #On ecrit le fichier Rhmax
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=Hmax[0].shape
        utils.writeTiffFromDicoArray(Hmax,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/humidityMax.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/humidityMax.tif")
        
        #On ecrit le fichier Rhmin
        geoTransform=utils.getGeoTransform(pressureFile)
        shape=Hmin[0].shape
        utils.writeTiffFromDicoArray(Hmin,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/humidityMin.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/humidityMin.tif")
    
        #On ecrit le fichier Tmax
        geoTransform=utils.getGeoTransform(T2mFile)
        shape=Tmax[0].shape
        utils.writeTiffFromDicoArray(Tmax,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/TemperatureMax.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/TemperatureMax.tif")
    
        #On ecrit le fichier Tmin
        geoTransform=utils.getGeoTransform(T2mFile)
        shape=Tmin[0].shape
        utils.writeTiffFromDicoArray(Tmin,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/TemperatureMin.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/TemperatureMin.tif")
        
        #On ecrit le fichier Rayonnement
        geoTransform=utils.getGeoTransform(RayFileDownShort)
        shape=RayDownShort[0].shape
        utils.writeTiffFromDicoArray(RayDownShort,oFolder+"/tmp.tif",shape,geoTransform)
    
        if 'pathToShapefile' in locals():
            utils.reprojRaster(oFolder+"/tmp.tif", oFolder+"/RayonnementMean.tif",shape, pathToShapefile)
            os.remove(oFolder+"/tmp.tif")
        else : 
            utils.moveFile(oFolder+"/tmp.tif", oFolder+"/RayonnementMean.tif")
    
    #on supprime les fichier intermédiare !
    for i in listePressureFile:
        os.remove(i)
    for i in listeTmpFile:
        os.remove(i)
    for i in listeDewFile:
        os.remove(i)
    for i in listeRayFile:
        os.remove(i)
    for i in listeGeoFile:
        os.remove(i)
    for i in ventFile:
        os.remove(i)
    for i in listePrecFile:
        os.remove(i)