def execute(self): from os import mkdir, path, listdir from flyingpigeon import segetalflora as sf init_process_logger('log.txt') self.output_log.setValue('log.txt') logging.debug('starting segetalflora process execution') self.status.set('starting calcualtion segetalflora', 5) ############################ # read argments to variables ############################ try: resource = self.getInputValues(identifier='resource') climate_type = self.climate_type.getValue() culture_type = self.culture_type.getValue() logging.info('urls for %s ncs found' % (len(resource))) logging.info('culture type: %s ' % (culture_type)) except Exception as e: logger.debug('failed to read in the arguments: %s ' % e) try: if type(climate_type) != list: climate_type = list([climate_type]) if type(culture_type) != list: culture_type = list([culture_type]) logger.info('arguments are lists') except Exception as e: logger.debug('failed to transform arguments to lists: %s ' % e) ############################# # get yearly mean temperature ############################# nc_tasmean = sf.get_yrmean(resource) ####################################### # main call for segetalflora processing ####################################### nc_sf = sf.get_segetalflora(resource=nc_tasmean, culture_type=culture_type, climate_type=climate_type) #################### # tar file archiving #################### try: from flyingpigeon.utils import archive self.status.set('files to tar archives', 99) tar_sf = archive(nc_sf, format='tar', dir_output='.', mode='w') tar_tasmean = archive(nc_tasmean, format='tar', dir_output='.', mode='w') logging.info('Archives prepared') except Exception as e: logger.debug('failed to archive files %s' % e) # === set output parameter self.out_segetalflora.setValue(tar_sf) self.out_tasmean.setValue(tar_tasmean) self.status.set("processing done", 100)
def execute(self): from os import mkdir, path, listdir # import tarfile # from tempfile import mkstemp #, mkdtemp # from datetime import datetime from flyingpigeon import segetalflora as sf logging.debug('starting segetalflora process execution') self.status.set('starting calcualtion segetalflora', 5) ############################ # read argments to variables ############################ try: resource = self.getInputValues(identifier='resource') climate_type = self.climate_type.getValue() culture_type = self.culture_type.getValue() logging.info('urls for %s ncs found' % (len(resource))) logging.info('culture type: %s ' % (culture_type)) except Exception as e: logger.debug('failed to read in the arguments: %s ' % e) try: if type(climate_type) != list: climate_type = list([climate_type]) if type(culture_type) != list: culture_type = list([culture_type]) logger.info('arguments are lists') except Exception as e: logger.debug('failed to transform arguments to lists: %s ' % e) ############################# # get yearly mean temperature ############################# nc_tasmean = sf.get_yrmean(resource) ####################################### # main call for segetalflora processing ####################################### nc_sf = sf.get_segetalflora(resource=nc_tasmean, culture_type=culture_type, climate_type=climate_type) #stepps = len(culture_type) * len(climate_type) #for a, cult in enumerate(culture_type): #for b, clim in enumerate(climate_type): #start = (a + 1) * (b + 1) #per = (start / stepps) * 95 #self.status.set('%s/%s processing for %s climate type: %s' %(start, stepps, culture_type, climate_type), per) #try: #sf_files = sf.get_segetalflora(resource=ncs, culture_type=cult, climate_type=clim) # dir_output=dir_tas, #self.status.set("processing of %s segetalflora files done " % (len(sf_files)) , 95) #except Exception as e: #logging.exception('segetalflora calculation failed %s %s' % ( climate_type, culture_type)) #raise ## === fieldmeans #from flyingpigeon import timeseries as ts #self.status.set('processing fieldmeans' , 97) #try: #ncs = [path.join(dir_segetalflora,nc) for nc in listdir(dir_segetalflora)] #ncs_fld = ts.fldmean(ncs, dir_output=dir_fieldmean) #logging.debug('%s fieldmeans processed' % (len(ncs_fld))) #except Exception as e: #logging.exception('fieldmeans failed') #raise ## === visualisation #from flyingpigeon import visualisation as vs #from os import rename #self.status.set('processing visualisation' , 98) ## sort files for plotting #try: #ncs = listdir(dir_segetalflora) #set_var = set() #set_contry = set() #for nc in ncs: #set_var = set_var.union([nc.split('_')[0]]) #set_contry = set_contry.union([nc.split('_')[1]]) #logging.debug('%s files to plots sorted' % (len(ncs))) #except Exception as e: #logging.exception('files sorting failed') #raise ## plot sorted files #try: #plots = [] #for v in set_var: #for c in set_contry: #ncs = [path.join(dir_segetalflora,nc) for nc in listdir(dir_segetalflora) if v in nc and c in nc ] #p = vs.spaghetti(ncs, #variable=v, #title='Segetalflora %s in %s' % (v, c), #dir_out=dir_plots) #newname = path.dirname(p)+'/%s_%s_birdhouse_output.html' %(v,c) #rename(p,newname) #plots.append(newname) #logging.debug('plot created and renamed for %s %s' % (v, c )) #except Exception as e: #logging.exception('ploting failed') #raise #################### # tar file archiving #################### from flyingpigeon.utils import archive try: self.status.set('files to tar archives', 99) tar_sf = archive(nc_sf, format='tar', dir_output='.', mode='w') tar_tasmean = archive(nc_tasmean, format='tar', dir_output='.', mode='w') logging.info('Archives prepared') except Exception as e: logger.debug('failed to archive files %s' % e) # === set output parameter #self.out_fieldmeans.setValue( tarf_fieldmeans ) self.out_segetalflora.setValue(tar_sf) self.out_tasmean.setValue(tar_tasmean) #self.out_plots.setValue( tarf_plots ) self.status.set("processing done", 100)
def _handler(self, request, response): init_process_logger('log.txt') response.outputs['output_log'].file = 'log.txt' process_start_time = time.time() # measure process execution time ... response.update_status( 'execution started at: {}'.format(str(dt.now())), 5) LOGGER.debug('starting segetalflora process execution') response.update_status('starting calcualtion segetalflora', 5) ############################ # read argments to variables ############################ try: resource = archiveextract( resource=rename_complexinputs(request.inputs['resource'])) climate_type = request.inputs['climate_type'][0].data culture_type = request.inputs['culture_type'][0].data LOGGER.info('urls for {} ncs found'.format(len(resource))) LOGGER.info('culture type: {}'.format(culture_type)) except Exception as ex: msg = 'Failed to read in the arguments: {}'.format(str(ex)) LOGGER.exception(msg) raise Exception(msg) try: if type(climate_type) != list: climate_type = list([climate_type]) if type(culture_type) != list: culture_type = list([culture_type]) LOGGER.info('arguments are lists') except Exception as ex: msg = 'Failed to transform arguments to lists: {}'.format(str(ex)) LOGGER.exception(msg) raise Exception(msg) ############################# # get yearly mean temperature ############################# nc_tasmean = sf.get_yrmean(resource) ####################################### # main call for segetalflora processing ####################################### nc_sf = sf.get_segetalflora(resource=nc_tasmean, culture_type=culture_type, climate_type=climate_type) #################### # tar file archiving #################### try: response.update_status('preparing output', 99) LOGGER.debug('length of sf: {}'.format(len(nc_sf))) if len(nc_sf) == 1: # TODO: fix pywps output formats OR use separate output params. response.outputs['out_segetalflora'].file = nc_sf[0] response.outputs['out_segetalflora'].format = FORMATS.NETCDF else: response.outputs['out_segetalflora'].file = archive( nc_sf, format='tar', dir_output='.', mode='w') response.outputs['out_segetalflora'].format = Format( 'application/x-tar') if len(nc_tasmean) == 1: response.outputs['out_tasmean'].file = nc_tasmean[0] response.outputs['out_segetalflora'].format = FORMATS.NETCDF else: response.outputs['out_tasmean'].file = archive(nc_tasmean, format='tar', dir_output='.', mode='w') response.outputs['out_segetalflora'].format = Format( 'application/x-tar') except Exception as ex: msg = 'Failed to prepare output files: {}'.format(str(ex)) LOGGER.exception(msg) raise Exception(msg) response.update_status('done', 100) LOGGER.debug( "total execution took {} seconds.".format(time.time() - process_start_time)) return response