|
33 | 33 |
|
34 | 34 | def create_json(input): |
35 | 35 |
|
36 | | -with open("input", "r" ) as rings: |
37 | | - data= rings.read() |
38 | | - lines = data.split("\n") |
39 | | - print (lines[0-2]) |
40 | | - # read every single line |
41 | | - for rows in lines: |
42 | | - print (rows) |
| 36 | + with open("input", "r" ) as rings: |
| 37 | + data= rings.read() |
| 38 | + lines = data.split("\n") |
| 39 | + print (lines[0-2]) |
| 40 | + # read every single line |
| 41 | + for rows in lines: |
| 42 | + print (rows) |
| 43 | + |
| 44 | + # remove empty lines from the file. |
| 45 | + |
| 46 | + # convert every element in each list to string- it is easier to manupilate the elements |
| 47 | + |
| 48 | + for items in lines: |
| 49 | + str(items) |
| 50 | + |
| 51 | + # State/province |
| 52 | + state_province = lines[1,3] |
| 53 | + |
| 54 | + # country |
| 55 | + country= lines[0,2] |
| 56 | + |
| 57 | + # species |
| 58 | + species = lines[1,4] |
| 59 | + |
| 60 | + # species code |
| 61 | + species_code= lines[1,5] |
| 62 | + |
| 63 | + # start year- start of collection |
| 64 | + start_date = int(lines[1,6]) |
| 65 | + |
| 66 | + # end year- completion year |
| 67 | + end_year = int(lines[1,7]) |
| 68 | + |
| 69 | + # latitude, longitude, elevation |
| 70 | + latitude = lines[2,4] |
| 71 | + longitude = lines[2,5] |
| 72 | + |
| 73 | + # lead investigator |
| 74 | + lead_investigator= lines[2,2] |
| 75 | + |
| 76 | + # site_id |
| 77 | + # Via indexing the first three digits of the site id are assigned to var named site_code and the rest to information. |
| 78 | + # Check if all the site codes are the same for the whole dataset |
| 79 | + for site_code in lines: |
| 80 | + current=0 |
| 81 | + information=0 |
| 82 | + site_id=0 |
| 83 | + unit = 0 |
| 84 | + while current >=0 and current<=len(lines)-1: |
| 85 | + if current <=3: |
| 86 | + site_code = lines[current,0,0] |
| 87 | + if current>=4 and current<=len(lines)-1: |
| 88 | + if lines[current,0,0] == lines[4,0,0]: |
| 89 | + site_id = lines[current,0,0] |
| 90 | + information = site_id[3::] |
| 91 | + site_id= site_code + information |
| 92 | + #data |
| 93 | + data=[] |
| 94 | + y=1 |
| 95 | + past= y-1 |
| 96 | + end=[current,-1] |
| 97 | + if site_id == lines[current,0] and start_date <= int(lines[current]): |
| 98 | + for points in lines[current,2:]: |
| 99 | + pts=[] |
| 100 | + pts.append(points) |
| 101 | + # End of data collection for that year |
| 102 | + if points== "999" or points== "-9999": |
| 103 | + pts[:-1] |
| 104 | + data.append(pts) |
| 105 | + pts=0 |
| 106 | + unit = points |
| 107 | + else: |
| 108 | + current = current + 1 |
| 109 | + pts=0 |
43 | 110 |
|
44 | | - # remove empty lines from the file. |
45 | | - |
46 | | - # convert every element in each list to string- it is easier to manupilate the elements |
47 | | - |
48 | | - for items in lines: |
49 | | - str(items) |
50 | | - |
51 | | - # State/province |
52 | | - state_province = lines[1,3] |
53 | | - |
54 | | - # country |
55 | | - country= lines[0,2] |
56 | | - |
57 | | - # species |
58 | | - species = lines[1,4] |
59 | | - |
60 | | - # species code |
61 | | - species_code= lines[1,5] |
62 | | - |
63 | | - # start year- start of collection |
64 | | - start_date = int(lines[1,6]) |
65 | | - |
66 | | - # end year- completion year |
67 | | - end_year = int(lines[1,7]) |
68 | | - |
69 | | -# latitude, longitude, elevation |
70 | | - latitude = lines[2,4] |
71 | | - longitude = lines[2,5] |
72 | | - |
73 | | - # lead investigator |
74 | | - lead_investigator= lines[2,2] |
75 | | - |
76 | | - # site_id |
77 | | - # Via indexing the first three digits of the site id are assigned to var named site_code and the rest to information. |
78 | | - # Check if all the site codes are the same for the whole dataset |
79 | | - for site_code in lines: |
80 | | - current=0 |
81 | | - information=0 |
82 | | - site_id=0 |
83 | | - unit = 0 |
84 | | - while current >=0 and current<=len(lines)-1: |
85 | | - if current <=3: |
86 | | - site_code = lines[current,0,0] |
87 | | - if current>=4 and current<=len(lines)-1: |
88 | | - if lines[current,0,0] == lines[4,0,0]: |
89 | | - site_id = lines[current,0,0] |
90 | | - information = site_id[3::] |
91 | | - site_id= site_code + information |
92 | | - #data |
93 | | - data=[] |
94 | | - y=1 |
95 | | - past= y-1 |
96 | | - end=[current,-1] |
97 | | - if site_id == lines[current,0] and start_date <= int(lines[]): |
98 | | - for points in lines[current,2:]: |
99 | | - pts=[] |
100 | | - pts.append(points) |
101 | | - # End of data collection for that year |
102 | | - if points== "999" or points== "-9999": |
103 | | - pts[:-1] |
104 | | - data.append(pts) |
105 | | - pts=0 |
106 | | - unit = points |
107 | | - else: |
108 | | - current = current + 1 |
109 | | - pts=0 |
110 | | - |
111 | | -jsonfile = {} |
112 | | -jsonfile["site_code"] = site_code |
113 | | -jsonfile["site_name"] = site_id |
114 | | -jsonfile["species_code"] = species_code |
115 | | -jsonfile["species_name"] = species |
116 | | -jsonfile["country"] = country |
117 | | -jsonfile["state_province"] = state_province |
118 | | -jsonfile["elevation"] = "" |
119 | | -jsonfile["latitude"] = latitude |
120 | | -jsonfile["longitude"] = longitude |
121 | | -if unit == "999": |
122 | | - jsonfile["unit"] = "0.01mm" |
123 | | -elif unit == "-9999": |
124 | | - jsonfile["unit"] = "0.001mm" |
125 | | -else: |
126 | | - print("ERROR - Stop code is not 999 or -9999. Cannot identify a unit.") |
127 | | - exit |
128 | | -jsonfile["lead_investigator"] = lead_investigator |
129 | | -jsonfile["collection_date"] = "" |
| 111 | + jsonfile = {} |
| 112 | + jsonfile["site_code"] = site_code |
| 113 | + jsonfile["site_name"] = site_id |
| 114 | + jsonfile["species_code"] = species_code |
| 115 | + jsonfile["species_name"] = species |
| 116 | + jsonfile["country"] = country |
| 117 | + jsonfile["state_province"] = state_province |
| 118 | + jsonfile["elevation"] = "" |
| 119 | + jsonfile["latitude"] = latitude |
| 120 | + jsonfile["longitude"] = longitude |
| 121 | + if unit == "999": |
| 122 | + jsonfile["unit"] = "0.01mm" |
| 123 | + elif unit == "-9999": |
| 124 | + jsonfile["unit"] = "0.001mm" |
| 125 | + else: |
| 126 | + print("ERROR - Stop code is not 999 or -9999. Cannot identify a unit.") |
| 127 | + exit |
| 128 | + jsonfile["lead_investigator"] = lead_investigator |
| 129 | + jsonfile["collection_date"] = "" |
0 commit comments