Skip to content

Commit

Permalink
Parse DD/MM/YYYY format in occurrence upload (#4172)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Aug 21, 2024
1 parent 312ca68 commit 70f26c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
55 changes: 28 additions & 27 deletions bims/scripts/occurrences_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,39 @@ def finish_processing_row(self, row, record):
pass

def parse_date(self, row):
# Parse date string to date object
# Raise value error if date string is not in a valid format
sampling_date = None
def try_parse_date(date_str, date_format):
try:
return datetime.strptime(date_str, date_format)
except ValueError:
return None

# Retrieve the date string from the row
date_string = DataCSVUpload.row_value(row, SAMPLING_DATE)
if not date_string:
date_string = DataCSVUpload.row_value(row, SAMPLING_DATE_2)
if '-' in date_string:
try:
sampling_date = datetime.strptime(
date_string, '%Y-%m-%d')
finally:
if not sampling_date:
self.handle_error(
row=row,
message='Incorrect date format'
)
return sampling_date
else:
try:
sampling_date = datetime.strptime(
date_string, '%Y/%m/%d')
except ValueError:
sampling_date = datetime.strptime(
date_string, '%m/%d/%Y')
finally:
if not sampling_date:
self.handle_error(
row=row,
message='Incorrect date format'
)

# If date_string is still None, handle the error
if not date_string:
self.handle_error(row=row, message='Date string is missing')
return None

# Define possible date formats
date_formats = [
'%Y-%m-%d',
'%Y/%m/%d',
'%d/%m/%Y',
]

# Attempt to parse the date string using the defined formats
for date_format in date_formats:
sampling_date = try_parse_date(date_string, date_format)
if sampling_date:
return sampling_date

# If no valid format found, handle the error
self.handle_error(row=row, message='Incorrect date format')
return None

def process_survey(self, record, location_site, sampling_date, collector):
"""Process survey data"""
try:
Expand Down
2 changes: 1 addition & 1 deletion bims/tests/data/csv_upload_test.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
UUID,User River Name,River Name,User Wetland Name,Wetland name,User Site Code,Site Code,Ecosystem type,Site description,User geomorphological zone,Latitude,Longitude,Park or MPA name,Sampling date,Kingdom,Phylum,Class,Order,Family,Genus,Species,SubSpecies,Taxon,Taxon rank,Sampling method,Sampling effort measure,Sampling effort value,Abundance measure,Abundance value,Hydroperiod,Wetland indicator status,Broad biotope,Specific biotope,Substratum,Origin,Endemism,Conservation status global,Conservation status national,Collector/owner,Collector/owner institute,Analyst,Analyst institute,Authors,Year,Source,Reference category,Title,Doi/url,Notes,Upstream id,Taxon key,Species key,Basis of record,Institution code,Collection code,Catalog number,Identified by,Rights holder,Recorded by,Decision support tool,Record type,Curation process,Biomass indicator: chl a,Biomass indicator: afdm,Autotrophic index (ai),Water level,Water turbidity,Embeddedness,Temperature (degrees c),Conductivity (ms/m),Ph (ph unit),Oxygen: dissolved (mg/l),Oxygen: dissolved: percentage saturation (%),Turbidity (ntu scale) (ntu),Depth of stream or river (m),Near bed velocity (m/s),Phosphorus: soluble reactive (mg/l),Phosphorus: total (mg/l),Nitrogen: ammonia (mg/l),Nitrogen: ammonium (mg/l),Nitrogen: nitrate + nitrite (mg/l),Nitrogen: nitrite (mg/l),Nitrogen: nitrate (mg/l),Nitrogen: total inorganic (mg/l),Chlorophyll a: benthic (mg/m2),Ash free dry mass: benthic (mg/m2),Sa province and sadc boundaries,Sa ecoregion level 2,Sa ecoregion level 1,Strategic water source areas - surface,Strategic water source areas - ground,Freshwater ecoregion of the world,Nfepa wetlands vegetation,Tertiary catchment area,Primary catchment area,Quaternary catchment area,Secondary catchment area,Quinary catchment area,Sub water management areas,Nfepa fish sanctuaries all species,Nfepa wetlands 2011,Nfepa rivers 2011,Nfepa fish sanctuaries 2011,Nfepa river fepas 2011,Nfepa wetland clusters 2011,National critical biodiversity,National ecological support areas,Present ecological state 1999,River condition 1999,Present ecological state 2018,Ecosystem threat status,Ecosystem protection level,Thermal framework,Hydrological regions,System resilience,Model accuracy,Hydrological region flow type,Geomorphological zone,Monthly average temperature january,Monthly average temperature february,Monthly average temperature march,Monthly average temperature april,Monthly average temperature may,Monthly average temperature june,Monthly average temperature july,Monthly average temperature august,Monthly average temperature september,Monthly average temperature october,Monthly average temperature november,Monthly average temperature december,Algae pesticide risk,Fish pesticide risk,Invertebrate pesticide risk,River management unit,Water management area,Nfepa quinaries,Division,Taxonomic status
5a08bfe1-0e9b-4e0e-bf30-5b50156d35a9,User River Name,Unknown,User Wetland Name,-,TEST-123,A1BRAK-00007,River,Soutpan,-,-24.746111,26.071944,,2023-10-23,Chromista,Ochrophyta,Bacillariophyceae,Naviculales,Achnanthaceae,Achnanthes,eutrophila,-,Achnanthes eutrophila,Species,In situ,min,12,Species valve/frustule count,3.0,-,-,Aquatic vegetation,Bedrock,Bedrock,Unknown,Unknown,Not evaluated,Not evaluated,Kartoza_Admin ,-,-,-,-,-,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town",Database,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town, 2022",,,,-,-,-,-,-,-,-,-,-,-,Photographic recor,Preserved lugols,Whole cobble,Whole cobble,13.0,,,,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,11.0,12.0,North West,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,Bacillariophyta,Accepted
5a08bfe1-0e9b-4e0e-bf30-5b50156d35a9,User River Name,Unknown,User Wetland Name,-,TEST-123,A1BRAK-00007,River,Soutpan,-,-24.746111,26.071944,,23/10/2023,Chromista,Ochrophyta,Bacillariophyceae,Naviculales,Achnanthaceae,Achnanthes,eutrophila,-,Achnanthes eutrophila,Species,In situ,min,12,Species valve/frustule count,3.0,-,-,Aquatic vegetation,Bedrock,Bedrock,Unknown,Unknown,Not evaluated,Not evaluated,Kartoza_Admin ,-,-,-,-,-,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town",Database,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town, 2022",,,,-,-,-,-,-,-,-,-,-,-,Photographic recor,Preserved lugols,Whole cobble,Whole cobble,13.0,,,,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,11.0,12.0,North West,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,Bacillariophyta,Accepted
5a08bfe1-0e9b-4e0e-bf30-5b50156d35a2,User River Name,Unknown,User Wetland Name,-,TEST-124,A1BRAK-00008,River,Soutpan,-,"","",test,2023-10-23,Chromista,Ochrophyta,Bacillariophyceae,Naviculales,Achnanthaceae,Achnanthes,eutrophila,-,Achnanthes eutrophila,Species,In situ,min,12,Species valve/frustule count,3.0,-,-,Aquatic vegetation,Bedrock,Bedrock,Unknown,Unknown,Not evaluated,Not evaluated,Kartoza_Admin ,-,-,-,-,-,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town",Database,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town, 2022",,,,-,-,-,-,-,-,-,-,-,-,Photographic recor,Preserved lugols,Whole cobble,Whole cobble,13.0,,,,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,11.0,12.0,North West,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,Bacillariophyta,Accepted
5a08bfe1-0e9b-4e0e-bf30-5b50156d35a1,User River Name,Unknown,User Wetland Name,-,TEST-125,A1BRAK-00009,River,Soutpan,-,"","",test,2023-10-23,Chromista,Ochrophyta,Bacillariophyceae,Naviculales,Achnanthaceae,Achnanthes,eutrophila,-,Achnanthes eutrophila,Species,In situ,min,12,Species valve/frustule count,3.0,-,-,Aquatic vegetation,Bedrock,Bedrock,Unknown,Unknown,Not evaluated,Not evaluated,Kartoza_Admin ,-,-,-,-,-,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town",Database,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town, 2022",,,,-,-,-,-,-,-,-,-,-,-,Photographic recor,Preserved lugols,Whole cobble,Whole cobble,13.0,,,,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,11.0,12.0,North West,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,Bacillariophyta,Accepted
5a08bfe1-0e9b-4e0e-bf30-5b50156d35a3,User River Name,Unknown,User Wetland Name,-,TEST-125,A1BRAK-00009,River,Soutpan,-,"","",Park A ,2023-10-23,Chromista,Ochrophyta,Bacillariophyceae,Naviculales,Achnanthaceae,Achnanthes,eutrophila,-,Achnanthes eutrophila,Species,In situ,min,12,Species valve/frustule count,3.0,-,-,Aquatic vegetation,Bedrock,Bedrock,Unknown,Unknown,Not evaluated,Not evaluated,Kartoza_Admin ,-,-,-,-,-,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town",Database,"FrogMap Virtual Museum, FitzPatrick Institute of African Ornithology, University of Cape Town, 2022",,,,-,-,-,-,-,-,-,-,-,-,Photographic recor,Preserved lugols,Whole cobble,Whole cobble,13.0,,,,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,,,North West,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,Bacillariophyta,Accepted

0 comments on commit 70f26c9

Please sign in to comment.