source: trunk/src/write_zi.py @ 54

Last change on this file since 54 was 9, checked in by pinsard, 10 years ago

fix thanks to coding rules

File size: 8.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4__docformat__ = 'reStructuredText'
5__autodoc__ = ['write_zi']
6
7import os
8import sys
9import csv
10
11def write_zi(west_value, east_value, north_value, south_value, zi_code, zi_description):
12
13    """
14
15    ========
16    write_zi
17    ========
18
19    .. :py:function:: write_zi(west_value, east_value, north_value, south_value, zi_code, zi_description)
20
21    DESCRIPTION
22    ===========
23
24    Write in a file :file:`${PROJECT_ID}/maps/varamma_zi_write_zi_py.csv`
25
26    Parameters
27    ----------
28
29    west_value : array_like
30        west most longitudes
31
32    east_value : array_like
33        east most longitudes
34
35    north_value : array_like
36        north most latitudes
37
38    south_value : array_like
39        south most latitudes
40
41    zi_code : array_like of strings
42        codes of the zone of interest
43
44    zi_description: array_like of strings
45        descriptions of the zone of interest
46
47    Returns
48    -------
49
50    result : int
51        1 if ok
52
53    Raises
54    ------
55
56    Missing parameters
57
58    EXAMPLES
59    ========
60
61    From scratch to write some simulated zi data in
62    :file:`${PROJECT_ID}/maps/varamma_zi_write_zi_py.csv`:
63
64    >>> # From scratch to write some simulated zi data in
65    >>> # :file:`${PROJECT_ID}/maps/varamma_zi_write_zi_py.csv`::
66    >>> # ++ matlab varamma_startup
67    >>>
68    >>> from simul_zi import simul_zi
69    >>> index_simulation = 1
70    >>> (west_value, east_value, north_value, south_value, zi_code, zi_description) = simul_zi(index_simulation)
71    >>> result = write_zi(west_value, east_value, north_value, south_value, zi_code, zi_description)
72    iii : opening for writing /usr/work/incas/fplod/varamma_d//maps/varamma_zi_write_zi_py.csv
73
74
75    From real file :file:`${PROJECT_ID}/maps/varamma_zi.csv`:
76
77    >>> from read_zi import read_zi
78    >>> (west_value, east_value, north_value, south_value, zi_code, zi_description) = read_zi()
79    iii : opening for reading /usr/work/incas/fplod/varamma_d//maps/varamma_zi.csv
80    >>> result = write_zi(west_value, east_value, north_value, south_value, zi_code, zi_description)
81    iii : opening for writing /usr/work/incas/fplod/varamma_d//maps/varamma_zi_write_zi_py.csv
82
83    SEE ALSO
84    ========
85
86    :ref:`zones`
87
88    py:func:`simul_zi`
89    py:func:`read_zi`
90
91    TODO
92    ====
93
94    coding rules
95
96    connexion with define_area
97
98    get rid of matlab stuff
99
100    avoid over writing
101
102    remove formatting (keept for learning issue)
103
104    include in sphinx processing
105
106    cf. http://forums.arcgis.com/threads/37866-python-script-for-batch-coversion-from-dbf-to-csv-files
107
108    cf. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//0017000000n4000000
109
110    EVOLUTIONS
111    ==========
112
113    $Id: write_zi.py 377 2011-09-07 15:25:13Z pinsard $
114
115    $URL: svn+ssh://pinsard@forge.ipsl.jussieu.fr/ipsl/forge/projets/varamma/svn/trunk/src/write_zi.py $
116
117    - fplod 20110907T090235Z cratos (Linux)
118
119      * add test from real file
120
121    - fplod 20110906T125953Z aedon.locean-ipsl.upmc.fr (Darwin)
122
123      * correction of test 1 to avoid Expecting nothing
124      * suppress quote around numeric
125      * better usage of format
126        see http://docs.python.org/library/string.html#formatstrings
127        but not used ... because of interaction with QUOTE_NONNUMERIC
128
129    - fplod 20110816T151904Z aedon.locean-ipsl.upmc.fr (Darwin)
130
131      * still draft but output file is created
132
133    - fplod 20110802T155839Z aedon.locean-ipsl.upmc.fr (Darwin)
134
135      * creation from write_zi.m to learn csv python writing
136        see http://www.commentcamarche.net/faq/2382-python-lire-et-ecrire-des-fichiers-csv
137        and http://www.chicoree.fr/w/Fichiers_CSV_en_Python/
138        and http://www.doughellmann.com/PyMOTW/csv/
139
140    """
141
142    result = -1
143    #
144    usage = 'result = write_zi(west_value, east_value, north_value, south_value, zi_code, zi_description)'
145    #
146    # ++ matlabif nargin~=6
147    # ++ matlab   disp(['Incorrect number of arguments ' num2str(nargin)])
148    # ++ matlab   error(usage)
149    # ++ matlabend
150    #
151    # ++ matlabarg_info=whos('west_value')
152    # ++ matlabif ~strcmp(arg_info.class,'double')
153    # ++ matlab   disp(['Incorrect type of arg west_value' arg_info.class])
154    # ++ matlab   whos west_value
155    # ++ matlab   error(usage)
156    # ++ matlabend
157    #
158    # ++ matlab if ~isvector(west_value)
159    # ++ matlab    disp('arg west_value must be a vector')
160    # ++ matlab    error(usage)
161    # ++ matlab end
162    #
163    # ++ matlab arg_info=whos('east_value')
164    # ++ matlab if ~strcmp(arg_info.class,'double')
165    # ++ matlab    disp(['Incorrect type of arg east_value ' arg_info.class])
166    # ++ matlab    whos east_value
167    # ++ matlab    error(usage)
168    # ++ matlab end
169    # ++ matlab #
170    # ++ matlab if ~isvector(east_value)
171    # ++ matlab    disp('arg east_value must be a vector')
172    # ++ matlab    error(usage)
173    # ++ matlab end
174    # ++ matlab #
175    # ++ matlab arg_info=whos('north_value')
176    # ++ matlab if ~strcmp(arg_info.class,'double')
177    # ++ matlab    disp(['Incorrect type of arg north_value' arg_info.class])
178    # ++ matlab    whos north_value
179    # ++ matlab    error(usage)
180    # ++ matlab end
181    #
182    # ++ matlab if ~isvector(north_value)
183    # ++ matlab    disp('arg north_value must be a vector')
184    # ++ matlab    error(usage)
185    # ++ matlab end
186    # ++ matlab #
187    # ++ matlab arg_info=whos('south_value')
188    # ++ matlab if ~strcmp(arg_info.class,'double')
189    # ++ matlab    disp(['Incorrect type of arg south_value ' arg_info.class])
190    # ++ matlab    whos south_value
191    # ++ matlab    error(usage)
192    # ++ matlab end
193    #
194    # ++ matlab if ~isvector(south_value)
195    # ++ matlab    disp('arg south_value must be a vector')
196    # ++ matlab    error(usage)
197    # ++ matlab end
198    #
199    # ++ matlab arg_info=whos('zi_code')
200    # ++ matlab if ~strcmp(arg_info.class,'cell')
201    # ++ matlab    disp(['Incorrect type of arg zi_code ' arg_info.class])
202    # ++ matlab    whos zi_code
203    # ++ matlab    error(usage)
204    # ++ matlab end
205    #
206    # +todo+   disp('arg zi_code must be a cell array of strings')
207    #
208    #
209    # ++ matlab arg_info=whos('zi_description')
210    # ++ matlab if ~strcmp(arg_info.class,'cell')
211    # ++ matlab    disp(['Incorrect type of arg zi_description ' arg_info.class])
212    # ++ matlab    whos zi_description
213    # ++ matlab    error(usage)
214    # ++ matlab end
215    #
216    # +todo+   disp('arg zi_description must be a cell array of strings')
217    #
218    # ++ matlab # check for size of nb_lat nb_lon nb_zi
219    nb_zi = len(zi_description)
220    #
221    # ++ matlab test_consistent = isequal(len(west_value), len(east_value), len(north_value, south_value), len(zi_code), len(zi_description))
222    # ++ matlab   otherwise
223    # ++ matlab      error('unknown application running')
224    # ++ matlab end
225    test_consistent = 1 # force++
226    if not test_consistent:
227        print ('eee : inconsistent dimensions of west_value, east_value, north_value, south_value, zi_code and zi_description')
228        west_value
229        east_value
230        north_value, south_value
231        zi_code
232        zi_description
233        print (usage)
234        sys.exit(0)
235    #
236    project_id_env = os.environ['PROJECT_ID']
237
238    # build ASCII filename to be written
239    # ++ replace 'write_zi' by the name of this function
240    fullfilename = project_id_env + '/maps/' + 'varamma_zi' + '_' + 'write_zi' + '_py' + '.csv'
241    print('iii : opening for writing %s' % (fullfilename))
242    fid = open(fullfilename, "wb")
243    fid_csv = csv.writer(fid,quoting=csv.QUOTE_NONNUMERIC)
244    #
245    # +todo+ check opening ok
246    #
247    # write header
248    fid_csv.writerow(["code", "description", "north", "south", "east", "west"])
249    #
250    # write data
251    #
252    sep = ','
253    format_lat = '6.3f'
254    format_lon = '7.3f'
255    # loop on zi to write each data line
256    for index_zi in range(0, nb_zi):
257        description_trim=zi_description[index_zi]
258        code_trim=zi_code[index_zi]
259        west_trim="{0:{format_lon}}".format(west_value[index_zi],format_lon=format_lon)
260        east_trim="{0:{format_lon}}".format(east_value[index_zi],format_lon=format_lon)
261        north_trim="{0:{format_lat}}".format(north_value[index_zi],format_lat=format_lat)
262        south_trim="{0:{format_lat}}".format(south_value[index_zi],format_lat=format_lat)
263        fid_csv.writerow(
264           (
265           '%s' % zi_code[index_zi],
266           '%s' % zi_description[index_zi],
267            north_value[index_zi],
268            south_value[index_zi],
269            east_value[index_zi],
270            west_value[index_zi])
271        )
272    #
273    fid.close()
274    #
275    #++whos
276    result = 0
277    #
278
279# Run main, if called from the command line
280if __name__ == '__main__':
281    import doctest
282    doctest.testmod()
Note: See TracBrowser for help on using the repository browser.