FFSM++  1.1.0
French Forest Sector Model ++
merge_lib Namespace Reference

Functions

def merge (forIFiles_h=[], prdIFiles_h=[], carbonIFiles_h=[], scenarios_h=[], forOFilename_h="", prdOFilename_h="", carbonOFilename_h="", variables_h=[], regions_h=[], years_h=[])
 
def determinePositions (headerRow)
 
def merge_single_file (i_filename_h, o_filename_h, scenarios_h, keepHeader=False, variables_h=[], regions_h=[], years_h=[])
 

Function Documentation

def merge_lib.determinePositions (   headerRow)

Definition at line 29 of file merge_lib.py.

Referenced by merge_single_file().

29 def determinePositions(headerRow):
30  fields = headerRow.split(';')
31  returnValues = [-1,-1,-1]
32  for idx, field in enumerate(fields):
33  if(field == 'parName' or field == 'balItem'): returnValues[0] = idx
34  if(field == 'region'): returnValues[1] = idx
35  if(field == 'year'): returnValues[2] = idx
36  if (returnValues[0] == -1 or returnValues[1] == -1 or returnValues[2] == -1):
37  print ("There has been an error reading the headers of a file.")
38  exit(1)
39  return returnValues
40 
41 # =============================================================================
def determinePositions(headerRow)
Definition: merge_lib.py:29

Here is the caller graph for this function:

def merge_lib.merge (   forIFiles_h = [],
  prdIFiles_h = [],
  carbonIFiles_h = [],
  scenarios_h = [],
  forOFilename_h = "",
  prdOFilename_h = "",
  carbonOFilename_h = "",
  variables_h = [],
  regions_h = [],
  years_h = [] 
)

Definition at line 5 of file merge_lib.py.

5 def merge(forIFiles_h=[],prdIFiles_h=[],carbonIFiles_h=[],scenarios_h=[],forOFilename_h="",prdOFilename_h="",carbonOFilename_h="", variables_h=[], regions_h=[], years_h=[]):
6  print("*** Processing..")
7  if len(forIFiles_h)>0:
8  open(forOFilename_h,'w').close()
9  if len(prdIFiles_h)>0:
10  open(prdOFilename_h,'w').close()
11  if len(carbonIFiles_h)>0:
12  open(carbonOFilename_h,'w').close()
13  forCounter=0
14  prdCounter=0
15  carbonCounter=0
16  for forIFile in forIFiles_h:
17  merge_single_file(forIFile, forOFilename_h, scenarios_h, False if forCounter else True, variables_h, regions_h, years_h)
18  forCounter += 1
19  for prdIFile in prdIFiles_h:
20  merge_single_file(prdIFile, prdOFilename_h, scenarios_h, False if prdCounter else True, variables_h, regions_h, years_h)
21  prdCounter += 1
22  for carbonIFile in carbonIFiles_h:
23  merge_single_file(carbonIFile, carbonOFilename_h, scenarios_h, False if carbonCounter else True, variables_h, regions_h, years_h)
24  carbonCounter += 1
25  print ("*** Done!")
26 
27 
28 # =============================================================================
def merge(forIFiles_h=[], prdIFiles_h=[], carbonIFiles_h=[], scenarios_h=[], forOFilename_h="", prdOFilename_h="", carbonOFilename_h="", variables_h=[], regions_h=[], years_h=[])
Definition: merge_lib.py:5
def merge_single_file(i_filename_h, o_filename_h, scenarios_h, keepHeader=False, variables_h=[], regions_h=[], years_h=[])
Definition: merge_lib.py:42

Here is the call graph for this function:

def merge_lib.merge_single_file (   i_filename_h,
  o_filename_h,
  scenarios_h,
  keepHeader = False,
  variables_h = [],
  regions_h = [],
  years_h = [] 
)

Definition at line 42 of file merge_lib.py.

Referenced by merge().

42 def merge_single_file(i_filename_h, o_filename_h, scenarios_h, keepHeader=False, variables_h=[], regions_h=[], years_h=[]):
43  i_file = open(i_filename_h,'r')
44  o_file = open(o_filename_h,'a')
45  newRow = 1
46  counterRow = 0
47  parNamePos = -1
48  regionPos = -1
49  yearPos = -1
50  positions = []
51 
52  while newRow:
53  row = i_file.readline()
54  scenarioFilter = False
55  variableFilter = False
56  regionFilter = False
57  yearFilter = False
58  finalFilter = False
59 
60  if row == '':
61  break
62  if(counterRow == 0):
63  positions = determinePositions(row)
64  parNamePos = positions[0]
65  regionPos = positions[1]
66  yearPos = positions[2]
67  if(keepHeader):
68  o_file.write(row)
69  counterRow += 1
70  fields = row.split(';')
71  rowScenario = fields[0]
72 
73  if(rowScenario in scenarios_h):
74  scenarioFilter = True
75 
76  if( (len(variables_h) == 0 ) or (fields[parNamePos] in variables_h) ):
77  variableFilter = True
78 
79  if( (len(regions_h) == 0) or (fields[regionPos] in regions_h) ):
80  regionFilter = True
81 
82  if( (len(years_h) == 0) or (fields[yearPos] in years_h) ):
83  yearFilter = True
84 
85  if (scenarioFilter and variableFilter and regionFilter and yearFilter):
86  finalFilter = True
87 
88  if(finalFilter):
89  o_file.write(row)
90  i_file.close()
91  o_file.close()
92 
def determinePositions(headerRow)
Definition: merge_lib.py:29
def merge_single_file(i_filename_h, o_filename_h, scenarios_h, keepHeader=False, variables_h=[], regions_h=[], years_h=[])
Definition: merge_lib.py:42

Here is the call graph for this function:

Here is the caller graph for this function: