# -*- coding: utf-8 -*- """ Description : ------------- Selection of files .hdf Usage : ------- Two options for the command line. Read the end of the program ( def init_prog_options( parser ) ) or the README Example : --------- Prerequisites : --------------- python >= 2.5 ; not tested but probably all versions Author : -------- CGTD-ICARE/UDEV Nicolas THOMAS ( nicolas.thomas-at-icare.univ-lille1.fr ) License : --------- This file must be used under the terms of the CeCILL. This source file is licensed as described in the file COPYING, which you should have received as part of this distribution. The terms are also available at http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt History : --------- v0.0.0 : 2010/01/25 - creation """ import os import glob import sys import os.path import re from optparse import OptionParser from calendar import * from datetime import datetime,timedelta from time import time, strptime, strftime, gmtime from numpy import * ##### APPLICATION ##### #__VERSION__="0.0.0" ##### APP RETURN CODES ##### EXIT_FAILURE=1 EXIT_SUCCESS=0 DEFAULT_OUTPUT_DIR =os.getcwd() products_parasol=['ATM_WV_D_','ATM_TU_D_','ATM_TAU_D_','ATM_TAUCSM_D_','ATM_TAUCNM_D_','ATM_TAUAFM_D_','ATM_TAUA_D_','ATM_SDWV_D_','ATM_REFFM_D_','ATM_REF_D_','ATM_QCC_D_','ATM_QAVIS_D_','ATM_PRAY_D_','ATM_POXY_D_','ATM_PHASE_D_','ATM_IQAI_D_','ATM_CC_D_','ATM_AVIS_D_','ATM_ASW_D_','ATM_ANGFM_D_','ATM_ANG_D_'] id_variable={} for name in iter(products_parasol) : id_variable[name]=('PARASOL','PR_ATM') products_seviri=['Angstrom_Exponent','Aerosol_Optical_Depth'] for name in iter(products_seviri) : id_variable[name]=('SEVIRI','SEVIRI_AER_OC') class Production : def __init__(self,variable,date_start,date_end,compress,subsetting,valmax,valmin,verbose,stats,command_line,time,output_dir,fixed_value,output_id,step): """ Constructor """ valid_variable=id_variable.keys() if variable not in valid_variable : raise ValueError ( "Unsupported Variable %s Must be in [%s]"%( variable, valid_variable ) ) self.variable=variable self.sensor=id_variable[self.variable][0] self.prod_id=id_variable[self.variable][1] self.date_start=date_start self.date_end=date_end self.fixed_value=fixed_value self.subsetting=subsetting self.time=time self.compress=compress self.valmax=valmax self.valmin=valmin self.verbose=verbose self.stats=stats self.command_line=command_line self.output_dir=output_dir self.output_id=output_id self.step=step if self.verbose is True : print "sensor :",self.sensor print "prod_id : ",self.prod_id print "variable :",self.variable print "date_start : ",self.date_start print "date_end : ",self.date_end print "fixed_value : ",self.fixed_value print "subsetting : ",self.subsetting print "time : ",self.time print "compress : ",self.compress print "valmax : ",self.valmax print "valmin : ",self.valmin print "verbose : ",self.verbose print "stats : ",self.stats print "command_line : ",self.command_line print "output_directory :",self.output_dir print "output_id :",self.output_id print "step :",self.step cmd_common="python get_file.py " cmd_common+='-r '+str(self.sensor)+' ' if self.sensor=='SEVIRI' : cmd_common+=self.prod_id+' ' cmd_common+=self.variable+' ' cmd_common+=self.date_start+' ' if self.date_end is not None : cmd_common+=self.date_end+' ' if self.sensor=='SEVIRI' and self.time is not None: cmd_common+='-t '+str(self.time[0])+' '+str(self.time[1])+' ' if self.sensor=='SEVIRI' and self.fixed_value is not None: cmd_common+='-l '+str(self.fixed_value)+' ' if self.sensor=='PARASOL' and self.valmax is not None: cmd_common+='-m '+str(self.valmax)+' ' if self.sensor=='PARASOL' and self.valmin is not None: cmd_common+='-n '+str(self.valmin)+' ' if self.subsetting is not None : cmd_common+='-s '+str(self.subsetting[0])+' '+str(self.subsetting[1])+' '+str(self.subsetting[2])+' '+str(self.subsetting[3])+' ' if self.compress is not True: cmd_common+=' -g ' if self.verbose is True : cmd_common+=' -e ' if self.stats is True : cmd_common+=' -k ' if self.command_line is True : cmd_common+=' -y ' if self.output_id is not None : cmd_common+='-f '+str(self.output_id)+' ' if self.step is not None : cmd_common+='-p '+str(self.step)+' ' if self.verbose is True : print "cmd :",cmd_common os.system(cmd_common) def init_prog_options( parser ): parser.usage ="Average_tools.py [options]" parser.usage+="\n\n Example : \n" parser.usage+= "python Average_tools.py ATM_POXY_D_ 2009-01-10 2009-02-17 \n" parser.usage+= "python Average_tools.py Aerosol_Optical_Depth 2009-07-05\n" parser.usage+="\n\nArgs :" parser.usage+="\n : ID of the Variable to process. One of " + str ( id_variable.keys() ) + " by product\n" parser.usage+="\n and : timestamp(s) of the input files\n" parser.usage+="\tCan have different formats by sensor:\n" parser.usage+="\t To PARASOL :\n" parser.usage+="\n First option :\n " parser.usage+="\t\t- YYYY-MM-DD and YYYY-MM-DD : will produce the browses during a given period at daily (Ex : 2007-09-10 2007-09-15)\n" parser.usage+="\t\t- YYYY-MM and YYYY-MM : will produce the browses for a period up to the monthly or seasonal scale (Ex :2007-09 2007-12)\n" parser.usage+="\t\t- YYYY and YYYY : will produce the browses on an annual basis (Ex : 2006 2008)\n" parser.usage+="\n Second option : " parser.usage+="\n : timestamp(s) of the input PARASOL files\n" parser.usage+="\tCan have different formats :\n" parser.usage+="\t\t- YYYY-MM : will produce the browses for the month specified (Ex: 2008-05)\n" parser.usage+="\t\t- YYYY : will produce the browses for the year specified (Ex : 2008)\n" parser.usage+="\n" parser.usage+="\t To SEVIRI :\n" parser.usage+="\n First option :\n " parser.usage+="\t\t- YYYY-MM-DDTHHMM and YYYY-MM-DDTHHMM : will produce the browses during a given period at daily between hours (Ex : 2007-09-10T0800 2007-09-15T1310)\n" parser.usage+="\t\t- YYYY-MM-DD and YYYY-MM-DD : will produce the browses during a given period at daily (Ex : 2007-09-10 2007-09-15)\n" parser.usage+="\t\t- YYYY-MM and YYYY-MM : will produce the browses for a period up to the monthly scale or seasonal scale (Ex :2007-09 2007-12)\n" parser.usage+="\t\t- YYYY and YYYY : will produce the browses on an annual basis (Ex : 2006 2008)\n" parser.usage+="\n Second option : " parser.usage+="\n : Variable Product SEVIRI, choose between Aerosol_Optical_Depth and Angstrom_Exponent\n" parser.usage+="\n : timestamp(s) of the input SEVIRI files\n" parser.usage+="\tCan have different formats :\n" parser.usage+="\t\t- YYYY-MM-DD : will produce the browses for the day specified (Ex: 2008-05-12)\n" parser.usage+="\t\t- YYYY-MM : will produce the browses for the month specified (Ex: 2008-05)\n" parser.usage+="\t\t- YYYY : will produce the browses for the year specified (Ex : 2008)\n" parser.usage+="\n" parser.usage+="\t\tIt is possible to choose a period of the day as the following example 2009-09-01 2009-09-10 -t 1000 1200\n" parser.usage+="\t\tFor each day, between 10h00 and 12h00 only files are read to calculate the statistics\n" parser.usage+="\n\nOptions :" parser.usage+="\n Subsetting's option : " parser.usage+="\n <-s xmin xmax ymin ymax> to select an interest area." parser.usage+="\n" parser.usage+="\n Maximum value's option :" parser.usage+="\n <-m 250> to choose the maximum value for the average image (not for SEVIRI)" parser.usage+="\n" parser.usage+="\n Minimum value's option :" parser.usage+="\n <-n 50> to choose the minimum value for the average image (not for SEVIRI)" parser.usage+="\n" parser.usage+="\n Fixed_Value :" parser.usage+="\n <-l name of fixed value> shows the fixed values and speeds up the computation time, example: space and land values for SEVIRI ex : -l LAND_VAL,SPACE_VAL. It's different of fill value" parser.usage+="\n" parser.usage+="\n Hdf file compression :" parser.usage+="\n <-g> Use this option to disable compression" parser.usage+="\n Print program's informations :" parser.usage+="\n <-e> Use this option to print program's informations" parser.usage+="\n Additional statistics (standard deviation, min max per pixel:" parser.usage+="\n <-k> Use this option to obtain additional statistics" parser.usage+="\n Provides the statistical results to png file :" parser.usage+="\n <-y> Use this option to execute os.system() and obtain the results to png file" parser.add_option("-f", "--output_id",type="string",dest="output_id",default= None,help="give a prefix of output file for HDF, PCf or PNG format") parser.add_option("-o","--output_directory",action="store", default=DEFAULT_OUTPUT_DIR,type="string",dest="output_dir",help="get_file output directory (default:"+DEFAULT_OUTPUT_DIR+")") parser.add_option("-s", "--subsetting",type="int",nargs=4,dest="subsetting",default=None,help="This option requires 4 arguments, xmin xmax ymin ymax") parser.add_option("-t","--time",action="store", default=None,type="string",nargs=2,dest="time",help="period chosen for reading data,if it's None all day is read") parser.add_option("-m","--valmax",type="string",dest="valmax",default = None) parser.add_option("-n","--valmin",type="string",dest="valmin",default = None) parser.add_option("-g","--compress",dest="compress",action='store_false',default = True,help="hdf file compression") parser.add_option("-e","--verbose",dest="verbose",action='store_true',default = False,help="print program's informations") parser.add_option("-k","--stats",dest="stats",action='store_true',default = False,help="Extra statistics calculated : std, min max per pixel") parser.add_option("-y","--command_line",dest="command_line",action='store_true',default = False,help="Execute os.system()") parser.add_option("-l","--fixed_value",dest="fixed_value",type="string",default = None,help="name of fill value, it is possible that the file has multiple values like SEVIRI (LAND_VAL,SPACE_VAL) ") parser.add_option("-p", "--step",type="int",nargs=1,dest="step",default=None,help="allows to choose the step of list of files") if len ( sys.argv ) < 2 : parser.print_help ( ) raise ValueError ( "\nMissing command-line options" ) if len ( sys.argv ) > 27 : parser.print_help ( ) raise ValueError ( "\n Too many arguments in command-line" ) # #____________________________________________________ # def main(): start_time=time() print "\n --------------- PROGRAMME Average_tool.py Creation Command_line ------------------\n" # parse the command line arguments parser = OptionParser() #parser = OptionParser() init_prog_options ( parser ) ( options, args ) = parser.parse_args ( ) if len(args)==2: prog=Production(args[0],args[1],date_end=None,time=options.time,output_dir=options.output_dir,subsetting=options.subsetting,compress=options.compress,verbose=options.verbose,stats=options.stats,command_line=options.command_line,valmax=options.valmax,valmin=options.valmin,fixed_value=options.fixed_value,output_id=options.output_id,step=options.step) else : prog=Production(args[0],args[1],args[2],time=options.time,output_dir=options.output_dir,subsetting=options.subsetting,compress=options.compress,verbose=options.verbose,stats=options.stats,command_line=options.command_line,valmax=options.valmax,valmin=options.valmin,fixed_value=options.fixed_value,output_id=options.output_id,step=options.step) #all works good print " ---------- end of PROGRAMME with status "+str(EXIT_SUCCESS)+" (elapsed time : "+strftime("%H:%M:%S",gmtime(time()-start_time))+") -------------" sys.exit(EXIT_SUCCESS) if __name__ == "__main__": main()