| 1 | # -*- coding: iso-8859-15 -*- |
|---|
| 2 | # L. Fita, LMD. Feburary 2015. Python script to generate a new ASCII file from a |
|---|
| 3 | # nother with [char] separated values |
|---|
| 4 | ## e.g. # create_list.py -a ';' -c 1,7,6 -f MeteoFrancesfc/0_Documentation/POSTES |
|---|
| 5 | ## e.g. # create_list.py -a ',' -c 1,2,3 -f AEMETsfc/0_Documentation/AWS_AEMET-stations.csv |
|---|
| 6 | import numpy as np |
|---|
| 7 | import os |
|---|
| 8 | import re |
|---|
| 9 | from optparse import OptionParser |
|---|
| 10 | |
|---|
| 11 | main = 'create_list.py' |
|---|
| 12 | errormsg = 'ERROR -- error -- ERROR -- error' |
|---|
| 13 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
|---|
| 14 | |
|---|
| 15 | def remove_NONascii(string): |
|---|
| 16 | """ Function to remove that characters which are not in the standard 127 ASCII |
|---|
| 17 | string= string to transform |
|---|
| 18 | >>> remove_NONascii('LluÃs') |
|---|
| 19 | Lluis |
|---|
| 20 | """ |
|---|
| 21 | fname = 'remove_NONascii' |
|---|
| 22 | |
|---|
| 23 | newstring = string |
|---|
| 24 | |
|---|
| 25 | RTFchar= ['á', 'é', 'Ã', 'ó', 'ú', 'à ', 'Ú', 'ì', 'ò', 'ù', 'â', 'ê', 'î', 'ÃŽ', \ |
|---|
| 26 | 'û', 'À', 'ë', 'ï', 'ö', 'ÃŒ', 'ç', 'ñ','Ê', 'Å', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', \ |
|---|
| 27 | 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã',\ |
|---|
| 28 | 'Ã', 'Å', '\n', '\t'] |
|---|
| 29 | ASCchar= ['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', \ |
|---|
| 30 | 'u', 'a', 'e', 'i', 'o', 'u', 'c', 'n','ae', 'oe', 'A', 'E', 'I', 'O', 'U', 'A', \ |
|---|
| 31 | 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'C', 'N',\ |
|---|
| 32 | 'AE', 'OE', '', ' '] |
|---|
| 33 | |
|---|
| 34 | Nchars = len(RTFchar) |
|---|
| 35 | for ichar in range(Nchars): |
|---|
| 36 | foundchar = string.find(RTFchar[ichar]) |
|---|
| 37 | if foundchar != 0: |
|---|
| 38 | newstring = newstring.replace(RTFchar[ichar], ASCchar[ichar]) |
|---|
| 39 | |
|---|
| 40 | return newstring |
|---|
| 41 | |
|---|
| 42 | ####### ###### ##### #### ### ## # |
|---|
| 43 | |
|---|
| 44 | parser = OptionParser() |
|---|
| 45 | parser.add_option("-a", "--chracter", dest="cchar", |
|---|
| 46 | help="chracter of new value", metavar="VALUE") |
|---|
| 47 | parser.add_option("-c", "--columns", dest="cols", |
|---|
| 48 | help="',' list of cols to use (1 based, sorted)", metavar="VALUES") |
|---|
| 49 | parser.add_option("-f", "--file", dest="stfile", help="stations file", |
|---|
| 50 | metavar="FILE") |
|---|
| 51 | (opts, args) = parser.parse_args() |
|---|
| 52 | |
|---|
| 53 | ######## ####### |
|---|
| 54 | ## MAIN |
|---|
| 55 | ####### |
|---|
| 56 | |
|---|
| 57 | ofile = 'newlist.dat' |
|---|
| 58 | |
|---|
| 59 | if not os.path.isfile(opts.stfile): |
|---|
| 60 | print errormsg |
|---|
| 61 | print ' ' + main + ": stations file '" + opts.stfile + "' does not exist !!" |
|---|
| 62 | quit(-1) |
|---|
| 63 | |
|---|
| 64 | desiredcols = opts.cols.split(',') |
|---|
| 65 | |
|---|
| 66 | print 'desiredcols:',desiredcols |
|---|
| 67 | |
|---|
| 68 | oasciif = open(opts.stfile, 'r') |
|---|
| 69 | onewf = open(ofile, 'w') |
|---|
| 70 | |
|---|
| 71 | for line in oasciif: |
|---|
| 72 | vals = line.replace('\n','').replace(chr(13),'') |
|---|
| 73 | values = vals.split(opts.cchar) |
|---|
| 74 | |
|---|
| 75 | newline = '' |
|---|
| 76 | for icol in range(len(desiredcols)): |
|---|
| 77 | newline = newline + " " + remove_NONascii(values[int(desiredcols[icol])-1]).replace('"','') |
|---|
| 78 | |
|---|
| 79 | # print vals |
|---|
| 80 | # print values |
|---|
| 81 | # print newline |
|---|
| 82 | |
|---|
| 83 | onewf.write(newline + '\n') |
|---|
| 84 | |
|---|
| 85 | oasciif.close() |
|---|
| 86 | onewf.close() |
|---|
| 87 | |
|---|
| 88 | print main + ": sucessfull written of '" + ofile + "' !!" |
|---|