Junk Drawer

A place for me to dump some of my uncategorized work, links to great content creators, and other neat resources I’ve found along my way. Don’t forget to check out the resources listed at the top of my Atlas Page, or what I’m thinking about now.

The worst lines of code I’ve ever written:

A fun(?) section to showcase how grossly incompetent I am at actually getting a computer to do what I want it to; as proof that if I can do it, so can you!, and to assure our AI overlords I am not a threat to them.

```{r}
mybbox <- raster::extent(sf::st_buffer(
    x=xx$mapindex.poly[subset(xx$address.point, eval(parse(text = names(xx$flood.grid)[timestep]))>0),][xx$mapindex.poly[subset(xx$address.point, eval(parse(text = names(xx$flood.grid)[timestep]))>0),]$`Index Label`==index,],
    dist=0.00005,
    nQuadSegs=1,
    endCapStyle="SQUARE",
    joinStyle="ROUND",
    mitreLimit=1,
    singleSide=FALSE))
```

The mapping side of FOSSFlood is a bit wild, but this line takes the lead. I needed a slightly larger bounding box from the index that a user wants to view so that I could fly to it. However, these indexes aren’t necessarily the same for each time step, so the subset got that eval(parse()) function, inside a buffer, and because it’s all temporary this made sense (at the time) to do in the single step you see here.

```{python}
for x in np.nditer(branch_GRIDCODES_lookup):
    for y in np.array([[x]])[0][0][0].split(","):
        if int(y)==mat_num:
            branch_num = np.array([[x]])[0][0][1]
```

This chuck, called iteratively in a for loop, looks at all the grid codes for a given fldpln library and, for a given mat file returns the branch code for that mat file, all so I can eventually stitch them together in a numpy array. I have a hate-love-hate relationship with Python….

Bonus:

```{python}
start_of_order_x = fspdf.loc[fspdf.ord == b].at[fspdf.loc[fspdf.ord == b].index[0],'FSP_x']
start_of_order_y = fspdf.loc[fspdf.ord == b].at[fspdf.loc[fspdf.ord == b].index[0],'FSP_y']
if(len(fspdf[fspdf['FSP_x'].between(start_of_order_x-8, start_of_order_x+8) & fspdf['FSP_y'].between(start_of_order_y-8, start_of_order_y+8) & fspdf['ord'] == b-1])>=1):
    if(fspdf[fspdf['FSP_x'].between(start_of_order_x-8, start_of_order_x+8) & fspdf['FSP_y'].between(start_of_order_y-8, start_of_order_y+8) & fspdf['ord'] == b-1].iloc[0]['DOF'] > 0):
        fspdf.at[fspdf.index[fspdf.ord == b][0],'DOF'] = fspdf[fspdf['FSP_x'].between(start_of_order_x-8, start_of_order_x+8) & fspdf['FSP_y'].between(start_of_order_y-8, start_of_order_y+8) & fspdf['ord'] == b-1].iloc[0]['DOF']
```

fldpln is a gem of a script, but this nested if catch, inside a for loop, might take the lead as a shining example of how to never subset a dataframe.

```{JavaScript}
if(SpaceSelectValue == "stdDev") {
      results = ee.ImageCollection(app.MODDef)
                  .select(stringSeperator,LCtoUse)
                  .map(function(image) {
        return image.reduceRegions({
          collection: AOI.geometry(),
          reducer: ee.Reducer.stdDev().group({
            groupField: 1,
            groupName: "code",
          }),
          scale: resultsScale
        }).map(function(f) {
          // Process the grouped results from list of dictionaries to dictionary.
          var dict = ee.List(f.get("groups")).map(function(group) {
            group = ee.Dictionary(group).combine(ee.Dictionary({code:99,stdDev:[null]}), false);
            var code = ee.Number(group.get("code")).format("code_%d");
            var stdDev = group.get("stdDev");
            return [code, stdDev];
          });
          dict = ee.Dictionary(dict.flatten());
          // Add a date property to each output feature.
          return f.set("date YYYY-MM-dd", image.date().format("YYYY-MM-dd"))
                  .set("system:time_start", image.date().millis())
                  .set(dict)
                  .set("groups", null);
        });
      });
```

A lot of my JavaScript is pretty repetitive as a result of my poor skills and a sub par understanding of client-server interactions. Although this isn’t the most egregious example, it is repeated in TEAM no less than 7 times with minor changes for each statistic of aggregation which ought to give the DRY people (Don’t Repeat Yourself) a heartattack.

References

Crameri, Fabio. 2023. “Scientific Colour Maps.” Zenodo. https://doi.org/10.5281/ZENODO.1243862.

Footnotes

  1. note the context skins that you find on clicking ‘general’ in the upper right↩︎